Apply suggestions from code review

Co-authored-by: DannyDaemonic <DannyDaemonic@gmail.com>
This commit is contained in:
jon-chuang 2023-04-30 18:10:08 +08:00 committed by GitHub
parent 4a98a0f21a
commit 710c4bbdbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 17 deletions

View file

@ -35,11 +35,17 @@ int32_t get_num_physical_cores() {
std::ifstream cpuinfo("/proc/cpuinfo");
std::string line;
while (std::getline(cpuinfo, line)) {
if (line.find("cpu cores") != std::string::npos) {
line.erase(0, line.find(": ") + 2);
try {
return (int32_t) std::stoul(line);
} catch (std::invalid_argument& e) {} // Ignore if we could not parse
std::size_t pos = line.find("cpu cores");
if (pos != std::string::npos) {
pos = line.find(": ", pos);
if (pos != std::string::npos) {
try {
// Extract the number and return it
return static_cast<int32_t>(std::stoul(line.substr(pos + 2)));
} catch (const std::invalid_argument &) {
// Ignore if we could not parse
}
}
}
}
#elif defined(__APPLE__) && defined(__MACH__)
@ -54,10 +60,10 @@ int32_t get_num_physical_cores() {
return num_physical_cores;
}
#elif defined(_WIN32)
std::cerr << "WARNING: automatic calibration not supported on Windows. Defaulting to 4 threads.\n" << std::endl;
return 4;
//TODO: Implement
#endif
return -1;
unsigned int n_threads = std::thread::hardware_concurrency()
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
}
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
@ -234,14 +240,6 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
exit(1);
}
// Clip if not a valid number of threads
if (params.n_threads <= 0) {
std::cerr << "\nWARNING: Using number of physical cores as the default number of threads.\n\
If your chipset has efficient/performance cores, use the number of performance cores instead.\n" << std::endl;
int32_t physical_cores = get_num_physical_cores();
params.n_threads = std::max(1, physical_cores);
}
return true;
}

View file

@ -15,7 +15,7 @@
struct gpt_params {
int32_t seed = -1; // RNG seed
int32_t n_threads = 0;
int32_t n_threads = get_num_physical_cores();
int32_t n_predict = 128; // new tokens to predict
int32_t repeat_last_n = 64; // last n tokens to penalize
int32_t n_parts = -1; // amount of model parts (-1 = determine from model dimensions)