# 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](./awq)、[🤗 Transformersのネイティブfp8](./finegrained_fp8)）に準拠したシリアライズレイアウトでモデルを生成することもサポートしています。

TransformersでQuark量子化モデルを読み込むには、まずライブラリをインストールする必要があります:

```bash
pip install amd-quark
```

## サポートマトリックス

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``               |   |
| 粒度                     | テンソルごと、チャネルごと、ブロックごと、レイヤーごと、レイヤータイプごと                                             |   |
| KVキャッシュ                        | fp8                                                                                                       |   |
| アクティベーションのキャリブレーション          | MinMax / パーセンタイル / MSE                                                                                 |   |
| 量子化戦略           | weight-only、static、dynamic、出力量子化あり/なし                                         |   |

## 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](./awq)経由で読み込むか、[🤗 Transformersのネイティブfp8サポート](./finegrained_fp8)を使用します。

## TransformersでQuarkモデルを使用する

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, device_map="auto")

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(model.device)

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
```

