Views
No views yet
transformers library.1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# Specify the path to your fine-tuned model (local or Hugging Face Hub ID)
5model_id = "tonyli8623/Hicoder-R1-Distill-Gemma-27B"
6
7# Load tokenizer and model
8tokenizer = AutoTokenizer.from_pretrained(model_id)
9model = AutoModelForCausalLM.from_pretrained(
10 model_id,
11 torch_dtype=torch.bfloat16, # Use bfloat16 for efficiency if supported
12 device_map="auto" # Automatically distribute across available GPUs
13)
14
15# --- Example 1: Simple Code Generation ---
16prompt_simple = "Write a Python function to calculate the factorial of a number."
17# Note: Use the appropriate chat template if the base model requires it (e.g., Gemma-2 instruct)
18# Example using Gemma-2 instruct template structure (adjust if needed):
19messages_simple = [
20 {"role": "user", "content": prompt_simple}
21]
22input_ids_simple = tokenizer.apply_chat_template(messages_simple, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
23
24outputs_simple = model.generate(
25 input_ids_simple,
26 max_new_tokens=150,
27 do_sample=True,
28 temperature=0.7,
29 top_k=50,
30 top_p=0.95
31)
32response_simple = tokenizer.decode(outputs_simple[0][input_ids_simple.shape[1]:], skip_special_tokens=True)
33print("--- Simple Code Generation ---")
34print(response_simple)
35
36# --- Example 2: Code Generation with CoT ---
37prompt_cot = """Think step-by-step to write a Python function that finds all prime numbers up to a given integer 'n' using the Sieve of Eratosthenes algorithm. Then, provide the function.
38
39Let's break this down:
401. Understand the Sieve of Eratosthenes.
412. Outline the steps needed in the function.
423. Write the Python code based on the outline."""
43
44messages_cot = [
45 {"role": "user", "content": prompt_cot}
46]
47input_ids_cot = tokenizer.apply_chat_template(messages_cot, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
48
49outputs_cot = model.generate(
50 input_ids_cot,
51 max_new_tokens=500, # Allow more tokens for CoT + code
52 do_sample=True,
53 temperature=0.6,
54 top_k=50,
55 top_p=0.95
56)
57response_cot = tokenizer.decode(outputs_cot[0][input_ids_cot.shape[1]:], skip_special_tokens=True)
58print("\n--- Code Generation with CoT ---")
59print(response_cot)
601@misc{hicoder_r1_distill_gemma_27b_[year],
2 title={Hicoder-R1-Distill-Gemma-27B: A Chain-of-Thought and Code Generation Focused Model},
3 author={[Your Name/Organization]},
4 year={[Year of Release]},
5 howpublished={\url{[Link to Model Hub or Repository]}}
6}
7
8@misc{gemma2_2024,
9 title={Gemma 3 Technical Report},
10 author={Gemma Team, Google},
11 year={2024},
12 howpublished={\url{https://ai.google.dev/gemma}} % Replace with actual Gemma 2 paper/report link if available
13}transformers 库来使用此模型。1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4# 指定您的微调模型的路径 (本地路径或 Hugging Face Hub ID)
5model_id = "tonyli8623/Hicoder-R1-Distill-Gemma-27B"
6# 加载分词器和模型
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8model = AutoModelForCausalLM.from_pretrained(
9 model_id,
10 torch_dtype=torch.bfloat16, # 如果硬件支持,使用 bfloat16 以提高效率
11 device_map="auto" # 自动将模型分配到可用的 GPU 上
12)
13
14# --- 示例 1: 简单代码生成 ---
15prompt_simple = "编写一个 Python 函数来计算一个数的阶乘。"
16# 注意: 如果基础模型需要,请使用相应的聊天模板 (例如 Gemma-2 instruct)
17# 使用 Gemma-2 instruct 模板结构的示例 (如果需要请调整):
18messages_simple = [
19 {"role": "user", "content": prompt_simple}
20]
21input_ids_simple = tokenizer.apply_chat_template(messages_simple, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
22
23outputs_simple = model.generate(
24 input_ids_simple,
25 max_new_tokens=150,
26 do_sample=True,
27 temperature=0.7,
28 top_k=50,
29 top_p=0.95
30)
31response_simple = tokenizer.decode(outputs_simple[0][input_ids_simple.shape[1]:], skip_special_tokens=True)
32print("--- 简单代码生成 ---")
33print(response_simple)
34
35# --- 示例 2: 带 CoT 的代码生成 ---
36prompt_cot = """请逐步思考如何编写一个 Python 函数,使用埃拉托斯特尼筛法 (Sieve of Eratosthenes) 找出小于等于给定整数 'n' 的所有素数。然后,提供该函数。
37
38让我们分解一下步骤:
391. 理解埃拉托斯特尼筛法的原理。
402. 概述函数中需要的步骤。
413. 基于概述编写 Python 代码。"""
42
43messages_cot = [
44 {"role": "user", "content": prompt_cot}
45]
46input_ids_cot = tokenizer.apply_chat_template(messages_cot, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
47
48outputs_cot = model.generate(
49 input_ids_cot,
50 max_new_tokens=500, # 为 CoT + 代码允许更多 token
51 do_sample=True,
52 temperature=0.6,
53 top_k=50,
54 top_p=0.95
55)
56response_cot = tokenizer.decode(outputs_cot[0][input_ids_cot.shape[1]:], skip_special_tokens=True)
57print("\n--- 带 CoT 的代码生成 ---")
58print(response_cot)
591@misc{hicoder_r1_distill_gemma_27b_[年份],
2 title={Hicoder-R1-Distill-Gemma-27B: 一个专注于思维链和代码生成的模型},
3 author={[您的姓名/组织名称]},
4 year={[发布年份]},
5 howpublished={\url{[模型 Hub 或仓库的链接]}}
6}
7
8@misc{gemma2_2024,
9 title={Gemma 2 Technical Report},
10 author={Gemma Team, Google},
11 year={2024},
12
13}