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

Chameleon: add model #31534

Merged
merged 86 commits into from
Jul 17, 2024
Merged

Chameleon: add model #31534

merged 86 commits into from
Jul 17, 2024

Conversation

zucchini-nlp
Copy link
Member

@zucchini-nlp zucchini-nlp commented Jun 21, 2024

What does this PR do?

Fixes #31505.

Adds Chameleon, a vision language model from Meta AI.

from transformers import ChameleonForCausalLM, ChameleonProcessor
from PIL import Image
import requests
import torch

model_path = "MODEL_PATH"
model = ChameleonForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, low_cpu_mem_usage=True, device_map="auto")
processor = ChameleonProcessor.from_pretrained(model_path)

prompt = "I'm very intrigued by this work of art:<image>Please tell me about the artist."
image = Image.open(requests.get("https://uploads4.wikiart.org/images/paul-klee/death-for-the-idea-1915.jpg!Large.jpg", stream=True).raw)

inputs = processor(prompt, images=[image], return_tensors="pt").to(model.device, dtype=torch.bfloat16)
out = model.generate(**inputs, max_new_tokens=40, do_sample=False)
generated_text = processor.batch_decode(out, skip_special_tokens=False)[0]
print(f"Generated text: {generated_text}")

# Multi-image example
prompt = "I used to know a lot about constellations when I was younger, but as I grew older, I forgot most of what I knew. These are the only two constellations that I really remember now.<image><image>I would like for you to tell me about 3 more constellations and give me a little bit of history about the constellation."
image = Image.open(requests.get("https://nineplanets.org/wp-content/uploads/2020/12/the-big-dipper-1.jpg", stream=True).raw)
image_2 = Image.open(requests.get("https://www.kxan.com/wp-content/uploads/sites/40/2020/10/ORION.jpg", stream=True).raw)

inputs = processor(prompt, images=[image, image_2], return_tensors="pt").to(model.device, dtype=torch.bfloat16)
out = model.generate(**inputs, max_new_tokens=200, do_sample=False)
generated_text = processor.batch_decode(out, skip_special_tokens=True)[0]
print(f"Generated text: {generated_text}")
>>> 

Project repo: https://github.com/facebookresearch/chameleon
Paper: https://arxiv.org/abs/2405.09818v1

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@jsm69
Copy link

jsm69 commented Jun 22, 2024

@amyeroberts @ArthurZucker

Comment on lines 308 to 310
# we need to expand on num_heads because there was not sharding done in 7B model
# and we need to calculate mean/var over each head_dim
# for sharded model we don't do expansion and simply do norm
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should be able to bake that by updating the alpha and beta for model parallelisme

Comment on lines 319 to 322
# permute key/value to use transformers RoPE implementation (see for more: https://github.com/huggingface/transformers/issues/25199)
# NOTE: permutation is done same way as in llama conversion script
key_states = key_states.view(-1, self.num_key_value_heads, self.head_dim // 2, 2).transpose(3, 2)
query_states = query_states.view(-1, self.num_heads, self.head_dim // 2, 2).transpose(3, 2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should permute everything in the weights

Comment on lines +206 to +208
def __init__(self, hidden_size, *args, **kwargs):
super().__init__(hidden_size, *args, **kwargs)
self.normalized_shape = (hidden_size[-1],)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this mean we are computing over say "head_dim" ?
How different is this from normal nn.layer_norm((head_dim,))
?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the weights are different (hidden_size) but the applied dim is hidden_size is that it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's only the weights that are different for 30B model. The 7B has simple repeated weights over all heads

src/transformers/models/chameleon/modeling_chameleon.py Outdated Show resolved Hide resolved

for token in tokenizer_config["added_tokens"]:
if token["content"] == "<reserved08707>":
token["content"] = "<image>"
Copy link
Contributor

@leloykun leloykun Jul 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zucchini-nlp @ArthurZucker We should also set token["special"] = False so that we can decode this token.

What do you guys think?

(It's what I'm currently doing in my PR btw and I haven't encountered any errors yet)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Special tokens should be decodable with skip_special_tokens=False

For my understanding, why do we need to decode the image token? Afaik it shouldn't affect image generation because it's a token we added manually to keep track of where to add an image in the text

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 great work

@zucchini-nlp zucchini-nlp merged commit 24cfcc2 into main Jul 17, 2024
27 checks passed
@zucchini-nlp zucchini-nlp deleted the chameleon branch July 17, 2024 05:41
amyeroberts added a commit to amyeroberts/transformers that referenced this pull request Jul 19, 2024
* Chameleon model integration

Co-authored-by: Jacob Kahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>

* fix 7B, again. mask away image tokens

* Apply suggestions from code review

Co-authored-by: Arthur <[email protected]>

* remove pretrained_config_map

* make fixup passing up to utils/check_config_docstrings.py; vqgan moved to the modeling file

* remove tokenizer (use llama's); remove codechameleon tests

* a few copied from statements and minor changes

* copied from in ChameleonModel

* some copies in ChameleonForCausalLM

* a few more copies

* VQModel moved to ChameleonModel (as opposed to being in the processor)

* ChameleonProcessor ready

* Fix chameleon weights convert

* update conversion script

* clean-up processing

* update modeling a bit

* update

* update (throws error...)

* correct conversion ready

* fix tests

* fix docs

* docs

* ve swin norm

* fix device for vocab map

* add normalization

* update

* update script with rope rotations

* final fix on model conversion

* add slow tests

* more info in docs

* fix repo consistency tests

* fix repo tests

* fix-copies

* hope this will make CI happy

* fix for 30b model

* Update docs/source/en/index.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/auto/configuration_auto.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* address comments

* remove assertion in conversion script

* add image processor test

* not copied

* port changes for qk layernorm

* fix-copies

* read token decorator for tests

* [run-slow] chameleon

* one more read-token

* address some comments

* qk norm changes

* tests and repo check

* moved rope permutations to conversion, YAY!

* fix past kv check

* docs

* layernorm done!

* let's be consistent in naming

* fix slow tests

* weird thing with slow CI, but let's see

* once more try

* remove past-kv as tuple following llama

* ignore

* style

---------

Co-authored-by: Pablo Montalvo <[email protected]>
Co-authored-by: ArthurZucker <[email protected]>
Co-authored-by: jacobkahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: Arthur <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: amyeroberts <[email protected]>
MHRDYN7 pushed a commit to MHRDYN7/transformers that referenced this pull request Jul 23, 2024
* Chameleon model integration

Co-authored-by: Jacob Kahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>

* fix 7B, again. mask away image tokens

* Apply suggestions from code review

Co-authored-by: Arthur <[email protected]>

* remove pretrained_config_map

* make fixup passing up to utils/check_config_docstrings.py; vqgan moved to the modeling file

* remove tokenizer (use llama's); remove codechameleon tests

* a few copied from statements and minor changes

* copied from in ChameleonModel

* some copies in ChameleonForCausalLM

* a few more copies

* VQModel moved to ChameleonModel (as opposed to being in the processor)

* ChameleonProcessor ready

* Fix chameleon weights convert

* update conversion script

* clean-up processing

* update modeling a bit

* update

* update (throws error...)

* correct conversion ready

* fix tests

* fix docs

* docs

* ve swin norm

* fix device for vocab map

* add normalization

* update

* update script with rope rotations

* final fix on model conversion

* add slow tests

* more info in docs

* fix repo consistency tests

* fix repo tests

* fix-copies

* hope this will make CI happy

* fix for 30b model

* Update docs/source/en/index.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/auto/configuration_auto.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* address comments

* remove assertion in conversion script

* add image processor test

* not copied

* port changes for qk layernorm

* fix-copies

* read token decorator for tests

* [run-slow] chameleon

* one more read-token

* address some comments

* qk norm changes

* tests and repo check

* moved rope permutations to conversion, YAY!

* fix past kv check

* docs

* layernorm done!

* let's be consistent in naming

* fix slow tests

* weird thing with slow CI, but let's see

* once more try

* remove past-kv as tuple following llama

* ignore

* style

---------

Co-authored-by: Pablo Montalvo <[email protected]>
Co-authored-by: ArthurZucker <[email protected]>
Co-authored-by: jacobkahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: Arthur <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: amyeroberts <[email protected]>
zucchini-nlp added a commit to zucchini-nlp/transformers that referenced this pull request Jul 24, 2024
* Chameleon model integration

Co-authored-by: Jacob Kahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>

* fix 7B, again. mask away image tokens

* Apply suggestions from code review

Co-authored-by: Arthur <[email protected]>

* remove pretrained_config_map

* make fixup passing up to utils/check_config_docstrings.py; vqgan moved to the modeling file

* remove tokenizer (use llama's); remove codechameleon tests

* a few copied from statements and minor changes

* copied from in ChameleonModel

* some copies in ChameleonForCausalLM

* a few more copies

* VQModel moved to ChameleonModel (as opposed to being in the processor)

* ChameleonProcessor ready

* Fix chameleon weights convert

* update conversion script

* clean-up processing

* update modeling a bit

* update

* update (throws error...)

* correct conversion ready

* fix tests

* fix docs

* docs

* ve swin norm

* fix device for vocab map

* add normalization

* update

* update script with rope rotations

* final fix on model conversion

* add slow tests

* more info in docs

* fix repo consistency tests

* fix repo tests

* fix-copies

* hope this will make CI happy

* fix for 30b model

* Update docs/source/en/index.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/auto/configuration_auto.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* address comments

* remove assertion in conversion script

* add image processor test

* not copied

* port changes for qk layernorm

* fix-copies

* read token decorator for tests

* [run-slow] chameleon

* one more read-token

* address some comments

* qk norm changes

* tests and repo check

* moved rope permutations to conversion, YAY!

* fix past kv check

* docs

* layernorm done!

* let's be consistent in naming

* fix slow tests

* weird thing with slow CI, but let's see

* once more try

* remove past-kv as tuple following llama

* ignore

* style

---------

Co-authored-by: Pablo Montalvo <[email protected]>
Co-authored-by: ArthurZucker <[email protected]>
Co-authored-by: jacobkahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: Arthur <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: amyeroberts <[email protected]>
itazap pushed a commit that referenced this pull request Jul 25, 2024
* Chameleon model integration

Co-authored-by: Jacob Kahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>

* fix 7B, again. mask away image tokens

* Apply suggestions from code review

Co-authored-by: Arthur <[email protected]>

* remove pretrained_config_map

* make fixup passing up to utils/check_config_docstrings.py; vqgan moved to the modeling file

* remove tokenizer (use llama's); remove codechameleon tests

* a few copied from statements and minor changes

* copied from in ChameleonModel

* some copies in ChameleonForCausalLM

* a few more copies

* VQModel moved to ChameleonModel (as opposed to being in the processor)

* ChameleonProcessor ready

* Fix chameleon weights convert

* update conversion script

* clean-up processing

* update modeling a bit

* update

* update (throws error...)

* correct conversion ready

* fix tests

* fix docs

* docs

* ve swin norm

* fix device for vocab map

* add normalization

* update

* update script with rope rotations

* final fix on model conversion

* add slow tests

* more info in docs

* fix repo consistency tests

* fix repo tests

* fix-copies

* hope this will make CI happy

* fix for 30b model

* Update docs/source/en/index.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update docs/source/en/model_doc/chameleon.md

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/auto/configuration_auto.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/image_processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update src/transformers/models/chameleon/processing_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* Update tests/models/chameleon/test_modeling_chameleon.py

Co-authored-by: amyeroberts <[email protected]>

* address comments

* remove assertion in conversion script

* add image processor test

* not copied

* port changes for qk layernorm

* fix-copies

* read token decorator for tests

* [run-slow] chameleon

* one more read-token

* address some comments

* qk norm changes

* tests and repo check

* moved rope permutations to conversion, YAY!

* fix past kv check

* docs

* layernorm done!

* let's be consistent in naming

* fix slow tests

* weird thing with slow CI, but let's see

* once more try

* remove past-kv as tuple following llama

* ignore

* style

---------

Co-authored-by: Pablo Montalvo <[email protected]>
Co-authored-by: ArthurZucker <[email protected]>
Co-authored-by: jacobkahn <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Leonid Shamis <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: Arthur <[email protected]>
Co-authored-by: Joao Gante <[email protected]>
Co-authored-by: amyeroberts <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Meta FAIR Chameleon 7b and 30b