micm_nlp.models.model¶
MODEL — build the backbone and keep track of where it lives on disk.
Two construction paths. In finetune/test mode the checkpoint named by
model.pretrained is loaded through the class in model.pretrained.cls, with
model.pretrained.args passed to it verbatim; in train mode the model is
initialised from scratch from the model.init block. Either way MODEL injects
the kwargs the task implies — num_labels for classification — and afterwards
records derived properties: parameter counts, maximum sequence length, embedding
dimension.
The remainder of the class is checkpoint bookkeeping. Every model gets a uuid4, and the lookup helpers resolve a uuid back to a filesystem path, so a later run can point at an earlier one by identifier rather than by path.
PEFT is applied separately, by micm_nlp.models.peft.
Classes¶
The backbone: built or loaded from config, then wrapped with PEFT if asked. |
Module Contents¶
- class micm_nlp.models.model.MODEL(config)¶
The backbone: built or loaded from config, then wrapped with PEFT if asked.
Construction does the whole job – paths are resolved and the model is set up in
__init__, so an instance is ready to hand toTRAINER. The HuggingFace model itself is reachable ashf.Which class is instantiated comes from YAML:
model.pretrained.cls(or theinitblock when training from scratch) is resolved by name againstCLS_SOURCE_MODULES, so adding a backbone normally needs no code here.The static half of this class is a small registry over
artefacts/models: models are stored in UUID-named directories, andfind_path_by_uuid4()and friends locate one and remember where it was.Resolve paths and build the model.
The config is deep-copied, so runtime fields written here (
uuid4,param_size) do not leak back into the caller’s object.- Parameters:
config – the validated run config.
- static extract_uuid_from_name(name)¶
Pull the leading UUID out of a run-directory name, or
None.Only the canonical hyphenated form is accepted, so a directory that merely starts with hex is not mistaken for one named by UUID.
- static find_path_by_uuid4(uuid4, root_path=None)¶
Find the model directory whose name starts with
uuid4.Checks the environment cache first, then walks
artefacts/models. The result is cached for the rest of the process.- Parameters:
uuid4 – the model’s UUID.
root_path – directory to search; defaults to
models_dir().
- Raises:
Exception – if no directory matches, or if more than one does – an ambiguous UUID is a corrupt run tree, not something to guess at.
- static get_base_model(model)¶
Unwrap a PEFT model down to the backbone it wraps.
Tries
get_base_model(), then abase_modelattribute, then returns the model unchanged – so it is safe to call on an unwrapped model.
- static get_last_checkpoint(path)¶
Highest
checkpoint-Nstep number underpath, orNone.Compares N numerically, so
checkpoint-1000beatscheckpoint-999.
- static get_last_checkpoint_path(path, last_checkpoint=None)¶
Path of the newest checkpoint under
path.Falls back to
pathitself when there are no checkpoint directories – which is what a final saved model looks like.
- static get_last_checkpoint_path_by_uuid4(source_model_uuid4)¶
Locate a model directory by UUID and return its newest checkpoint.
- static get_path_by_uuid4_from_envs(uuid4)¶
Read back a path cached by
store_path_by_uuid4_in_envs().
- static get_uuid_path_dict(root_path=None)¶
Map every model UUID under
root_pathto its directory.One walk instead of many: cheaper than calling
find_path_by_uuid4()for each of a long list of models. The key is the part of the directory name before the first_.
- print_named_parameters(requires_grad=None, model=None)¶
Print each parameter’s name,
requires_gradand mean.The quickest way to check that PEFT froze what it should have.
- Parameters:
requires_grad – print only parameters with this flag;
Noneprints all of them.model – model to inspect; defaults to this one.
- reinit(config)¶
Rebuild this instance from a different config, in place.
Used to swap models between phases of a run without dropping the object the surrounding code holds.
- static store_path_by_uuid4_in_envs(uuid4, path)¶
Cache a resolved model path in
MODEL_PATH_<uuid4>.The cache is the environment because the walk in
find_path_by_uuid4()is expensive and a run resolves the same UUID repeatedly. It lives only for the process.
- CLS_SOURCE_MODULES: ClassVar[list[str]] = ['transformers']¶
- checkpoint_pref = 'checkpoint-'¶
- property hf¶
The underlying HuggingFace model – PEFT-wrapped if PEFT is configured.