Python API
EngineSurogate EngineSurogate’s main Python API lives in the surogate._surogate extension module, built with nanobind. Use it when you want to drive training from your own Python code instead of the CLI.
Importing
Section titled “Importing”from surogate._surogate import ( DataLoader, OptimizerConfig, PretrainedConfig, RuntimeOptions, SurogateTrainer,)Trainer lifecycle (high level)
Section titled “Trainer lifecycle (high level)”A typical run looks like this:
- Build a
PretrainedConfig(model architecture) andRuntimeOptions(runtime, memory and precision). - Construct a
SurogateTrainer. - Feed token batches with
step()and run optimizer updates withupdate_with_config(). - Save checkpoints with
save_checkpoint().
Shapes and dtypes
Section titled “Shapes and dtypes”SurogateTrainer.step() expects NumPy arrays:
inputs:int32shaped[batch_size * world_size, seq_length]targets:int32shaped[batch_size * world_size, seq_length]
Useful entry points
Section titled “Useful entry points”SurogateTrainer.from_pretrained(...): create a trainer from pretrained weights and config (see the docstring in the stubs)SurogateTrainer.import_weights(path): import weights from a Hugging Face safetensors fileSurogateTrainer.export_model(path): export full model weightsSurogateTrainer.export_adapter(path, base_model_path=...): export LoRA adapters (PEFT-compatible)SurogateTrainer.step_with_kd(...)/SurogateTrainer.get_kd_loss(): knowledge-distillation micro-step with a teacher top-K signal, and the resulting KD metric (see Knowledge Distillation)SurogateTrainer.get_allocator_info(gpu_id=0): allocator stats, handy for memory debugging
Where to look next
Section titled “Where to look next”The generated typing stubs document the full API:
surogate/_surogate.pyi
See also
Section titled “See also”- CLI reference: the same training workflows driven from YAML configs
- Config reference: every config option with its default
- Back to docs index