推理引擎大模型【免费下载链接】FlexGenRunning large language models on a single GPU for throughput-oriented scenarios.项目地址https://gitcode.com/gh_mirrors/fl/FlexGen点击查看免费下载导读本指南以仓库benchmark/third_party/transformers/templates/adding_a_missing_tokenization_test/目录下的官方模板为绝对核心系统讲解如何通过 Cookiecutter 为一个尚缺分词测试的新模型一键生成test_tokenization_Xxx.py测试骨架。你将掌握模板的交互式变量配置模型命名、慢/快分词器组合、SentencePiece 依赖、生成后测试文件的结构与关键实现点以及如何把生成文件安置到对应模型的测试子目录并接入TokenizerTesterMixin公共测试体系最终用pytest驱动验证。文中全部证据来自仓库内模板、cookiecutter.json配置与tests/test_tokenization_common.py、tests/models/bert/test_tokenization_bert.py等源码。一、模板是什么为“缺失的分词测试”补课在 Hugging Face Transformers 中每个模型BERT、RoBERTa、DeBERTa 等在tests/models/model/下都应有一份test_tokenization_*.py用来回归验证该模型分词器的行为。当社区提交一个新模型或发现某模型缺少分词测试时最规范的做法不是手写重复的样板代码而是使用本目录提供的 Cookiecutter 模板生成标准骨架。仓库中该模板由三个部分组成相对仓库根目录模板说明文档即本指南依据的主文档交代 fork、clone、安装与调用方式cookiecutter.json定义模板的全部交互变量及默认值Jinja 模板源文件用{{cookiecutter.xxx}}占位符生成最终测试文件。从源码结构看这是 transformers 官方templates/目录被完整随仓库收录的开发者工具属于“给库本身做贡献”的流程组件与 FlexGen 的推理基准脚本无直接耦合可以独立使用。二、环境准备fork、clone 与 dev 依赖安装模板说明文档给出的第一步是准备开发环境。注意本仓库为只读镜像以下命令面向你在 GitHub 上的 fork 副本执行不修改当前仓库git clone https://github.com/YOUR-USERNAME/transformers cd transformers pip install -e .[dev]要点说明pip install -e .[dev]以可编辑模式安装 transformers 及其全部开发依赖。cookiecutter 属于 dev 依赖之一因此文档强调“使用 cookiecutter 需要先装齐所有 dev 依赖”若你的环境是 Conda官方文档一般建议先创建独立环境再执行上述安装避免污染系统 Python安装完成后可用cookiecutter --version验证工具可用。三、生成模板一条命令 一轮问答安装就绪后在 transformers 仓库根目录下运行cookiecutter path-to-the folder/adding_a_missing_tokenization_test/即指向本目录的路径在本仓库中为benchmark/third_party/transformers/templates/adding_a_missing_tokenization_test/。运行后Cookiecutter 会在当前工作目录新建一个以{{cookiecutter.modelname}}命名的文件夹例如BrandNewBERT/依次弹出变量问答见下一节变量表模板引擎把 Jinja 占位符替换成你回答的值最终在新建文件夹内生成唯一一个文件test_tokenization_{{cookiecutter.lowercase_modelname}}.py。文档特别提醒模板生成位置是当前工作目录下的新文件夹生成完成后需要手动把它移动到tests/models/对应模型名/子目录。四、交互变量详解一份 cookiecutter.json 的完整拆解模板变量配置 定义了 8 个变量下面逐个给出含义、默认值与影响变量默认值可选值作用与注意事项modelnameBrandNewBERT任意字符串模型显示名按纯文本大小写书写如BERT、RoBERTa、DeBERTa。文档明确要求按模型原文大小写填写它同时决定新建文件夹名uppercase_modelnameBRAND_NEW_BERT任意全大写蛇形名用于在注释、常量等场景引用模型lowercase_modelnamebrand_new_bert任意全小写蛇形名直接决定生成文件名test_tokenization_brand_new_bert.pycamelcase_modelnameBrandNewBert任意驼峰名用于生成测试类名BrandNewBertTokenizationTest及导入BrandNewBertTokenizer/BrandNewBertTokenizerFasthas_slow_classTrueTrue/False模型是否提供 Python 慢分词器。为True时测试类绑定tokenizer_class并开启test_slow_tokenizerhas_fast_classTrueTrue/False模型是否提供基于 Tokenizers 的快分词器。为True时绑定rust_tokenizer_class并开启test_rust_tokenizerslow_tokenizer_use_sentencepieceTrueTrue/False慢分词器是否基于 SentencePiece。为True时测试类追加test_sentencepiece True并自动加上require_sentencepiece装饰器authorsThe HuggingFace Team任意写入生成文件版权头的作者名变量之间的组合关系由 Jinja 模板源文件 的{% if %}逻辑决定导入语句仅慢类时导入XxxTokenizer仅快类时导入XxxTokenizerFast两者都有时同时导入装饰器慢类用 SentencePiece → 加require_sentencepiece含快类 → 加require_tokenizers两者都有且用 SentencePiece → 两个装饰器都加都不用 → 无装饰器。这两个装饰器定义在 transformers/testing_utils.pyrequire_sentencepiece在 SentencePiece 未安装时跳过测试require_tokenizers在 Tokenizers 未安装时跳过测试。也就是说生成文件的“可运行性”由你的环境决定——只有安装对应依赖才能跑这些用例。五、生成的测试文件长什么样以默认值BrandNewBERT、双类、用 SentencePiece生成后文件核心结构如下 Testing suite for the BrandNewBERT tokenizer. import unittest from transformers import BrandNewBertTokenizer, BrandNewBertTokenizerFast from transformers.testing_utils import require_sentencepiece, require_tokenizers from ...test_tokenization_common import TokenizerTesterMixin require_sentencepiece require_tokenizers class BrandNewBertTokenizationTest(TokenizerTesterMixin, unittest.TestCase): tokenizer_class BrandNewBertTokenizer test_slow_tokenizer True rust_tokenizer_class BrandNewBertTokenizerFast test_rust_tokenizer True test_sentencepiece True # TODO: Check in TokenizerTesterMixin if other attributes need to be changed def setUp(self): super().setUp() raise NotImplementedError( Here you have to implement the saving of a toy tokenizer in self.tmpdirname. ) # TODO: add tests with hard-coded target values5.1 类级属性声明被测对象tokenizer_class/rust_tokenizer_class指定被测的慢、快分词器类供 mixin 的get_tokenizer()/get_rust_tokenizer()加载见 公共测试基类test_slow_tokenizer/test_rust_tokenizer开关对应慢/快分词器的公共用例test_sentencepiece开关 SentencePiece 专属用例setUp中的self.tmpdirname由 mixin 的setUp创建tempfile.mkdtemp()见 test_tokenization_common.py测试结束由tearDown清理。5.2 模板留白两个 TODO 是“必答题”模板刻意留下两处raise NotImplementedError/ TODO这是新增测试的核心工作量所在setUp必须被重写你要在self.tmpdirname中保存一个“玩具分词器”toy tokenizer例如写一份 vocab 文件。参考 BERT 的真实实现——test_tokenization_bert.py 在setUp里构造了含[UNK]、[CLS]、[SEP]、[PAD]、[MASK]及若干子词want、##want、##ed、wa、un、runn、##ing等的词表并写入self.vocab_file还实现了get_input_output_texts返回一对文本/期望输出补充硬编码目标值的用例模板的# TODO: add tests with hard-coded target values提示你仿照test_full_tokenizer见 test_tokenization_bert.py写出确定性的 token→id 断言而不是只依赖 mixin 的通用检查。5.3 mixin 带来的“免费”测试集TokenizerTesterMixin是约 4000 行的公共测试基类tests/test_tokenization_common.py子类仅需配置少量属性即可自动继承大量用例主要包括基础行为test_tokenize_special_tokensL338、test_sentencepiece_tokenize_and_convert_tokens_to_stringL365、test_rust_and_python_full_tokenizersL494验证 Python/Rust 分词结果一致性编解码test_save_and_load_tokenizerL610、test_pickle_tokenizerL700、test_internal_consistencyL868、test_conversion_reversibleL1915特殊 tokentest_add_tokens_tokenizerL795、test_add_special_tokensL846、test_special_tokens_maskL1415长度/填充/截断test_maximum_encoding_length_single_inputL993、test_right_and_left_paddingL1535、test_padding_to_max_lengthL1655批处理与模型输入test_batch_encode_plus_tensorsL2244、test_torch_encode_plus_sent_to_modelL2339、test_offsets_mappingL2883对齐与保存test_tokenization_python_rust_equalsL2768、test_save_pretrainedL3390。另有从tests/fixtures/sample_text.txt读取样本文本驱动慢/快一致性检查的逻辑L171-L172。六、把生成文件归位并运行测试生成完成后按文档说明执行归位与验证# 1. 将新生成的文件夹移动到对应模型的测试子目录 # 例如 tests/models/brand_new_bert/test_tokenization_brand_new_bert.py # 2. 运行该测试文件 pytest tests/models/brand_new_bert/test_tokenization_brand_new_bert.py若在仓库内查看现成范例可对照 tests/models/bert/test_tokenization_bert.pyBERT、tests/models/albert/test_tokenization_albert.pyALBERT等十余个模型的分词测试它们都是从同一 mixin 体系派生并各自补齐setUp与硬编码断言的成熟样例。注意在仓库中运行此类测试前需确认已安装sentencepiecerequire_sentencepiece依赖与tokenizersrequire_tokenizers依赖否则相关用例会被unittest.skipUnless跳过而不是失败装饰器语义见 testing_utils.py。七、最佳实践清单命名三件套保持一致modelname原样大小写、camelcase_modelname类名/导入名、lowercase_modelname文件名三者必须对应同一模型避免类名与文件名错位如实申报类与依赖has_slow_class/has_fast_class/slow_tokenizer_use_sentencepiece必须与模型实际实现一致否则生成的 import 或装饰器会让测试误跳过或误失败必改setUp不实现玩具分词器的保存get_tokenizer()/get_rust_tokenizer()会因from_pretrained(self.tmpdirname)找不到文件而失败加载逻辑见 test_tokenization_common.py补硬编码断言mixin 只保证通用契约模型特有的分词边界如 BERT 的##子词切分必须靠自定义测试固定下来提交前跑通全套先单跑生成文件再跑tests/test_tokenization_common.py中与test_slow_tokenizer/test_rust_tokenizer开关相关的公共用例确保与既有模型测试风格一致。八、关联文件索引模板说明README.md变量配置cookiecutter.json生成骨架test_tokenization_{{cookiecutter.lowercase_modelname}}.py公共测试基类tests/test_tokenization_common.py成熟范例tests/models/bert/test_tokenization_bert.py依赖装饰器src/transformers/testing_utils.py赞分享推理引擎大模型【免费下载链接】FlexGenRunning large language models on a single GPU for throughput-oriented scenarios.项目地址https://gitcode.com/gh_mirrors/fl/FlexGen点击查看免费下载相关推荐开源项目 Mantle 亮点深度解析Objective-C模型层的革命性简化开源项目 Mantle 亮点深度解析Objective C模型层的革命性简化 引言Objective C模型开发的痛点 在iOS/macOS开发中处理JS推理引擎大模型FlexGen 基准测试仓库中的 Hugging Face Transformers预训练模型加载、pipeline 推理与多框架实践指南FlexGen 基准测试仓库中的 Hugging Face Transformers预训练模型加载、pipeline 推理与多框架实践指南 本篇技术指南以当前推理引擎大模型FlexGen 仓库中的 Hugging Face transformers 混合 INT8 量化测试指南基于 bitsandbytes 的 8-bit 模型加载、调试与验证FlexGen 仓库中的 Hugging Face transformers 混合 INT8 量化测试指南基于 bitsandbytes 的 8 bit 模型推理引擎大模型上一篇Lightweight Charts终极代码分割策略基于Rollup的chunk优化完全指南下一篇cann/asc-devkit矩阵计算空间API创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考