Qwen LLM

repository·main·Indexed 12 days ago

https://github.com/qwenlm/qwen

A series of powerful multilingual large language models (LLMs) providing base models for pretraining/finetuning and chat-aligned models for conversational tasks, tool use, and agentic workflows. Available in sizes 1.8B, 7B, 14B, and 72B, with support for Int4 and Int8 quantization. Documentation includes guides for full-parameter finetuning, KV cache quantization, and deployment via vLLM, FastChat, MindFormers on Ascend 910, and fastllm on DCU.

Tokens
61.4K
Snippets
189
Records
255
Agent score
97%

What's inside Qwen

  1. Overview of Qwen-7B models

    main

    Qwen-7B is a series of 7B-parameter large language models. The release provides two primary versions:

    • Qwen-7B: The base pretrained language model, trained on over 2.2 trillion tokens with a context length of 2048.
    • Qwen-7B-Chat: A version fine-tuned to align with human intent using curated task-oriented, security-oriented, and service-oriented data.

    The repository includes example codes for fine-tuning, evaluation, and inference, as well as guides for long-context and tool-use inference.

  2. Overview of Qwen model families

    main

    Qwen provides two main types of models:

    1. Qwen (Base Models): Foundational language models trained on up to 3 trillion tokens across multiple languages (primarily Chinese and English).
    2. Qwen-Chat (Chat Models): Models fine-tuned using SFT (Supervised Fine-Tuning) and RLHF (Reinforcement Learning from Human Feedback) to follow human instructions. They are capable of chat, content creation, information extraction, summarization, translation, coding, and solving mathematical problems.

    Available sizes include 1.8B, 7B, 14B, and 72B parameters. Quantized versions (Int4 and Int8) are also available for reduced memory usage.

  3. Overview of Qwen model variants and specifications

    main

    The Qwen series includes Base models and Chat models across several parameter scales.

    Model Specifications

    ModelMax ContextSystem Prompt SupportTool Calling
    Qwen-1.8B32K
    Qwen-7B32K
    Qwen-14B8K
    Qwen-72B32K

    Available Scales

    • 1.8B: Qwen-1.8B, Qwen-1.8B-Chat
    • 7B: Qwen-7B, Qwen-7B-Chat
    • 14B: Qwen-14B, Qwen-14B-Chat
    • 72B: Qwen-72B, Qwen-72B-Chat

    Quantized versions (Int4 and Int8) are also available for various scales to reduce memory footprint.

  4. Fine-tune Qwen using DeepSpeed

    main

    Qwen provides an official training script finetune.py and several notebooks to facilitate fine-tuning pre-trained models for downstream applications. The supported algorithms include:

    • Full-parameter fine-tuning: Updates all parameters in the model.
    • LoRA fine-tuning: Updates only the parameters of adapter layers while keeping the original LLM layers frozen, reducing memory and computation costs.
    • Q-LoRA fine-tuning: Uses a quantized large language model to further reduce memory costs.

    Training notebooks are available for different combinations of algorithms and hardware configurations (Single GPU vs. Multiple GPUs).

  5. Overview of Qwen model series

    main

    The Qwen series consists of two main types of models available in various sizes (1.8B, 7B, 14B, and 72B):

    1. Qwen (Base Models): Strong base language models pretrained on up to 3 trillion tokens of multilingual data. Available as Qwen-1.8B, Qwen-7B, Qwen-14B, and Qwen-72B.
    2. Qwen-Chat (Chat Models): Models aligned with human preferences via SFT and RLHF, capable of chatting, content creation, summarization, coding, math solving, and tool usage. Available as Qwen-1.8B-Chat, Qwen-7B-Chat, Qwen-14B-Chat, and Qwen-72B-Chat.

    Models are also released in quantized versions (Int4 and Int8) to reduce memory requirements and improve inference speed.

  6. Data format of quantized layer_past

    main

    When using KV cache quantization, the layer_past format changes from a simple tuple of tensors to a tuple containing quantized tensors and their respective quantization parameters (scale and zero point).

    Original format: layer_past=(key, value)

    Quantized format: layer_past=((q_key, key_scale, key_zero_point), (q_value, value_scale, value_zero_point))

  7. Implement tool use and function calling with Qwen-Chat

    main

    Qwen-Chat is optimized for tool use and function calling capabilities. You can develop agents, LangChain applications, or augment Qwen with a Python code interpreter.

    Implementation follows the ReAct Prompting principle. Support for function calling is provided in openai_api.py based on this principle. For detailed implementation guidance, refer to the ReAct example.

  8. Implement tool calling and function calling with Qwen

    main

    Qwen-Chat is optimized for tool use and function calling, making it suitable for building Agents, LangChain applications, or Code Interpreters.

    Implementation details:

    • ReAct Prompting: You can implement tool calling based on the ReAct prompting principle. Detailed guidance is available in the examples/react_prompt.md file.
    • Function Calling Support: The openai_api.py file provides built-in support for Function Calling, following the ReAct principle.
    • Code Interpreter: Qwen demonstrates strong capabilities in generating executable Python code for tasks like mathematics, data visualization, file processing, and web crawling.
    # Refer to openai_api.py for Function Calling implementation
    # Refer to examples/react_prompt.md for ReAct prompting implementation
  9. Note on tiktoken-based tokenizer and fine-tuning

    main

    Qwen uses a tokenizer based on tiktoken, which differs from other tokenizers like SentencePiece. When performing fine-tuning, you must pay close attention to special tokens.

    For detailed information regarding the tokenizer and how to use it during fine-tuning, refer to the tokenization notes.

  10. Configure special tokens and padding

    main

    Special tokens serve functional purposes (e.g., <|endoftext|> for end-of-document).

    • Qwen-7B uses <|endoftext|>.
    • Qwen-7B-Chat uses <|endoftext|>, <|im_start|>, and <|im_end|>.
    • Tokens <|extra_0|> through <|extra_204|> are reserved.

    Important Constraints:

    • Concepts like bos, eos, unk, pad, mask, and sep do not apply to the pre-trained Qwen-7B/Chat models in the traditional sense.
    • Warning: Do not use <|endoftext|> as your eos_token unless you are certain that the end of a document is strictly synonymous with the end of a sequence.
    • If you need to specify a pad_token for fine-tuning or other frameworks, it is recommended to use an existing special token like <|endoftext|>. Mapping special token strings to IDs can be done via tokenizer.special_tokens.
    from transformers import AutoTokenizer
    
    tokenizer = AutoTokenizer.from_pretrained('Qwen/Qwen-7B', trust_remote_code=True, pad_token='<|endoftext|>')