From 710c4bbdbfc7a7ada9954bb1bdb0ee466f7db4e3 Mon Sep 17 00:00:00 2001 From: jon-chuang <9093549+jon-chuang@users.noreply.github.com> Date: Sun, 30 Apr 2023 18:10:08 +0800 Subject: [PATCH] Apply suggestions from code review Co-authored-by: DannyDaemonic --- examples/common.cpp | 30 ++++++++++++++---------------- examples/common.h | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/common.cpp b/examples/common.cpp index cd80b1b05..073097b58 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -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(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; } diff --git a/examples/common.h b/examples/common.h index b3d2c5033..df939ed28 100644 --- a/examples/common.h +++ b/examples/common.h @@ -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)