当前位置: 首页 > 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.dtcms.com/a/323018.html

相关文章:

  • 【C++】继承机制全解析
  • Spring-rabbit使用实战七
  • 48伏电气系统—— 铺就电动出行之路的关键技术
  • 大语言模型中的幻觉
  • 24SpringCloud黑马商城微服务整合Seata重启服务报错的解决办法
  • 使用SymPy lambdify处理齐次矩阵的高效向量化计算
  • Poetry与UV——现代Python依赖管理的革新者
  • GitHub 趋势日报 (2025年08月08日)
  • java10学习笔记
  • EPI2ME分析软件测试
  • Java 8 特性
  • PG靶机 - Shiftdel
  • 计算机网络:CIDR地址块划分子网可以使用VLSM吗?
  • 使用 Vuepress + GitHub Pages 搭建项目文档(2)- 使用 GitHub Actions 工作流自动部署
  • [激光原理与应用-206]:光学器件 - SESAM - 基本结构与工作原理
  • “高大上“的SpringCloud?(微服务体系入门)
  • 关于灰度图像相似度的损失函数(笔记)
  • 【Datawhale AI夏令营】基于多模态RAG的企业财报问答系统
  • MySQL弹幕内容字段设计总结
  • Linux Makefile解析
  • 元宇宙技术如何改变社交方式?
  • MyBatis联合查询 - 注解篇
  • QT系统相关
  • gpt-oss 全量技术解读
  • Alibaba Cloud Linux 3 安装 git
  • 【Spring Boot启动流程底层源码详解】
  • kubectl get node k8s-node01 -o yaml | grep taint -B 5 -A 5
  • 如何理解SA_RESTART”被信号中断的系统调用自动重启“?
  • 腾讯COS云存储入门
  • 笔试——Day33