Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f0bb2556e | ||
|
|
fdd89d6580 | ||
|
|
1934efb0b5 | ||
|
|
b83edeb9ef | ||
|
|
7886758053 | ||
|
|
d297852b9e | ||
|
|
cecb89880e | ||
|
|
0292c90ca1 | ||
|
|
617c7dc6b9 | ||
|
|
542b79dacf | ||
|
|
0a25b3bd31 |
@@ -0,0 +1,89 @@
|
|||||||
|
name: Build CUDA image (fork)
|
||||||
|
|
||||||
|
# Builds this fork's llama-swap (serial scheduler + embedded UI) from source and
|
||||||
|
# layers it on a pinned llama.cpp CUDA server base, then pushes to the Gitea
|
||||||
|
# container registry, e.g. gitea.stevedudenhoeffer.com/steve/llama-swap:v230-cuda-b9821
|
||||||
|
#
|
||||||
|
# Requires repo secrets: REGISTRY_USER, REGISTRY_PASSWORD (push to the registry).
|
||||||
|
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
llama_swap_version:
|
||||||
|
description: "llama-swap version label (image tag prefix)"
|
||||||
|
required: false
|
||||||
|
default: "v230"
|
||||||
|
llamacpp_build:
|
||||||
|
description: "llama.cpp CUDA server build (base image tag suffix)"
|
||||||
|
required: false
|
||||||
|
default: "b9821"
|
||||||
|
# Any change that lands in the shipped binary or image kicks off a fresh
|
||||||
|
# build (source, UI, deps, build definition). Docs/config-example edits
|
||||||
|
# don't.
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
paths:
|
||||||
|
- ".gitea/workflows/build-cuda-image.yml"
|
||||||
|
- "docker/fork-cuda.Containerfile"
|
||||||
|
- "**/*.go"
|
||||||
|
- "go.mod"
|
||||||
|
- "go.sum"
|
||||||
|
- "ui-svelte/**"
|
||||||
|
- "llama-swap.go"
|
||||||
|
|
||||||
|
env:
|
||||||
|
REGISTRY: gitea.stevedudenhoeffer.com
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Compute image metadata
|
||||||
|
id: meta
|
||||||
|
run: |
|
||||||
|
LS_VER="${{ inputs.llama_swap_version || 'v230' }}"
|
||||||
|
LCPP="${{ inputs.llamacpp_build || 'b9821' }}"
|
||||||
|
SHORT_SHA="$(echo '${{ github.sha }}' | cut -c1-7)"
|
||||||
|
IMAGE="${REGISTRY}/${{ github.repository }}"
|
||||||
|
# Three tags per build: the version label (mutable, human-facing),
|
||||||
|
# an explicit moving main-cuda tag, and an immutable sha tag so a
|
||||||
|
# deployment can always pin the exact build.
|
||||||
|
{
|
||||||
|
echo "image=${IMAGE}"
|
||||||
|
echo "tag=${LS_VER}-cuda-${LCPP}"
|
||||||
|
echo "tags=${IMAGE}:${LS_VER}-cuda-${LCPP},${IMAGE}:main-cuda-${LCPP},${IMAGE}:sha-${SHORT_SHA}"
|
||||||
|
echo "base_tag=server-cuda-${LCPP}"
|
||||||
|
echo "ls_version=${LS_VER}"
|
||||||
|
echo "build_date=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||||
|
} >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
|
- name: Log in to Gitea registry
|
||||||
|
uses: docker/login-action@v3
|
||||||
|
with:
|
||||||
|
registry: ${{ env.REGISTRY }}
|
||||||
|
username: ${{ secrets.REGISTRY_USER }}
|
||||||
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: docker/fork-cuda.Containerfile
|
||||||
|
push: true
|
||||||
|
provenance: false
|
||||||
|
build-args: |
|
||||||
|
BASE_TAG=${{ steps.meta.outputs.base_tag }}
|
||||||
|
LS_VERSION=${{ steps.meta.outputs.ls_version }}
|
||||||
|
GIT_HASH=${{ github.sha }}
|
||||||
|
BUILD_DATE=${{ steps.meta.outputs.build_date }}
|
||||||
|
tags: ${{ steps.meta.outputs.tags }}
|
||||||
|
|
||||||
|
- name: Summary
|
||||||
|
run: |
|
||||||
|
echo "Pushed ${{ steps.meta.outputs.tags }}" >> "$GITHUB_STEP_SUMMARY"
|
||||||
@@ -5,16 +5,14 @@ llama-swap is a light weight, transparent proxy server that provides automatic m
|
|||||||
## Tech stack
|
## Tech stack
|
||||||
|
|
||||||
- golang
|
- golang
|
||||||
- typescript, vite and svelt5 for UI (located in ui/)
|
- typescript, vite and svelte 5 for UI (located in ui-svelte/)
|
||||||
|
|
||||||
## Workflow Tasks
|
## Workflow Tasks
|
||||||
|
|
||||||
- when summarizing changes only include details that require further action
|
- when summarizing changes only include details that require further action
|
||||||
- just say "Done." when there is no further action
|
|
||||||
- use the github CLI `gh` to create pull requests and work with github
|
|
||||||
- Rules for creating pull requests:
|
- Rules for creating pull requests:
|
||||||
- keep them short and focused on changes.
|
- keep them short and focused on changes
|
||||||
- never include a test plan
|
- skip the test plan
|
||||||
- write the summary using the same style rules as commit message
|
- write the summary using the same style rules as commit message
|
||||||
|
|
||||||
## Testing
|
## Testing
|
||||||
@@ -30,7 +28,7 @@ llama-swap is a light weight, transparent proxy server that provides automatic m
|
|||||||
### Commit message example format:
|
### Commit message example format:
|
||||||
|
|
||||||
```
|
```
|
||||||
proxy: add new feature
|
internal/server: add new feature
|
||||||
|
|
||||||
Add new feature that implements functionality X and Y.
|
Add new feature that implements functionality X and Y.
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -601,10 +601,11 @@
|
|||||||
"use": {
|
"use": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": [
|
"enum": [
|
||||||
|
"serial",
|
||||||
"fifo"
|
"fifo"
|
||||||
],
|
],
|
||||||
"default": "fifo",
|
"default": "serial",
|
||||||
"description": "Scheduler to use. Only 'fifo' is currently supported."
|
"description": "Scheduler to use. 'serial' (default on this fork): strict one-model-at-a-time, requests run in exact arrival order, switching models evicts every other model first. 'fifo': throughput-oriented, batches same-model requests and allows parallel/co-resident models."
|
||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|||||||
+13
-3
@@ -556,11 +556,21 @@ routing:
|
|||||||
# expands to: [L]
|
# expands to: [L]
|
||||||
full: "L"
|
full: "L"
|
||||||
|
|
||||||
# scheduler: how queued requests are ordered.
|
# scheduler: how queued requests are ordered and run.
|
||||||
# The default and only valid scheduler is "fifo"
|
# - optional, default on this fork: "serial"
|
||||||
|
# - valid values:
|
||||||
|
# - "serial": strict one-model-at-a-time. Requests run in exact arrival
|
||||||
|
# order; only one request runs at a time; switching to a different model
|
||||||
|
# evicts every other running model first so a single model occupies memory
|
||||||
|
# at a time. This ignores group/matrix co-residency entirely. The "fifo"
|
||||||
|
# settings below (priority) do not apply.
|
||||||
|
# - "fifo": throughput-oriented. Same-model requests are batched to reduce
|
||||||
|
# swaps and a model serves up to its concurrencyLimit in parallel; models
|
||||||
|
# in non-exclusive groups can run concurrently. Requests may be reordered.
|
||||||
scheduler:
|
scheduler:
|
||||||
use: fifo
|
use: serial
|
||||||
settings:
|
settings:
|
||||||
|
# fifo settings only apply when use: fifo
|
||||||
fifo:
|
fifo:
|
||||||
# priority: a dictionary of model ID -> priority
|
# priority: a dictionary of model ID -> priority
|
||||||
# - optional, default: empty dictionary
|
# - optional, default: empty dictionary
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
# Build a CUDA llama-swap image FROM THIS FORK's source (includes the serial
|
||||||
|
# scheduler) and layer it on a pinned llama.cpp CUDA server base. Produces e.g.:
|
||||||
|
# gitea.stevedudenhoeffer.com/steve/llama-swap:v230-cuda-b9821
|
||||||
|
#
|
||||||
|
# BASE_TAG selects the llama.cpp CUDA runtime + llama-server build, e.g.
|
||||||
|
# "server-cuda-b9821". The llama-swap binary (with the embedded Svelte UI) is
|
||||||
|
# compiled from the repo at build time, so no GitHub release is required.
|
||||||
|
#
|
||||||
|
# Build context is the repo root:
|
||||||
|
# docker build -f docker/fork-cuda.Containerfile \
|
||||||
|
# --build-arg BASE_TAG=server-cuda-b9821 -t llama-swap:v230-cuda-b9821 .
|
||||||
|
|
||||||
|
ARG BASE_IMAGE=ghcr.io/ggml-org/llama.cpp
|
||||||
|
ARG BASE_TAG=server-cuda-b9821
|
||||||
|
|
||||||
|
# ---- Stage 1: build the Svelte UI (embedded into the binary) ----
|
||||||
|
FROM node:22-bookworm-slim AS ui
|
||||||
|
WORKDIR /src/ui-svelte
|
||||||
|
# Install deps first for layer caching. .npmrc carries legacy-peer-deps=true,
|
||||||
|
# which the project relies on (tailwind/vite peer ranges), so copy it before
|
||||||
|
# npm ci or the strict resolver fails with ERESOLVE.
|
||||||
|
COPY ui-svelte/package.json ui-svelte/package-lock.json ui-svelte/.npmrc ./
|
||||||
|
RUN npm ci
|
||||||
|
COPY ui-svelte/ ./
|
||||||
|
# `npm run build` is `vite build --emptyOutDir`; vite.config.ts writes to
|
||||||
|
# ../internal/server/ui_dist, which //go:embed picks up in the next stage.
|
||||||
|
RUN mkdir -p /src/internal/server && npm run build
|
||||||
|
|
||||||
|
# ---- Stage 2: build the llama-swap binary with the embedded UI ----
|
||||||
|
FROM golang:1.26-bookworm AS build
|
||||||
|
WORKDIR /src
|
||||||
|
# Cache modules independently of source churn.
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
COPY . .
|
||||||
|
# Overlay the freshly built UI so //go:embed ui_dist ships the real assets
|
||||||
|
# instead of the committed placeholder.
|
||||||
|
COPY --from=ui /src/internal/server/ui_dist/ ./internal/server/ui_dist/
|
||||||
|
ARG LS_VERSION=v230
|
||||||
|
ARG GIT_HASH=unknown
|
||||||
|
ARG BUILD_DATE=unknown
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||||
|
-ldflags="-X main.version=${LS_VERSION} -X main.commit=${GIT_HASH} -X main.date=${BUILD_DATE}" \
|
||||||
|
-o /out/llama-swap .
|
||||||
|
|
||||||
|
# ---- Stage 3: runtime image on the pinned llama.cpp CUDA base ----
|
||||||
|
FROM ${BASE_IMAGE}:${BASE_TAG}
|
||||||
|
|
||||||
|
# Run as root by default to match the upstream `vNNN-cuda-bNNNN` (non-suffixed)
|
||||||
|
# image that ragnaros pulls today: it needs root to reach the mounted docker
|
||||||
|
# socket for container-backed models (sd-server). Override UID/GID at build time
|
||||||
|
# for a non-root variant.
|
||||||
|
ARG UID=0
|
||||||
|
ARG GID=0
|
||||||
|
ARG USER_HOME=/root
|
||||||
|
ENV HOME=$USER_HOME
|
||||||
|
|
||||||
|
RUN set -eux; \
|
||||||
|
if [ "$UID" -ne 0 ]; then \
|
||||||
|
if [ "$GID" -ne 0 ]; then groupadd --system --gid "$GID" app; fi; \
|
||||||
|
useradd --system --uid "$UID" --gid "$GID" --home "$USER_HOME" app; \
|
||||||
|
fi; \
|
||||||
|
mkdir --parents "$HOME" /app; \
|
||||||
|
chown --recursive "$UID:$GID" "$HOME" /app
|
||||||
|
|
||||||
|
COPY --from=build --chown=$UID:$GID /out/llama-swap /app/llama-swap
|
||||||
|
COPY --chown=$UID:$GID docker/config.example.yaml /app/config.yaml
|
||||||
|
|
||||||
|
USER $UID:$GID
|
||||||
|
WORKDIR /app
|
||||||
|
ENV PATH="/app:${PATH}"
|
||||||
|
|
||||||
|
HEALTHCHECK CMD curl -f http://localhost:8080/ || exit 1
|
||||||
|
ENTRYPOINT [ "/app/llama-swap", "-config", "/app/config.yaml" ]
|
||||||
@@ -277,7 +277,7 @@ groups:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Scheduler: SchedulerConfig{
|
Scheduler: SchedulerConfig{
|
||||||
Use: "fifo",
|
Use: "serial",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1572,7 +1572,7 @@ groups:
|
|||||||
assert.Equal(t, "group", cfg.Routing.Router.Use)
|
assert.Equal(t, "group", cfg.Routing.Router.Use)
|
||||||
// default group injected for orphaned models (none here) still leaves g1
|
// default group injected for orphaned models (none here) still leaves g1
|
||||||
assert.Contains(t, cfg.Routing.Router.Settings.Groups, "g1")
|
assert.Contains(t, cfg.Routing.Router.Settings.Groups, "g1")
|
||||||
assert.Equal(t, "fifo", cfg.Routing.Scheduler.Use)
|
assert.Equal(t, "serial", cfg.Routing.Scheduler.Use)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig_Routing_LegacyTopLevelMatrix(t *testing.T) {
|
func TestConfig_Routing_LegacyTopLevelMatrix(t *testing.T) {
|
||||||
@@ -1631,7 +1631,7 @@ func TestConfig_Routing_DefaultsToGroup(t *testing.T) {
|
|||||||
cfg, err := LoadConfigFromReader(strings.NewReader(twoModels))
|
cfg, err := LoadConfigFromReader(strings.NewReader(twoModels))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "group", cfg.Routing.Router.Use)
|
assert.Equal(t, "group", cfg.Routing.Router.Use)
|
||||||
assert.Equal(t, "fifo", cfg.Routing.Scheduler.Use)
|
assert.Equal(t, "serial", cfg.Routing.Scheduler.Use)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig_Routing_LegacyAndRoutingConflict(t *testing.T) {
|
func TestConfig_Routing_LegacyAndRoutingConflict(t *testing.T) {
|
||||||
|
|||||||
@@ -266,7 +266,7 @@ groups:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Scheduler: SchedulerConfig{
|
Scheduler: SchedulerConfig{
|
||||||
Use: "fifo",
|
Use: "serial",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,11 +358,16 @@ func LoadConfigFromReader(r io.Reader) (Config, error) {
|
|||||||
config.Routing.Router.Settings.Matrix = config.Matrix
|
config.Routing.Router.Settings.Matrix = config.Matrix
|
||||||
config.Routing.Router.Settings.Groups = config.Groups
|
config.Routing.Router.Settings.Groups = config.Groups
|
||||||
|
|
||||||
|
// This fork defaults to the "serial" scheduler: one model loaded at a time,
|
||||||
|
// requests served in strict arrival order. Set use: fifo for the upstream
|
||||||
|
// throughput-oriented behavior that batches same-model requests.
|
||||||
if config.Routing.Scheduler.Use == "" {
|
if config.Routing.Scheduler.Use == "" {
|
||||||
config.Routing.Scheduler.Use = "fifo"
|
config.Routing.Scheduler.Use = "serial"
|
||||||
}
|
}
|
||||||
if config.Routing.Scheduler.Use != "fifo" {
|
switch config.Routing.Scheduler.Use {
|
||||||
return Config{}, fmt.Errorf("routing.scheduler.use: unknown scheduler %q (valid: fifo)", config.Routing.Scheduler.Use)
|
case "fifo", "serial":
|
||||||
|
default:
|
||||||
|
return Config{}, fmt.Errorf("routing.scheduler.use: unknown scheduler %q (valid: fifo, serial)", config.Routing.Scheduler.Use)
|
||||||
}
|
}
|
||||||
for modelID := range config.Routing.Scheduler.Settings.Fifo.Priority {
|
for modelID := range config.Routing.Scheduler.Settings.Fifo.Priority {
|
||||||
if _, found := config.RealModelName(modelID); !found {
|
if _, found := config.RealModelName(modelID); !found {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ var validModalities = map[string]struct{}{
|
|||||||
"text": {},
|
"text": {},
|
||||||
"audio": {},
|
"audio": {},
|
||||||
"image": {},
|
"image": {},
|
||||||
|
"video": {},
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModelCapConfig defines what modalities and features a model supports.
|
// ModelCapConfig defines what modalities and features a model supports.
|
||||||
@@ -37,12 +38,12 @@ func (c ModelCapConfig) Empty() bool {
|
|||||||
func (c ModelCapConfig) Validate() error {
|
func (c ModelCapConfig) Validate() error {
|
||||||
for _, m := range c.In {
|
for _, m := range c.In {
|
||||||
if _, ok := validModalities[m]; !ok {
|
if _, ok := validModalities[m]; !ok {
|
||||||
return fmt.Errorf("capabilities.in: invalid modality %q, must be one of: text, audio, image", m)
|
return fmt.Errorf("capabilities.in: invalid modality %q, must be one of: text, audio, image, video", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, m := range c.Out {
|
for _, m := range c.Out {
|
||||||
if _, ok := validModalities[m]; !ok {
|
if _, ok := validModalities[m]; !ok {
|
||||||
return fmt.Errorf("capabilities.out: invalid modality %q, must be one of: text, audio, image", m)
|
return fmt.Errorf("capabilities.out: invalid modality %q, must be one of: text, audio, image, video", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c.Context < 0 {
|
if c.Context < 0 {
|
||||||
|
|||||||
@@ -296,20 +296,25 @@ func TestConfig_ModelCapabilities_Validate(t *testing.T) {
|
|||||||
assert.NoError(t, caps.Validate())
|
assert.NoError(t, caps.Validate())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("valid_video_modality", func(t *testing.T) {
|
||||||
|
caps := ModelCapConfig{In: []string{"text", "image"}, Out: []string{"video"}}
|
||||||
|
assert.NoError(t, caps.Validate())
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("invalid_in_modality", func(t *testing.T) {
|
t.Run("invalid_in_modality", func(t *testing.T) {
|
||||||
caps := ModelCapConfig{In: []string{"video"}}
|
caps := ModelCapConfig{In: []string{"smell"}}
|
||||||
err := caps.Validate()
|
err := caps.Validate()
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "capabilities.in")
|
assert.Contains(t, err.Error(), "capabilities.in")
|
||||||
assert.Contains(t, err.Error(), "video")
|
assert.Contains(t, err.Error(), "smell")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("invalid_out_modality", func(t *testing.T) {
|
t.Run("invalid_out_modality", func(t *testing.T) {
|
||||||
caps := ModelCapConfig{Out: []string{"video"}}
|
caps := ModelCapConfig{Out: []string{"smell"}}
|
||||||
err := caps.Validate()
|
err := caps.Validate()
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "capabilities.out")
|
assert.Contains(t, err.Error(), "capabilities.out")
|
||||||
assert.Contains(t, err.Error(), "video")
|
assert.Contains(t, err.Error(), "smell")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("negative_context", func(t *testing.T) {
|
t.Run("negative_context", func(t *testing.T) {
|
||||||
@@ -327,10 +332,10 @@ models:
|
|||||||
capabilities:
|
capabilities:
|
||||||
in:
|
in:
|
||||||
- text
|
- text
|
||||||
- video
|
- smell
|
||||||
`
|
`
|
||||||
_, err := LoadConfigFromReader(strings.NewReader(content))
|
_, err := LoadConfigFromReader(strings.NewReader(content))
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "video")
|
assert.Contains(t, err.Error(), "smell")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,9 +92,14 @@ type Effects interface {
|
|||||||
StopProcesses(timeout time.Duration, ids []string)
|
StopProcesses(timeout time.Duration, ids []string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a Scheduler selected by conf.Routing.Scheduler.Use, configured
|
// New returns a Scheduler selected by conf.Routing.Scheduler.Use, configured from
|
||||||
// from conf and bound to the given planner and effects. Currently only "fifo"
|
// conf and bound to the given planner and effects. Supported values are "fifo"
|
||||||
// (the default) is supported.
|
// (throughput-oriented, batches same-model requests) and "serial" (strict
|
||||||
|
// one-model-at-a-time, exact arrival order).
|
||||||
|
//
|
||||||
|
// The deployment default is applied by config loading (LoadConfig sets Use to
|
||||||
|
// "serial" when unset). The "" fallback here is the library default and remains
|
||||||
|
// "fifo" so callers that build a Config directly keep the original behavior.
|
||||||
func New(conf config.Config, name string, logger *logmon.Monitor, planner Swapper, eff Effects) (Scheduler, error) {
|
func New(conf config.Config, name string, logger *logmon.Monitor, planner Swapper, eff Effects) (Scheduler, error) {
|
||||||
use := conf.Routing.Scheduler.Use
|
use := conf.Routing.Scheduler.Use
|
||||||
if use == "" {
|
if use == "" {
|
||||||
@@ -103,6 +108,9 @@ func New(conf config.Config, name string, logger *logmon.Monitor, planner Swappe
|
|||||||
switch use {
|
switch use {
|
||||||
case "fifo":
|
case "fifo":
|
||||||
return NewFIFO(name, logger, planner, conf.Routing.Scheduler.Settings.Fifo, conf.Models, eff), nil
|
return NewFIFO(name, logger, planner, conf.Routing.Scheduler.Settings.Fifo, conf.Models, eff), nil
|
||||||
|
case "serial":
|
||||||
|
// Serial ignores the group planner: it always evicts every other model.
|
||||||
|
return NewSerial(name, logger, eff), nil
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unsupported scheduler type: %q", use)
|
return nil, fmt.Errorf("unsupported scheduler type: %q", use)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,253 @@
|
|||||||
|
package scheduler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mostlygeek/llama-swap/internal/logmon"
|
||||||
|
"github.com/mostlygeek/llama-swap/internal/process"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Serial is a strict one-model-at-a-time scheduler. Unlike FIFO it never reorders
|
||||||
|
// or batches: requests run in exact arrival order and at most one request runs at
|
||||||
|
// any instant. When the next request targets a model other than the one loaded,
|
||||||
|
// every other running model is evicted and the target is loaded before it runs,
|
||||||
|
// so a single model occupies memory at a time — at the cost of throughput.
|
||||||
|
//
|
||||||
|
// Example: A B C A is served as A B C A. The final A reloads its model even
|
||||||
|
// though it ran first, because B and C displaced it in between. (FIFO, by
|
||||||
|
// contrast, would batch the two A requests: A A B C.)
|
||||||
|
//
|
||||||
|
// Serial ignores group/eviction policy entirely: it always evicts every other
|
||||||
|
// running model, regardless of how groups are configured. That is what makes the
|
||||||
|
// single-model guarantee a property of the scheduler rather than of the config.
|
||||||
|
//
|
||||||
|
// Like FIFO, every method runs on the router's single run-loop goroutine, so no
|
||||||
|
// internal locking is needed.
|
||||||
|
type Serial struct {
|
||||||
|
name string
|
||||||
|
logger *logmon.Monitor
|
||||||
|
effects Effects
|
||||||
|
|
||||||
|
// queued holds requests in strict arrival order. It is never reordered.
|
||||||
|
queued []HandlerReq
|
||||||
|
|
||||||
|
// active is the one request currently being processed (loading or serving),
|
||||||
|
// or nil when idle. phase is meaningful only while active != nil.
|
||||||
|
active *HandlerReq
|
||||||
|
phase serialPhase
|
||||||
|
}
|
||||||
|
|
||||||
|
// serialPhase is the lifecycle stage of the active request.
|
||||||
|
type serialPhase int
|
||||||
|
|
||||||
|
const (
|
||||||
|
phaseIdle serialPhase = iota
|
||||||
|
phaseSwapping // waiting for OnSwapDone for active.Model
|
||||||
|
phaseServing // waiting for OnServeDone for active.Model
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewSerial builds a Serial scheduler. It takes no Swapper: eviction is always
|
||||||
|
// "stop every other running model", so the group planner is not consulted.
|
||||||
|
func NewSerial(name string, logger *logmon.Monitor, eff Effects) *Serial {
|
||||||
|
return &Serial{
|
||||||
|
name: name,
|
||||||
|
logger: logger,
|
||||||
|
effects: eff,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnRequest validates the model and appends the request to the tail of the queue,
|
||||||
|
// then tries to start the next job. Unknown models fail immediately.
|
||||||
|
func (s *Serial) OnRequest(req HandlerReq) {
|
||||||
|
if _, ok := s.effects.ModelState(req.Model); !ok {
|
||||||
|
s.logger.Debugf("%s: model %s not handled by this router", s.name, req.Model)
|
||||||
|
s.effects.GrantError(req, ErrModelNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.queued = append(s.queued, req)
|
||||||
|
broadcastQueuePositions(s.queued)
|
||||||
|
s.startNext()
|
||||||
|
}
|
||||||
|
|
||||||
|
// startNext begins processing the head of the queue when nothing is active. It
|
||||||
|
// fast-paths a request whose model is already the sole loaded-and-ready process;
|
||||||
|
// otherwise it launches a swap that evicts every other running model first. The
|
||||||
|
// loop skips over requests for models that vanished (e.g. a config reload) and
|
||||||
|
// requests whose caller disconnected before they could be served.
|
||||||
|
func (s *Serial) startNext() {
|
||||||
|
if s.active != nil {
|
||||||
|
return // a job is already loading or serving
|
||||||
|
}
|
||||||
|
for len(s.queued) > 0 {
|
||||||
|
req := s.queued[0]
|
||||||
|
s.queued = s.queued[1:]
|
||||||
|
broadcastQueuePositions(s.queued)
|
||||||
|
|
||||||
|
state, ok := s.effects.ModelState(req.Model)
|
||||||
|
if !ok {
|
||||||
|
s.effects.GrantError(req, ErrModelNotFound)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
r := req
|
||||||
|
s.active = &r
|
||||||
|
|
||||||
|
evict := s.otherRunning(req.Model)
|
||||||
|
if state == process.StateReady && len(evict) == 0 {
|
||||||
|
// Already loaded and the only model running — serve immediately.
|
||||||
|
s.logger.Debugf("%s: serving model %s (already loaded)", s.name, req.Model)
|
||||||
|
if s.serve() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
continue // caller gone; pick the next request
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Debugf("%s: swapping to model %s, evicting %v", s.name, req.Model, evict)
|
||||||
|
s.phase = phaseSwapping
|
||||||
|
s.effects.StartSwap(req.Model, evict)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// serve hands the active request its tracked handler. It returns true when the
|
||||||
|
// request is now serving (await OnServeDone); false when the caller had already
|
||||||
|
// disconnected, in which case active is cleared so the next job can start.
|
||||||
|
func (s *Serial) serve() bool {
|
||||||
|
if s.effects.GrantServe(*s.active, s.active.Model) {
|
||||||
|
s.phase = phaseServing
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
s.logger.Debugf("%s: caller for model %s gone before serve", s.name, s.active.Model)
|
||||||
|
s.active = nil
|
||||||
|
s.phase = phaseIdle
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnSwapDone fires when the load for the active request completes. On success the
|
||||||
|
// request is served; on failure its caller receives the error and the queue
|
||||||
|
// advances. A SwapDone that does not match the active load (e.g. its request was
|
||||||
|
// unloaded or cancelled mid-load) is ignored.
|
||||||
|
func (s *Serial) OnSwapDone(ev SwapDone) {
|
||||||
|
if s.active == nil || s.phase != phaseSwapping || s.active.Model != ev.ModelID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ev.Err != nil {
|
||||||
|
s.logger.Debugf("%s: swap for model %s failed: %v", s.name, ev.ModelID, ev.Err)
|
||||||
|
s.effects.GrantError(*s.active, ev.Err)
|
||||||
|
s.active = nil
|
||||||
|
s.phase = phaseIdle
|
||||||
|
s.startNext()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !s.serve() {
|
||||||
|
s.startNext() // caller vanished while the model loaded; move on
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnServeDone fires when the active request's handler returns. The slot is freed
|
||||||
|
// and the next queued request begins.
|
||||||
|
func (s *Serial) OnServeDone(ev ServeDoneEvent) {
|
||||||
|
if s.active == nil || s.phase != phaseServing {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.active = nil
|
||||||
|
s.phase = phaseIdle
|
||||||
|
s.startNext()
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCancel removes a disconnected client's request from the queue. A request that
|
||||||
|
// is already active is left to finish: if it was loading, OnSwapDone's serve()
|
||||||
|
// will find the caller gone (GrantServe false) and advance; if it was serving,
|
||||||
|
// its handler returns normally and reaches OnServeDone.
|
||||||
|
func (s *Serial) OnCancel(req HandlerReq) {
|
||||||
|
if len(s.queued) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
kept := s.queued[:0]
|
||||||
|
removed := false
|
||||||
|
for _, q := range s.queued {
|
||||||
|
if q.Respond == req.Respond {
|
||||||
|
removed = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
kept = append(kept, q)
|
||||||
|
}
|
||||||
|
s.queued = kept
|
||||||
|
if removed {
|
||||||
|
s.logger.Debugf("%s: cancelled request for model %s pruned from queue", s.name, req.Model)
|
||||||
|
broadcastQueuePositions(s.queued)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnUnload reconciles state for an unload, stops the targeted processes, and
|
||||||
|
// advances the queue. It mirrors the FIFO contract: queued requests for unloaded
|
||||||
|
// models are failed; an active *loading* request for an unloaded model is failed
|
||||||
|
// (its swap goroutine is left to finish and its SwapDone is then ignored); an
|
||||||
|
// active *serving* request is left for its handler to end when StopProcesses
|
||||||
|
// kills the upstream. The Stop is synchronous so callers of Unload can rely on
|
||||||
|
// the processes being stopped on return.
|
||||||
|
func (s *Serial) OnUnload(targets []string, timeout time.Duration) {
|
||||||
|
unloadErr := fmt.Errorf("%s: model unloaded", s.name)
|
||||||
|
|
||||||
|
targetSet := make(map[string]bool, len(targets))
|
||||||
|
for _, id := range targets {
|
||||||
|
targetSet[id] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.active != nil && s.phase == phaseSwapping && targetSet[s.active.Model] {
|
||||||
|
s.effects.GrantError(*s.active, unloadErr)
|
||||||
|
s.active = nil
|
||||||
|
s.phase = phaseIdle
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(s.queued) > 0 {
|
||||||
|
kept := s.queued[:0]
|
||||||
|
for _, q := range s.queued {
|
||||||
|
if targetSet[q.Model] {
|
||||||
|
s.effects.GrantError(q, unloadErr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
kept = append(kept, q)
|
||||||
|
}
|
||||||
|
s.queued = kept
|
||||||
|
broadcastQueuePositions(s.queued)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.effects.StopProcesses(timeout, targets)
|
||||||
|
|
||||||
|
// A still-serving active request advances via OnServeDone when its killed
|
||||||
|
// handler returns; only start the next job when nothing is active now.
|
||||||
|
if s.active == nil {
|
||||||
|
s.startNext()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnShutdown grants err to every request the scheduler still holds: an active
|
||||||
|
// loading request and all queued requests. A serving request is torn down with
|
||||||
|
// its process by the baseRouter.
|
||||||
|
func (s *Serial) OnShutdown(err error) {
|
||||||
|
if s.active != nil && s.phase == phaseSwapping {
|
||||||
|
s.effects.GrantError(*s.active, err)
|
||||||
|
s.active = nil
|
||||||
|
s.phase = phaseIdle
|
||||||
|
}
|
||||||
|
for _, q := range s.queued {
|
||||||
|
s.effects.GrantError(q, err)
|
||||||
|
}
|
||||||
|
s.queued = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherRunning returns every running model except target, sorted for
|
||||||
|
// deterministic eviction.
|
||||||
|
func (s *Serial) otherRunning(target string) []string {
|
||||||
|
var out []string
|
||||||
|
for id := range s.effects.RunningModels() {
|
||||||
|
if id != target {
|
||||||
|
out = append(out, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
@@ -0,0 +1,391 @@
|
|||||||
|
package scheduler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mostlygeek/llama-swap/internal/logmon"
|
||||||
|
"github.com/mostlygeek/llama-swap/internal/process"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Serial methods all run on the router's single run-loop goroutine, so these
|
||||||
|
// tests drive them directly and synchronously, reusing fakeEffects and the
|
||||||
|
// req/reqCh helpers from fifo_test.go. A load completes via OnSwapDone and a
|
||||||
|
// served request finishes via OnServeDone — the events the run loop delivers.
|
||||||
|
|
||||||
|
func newSerial(eff Effects) *Serial {
|
||||||
|
return NewSerial("test", logmon.NewWriter(io.Discard), eff)
|
||||||
|
}
|
||||||
|
|
||||||
|
// lastStart returns the most recent StartSwap record.
|
||||||
|
func lastStart(t *testing.T, eff *fakeEffects) startRec {
|
||||||
|
t.Helper()
|
||||||
|
if len(eff.starts) == 0 {
|
||||||
|
t.Fatal("no StartSwap recorded")
|
||||||
|
}
|
||||||
|
return eff.starts[len(eff.starts)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func sameSet(a, b []string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m := map[string]int{}
|
||||||
|
for _, x := range a {
|
||||||
|
m[x]++
|
||||||
|
}
|
||||||
|
for _, x := range b {
|
||||||
|
m[x]--
|
||||||
|
}
|
||||||
|
for _, v := range m {
|
||||||
|
if v != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// servedOrder returns the model IDs of every successful serve grant in order.
|
||||||
|
func servedOrder(eff *fakeEffects) []string {
|
||||||
|
var out []string
|
||||||
|
for _, g := range eff.grants {
|
||||||
|
if g.err == nil && g.serve {
|
||||||
|
out = append(out, g.model)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_FastPath_AlreadyLoaded(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a"))
|
||||||
|
|
||||||
|
if got := len(eff.starts); got != 0 {
|
||||||
|
t.Errorf("StartSwap calls=%d want 0 (already loaded, no swap)", got)
|
||||||
|
}
|
||||||
|
if got := eff.served("a"); got != 1 {
|
||||||
|
t.Errorf("served(a)=%d want 1", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_ColdStart_LoadsThenServes(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a"))
|
||||||
|
if got := eff.startsFor("a"); got != 1 {
|
||||||
|
t.Fatalf("StartSwap(a)=%d want 1", got)
|
||||||
|
}
|
||||||
|
if got := eff.served("a"); got != 0 {
|
||||||
|
t.Errorf("served(a)=%d want 0 before load completes", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
s.OnSwapDone(SwapDone{ModelID: "a"})
|
||||||
|
if got := eff.served("a"); got != 1 {
|
||||||
|
t.Errorf("served(a)=%d want 1 after load", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_UnknownModel(t *testing.T) {
|
||||||
|
eff := newFakeEffects() // no states => unknown
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("ghost"))
|
||||||
|
|
||||||
|
if len(eff.starts) != 0 {
|
||||||
|
t.Errorf("StartSwap calls=%d want 0", len(eff.starts))
|
||||||
|
}
|
||||||
|
if eff.errored("ghost") != 1 {
|
||||||
|
t.Fatalf("errored(ghost)=%d want 1", eff.errored("ghost"))
|
||||||
|
}
|
||||||
|
if !errors.Is(eff.grants[0].err, ErrModelNotFound) {
|
||||||
|
t.Errorf("err=%v want ErrModelNotFound", eff.grants[0].err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_EvictsEveryOtherModel(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["x"] = process.StateReady // already running
|
||||||
|
eff.states["y"] = process.StateReady // also running (e.g. left over)
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a"))
|
||||||
|
|
||||||
|
st := lastStart(t, eff)
|
||||||
|
if st.model != "a" {
|
||||||
|
t.Fatalf("loading %s want a", st.model)
|
||||||
|
}
|
||||||
|
if !sameSet(st.evict, []string{"x", "y"}) {
|
||||||
|
t.Errorf("evict=%v want [x y] (serial evicts ALL other models)", st.evict)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSerial_OneJobAtATime verifies a second request waits while the first is
|
||||||
|
// serving, and only starts after the first finishes.
|
||||||
|
func TestSerial_OneJobAtATime(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a")) // served immediately
|
||||||
|
s.OnRequest(req("b")) // must wait — a is serving
|
||||||
|
|
||||||
|
if got := eff.startsFor("b"); got != 0 {
|
||||||
|
t.Fatalf("StartSwap(b)=%d want 0 while a is serving", got)
|
||||||
|
}
|
||||||
|
if got := eff.served("a"); got != 1 {
|
||||||
|
t.Fatalf("served(a)=%d want 1", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
// a finishes -> b may now load (evicting a).
|
||||||
|
s.OnServeDone(ServeDoneEvent{ModelID: "a"})
|
||||||
|
if got := eff.startsFor("b"); got != 1 {
|
||||||
|
t.Fatalf("StartSwap(b)=%d want 1 after a finished", got)
|
||||||
|
}
|
||||||
|
if st := lastStart(t, eff); !sameSet(st.evict, []string{"a"}) {
|
||||||
|
t.Errorf("b evict=%v want [a]", st.evict)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSerial_SameModelConsecutive_NoReload verifies back-to-back requests for the
|
||||||
|
// already-loaded model run without a reload, one after another.
|
||||||
|
func TestSerial_SameModelConsecutive_NoReload(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a")) // cold load
|
||||||
|
s.OnRequest(req("a")) // queued behind the first
|
||||||
|
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
s.OnSwapDone(SwapDone{ModelID: "a"}) // first serves
|
||||||
|
if got := eff.served("a"); got != 1 {
|
||||||
|
t.Fatalf("served(a)=%d want 1 (one at a time)", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
s.OnServeDone(ServeDoneEvent{ModelID: "a"}) // first done -> second serves
|
||||||
|
if got := eff.served("a"); got != 2 {
|
||||||
|
t.Fatalf("served(a)=%d want 2", got)
|
||||||
|
}
|
||||||
|
if got := eff.startsFor("a"); got != 1 {
|
||||||
|
t.Errorf("StartSwap(a)=%d want 1 (second request must not reload)", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSerial_StrictArrivalOrder is the core guarantee: qwen36, qwen35, sdxl,
|
||||||
|
// qwen36 execute in EXACTLY that order with evictions between each model switch,
|
||||||
|
// including reloading qwen36 at the end even though it ran first.
|
||||||
|
func TestSerial_StrictArrivalOrder(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
for _, m := range []string{"qwen36", "qwen35", "sdxl"} {
|
||||||
|
eff.states[m] = process.StateStopped
|
||||||
|
}
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
for _, m := range []string{"qwen36", "qwen35", "sdxl", "qwen36"} {
|
||||||
|
s.OnRequest(req(m))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only the first job starts loading; the rest wait their turn.
|
||||||
|
if len(eff.starts) != 1 || eff.starts[0].model != "qwen36" {
|
||||||
|
t.Fatalf("starts=%+v want only [qwen36] loading first", eff.starts)
|
||||||
|
}
|
||||||
|
|
||||||
|
// step completes the current model's load+serve and returns control to the
|
||||||
|
// scheduler, which must start the next queued model.
|
||||||
|
step := func(model string, wantEvict []string) {
|
||||||
|
t.Helper()
|
||||||
|
st := lastStart(t, eff)
|
||||||
|
if st.model != model {
|
||||||
|
t.Fatalf("loading %q want %q", st.model, model)
|
||||||
|
}
|
||||||
|
if !sameSet(st.evict, wantEvict) {
|
||||||
|
t.Fatalf("loading %q evict=%v want %v", model, st.evict, wantEvict)
|
||||||
|
}
|
||||||
|
// Simulate the eviction + load actually happening.
|
||||||
|
for _, e := range st.evict {
|
||||||
|
eff.states[e] = process.StateStopped
|
||||||
|
}
|
||||||
|
eff.states[model] = process.StateReady
|
||||||
|
s.OnSwapDone(SwapDone{ModelID: model})
|
||||||
|
s.OnServeDone(ServeDoneEvent{ModelID: model})
|
||||||
|
}
|
||||||
|
|
||||||
|
step("qwen36", nil) // cold load, nothing else running
|
||||||
|
step("qwen35", []string{"qwen36"}) // evict qwen36
|
||||||
|
step("sdxl", []string{"qwen35"}) // evict qwen35
|
||||||
|
step("qwen36", []string{"sdxl"}) // RELOAD qwen36, evict sdxl
|
||||||
|
|
||||||
|
want := []string{"qwen36", "qwen35", "sdxl", "qwen36"}
|
||||||
|
if got := servedOrder(eff); !sameOrder(got, want) {
|
||||||
|
t.Fatalf("serve order=%v want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func sameOrder(a, b []string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range a {
|
||||||
|
if a[i] != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_SwapError_FailsCallerAndAdvances(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a"))
|
||||||
|
s.OnRequest(req("b")) // queued behind a
|
||||||
|
|
||||||
|
// a's load fails: its caller is errored and b proceeds.
|
||||||
|
s.OnSwapDone(SwapDone{ModelID: "a", Err: errors.New("boom")})
|
||||||
|
if eff.errored("a") != 1 {
|
||||||
|
t.Fatalf("errored(a)=%d want 1", eff.errored("a"))
|
||||||
|
}
|
||||||
|
if got := eff.startsFor("b"); got != 1 {
|
||||||
|
t.Fatalf("StartSwap(b)=%d want 1 after a's load failed", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSerial_GrantServeFalse_Advances verifies that when the active request's
|
||||||
|
// caller has disconnected by serve time, the queue advances to the next request.
|
||||||
|
func TestSerial_GrantServeFalse_Advances(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
eff.serveResult["a"] = false // a's caller is gone by grant time
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a"))
|
||||||
|
s.OnRequest(req("b")) // queued
|
||||||
|
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
s.OnSwapDone(SwapDone{ModelID: "a"}) // grant fails -> advance to b
|
||||||
|
|
||||||
|
if got := eff.served("a"); got != 0 {
|
||||||
|
t.Errorf("served(a)=%d want 0 (caller gone)", got)
|
||||||
|
}
|
||||||
|
if got := eff.startsFor("b"); got != 1 {
|
||||||
|
t.Fatalf("StartSwap(b)=%d want 1 (advanced after gone caller)", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_OnCancel_QueuedRequest(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(reqCh("a")) // starts loading a
|
||||||
|
cancelled := reqCh("b")
|
||||||
|
s.OnRequest(cancelled) // queued behind a
|
||||||
|
if len(s.queued) != 1 {
|
||||||
|
t.Fatalf("queued=%d want 1", len(s.queued))
|
||||||
|
}
|
||||||
|
|
||||||
|
s.OnCancel(cancelled)
|
||||||
|
if len(s.queued) != 0 {
|
||||||
|
t.Fatalf("queued=%d want 0 after cancel", len(s.queued))
|
||||||
|
}
|
||||||
|
|
||||||
|
// a completes; b is gone, so nothing starts for it.
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
s.OnSwapDone(SwapDone{ModelID: "a"})
|
||||||
|
s.OnServeDone(ServeDoneEvent{ModelID: "a"})
|
||||||
|
if got := eff.startsFor("b"); got != 0 {
|
||||||
|
t.Errorf("StartSwap(b)=%d want 0 (cancelled before its turn)", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_OnShutdown_FailsQueuedAndActiveLoad(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
eff.states["c"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a")) // active (loading)
|
||||||
|
s.OnRequest(req("b")) // queued
|
||||||
|
s.OnRequest(req("c")) // queued
|
||||||
|
|
||||||
|
s.OnShutdown(errors.New("shutting down"))
|
||||||
|
|
||||||
|
if got := eff.errored(""); got != 3 {
|
||||||
|
t.Errorf("error grants=%d want 3 (active load + 2 queued)", got)
|
||||||
|
}
|
||||||
|
if len(s.queued) != 0 {
|
||||||
|
t.Errorf("queued=%d want 0 after shutdown", len(s.queued))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestSerial_OnUnload_WhileServing verifies that unloading the model that is
|
||||||
|
// actively serving does not strand the queue: OnUnload stops the process but
|
||||||
|
// leaves the active request to end via OnServeDone, which then advances.
|
||||||
|
func TestSerial_OnUnload_WhileServing(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateReady
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a")) // served immediately (a ready)
|
||||||
|
s.OnRequest(req("b")) // queued behind a
|
||||||
|
if got := eff.served("a"); got != 1 {
|
||||||
|
t.Fatalf("served(a)=%d want 1", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unload a while it is serving: the process is stopped, but the queue must
|
||||||
|
// not advance yet — the active serve is still outstanding.
|
||||||
|
s.OnUnload([]string{"a"}, time.Second)
|
||||||
|
if len(eff.stops) != 1 || !sameSet(eff.stops[0].ids, []string{"a"}) {
|
||||||
|
t.Errorf("StopProcesses=%+v want one call stopping [a]", eff.stops)
|
||||||
|
}
|
||||||
|
if got := eff.startsFor("b"); got != 0 {
|
||||||
|
t.Fatalf("StartSwap(b)=%d want 0 before the serving request ends", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The killed handler returns -> OnServeDone advances to b.
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
s.OnServeDone(ServeDoneEvent{ModelID: "a"})
|
||||||
|
if got := eff.startsFor("b"); got != 1 {
|
||||||
|
t.Fatalf("StartSwap(b)=%d want 1 after the serving request ended", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSerial_OnUnload_DropsQueuedAndStops(t *testing.T) {
|
||||||
|
eff := newFakeEffects()
|
||||||
|
eff.states["a"] = process.StateStopped
|
||||||
|
eff.states["b"] = process.StateStopped
|
||||||
|
s := newSerial(eff)
|
||||||
|
|
||||||
|
s.OnRequest(req("a")) // active (loading a)
|
||||||
|
s.OnRequest(req("b")) // queued
|
||||||
|
|
||||||
|
// Unload a: its active load is failed and a is stopped.
|
||||||
|
s.OnUnload([]string{"a"}, time.Second)
|
||||||
|
|
||||||
|
if eff.errored("a") != 1 {
|
||||||
|
t.Errorf("errored(a)=%d want 1 (active load failed)", eff.errored("a"))
|
||||||
|
}
|
||||||
|
if len(eff.stops) != 1 || !sameSet(eff.stops[0].ids, []string{"a"}) {
|
||||||
|
t.Errorf("StopProcesses=%+v want one call stopping [a]", eff.stops)
|
||||||
|
}
|
||||||
|
// b was queued and not unloaded; with a's load cancelled it now starts.
|
||||||
|
if got := eff.startsFor("b"); got != 1 {
|
||||||
|
t.Errorf("StartSwap(b)=%d want 1 after unload advanced the queue", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -88,6 +88,12 @@ func renderCapabilities(caps config.ModelCapConfig) (arch map[string]any, capsMa
|
|||||||
if contains(caps.In, "image") && contains(caps.Out, "image") {
|
if contains(caps.In, "image") && contains(caps.Out, "image") {
|
||||||
capsMap["image_to_image"] = true
|
capsMap["image_to_image"] = true
|
||||||
}
|
}
|
||||||
|
if contains(caps.In, "text") && contains(caps.Out, "video") {
|
||||||
|
capsMap["video_generation"] = true
|
||||||
|
}
|
||||||
|
if contains(caps.In, "image") && contains(caps.Out, "video") {
|
||||||
|
capsMap["image_to_video"] = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if caps.Tools {
|
if caps.Tools {
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// captureFieldsByPath overrides the default capture mask for routes carrying
|
// captureFieldsByPath overrides the default capture mask for routes carrying
|
||||||
// large binary payloads (audio/image) where storing the full body is wasteful.
|
// large binary payloads (audio/image/video) where storing the full body is
|
||||||
|
// wasteful.
|
||||||
var captureFieldsByPath = map[string]captureFields{
|
var captureFieldsByPath = map[string]captureFields{
|
||||||
"/v1/audio/speech": captureReqAll | captureRespHeaders,
|
"/v1/audio/speech": captureReqAll | captureRespHeaders,
|
||||||
"/v1/audio/voices": captureReqHeaders | captureRespAll,
|
"/v1/audio/voices": captureReqHeaders | captureRespAll,
|
||||||
@@ -46,6 +47,9 @@ var captureFieldsByPath = map[string]captureFields{
|
|||||||
"/v1/images/edits": captureReqHeaders | captureRespHeaders,
|
"/v1/images/edits": captureReqHeaders | captureRespHeaders,
|
||||||
"/sdapi/v1/txt2img": captureReqAll | captureRespHeaders,
|
"/sdapi/v1/txt2img": captureReqAll | captureRespHeaders,
|
||||||
"/sdapi/v1/img2img": captureReqHeaders | captureRespHeaders,
|
"/sdapi/v1/img2img": captureReqHeaders | captureRespHeaders,
|
||||||
|
// video: the request multipart can carry a conditioning frame and the
|
||||||
|
// response body IS the clip — headers only, both directions.
|
||||||
|
"/v1/videos/sync": captureReqHeaders | captureRespHeaders,
|
||||||
}
|
}
|
||||||
|
|
||||||
// captureFieldsFor returns the capture mask for a request path. Unlisted routes
|
// captureFieldsFor returns the capture mask for a request path. Unlisted routes
|
||||||
|
|||||||
@@ -80,6 +80,15 @@ var modelPostJSONRoutes = []string{
|
|||||||
var modelPostFormRoutes = []string{
|
var modelPostFormRoutes = []string{
|
||||||
"/v1/audio/transcriptions",
|
"/v1/audio/transcriptions",
|
||||||
"/v1/images/edits",
|
"/v1/images/edits",
|
||||||
|
|
||||||
|
// video generation: ONLY the blocking sync shape (vLLM-Omni
|
||||||
|
// /v1/videos/sync — the response body is the clip). The async
|
||||||
|
// OpenAI-style POST /v1/videos is deliberately NOT dispatched: its
|
||||||
|
// poll/download companions (GET /v1/videos/{id}[/content]) carry no
|
||||||
|
// model field to route by, so registering creation alone would start
|
||||||
|
// unretrievable upstream jobs. Extraction is content-type driven, so
|
||||||
|
// JSON bodies dispatch here too.
|
||||||
|
"/v1/videos/sync",
|
||||||
}
|
}
|
||||||
|
|
||||||
// modelGetRoutes are model-dispatched GET endpoints (the model arrives as a
|
// modelGetRoutes are model-dispatched GET endpoints (the model arrives as a
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -339,3 +341,56 @@ func TestServer_LogStream_UnknownID_Returns400(t *testing.T) {
|
|||||||
t.Errorf("status=%d want 400", w.Code)
|
t.Errorf("status=%d want 400", w.Code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServer_VideoRoutesDispatch(t *testing.T) {
|
||||||
|
s := newTestServer(
|
||||||
|
newStubRouter([]string{"videogen-model"}, "video response"),
|
||||||
|
newStubRouter(nil, ""),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Multipart form on /v1/videos/sync.
|
||||||
|
var buf bytes.Buffer
|
||||||
|
mw := multipart.NewWriter(&buf)
|
||||||
|
mw.WriteField("model", "videogen-model")
|
||||||
|
mw.WriteField("prompt", "a cat")
|
||||||
|
mw.Close()
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/v1/videos/sync", &buf)
|
||||||
|
req.Header.Set("Content-Type", mw.FormDataContentType())
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.ServeHTTP(w, req)
|
||||||
|
if w.Code != http.StatusOK || w.Body.String() != "video response" {
|
||||||
|
t.Fatalf("/v1/videos/sync: status=%d body=%q", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON bodies dispatch too (extraction is content-type driven).
|
||||||
|
jsonReq := httptest.NewRequest(http.MethodPost, "/v1/videos/sync",
|
||||||
|
strings.NewReader(`{"model":"videogen-model","prompt":"a cat"}`))
|
||||||
|
jsonReq.Header.Set("Content-Type", "application/json")
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.ServeHTTP(w, jsonReq)
|
||||||
|
if w.Code != http.StatusOK || w.Body.String() != "video response" {
|
||||||
|
t.Fatalf("/v1/videos/sync json: status=%d body=%q", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unknown model on the video route still 404s.
|
||||||
|
nopeReq := httptest.NewRequest(http.MethodPost, "/v1/videos/sync",
|
||||||
|
strings.NewReader(`{"model":"nope","prompt":"a cat"}`))
|
||||||
|
nopeReq.Header.Set("Content-Type", "application/json")
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.ServeHTTP(w, nopeReq)
|
||||||
|
if w.Code != http.StatusNotFound {
|
||||||
|
t.Fatalf("/v1/videos/sync unknown model: status=%d want 404", w.Code)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The async job-creation route is NOT dispatched (no poll routes to
|
||||||
|
// complete the flow) — a stray OpenAI-videos client gets a clean 404
|
||||||
|
// instead of an unretrievable upstream job.
|
||||||
|
asyncReq := httptest.NewRequest(http.MethodPost, "/v1/videos",
|
||||||
|
strings.NewReader(`{"model":"videogen-model","prompt":"a cat"}`))
|
||||||
|
asyncReq.Header.Set("Content-Type", "application/json")
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.ServeHTTP(w, asyncReq)
|
||||||
|
if w.Code != http.StatusNotFound {
|
||||||
|
t.Fatalf("POST /v1/videos: status=%d want 404 (async family not routed)", w.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,359 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { models } from "../../stores/api";
|
||||||
|
import { persistentStore } from "../../stores/persistent";
|
||||||
|
import { generateVideoSync } from "../../lib/videoApi";
|
||||||
|
import { playgroundStores } from "../../stores/playgroundActivity";
|
||||||
|
import ModelSelector from "./ModelSelector.svelte";
|
||||||
|
import ExpandableTextarea from "./ExpandableTextarea.svelte";
|
||||||
|
|
||||||
|
const selectedModelStore = persistentStore<string>("playground-video-model", "");
|
||||||
|
const selectedSizeStore = persistentStore<string>("playground-video-size", "");
|
||||||
|
const negativePromptStore = persistentStore<string>("playground-video-negative-prompt", "");
|
||||||
|
const numFramesStore = persistentStore<number>("playground-video-num-frames", 0);
|
||||||
|
const fpsStore = persistentStore<number>("playground-video-fps", 0);
|
||||||
|
const stepsStore = persistentStore<number>("playground-video-steps", 0);
|
||||||
|
const guidanceStore = persistentStore<number>("playground-video-guidance", 0);
|
||||||
|
const seedStore = persistentStore<number>("playground-video-seed", -1);
|
||||||
|
|
||||||
|
let prompt = $state("");
|
||||||
|
let isGenerating = $state(false);
|
||||||
|
let videoUrl = $state<string | null>(null);
|
||||||
|
let videoMime = $state("video/mp4");
|
||||||
|
let videoBytes = $state(0);
|
||||||
|
let elapsedSeconds = $state(0);
|
||||||
|
let error = $state<string | null>(null);
|
||||||
|
let abortController = $state<AbortController | null>(null);
|
||||||
|
let showSettings = $state(false);
|
||||||
|
let initImage = $state<File | null>(null);
|
||||||
|
let initImagePreview = $state<string | null>(null);
|
||||||
|
let fileInput = $state<HTMLInputElement | null>(null);
|
||||||
|
let timer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
|
||||||
|
let hasModels = $derived($models.some((m) => !m.unlisted));
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
playgroundStores.videoGenerating.set(isGenerating);
|
||||||
|
});
|
||||||
|
|
||||||
|
function onInitImageChange(event: Event) {
|
||||||
|
const input = event.target as HTMLInputElement;
|
||||||
|
const file = input.files?.[0] ?? null;
|
||||||
|
setInitImage(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
function setInitImage(file: File | null) {
|
||||||
|
if (initImagePreview) URL.revokeObjectURL(initImagePreview);
|
||||||
|
initImage = file;
|
||||||
|
initImagePreview = file ? URL.createObjectURL(file) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearInitImage() {
|
||||||
|
setInitImage(null);
|
||||||
|
if (fileInput) fileInput.value = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
async function generate() {
|
||||||
|
const trimmedPrompt = prompt.trim();
|
||||||
|
if (!trimmedPrompt || !$selectedModelStore || isGenerating) return;
|
||||||
|
|
||||||
|
isGenerating = true;
|
||||||
|
error = null;
|
||||||
|
elapsedSeconds = 0;
|
||||||
|
abortController = new AbortController();
|
||||||
|
timer = setInterval(() => (elapsedSeconds += 1), 1000);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await generateVideoSync(
|
||||||
|
$selectedModelStore,
|
||||||
|
trimmedPrompt,
|
||||||
|
{
|
||||||
|
negativePrompt: $negativePromptStore || undefined,
|
||||||
|
size: $selectedSizeStore || undefined,
|
||||||
|
numFrames: $numFramesStore,
|
||||||
|
fps: $fpsStore,
|
||||||
|
steps: $stepsStore,
|
||||||
|
guidanceScale: $guidanceStore,
|
||||||
|
seed: $seedStore,
|
||||||
|
initImage,
|
||||||
|
},
|
||||||
|
abortController.signal
|
||||||
|
);
|
||||||
|
if (videoUrl) URL.revokeObjectURL(videoUrl);
|
||||||
|
videoUrl = result.url;
|
||||||
|
videoMime = result.mime;
|
||||||
|
videoBytes = result.sizeBytes;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof Error && err.name === "AbortError") {
|
||||||
|
// User cancelled
|
||||||
|
} else {
|
||||||
|
error = err instanceof Error ? err.message : "An error occurred";
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
isGenerating = false;
|
||||||
|
abortController = null;
|
||||||
|
if (timer) clearInterval(timer);
|
||||||
|
timer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function cancelGeneration() {
|
||||||
|
abortController?.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearVideo() {
|
||||||
|
if (videoUrl) URL.revokeObjectURL(videoUrl);
|
||||||
|
videoUrl = null;
|
||||||
|
error = null;
|
||||||
|
prompt = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function downloadVideo() {
|
||||||
|
if (!videoUrl) return;
|
||||||
|
const ext = videoMime.includes("webm") ? "webm" : "mp4";
|
||||||
|
const link = document.createElement("a");
|
||||||
|
link.href = videoUrl;
|
||||||
|
link.download = `generated-video-${Date.now()}.${ext}`;
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
document.body.removeChild(link);
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatBytes(n: number): string {
|
||||||
|
if (n > 1024 * 1024) return `${(n / 1024 / 1024).toFixed(1)} MB`;
|
||||||
|
if (n > 1024) return `${(n / 1024).toFixed(0)} KB`;
|
||||||
|
return `${n} B`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatElapsed(s: number): string {
|
||||||
|
const m = Math.floor(s / 60);
|
||||||
|
return m > 0 ? `${m}m ${s % 60}s` : `${s}s`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleKeyDown(event: KeyboardEvent) {
|
||||||
|
if (event.key === "Enter" && !event.shiftKey) {
|
||||||
|
event.preventDefault();
|
||||||
|
generate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="flex flex-col h-full">
|
||||||
|
<!-- Model selector and options -->
|
||||||
|
<div class="shrink-0 flex flex-wrap gap-2 mb-4">
|
||||||
|
<ModelSelector
|
||||||
|
bind:value={$selectedModelStore}
|
||||||
|
placeholder="Select a video model..."
|
||||||
|
disabled={isGenerating}
|
||||||
|
capabilities={["video_generation", "image_to_video"]}
|
||||||
|
matchAny={true}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<select
|
||||||
|
class="px-3 py-2 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary"
|
||||||
|
bind:value={$selectedSizeStore}
|
||||||
|
disabled={isGenerating}
|
||||||
|
>
|
||||||
|
<option value="">Model default size</option>
|
||||||
|
<optgroup label="Landscape">
|
||||||
|
<option value="1280x704">1280x704</option>
|
||||||
|
<option value="832x480">832x480</option>
|
||||||
|
</optgroup>
|
||||||
|
<optgroup label="Portrait">
|
||||||
|
<option value="704x1280">704x1280</option>
|
||||||
|
<option value="480x832">480x832</option>
|
||||||
|
</optgroup>
|
||||||
|
<optgroup label="Square">
|
||||||
|
<option value="704x704">704x704</option>
|
||||||
|
</optgroup>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="px-3 py-2 rounded border border-gray-200 dark:border-white/10 bg-surface hover:bg-secondary-hover transition-colors"
|
||||||
|
onclick={() => (showSettings = !showSettings)}
|
||||||
|
>
|
||||||
|
{showSettings ? "Hide Settings" : "Settings"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Settings panel -->
|
||||||
|
{#if showSettings}
|
||||||
|
<div class="shrink-0 mb-4 p-4 rounded border border-gray-200 dark:border-white/10 bg-surface">
|
||||||
|
<div class="grid grid-cols-2 md:grid-cols-5 gap-3 mb-3">
|
||||||
|
<label class="flex flex-col gap-1">
|
||||||
|
<span class="text-xs text-txtsecondary">Frames (0 = default)</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
class="px-2 py-1 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary"
|
||||||
|
bind:value={$numFramesStore}
|
||||||
|
min="0"
|
||||||
|
max="1000"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label class="flex flex-col gap-1">
|
||||||
|
<span class="text-xs text-txtsecondary">FPS (0 = default)</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
class="px-2 py-1 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary"
|
||||||
|
bind:value={$fpsStore}
|
||||||
|
min="0"
|
||||||
|
max="60"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label class="flex flex-col gap-1">
|
||||||
|
<span class="text-xs text-txtsecondary">Steps (0 = default)</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
class="px-2 py-1 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary"
|
||||||
|
bind:value={$stepsStore}
|
||||||
|
min="0"
|
||||||
|
max="150"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label class="flex flex-col gap-1">
|
||||||
|
<span class="text-xs text-txtsecondary">Guidance (0 = default)</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
class="px-2 py-1 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary"
|
||||||
|
bind:value={$guidanceStore}
|
||||||
|
min="0"
|
||||||
|
max="30"
|
||||||
|
step="0.5"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label class="flex flex-col gap-1">
|
||||||
|
<span class="text-xs text-txtsecondary">Seed (-1 = random)</span>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
class="px-2 py-1 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary"
|
||||||
|
bind:value={$seedStore}
|
||||||
|
min="-1"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label class="flex flex-col gap-1">
|
||||||
|
<span class="text-xs text-txtsecondary">Negative Prompt</span>
|
||||||
|
<textarea
|
||||||
|
class="px-2 py-1 rounded border border-gray-200 dark:border-white/10 bg-surface focus:outline-none focus:ring-2 focus:ring-primary resize-y text-sm"
|
||||||
|
bind:value={$negativePromptStore}
|
||||||
|
rows="2"
|
||||||
|
placeholder="Elements to avoid..."
|
||||||
|
></textarea>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- Empty state for no models configured -->
|
||||||
|
{#if !hasModels}
|
||||||
|
<div class="flex-1 flex items-center justify-center text-txtsecondary">
|
||||||
|
<p>No models configured. Add models to your configuration to generate videos.</p>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<!-- Video display area -->
|
||||||
|
<div
|
||||||
|
class="flex-1 overflow-auto mb-4 flex items-center justify-center bg-surface border border-gray-200 dark:border-white/10 rounded"
|
||||||
|
>
|
||||||
|
{#if isGenerating}
|
||||||
|
<div class="text-center text-txtsecondary">
|
||||||
|
<div
|
||||||
|
class="inline-block w-8 h-8 border-4 border-primary border-t-transparent rounded-full animate-spin mb-2"
|
||||||
|
></div>
|
||||||
|
<p>Generating video… {formatElapsed(elapsedSeconds)}</p>
|
||||||
|
<p class="text-xs mt-1">
|
||||||
|
Video generation takes minutes — a cold model load takes longer still.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
{:else if error}
|
||||||
|
<div class="text-center text-red-500 p-4">
|
||||||
|
<p class="font-medium">Error</p>
|
||||||
|
<p class="text-sm mt-1 break-all">{error}</p>
|
||||||
|
</div>
|
||||||
|
{:else if videoUrl}
|
||||||
|
<div class="relative max-w-full max-h-full flex items-center justify-center">
|
||||||
|
<!-- svelte-ignore a11y_media_has_caption -->
|
||||||
|
<video src={videoUrl} class="max-w-full max-h-full object-contain" controls autoplay loop></video>
|
||||||
|
<button
|
||||||
|
class="absolute bottom-2 right-2 p-2 bg-black/60 hover:bg-black/80 text-white rounded-full transition-colors"
|
||||||
|
onclick={downloadVideo}
|
||||||
|
aria-label="Download video"
|
||||||
|
title="Download ({formatBytes(videoBytes)})"
|
||||||
|
>
|
||||||
|
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||||
|
<path
|
||||||
|
stroke-linecap="round"
|
||||||
|
stroke-linejoin="round"
|
||||||
|
stroke-width="2"
|
||||||
|
d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"
|
||||||
|
></path>
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{:else}
|
||||||
|
<div class="text-center text-txtsecondary">
|
||||||
|
<p>Enter a prompt below to generate a short video clip</p>
|
||||||
|
<p class="text-xs mt-1">Attach an image to animate it (image-to-video)</p>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Init image chip -->
|
||||||
|
{#if initImagePreview}
|
||||||
|
<div class="shrink-0 flex items-center gap-2 mb-2">
|
||||||
|
<img src={initImagePreview} alt="Conditioning frame" class="h-12 rounded border border-gray-200 dark:border-white/10" />
|
||||||
|
<span class="text-xs text-txtsecondary">Animating this image (image-to-video)</span>
|
||||||
|
<button
|
||||||
|
class="px-1.5 py-0.5 text-xs rounded border border-gray-200 dark:border-white/10 hover:bg-red-500 hover:text-white hover:border-red-500 transition-colors"
|
||||||
|
onclick={clearInitImage}
|
||||||
|
disabled={isGenerating}
|
||||||
|
>
|
||||||
|
Remove
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
<!-- Prompt input area -->
|
||||||
|
<div class="shrink-0 flex flex-col md:flex-row gap-2">
|
||||||
|
<ExpandableTextarea
|
||||||
|
bind:value={prompt}
|
||||||
|
placeholder="Describe the video you want to generate..."
|
||||||
|
rows={3}
|
||||||
|
onkeydown={handleKeyDown}
|
||||||
|
disabled={isGenerating || !$selectedModelStore}
|
||||||
|
/>
|
||||||
|
<div class="flex flex-row md:flex-col gap-2">
|
||||||
|
{#if isGenerating}
|
||||||
|
<button class="btn bg-red-500 hover:bg-red-600 text-white flex-1 md:flex-none" onclick={cancelGeneration}>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
{:else}
|
||||||
|
<button
|
||||||
|
class="btn bg-primary text-btn-primary-text hover:opacity-90 flex-1 md:flex-none"
|
||||||
|
onclick={generate}
|
||||||
|
disabled={!prompt.trim() || !$selectedModelStore}
|
||||||
|
>
|
||||||
|
Generate
|
||||||
|
</button>
|
||||||
|
<label
|
||||||
|
class="btn flex-1 md:flex-none text-center cursor-pointer {isGenerating ? 'opacity-50 pointer-events-none' : ''}"
|
||||||
|
>
|
||||||
|
Image…
|
||||||
|
<input
|
||||||
|
type="file"
|
||||||
|
accept="image/*"
|
||||||
|
class="hidden"
|
||||||
|
bind:this={fileInput}
|
||||||
|
onchange={onInitImageChange}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<button
|
||||||
|
class="btn flex-1 md:flex-none"
|
||||||
|
onclick={clearVideo}
|
||||||
|
disabled={!videoUrl && !error && !prompt.trim()}
|
||||||
|
>
|
||||||
|
Clear
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
@@ -8,6 +8,8 @@ export interface ModelCapabilities {
|
|||||||
audio_speech?: boolean;
|
audio_speech?: boolean;
|
||||||
image_generation?: boolean;
|
image_generation?: boolean;
|
||||||
image_to_image?: boolean;
|
image_to_image?: boolean;
|
||||||
|
video_generation?: boolean;
|
||||||
|
image_to_video?: boolean;
|
||||||
function_calling?: boolean;
|
function_calling?: boolean;
|
||||||
reranker?: boolean;
|
reranker?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
export interface VideoGenerationOptions {
|
||||||
|
negativePrompt?: string;
|
||||||
|
size?: string; // "WxH"
|
||||||
|
numFrames?: number;
|
||||||
|
fps?: number;
|
||||||
|
steps?: number;
|
||||||
|
guidanceScale?: number;
|
||||||
|
seed?: number; // -1 = random (omitted)
|
||||||
|
initImage?: File | null; // image-to-video conditioning frame
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /v1/videos/sync (multipart; llama-swap routes by the `model` field).
|
||||||
|
// The response body IS the encoded clip — returns an object URL for <video>.
|
||||||
|
// Callers must URL.revokeObjectURL() the result when done with it.
|
||||||
|
export async function generateVideoSync(
|
||||||
|
model: string,
|
||||||
|
prompt: string,
|
||||||
|
options: VideoGenerationOptions = {},
|
||||||
|
signal?: AbortSignal
|
||||||
|
): Promise<{ url: string; mime: string; sizeBytes: number }> {
|
||||||
|
const form = new FormData();
|
||||||
|
form.set("model", model);
|
||||||
|
form.set("prompt", prompt);
|
||||||
|
if (options.negativePrompt) form.set("negative_prompt", options.negativePrompt);
|
||||||
|
if (options.size) {
|
||||||
|
const [w, h] = options.size.split("x").map(Number);
|
||||||
|
if (w > 0 && h > 0) {
|
||||||
|
// Both conventions: vLLM-Omni reads width/height, OpenAI-shaped
|
||||||
|
// upstreams read size; they can never disagree.
|
||||||
|
form.set("width", String(w));
|
||||||
|
form.set("height", String(h));
|
||||||
|
form.set("size", options.size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (options.numFrames && options.numFrames > 0) form.set("num_frames", String(options.numFrames));
|
||||||
|
if (options.fps && options.fps > 0) form.set("fps", String(options.fps));
|
||||||
|
if (options.steps && options.steps > 0) form.set("num_inference_steps", String(options.steps));
|
||||||
|
if (options.guidanceScale && options.guidanceScale > 0)
|
||||||
|
form.set("guidance_scale", String(options.guidanceScale));
|
||||||
|
if (options.seed !== undefined && options.seed >= 0) form.set("seed", String(options.seed));
|
||||||
|
if (options.initImage) form.set("input_reference", options.initImage, options.initImage.name);
|
||||||
|
|
||||||
|
const response = await fetch("/v1/videos/sync", {
|
||||||
|
method: "POST",
|
||||||
|
body: form,
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
throw new Error(`Video API error: ${response.status} - ${errorText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const blob = await response.blob();
|
||||||
|
const mime = response.headers.get("Content-Type") || "video/mp4";
|
||||||
|
return { url: URL.createObjectURL(blob), mime, sizeBytes: blob.size };
|
||||||
|
}
|
||||||
@@ -2,12 +2,13 @@
|
|||||||
import { persistentStore } from "../stores/persistent";
|
import { persistentStore } from "../stores/persistent";
|
||||||
import ChatInterface from "../components/playground/ChatInterface.svelte";
|
import ChatInterface from "../components/playground/ChatInterface.svelte";
|
||||||
import ImageInterface from "../components/playground/ImageInterface.svelte";
|
import ImageInterface from "../components/playground/ImageInterface.svelte";
|
||||||
|
import VideoInterface from "../components/playground/VideoInterface.svelte";
|
||||||
import AudioInterface from "../components/playground/AudioInterface.svelte";
|
import AudioInterface from "../components/playground/AudioInterface.svelte";
|
||||||
import SpeechInterface from "../components/playground/SpeechInterface.svelte";
|
import SpeechInterface from "../components/playground/SpeechInterface.svelte";
|
||||||
import RerankInterface from "../components/playground/RerankInterface.svelte";
|
import RerankInterface from "../components/playground/RerankInterface.svelte";
|
||||||
import ConcurrencyInterface from "../components/playground/ConcurrencyInterface.svelte";
|
import ConcurrencyInterface from "../components/playground/ConcurrencyInterface.svelte";
|
||||||
|
|
||||||
type Tab = "chat" | "images" | "speech" | "audio" | "rerank" | "concurrency";
|
type Tab = "chat" | "images" | "video" | "speech" | "audio" | "rerank" | "concurrency";
|
||||||
|
|
||||||
const selectedTabStore = persistentStore<Tab>("playground-selected-tab", "chat");
|
const selectedTabStore = persistentStore<Tab>("playground-selected-tab", "chat");
|
||||||
let mobileMenuOpen = $state(false);
|
let mobileMenuOpen = $state(false);
|
||||||
@@ -15,6 +16,7 @@
|
|||||||
const tabs: { id: Tab; label: string }[] = [
|
const tabs: { id: Tab; label: string }[] = [
|
||||||
{ id: "chat", label: "Chat" },
|
{ id: "chat", label: "Chat" },
|
||||||
{ id: "images", label: "Images" },
|
{ id: "images", label: "Images" },
|
||||||
|
{ id: "video", label: "Video" },
|
||||||
{ id: "speech", label: "Speech" },
|
{ id: "speech", label: "Speech" },
|
||||||
{ id: "audio", label: "Transcription" },
|
{ id: "audio", label: "Transcription" },
|
||||||
{ id: "rerank", label: "Rerank" },
|
{ id: "rerank", label: "Rerank" },
|
||||||
@@ -92,6 +94,9 @@
|
|||||||
<div class="h-full" class:tab-hidden={$selectedTabStore !== "images"}>
|
<div class="h-full" class:tab-hidden={$selectedTabStore !== "images"}>
|
||||||
<ImageInterface />
|
<ImageInterface />
|
||||||
</div>
|
</div>
|
||||||
|
<div class="h-full" class:tab-hidden={$selectedTabStore !== "video"}>
|
||||||
|
<VideoInterface />
|
||||||
|
</div>
|
||||||
<div class="h-full" class:tab-hidden={$selectedTabStore !== "speech"}>
|
<div class="h-full" class:tab-hidden={$selectedTabStore !== "speech"}>
|
||||||
<SpeechInterface />
|
<SpeechInterface />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -2,18 +2,20 @@ import { writable, derived } from "svelte/store";
|
|||||||
|
|
||||||
const chatStreaming = writable(false);
|
const chatStreaming = writable(false);
|
||||||
const imageGenerating = writable(false);
|
const imageGenerating = writable(false);
|
||||||
|
const videoGenerating = writable(false);
|
||||||
const speechGenerating = writable(false);
|
const speechGenerating = writable(false);
|
||||||
const audioTranscribing = writable(false);
|
const audioTranscribing = writable(false);
|
||||||
const rerankLoading = writable(false);
|
const rerankLoading = writable(false);
|
||||||
|
|
||||||
export const playgroundActivity = derived(
|
export const playgroundActivity = derived(
|
||||||
[chatStreaming, imageGenerating, speechGenerating, audioTranscribing, rerankLoading],
|
[chatStreaming, imageGenerating, videoGenerating, speechGenerating, audioTranscribing, rerankLoading],
|
||||||
([$chat, $image, $speech, $audio, $rerank]) => $chat || $image || $speech || $audio || $rerank
|
([$chat, $image, $video, $speech, $audio, $rerank]) => $chat || $image || $video || $speech || $audio || $rerank
|
||||||
);
|
);
|
||||||
|
|
||||||
export const playgroundStores = {
|
export const playgroundStores = {
|
||||||
chatStreaming,
|
chatStreaming,
|
||||||
imageGenerating,
|
imageGenerating,
|
||||||
|
videoGenerating,
|
||||||
speechGenerating,
|
speechGenerating,
|
||||||
audioTranscribing,
|
audioTranscribing,
|
||||||
rerankLoading,
|
rerankLoading,
|
||||||
|
|||||||
Reference in New Issue
Block a user