Skip to main content

Overview

Qwen supports both chat models (e.g., Qwen-7B-Chat) and base language models (e.g., Qwen-7B) for inference. This guide covers the basic usage patterns for running Qwen models.

Requirements

Before you begin, ensure your environment meets these requirements:
  • Python 3.8 or above
  • PyTorch 1.12 or above (2.0+ recommended)
  • Transformers 4.32 or above
  • CUDA 11.4 or above (for GPU users)
Make sure you have the correct versions installed to avoid compatibility issues.

Installation

Install the required dependencies:

Optional: Flash Attention

For higher efficiency and lower memory usage, install flash-attention if your device supports fp16 or bf16:
Flash attention is optional. Qwen can run normally without it, but with flash attention you’ll see improved performance.

Chat Model Inference

Qwen-Chat models are fine-tuned for conversational interactions. Here’s how to use them:

Base Model Inference

The base language models (non-chat) are suitable for completion tasks:

Available Models

Qwen-1.8B

  • Context: 32K tokens
  • Parameters: 1.8 billion
  • Min GPU Memory (Int4): 2.9GB

Qwen-7B

  • Context: 32K tokens
  • Parameters: 7 billion
  • Min GPU Memory (Int4): 8.2GB

Qwen-14B

  • Context: 8K tokens
  • Parameters: 14 billion
  • Min GPU Memory (Int4): 13.0GB

Qwen-72B

  • Context: 32K tokens
  • Parameters: 72 billion
  • Min GPU Memory (Int4): 48.9GB

Managing Conversation History

The history parameter maintains conversation context:

Generation Configuration

Customize generation behavior with GenerationConfig:

Next Steps

Transformers Library

Learn more about using Qwen with Transformers

Batch Inference

Process multiple inputs efficiently

Streaming Responses

Get real-time token generation

Multi-GPU Setup

Scale inference across multiple GPUs