Serving
The translation server behind mammoth_serve (see Running a Translation Server).
Model sources
- mammoth.serving.open_source(model: str, step: int | None = None) ModelSource[source]
Open a training checkpoint directory (has
*_frame.pt), or an HF repo of any Mammoth format.step picks a checkpoint step.
- class mammoth.serving.ModelSource[source]
Bases:
ABC- has_component(name: str) bool[source]
Whether the component’s file is on disk already (so loading it downloads nothing).
- abstractmethod load_component(name: str) dict[source]
State dict of one component, keyed as in the Mammoth shard (see component_to_hf).
- class mammoth.serving.CheckpointSource(ckpt_dir: str, step: int | None = None)[source]
Bases:
ModelSourceA Mammoth training checkpoint directory, read shard by shard without conversion.
- class mammoth.serving.HFSource(model_id: str, manifest: dict | None = None)[source]
Bases:
ModelSourceA modular HF repo (convert_mammoth_to_hf.py –modular); files are fetched on demand.
A Hub repo id is fetched into the HF cache. A local directory is used as is; if mammoth_download made it (its mammoth_download.json names the repo and revision), the files it lacks are downloaded into it, so the folder grows like a later mammoth_download.
- class mammoth.serving.ArtifactSource(model_id: str, config: dict | None = None)[source]
Bases:
ModelSourcePer-task HF artifacts, split back into components.
standalone (config.json model_type “mammoth”): one task, model.safetensors
bundle (model_type “mammoth_hub”, –single-artifact): {task}.safetensors per task
Each component is named from the task’s languages and sharing groups, exactly as in a modular repo, and the post-embedding norm goes to the language embedding or to the wrapper as config.json’s post_emb_norm_scope says, so tasks of a bundle share components in memory and zero-shot directions work. A component found in several task files was exported from the same checkpoint shard, so the first task’s copy is used.
Component pool
- class mammoth.serving.ComponentPool(source: ModelSource, device: str = 'cpu', dtype: dtype = torch.float32)[source]
Bases:
object- add_task(task: str) LoadedTask[source]
Load direction “src-tgt” (no-op if loaded); only missing components are read.
- available_tasks() dict[source]
Every direction this model can serve: name → {src, tgt, encoder, decoder, trained}.
Trained tasks plus every zero-shot src→tgt combination whose components exist.
- downloaded_tasks() set[str][source]
The available directions whose files are all on disk, so loading downloads nothing.
Checked afresh on each call: files may have been downloaded in the meantime.
- loaded_tasks() dict[str, LoadedTask][source]
A snapshot of the loaded tasks; safe to call while another thread loads one.
- class mammoth.serving.LoadedTask(name: str, spec: dict, trained: bool, model: mammoth.hf_integration.to_hf.modeling_mammoth.MammothForConditionalGeneration, src_tokenizer: transformers.tokenization_utils_tokenizers.TokenizersBackend, tgt_tokenizer: transformers.tokenization_utils_tokenizers.TokenizersBackend)[source]
Bases:
object
Translation engine
- class mammoth.serving.GenerationSettings(num_beams: int = 4, max_new_tokens: int = 512, max_length_factor: float = 2.0, max_length_slack: int = 10, max_batch_size: int = 32, max_batch_tokens: int = 4096)[source]
Bases:
object
- class mammoth.serving.TranslationEngine(pool: ComponentPool, settings: GenerationSettings | None = None)[source]
Bases:
object- translate(task: str, text: str | list[str], **overrides) str | list[str][source]
Translate one string or a list of strings with loaded task “src-tgt”.
overrides: any GenerationSettings field for this call only (None = keep default). Returns a string for a string input and a list for a list input. Empty or whitespace-only segments translate to “”.
HTTP API
- mammoth.serving.app.create_app(engine: TranslationEngine) FastAPI[source]