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

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.xdnf.cn/news/17551.html

相关文章:

  • Gltf 模型 加载到 Cesium 的坐标轴映射浅谈
  • 深入理解C++构造函数与初始化列表
  • Python训练营打卡Day27-类的定义和方法
  • AudioLLM
  • 专题二_滑动窗口_找到字符串中所有字母异位词
  • 第二十天:数论度量
  • 前端Web在Vue中的知识详解
  • 数据溢出ERROR L107:ADDRESS SPACE OVERFLOW
  • 11. 为什么要用static关键字
  • 【C++】string 的特性和使用
  • Python(13) -- 面向对象
  • 【面试场景题】通过LinkedHashMap来实现LRU与LFU
  • Java+Vue打造的采购招投标一体化管理系统,涵盖招标、投标、开标、评标全流程,功能完备,附完整可二次开发的源码
  • 标准IO实现
  • Effective C++ 条款32:确定你的public继承塑模出 is-a 关系
  • AWT 基本组件深入浅出:Button/Label/TextField/Checkbox/Choice/List 全面实战与性能优化
  • 2025-08-09 李沐深度学习14——经典卷积神经网络 (2)
  • MySQL相关概念和易错知识点(4)(分组查询、连接查询、合并查询、子查询)
  • Mysql笔记-系统变量\用户变量管理
  • 【LLM实战|langchain】langchain基础
  • toRef和toRefs
  • 智慧城管复杂人流场景下识别准确率↑32%:陌讯多模态感知引擎实战解析
  • Easysearch 冷热架构实战
  • Linux下管道的实现
  • SpringBoot 集成 MapStruct
  • 《从零实现哈希表:详解设计、冲突解决与优化》
  • [激光原理与应用-197]:光学器件 - 图解双折射晶体的工作原理
  • Aurora接口FPGA设计
  • C# 异步编程(使用异步Lambda表达式)
  • pdf预览Vue-PDF-Embed