Loading¶
trimbed.loading
¶
Loading tokenizers and models.
load_tokenizer
¶
load_tokenizer(
model: str,
revision: str | None = None,
trust_remote_code: bool = False,
) -> PreTrainedTokenizerFast
Load a tokenizer as a fast tokenizer, converting it if necessary.
Tokenizers distributed only as a SentencePiece model (mT5's spiece.model, say)
are converted to the tokenizers format on load so we can trim them following
the same code path as everything else.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Hub model id or local path, e.g. |
required |
revision
|
str | None
|
Optional revision to pin, e.g. |
None
|
trust_remote_code
|
bool
|
Allow tokenizer code shipped with the checkpoint. |
False
|
Returns:
| Type | Description |
|---|---|
PreTrainedTokenizerFast
|
A |
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
If conversion needs |
ValueError
|
If the result is not backed by a |
require_torch
¶
Import and return torch or explain how to install it.
Returns:
| Type | Description |
|---|---|
ModuleType
|
The imported |
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
If torch is not installed. |
resolve_model_class
¶
resolve_model_class(
model: str,
revision: str | None = None,
config: EmbeddingTrimConfig | None = None,
trust_remote_code: bool = False,
) -> type[PreTrainedModel] | type[AutoModel]
Find the model class to load a checkpoint with.
The class named in config.architectures is what the checkpoint actually contains,
so that is what gets loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Hub model id or local path. |
required |
revision
|
str | None
|
Optional revision to pin. |
None
|
config
|
EmbeddingTrimConfig | None
|
Embedding-trimming settings. Set |
None
|
trust_remote_code
|
bool
|
Allow modelling code shipped with the checkpoint. |
False
|
Returns:
| Type | Description |
|---|---|
A class exposing `from_pretrained`, read off `config.architectures`
|
e.g.
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
load_model
¶
load_model(
model: str,
revision: str | None = None,
config: EmbeddingTrimConfig | None = None,
trust_remote_code: bool = False,
) -> PreTrainedModel
Load a model for embedding trimming.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Hub model id or local path. |
required |
revision
|
str | None
|
Optional revision to pin. |
None
|
config
|
EmbeddingTrimConfig | None
|
Embedding-trimming settings, which control the class, dtype and
placement, e.g. |
None
|
trust_remote_code
|
bool
|
Allow modelling code shipped with the checkpoint, as the gte and jina encoders need. |
False
|
Returns:
| Type | Description |
|---|---|
PreTrainedModel
|
A |