Base for every Lucid model that supports the pretrained-checkpoint flow.
Subclasses inherit from_pretrained / save_pretrained
plus parameter-counting and embedding-access helpers. The contract:
- Set
config_class = MyConfigas aClassVar— required; enforced at__init__time. - Define
__init__(self, config: MyConfig) -> Nonetaking a single config argument. All architectural variation (depth, width, activation choices, …) belongs inside the config — no extra constructor parameters. - Implement
forward(...) -> ModelOutputreturning one of the dataclasses inlucid.models._output.
Attributes
config_classClassVar[type[ModelConfig] or None]None signals "not set" — the base
__init__ raises in that case.base_model_prefixClassVar[str]"bert" on
BERTForMaskedLM). Used by future state-dict remapping logic
when loading head-less checkpoints into head-bearing models.configModelConfig__init__.Notes
The class is a thin lift on lucid.nn.Module — all parameter
management still flows through the standard Module machinery.
Two persistence formats are supported when saving / loading: the
pickle-based weights.lucid (default) and the cross-framework
model.safetensors.
Examples
>>> class MyModel(PretrainedModel):
... config_class: ClassVar[type[MyConfig]] = MyConfig
... def __init__(self, config):
... super().__init__(config)
... self.linear = nn.Linear(config.hidden_size, config.num_classes)
... def forward(self, x):
... return self.linear(x)
>>> model = MyModel(MyConfig(hidden_size=128, num_classes=10))
>>> model.num_parameters()
1290Used by 55
- lucid.models
- lucid.models._auto
- lucid.models._registry
- lucid.models.generative.ddpm._model
- lucid.models.generative.ncsn._model
- lucid.models.generative.vae._model
- lucid.models.text.bert._model
- lucid.models.text.gpt._model
- lucid.models.text.gpt2._model
- lucid.models.text.roformer._model
- lucid.models.text.transformer._model
- lucid.models.vision.alexnet._model
… 43 more
Constructors
1Initialise the module and validate the supplied config.
Parameters
configModelConfigconfig_class.Raises
TypeErrorconfig_class has not been set on the concrete subclass,
or if config is not an instance of config_class.Class methods
1Load a model from a registered name or a local directory.
Two modes are supported:
- Registered name (
"resnet_50"/"resnet-50") — looked up in the global registry; the factory is invoked withpretrained=True. The factory result is validated to be an instance ofcls(so subclass calls likeResNet.from_pretrained("vit_base_16")raise rather than silently returning the wrong family). - Local directory containing
config.jsonplus eithermodel.safetensors(preferred) orweights.lucid. The config is restored viacls.config_class.loadand the weights are loaded withlucid.load.
Parameters
name_or_pathstrstrict(bool, optional, keyword - only)= Trueload_state_dict.Returns
SelfA fully constructed model instance.
Raises
ValueErrorFileNotFoundErrorTypeErrorcls,
or config_class is unset, or the weights file lacks a
state-dict.Notes
For task-aware dispatch that resolves to a different concrete
subclass per task, use the AutoModelFor* family instead.
Examples
>>> # Registered name
>>> model = ResNetForImageClassification.from_pretrained("resnet_50")
>>>
>>> # Local directory
>>> model.save_pretrained("/tmp/my_resnet50")
>>> reloaded = ResNetForImageClassification.from_pretrained("/tmp/my_resnet50")Instance methods
4Return the input-embedding submodule, or None if not applicable.
Returns
nn.Module or NoneThe embedding layer for text / token-id models (BERT, GPT,
…); None for vision and other non-embedding models.
Notes
Override in subclasses that have an embedding table. Used by tools that need to resize / share / introspect token embeddings without coupling to family-specific attribute names.
Return the total number of elements across all parameters.
Parameters
only_trainable(bool, optional, keyword - only)= FalseTrue, parameters with requires_grad=False are
excluded (useful for reporting trainable model size after
freezing the backbone).Returns
intSum of prod(p.shape) over the selected parameters.
Examples
>>> model = create_model("resnet_50")
>>> model.num_parameters()
25557032
>>> for p in model.backbone.parameters():
... p.requires_grad = False
>>> model.num_parameters(only_trainable=True) < 25557032
TrueWrite config.json and weights to path.
Parameters
pathstrsafe_serialization(bool, optional, keyword - only)= FalseTrue, save weights as model.safetensors (requires
pip install safetensors). If False, save as
weights.lucid using the native pickle-based format.Notes
Output layout:
path/
config.json
model.safetensors # when safe_serialization=True
weights.lucid # when safe_serialization=False
The companion from_pretrained (or any AutoModelFor*
class) reads this directory layout and prefers SafeTensors when
both files are present.
Examples
>>> model = create_model("resnet_50")
>>> model.save_pretrained("/tmp/my_resnet50", safe_serialization=True)Replace the input-embedding submodule.
Parameters
valuenn.ModuleRaises
NotImplementedErrorNotes
Companion to get_input_embeddings. Subclasses that
override one should override the other.