Skip to content

Commit

Permalink
rm Chameleon's slow tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
leloykun committed Aug 24, 2024
1 parent ae5d537 commit b252643
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 32 deletions.
5 changes: 1 addition & 4 deletions src/transformers/models/auto/tokenization_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@
("canine", ("CanineTokenizer", None)),
(
"chameleon",
(
"LlamaTokenizer" if is_sentencepiece_available() else None,
"LlamaTokenizerFast" if is_tokenizers_available() else None,
),
(None, "LlamaTokenizerFast" if is_tokenizers_available() else None),
),
("chinese_clip", ("BertTokenizer", "BertTokenizerFast" if is_tokenizers_available() else None)),
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from transformers import (
ChameleonConfig,
ChameleonForCausalLM,
ChameleonForConditionalGeneration,
ChameleonImageProcessor,
ChameleonProcessor,
)
Expand All @@ -49,10 +49,10 @@
Thereafter, models can be loaded via:
```py
from transformers import ChameleonForCausalLM, LlamaTokenizer
from transformers import ChameleonForConditionalGeneration, LlamaTokenizerFast
model = ChameleonForCausalLM.from_pretrained("/output/path")
tokenizer = LlamaTokenizer.from_pretrained("/output/path")
model = ChameleonForConditionalGeneration.from_pretrained("/output/path")
tokenizer = LlamaTokenizerFast.from_pretrained("/output/path")
```
Important note: you need to be able to host the whole model in RAM to execute this script (even if the biggest versions
Expand Down Expand Up @@ -372,7 +372,7 @@ def permute(w, n_heads, dim1=dim, dim2=dim):
vocabulary_map=vocabulary_map,
)
with init_empty_weights():
model = ChameleonForCausalLM(config)
model = ChameleonForConditionalGeneration(config)

model.load_state_dict(state_dict, assign=True, strict=False)
model.save_pretrained(model_path, safe_serialization=True)
Expand All @@ -397,7 +397,7 @@ def permute(w, n_heads, dim1=dim, dim2=dim):
# taken from https://github.com/facebookresearch/chameleon/blob/7a72f40aa5f462965c8374f25257f55b65b25ff4/data/prompts_for_human_evaluations.jsonl
print("Loading the checkpoint in a Chameleon model...")
print("*" * 100)
model = ChameleonForCausalLM.from_pretrained(
model = ChameleonForConditionalGeneration.from_pretrained(
model_path, attn_implementation="eager", torch_dtype=torch.bfloat16, device_map="auto"
)
processor = ChameleonProcessor.from_pretrained(model_path)
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/models/chameleon/processing_chameleon.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class ChameleonProcessor(ProcessorMixin):
"""

attributes = ["image_processor", "tokenizer"]
tokenizer_class = ("LlamaTokenizer", "LlamaTokenizerFast")
tokenizer_class = "LlamaTokenizerFast"
image_processor_class = "ChameleonImageProcessor"

def __init__(self, image_processor, tokenizer, image_seq_length: int = 1024, image_token: str = "<image>"):
Expand Down
21 changes: 0 additions & 21 deletions tests/models/chameleon/test_processor_chameleon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import unittest

from transformers import ChameleonProcessor
from transformers.models.auto.processing_auto import processor_class_from_name

from ...test_processing_common import ProcessorTesterMixin

Expand All @@ -11,26 +10,6 @@ class ChameleonProcessorTest(ProcessorTesterMixin, unittest.TestCase):
from_pretrained_id = "leloy/Anole-7b-v0.1-hf"
processor_class = ChameleonProcessor

def get_component(self, attribute, **kwargs):
assert attribute in self.processor_class.attributes
if attribute != "tokenizer":
return super().get_component(attribute, **kwargs)
# We use the fast tokenizer by default as the slow tokenizer expects the vocab file to be present in the loading directory.
# This vocab file is neither in the official repo nor does it get saved when we save the processor in `setUp` below.
component_class_name = getattr(self.processor_class, f"{attribute}_class")
if isinstance(component_class_name, tuple):
if "_fast" in component_class_name[0]:
component_class_name = component_class_name[0]
else:
component_class_name = component_class_name[1]

component_class = processor_class_from_name(component_class_name)
component = component_class.from_pretrained(self.tmpdirname, **kwargs) # noqa
if attribute == "tokenizer" and not component.pad_token:
component.pad_token = "[TEST_PAD]"

return component

def setUp(self):
self.tmpdirname = tempfile.mkdtemp()
processor = self.processor_class.from_pretrained(self.from_pretrained_id)
Expand Down

0 comments on commit b252643

Please sign in to comment.