From bdd6769b2dcbb8f43cd9f51a53f7f8d05ffc83f3 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Tue, 4 Jun 2024 15:23:29 +0200 Subject: [PATCH] feat(default): use number of physical cores as default (#2483) Signed-off-by: Ettore Di Giacinto --- core/cli/run.go | 2 +- core/config/application_config.go | 5 ++++- pkg/xsysinfo/cpu.go | 7 +++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/cli/run.go b/core/cli/run.go index 6c41f63b..009f5315 100644 --- a/core/cli/run.go +++ b/core/cli/run.go @@ -37,7 +37,7 @@ type RunCMD struct { PreloadModelsConfig string `env:"LOCALAI_PRELOAD_MODELS_CONFIG,PRELOAD_MODELS_CONFIG" help:"A List of models to apply at startup. Path to a YAML config file" group:"models"` F16 bool `name:"f16" env:"LOCALAI_F16,F16" help:"Enable GPU acceleration" group:"performance"` - Threads int `env:"LOCALAI_THREADS,THREADS" short:"t" default:"4" help:"Number of threads used for parallel computation. Usage of the number of physical cores in the system is suggested" group:"performance"` + Threads int `env:"LOCALAI_THREADS,THREADS" short:"t" help:"Number of threads used for parallel computation. Usage of the number of physical cores in the system is suggested" group:"performance"` ContextSize int `env:"LOCALAI_CONTEXT_SIZE,CONTEXT_SIZE" default:"512" help:"Default context size for models" group:"performance"` Address string `env:"LOCALAI_ADDRESS,ADDRESS" default:":8080" help:"Bind address for the API server" group:"api"` diff --git a/core/config/application_config.go b/core/config/application_config.go index 398418ad..a71b6223 100644 --- a/core/config/application_config.go +++ b/core/config/application_config.go @@ -7,6 +7,7 @@ import ( "time" "github.com/go-skynet/LocalAI/pkg/gallery" + "github.com/go-skynet/LocalAI/pkg/xsysinfo" "github.com/rs/zerolog/log" ) @@ -59,7 +60,6 @@ func NewApplicationConfig(o ...AppOption) *ApplicationConfig { opt := &ApplicationConfig{ Context: context.Background(), UploadLimitMB: 15, - Threads: 1, ContextSize: 512, Debug: true, } @@ -213,6 +213,9 @@ func WithUploadLimitMB(limit int) AppOption { func WithThreads(threads int) AppOption { return func(o *ApplicationConfig) { + if threads == 0 { // 0 is not allowed + threads = xsysinfo.CPUPhysicalCores() + } o.Threads = threads } } diff --git a/pkg/xsysinfo/cpu.go b/pkg/xsysinfo/cpu.go index e3066b56..b1ff20fe 100644 --- a/pkg/xsysinfo/cpu.go +++ b/pkg/xsysinfo/cpu.go @@ -36,3 +36,10 @@ func CPUCapabilities() ([]string, error) { func HasCPUCaps(ids ...cpuid.FeatureID) bool { return cpuid.CPU.Supports(ids...) } + +func CPUPhysicalCores() int { + if cpuid.CPU.PhysicalCores == 0 { + return 1 + } + return cpuid.CPU.PhysicalCores +}