apply code review

This commit is contained in:
jon-chuang 2023-04-14 03:07:45 +08:00
parent 8694318c71
commit e0325353be
2 changed files with 22 additions and 16 deletions

View file

@ -13,13 +13,10 @@
#include <alloca.h> #include <alloca.h>
#endif #endif
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
#if defined (_WIN32) #if defined (_WIN32)
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#include <windows.h>
#pragma comment(lib,"kernel32.lib") #pragma comment(lib,"kernel32.lib")
extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle); extern "C" __declspec(dllimport) void* __stdcall GetStdHandle(unsigned long nStdHandle);
extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode); extern "C" __declspec(dllimport) int __stdcall GetConsoleMode(void* hConsoleHandle, unsigned long* lpMode);
@ -33,9 +30,7 @@ extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int
#define CP_UTF8 65001 #define CP_UTF8 65001
#endif #endif
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) { int32_t get_num_physical_cores() {
// determine sensible default number of threads.
// std::thread::hardware_concurrency may not be equal to the number of cores, or may return 0.
#ifdef __linux__ #ifdef __linux__
std::ifstream cpuinfo("/proc/cpuinfo"); std::ifstream cpuinfo("/proc/cpuinfo");
std::string line; std::string line;
@ -43,25 +38,34 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
if (line.find("cpu cores") != std::string::npos) { if (line.find("cpu cores") != std::string::npos) {
line.erase(0, line.find(": ") + 2); line.erase(0, line.find(": ") + 2);
try { try {
params.n_threads = std::stoul(line); return (int32_t) std::stoul(line);
break;
} catch (std::invalid_argument& e) {} // Ignore if we could not parse } catch (std::invalid_argument& e) {} // Ignore if we could not parse
} }
} }
#elif defined(__APPLE__) && defined(__MACH__) #elif defined(__APPLE__) && defined(__MACH__)
int num_physical_cores; int num_physical_cores;
size_t len = sizeof(num_physical_cores); size_t len = sizeof(num_physical_cores);
int result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0); int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
if (result == 0) { if (result == 0) {
params.n_threads = num_physical_cores; return (int32_t) num_physical_cores;
} else {
int result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
if (result == 0) {
return (int32_t) num_physical_cores;
}
} }
#elif defined(_WIN32) || defined(_WIN64) #elif defined(_WIN32)
SYSTEM_INFO sysinfo; SYSTEM_INFO sysinfo;
GetNativeSystemInfo(&sysinfo); GetNativeSystemInfo(&sysinfo);
params.n_threads = sysinfo.dwNumberOfProcessors; return (in32_t) sysinfo.dwNumberOfProcessors;
#endif #endif
if (params.n_threads == 0) { return -1;
params.n_threads = std::max(1, (int32_t) std::thread::hardware_concurrency()); }
bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
// Clip if not a valid number of threads
if (params.n_threads <= 0) {
params.n_threads = std::max(1, std::min(8, (int32_t) std::thread::hardware_concurrency()));
} }
bool invalid_param = false; bool invalid_param = false;

View file

@ -13,9 +13,11 @@
// CLI argument parsing // CLI argument parsing
// //
int32_t get_num_physical_cores();
struct gpt_params { struct gpt_params {
int32_t seed = -1; // RNG seed int32_t seed = -1; // RNG seed
int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency()); int32_t n_threads = get_num_physical_cores(); // (if <= 0, = clip(num_logical_cores, 1, 8))
int32_t n_predict = 128; // new tokens to predict int32_t n_predict = 128; // new tokens to predict
int32_t repeat_last_n = 64; // last n tokens to penalize 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) int32_t n_parts = -1; // amount of model parts (-1 = determine from model dimensions)