| <!--Copyright 2024 The HuggingFace Team. All rights reserved. | |
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | |
| the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | |
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations under the License. | |
| โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be | |
| rendered properly in your Markdown viewer. | |
| --> | |
| # Chameleon [[chameleon]] | |
| ## ๊ฐ์ [[overview]] | |
| Chameleon ๋ชจ๋ธ์ META AI Chameleon ํ์ ๋ ผ๋ฌธ [Chameleon: Mixed-Modal Early-Fusion Foundation Models](https://huggingface.co/papers/2405.09818)์์ ์ ์๋์์ต๋๋ค. Chameleon์ ๋ฒกํฐ ์์ํ๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ ํ ํฐํํจ์ผ๋ก์จ ๋ฉํฐ๋ชจ๋ฌ ์ถ๋ ฅ์ ์์ฑํ ์ ์๋ ๋น์ -์ธ์ด ๋ชจ๋ธ์ ๋๋ค. ์ด ๋ชจ๋ธ์ ๊ต์ฐจ๋ ํ์์ ํฌํจํ ์ด๋ฏธ์ง์ ํ ์คํธ๋ฅผ ์ ๋ ฅ์ผ๋ก ๋ฐ์ผ๋ฉฐ, ํ ์คํธ ์๋ต์ ์์ฑํฉ๋๋ค. ์ด๋ฏธ์ง ์์ฑ ๋ชจ๋์ ์์ง ๊ณต๊ฐ๋์ง ์์์ต๋๋ค. | |
| ๋ ผ๋ฌธ์ ์ด๋ก์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค: | |
| *์ฐ๋ฆฌ๋ ์ด๋ฏธ์ง์ ํ ์คํธ๋ฅผ ์์์ ์์๋ก ์ดํดํ๊ณ ์์ฑํ ์ ์๋ early-fusion ํ ํฐ ๊ธฐ๋ฐ์ ํผํฉ ๋ชจ๋ฌ(mixed-modal) ๋ชจ๋ธ์ ์ผ์ข ์ธ Chameleon์ ์๊ฐํฉ๋๋ค. ์ฐ๋ฆฌ๋ ์ด๊ธฐ๋ถํฐ ์์ ์ ์ธ ํ๋ จ ์ ๊ทผ๋ฒ, ์ ๋ ฌ ๋ฐฉ๋ฒ, ๊ทธ๋ฆฌ๊ณ early-fusion, ํ ํฐ ๊ธฐ๋ฐ, ํผํฉ ๋ชจ๋ฌ ์ค์ ์ ๋ง์ถ ์ํคํ ์ฒ ๋งค๊ฐ๋ณ์๋ฅผ ์ ์ํฉ๋๋ค. ์ด ๋ชจ๋ธ๋ค์ ์๊ฐ์ ์ง๋ฌธ ์๋ต, ์ด๋ฏธ์ง ์บก์ ์์ฑ, ํ ์คํธ ์์ฑ, ์ด๋ฏธ์ง ์์ฑ, ์ฅ๋ฌธ ํผํฉ ๋ชจ๋ฌ ์์ฑ ๋ฑ ํฌ๊ด์ ์ธ ์์ ๋ฒ์์์ ํ๊ฐ๋์์ต๋๋ค. Chameleon์ ๋จ์ผ ๋ชจ๋ธ์์ ์ด๋ฏธ์ง ์บก์ ์์ฑ ์์ ์์์ ์ต์ฒจ๋จ ์ฑ๋ฅ์ ํฌํจํ ๊ด๋ฒ์ํ๊ณ ์ผ๋ฐ์ ์ผ๋ก ์ ์ฉ ๊ฐ๋ฅํ ๋ฅ๋ ฅ์ ๋ณด์ฌ์ฃผ๋ฉฐ, ํ ์คํธ ์ ์ฉ ์์ ์์ Llama-2๋ฅผ ๋ฅ๊ฐํ๋ฉด์ Mixtral 8x7B์ Gemini-Pro์ ๊ฐ์ ๋ชจ๋ธ๋ค ์ฌ์ด์์๋ ๊ฒฝ์๋ ฅ์ ๊ฐ์ถ๊ณ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์๋นํ ์ฑ๋ฅ์ ์ด๋ฏธ์ง ์์ฑ๋ ์ํํฉ๋๋ค. ๋ํ ํ๋กฌํํธ๋ ์ถ๋ ฅ์ ์ด๋ฏธ์ง์ ํ ์คํธ์ ํผํฉ ์ํ์ค๊ฐ ํฌํจ๋ ์๋ก์ด ์ฅ๋ฌธ ํผํฉ ๋ชจ๋ฌ ์์ฑ ํ๊ฐ์์, ์ธ๊ฐ์ ํ๋จ์ ๋ฐ๋ฅด๋ฉด Gemini Pro์ GPT-4V๋ฅผ ํฌํจํ ํจ์ฌ ๋ ํฐ ๋ชจ๋ธ์ ์ฑ๋ฅ๊ณผ ๋๋ฑํ๊ฑฐ๋ ์ด๋ฅผ ๋ฅ๊ฐํฉ๋๋ค. Chameleon์ ์์ ํ ๋ฉํฐ๋ชจ๋ฌ ๋ฌธ์์ ํตํฉ ๋ชจ๋ธ๋ง์์ ์ค์ํ ๋ฐ์ ์ ๋ณด์ฌ์ค๋๋ค.* | |
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/model_doc/chameleon_arch.png" | |
| alt="drawing" width="600"/> | |
| <small>Chameleon์ ์ด๋ฏธ์ง๋ฅผ ์ด์ฐ์ ์ธ ํ ํฐ์ผ๋ก ๋ณํํ๊ธฐ ์ํด ๋ฒกํฐ ์์ํ ๋ชจ๋์ ํตํฉํฉ๋๋ค. ์ด๋ ์๊ธฐํ๊ท transformer๋ฅผ ์ฌ์ฉํ ์ด๋ฏธ์ง ์์ฑ์ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค. <a href="https://huggingface.co/papers/2405.09818">์๋ณธ ๋ ผ๋ฌธ</a>์์ ๊ฐ์ ธ์์ต๋๋ค.</small> | |
| ์ด ๋ชจ๋ธ์ [joaogante](https://huggingface.co/joaogante)์ [RaushanTurganbay](https://huggingface.co/RaushanTurganbay)๊ฐ ๊ธฐ์ฌํ์ต๋๋ค. ์๋ณธ ์ฝ๋๋ [์ฌ๊ธฐ](https://github.com/facebookresearch/chameleon)์์ ์ฐพ์ ์ ์์ต๋๋ค. | |
| ## ์ฌ์ฉ ํ [[usage-tips]] | |
| - ๋ ์ ํํ ๊ฒฐ๊ณผ๋ฅผ ์ํด, ๋ฐฐ์น ์์ฑ ์ `padding_side="left"`๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ์์ฑํ๊ธฐ ์ ์ `processor.tokenizer.padding_side = "left"`๋ก ์ค์ ํ์ญ์์ค. | |
| - Chameleon์ ์์ ์ฑ ์ ๋ ฌ์ ์ํด ํ๋๋์์์ ์ ์ํ์ญ์์ค. ๋ชจ๋ธ์ด ์๋ต์ ๊ฑฐ๋ถํ๋ ๊ฒฝ์ฐ, ์ด๋ฆฐ ์ง๋ฌธ๋ณด๋ค๋ ๋ ๊ตฌ์ฒด์ ์ผ๋ก ์ง๋ฌธ์ ํด๋ณด์ธ์. | |
| - Chameleon์ ์ฑํ ํ์์ผ๋ก ์์ฑํ๋ฏ๋ก, ์์ฑ๋ ํ ์คํธ๋ ํญ์ "assistant's turn"์ผ๋ก ํ์๋ฉ๋๋ค. ํ๋ก์ธ์๋ฅผ ํธ์ถํ ๋ `return_for_text_completion=True`๋ฅผ ์ ๋ฌํ์ฌ ํ ์คํธ ์์ฑ ์์ฑ์ ํ์ฑํํ ์ ์์ต๋๋ค. | |
| > [!NOTE] | |
| > Transformers์์์ Chameleon ๊ตฌํ์ ์ด๋ฏธ์ง ์๋ฒ ๋ฉ์ ๋ณํฉํ ์์น๋ฅผ ๋ํ๋ด๊ธฐ ์ํด ํน๋ณํ ์ด๋ฏธ์ง ํ ํฐ์ ์ฌ์ฉํฉ๋๋ค. ํน๋ณํ ์ด๋ฏธ์ง ํ ํฐ์ ์ํด ์๋ก์ด ํ ํฐ์ ์ถ๊ฐํ์ง ์๊ณ ์์ฝ๋ ํ ํฐ ์ค ํ๋์ธ `<reserved08707>`๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ์ฌ๋ฐ๋ฅธ ์์ฑ์ ์ํด ํ๋กฌํํธ์์ ์ด๋ฏธ์ง๊ฐ ์๋ฒ ๋ฉ๋ ์์น์ `<image>`๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค. | |
| ## ์ฌ์ฉ ์์ [[usage-example]] | |
| ### ๋จ์ผ ์ด๋ฏธ์ง ์ถ๋ก [[single-image-inference]] | |
| Chameleon์ ๊ฒ์ดํฐ๋(gated) ๋ชจ๋ธ์ด๋ฏ๋ก Hugging Face Hub์ ๋ํ ์ก์ธ์ค ๊ถํ์ด ์๊ณ ํ ํฐ์ผ๋ก ๋ก๊ทธ์ธํ๋์ง ํ์ธํ์ธ์. ๋ค์์ ๋ชจ๋ธ์ ๋ก๋ํ๊ณ ๋ฐ์ ๋ฐ๋(`torch.bfloat16`)๋ก ์ถ๋ก ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค: | |
| ```python | |
| from transformers import ChameleonProcessor, ChameleonForConditionalGeneration | |
| import torch | |
| from PIL import Image | |
| import requests | |
| processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") | |
| model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", dtype=torch.bfloat16, device_map="cuda") | |
| # ์ด๋ฏธ์ง์ ํ ์คํธ ํ๋กฌํํธ ์ค๋น | |
| url = 'http://images.cocodataset.org/val2017/000000039769.jpg' | |
| image = Image.open(requests.get(url, stream=True).raw) | |
| prompt = "์ด ์ด๋ฏธ์ง์์ ๋ฌด์์ ๋ณด๋์?<image>" | |
| inputs = processor(images=image, text=prompt, return_tensors="pt").to(model.device, dtype=torch.bfloat16) | |
| # ํ๋กฌํํธ๋ฅผ ์๊ธฐํ๊ท์ ์ผ๋ก ์์ฑ | |
| output = model.generate(**inputs, max_new_tokens=50) | |
| print(processor.decode(output[0], skip_special_tokens=True)) | |
| ``` | |
| ### ๋ค์ค ์ด๋ฏธ์ง ์ถ๋ก [[multi-image-inference]] | |
| Chameleon์ ์ฌ๋ฌ ์ด๋ฏธ์ง๋ฅผ ์ ๋ ฅ์ผ๋ก ๋ฐ์๋ค์ด๋ฉฐ, ์ด๋ฏธ์ง๋ค์ ๋์ผํ ํ๋กฌํํธ์ ์ํ๊ฑฐ๋ ๋ค๋ฅธ ํ๋กฌํํธ์ ์ํ ์ ์์ต๋๋ค(๋ฐฐ์น ์ถ๋ก ์์). ๋ค์์ ๊ทธ ๋ฐฉ๋ฒ์ ๋๋ค: | |
| ```python | |
| from transformers import ChameleonProcessor, ChameleonForConditionalGeneration | |
| import torch | |
| from PIL import Image | |
| import requests | |
| processor = ChameleonProcessor.from_pretrained("facebook/chameleon-7b") | |
| model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", dtype=torch.bfloat16, device_map="cuda") | |
| # ์ธ ๊ฐ์ง ๋ค๋ฅธ ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ | |
| url = "https://www.ilankelman.org/stopsigns/australia.jpg" | |
| image_stop = Image.open(requests.get(url, stream=True).raw) | |
| url = "http://images.cocodataset.org/val2017/000000039769.jpg" | |
| image_cats = Image.open(requests.get(url, stream=True).raw) | |
| url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg" | |
| image_snowman = Image.open(requests.get(url, stream=True).raw) | |
| # ๋ฐฐ์น๋ ํ๋กฌํํธ ์ค๋น: ์ฒซ ๋ฒ์งธ๋ ๋ค์ค ์ด๋ฏธ์ง ํ๋กฌํํธ์ด๊ณ ๋ ๋ฒ์งธ๋ ๋จ์ผ ์ด๋ฏธ์ง ํ๋กฌํํธ์ ๋๋ค | |
| prompts = [ | |
| "์ด ์ด๋ฏธ์ง๋ค์ ๋ฌด์์ด ๊ณตํต์ ์ธ๊ฐ์?<image><image>", | |
| "<image>์ด ์ด๋ฏธ์ง์ ๋ฌด์์ด ๋ํ๋ ์๋์?" | |
| ] | |
| # ์ด๋ฏธ์ง๋ค์ ํ ์คํธ ํ๋กฌํํธ์์ ์ฌ์ฉ๋์ด์ผ ํ๋ ์์๋๋ก ์ ๋ ฅํ ์ ์์ต๋๋ค | |
| # ๊ฐ "<image>" ํ ํฐ์ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๋ฉฐ, ๋ค์ "<image>" ํ ํฐ์ ๋ค์ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํฉ๋๋ค | |
| inputs = processor(images=[image_stop, image_cats, image_snowman], text=prompts, padding=True, return_tensors="pt").to(device="cuda", dtype=torch.bfloat16) | |
| # ์์ฑ | |
| generate_ids = model.generate(**inputs, max_new_tokens=50) | |
| processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False) | |
| ``` | |
| ## ๋ชจ๋ธ ์ต์ ํ [[model-optimization]] | |
| ### Bitsandbytes๋ฅผ ์ฌ์ฉํ ์์ํ [[quantization-using-bitsandbytes]] | |
| ๋ชจ๋ธ์ 8๋นํธ ๋๋ 4๋นํธ๋ก ๋ก๋ํ ์ ์์ผ๋ฉฐ, ์ด๋ ์๋ณธ ๋ชจ๋ธ์ ์ฑ๋ฅ์ ์ ์งํ๋ฉด์ ๋ฉ๋ชจ๋ฆฌ ์๊ตฌ ์ฌํญ์ ํฌ๊ฒ ์ค์ฌ์ค๋๋ค. ๋จผ์ bitsandbytes๋ฅผ ์ค์นํ๊ณ (`pip install bitsandbytes`), ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ง์ํ๋ GPU/๊ฐ์๊ธฐ๋ฅผ ์ฌ์ฉ ์ค์ธ์ง ํ์ธํ์ญ์์ค. | |
| <Tip> | |
| bitsandbytes๋ CUDA ์ด์ธ์ ์ฌ๋ฌ ๋ฐฑ์๋๋ฅผ ์ง์ํ๋๋ก ๋ฆฌํฉํฐ๋ง๋๊ณ ์์ต๋๋ค. ํ์ฌ ROCm(AMD GPU) ๋ฐ Intel CPU ๊ตฌํ์ด ์ฑ์ ๋จ๊ณ์ด๋ฉฐ, Intel XPU๋ ์งํ ์ค์ด๊ณ Apple Silicon ์ง์์ Q4/Q1์ ์์๋ฉ๋๋ค. ์ค์น ์ง์นจ ๋ฐ ์ต์ ๋ฐฑ์๋ ์ ๋ฐ์ดํธ๋ [์ด ๋งํฌ](https://huggingface.co/docs/bitsandbytes/main/en/installation#multi-backend)๋ฅผ ๋ฐฉ๋ฌธํ์ธ์. | |
| ์ ์ฒด ๊ณต๊ฐ ์ ์ ๋ฒ๊ทธ๋ฅผ ์๋ณํ๋ ๋ฐ ๋์์ด ๋๋ ํผ๋๋ฐฑ์ ํ์ํฉ๋๋ค! ์์ธํ ๋ด์ฉ๊ณผ ํผ๋๋ฐฑ์ [์ด ๋ฌธ์](https://huggingface.co/docs/bitsandbytes/main/en/non_cuda_backends)๋ฅผ ํ์ธํ์ธ์. | |
| </Tip> | |
| ์์ ์ฝ๋ ์ค๋ํซ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๋ฉด ๋ฉ๋๋ค: | |
| ```python | |
| from transformers import ChameleonForConditionalGeneration, BitsAndBytesConfig | |
| # ๋ชจ๋ธ ์์ํ ๋ฐฉ์ ์ง์ | |
| quantization_config = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_quant_type="nf4", | |
| bnb_4bit_compute_dtype=torch.bfloat16, | |
| ) | |
| model = ChameleonForConditionalGeneration.from_pretrained("facebook/chameleon-7b", quantization_config=quantization_config, device_map="cuda") | |
| ``` | |
| ### Flash-Attention 2์ SDPA๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ ์๋ ํฅ์ [[use-flash-attention-2-and-sdpa-to-further-speed-up-generation]] | |
| ์ด ๋ชจ๋ธ์ ์ต์ ํ๋ฅผ ์ํด Flash-Attention 2์ PyTorch์ [`torch.nn.functional.scaled_dot_product_attention`](https://pytorch.org/docs/master/generated/torch.nn.functional.scaled_dot_product_attention.html)๋ฅผ ๋ชจ๋ ์ง์ํฉ๋๋ค. SDPA๋ ๋ชจ๋ธ์ ๋ก๋ํ ๋ ๊ธฐ๋ณธ ์ต์ ์ ๋๋ค. Flash Attention 2๋ก ์ ํํ๋ ค๋ฉด ๋จผ์ flash-attn์ ์ค์นํด์ผ ํฉ๋๋ค. ํด๋น ํจํค์ง ์ค์น์ ๋ํด์๋ [์๋ณธ ๋ฆฌํฌ์งํ ๋ฆฌ](https://github.com/Dao-AILab/flash-attention)๋ฅผ ์ฐธ๊ณ ํ์ญ์์ค. ์์ ์ฝ๋ ์ค๋ํซ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๋ฉด ๋ฉ๋๋ค: | |
| ```python | |
| from transformers import ChameleonForConditionalGeneration | |
| model_id = "facebook/chameleon-7b" | |
| model = ChameleonForConditionalGeneration.from_pretrained( | |
| model_id, | |
| dtype=torch.bfloat16, | |
| attn_implementation="flash_attention_2" | |
| ).to(0) | |
| ``` | |
| ## ChameleonConfig [[transformers.ChameleonConfig]] | |
| [[autodoc]] ChameleonConfig | |
| ## ChameleonVQVAEConfig [[transformers.ChameleonVQVAEConfig]] | |
| [[autodoc]] ChameleonVQVAEConfig | |
| ## ChameleonProcessor [[transformers.ChameleonProcessor]] | |
| [[autodoc]] ChameleonProcessor | |
| ## ChameleonImageProcessor [[transformers.ChameleonImageProcessor]] | |
| [[autodoc]] ChameleonImageProcessor | |
| - preprocess | |
| ## ChameleonVQVAE [[transformers.ChameleonVQVAE]] | |
| [[autodoc]] ChameleonVQVAE | |
| - forward | |
| ## ChameleonModel [[transformers.ChameleonModel]] | |
| [[autodoc]] ChameleonModel | |
| - forward | |
| ## ChameleonForConditionalGeneration [[transformers.ChameleonForConditionalGeneration]] | |
| [[autodoc]] ChameleonForConditionalGeneration | |
| - forward | |