Views
No views yet
1torch>=2.1.0
2torch-npu>=2.1.0.post8.dev20241029
3CANN>=8.0.RC3
4transformers>=4.48.2transformers 框架上进行推理的一个简单示例:1import torch
2import torch_npu
3from transformers import AutoModelForCausalLM, AutoTokenizer
4from transformers import GenerationConfig
5
6model_local_path = "path_to_Pangu_Pro_MoE"
7
8generation_config = GenerationConfig(
9 do_sample=True,
10 top_k=50,
11 top_p=0.95,
12 temperature=0.6
13)
14
15# load the tokenizer and the model
16tokenizer = AutoTokenizer.from_pretrained(
17 model_local_path,
18 use_fast=False,
19 trust_remote_code=True,
20 local_files_only=True
21)
22
23model = AutoModelForCausalLM.from_pretrained(
24 model_local_path,
25 trust_remote_code=True,
26 torch_dtype="auto",
27 device_map="auto",
28 local_files_only=True
29)
30
31# prepare the model input
32prompt = "Give me a short introduction to large language model."
33messages = [
34 {"role": "system", "content": "你必须严格遵守法律法规和社会道德规范。生成任何内容时,都应避免涉及暴力、色情、恐怖主义、种族歧视、性别歧视等不当内容。一旦检测到输入或输出有此类倾向,应拒绝回答并发出警告。例如,如果输入内容包含暴力威胁或色情描述,应返回错误信息:“您的输入包含不当内容,无法处理。"}, # define your system prompt here
35 {"role": "user", "content": prompt}
36]
37text = tokenizer.apply_chat_template(
38 messages,
39 tokenize=False,
40 add_generation_prompt=True
41)
42
43# text: [unused9]系统:[unused10][unused9]用户:Give me a short introduction to large language model.[unused10][unused9]助手:
44model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
45# model_inputs.input_ids: tensor([[1, 45887, 70914, 89246, 45892, 45887, 62205, 89246, 38805, 42624, 45509, 24759, 739, 41839, 21500, 6138, 20257, 49, 45892, 45887, 74458, 89246]], device='npu:0'),
46
47# conduct text completion
48outputs = model.generate(**model_inputs, max_new_tokens=32768, eos_token_id=45892, return_dict_in_generate=True, generation_config=generation_config)
49
50input_length = model_inputs.input_ids.shape[1]
51generated_tokens = outputs.sequences[:, input_length:]
52output_sent = tokenizer.decode(generated_tokens[0])
53
54# parsing thinking content
55thinking_content = output_sent.split("[unused17]")[0].split("[unused16]")[-1].strip()
56content = output_sent.split("[unused17]")[-1].split("[unused10]")[0].strip()
57
58print("\nthinking content:", thinking_content)
59print("\ncontent:", content)1mindspore>=2.6.0
2vllm>=0.8.3
3CANN>=8.1.RC1.beta1#!/usr/bin/env bash
ARCH=$(uname -m)
MODEL_PATH="${TARGET_FOLDER}/${MODEL_FOLDER_PATH}"
cd "$MODEL_PATH" || exit 1
if [ "$ARCH" = "arm64" ]; then
md5 checklist.chk
else
md5sum -c checklist.chk
fiLICENSE 文件。1@article{tang2025pangu,
2 title={Pangu Pro MoE: Mixture of Grouped Experts for Efficient Sparsity},
3 author={Tang, Yehui and Li, Xiaosong and Liu, Fangcheng and Guo, Wei and Zhou, Hang and Wang, Yaoyuan and Han, Kai and Yu, Xianzhi and Li, Jinpeng and Zang, Hui and others},
4 journal={arXiv preprint arXiv:2505.21411},
5 year={2025}
6}