第2节:Qwen模型家族基础 Qwen模型家族代表了阿里云在大语言模型和多模态AI领域的全面探索,展示了开源模型在实现卓越性能的同时,能够适应多种部署场景的可能性。理解Qwen家族如何通过灵活的部署选项实现强大的AI能力,同时在各种任务中保持竞争力,是非常重要的。 开发者资源 Hugging Face模型库 部分Qwen家族模型可通过 Hugging Face 获取,提供这些模型的部分变体。您可以探索可用的变体,根据具体需求进行微调,并通过各种框架进行部署。 本地开发工具 对于本地开发和测试,您可以使用 Microsoft Foundry Local 在开发机器上运行Qwen模型,并获得优化的性能。
Qwen模型家族代表了阿里云在大语言模型和多模态AI领域的全面探索,展示了开源模型在实现卓越性能的同时,能够适应多种部署场景的可能性。理解Qwen家族如何通过灵活的部署选项实现强大的AI能力,同时在各种任务中保持竞争力,是非常重要的。
部分Qwen家族模型可通过 Hugging Face 获取,提供这些模型的部分变体。您可以探索可用的变体,根据具体需求进行微调,并通过各种框架进行部署。
对于本地开发和测试,您可以使用 Microsoft Foundry Local 在开发机器上运行Qwen模型,并获得优化的性能。
在本教程中,我们将探讨阿里巴巴的Qwen模型家族及其基本概念。我们将涵盖Qwen家族的演变、使Qwen模型高效的创新训练方法、家族中的关键变体,以及在不同场景中的实际应用。
通过本教程,您将能够:
AI领域已经发生了显著变化,不同组织在语言模型开发方面采取了不同的策略。一些组织专注于专有的闭源模型,而另一些则强调开源的可访问性和透明性。传统方法通常涉及只能通过API访问的大型专有模型,或能力可能稍显落后的开源模型。
这种范式为希望在保持数据控制、成本管理和部署灵活性的同时获得强大AI能力的组织带来了挑战。传统方法通常需要在尖端性能和实际部署考虑之间做出选择。
在各种场景中,对高质量、可访问AI的需求变得越来越重要。考虑以下应用场景:需要灵活部署选项以满足不同组织需求、成本效益的实现(API成本可能会显著增加)、全球应用中的多语言能力,或在编码和数学等领域的专业知识。
现代AI部署面临一些基本需求,这些需求限制了其实际适用性:
Qwen模型家族代表了一种全面的AI模型开发方法,优先考虑开源可访问性、多语言能力和实际部署,同时保持竞争性能特性。Qwen模型通过多种模型规模、高质量的训练方法,以及针对不同领域的专属变体实现了这一目标。
Qwen家族涵盖了多种方法,旨在提供性能与效率之间的选择,从移动设备到企业服务器的部署,同时提供有意义的AI能力。目标是让高质量AI的访问民主化,同时提供部署选择的灵活性。
Qwen模型基于几个基础原则,这些原则使其区别于其他语言模型家族:
Qwen家族的一个显著特点是模型开发中投入的大规模训练数据和计算资源。Qwen模型利用精心策划的多语言数据集,覆盖数万亿个token,旨在提供全面的世界知识和推理能力。
这种方法通过结合高质量的网络内容、学术文献、代码库和多语言资源来实现。训练方法强调知识的广度和对各种领域和语言的深度理解。
最新的Qwen模型融入了复杂的推理能力,使得复杂的多步骤问题解决成为可能:
思考模式(Qwen3):模型可以在给出最终答案之前进行详细的逐步推理,类似于人类解决问题的方式。
双模式操作:能够在简单查询的快速响应模式和复杂问题的深度思考模式之间切换。
思维链整合:自然地整合推理步骤,提高复杂任务的透明度和准确性。
Qwen家族融入了多种架构优化,旨在同时实现性能和效率:
可扩展设计:一致的架构跨越不同模型规模,便于扩展和比较。
多模态整合:在统一架构中无缝整合文本、视觉和音频处理能力。
部署优化:针对不同硬件配置提供多种量化选项和部署格式。
现代部署环境受益于Qwen模型在各种计算需求中的灵活性:
Qwen提供高效的小型模型,适合边缘部署、移动应用和资源受限环境,同时保持令人印象深刻的能力。
中型模型为专业应用提供增强能力,在性能和计算需求之间实现了良好的平衡。
全规模模型为需要最大能力的高要求应用、研究和企业部署提供了最先进的性能。
Qwen模型提供完全的透明性和定制能力,使组织能够理解、修改和调整模型以满足其特定需求,而无需依赖供应商。
多种模型规模使得从移动设备到高端服务器的多样化硬件配置部署成为可能,为组织的AI基础设施选择提供了灵活性。
Qwen模型在多语言理解和生成方面表现出色,支持数十种语言,尤其在英语和中文方面表现突出,非常适合全球应用。
Qwen模型在基准测试中始终表现出竞争力,同时提供开源可访问性,证明开源模型可以与专有模型媲美。
像Qwen-Coder和Qwen-Math这样的领域专属变体在保持一般语言理解能力的同时提供专业知识。
在深入技术细节之前,让我们探索一些Qwen模型可以实现的具体示例:
Qwen-Math擅长逐步解决数学问题。例如,当被要求解决复杂的微积分问题时:
User: Find the derivative of f(x) = x³ + 2x² - 5x + 3 Qwen-Math: I'll solve this step by step using the power rule for derivatives. For f(x) = x³ + 2x² - 5x + 3, I need to find f'(x). Using the power rule: d/dx(xⁿ) = n·xⁿ⁻¹ Breaking down each term: - d/dx(x³) = 3x² - d/dx(2x²) = 2 · 2x¹ = 4x - d/dx(-5x) = -5 - d/dx(3) = 0 (constant term) Therefore: f'(x) = 3x² + 4x - 5
Qwen模型在多种语言中表现出强大的多语言能力:
User: Explain the concept of artificial intelligence in both English and Chinese. Qwen: **English:** Artificial Intelligence (AI) refers to the simulation of human intelligence processes by machines, especially computer systems. These processes include learning, reasoning, problem-solving, perception, and language understanding. AI enables machines to perform tasks that typically require human cognitive abilities. **中文:** 人工智能(AI)是指机器(特别是计算机系统)对人类智能过程的模拟。这些过程包括学习、推理、解决问题、感知和语言理解。人工智能使机器能够执行通常需要人类认知能力的任务。
Qwen-VL可以同时处理文本和图像:
User: [Image of a chart showing sales data] What trends do you see in this sales chart? Qwen-VL: Looking at this sales chart, I can identify several key trends: 1. **Seasonal Pattern**: There's a clear seasonal cycle with peaks typically occurring in Q4, likely due to holiday sales. 2. **Overall Growth**: Despite fluctuations, there's an upward trend over the time period shown, indicating business growth. 3. **Quarterly Variations**: Q1 and Q3 show consistently lower performance compared to Q2 and Q4. 4. **Recent Acceleration**: The most recent data points suggest accelerating growth, with steeper increases than historical averages. 5. **Volatility Changes**: Earlier periods show more stability, while recent periods display increased volatility, possibly indicating market expansion or increased competition.
Qwen-Coder擅长生成和解释多种编程语言的代码:
User: Create a Python function to implement a binary search algorithm with detailed comments. Qwen-Coder: Here's a Python implementation of binary search with comprehensive comments: ```python def binary_search(arr, target): """ Perform binary search on a sorted array to find the target value. Args: arr (list): A sorted list of comparable elements target: The value to search for Returns: int: Index of target if found, -1 if not found Time Complexity: O(log n) Space Complexity: O(1) """ # Initialize left and right pointers left, right = 0, len(arr) - 1 # Continue searching while the search space is valid while left <= right: # Calculate middle index to avoid integer overflow mid = left + (right - left) // 2 # Check if we found the target if arr[mid] == target: return mid # If target is smaller, search left half elif arr[mid] > target: right = mid - 1 # If target is larger, search right half else: left = mid + 1 # Target not found return -1 # Example usage: sorted_list = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] result = binary_search(sorted_list, 7) print(f"Index of 7: {result}") # Output: Index of 7: 3
此实现遵循最佳实践,具有清晰的变量命名、全面的文档和高效的逻辑。
### Edge Deployment Example Qwen models can be deployed on various edge devices with optimized configurations:
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model = AutoModelForCausalLM.from_pretrained( "Qwen/Qwen2.5-1.5B-Instruct", torch_dtype=torch.float16, device_map="auto", load_in_8bit=True # 8-bit quantization for efficiency ) tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct") # Mobile-optimized inference def mobile_inference(prompt): inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True) with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=100, do_sample=True, temperature=0.7, pad_token_id=tokenizer.eos_token_id ) response = tokenizer.decode(outputs[0], skip_special_tokens=True) return response.replace(prompt, "").strip()
早期的Qwen模型确立了全面训练和开源可访问性的基础原则:
Qwen2系列在语言和多模态能力方面取得了显著进展:
Qwen2.5系列在各个维度上带来了显著改进:
最新一代推动了推理和思考能力的边界:
组织使用Qwen模型进行文档分析、客户服务自动化、代码生成辅助和商业智能应用。开源特性使得能够根据具体业务需求进行定制,同时保持数据隐私和控制。
移动应用利用Qwen模型进行实时翻译、智能助手、内容生成和个性化推荐。多种模型规模使得从移动设备到边缘服务器的部署成为可能。
教育平台使用Qwen模型进行个性化辅导、自动内容生成、语言学习辅助和互动教育体验。像Qwen-Math这样的专属模型提供领域专业知识。
国际应用受益于Qwen模型强大的多语言能力,能够在不同语言和文化背景下提供一致的AI体验。
尽管Qwen提供了各种规模的模型,但较大的变体仍需要显著的计算资源以实现最佳性能,这可能限制某些组织的部署选项。
尽管Qwen模型在一般领域表现良好,但高度专业化的应用可能需要领域专属微调或专属模型。
可用模型和变体的广泛范围可能使新用户在生态系统中选择变得具有挑战性。
尽管支持多种语言,但不同语言的性能可能有所不同,英语和中文的能力最强。
Qwen模型家族代表了向民主化、高质量AI发展的持续演变。未来的发展包括效率优化的增强、多模态能力的扩展、推理机制的改进,以及在不同部署场景中的更好整合。
随着技术的不断发展,我们可以期待Qwen模型变得越来越强大,同时保持其开源可访问性,使AI能够在多种场景和用例中部署。
Qwen家族展示了AI开发的未来可以同时拥抱尖端性能和开放可访问性,为组织提供强大的工具,同时保持透明性和控制。
以下是如何使用Hugging Face Transformers库快速开始使用Qwen模型:
from transformers import AutoModelForCausalLM, AutoTokenizer import torch # Load Qwen3-8B model model_name = "Qwen/Qwen3-8B" model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map="auto" ) tokenizer = AutoTokenizer.from_pretrained(model_name) # Prepare conversation with chat template messages = [ {"role": "user", "content": "Give me a short introduction to large language models."} ] # Apply chat template and generate response text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) model_inputs = tokenizer([text], return_tensors="pt").to(model.device) generated_ids = model.generate( **model_inputs, max_new_tokens=512, do_sample=True, temperature=0.7 ) # Extract and display response output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() response = tokenizer.decode(output_ids, skip_special_tokens=True) print(response)
from transformers import AutoModelForCausalLM, AutoTokenizer # Example with Qwen2.5-7B-Instruct model = AutoModelForCausalLM.from_pretrained( "Qwen/Qwen2.5-7B-Instruct", torch_dtype="auto", device_map="auto" ) tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct") # Structured conversation example messages = [ {"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Explain quantum computing in simple terms."} ] text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) # Generate response with optimized settings model_inputs = tokenizer([text], return_tensors="pt") generated_ids = model.generate( model_inputs.input_ids, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.8, repetition_penalty=1.05 ) response = tokenizer.decode(generated_ids[0], skip_special_tokens=True) print(response)
使用Qwen-Coder进行代码生成:
# Using Qwen2.5-Coder for programming tasks model_name = "Qwen/Qwen2.5-Coder-7B-Instruct" model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto") tokenizer = AutoTokenizer.from_pretrained(model_name) prompt = """ Create a Python function that: 1. Takes a list of numbers as input 2. Returns the median value 3. Handles edge cases like empty lists 4. Include proper documentation and type hints """ messages = [{"role": "user", "content": prompt}] # Process with model to generate code solution
数学问题解决:
# Using Qwen2.5-Math for mathematical reasoning model_name = "Qwen/Qwen2.5-Math-7B-Instruct" prompt = """ Solve this step by step: Find the derivative of f(x) = x³ + 2x² - 5x + 3 and then find the critical points. """ messages = [{"role": "user", "content": prompt}] # Generate mathematical solution with step-by-step reasoning
视觉语言任务:
# For image understanding with Qwen-VL from qwen_vl_utils import process_vision_info messages = [ { "role": "user", "content": [ {"type": "image", "image": "https://www.aiknowledge.cn/images/%E5%88%9D%E5%AD%A6%E8%80%85%E7%89%88EdgeAI/image.webp"}, {"type": "text", "text": "Describe what's happening in this image and identify any text present."} ] } ] # Process image and generate comprehensive description
# Using Qwen3 with thinking mode for complex reasoning model_name = "Qwen/Qwen3-8B" # Enable thinking mode for complex problems prompt = """ Analyze the following business scenario and provide a strategic recommendation: A startup has developed an innovative AI-powered educational app. They have limited funding, strong technical capabilities, but no marketing experience. They're deciding between: 1. Focusing on B2B sales to schools 2. Direct-to-consumer marketing 3. Partnering with existing educational publishers Consider market dynamics, resource constraints, and growth potential. """ messages = [{"role": "user", "content": prompt}] # The model will generate <think>...</think> reasoning before final answer text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) model_inputs = tokenizer([text], return_tensors="pt").to(model.device) # Generate with thinking mode generated_ids = model.generate( **model_inputs, max_new_tokens=1024, thinking_budget=512 # Allow extended reasoning ) # Parse thinking content and final response output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() # Extract thinking process and final answer try: index = len(output_ids) - output_ids[::-1].index(151668) # </think> token except ValueError: index = 0 thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True) final_response = tokenizer.decode(output_ids[index:], skip_special_tokens=True) print("Thinking Process:", thinking_content) print("Final Recommendation:", final_response)
# Optimized deployment for resource-constrained environments from transformers import AutoModelForCausalLM, AutoTokenizer import torch # Load smallest efficient model with quantization model = AutoModelForCausalLM.from_pretrained( "Qwen/Qwen2.5-1.5B-Instruct", torch_dtype=torch.float16, device_map="auto", load_in_8bit=True, # Reduce memory usage trust_remote_code=True ) tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct") def efficient_inference(prompt, max_length=256): """Optimized inference for mobile/edge deployment""" inputs = tokenizer( prompt, return_tensors="pt", max_length=512, truncation=True ) with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=max_length, do_sample=True, temperature=0.7, pad_token_id=tokenizer.eos_token_id, early_stopping=True ) response = tokenizer.decode(outputs[0], skip_special_tokens=True) return response.replace(prompt, "").strip() # Example mobile-optimized usage quick_response = efficient_inference("What is machine learning?", max_length=100) print(quick_response)
# Deploy Qwen model as API using vLLM from vllm import LLM, SamplingParams # Initialize model for API serving llm = LLM( model="Qwen/Qwen2.5-7B-Instruct", tensor_parallel_size=1, gpu_memory_utilization=0.8 ) # Configure sampling parameters sampling_params = SamplingParams( temperature=0.7, top_p=0.8, max_tokens=512 ) def api_generate(prompts): """API endpoint for text generation""" # Format prompts with chat template formatted_prompts = [] for prompt in prompts: messages = [{"role": "user", "content": prompt}] formatted_prompt = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) formatted_prompts.append(formatted_prompt) # Generate responses outputs = llm.generate(formatted_prompts, sampling_params) # Extract and return responses responses = [] for output in outputs: response = output.outputs[0].text.strip() responses.append(response) return responses # Example API usage user_prompts = [ "Explain the benefits of renewable energy", "Write a Python function to calculate factorial" ] responses = api_generate(user_prompts) for prompt, response in zip(user_prompts, responses): print(f"Prompt: {prompt}") print(f"Response: {response}\n")
Qwen模型家族在各种基准测试中取得了显著性能,同时保持开源可访问性:
推理卓越:
效率成就:
多语言能力:
训练规模:
| 模型系列 | 参数范围 | 上下文长度 | 关键优势 | 最佳应用场景 |
|---|---|---|---|---|
| Qwen2.5 | 0.5B-72B | 32K-128K | 性能均衡,多语言支持 | 通用应用,生产部署 |
| Qwen2.5-Coder | 1.5B-32B | 128K | 代码生成,编程 | 软件开发,编程辅助 |
| Qwen2.5-Math | 1.5B-72B | 4K-128K | 数学推理 | 教育平台,STEM 应用 |
| Qwen2.5-VL | 多种 | 可变 | 视觉语言理解 | 多模态应用,图像分析 |
| Qwen3 | 0.6B-235B | 可变 | 高级推理,思维模式 | 复杂推理,研究应用 |
| Qwen3 MoE | 总计 30B-235B | 可变 | 高效的大规模性能 | 企业应用,高性能需求 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments from peft import LoraConfig, get_peft_model from trl import SFTTrainer from datasets import load_dataset # Load base model for fine-tuning model_name = "Qwen/Qwen2.5-7B-Instruct" model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype=torch.bfloat16, device_map="auto" ) tokenizer = AutoTokenizer.from_pretrained(model_name) tokenizer.pad_token = tokenizer.eos_token # Configure LoRA for efficient fine-tuning peft_config = LoraConfig( r=16, lora_alpha=32, lora_dropout=0.1, bias="none", task_type="CAUSAL_LM", target_modules=["q_proj", "k_proj", "v_proj", "o_proj"] ) # Apply LoRA to model model = get_peft_model(model, peft_config) # Training configuration training_args = TrainingArguments( output_dir="./qwen-finetuned", learning_rate=5e-5, per_device_train_batch_size=4, gradient_accumulation_steps=4, num_train_epochs=3, warmup_steps=100, logging_steps=10, save_steps=500, evaluation_strategy="steps", eval_steps=500, bf16=True, remove_unused_columns=False ) # Load and prepare dataset def format_instruction(example): return f"<|im_start|>user\n{example['instruction']}<|im_end|>\n<|im_start|>assistant\n{example['output']}<|im_end|>" dataset = load_dataset("your-custom-dataset") dataset = dataset.map( lambda x: {"text": format_instruction(x)}, remove_columns=dataset["train"].column_names ) # Initialize trainer trainer = SFTTrainer( model=model, args=training_args, train_dataset=dataset["train"], eval_dataset=dataset["validation"], tokenizer=tokenizer, max_seq_length=2048, packing=True ) # Start fine-tuning trainer.train()
针对复杂推理任务:
def create_reasoning_prompt(problem, context=""): """Create structured prompt for complex reasoning""" prompt = f"""<|im_start|>system You are Qwen, a helpful AI assistant. When solving complex problems, break down your reasoning into clear steps. Instructions: 1. Analyze the problem carefully 2. Identify key components and relationships 3. Work through the solution step by step 4. Verify your answer 5. Provide a clear final answer {context} <|im_end|> <|im_start|>user {problem} Please solve this step by step, showing your reasoning process. <|im_end|> <|im_start|>assistant""" return prompt # Example usage complex_problem = """ A company's revenue grows by 15% each year. If they had $2 million in revenue in 2020, and they want to reach $5 million by 2025, will they achieve this goal? If not, what growth rate would they need? """ reasoning_prompt = create_reasoning_prompt(complex_problem)
针对带上下文的代码生成:
def create_coding_prompt(task, language="Python", context="", constraints=""): """Create structured prompt for code generation""" prompt = f"""<|im_start|>system You are Qwen-Coder, an expert programming assistant. Generate clean, efficient, and well-documented code. Requirements: - Use {language} programming language - Include comprehensive docstrings - Add type hints where appropriate - Follow best practices and conventions - Include example usage {context} <|im_end|> <|im_start|>user Task: {task} {f"Constraints: {constraints}" if constraints else ""} Please provide a complete, production-ready solution. <|im_end|> <|im_start|>assistant""" return prompt # Example usage coding_task = """ Create a class that manages a simple in-memory cache with TTL (time-to-live) support. The cache should support get, set, delete operations and automatically expire entries. """ constraints = """ - Thread-safe operations - Configurable default TTL - Memory-efficient cleanup of expired entries - Support for custom serialization """ coding_prompt = create_coding_prompt(coding_task, "Python", constraints=constraints)
def create_multilingual_prompt(query, target_languages=["en", "zh", "es"]): """Create prompt for multilingual responses""" language_names = { "en": "English", "zh": "Chinese (中文)", "es": "Spanish (Español)", "fr": "French (Français)", "de": "German (Deutsch)", "ja": "Japanese (日本語)" } lang_list = [language_names.get(lang, lang) for lang in target_languages] lang_str = ", ".join(lang_list) prompt = f"""<|im_start|>system You are Qwen, a multilingual AI assistant. Provide responses in multiple languages as requested. Ensure cultural appropriateness and natural expression in each language. <|im_end|> <|im_start|>user Please answer the following question in {lang_str}: {query} Provide clear, culturally appropriate responses in each requested language. <|im_end|> <|im_start|>assistant""" return prompt # Example usage multilingual_query = "What are the benefits of renewable energy for the environment?" multilingual_prompt = create_multilingual_prompt( multilingual_query, target_languages=["en", "zh", "es"] )
import asyncio from typing import List, Dict, Optional from dataclasses import dataclass import torch from transformers import AutoModelForCausalLM, AutoTokenizer @dataclass class GenerationConfig: max_tokens: int = 512 temperature: float = 0.7 top_p: float = 0.9 repetition_penalty: float = 1.05 do_sample: bool = True class QwenService: """Production-ready Qwen model service""" def __init__(self, model_name: str, device: str = "auto"): self.model_name = model_name self.device = device self.model = None self.tokenizer = None self._load_model() def _load_model(self): """Load model and tokenizer""" self.tokenizer = AutoTokenizer.from_pretrained(self.model_name) self.model = AutoModelForCausalLM.from_pretrained( self.model_name, torch_dtype=torch.bfloat16, device_map=self.device, trust_remote_code=True ) # Optimize for inference self.model.eval() if hasattr(self.model, 'generation_config'): self.model.generation_config.pad_token_id = self.tokenizer.eos_token_id def format_chat(self, messages: List[Dict[str, str]]) -> str: """Format messages using chat template""" return self.tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True ) async def generate_async( self, messages: List[Dict[str, str]], config: GenerationConfig = GenerationConfig() ) -> str: """Async generation for high-throughput applications""" formatted_prompt = self.format_chat(messages) # Tokenize input inputs = self.tokenizer( formatted_prompt, return_tensors="pt", truncation=True, max_length=4096 ).to(self.model.device) # Generate response with torch.no_grad(): outputs = await asyncio.get_event_loop().run_in_executor( None, lambda: self.model.generate( **inputs, max_new_tokens=config.max_tokens, temperature=config.temperature, top_p=config.top_p, repetition_penalty=config.repetition_penalty, do_sample=config.do_sample, pad_token_id=self.tokenizer.eos_token_id ) ) # Extract generated text generated_text = self.tokenizer.decode( outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True ) return generated_text.strip() def generate_batch( self, batch_messages: List[List[Dict[str, str]]], config: GenerationConfig = GenerationConfig() ) -> List[str]: """Batch generation for efficiency""" formatted_prompts = [self.format_chat(messages) for messages in batch_messages] # Tokenize batch inputs = self.tokenizer( formatted_prompts, return_tensors="pt", padding=True, truncation=True, max_length=4096 ).to(self.model.device) # Generate responses with torch.no_grad(): outputs = self.model.generate( **inputs, max_new_tokens=config.max_tokens, temperature=config.temperature, top_p=config.top_p, repetition_penalty=config.repetition_penalty, do_sample=config.do_sample, pad_token_id=self.tokenizer.eos_token_id ) # Extract all generated texts responses = [] for i, output in enumerate(outputs): generated_text = self.tokenizer.decode( output[inputs.input_ids[i].shape[0]:], skip_special_tokens=True ) responses.append(generated_text.strip()) return responses # Example usage async def main(): # Initialize service qwen_service = QwenService("Qwen/Qwen2.5-7B-Instruct") # Single generation messages = [ {"role": "user", "content": "Explain machine learning in simple terms"} ] response = await qwen_service.generate_async(messages) print("Single Response:", response) # Batch generation batch_messages = [ [{"role": "user", "content": "What is artificial intelligence?"}], [{"role": "user", "content": "How does deep learning work?"}], [{"role": "user", "content": "What are neural networks?"}] ] batch_responses = qwen_service.generate_batch(batch_messages) for i, response in enumerate(batch_responses): print(f"Batch Response {i+1}:", response) # Run the example # asyncio.run(main())
# Memory-efficient loading strategies from transformers import AutoModelForCausalLM, BitsAndBytesConfig # 8-bit quantization for memory efficiency quantization_config = BitsAndBytesConfig( load_in_8bit=True, llm_int8_threshold=6.0, llm_int8_has_fp16_weight=False ) model = AutoModelForCausalLM.from_pretrained( "Qwen/Qwen2.5-7B-Instruct", quantization_config=quantization_config, device_map="auto", torch_dtype=torch.float16 ) # 4-bit quantization for maximum efficiency quantization_config_4bit = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4" ) efficient_model = AutoModelForCausalLM.from_pretrained( "Qwen/Qwen2.5-7B-Instruct", quantization_config=quantization_config_4bit, device_map="auto" )
import torch from torch.nn.attention import SDPABackend, sdpa_kernel # Optimized inference configuration def optimized_inference_setup(): """Configure optimizations for inference""" # Enable optimized attention mechanisms torch.backends.cuda.enable_flash_sdp(True) torch.backends.cuda.enable_math_sdp(True) torch.backends.cuda.enable_mem_efficient_sdp(True) # Set optimal threading torch.set_num_threads(4) # Adjust based on your CPU # Enable JIT compilation for repeated patterns torch.jit.set_fusion_strategy([('STATIC', 3), ('DYNAMIC', 20)]) def fast_generate(model, tokenizer, prompt, max_tokens=256): """Optimized generation function""" with torch.no_grad(): # Use optimized attention backend with sdpa_kernel(SDPABackend.FLASH_ATTENTION): inputs = tokenizer(prompt, return_tensors="pt").to(model.device) # Generate with optimizations outputs = model.generate( **inputs, max_new_tokens=max_tokens, do_sample=True, temperature=0.7, use_cache=True, # Enable KV caching pad_token_id=tokenizer.eos_token_id, early_stopping=True ) response = tokenizer.decode( outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True ) return response.strip()
import hashlib import time from typing import Optional class SecureQwenService: """Security-focused Qwen service implementation""" def __init__(self, model_name: str): self.model_name = model_name self.model = None self.tokenizer = None self.request_logs = {} self._load_model() def _sanitize_input(self, text: str) -> str: """Sanitize user input to prevent injection attacks""" # Remove or escape potentially harmful patterns dangerous_patterns = [ "<script>", "</script>", "javascript:", "data:", "<iframe>", "</iframe>" ] sanitized = text for pattern in dangerous_patterns: sanitized = sanitized.replace(pattern, "") return sanitized def _rate_limit_check(self, user_id: str, max_requests: int = 100, window: int = 3600) -> bool: """Simple rate limiting implementation""" current_time = time.time() if user_id not in self.request_logs: self.request_logs[user_id] = [] # Clean old requests self.request_logs[user_id] = [ req_time for req_time in self.request_logs[user_id] if current_time - req_time < window ] # Check rate limit if len(self.request_logs[user_id]) >= max_requests: return False # Log current request self.request_logs[user_id].append(current_time) return True def _hash_sensitive_data(self, data: str) -> str: """Hash sensitive data for logging""" return hashlib.sha256(data.encode()).hexdigest()[:16] def secure_generate( self, messages: List[Dict[str, str]], user_id: str, max_tokens: int = 512 ) -> Optional[str]: """Generate with security measures""" # Rate limiting if not self._rate_limit_check(user_id): return "Rate limit exceeded. Please try again later." # Input sanitization sanitized_messages = [] for message in messages: sanitized_content = self._sanitize_input(message.get("content", "")) sanitized_messages.append({ "role": message.get("role", "user"), "content": sanitized_content }) # Content length validation total_content_length = sum(len(msg["content"]) for msg in sanitized_messages) if total_content_length > 8192: # Reasonable limit return "Input too long. Please reduce the content length." # Log request (with hashed sensitive data) content_hash = self._hash_sensitive_data(str(sanitized_messages)) print(f"Processing request from user {user_id[:8]}... Content hash: {content_hash}") # Generate response try: formatted_prompt = self.tokenizer.apply_chat_template( sanitized_messages, tokenize=False, add_generation_prompt=True ) inputs = self.tokenizer(formatted_prompt, return_tensors="pt").to(self.model.device) with torch.no_grad(): outputs = self.model.generate( **inputs, max_new_tokens=min(max_tokens, 1024), # Enforce reasonable limits temperature=0.7, top_p=0.9, repetition_penalty=1.05, do_sample=True, pad_token_id=self.tokenizer.eos_token_id ) response = self.tokenizer.decode( outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True ) return response.strip() except Exception as e: print(f"Generation error for user {user_id[:8]}...: {str(e)}") return "An error occurred while processing your request."
import time import psutil import torch from dataclasses import dataclass from typing import List, Dict, Any @dataclass class PerformanceMetrics: """Performance metrics for monitoring""" response_time: float memory_usage: float gpu_usage: float token_count: int tokens_per_second: float class QwenMonitor: """Monitor Qwen model performance and health""" def __init__(self): self.metrics_history = [] def measure_performance(self, model, tokenizer, prompt: str) -> PerformanceMetrics: """Measure comprehensive performance metrics""" start_time = time.time() start_memory = psutil.Process().memory_info().rss / 1024 / 1024 # MB # GPU metrics (if available) gpu_usage = 0 if torch.cuda.is_available(): torch.cuda.reset_peak_memory_stats() gpu_usage = torch.cuda.memory_allocated() / 1024 / 1024 # MB # Generate response inputs = tokenizer(prompt, return_tensors="pt").to(model.device) with torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=256, temperature=0.7, do_sample=True, pad_token_id=tokenizer.eos_token_id ) # Calculate metrics end_time = time.time() end_memory = psutil.Process().memory_info().rss / 1024 / 1024 response_time = end_time - start_time memory_usage = end_memory - start_memory if torch.cuda.is_available(): gpu_usage = torch.cuda.max_memory_allocated() / 1024 / 1024 token_count = outputs.shape[1] - inputs.input_ids.shape[1] tokens_per_second = token_count / response_time if response_time > 0 else 0 metrics = PerformanceMetrics( response_time=response_time, memory_usage=memory_usage, gpu_usage=gpu_usage, token_count=token_count, tokens_per_second=tokens_per_second ) self.metrics_history.append(metrics) return metrics def get_average_metrics(self, last_n: int = 10) -> Dict[str, float]: """Get average metrics from recent measurements""" if not self.metrics_history: return {} recent_metrics = self.metrics_history[-last_n:] return { "avg_response_time": sum(m.response_time for m in recent_metrics) / len(recent_metrics), "avg_memory_usage": sum(m.memory_usage for m in recent_metrics) / len(recent_metrics), "avg_gpu_usage": sum(m.gpu_usage for m in recent_metrics) / len(recent_metrics), "avg_tokens_per_second": sum(m.tokens_per_second for m in recent_metrics) / len(recent_metrics) } def health_check(self, model, tokenizer) -> Dict[str, Any]: """Perform comprehensive health check""" health_status = { "status": "healthy", "checks": {}, "recommendations": [] } try: # Test basic functionality test_prompt = "Hello, how are you?" metrics = self.measure_performance(model, tokenizer, test_prompt) # Check response time if metrics.response_time > 10.0: # seconds health_status["checks"]["response_time"] = "slow" health_status["recommendations"].append("Consider model optimization or hardware upgrade") else: health_status["checks"]["response_time"] = "good" # Check memory usage if metrics.memory_usage > 1000: # MB health_status["checks"]["memory_usage"] = "high" health_status["recommendations"].append("Monitor memory usage and consider cleanup") else: health_status["checks"]["memory_usage"] = "good" # Check token generation rate if metrics.tokens_per_second < 5: health_status["checks"]["generation_speed"] = "slow" health_status["recommendations"].append("Optimize inference configuration") else: health_status["checks"]["generation_speed"] = "good" # Overall status if any(check in ["slow", "high"] for check in health_status["checks"].values()): health_status["status"] = "degraded" except Exception as e: health_status["status"] = "unhealthy" health_status["error"] = str(e) health_status["recommendations"].append("Check model loading and configuration") return health_status # Example usage monitor = QwenMonitor() # Regular performance monitoring def monitor_model_performance(model, tokenizer, test_prompts: List[str]): """Monitor model performance with various prompts""" for prompt in test_prompts: metrics = monitor.measure_performance(model, tokenizer, prompt) print(f"Prompt: {prompt[:50]}...") print(f"Response time: {metrics.response_time:.2f}s") print(f"Tokens/sec: {metrics.tokens_per_second:.1f}") print(f"Memory usage: {metrics.memory_usage:.1f}MB") print("-" * 50) # Show average metrics avg_metrics = monitor.get_average_metrics() print("Average Performance Metrics:") for metric, value in avg_metrics.items(): print(f"{metric}: {value:.2f}")
Qwen 模型系列代表了一种全面的方式,旨在普及 AI 技术,同时在多样化应用中保持竞争性能。通过其对开源可访问性、多语言能力和灵活部署选项的承诺,Qwen 使组织和开发者能够利用强大的 AI 能力,无论其资源或具体需求如何。
开源卓越:Qwen 展示了开源模型可以在性能上与专有替代品竞争,同时提供透明性、定制性和控制力。
可扩展架构:从 0.5B 到 235B 参数的范围使得模型可以部署在从移动设备到企业集群的所有计算环境中。
专业能力:领域特定的变体(如 Qwen-Coder、Qwen-Math 和 Qwen-VL)在保持通用语言理解的同时提供专业知识。
全球可访问性:对 119+ 种语言的强大多语言支持使 Qwen 适用于国际应用和多样化用户群。
持续创新:从 Qwen 1.0 到 Qwen3 的演变显示了能力、效率和部署选项的持续改进。
随着 Qwen 系列的不断发展,我们可以期待:
无论您是在构建聊天机器人、开发教育工具、创建编程助手,还是从事多语言应用,Qwen 系列都提供了可扩展的解决方案,并具有强大的社区支持和全面的文档。
有关最新更新、模型发布和详细技术文档,请访问 Hugging Face 上的 Qwen 官方库,并探索活跃的社区讨论和示例。
AI 开发的未来在于可访问、透明和强大的工具,这些工具能够推动各个领域和规模的创新。Qwen 系列体现了这一愿景,为组织和开发者提供了构建下一代 AI 驱动应用的基础。
完成本模块后,您将能够:
免责声明:
本文档使用AI翻译服务 Co-op Translator 进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于关键信息,建议使用专业人工翻译。我们对因使用此翻译而产生的任何误解或误读不承担责任。