Skip to main content
Get up and running with Qwen models in just a few minutes. This guide will help you load a model and start generating text or having conversations.

Prerequisites

Before you begin, ensure you have:
  • Python 3.8 or higher
  • PyTorch 1.12 or higher (2.0+ recommended)
  • CUDA 11.4 or higher (for GPU users)
  • At least 8GB GPU memory for Qwen-7B (16GB+ recommended)

Installation

Install the required dependencies:
For detailed installation instructions including flash-attention setup, see the Installation guide.

Quick Start with Qwen-Chat

The fastest way to start using Qwen is with the chat models. Here’s a simple example:
1

Import Libraries

Import the necessary libraries from Transformers:
2

Load Model and Tokenizer

Load the Qwen-7B-Chat model and tokenizer:
The device_map="auto" parameter automatically handles device placement and multi-GPU distribution.
3

Start Chatting

Use the chat interface to have a conversation:
The history parameter maintains conversation context across multiple turns.

Available Models

Qwen provides models in various sizes to suit different needs:

Qwen-1.8B-Chat

Smallest model, fastest inference, lowest memory requirements (2.9GB GPU)

Qwen-7B-Chat

Balanced performance and efficiency (8.2GB GPU)

Qwen-14B-Chat

High performance for complex tasks (13.0GB GPU)

Qwen-72B-Chat

Best performance, highest capabilities (requires 2xA100 or 48.9GB with Int4)

Model Precision Options

Qwen supports multiple precision formats for different hardware and performance needs:
CPU-only inference is significantly slower than GPU inference. For production use, we recommend GPU acceleration or consider using qwen.cpp for optimized CPU inference.

Using Base Models

If you need the base language model (without chat alignment) for completion tasks:

Using ModelScope (Alternative)

For users in regions with better access to ModelScope:

Running the CLI Demo

Qwen includes a command-line interactive chat demo:
The CLI demo supports several commands:
  • :help or :h - Show help message
  • :exit or :q - Exit the demo
  • :clear or :cl - Clear screen
  • :clear-his or :clh - Clear conversation history
  • :history or :his - Show conversation history
  • :seed <N> - Set random seed
  • :conf - Show current generation config
  • :conf <key>=<value> - Change generation config
  • :reset-conf - Reset generation config

CLI Demo Options

Using Quantized Models

For lower memory requirements and faster inference, use quantized models:
Quantized models achieve minimal performance degradation while significantly reducing memory requirements. See the Quantization section for detailed performance comparisons.

Memory Requirements

Here’s a quick reference for GPU memory requirements when generating 2048 tokens:

Troubleshooting

Make sure you’re using transformers>=4.32.0. Update with:
Try these solutions:
  1. Use a smaller model (e.g., Qwen-1.8B or Qwen-7B)
  2. Use quantized models (Int4 or Int8)
  3. Reduce the maximum sequence length
  4. Enable gradient checkpointing for training
To improve inference speed:
  1. Install flash-attention (see Installation)
  2. Use quantized models for faster generation
  3. Consider using vLLM for production deployments
  4. Use GPU instead of CPU
If you have trouble downloading from Hugging Face:

Next Steps

Installation

Complete installation guide with flash-attention and Docker setup

Model Selection

Choose the right model for your use case

Inference Guide

Learn advanced inference techniques

Fine-tuning

Customize Qwen for your specific tasks