bridge.models.hf_pretrained.base#
Module Contents#
Classes#
Abstract base class for all pretrained models. |
API#
- class bridge.models.hf_pretrained.base.PreTrainedBase(**kwargs)#
Bases:
abc.ABCAbstract base class for all pretrained models.
This class provides a generic mechanism for managing model artifacts (e.g., config, tokenizer) with lazy loading. Subclasses that are decorated with
@dataclasscan define artifacts as fields with metadata specifying a loader method. Themodelitself is handled via a dedicated property that relies on the abstract_load_modelmethod... rubric:: Example
@dataclass class MyModel(PreTrainedBase): config: AutoConfig = field( init=False, metadata=artifact(loader=”_load_config”) )
def _load_model(self) -> "PreTrainedModel": # Implementation for the loading logic ...Initialization
- model_name_or_path: Union[str, pathlib.Path]#
None
- ARTIFACTS: ClassVar[List[str]]#
[]
- OPTIONAL_ARTIFACTS: ClassVar[List[str]]#
[]
- get_artifacts() Dict[str, str]#
Get the artifacts dictionary mapping artifact names to their attribute names.
- _copy_custom_modeling_files(
- source_path: Union[str, pathlib.Path],
- target_path: Union[str, pathlib.Path],
- file_patterns: Optional[List[str]] = None,
Copy files matching patterns from source to target directory.
This preserves custom modeling files that were used during model loading with trust_remote_code=True, or copies additional files specified by patterns.
- Parameters:
source_path – Source directory or HuggingFace Hub ID containing files
target_path – Target directory to copy files to
file_patterns – Optional list of file names or patterns to copy. Supports both exact file names (e.g., “vocab.json”) and glob patterns (e.g., “*.json”). If not provided, defaults to self.custom_file_patterns.
- Returns:
List of successfully copied file names
- save_artifacts(
- save_directory: Union[str, pathlib.Path],
- original_source_path: Optional[Union[str, pathlib.Path]] = None,
- additional_files: Optional[List[str]] = None,
Saves all loaded, generic artifacts that have a
save_pretrainedmethod to the specified directory. Note: This does not save themodelattribute.If the model was loaded with trust_remote_code=True, this method will also attempt to preserve any custom modeling files to ensure the saved model can be loaded properly.
- Parameters:
save_directory – Directory to save artifacts to
original_source_path – Optional path to the original source for custom modeling files
additional_files – Optional list of additional file names or patterns to download/copy from the source. Supports both exact file names (e.g., “vocab.json”) and glob patterns (e.g., “*.json”). These files will be copied from the source path.
- abstractmethod _load_model() transformers.PreTrainedModel#
Subclasses must implement this to load the main model.
- abstractmethod _load_config() transformers.AutoConfig#
Subclasses must implement this to load the model config.
- property model: transformers.PreTrainedModel#
Lazily loads and returns the underlying model.
- property config: transformers.AutoConfig#
Lazy load and return the model config.
- property state: megatron.bridge.models.hf_pretrained.state.StateDict#
Get the state dict accessor for pandas-like querying.
This accessor can be backed by either a fully loaded model in memory or a “.safetensors” checkpoint on disk, enabling lazy loading of tensors.
.. rubric:: Examples
model.state() # Get full state dict model.state[“key”] # Get single entry model.state[[“key1”, “key2”]] # Get multiple entries model.state[”.weight”] # Glob pattern model.state.regex(r”..bias$”) # Regex pattern