11# Module Architecture Guide
22
33This guide documents the internal module structure of ` mobius ` ,
4- covering model registration, decoder layer extension, weight loading security,
5- and backward compatibility.
4+ covering model registration, decoder layer extension, and weight loading security.
65
76## Module Overview
87
9- The original ` _exporter.py ` monolith (1,188 lines) has been split into five
10- focused modules:
8+ The original ` _exporter.py ` monolith (1,188 lines) has been split into focused
9+ modules:
1110
1211| Module | Responsibility |
1312| ---| ---|
1413| ` _registry.py ` | Model registration — maps HuggingFace ` model_type ` strings to ` nn.Module ` subclasses |
15- | ` _config_resolver.py ` | Resolves HuggingFace ` PretrainedConfig ` objects to internal ` BaseModelConfig ` subclasses |
16- | ` _weight_loading.py ` | Downloads and applies safetensors weights to ONNX IR models |
17- | ` _builder.py ` | Core build API — ` build() ` and ` build_from_module() ` |
18- | ` _diffusers_builder.py ` | Builds ONNX models from diffusers pipelines (Flux, SD3, VAEs) |
14+ | ` integrations/transformers/_config_resolver.py ` | Resolves HuggingFace ` PretrainedConfig ` objects to internal ` BaseModelConfig ` subclasses |
15+ | ` integrations/_weight_loading.py ` | Downloads and applies safetensors weights to ONNX IR models |
16+ | ` _builder.py ` | Ecosystem-agnostic graph construction via ` build_from_module() ` |
17+ | ` integrations/transformers/_builder.py ` | Transformers checkpoint orchestration |
18+ | ` integrations/diffusers/_builder.py ` | Diffusers pipeline orchestration |
1919
2020### Dependency graph
2121
2222```
23- _builder.py ← build(), build_from_module ()
23+ integrations/transformers/ _builder.py ← build_transformers_model ()
2424 ├── _registry.py
25- ├── _config_resolver.py
26- └── _weight_loading.py
27- _diffusers_builder.py
25+ ├── integrations/transformers/_config_resolver.py
26+ ├── integrations/_weight_loading.py
27+ └── _builder.py (build_from_module, resolve_dtype)
28+ integrations/diffusers/_builder.py
2829 ├── _builder.py (build_from_module, resolve_dtype)
29- └── _weight_loading.py
30+ └── integrations/ _weight_loading.py
3031```
3132
3233---
@@ -290,7 +291,8 @@ apply_weights(model, state_dict)
290291
291292### For diffusers components
292293
293- ` _diffusers_builder.py ` uses ` _download_diffusers_component_weights() ` which
294+ ` integrations/diffusers/_builder.py ` uses
295+ ` _download_diffusers_component_weights() ` which
294296follows the same safetensors-only pattern. It looks for:
295297- ` {component}/diffusion_pytorch_model.safetensors(.index.json) `
296298- ` {component}/model.safetensors(.index.json) `
@@ -312,21 +314,23 @@ follows the same safetensors-only pattern. It looks for:
312314All code imports directly from the specific module that owns the symbol:
313315
314316``` python
315- from mobius._builder import build, build_from_module
317+ from mobius._builder import build_from_module
316318from mobius._registry import registry
317- from mobius._weight_loading import apply_weights
318- from mobius._diffusers_builder import build_diffusers_pipeline
319+ from mobius.integrations._weight_loading import apply_weights
320+ from mobius.integrations.diffusers._builder import build_diffusers_pipeline
321+ from mobius.integrations.transformers._builder import build_transformers_model
319322```
320323
321324### Module → symbols reference
322325
323326| Source module | Symbols |
324327| ---| ---|
325- | ` _builder ` | ` DTYPE_MAP ` , ` build ` , ` build_from_module ` , ` resolve_dtype ` , ` _cast_module_dtype ` , ` _DEFAULT_PASSES ` , ` _optimize ` |
326- | ` _config_resolver ` | ` _config_from_hf ` , ` _default_task_for_model ` , ` _dict_to_pretrained_config ` , ` _try_load_config_json ` |
327- | ` _diffusers_builder ` | ` build_diffusers_pipeline ` , ` _DIFFUSERS_CLASS_MAP ` , ` _download_diffusers_component_weights ` , ` _init_diffusers_class_map ` , ` _load_diffusers_component_config ` , ` _load_diffusers_pipeline_index ` |
328+ | ` _builder ` | ` DTYPE_MAP ` , ` build_from_module ` , ` resolve_dtype ` , ` _cast_module_dtype ` |
329+ | ` integrations.transformers._builder ` | ` build_transformers_model ` |
330+ | ` integrations.transformers._config_resolver ` | ` _config_from_hf ` , ` _default_task_for_model ` , ` _dict_to_pretrained_config ` , ` _try_load_config_json ` |
331+ | ` integrations.diffusers._builder ` | ` build_diffusers_pipeline ` , ` _DIFFUSERS_CLASS_MAP ` , ` _download_diffusers_component_weights ` , ` _init_diffusers_class_map ` , ` _load_diffusers_component_config ` , ` _load_diffusers_pipeline_index ` |
328332| ` _registry ` | ` MODEL_MAP ` , ` ModelRegistration ` , ` ModelRegistry ` , ` registry ` |
329- | ` _weight_loading ` | ` apply_weights ` , ` _download_weights ` , ` _parallel_download ` |
333+ | ` integrations. _weight_loading` | ` apply_weights ` , ` _download_weights ` , ` _parallel_download ` |
330334
331335---
332336
@@ -335,7 +339,7 @@ from mobius._diffusers_builder import build_diffusers_pipeline
335339### Build a model from HuggingFace
336340
337341``` python
338- from mobius._builder import build
342+ from mobius import build
339343
340344pkg = build(" meta-llama/Llama-3-8B" )
341345pkg.save(" /output/llama/" )
@@ -344,7 +348,7 @@ pkg.save("/output/llama/")
344348### Build from a custom module
345349
346350``` python
347- from mobius._builder import build_from_module
351+ from mobius import build_from_module
348352
349353module = MyCausalLMModel(config)
350354pkg = build_from_module(module, config, task = " text-generation" )
@@ -353,8 +357,8 @@ pkg = build_from_module(module, config, task="text-generation")
353357### Register and build a custom architecture
354358
355359``` python
360+ from mobius import build
356361from mobius._registry import registry
357- from mobius._builder import build
358362
359363registry.register(" my_arch" , MyCausalLMModel)
360364pkg = build(" my-org/my-model" ) # auto-detects "my_arch" from config.json
@@ -363,8 +367,8 @@ pkg = build("my-org/my-model") # auto-detects "my_arch" from config.json
363367### Apply weights separately
364368
365369``` python
366- from mobius._builder import build
367- from mobius._weight_loading import apply_weights, _download_weights
370+ from mobius import build
371+ from mobius.integrations. _weight_loading import apply_weights, _download_weights
368372
369373pkg = build(" meta-llama/Llama-3-8B" , load_weights = False )
370374state_dict = _download_weights(" meta-llama/Llama-3-8B" )
0 commit comments