CrossEncoderConfig
- class lightning_ir.cross_encoder.config.CrossEncoderConfig(query_length: int = 32, doc_length: int = 512, pooling_strategy: 'first' | 'mean' | 'max' | 'sum' = 'first', linear_bias: bool = False, **kwargs)[source]
Bases:
LightningIRConfig
- __init__(query_length: int = 32, doc_length: int = 512, pooling_strategy: 'first' | 'mean' | 'max' | 'sum' = 'first', linear_bias: bool = False, **kwargs)[source]
Configuration class for a cross-encoder model
- Parameters:
query_length (int, optional) – Maximum query length, defaults to 32
doc_length (int, optional) – Maximum document length, defaults to 512
pooling_strategy (Literal['first', 'mean', 'max', 'sum'], optional) – Pooling strategy to aggregate the contextualized embeddings into a single vector for computing a relevance score, defaults to “first”
linear_bias (bool, optional) – Whether to use a bias in the prediction linear layer, defaults to False
Methods
__init__
([query_length, doc_length, ...])Configuration class for a cross-encoder model
Checks whether the passed dictionary and its nested dicts have a torch_dtype key and if it's not None, converts torch.dtype to a string of just the type.
from_dict
(config_dict, **kwargs)Instantiates a [PretrainedConfig] from a Python dictionary of parameters.
from_json_file
(json_file)Instantiates a [PretrainedConfig] from the path to a JSON file of parameters.
from_pretrained
(...)Loads the configuration from a pretrained model.
get_config_dict
(...)From a pretrained_model_name_or_path, resolve to a dictionary of parameters, to be used for instantiating a [PretrainedConfig] using from_dict.
get_text_config
([decoder])Returns the config that is meant to be used with text IO.
push_to_hub
(repo_id[, use_temp_dir, ...])Upload the configuration file to the 🤗 Model Hub.
register_for_auto_class
([auto_class])Register this class with a given auto class.
save_pretrained
(save_directory[, push_to_hub])Save a configuration object to the directory save_directory, so that it can be re-loaded using the [~PretrainedConfig.from_pretrained] class method.
Outputs a dictionary of the added arguments.
to_dict
()Overrides the transformers.PretrainedConfig.to_dict method to include the added arguments and the backbone model type.
Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary.
to_json_file
(json_file_path[, use_diff])Save this instance to a JSON file.
to_json_string
([use_diff])Serializes this instance to a JSON string.
Outputs a dictionary of the tokenizer arguments.
update
(config_dict)Updates attributes of this class with attributes from config_dict.
update_from_string
(update_str)Updates attributes of this class with attributes from update_str.
Attributes
Arguments added to the configuration.
TOKENIZER_ARGS
Arguments for the tokenizer.
attribute_map
backbone_model_type
Backbone model type for the configuration.
base_config_key
base_model_pp_plan
base_model_tp_plan
is_composition
Model type for cross-encoder models.
name_or_path
The number of labels for classification models.
sub_configs
Whether or not return [~utils.ModelOutput] instead of tuples.
- ADDED_ARGS: Set[str] = {'doc_length', 'linear_bias', 'pooling_strategy', 'query_length'}
Arguments added to the configuration.
- dict_torch_dtype_to_str(d: Dict[str, Any]) None
Checks whether the passed dictionary and its nested dicts have a torch_dtype key and if it’s not None, converts torch.dtype to a string of just the type. For example, torch.float32 get converted into “float32” string, which can then be stored in the json format.
- classmethod from_dict(config_dict: Dict[str, Any], **kwargs) PretrainedConfig
Instantiates a [PretrainedConfig] from a Python dictionary of parameters.
- Parameters:
config_dict (Dict[str, Any]) – Dictionary that will be used to instantiate the configuration object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [~PretrainedConfig.get_config_dict] method.
kwargs (Dict[str, Any]) – Additional parameters from which to initialize the configuration object.
- Returns:
The configuration object instantiated from those parameters.
- Return type:
[PretrainedConfig]
- classmethod from_json_file(json_file: str | PathLike) PretrainedConfig
Instantiates a [PretrainedConfig] from the path to a JSON file of parameters.
- Parameters:
json_file (str or os.PathLike) – Path to the JSON file containing the parameters.
- Returns:
The configuration object instantiated from that JSON file.
- Return type:
[PretrainedConfig]
- classmethod from_pretrained(pretrained_model_name_or_path: str | Path, *args, **kwargs) LightningIRConfig
Loads the configuration from a pretrained model. Wraps the transformers.PretrainedConfig.from_pretrained
- Parameters:
pretrained_model_name_or_path (str | Path) – Pretrained model name or path
- Raises:
ValueError – If pre_trained_model_name_or_path is not a Lightning IR model and no
LightningIRConfig
is passed- Returns:
Derived LightningIRConfig class
- Return type:
- classmethod get_config_dict(pretrained_model_name_or_path: str | PathLike, **kwargs) Tuple[Dict[str, Any], Dict[str, Any]]
From a pretrained_model_name_or_path, resolve to a dictionary of parameters, to be used for instantiating a [PretrainedConfig] using from_dict.
- Parameters:
pretrained_model_name_or_path (str or os.PathLike) – The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.
- Returns:
The dictionary(ies) that will be used to instantiate the configuration object.
- Return type:
Tuple[Dict, Dict]
- get_text_config(decoder=False) PretrainedConfig
Returns the config that is meant to be used with text IO. On most models, it is the original config instance itself. On specific composite models, it is under a set of valid names.
If decoder is set to True, then only search for decoder config names.
- push_to_hub(repo_id: str, use_temp_dir: bool | None = None, commit_message: str | None = None, private: bool | None = None, token: bool | str | None = None, max_shard_size: int | str | None = '5GB', create_pr: bool = False, safe_serialization: bool = True, revision: str = None, commit_description: str = None, tags: List[str] | None = None, **deprecated_kwargs) str
Upload the configuration file to the 🤗 Model Hub.
- Parameters:
repo_id (str) – The name of the repository you want to push your config to. It should contain your organization name when pushing to a given organization.
use_temp_dir (bool, optional) – Whether or not to use a temporary directory to store the files saved before they are pushed to the Hub. Will default to True if there is no directory named like repo_id, False otherwise.
commit_message (str, optional) – Message to commit while pushing. Will default to “Upload config”.
private (bool, optional) – Whether to make the repo private. If None (default), the repo will be public unless the organization’s default is private. This value is ignored if the repo already exists.
token (bool or str, optional) – The token to use as HTTP bearer authorization for remote files. If True, will use the token generated when running huggingface-cli login (stored in ~/.huggingface). Will default to True if repo_url is not specified.
max_shard_size (int or str, optional, defaults to “5GB”) – Only applicable for models. The maximum size for a checkpoint before being sharded. Checkpoints shard will then be each of size lower than this size. If expressed as a string, needs to be digits followed by a unit (like “5MB”). We default it to “5GB” so that users can easily load models on free-tier Google Colab instances without any CPU OOM issues.
create_pr (bool, optional, defaults to False) – Whether or not to create a PR with the uploaded files or directly commit.
safe_serialization (bool, optional, defaults to True) – Whether or not to convert the model weights in safetensors format for safer serialization.
revision (str, optional) – Branch to push the uploaded files to.
commit_description (str, optional) – The description of the commit that will be created
tags (List[str], optional) – List of tags to push on the Hub.
Examples:
```python from transformers import AutoConfig
config = AutoConfig.from_pretrained(“google-bert/bert-base-cased”)
# Push the config to your namespace with the name “my-finetuned-bert”. config.push_to_hub(“my-finetuned-bert”)
# Push the config to an organization with the name “my-finetuned-bert”. config.push_to_hub(“huggingface/my-finetuned-bert”) ```
- classmethod register_for_auto_class(auto_class='AutoConfig')
Register this class with a given auto class. This should only be used for custom configurations as the ones in the library are already mapped with AutoConfig.
<Tip warning={true}>
This API is experimental and may have some slight breaking changes in the next releases.
</Tip>
- Parameters:
auto_class (str or type, optional, defaults to “AutoConfig”) – The auto class to register this new configuration with.
- save_pretrained(save_directory: str | PathLike, push_to_hub: bool = False, **kwargs)
Save a configuration object to the directory save_directory, so that it can be re-loaded using the [~PretrainedConfig.from_pretrained] class method.
- Parameters:
save_directory (str or os.PathLike) – Directory where the configuration JSON file will be saved (will be created if it does not exist).
push_to_hub (bool, optional, defaults to False) – Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with repo_id (will default to the name of save_directory in your namespace).
kwargs (Dict[str, Any], optional) – Additional key word arguments passed along to the [~utils.PushToHubMixin.push_to_hub] method.
- to_added_args_dict() Dict[str, Any]
Outputs a dictionary of the added arguments.
- Returns:
Added arguments
- Return type:
Dict[str, Any]
- to_dict() Dict[str, Any]
Overrides the transformers.PretrainedConfig.to_dict method to include the added arguments and the backbone model type.
- Returns:
Configuration dictionary
- Return type:
Dict[str, Any]
- to_diff_dict() Dict[str, Any]
Removes all attributes from config which correspond to the default config attributes for better readability and serializes to a Python dictionary.
- Returns:
Dictionary of all the attributes that make up this configuration instance,
- Return type:
Dict[str, Any]
- to_json_file(json_file_path: str | PathLike, use_diff: bool = True)
Save this instance to a JSON file.
- Parameters:
json_file_path (str or os.PathLike) – Path to the JSON file in which this configuration instance’s parameters will be saved.
use_diff (bool, optional, defaults to True) – If set to True, only the difference between the config instance and the default PretrainedConfig() is serialized to JSON file.
- to_json_string(use_diff: bool = True) str
Serializes this instance to a JSON string.
- Parameters:
use_diff (bool, optional, defaults to True) – If set to True, only the difference between the config instance and the default PretrainedConfig() is serialized to JSON string.
- Returns:
String containing all the attributes that make up this configuration instance in JSON format.
- Return type:
str
- to_tokenizer_dict() Dict[str, Any]
Outputs a dictionary of the tokenizer arguments.
- Returns:
Tokenizer arguments
- Return type:
Dict[str, Any]
- update(config_dict: Dict[str, Any])
Updates attributes of this class with attributes from config_dict.
- Parameters:
config_dict (Dict[str, Any]) – Dictionary of attributes that should be updated for this class.
- update_from_string(update_str: str)
Updates attributes of this class with attributes from update_str.
The expected format is ints, floats and strings as is, and for booleans use true or false. For example: “n_embd=10,resid_pdrop=0.2,scale_attn_weights=false,summary_type=cls_index”
The keys to change have to already exist in the config object.
- Parameters:
update_str (str) – String with attributes that should be updated for this class.