--- pipeline_tag: text-generation license: apache-2.0 base_model: Qwen/Qwen2-7B-Instruct library_name: kerasformers language: - en tags: - keras - kerasformers - qwen2 - text-generation - pytorch - jax - tf --- Paper: [Qwen2 Technical Report (arXiv:2407.10671)](https://arxiv.org/abs/2407.10671) ยท [HF Papers](https://huggingface.co/papers/2407.10671) ## ***See [our collection](https://huggingface.co/collections/kerasformers/qwen2-6a69d274d16370be5d0221c8) for all Qwen2 versions.*** # Run Qwen2 with Keras 3: JAX, PyTorch, or TensorFlow [![GitHub](https://img.shields.io/badge/GitHub-KerasFormers-black?logo=github)](https://github.com/IMvision12/KerasFormers) [![Docs](https://img.shields.io/badge/Docs-Qwen2-blue)](https://imvision12.github.io/KerasFormers/qwen2/) [![Collection](https://img.shields.io/badge/HF-Qwen2%20collection-yellow)](https://huggingface.co/collections/kerasformers/qwen2-6a69d274d16370be5d0221c8) # kerasformers/qwen2-7b-instruct Qwen2 is Alibaba's decoder-only transformer family: grouped-query attention with q/k/v bias, SwiGLU MLPs, RMSNorm, and rotary positions, in dense 0.5B-72B sizes (plus the Qwen2-57B-A14B mixture-of-experts), as base and instruct variants. For more details on the model, please see the upstream [model card](https://huggingface.co/Qwen/Qwen2-7B-Instruct). Pure-**Keras 3** conversion of [`Qwen/Qwen2-7B-Instruct`](https://huggingface.co/Qwen/Qwen2-7B-Instruct) for [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on **TensorFlow / Torch / JAX**. This is an **instruct** (chat-tuned) checkpoint; load `Qwen2Tokenizer` so the chat template is applied. ## Quick start ```python import os os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow" from kerasformers.models.qwen2 import Qwen2TextGenerate, Qwen2Tokenizer model = Qwen2TextGenerate.from_weights("kerasformers/qwen2-7b-instruct") tokenizer = Qwen2Tokenizer.from_weights("kerasformers/qwen2-7b-instruct") inputs = tokenizer([ {"role": "user", "content": "Explain rotary embeddings in one sentence."} ]) outputs = model.generate(**inputs, max_new_tokens=64) print(tokenizer.decode(outputs[0])) ``` Load any Qwen2 variant the same way with `from_weights("kerasformers/")`: | Variant | Hub | Type | |---|---|---| | `qwen2-0.5b` | [`kerasformers/qwen2-0.5b`](https://huggingface.co/kerasformers/qwen2-0.5b) | base | | `qwen2-0.5b-instruct` | [`kerasformers/qwen2-0.5b-instruct`](https://huggingface.co/kerasformers/qwen2-0.5b-instruct) | instruct | | `qwen2-1.5b` | [`kerasformers/qwen2-1.5b`](https://huggingface.co/kerasformers/qwen2-1.5b) | base | | `qwen2-1.5b-instruct` | [`kerasformers/qwen2-1.5b-instruct`](https://huggingface.co/kerasformers/qwen2-1.5b-instruct) | instruct | | `qwen2-7b` | [`kerasformers/qwen2-7b`](https://huggingface.co/kerasformers/qwen2-7b) | base | | `qwen2-7b-instruct` | [`kerasformers/qwen2-7b-instruct`](https://huggingface.co/kerasformers/qwen2-7b-instruct) | instruct | | `qwen2-72b` | [`kerasformers/qwen2-72b`](https://huggingface.co/kerasformers/qwen2-72b) | base | | `qwen2-72b-instruct` | [`kerasformers/qwen2-72b-instruct`](https://huggingface.co/kerasformers/qwen2-72b-instruct) | instruct | | `qwen2-57b-a14b` | [`kerasformers/qwen2-57b-a14b`](https://huggingface.co/kerasformers/qwen2-57b-a14b) | MoE base | | `qwen2-57b-a14b-instruct` | [`kerasformers/qwen2-57b-a14b-instruct`](https://huggingface.co/kerasformers/qwen2-57b-a14b-instruct) | MoE instruct | | `qwen1.5-moe-a2.7b` | [`kerasformers/qwen1.5-moe-a2.7b`](https://huggingface.co/kerasformers/qwen1.5-moe-a2.7b) | MoE base | | `qwen1.5-moe-a2.7b-chat` | [`kerasformers/qwen1.5-moe-a2.7b-chat`](https://huggingface.co/kerasformers/qwen1.5-moe-a2.7b-chat) | MoE chat | ## Tips - Set `KERAS_BACKEND` **before** importing Keras / kerasformers. - Prefer `Qwen2Tokenizer.from_weights(...)` so the chat template matches. - Larger checkpoints: try `load_dtype="bfloat16"` or `quantization="int8"`. - See [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/) and the [Qwen2 docs](https://imvision12.github.io/KerasFormers/qwen2/). - Community / upstream safetensors still work via the `hf:` prefix, e.g. `Qwen2TextGenerate.from_weights("hf:Qwen/Qwen2-7B-Instruct")`. ## Special Thanks A huge thank you to the Qwen team at Alibaba for creating and releasing these models. License: Apache 2.0.