Skip to main content

A library used to build custom functions in Cozy Creator's serverless function platform.

Project description

gen-worker

Python SDK for writing endpoints that run on Cozy's worker pool. You write a decorated Python function; the SDK handles discovery, scheduling, model loading, cancellation, file I/O, and reporting to the control plane.

Three endpoint kinds:

  • Inference — request/response (optionally streaming).
  • Training — long-running, stateful, can publish checkpoints back to a repo.
  • Conversion — produces weight artifacts on a destination repo.

Install

uv add gen-worker          # core
uv add gen-worker[torch]   # with PyTorch

Quick start

import msgspec
from gen_worker import RequestContext, inference_function

class Input(msgspec.Struct):
    prompt: str

class Output(msgspec.Struct):
    text: str

@inference_function
def hello(ctx: RequestContext, payload: Input) -> Output:
    return Output(text=f"Hello, {payload.prompt}!")

Pair it with an endpoint.toml:

schema_version = 1
name = "hello"
main = "my_pkg.main"   # import path that contains your @inference_function

[resources]
ram_gb = 2
cpu_cores = 1

…and a Dockerfile:

FROM python:3.12-slim
WORKDIR /app
COPY . /app
RUN pip install uv && uv sync --frozen
RUN mkdir -p /app/.tensorhub && \
    uv run python -m gen_worker.discovery > /app/.tensorhub/endpoint.lock
ENTRYPOINT ["uv", "run", "python", "-m", "gen_worker.entrypoint"]

Publish with cozyctl endpoint deploy (or via the platform UI). The control plane reads /app/.tensorhub/endpoint.lock from the image and routes invocations.

Reference

See docs/endpoint-authoring.md for the full authoring guide: model injection, streaming, file uploads, training/conversion contracts, error types, and local testing.

Public surface

The top-level gen_worker module exports only what endpoint authors need:

  • Decorators: inference_function, Resources
  • Bindings: Repo, Dispatch, dispatch
  • Context: RequestContext (inference; the base), ConversionContext (transform / conversion endpoints), DatasetContext (dataset-generation), TrainingContext (trainer-class)
  • Types: Asset, Tensors, Compute, LoraSpec
  • Errors: ValidationError, RetryableError, FatalError, ResourceError, AuthError, CanceledError, OutputTooLargeError, WorkerError
  • Helpers: Clamp, iter_transformers_text_deltas, load_loras, apply_low_vram_config, with_oom_retry

Training and conversion live in their own submodules: gen_worker.trainer, gen_worker.conversion, gen_worker.clone.

Migrating 0.6.x → 0.7.0

The 0.7.0 cut replaces the Annotated[T, ModelRef(...)] injection pattern and the endpoint.toml [models] table with a single models={...} kwarg on @inference_function. ResourceRequirements and ScalingHints merged into one Resources struct (declared per function). The require_vram / require_compute_capability / require_cuda_library runtime helpers are gone — the worker now boot-checks each function's Resources envelope against host hardware and self-advertises only runnable functions.

# 0.6.x:
from gen_worker import ModelRef, ResourceRequirements, ScalingHints, inference_function
from gen_worker.capability import require_vram

@inference_function(
    resources=ResourceRequirements(min_vram_gb=4.0),
    scaling_hints=ScalingHints(vram_scales_with=("width", "height")),
)
def generate(
    ctx,
    pipe: Annotated[FluxPipeline, ModelRef(Src.FIXED, ref="acme/flux", flavor="bf16")],
    payload: Input,
) -> Output:
    require_vram(22 * 1024**3)
    ...

# 0.7.0:
from gen_worker import Repo, Resources, inference_function

flux = Repo("acme/flux")

@inference_function(
    resources=Resources(
        requires_gpu=True,
        min_vram_gb=22.0,
        vram_scales_with=("width", "height"),
    ),
    models={"pipe": flux.flavor("bf16")},
)
def generate(ctx, pipe: FluxPipeline, payload: Input) -> Output:
    ...

Bare imports of the removed symbols (ModelRef, ModelRefSource, Src, ResourceRequirements, ScalingHints, require_vram, etc.) raise ImportError with a one-line migration pointer.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gen_worker-0.7.0.tar.gz (376.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gen_worker-0.7.0-py3-none-any.whl (429.7 kB view details)

Uploaded Python 3

File details

Details for the file gen_worker-0.7.0.tar.gz.

File metadata

  • Download URL: gen_worker-0.7.0.tar.gz
  • Upload date:
  • Size: 376.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gen_worker-0.7.0.tar.gz
Algorithm Hash digest
SHA256 92b57e43c602c84f74f5bf0b9519b1295873fc06564a34219e3a90f09efb20d8
MD5 d667db4603b77fe7854e59e64d36c760
BLAKE2b-256 df1d0d13358885e8099e1865e55ff53c09869530ba7a6b1bb58fdee8b9752bd5

See more details on using hashes here.

File details

Details for the file gen_worker-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: gen_worker-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 429.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for gen_worker-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7e3adbc5e04decdc52cd0abad96e22713d221ddc5b635a89e093da2bc676e51
MD5 fee9419001463b1a36f51d740b686d11
BLAKE2b-256 660978ebf69006398d7836c679c6d70284721a0fcf87a2e4ded92ef4e47e1a52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page