当前位置: 首页 > news >正文

pytorch llm 计算flops和参数量

最近需要计算LLM的flops和参数量,我这里分享一下我的代码,供大家参考学习:
首先安装thop

pip install thop

然后加载模型,执行下面的命令:

import torch
from thop import profile
from transformers import AutoTokenizer, AutoModelForCausalLMdevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')model_dir = "<your model path>"
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True, device_map="auto")model = AutoModelForCausalLM.from_pretrained(model_dir, trust_remote_code=True, device_map='auto').to(device)message = [{"role": "system","content": "You are a helpful assistant."},{"role": "user","content": "Below is an instruction that describes a task, paired with an input that provides further context.\nWrite a response that appropriately completes the request.\n\n\n### Instruction:\nGenerate a sequence of motion tokens matching the following human motion description.Your output should be valid JSON object:\n{\n\"motion_sequences\": <list of motion sequence>\n}\n\n### Input:\n\nperson has arms extended to side of body shoulder height then moves both hands into centre and holds together\n\n### Response:"}]
input_text = tokenizer.apply_chat_template(message tokenize=False, add_generation_prompt=True)
print(input_text)
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
print(input_ids)
# 使用 thop.profile 计算 FLOPs 和参数量
flops, params = profile(model, inputs=(input_ids,))print(f"FLOPs: {flops}")
print(f"Parameters: {params}")

这就是我带chat_template的测试啦。

http://www.lryc.cn/news/615673.html

相关文章:

  • Busybox编译、制作initramfs,并在QEMU中运行
  • 束搜索(Beam Search):原理、演进与挑战
  • Java -- 日期类-第一代-第二代-第三代日期
  • NLP:Transformer输出部分
  • 第十九天-输入捕获实验
  • AI编程工具 | Trae介绍
  • Linux高级编程-文件操作
  • SpringBoot 集成 MapStruct
  • Vue 3.6 Vapor模式完全指南:告别虚拟DOM,性能飞跃式提升
  • 使用GTX ip core + SDI IP core实现SDI设计
  • Vue3 路由
  • C++算法练习:单词识别
  • 决策树技术详解:从理论到Python实战
  • 【ref、toRef、toRefs、reactive】
  • 多级缓存详解
  • 《励曼旋耕》Liman Rotary Tillage
  • Python如何合并两个Excel文件
  • 花生4CL基因家族鉴定及对干旱与盐胁迫响应分析--文献精读157
  • 本地进行语音文字互转
  • CVPR中深度学习新范式:通用性、鲁棒性与多模态的创新突破
  • 分布式事务Seata TCC模式篇
  • Linux网络转发系统框架分析
  • 【密码学】7. 数字签名
  • orcad的操作(1)
  • 【LLM】Openai之gpt-oss模型和GPT5模型
  • 【unitrix数间混合计算】2.9 小数部分特征(t_non_zero_bin_frac.rs)
  • DAY35打卡
  • 【js】判断异步函数的返回值要加await
  • 【机器学习深度学习】模型选型:如何根据现有设备选择合适的训练模型
  • Redis面试题及详细答案100道(01-15) --- 基础认知篇