bridge.models.config
#
Module Contents#
Classes#
Protocol defining the configuration interface for model providers. |
Functions#
Load a pretrained model configuration from a directory or file. |
|
Save the model configuration to a directory. |
|
Convert an object to a dictionary representation. |
|
Recursively convert a value to a dictionary representation. |
|
Check if a configuration dictionary contains code references. |
Data#
API#
- bridge.models.config.T#
‘TypeVar(…)’
- bridge.models.config.ConfigFormat#
None
- class bridge.models.config.ConfigProtocol#
Bases:
typing.Protocol
Protocol defining the configuration interface for model providers.
- classmethod from_hf_pretrained(
- pretrained_model_name_or_path: Union[str, pathlib.Path],
- trust_remote_code: bool = False,
- mode: megatron.bridge.utils.instantiate_utils.InstantiationMode = InstantiationMode.LENIENT,
- **kwargs,
Load a pretrained model configuration from a directory or file.
- save_hf_pretrained(
- save_directory: Union[str, pathlib.Path],
- config_format: bridge.models.config.ConfigFormat | None = None,
- config_name: Optional[str] = None,
- **kwargs,
Save the model configuration to a directory.
- bridge.models.config.from_hf_pretrained(
- cls: Type[bridge.models.config.T],
- pretrained_model_name_or_path: Union[str, pathlib.Path],
- trust_remote_code: bool = False,
- mode: megatron.bridge.utils.instantiate_utils.InstantiationMode = InstantiationMode.LENIENT,
- config_name: str = 'config',
- **kwargs,
Load a pretrained model configuration from a directory or file.
- Parameters:
cls – The class to instantiate
pretrained_model_name_or_path – Path to a directory containing a config file, or direct path to a config file (yaml/json/toml)
trust_remote_code – Whether to trust and execute code references (classes/functions) found in the configuration. Required to be True if the config contains any class or function references. Default: False
mode – Instantiation mode (STRICT or LENIENT) for the instantiate function
config_name – Base name of the config file (without extension)
**kwargs – Additional keyword arguments to override loaded configuration
- Returns:
Instance of the class with loaded configuration
.. rubric:: Example
# Load from directory (looks for config.yaml, config.json, or config.toml) model = from_hf_pretrained(MyModel, "./saved_model/") # Load from specific file model = from_hf_pretrained(MyModel, "./saved_model/config.yaml") # With code references model = from_pretrained(MyModel, "./saved_model/", trust_remote_code=True) # Override configuration values model = from_pretrained(MyModel, "./saved_model/", temperature=0.8)
- bridge.models.config.save_hf_pretrained(
- obj: Any,
- save_directory: Union[str, pathlib.Path],
- config_format: bridge.models.config.ConfigFormat = 'json',
- config_name: str = 'config',
- **kwargs,
Save the model configuration to a directory.
- Parameters:
obj – The object to save
save_directory – Directory where to save the configuration
config_format – Format to save in (“yaml”, “json”, or “toml”). Default: “json”
config_name – Name for the config file (without extension)
**kwargs – Additional metadata to save alongside the configuration
.. rubric:: Example
# Save as JSON (default) save_hf_pretrained(model, "./saved_model/") # Save as YAML save_hf_pretrained(model, "./saved_model/", config_format="yaml") # Save with custom name save_hf_pretrained(model, "./saved_model/", config_name="my_config")
- bridge.models.config._to_dict(obj: Any) Dict[str, Any] #
Convert an object to a dictionary representation.
- Parameters:
obj – The object to convert
- Returns:
Dictionary representation of the object
- bridge.models.config._convert_value_to_dict(value: Any) Any #
Recursively convert a value to a dictionary representation.
- Parameters:
value – The value to convert
- Returns:
The converted value
- bridge.models.config._contains_code_references(
- config_dict: Dict[str, Any],
Check if a configuration dictionary contains code references.
- Parameters:
config_dict – The configuration dictionary to check
- Returns:
True if code references are found, False otherwise