2023-07-14 23:19:43 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
2024-04-03 20:25:47 +00:00
|
|
|
"math/rand"
|
2023-07-14 23:19:43 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-06-23 08:24:36 +00:00
|
|
|
"github.com/mudler/LocalAI/core/config"
|
|
|
|
pb "github.com/mudler/LocalAI/pkg/grpc/proto"
|
|
|
|
"github.com/mudler/LocalAI/pkg/model"
|
2024-04-26 08:34:31 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-07-14 23:19:43 +00:00
|
|
|
)
|
|
|
|
|
2024-10-02 06:55:58 +00:00
|
|
|
func ModelOptions(c config.BackendConfig, so *config.ApplicationConfig, opts []model.Option) []model.Option {
|
|
|
|
name := c.Name
|
|
|
|
if name == "" {
|
|
|
|
name = c.Model
|
|
|
|
}
|
|
|
|
|
|
|
|
defOpts := []model.Option{
|
|
|
|
model.WithBackendString(c.Backend),
|
|
|
|
model.WithModel(c.Model),
|
|
|
|
model.WithAssetDir(so.AssetsDestination),
|
|
|
|
model.WithContext(so.Context),
|
|
|
|
model.WithModelID(name),
|
|
|
|
}
|
|
|
|
|
|
|
|
threads := 1
|
|
|
|
|
|
|
|
if c.Threads != nil {
|
|
|
|
threads = *c.Threads
|
|
|
|
}
|
|
|
|
|
|
|
|
if so.Threads != 0 {
|
|
|
|
threads = so.Threads
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Threads = &threads
|
|
|
|
|
|
|
|
grpcOpts := grpcModelOpts(c)
|
|
|
|
defOpts = append(defOpts, model.WithLoadGRPCLoadModelOpts(grpcOpts))
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
if so.SingleBackend {
|
2024-10-02 06:55:58 +00:00
|
|
|
defOpts = append(defOpts, model.WithSingleActiveBackend())
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
if so.ParallelBackendRequests {
|
2024-10-02 06:55:58 +00:00
|
|
|
defOpts = append(defOpts, model.EnableParallelRequests)
|
2023-11-16 07:20:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
if c.GRPC.Attempts != 0 {
|
2024-10-02 06:55:58 +00:00
|
|
|
defOpts = append(defOpts, model.WithGRPCAttempts(c.GRPC.Attempts))
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
if c.GRPC.AttemptsSleepTime != 0 {
|
2024-10-02 06:55:58 +00:00
|
|
|
defOpts = append(defOpts, model.WithGRPCAttemptsDelay(c.GRPC.AttemptsSleepTime))
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
for k, v := range so.ExternalGRPCBackends {
|
2024-10-02 06:55:58 +00:00
|
|
|
defOpts = append(defOpts, model.WithExternalBackend(k, v))
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 06:55:58 +00:00
|
|
|
return append(defOpts, opts...)
|
2023-08-18 23:49:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
func getSeed(c config.BackendConfig) int32 {
|
2024-10-02 06:55:58 +00:00
|
|
|
var seed int32 = config.RAND_SEED
|
|
|
|
|
|
|
|
if c.Seed != nil {
|
|
|
|
seed = int32(*c.Seed)
|
|
|
|
}
|
|
|
|
|
2024-04-03 20:25:47 +00:00
|
|
|
if seed == config.RAND_SEED {
|
|
|
|
seed = rand.Int31()
|
|
|
|
}
|
|
|
|
|
|
|
|
return seed
|
|
|
|
}
|
|
|
|
|
2024-10-02 06:55:58 +00:00
|
|
|
func grpcModelOpts(c config.BackendConfig) *pb.ModelOptions {
|
2023-07-14 23:19:43 +00:00
|
|
|
b := 512
|
|
|
|
if c.Batch != 0 {
|
|
|
|
b = c.Batch
|
|
|
|
}
|
2024-10-02 06:55:58 +00:00
|
|
|
|
|
|
|
f16 := false
|
|
|
|
if c.F16 != nil {
|
|
|
|
f16 = *c.F16
|
|
|
|
}
|
|
|
|
|
|
|
|
embeddings := false
|
|
|
|
if c.Embeddings != nil {
|
|
|
|
embeddings = *c.Embeddings
|
|
|
|
}
|
|
|
|
|
|
|
|
lowVRAM := false
|
|
|
|
if c.LowVRAM != nil {
|
|
|
|
lowVRAM = *c.LowVRAM
|
|
|
|
}
|
|
|
|
|
|
|
|
mmap := false
|
|
|
|
if c.MMap != nil {
|
|
|
|
mmap = *c.MMap
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxSize := 1024
|
|
|
|
if c.ContextSize != nil {
|
|
|
|
ctxSize = *c.ContextSize
|
|
|
|
}
|
|
|
|
|
|
|
|
mmlock := false
|
|
|
|
if c.MMlock != nil {
|
|
|
|
mmlock = *c.MMlock
|
|
|
|
}
|
|
|
|
|
|
|
|
nGPULayers := 9999999
|
|
|
|
if c.NGPULayers != nil {
|
|
|
|
nGPULayers = *c.NGPULayers
|
|
|
|
}
|
|
|
|
|
2023-08-15 23:11:42 +00:00
|
|
|
return &pb.ModelOptions{
|
2024-03-07 13:37:45 +00:00
|
|
|
CUDA: c.CUDA || c.Diffusers.CUDA,
|
|
|
|
SchedulerType: c.Diffusers.SchedulerType,
|
|
|
|
PipelineType: c.Diffusers.PipelineType,
|
|
|
|
CFGScale: c.Diffusers.CFGScale,
|
|
|
|
LoraAdapter: c.LoraAdapter,
|
|
|
|
LoraScale: c.LoraScale,
|
2024-10-02 06:55:58 +00:00
|
|
|
F16Memory: f16,
|
2024-03-07 13:37:45 +00:00
|
|
|
LoraBase: c.LoraBase,
|
|
|
|
IMG2IMG: c.Diffusers.IMG2IMG,
|
|
|
|
CLIPModel: c.Diffusers.ClipModel,
|
|
|
|
CLIPSubfolder: c.Diffusers.ClipSubFolder,
|
|
|
|
CLIPSkip: int32(c.Diffusers.ClipSkip),
|
|
|
|
ControlNet: c.Diffusers.ControlNet,
|
2024-10-02 06:55:58 +00:00
|
|
|
ContextSize: int32(ctxSize),
|
2024-04-03 20:25:47 +00:00
|
|
|
Seed: getSeed(c),
|
2024-03-01 21:48:53 +00:00
|
|
|
NBatch: int32(b),
|
|
|
|
NoMulMatQ: c.NoMulMatQ,
|
|
|
|
DraftModel: c.DraftModel,
|
|
|
|
AudioPath: c.VallE.AudioPath,
|
|
|
|
Quantization: c.Quantization,
|
|
|
|
GPUMemoryUtilization: c.GPUMemoryUtilization,
|
|
|
|
TrustRemoteCode: c.TrustRemoteCode,
|
|
|
|
EnforceEager: c.EnforceEager,
|
|
|
|
SwapSpace: int32(c.SwapSpace),
|
|
|
|
MaxModelLen: int32(c.MaxModelLen),
|
2024-04-20 14:37:02 +00:00
|
|
|
TensorParallelSize: int32(c.TensorParallelSize),
|
2024-03-01 21:48:53 +00:00
|
|
|
MMProj: c.MMProj,
|
2024-05-13 17:07:51 +00:00
|
|
|
FlashAttention: c.FlashAttention,
|
|
|
|
NoKVOffload: c.NoKVOffloading,
|
2024-03-01 21:48:53 +00:00
|
|
|
YarnExtFactor: c.YarnExtFactor,
|
|
|
|
YarnAttnFactor: c.YarnAttnFactor,
|
|
|
|
YarnBetaFast: c.YarnBetaFast,
|
|
|
|
YarnBetaSlow: c.YarnBetaSlow,
|
|
|
|
NGQA: c.NGQA,
|
|
|
|
RMSNormEps: c.RMSNormEps,
|
2024-10-02 06:55:58 +00:00
|
|
|
MLock: mmlock,
|
2024-03-01 21:48:53 +00:00
|
|
|
RopeFreqBase: c.RopeFreqBase,
|
|
|
|
RopeScaling: c.RopeScaling,
|
|
|
|
Type: c.ModelType,
|
|
|
|
RopeFreqScale: c.RopeFreqScale,
|
|
|
|
NUMA: c.NUMA,
|
2024-10-02 06:55:58 +00:00
|
|
|
Embeddings: embeddings,
|
|
|
|
LowVRAM: lowVRAM,
|
|
|
|
NGPULayers: int32(nGPULayers),
|
|
|
|
MMap: mmap,
|
2024-03-01 21:48:53 +00:00
|
|
|
MainGPU: c.MainGPU,
|
2024-03-13 09:05:30 +00:00
|
|
|
Threads: int32(*c.Threads),
|
2024-03-01 21:48:53 +00:00
|
|
|
TensorSplit: c.TensorSplit,
|
2023-08-09 06:38:51 +00:00
|
|
|
// AutoGPTQ
|
|
|
|
ModelBaseName: c.AutoGPTQ.ModelBaseName,
|
|
|
|
Device: c.AutoGPTQ.Device,
|
|
|
|
UseTriton: c.AutoGPTQ.Triton,
|
|
|
|
UseFastTokenizer: c.AutoGPTQ.UseFastTokenizer,
|
2023-08-22 16:48:06 +00:00
|
|
|
// RWKV
|
|
|
|
Tokenizer: c.Tokenizer,
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 21:33:49 +00:00
|
|
|
func gRPCPredictOpts(c config.BackendConfig, modelPath string) *pb.PredictOptions {
|
2023-07-14 23:19:43 +00:00
|
|
|
promptCachePath := ""
|
2024-04-17 21:33:49 +00:00
|
|
|
if c.PromptCachePath != "" {
|
|
|
|
p := filepath.Join(modelPath, c.PromptCachePath)
|
2024-04-26 08:34:31 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(p), 0750)
|
|
|
|
if err == nil {
|
|
|
|
promptCachePath = p
|
|
|
|
} else {
|
|
|
|
log.Error().Err(err).Str("promptCachePath", promptCachePath).Msg("error creating prompt cache folder")
|
|
|
|
}
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
2024-03-13 09:05:30 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
return &pb.PredictOptions{
|
2024-04-17 21:33:49 +00:00
|
|
|
Temperature: float32(*c.Temperature),
|
|
|
|
TopP: float32(*c.TopP),
|
|
|
|
NDraft: c.NDraft,
|
|
|
|
TopK: int32(*c.TopK),
|
|
|
|
Tokens: int32(*c.Maxtokens),
|
|
|
|
Threads: int32(*c.Threads),
|
|
|
|
PromptCacheAll: c.PromptCacheAll,
|
|
|
|
PromptCacheRO: c.PromptCacheRO,
|
2023-07-25 17:05:27 +00:00
|
|
|
PromptCachePath: promptCachePath,
|
2024-04-17 21:33:49 +00:00
|
|
|
F16KV: *c.F16,
|
|
|
|
DebugMode: *c.Debug,
|
|
|
|
Grammar: c.Grammar,
|
|
|
|
NegativePromptScale: c.NegativePromptScale,
|
|
|
|
RopeFreqBase: c.RopeFreqBase,
|
|
|
|
RopeFreqScale: c.RopeFreqScale,
|
|
|
|
NegativePrompt: c.NegativePrompt,
|
|
|
|
Mirostat: int32(*c.LLMConfig.Mirostat),
|
|
|
|
MirostatETA: float32(*c.LLMConfig.MirostatETA),
|
|
|
|
MirostatTAU: float32(*c.LLMConfig.MirostatTAU),
|
|
|
|
Debug: *c.Debug,
|
|
|
|
StopPrompts: c.StopWords,
|
2024-06-26 12:58:50 +00:00
|
|
|
Repeat: int32(c.RepeatLastN),
|
|
|
|
FrequencyPenalty: float32(c.FrequencyPenalty),
|
|
|
|
PresencePenalty: float32(c.PresencePenalty),
|
|
|
|
Penalty: float32(c.RepeatPenalty),
|
2024-04-17 21:33:49 +00:00
|
|
|
NKeep: int32(c.Keep),
|
|
|
|
Batch: int32(c.Batch),
|
|
|
|
IgnoreEOS: c.IgnoreEOS,
|
|
|
|
Seed: getSeed(c),
|
|
|
|
MLock: *c.MMlock,
|
|
|
|
MMap: *c.MMap,
|
|
|
|
MainGPU: c.MainGPU,
|
|
|
|
TensorSplit: c.TensorSplit,
|
|
|
|
TailFreeSamplingZ: float32(*c.TFZ),
|
|
|
|
TypicalP: float32(*c.TypicalP),
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
}
|