fix(seed): generate random seed per-request if -1 is set (#1952)

* fix(seed): generate random seed per-request if -1 is set

Also update ci with new workflows and allow the aio tests to run with an
api key

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* docs(openvino): Add OpenVINO example

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
This commit is contained in:
Ettore Di Giacinto 2024-04-03 22:25:47 +02:00 committed by GitHub
parent 93cfec3c32
commit ff77d3bc22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 110 additions and 7 deletions

19
.github/labeler.yml vendored Normal file
View File

@ -0,0 +1,19 @@
enhancements:
- head-branch: ['^feature', 'feature']
kind/documentation:
- any:
- changed-files:
- any-glob-to-any-file: 'docs/*'
- changed-files:
- any-glob-to-any-file: '*.md'
examples:
- any:
- changed-files:
- any-glob-to-any-file: 'examples/*'
ci:
- any:
- changed-files:
- any-glob-to-any-file: '.github/*'

12
.github/workflows/labeler.yml vendored Normal file
View File

@ -0,0 +1,12 @@
name: "Pull Request Labeler"
on:
- pull_request_target
jobs:
labeler:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v5

27
.github/workflows/secscan.yaml vendored Normal file
View File

@ -0,0 +1,27 @@
name: "Security Scan"
# Run workflow each time code is pushed to your repository and on a schedule.
# The scheduled workflow runs every at 00:00 on Sunday UTC time.
on:
push:
schedule:
- cron: '0 0 * * 0'
jobs:
tests:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- name: Checkout Source
uses: actions/checkout@v3
- name: Run Gosec Security Scanner
uses: securego/gosec@master
with:
# we let the report trigger content trigger a failure using the GitHub Security features.
args: '-no-fail -fmt sarif -out results.sarif ./...'
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v2
with:
# Path to SARIF file relative to the root of the repository
sarif_file: results.sarif

View File

@ -1,6 +1,7 @@
package backend package backend
import ( import (
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -33,12 +34,20 @@ func modelOpts(c config.BackendConfig, so *config.ApplicationConfig, opts []mode
return opts return opts
} }
func getSeed(c config.BackendConfig) int32 {
seed := int32(*c.Seed)
if seed == config.RAND_SEED {
seed = rand.Int31()
}
return seed
}
func gRPCModelOpts(c config.BackendConfig) *pb.ModelOptions { func gRPCModelOpts(c config.BackendConfig) *pb.ModelOptions {
b := 512 b := 512
if c.Batch != 0 { if c.Batch != 0 {
b = c.Batch b = c.Batch
} }
return &pb.ModelOptions{ return &pb.ModelOptions{
CUDA: c.CUDA || c.Diffusers.CUDA, CUDA: c.CUDA || c.Diffusers.CUDA,
SchedulerType: c.Diffusers.SchedulerType, SchedulerType: c.Diffusers.SchedulerType,
@ -54,7 +63,7 @@ func gRPCModelOpts(c config.BackendConfig) *pb.ModelOptions {
CLIPSkip: int32(c.Diffusers.ClipSkip), CLIPSkip: int32(c.Diffusers.ClipSkip),
ControlNet: c.Diffusers.ControlNet, ControlNet: c.Diffusers.ControlNet,
ContextSize: int32(*c.ContextSize), ContextSize: int32(*c.ContextSize),
Seed: int32(*c.Seed), Seed: getSeed(c),
NBatch: int32(b), NBatch: int32(b),
NoMulMatQ: c.NoMulMatQ, NoMulMatQ: c.NoMulMatQ,
DraftModel: c.DraftModel, DraftModel: c.DraftModel,
@ -129,7 +138,7 @@ func gRPCPredictOpts(c config.BackendConfig, modelPath string) *pb.PredictOption
NKeep: int32(c.Keep), NKeep: int32(c.Keep),
Batch: int32(c.Batch), Batch: int32(c.Batch),
IgnoreEOS: c.IgnoreEOS, IgnoreEOS: c.IgnoreEOS,
Seed: int32(*c.Seed), Seed: getSeed(c),
FrequencyPenalty: float32(c.FrequencyPenalty), FrequencyPenalty: float32(c.FrequencyPenalty),
MLock: *c.MMlock, MLock: *c.MMlock,
MMap: *c.MMap, MMap: *c.MMap,

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"math/rand"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -20,6 +19,10 @@ import (
"github.com/charmbracelet/glamour" "github.com/charmbracelet/glamour"
) )
const (
RAND_SEED = -1
)
type BackendConfig struct { type BackendConfig struct {
schema.PredictionOptions `yaml:"parameters"` schema.PredictionOptions `yaml:"parameters"`
Name string `yaml:"name"` Name string `yaml:"name"`
@ -218,7 +221,7 @@ func (cfg *BackendConfig) SetDefaults(opts ...ConfigLoaderOption) {
if cfg.Seed == nil { if cfg.Seed == nil {
// random number generator seed // random number generator seed
defaultSeed := int(rand.Int31()) defaultSeed := RAND_SEED
cfg.Seed = &defaultSeed cfg.Seed = &defaultSeed
} }

View File

@ -304,6 +304,7 @@ The backend will automatically download the required files in order to run the m
| Type | Description | | Type | Description |
| --- | --- | | --- | --- |
| `AutoModelForCausalLM` | `AutoModelForCausalLM` is a model that can be used to generate sequences. | | `AutoModelForCausalLM` | `AutoModelForCausalLM` is a model that can be used to generate sequences. |
| `OVModelForCausalLM` | for OpenVINO models |
| N/A | Defaults to `AutoModel` | | N/A | Defaults to `AutoModel` |
@ -325,3 +326,34 @@ curl http://localhost:8080/v1/completions -H "Content-Type: application/json" -d
"temperature": 0.1, "top_p": 0.1 "temperature": 0.1, "top_p": 0.1
}' }'
``` ```
#### Examples
##### OpenVINO
A model configuration file for openvion and starling model:
```yaml
name: starling-openvino
backend: transformers
parameters:
model: fakezeta/Starling-LM-7B-beta-openvino-int8
context_size: 8192
threads: 6
f16: true
type: OVModelForCausalLM
stopwords:
- <|end_of_turn|>
- <|endoftext|>
prompt_cache_path: "cache"
prompt_cache_all: true
template:
chat_message: |
{{if eq .RoleName "system"}}{{.Content}}<|end_of_turn|>{{end}}{{if eq .RoleName "assistant"}}<|end_of_turn|>GPT4 Correct Assistant: {{.Content}}<|end_of_turn|>{{end}}{{if eq .RoleName "user"}}GPT4 Correct User: {{.Content}}{{end}}
chat: |
{{.Input}}<|end_of_turn|>GPT4 Correct Assistant:
completion: |
{{.Input}}
```

View File

@ -23,6 +23,7 @@ var containerImageTag = os.Getenv("LOCALAI_IMAGE_TAG")
var modelsDir = os.Getenv("LOCALAI_MODELS_DIR") var modelsDir = os.Getenv("LOCALAI_MODELS_DIR")
var apiPort = os.Getenv("LOCALAI_API_PORT") var apiPort = os.Getenv("LOCALAI_API_PORT")
var apiEndpoint = os.Getenv("LOCALAI_API_ENDPOINT") var apiEndpoint = os.Getenv("LOCALAI_API_ENDPOINT")
var apiKey = os.Getenv("LOCALAI_API_KEY")
func TestLocalAI(t *testing.T) { func TestLocalAI(t *testing.T) {
RegisterFailHandler(Fail) RegisterFailHandler(Fail)
@ -38,11 +39,11 @@ var _ = BeforeSuite(func() {
var defaultConfig openai.ClientConfig var defaultConfig openai.ClientConfig
if apiEndpoint == "" { if apiEndpoint == "" {
startDockerImage() startDockerImage()
defaultConfig = openai.DefaultConfig("") defaultConfig = openai.DefaultConfig(apiKey)
defaultConfig.BaseURL = "http://localhost:" + apiPort + "/v1" defaultConfig.BaseURL = "http://localhost:" + apiPort + "/v1"
} else { } else {
fmt.Println("Default ", apiEndpoint) fmt.Println("Default ", apiEndpoint)
defaultConfig = openai.DefaultConfig("") defaultConfig = openai.DefaultConfig(apiKey)
defaultConfig.BaseURL = apiEndpoint defaultConfig.BaseURL = apiEndpoint
} }