Skip to content

Seeding

How experiment seeds reach each model back-end; see Models.

rupsycho.seeding

Reproducible sampling: map an experiment seed onto each model back-end.

Every model call of an experiment carries a seed (parameters.seeds). How that seed reaches the model differs per back-end, and silently ignoring it would defeat the purpose of a seeded experiment:

  • Local Hugging Face pipelines have no seed argument. Their sampling is driven by global random number generators, so the seed is applied with transformers.set_seed right before every call.
  • API / server back-ends with a seed field (OpenAI, DeepSeek, Ollama, ...) receive the seed through a copy of the model with that field set; chat models around a Hugging Face endpoint receive it as a request parameter.
  • Back-ends without any seed support (e.g. Google Gemini) cannot be seeded. A warning is emitted once per model type; repetitions are then independent samples.

Custom model types can be registered with :func:register_seeder.

Seeder module-attribute

Seeder = Callable[[Any, int], Runnable]

A function (model, seed) -> seeded runnable.

register_seeder

register_seeder(model_type: type, seeder: Seeder) -> None

Register how models of model_type are seeded.

The most recent registration wins and takes precedence over the built-in strategies.

PARAMETER DESCRIPTION
model_type

The model class (subclasses match too).

TYPE: type

seeder

Function (model, seed) returning a runnable that behaves like model but samples reproducibly for seed.

TYPE: Seeder

Example
from rupsycho.seeding import register_seeder

register_seeder(MyModel, lambda model, seed: model.model_copy(update={"rng_seed": seed}))

is_thread_safe

is_thread_safe(model: Any) -> bool

Whether calls to model may run concurrently in several threads.

Local pipelines rely on process-global state (random generators, accelerator memory) and are therefore not thread safe; API clients are.

supports_seeding

supports_seeding(model: Any) -> bool

Whether :func:seed_model can make model reproducible.

seed_model

seed_model(model: Any, seed: int) -> Runnable

Return a runnable that samples from model reproducibly for seed.

PARAMETER DESCRIPTION
model

The model (any LangChain runnable).

TYPE: Any

seed

The seed of the current experiment run.

TYPE: int

RETURNS DESCRIPTION
Runnable

A runnable to use in place of model. For models that cannot be seeded the model

Runnable

itself is returned and a warning is emitted (once per model type).

Example
chain = prompt | seed_model(model, 42) | parser