2023-07-14 23:19:43 +00:00
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
"github.com/go-skynet/LocalAI/core/config"
|
2023-07-14 23:19:43 +00:00
|
|
|
pb "github.com/go-skynet/LocalAI/pkg/grpc/proto"
|
2024-01-05 17:04:46 +00:00
|
|
|
model "github.com/go-skynet/LocalAI/pkg/model"
|
2023-07-14 23:19:43 +00:00
|
|
|
)
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
func modelOpts(c config.BackendConfig, so *config.ApplicationConfig, opts []model.Option) []model.Option {
|
|
|
|
if so.SingleBackend {
|
2023-08-18 23:49:33 +00:00
|
|
|
opts = append(opts, model.WithSingleActiveBackend())
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
if so.ParallelBackendRequests {
|
2023-11-16 07:20:05 +00:00
|
|
|
opts = append(opts, model.EnableParallelRequests)
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:49:33 +00:00
|
|
|
if c.GRPC.Attempts != 0 {
|
|
|
|
opts = append(opts, model.WithGRPCAttempts(c.GRPC.Attempts))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.GRPC.AttemptsSleepTime != 0 {
|
|
|
|
opts = append(opts, model.WithGRPCAttemptsDelay(c.GRPC.AttemptsSleepTime))
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +00:00
|
|
|
for k, v := range so.ExternalGRPCBackends {
|
2023-08-18 23:49:33 +00:00
|
|
|
opts = append(opts, model.WithExternalBackend(k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:19:53 +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
|
|
|
|
}
|
2023-08-09 06:38:51 +00:00
|
|
|
|
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-03-13 09:05:30 +00:00
|
|
|
F16Memory: *c.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-03-13 09:05:30 +00:00
|
|
|
ContextSize: int32(*c.ContextSize),
|
|
|
|
Seed: int32(*c.Seed),
|
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),
|
|
|
|
MMProj: c.MMProj,
|
|
|
|
YarnExtFactor: c.YarnExtFactor,
|
|
|
|
YarnAttnFactor: c.YarnAttnFactor,
|
|
|
|
YarnBetaFast: c.YarnBetaFast,
|
|
|
|
YarnBetaSlow: c.YarnBetaSlow,
|
|
|
|
NGQA: c.NGQA,
|
|
|
|
RMSNormEps: c.RMSNormEps,
|
2024-03-13 09:05:30 +00:00
|
|
|
MLock: *c.MMlock,
|
2024-03-01 21:48:53 +00:00
|
|
|
RopeFreqBase: c.RopeFreqBase,
|
|
|
|
RopeScaling: c.RopeScaling,
|
|
|
|
Type: c.ModelType,
|
|
|
|
RopeFreqScale: c.RopeFreqScale,
|
|
|
|
NUMA: c.NUMA,
|
|
|
|
Embeddings: c.Embeddings,
|
2024-03-13 09:05:30 +00:00
|
|
|
LowVRAM: *c.LowVRAM,
|
|
|
|
NGPULayers: int32(*c.NGPULayers),
|
|
|
|
MMap: *c.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-03-01 15:19:53 +00:00
|
|
|
func gRPCPredictOpts(c config.BackendConfig, modelPath string) *pb.PredictOptions {
|
2023-07-14 23:19:43 +00:00
|
|
|
promptCachePath := ""
|
|
|
|
if c.PromptCachePath != "" {
|
|
|
|
p := filepath.Join(modelPath, c.PromptCachePath)
|
|
|
|
os.MkdirAll(filepath.Dir(p), 0755)
|
|
|
|
promptCachePath = p
|
|
|
|
}
|
2024-03-13 09:05:30 +00:00
|
|
|
|
2023-07-14 23:19:43 +00:00
|
|
|
return &pb.PredictOptions{
|
2024-03-13 09:05:30 +00:00
|
|
|
Temperature: float32(*c.Temperature),
|
|
|
|
TopP: float32(*c.TopP),
|
2023-09-14 15:44:16 +00:00
|
|
|
NDraft: c.NDraft,
|
2024-03-13 09:05:30 +00:00
|
|
|
TopK: int32(*c.TopK),
|
|
|
|
Tokens: int32(*c.Maxtokens),
|
|
|
|
Threads: int32(*c.Threads),
|
2023-07-25 17:05:27 +00:00
|
|
|
PromptCacheAll: c.PromptCacheAll,
|
|
|
|
PromptCacheRO: c.PromptCacheRO,
|
|
|
|
PromptCachePath: promptCachePath,
|
2024-03-13 09:05:30 +00:00
|
|
|
F16KV: *c.F16,
|
|
|
|
DebugMode: *c.Debug,
|
2023-07-25 17:05:27 +00:00
|
|
|
Grammar: c.Grammar,
|
|
|
|
NegativePromptScale: c.NegativePromptScale,
|
|
|
|
RopeFreqBase: c.RopeFreqBase,
|
|
|
|
RopeFreqScale: c.RopeFreqScale,
|
|
|
|
NegativePrompt: c.NegativePrompt,
|
2024-03-13 09:05:30 +00:00
|
|
|
Mirostat: int32(*c.LLMConfig.Mirostat),
|
|
|
|
MirostatETA: float32(*c.LLMConfig.MirostatETA),
|
|
|
|
MirostatTAU: float32(*c.LLMConfig.MirostatTAU),
|
|
|
|
Debug: *c.Debug,
|
2023-07-25 17:05:27 +00:00
|
|
|
StopPrompts: c.StopWords,
|
|
|
|
Repeat: int32(c.RepeatPenalty),
|
|
|
|
NKeep: int32(c.Keep),
|
|
|
|
Batch: int32(c.Batch),
|
|
|
|
IgnoreEOS: c.IgnoreEOS,
|
2024-03-13 09:05:30 +00:00
|
|
|
Seed: int32(*c.Seed),
|
2023-07-25 17:05:27 +00:00
|
|
|
FrequencyPenalty: float32(c.FrequencyPenalty),
|
2024-03-13 09:05:30 +00:00
|
|
|
MLock: *c.MMlock,
|
|
|
|
MMap: *c.MMap,
|
2023-07-25 17:05:27 +00:00
|
|
|
MainGPU: c.MainGPU,
|
|
|
|
TensorSplit: c.TensorSplit,
|
|
|
|
TailFreeSamplingZ: float32(c.TFZ),
|
|
|
|
TypicalP: float32(c.TypicalP),
|
2023-07-14 23:19:43 +00:00
|
|
|
}
|
|
|
|
}
|