Hugging Face export
Export a Mammoth checkpoint to Hugging Face format and load the result (see Exporting to Hugging Face).
Export
- mammoth.hf_integration.to_hf.convert_mammoth_to_hf.convert_checkpoint(ckpt_dir: str, output_dir: str, task: str | None = None, step: int | None = None) None[source]
Discover tasks from the checkpoint’s stored opts and convert each one.
multi-task checkpoint → every task into output_dir/<src>-<tgt>/
single-task checkpoint → flat into output_dir/
- task=”src-tgt” → only that task, same layout (a subdirectory on a multi-task
checkpoint)
- mammoth.hf_integration.to_hf.convert_mammoth_to_hf.convert_multi_task_artifact(ckpt_dir: str, output_dir: str, step: int | None = None) None[source]
Convert all tasks from a multi-task checkpoint into a single bundled directory.
Each task is converted via convert() into a temp dir, then the weight files are renamed to {task}.safetensors and collected into output_dir alongside shared code files and a manifest config.json.
- mammoth.hf_integration.to_hf.convert_mammoth_to_hf.convert_modular(ckpt_dir: str, output_dir: str, step: int | None = None) None[source]
Export every Mammoth component once, so models can be assembled per task at load time.
Output layout:
config.json manifest: architecture, components, tokenizers, tasks components/<name>.safetensors one per language embedding, (stack, xcoder_id) stack, and encoder/decoder wrapper path tokenizers/{src,tgt}/<lang>/ one tokenizer per side and language *.py vendored model code + mammoth_hub.py (the loader)
Unlike –single-artifact, a component shared by several tasks is written once. Load with MammothHub.from_pretrained(output_dir, task=…) or with src/tgt/encoder/decoder.
Model
- class mammoth.hf_integration.to_hf.configuration_mammoth.MammothConfig(model_dim: int = 512, heads: int = 8, enc_ff_mult: float | None = None, dec_ff_mult: float | None = None, enc_ff_activation: str | None = None, dec_ff_activation: str | None = None, ff_mult: float = 2.67, ff_swiglu: bool = True, enc_layers: List[int] = (6,), dec_layers: List[int] = (6,), attn_dropout: float = 0.0, ff_dropout: float = 0.0, emb_dropout: float = 0.0, rotary_pos_emb: bool = True, rope_theta: float = 10000.0, post_emb_norm: bool = True, src_vocab_size: int = 32000, tgt_vocab_size: int = 32000, pad_token_id: int = 1, bos_token_id: int = 0, eos_token_id: int = 2, decoder_start_token_id: int = 2, src_tokenizer_dir: str = 'src_tokenizer', tgt_tokenizer_dir: str = 'tgt_tokenizer', encoder_sharing_groups: List[str] | None = None, decoder_sharing_groups: List[str] | None = None, use_cache: bool = True, **kwargs)[source]
Bases:
PreTrainedConfig
- class mammoth.hf_integration.to_hf.modeling_mammoth.MammothForConditionalGeneration(config: MammothConfig)[source]
Bases:
MammothPreTrainedModel,GenerationMixinSeq2seq LM wrapping native Mammoth encoder-decoder.
- forward(input_ids: LongTensor | None = None, attention_mask: Tensor | None = None, decoder_input_ids: LongTensor | None = None, decoder_attention_mask: Tensor | None = None, encoder_outputs: Tensor | None = None, past_key_values: KVCache | None = None, use_cache: bool | None = None, labels: LongTensor | None = None, **kwargs) Seq2SeqLMOutput[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_encoder()[source]
Best-effort lookup of the encoder module. If provided with modality argument, it looks for a modality-specific encoder in multimodal models (e.g. “image_encoder”) By default the function returns model’s text encoder if any, and otherwise returns self.
Possible modality values are “image”, “video” and “audio”.
- class mammoth.hf_integration.to_hf.mammoth_hub.MammothHub[source]
Bases:
objectLoads a translation model from a Mammoth HF repo (modular or legacy bundle).
- classmethod from_pretrained(model_id: str, task: str | None = None, device: str | None = None, *, src: str | None = None, tgt: str | None = None, encoder: Sequence[str] | None = None, decoder: Sequence[str] | None = None)[source]
Load one translation model.
- Parameters:
model_id – Local directory path or HF Hub repo id (e.g. “org/repo”).
task – Trained task name, e.g. “eng-spa”.
device – Device to load the model on (default: cpu).
src – Source/target language — alternative to task (modular repos only).
tgt – Source/target language — alternative to task (modular repos only).
encoder – xcoder_id per layer stack, e.g. [“eng”, “all”]. Override the task’s paths, or define new combinations (modular repos only).
decoder – xcoder_id per layer stack, e.g. [“eng”, “all”]. Override the task’s paths, or define new combinations (modular repos only).
- Returns:
A MammothForConditionalGeneration in eval mode, with .src_tokenizer and .tgt_tokenizer attached (and .mammoth_spec for modular repos).
Loading and translating
- mammoth.hf_integration.to_hf.inference.load_model(model_dir: str, task: str | None = None, src: str | None = None, tgt: str | None = None, encoder: list[str] | None = None, decoder: list[str] | None = None, device: str = 'cpu')[source]
Load any Mammoth HF repo (standalone, single-artifact or modular).
Returns (src_tokenizer, tgt_tokenizer, model). Multi-task repos need task, or src and tgt (plus optional encoder/decoder) for modular repos.