Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Passing through kwargs to TransformersTokenizer #922

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions guidance/models/transformers/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
from .._model import Engine, Model
from .._tokenizer import Tokenizer

# Formed by comparing model and tokenizer from_pretrained methods
# transformers/models/auto/auto_factory.py
# transformers/models/auto/tokenization_auto.py
_COMMON_TRANSFORMERS_KWARGS = [
"cache_dir",
"force_download",
"proxies",
"resume_download",
"revision",
"subfolder",
"trust_remote_code",
]


class TransformersTokenizer(Tokenizer):
def __init__(
Expand All @@ -33,9 +46,10 @@ def __init__(
],
chat_template=None,
ignore_bos_token=False,
**kwargs,
):
if transformers_tokenizer is None:
transformers_tokenizer = self._tokenizer(model)
transformers_tokenizer = self._tokenizer(model, **kwargs)
else:
is_ptt = isinstance(transformers_tokenizer, transformers_package.PreTrainedTokenizer)
is_ptt_fast = isinstance(
Expand Down Expand Up @@ -232,8 +246,20 @@ def __init__(self, model, tokenizer, compute_log_probs: bool, chat_template=None
if self.model_obj.config.model_type in ["phi3"]:
self._disable_retokenize_check = True

# Automatically fill common args between Transformers
# model and tokenizer
passed_common_kwargs = {}
for arg_name in _COMMON_TRANSFORMERS_KWARGS:
if arg_name in kwargs:
passed_common_kwargs[arg_name] = kwargs[arg_name]

# Create the tokenizer
my_tokenizer = TransformersTokenizer(
model, tokenizer, chat_template, **passed_common_kwargs
)

super().__init__(
TransformersTokenizer(model, tokenizer, chat_template),
my_tokenizer,
compute_log_probs=compute_log_probs,
)
assert self._token_trie.match
Expand Down