mirror of
https://github.com/mudler/LocalAI.git
synced 2026-11-04 03:24:43 -05:00
* feat(grpc): add a Forecast RPC for time-series models Add Forecast to the backend service with columnar request and response messages: flat row-major arrays, NaN for missing values, and one message per series so a caller can send series of different shapes in one call. The response keeps the request order. Wire the RPC through the server, client, embedded client, connection evicting client, in-flight tracking client and the base backend, which answers unimplemented. core/backend gains a Forecast helper that stamps the model identity and records a trace, like the other single-purpose RPCs. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(config): add the forecast usecase FLAG_FORECAST marks a model as a time-series forecaster. The timestral-cpp backend declares it as its default usecase, so a config with that backend needs no extra lines. The backend is also listed as a non-text backend, so a forecast model is never offered as a chat model. It shows up in the gallery filter and in the capability list. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(api): add POST /v1/forecast Take one or more numeric series and return quantile trajectories for the next prediction_length steps. Targets are [variates][steps] or a flat array for one variate; JSON null marks a missing value. Past and known covariates and categorical codes are checked for length before the backend sees them. Quantiles default to 0.1, 0.5 and 0.9 in the backend. The route is gated by a new default-on forecast API feature. The endpoint is listed in the swagger spec, the /api/instructions index, the UI model filter and the capability symbols. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(backends): add the timestral-cpp forecast backend Wrap the timestral.cpp C ABI with purego, the way the other ggml backends do: libtimestral is built from a pinned commit with ggml embedded, and the Go binary loads it at start. Forecast validates the counts against the array lengths, groups the series by shape, makes one library call per group and puts the results back in the order the caller sent them. Quantiles default to 0.1, 0.5 and 0.9, and a level the model does not have is refused with the list of valid ones. A mutex guards the handle because the library is not reentrant. Request size is bounded by two model options. The unit specs use a fake library. The integration specs and a gRPC spec run against the 5M F32 model that test.sh downloads and checks against a fixed SHA-256. Assisted-by: Claude:claude-sonnet-5-5 go-vet * build(backends): register timestral-cpp in the build and the library Add the CPU image to the backend matrix for amd64 and arm64, plus an Apple silicon entry that is not built or tested yet. The image has no GPU keys because the engine runs on the CPU only. Wire the make targets, the path filter and the dependency bump bot. The library entries were generated with backendcatalog. The images are not published yet, so the release entries are unpinned until a maintainer runs --repin. The gallery reader test now counts the names in the library instead of a fixed number, so adding a backend does not break it. Assisted-by: Claude:claude-sonnet-5-5 go-vet * feat(gallery): add the Timestral forecast models Add Timestral 5M, 28M, 123M and 209M as F32 entries, each with its F16 build as a variant. Q8_0 and the K-quants are left out until the quality question is settled. Each entry runs on the timestral-cpp backend, on the CPU only. The weights use Mistral's Modified MIT licence, which bars use by companies above a monthly revenue threshold. The first paragraph after the summary says so, the licence file is installed next to the weights, and the revenue-restricted-license tag makes the entries filterable. The memory each build needs has not been measured, so no size is set. Assisted-by: Claude:claude-sonnet-5-5 * docs(forecast): document /v1/forecast and the Timestral models Add a feature page with the request and response shapes, the error cases, the model options and the licence note. List the engine in the README, the compatibility table and the backend categories. Assisted-by: Claude:claude-sonnet-5-5 --------- Co-authored-by: Ettore Di Giacinto <mudler@localai.io>
72 lines
2.6 KiB
Go
72 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"unsafe"
|
|
|
|
"github.com/ebitengine/purego"
|
|
)
|
|
|
|
// purego-bound entry points from libtimestral.so. Names match
|
|
// timestral_capi.h exactly, so `nm libtimestral.so | grep timestral_capi` is
|
|
// enough to spot drift.
|
|
//
|
|
// The context and the malloc'd forecast buffer are uintptr so the raw pointer
|
|
// comes back and can be released with timestral_capi_free_buffer. A float*
|
|
// return typed as a Go slice would be copied and the C buffer leaked.
|
|
var (
|
|
CppAbiVersion func() int32
|
|
CppLoad func(ggufPath string, threads int32) uintptr
|
|
CppFree func(ctx uintptr)
|
|
CppLastError func(ctx uintptr) string
|
|
CppNumQuantiles func(ctx uintptr) int32
|
|
CppQuantileLevel func(ctx uintptr, i int32) float64
|
|
CppForecast func(ctx uintptr, req unsafe.Pointer, outQ unsafe.Pointer) uintptr
|
|
CppFreeBuffer func(p uintptr)
|
|
abiVersionRequired int32 = 1
|
|
)
|
|
|
|
// forecastRequest mirrors timestral_forecast_request (ABI version 1). Go lays
|
|
// the struct out like a 64-bit C compiler does, and abi_test.go checks every
|
|
// offset. The pointer fields are unsafe.Pointer, not uintptr, so the buffers
|
|
// they name stay reachable for the garbage collector until the call returns.
|
|
type forecastRequest struct {
|
|
context unsafe.Pointer // B*V*T float32, NaN = missing
|
|
observationMask unsafe.Pointer // B*V*T bytes or nil; not used by this backend
|
|
B, V, T int32
|
|
predictionLen int32
|
|
pastCov unsafe.Pointer // B*c_past*T float32
|
|
cPast int32
|
|
knownCov unsafe.Pointer // B*c_known*(T+H) float32
|
|
cKnown int32
|
|
catPast unsafe.Pointer // B*cc_past*T float64
|
|
ccPast int32
|
|
catKnown unsafe.Pointer // B*cc_known*(T+H) float64
|
|
ccKnown int32
|
|
symmetricAvg int32 // -1 model default, 0, 1
|
|
clipNonneg int32 // -1 model default, 0, 1
|
|
maxContext int32 // 0 model default
|
|
}
|
|
|
|
// libFunc pairs a Go function variable with its C symbol.
|
|
type libFunc struct {
|
|
ptr any
|
|
name string
|
|
}
|
|
|
|
// registerLibFuncs binds the eight timestral_capi_* entry points of an opened
|
|
// library. main.go and the integration tests share it.
|
|
func registerLibFuncs(lib uintptr) {
|
|
for _, lf := range []libFunc{
|
|
{&CppAbiVersion, "timestral_capi_abi_version"},
|
|
{&CppLoad, "timestral_capi_load"},
|
|
{&CppFree, "timestral_capi_free"},
|
|
{&CppLastError, "timestral_capi_last_error"},
|
|
{&CppNumQuantiles, "timestral_capi_num_quantiles"},
|
|
{&CppQuantileLevel, "timestral_capi_quantile_level"},
|
|
{&CppForecast, "timestral_capi_forecast"},
|
|
{&CppFreeBuffer, "timestral_capi_free_buffer"},
|
|
} {
|
|
purego.RegisterLibFunc(lf.ptr, lib, lf.name)
|
|
}
|
|
}
|