| <!--Copyright 2025 Advanced Micro Devices, Inc. and 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. | |
| --> | |
| # Quark[[quark]] | |
| [Quark](https://quark.docs.amd.com/latest/)๋ ํน์ ๋ฐ์ดํฐ ํ์ , ์๊ณ ๋ฆฌ์ฆ, ํ๋์จ์ด์ ๊ตฌ์ ๋ฐ์ง ์๋๋ก ์ค๊ณ๋ ๋ฅ๋ฌ๋ ์์ํ ํดํท์ ๋๋ค. Quark์์๋ ๋ค์ํ ์ ์ฒ๋ฆฌ ์ ๋ต, ์๊ณ ๋ฆฌ์ฆ, ๋ฐ์ดํฐ ํ์ ์ ์กฐํฉํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค. | |
| ๐ค Transformers๋ฅผ ํตํด ํตํฉ๋ PyTorch ์ง์์ ์ฃผ๋ก AMD CPU ๋ฐ GPU๋ฅผ ๋์์ผ๋ก ํ๋ฉฐ, ์ฃผ๋ก ํ๊ฐ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. ์๋ฅผ ๋ค์ด, [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness)๋ฅผ ๐ค Transformers ๋ฐฑ์๋์ ํจ๊ป ์ฌ์ฉํ์ฌ Quark๋ก ์์ํ๋ ๋ค์ํ ๋ชจ๋ธ์ ์ํํ๊ฒ ํ๊ฐํ ์ ์์ต๋๋ค. | |
| Quark์ ๊ด์ฌ์ด ์๋ ์ฌ์ฉ์๋ [๋ฌธ์](https://quark.docs.amd.com/latest/)๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋ชจ๋ธ ์์ํ๋ฅผ ์์ํ๊ณ ์ง์๋๋ ์คํ ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ฌ์ฉํ ์ ์์ต๋๋ค! | |
| Quark๋ ์์ฒด ์ฒดํฌํฌ์ธํธ/[์ค์ ํฌ๋งท](https://huggingface.co/amd/Llama-3.1-8B-Instruct-FP8-KV-Quark-test/blob/main/config.json#L26)๋ฅผ ๊ฐ์ง๊ณ ์์ง๋ง, ๋ค๋ฅธ ์์ํ/๋ฐํ์ ๊ตฌํ์ฒด ([AutoAWQ](https://huggingface.co/docs/transformers/quantization/awq), [๋ค์ดํฐ๋ธ fp8](https://huggingface.co/docs/transformers/quantization/finegrained_fp8))์ ํธํ๋๋ ์ง๋ ฌํ ๋ ์ด์์์ผ๋ก ๋ชจ๋ธ์ ์์ฑํ๋ ๊ฒ๋ ์ง์ํฉ๋๋ค. | |
| Transformer์์ Quark ์์ํ ๋ชจ๋ธ์ ๋ก๋ํ๋ ค๋ฉด ๋จผ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํด์ผ ํฉ๋๋ค: | |
| ```bash | |
| pip install amd-quark | |
| ``` | |
| ## ์ง์ ๋งคํธ๋ฆญ์ค[[Support matrix]] | |
| Quark๋ฅผ ํตํด ์์ํ๋ ๋ชจ๋ธ์ ํจ๊ป ์กฐํฉํ ์ ์๋ ๊ด๋ฒ์ํ ๊ธฐ๋ฅ์ ์ง์ํฉ๋๋ค. ๊ตฌ์ฑ์ ๊ด๊ณ์์ด ๋ชจ๋ ์์ํ๋ ๋ชจ๋ธ์ `PretrainedModel.from_pretrained`๋ฅผ ํตํด ์ํํ๊ฒ ๋ค์ ๋ก๋ํ ์ ์์ต๋๋ค. | |
| ์๋ ํ๋ Quark์์ ์ง์ํ๋ ๋ช ๊ฐ์ง ๊ธฐ๋ฅ์ ๋ณด์ฌ์ค๋๋ค: | |
| | **๊ธฐ๋ฅ** | **Quark์์ ์ง์ํ๋ ํญ๋ชฉ** | | | |
| |---------------------------------|-----------------------------------------------------------------------------------------------------------|---| | |
| | ๋ฐ์ดํฐ ํ์ | int8, int4, int2, bfloat16, float16, fp8_e5m2, fp8_e4m3, fp6_e3m2, fp6_e2m3, fp4, OCP MX, MX6, MX9, bfp16 | | | |
| | ์์ํ ์ ๋ชจ๋ธ ๋ณํ | SmoothQuant, QuaRot, SpinQuant, AWQ | | | |
| | ์์ํ ์๊ณ ๋ฆฌ์ฆ | GPTQ | | | |
| | ์ง์ ์ฐ์ฐ์ | ``nn.Linear``, ``nn.Conv2d``, ``nn.ConvTranspose2d``, ``nn.Embedding``, ``nn.EmbeddingBag`` | | | |
| | ์ธ๋ถ์ฑ(Granularity) | per-tensor, per-channel, per-block, per-layer, per-layer type | | | |
| | KV ์บ์ | fp8 | | | |
| | ํ์ฑํ ์บ๋ฆฌ๋ธ๋ ์ด์ | MinMax / Percentile / MSE | | | |
| | ์์ํ ์ ๋ต | weight-only, static, dynamic, with or without output quantization | | | |
| ## Hugging Face Hub์ ๋ชจ๋ธ[[Models on Hugging Face Hub]] | |
| Quark ๋ค์ดํฐ๋ธ ์ง๋ ฌํ๋ฅผ ์ฌ์ฉํ๋ ๊ณต๊ฐ ๋ชจ๋ธ์ https://huggingface.co/models?other=quark ์์ ์ฐพ์ ์ ์์ต๋๋ค. | |
| Quark๋ [`quant_method="fp8"`์ ์ด์ฉํ๋ ๋ชจ๋ธ](https://huggingface.co/models?other=fp8)๊ณผ [`quant_method="awq"`์ ์ฌ์ฉํ๋ ๋ชจ๋ธ](https://huggingface.co/models?other=awq)๋ ์ง์ํ์ง๋ง, Transformers๋ ์ด๋ฌํ ๋ชจ๋ธ์ [AutoAWQ](https://huggingface.co/docs/transformers/quantization/awq)๋ฅผ ํตํด ๋ถ๋ฌ์ค๊ฑฐ๋ | |
| [๐ค Transformers์ ๋ค์ดํฐ๋ธ fp8 ์ง์](https://huggingface.co/docs/transformers/quantization/finegrained_fp8)์ ์ฌ์ฉํฉ๋๋ค. | |
| ## Transformers์์ Quark๋ชจ๋ธ ์ฌ์ฉํ๊ธฐ[[Using Quark models in Transformers]] | |
| ๋ค์์ Transformers์์ Quark ๋ชจ๋ธ์ ๋ถ๋ฌ์ค๋ ๋ฐฉ๋ฒ์ ์์์ ๋๋ค: | |
| ```python | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| model_id = "EmbeddedLLM/Llama-3.1-8B-Instruct-w_fp8_per_channel_sym" | |
| model = AutoModelForCausalLM.from_pretrained(model_id) | |
| model = model.to("cuda") | |
| print(model.model.layers[0].self_attn.q_proj) | |
| # QParamsLinear( | |
| # (weight_quantizer): ScaledRealQuantizer() | |
| # (input_quantizer): ScaledRealQuantizer() | |
| # (output_quantizer): ScaledRealQuantizer() | |
| # ) | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| inp = tokenizer("Where is a good place to cycle around Tokyo?", return_tensors="pt") | |
| inp = inp.to("cuda") | |
| res = model.generate(**inp, min_new_tokens=50, max_new_tokens=100) | |
| print(tokenizer.batch_decode(res)[0]) | |
| # <|begin_of_text|>Where is a good place to cycle around Tokyo? There are several places in Tokyo that are suitable for cycling, depending on your skill level and interests. Here are a few suggestions: | |
| # 1. Yoyogi Park: This park is a popular spot for cycling and has a wide, flat path that's perfect for beginners. You can also visit the Meiji Shrine, a famous Shinto shrine located in the park. | |
| # 2. Imperial Palace East Garden: This beautiful garden has a large, flat path that's perfect for cycling. You can also visit the | |
| ``` | |