---tasks: - Large Language Modelstraining_framework: PaddlePaddlehardware_support: - KUNLUNXIN - NVIDIA---# Olmo-3-7B-Instruct-paddle版本权重## 1. 权重来源- **HuggingFace 模型 ID**: [allenai/Olmo-3-7B-Instruct](https://huggingface.co/allenai/Olmo-3-7B-Instruct)## 2. 模型描述Olmo-3-7B-Instruct 是由 Allen AI 开发的 7B 参数指令微调语言模型,已迁移至 PaddleFormers 框架。其核心代码主要基于Olmo2,Olmo2的代码也都迁移到PaddleFormers框架。## 3. 模型转换使用 `convert_weights_flex.py` 进行模型转换。PaddleFormers 通过 `_gen_aoa_config` 自动处理 HuggingFace 权重映射,无需手动转置权重。## 4. 推理测试推理测试的方式:要求模型用一个单词输出“中国的首都是哪里?” 必须包含beijing```pythonimport osimport sysimport paddlefrom paddleformers.transformers import Olmo3ForCausalLMdef test_flex_weights_inference(): """使用转换后的 flex_checkpoint 格式权重进行推理测试""" print("=" * 80) print("使用转换后的 flex_checkpoint 格式权重进行推理测试") print("=" * 80) try: from transformers import AutoTokenizer as HFAutoTokenizer except ImportError: print("[ERROR] 需要安装 transformers 库") return False # 自行设定转换后的权重目录 pdparams_dir = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "convert-weight", "olmo3-7b-instruct-pd" ) print(f"\n[INFO] 权重目录: {pdparams_dir}") # 检查权重文件 if not os.path.isdir(pdparams_dir): print(f"[ERROR] 权重目录不存在: {pdparams_dir}") return False files = sorted(os.listdir(pdparams_dir)) safetensors_files = [f for f in files if f.endswith('.safetensors')] print(f"[INFO] safetensors 文件数量: {len(safetensors_files)}") if not safetensors_files: print(f"[ERROR] 没有找到 safetensors 文件") return False # 加载模型 print(f"\n[INFO] 正在加载模型...") try: model = Olmo3ForCausalLM.from_pretrained( pdparams_dir, dtype="bfloat16", load_checkpoint_format="flex_checkpoint", # 使用 flex_checkpoint 格式加载 load_via_cpu=True, # 通过 CPU 加载权重避免 GPU OOM ) model.eval() print(f"[SUCCESS] 模型加载成功!") except Exception as e: print(f"[ERROR] 模型加载失败: {e}") import traceback traceback.print_exc() return False # 加载 tokenizer print(f"\n[INFO] 正在加载 tokenizer...") try: tokenizer = HFAutoTokenizer.from_pretrained( "allenai/OLMo-3-7B-Instruct", trust_remote_code=True, ) print(f"[SUCCESS] Tokenizer 加载成功!") except Exception as e: print(f"[ERROR] Tokenizer 加载失败: {e}") return False # 准备输入 messages = [{"role": "user", "content": "What is the capital of China? Answer in one word."}] input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) print(f"\n[INFO] 输入文本:") print(f"{input_text}") inputs = tokenizer(input_text, return_tensors="np") input_ids = paddle.to_tensor(inputs["input_ids"]) print(f"[INFO] Input shape: {input_ids.shape}") # 执行推理 print(f"\n[INFO] 正在执行推理...") try: with paddle.no_grad(): output = model.generate( input_ids, max_new_tokens=32, do_sample=False, temperature=1.0, top_p=1.0, ) # model.generate 返回 (output_ids, None) if isinstance(output, tuple): output_ids = output[0] else: output_ids = output output_ids_list = output_ids[0].cpu().tolist() generated_text = tokenizer.decode(output_ids_list, skip_special_tokens=True) generated_text_lower = generated_text.lower() print(f"\n[SUCCESS] 推理成功!") print(f"[INFO] 生成的文本:") print(f"{generated_text}") # 检查是否包含 "beijing" if "beijing" in generated_text_lower: print(f"\n[SUCCESS] 生成的文本包含 'beijing'!") return True else: print(f"\n[ERROR] 生成的文本不包含 'beijing'") print(f"[INFO] 生成的文本: {generated_text}") return False except Exception as e: print(f"[ERROR] 推理失败: {e}") import traceback traceback.print_exc() return Falseif __name__ == "__main__": success = test_flex_weights_inference() print(f"\n{'=' * 80}") if success: print(f"[SUCCESS] flex_checkpoint 格式权重推理测试通过!") else: print(f"[ERROR] flex_checkpoint 格式权重推理测试失败!") print(f"{'=' * 80}") sys.exit(0 if success else 1)```