Buckets:

mishig's picture
download
raw
99 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;编写模型测试&quot;,&quot;local&quot;:&quot;writing-model-tests&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;在本地运行模型集成合约&quot;,&quot;local&quot;:&quot;run-model-integration-contracts-locally&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;选择一个基础测试类&quot;,&quot;local&quot;:&quot;pick-a-base-test-class&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;CausalLMModelTest&quot;,&quot;local&quot;:&quot;causallmmodeltest&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;在 CausalLMTester 中覆盖默认值&quot;,&quot;local&quot;:&quot;overriding-defaults-in-the-causallmtester&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;VLMModelTest&quot;,&quot;local&quot;:&quot;vlmmodeltest&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;在 VLMModelTester 中覆盖默认值&quot;,&quot;local&quot;:&quot;overriding-defaults-in-the-vlmmodeltester&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;ALMModelTest&quot;,&quot;local&quot;:&quot;almmodeltest&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;在 ALMModelTester 中覆盖默认值&quot;,&quot;local&quot;:&quot;overriding-defaults-in-the-almmodeltester&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;为其他架构编写测试&quot;,&quot;local&quot;:&quot;write-tests-for-other-architectures&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;ModelTester 和 ModelTest&quot;,&quot;local&quot;:&quot;modeltester-and-modeltest&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;测试混入&quot;,&quot;local&quot;:&quot;test-mixins&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;编写模型测试&quot;,&quot;local&quot;:&quot;writing-a-model-test&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;文件组织&quot;,&quot;local&quot;:&quot;file-organization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;配置测试&quot;,&quot;local&quot;:&quot;config-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;集成测试与小型模型&quot;,&quot;local&quot;:&quot;integration-tests-and-tiny-models&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;编写集成测试&quot;,&quot;local&quot;:&quot;writing-integration-tests&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;在测试之间释放内存&quot;,&quot;local&quot;:&quot;releasing-memory-between-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4},{&quot;title&quot;:&quot;生成集成测试&quot;,&quot;local&quot;:&quot;generation-integration-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4},{&quot;title&quot;:&quot;特定硬件的预期&quot;,&quot;local&quot;:&quot;hardware-specific-expectations&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4}],&quot;depth&quot;:3},{&quot;title&quot;:&quot;创建小型模型&quot;,&quot;local&quot;:&quot;creating-tiny-models&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;控制测试内容&quot;,&quot;local&quot;:&quot;control-what-gets-tested&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;后续步骤&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.C3-W8qqF.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Baox-VVd.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/B70Tli8p.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app._pwPQKrO.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DDvWAwjm.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BwPQCktU.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.BPY7UeUb.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Cvo5aEJt.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/6.eoJWLTJt.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CoCKmtgk.js" rel="modulepreload">
<!--6vfrt7--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Writing model tests&quot;,&quot;local&quot;:&quot;writing-model-tests&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Run Model Integration Contracts locally&quot;,&quot;local&quot;:&quot;run-model-integration-contracts-locally&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Pick a base test class&quot;,&quot;local&quot;:&quot;pick-a-base-test-class&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;CausalLMModelTest&quot;,&quot;local&quot;:&quot;causallmmodeltest&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overriding defaults in the CausalLMTester&quot;,&quot;local&quot;:&quot;overriding-defaults-in-the-causallmtester&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;VLMModelTest&quot;,&quot;local&quot;:&quot;vlmmodeltest&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overriding defaults in the VLMModelTester&quot;,&quot;local&quot;:&quot;overriding-defaults-in-the-vlmmodeltester&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;ALMModelTest&quot;,&quot;local&quot;:&quot;almmodeltest&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Overriding defaults in the ALMModelTester&quot;,&quot;local&quot;:&quot;overriding-defaults-in-the-almmodeltester&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Write tests for other architectures&quot;,&quot;local&quot;:&quot;write-tests-for-other-architectures&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;ModelTester and ModelTest&quot;,&quot;local&quot;:&quot;modeltester-and-modeltest&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Test mixins&quot;,&quot;local&quot;:&quot;test-mixins&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Writing a model test&quot;,&quot;local&quot;:&quot;writing-a-model-test&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;File organization&quot;,&quot;local&quot;:&quot;file-organization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Config tests&quot;,&quot;local&quot;:&quot;config-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Integration tests and tiny models&quot;,&quot;local&quot;:&quot;integration-tests-and-tiny-models&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Writing integration tests&quot;,&quot;local&quot;:&quot;writing-integration-tests&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Releasing memory between tests&quot;,&quot;local&quot;:&quot;releasing-memory-between-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4},{&quot;title&quot;:&quot;Generation integration tests&quot;,&quot;local&quot;:&quot;generation-integration-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4},{&quot;title&quot;:&quot;Hardware-specific expectations&quot;,&quot;local&quot;:&quot;hardware-specific-expectations&quot;,&quot;sections&quot;:[],&quot;depth&quot;:4}],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Creating tiny models&quot;,&quot;local&quot;:&quot;creating-tiny-models&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Control what gets tested&quot;,&quot;local&quot;:&quot;control-what-gets-tested&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="writing-model-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#writing-model-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->编写模型测试<!----></span></h1><!--]--><!----> <p>Transformers 测试套件采用基于混入(mixin)的架构,通过极少量的代码自动生成 100 多项测试。您只需编写少量与特定模型相关的代码,混入便会处理保存/加载、生成、流水线、训练以及张量并行。</p> <p>使用以下命令运行模型的测试。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!----><span class="hljs-comment"># run your model&#x27;s tests</span>
pytest tests/models/mymodel/test_modeling_mymodel.py -v
<span class="hljs-comment"># run a specific test</span>
pytest tests/models/mymodel/test_modeling_mymodel.py::MyModelTest::test_model
<span class="hljs-comment"># run tests matching a keyword pattern (useful to run all integration tests)</span>
pytest tests/models/mymodel/ -k integration -v
<span class="hljs-comment"># include slow integration tests</span>
RUN_SLOW=1 pytest tests/models/mymodel/ -v<!----></pre></div><!----> <p>Hugging Face CI 会在每个拉取请求中运行不带 <code>@slow</code> 的模型测试,而慢速测试则按每日计划运行(有关 CI 验证的内容,请参阅 <a href="./pr_checks">拉取请求检查</a>)。</p> <!--[1--><h2 class="relative group"><a id="run-model-integration-contracts-locally" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-model-integration-contracts-locally"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->在本地运行模型集成合约<!----></span></h2><!--]--><!----> <p>MIC 使用小型测试用例,将已发布模型的使用路径与经过审查的基线进行比对。
运行器(<code>transformers-mic</code>)和 <code>hf-doc-builder</code> 附带 <code>testing</code> 额外依赖。从你的 Transformers 检出目录中:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!---->python -m pip install -e <span class="hljs-string">&quot;.[testing]&quot;</span><!----></pre></div><!----> <p>然后运行:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!---->make run-mic MODEL=bert <span class="hljs-comment"># only the contracts for this model directory under src/transformers/models/</span>
make run-mic MODEL=Wav2Vec2 <span class="hljs-comment"># a model name works too (case and punctuation are ignored)</span>
make run-mic MODEL=qwen+ <span class="hljs-comment"># every matching model directory: + stands for any characters</span>
make run-mic <span class="hljs-comment"># contracts affected by local changes</span>
make run-mic ALL=1 <span class="hljs-comment"># all fixtures in the fleet</span>
make run-mic BASE=main <span class="hljs-comment"># use a different Git comparison base</span>
make run-mic FLEET=control <span class="hljs-comment"># the control fleet instead of the default prototypes fleet</span>
make run-mic ALL=1 JOBS=8 <span class="hljs-comment"># number of runner processes working in parallel (default: cores / 4, up to 4)</span><!----></pre></div><!----> <p>该命令使用此签出(checkout)的源代码以及 <code>tests/contracts.lock</code> 中的 fleet 修订版本。
默认情况下,它会运行 prototypes fleet;使用 <code>FLEET=control</code> 可选择较小的 control fleet。
与 CI 类似,它在 <code>bfloat16</code> 下检查 prototypes fleet,并在 <code>default</code> 和 <code>bfloat16</code> 两种模式下检查 control fleet。
它会按需下载固定的 contracts、fixtures 和 baselines,在本地执行它们,
并为每个 fixture 打印一行输出以及一个摘要;任何失败的 fixture 都会导致命令失败。
Contracts 会分配给并行的 runner 进程(<code>JOBS</code>,默认最多为 4,使用 <code>JOBS=1</code> 可逐个运行)。
每个 runner 使用 <code>OMP_NUM_THREADS=4</code>,即记录 baselines 时使用的线程数(设置 <code>OMP_NUM_THREADS</code> 可覆盖此值),因此线程数不会增加与 CI 之间的差异。<code>make run-mic</code> 是 <code>mic</code> 检查器(<code>python utils/checkers.py mic</code>)的封装,它是可选的,
并且不属于 <code>make check-repo</code> 或 CI 的一部分,因为它需要网络访问和模型运行时。它不会创建或发布 contracts。
需要模型特定的运行时依赖项以及针对您的平台经过审核的 baseline;
fixture 通过并不验证完整的 checkpoint。</p> <p>默认情况下,已更改的文件是合并基点与 <code>origin/main</code> 以及你的工作树之间的差异,外加未跟踪的文件。这包括分支提交、已暂存和未暂存的更改,以及重命名的两个路径。干净的检出没有任何可运行的内容。
当更改选中了所有合约(例如 <code>setup.py</code>、共享代码或 <code>tests/contracts.lock</code> 中的车队固定版本)时,不会运行任何内容:该命令会说明原因,然后你选择 <code>MODEL=&lt;name></code> 或 <code>ALL=1</code>。</p> <p>MIC 从 fixture 配置(包括嵌套模型)、架构以及 tokenizer/processor 类中推导模型依赖关系。模型目录的更改会选择匹配的契约;对共享的 Transformers 代码、Auto 类、依赖声明或 fleet pin 的更改会选择所有契约。文档和仅涉及普通测试的更改不会选择任何内容。运行器会打印其选择结果及原因;没有匹配的契约意味着没有覆盖,而非模型检查成功。选择机制是一种启发式方法,并非完整的 Python 依赖图;如果不确定间接依赖是否被覆盖,请使用 <code>ALL=1</code>。</p> <p><code>MODEL=bert</code> 的范围更窄:它仅运行那些在 <code>models/bert</code> 中定义了 <code>model_type</code> 的 fixture <code>config.json</code> 所对应的合约;仅嵌入 BERT 的复合模型不会被运行;请使用默认的变更文件选择(或 <code>ALL=1</code>)来包含它们。</p> <!--[1--><h2 class="relative group"><a id="pick-a-base-test-class" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pick-a-base-test-class"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->选择一个基础测试类<!----></span></h2><!--]--><!----> <p>三个基类涵盖了最常见的模型家族。请选择与您的模型模态相匹配的基类。</p> <table><thead><tr><th>基类</th><th>用于</th><th>混入</th></tr></thead><tbody><tr><td><code>CausalLMModelTest</code></td><td>因果语言模型</td><td><code>ModelTesterMixin</code>, <code>GenerationTesterMixin</code>, <code>PipelineTesterMixin</code>, <code>TrainingTesterMixin</code>, <code>TensorParallelTesterMixin</code></td></tr><tr><td><code>VLMModelTest</code></td><td>视觉语言模型</td><td><code>ModelTesterMixin</code>, <code>GenerationTesterMixin</code>, <code>PipelineTesterMixin</code></td></tr><tr><td><code>ALMModelTest</code></td><td>音频语言模型</td><td><code>ModelTesterMixin</code>, <code>GenerationTesterMixin</code>, <code>PipelineTesterMixin</code></td></tr></tbody></table> <p><code>VLMModelTest</code> 和 <code>ALMModelTest</code> 共享一个公共的 <code>MultiModalModelTest</code> 父类,该父类将子配置嵌套到一个复合的顶层配置中,并在 <code>input_ids</code> 中放置模态占位符 token,与原始模态特征(音频或视觉)并列。<code>CausalLMModelTest</code> 不使用多模态父类。它基于三个共享的 mixin 构建,并添加了 <code>TrainingTesterMixin</code> 和 <code>TensorParallelTesterMixin</code> 以覆盖训练和张量并行测试。</p> <p>对于不符合上述三种(仅编码器、编码器-解码器等)任何一种的架构,请直接从下文描述的 <a href="#modeltester-and-modeltest">双类模式</a> 和 <a href="#test-mixins">测试混入</a> 构建测试基础设施。</p> <!--[1--><h2 class="relative group"><a id="causallmmodeltest" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#causallmmodeltest"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->CausalLMModelTest<!----></span></h2><!--]--><!----> <p><code>CausalLMModelTest</code> 是测试因果语言模型的推荐基类。它继承自五个 <a href="#test-mixins">测试混入类</a>,并自动生成用于保存/加载、生成、流水线、训练和张量并行的测试。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> unittest
<span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> is_torch_available
<span class="hljs-keyword">from</span> ...causal_lm_tester <span class="hljs-keyword">import</span> CausalLMModelTest, CausalLMModelTester
<span class="hljs-keyword">if</span> is_torch_available():
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> MyModel
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelTester</span>(<span class="hljs-title class_ inherited__">CausalLMModelTester</span>):
<span class="hljs-keyword">if</span> is_torch_available():
base_model_class = MyModel
<span class="hljs-meta">@require_torch</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelTest</span>(CausalLMModelTest, unittest.TestCase):
model_tester_class = MyModelTester<!----></pre></div><!----> <p>这两个类为 <code>MyModel</code> 及其所有头类(<code>MyModelForCausalLM</code>、<code>MyModelForSequenceClassification</code> 等)提供了完整的测试覆盖。请参阅 <a href="https://github.com/huggingface/transformers/blob/main/tests/models/llama/test_modeling_llama.py" rel="nofollow">tests/models/llama/test_modeling_llama.py</a> 中的实际示例。</p> <p><code>CausalLMModelTester</code> 仅需 <code>base_model_class</code>。该测试器会去除 <code>Model</code> 后缀以获取基础名称(<code>LlamaModel</code> 变为 <code>Llama</code>),然后添加诸如 <code>Config</code> 或 <code>ForCausalLM</code> 之类的后缀来发现相关类。如果模块中不存在某个类,该属性将保持为 <code>None</code>,测试器会跳过相应的测试。</p> <!--[2--><h3 class="relative group"><a id="overriding-defaults-in-the-causallmtester" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#overriding-defaults-in-the-causallmtester"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->在 CausalLMTester 中覆盖默认值<!----></span></h3><!--]--><!----> <p>如果你的模型不符合标准命名规范,或者需要自定义行为,请在测试器或测试类上覆盖相关属性。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelTester</span>(<span class="hljs-title class_ inherited__">CausalLMModelTester</span>):
<span class="hljs-keyword">if</span> is_torch_available():
base_model_class = MyModel
<span class="hljs-comment"># override if the class name doesn&#x27;t follow the convention</span>
causal_lm_class = MyCustomCausalLM
<span class="hljs-meta">@require_torch</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelTest</span>(CausalLMModelTest, unittest.TestCase):
model_tester_class = MyModelTester
<span class="hljs-comment"># disable embedding resize tests</span>
test_resize_embeddings = <span class="hljs-literal">False</span><!----></pre></div><!----> <p>对于需要在测试器上设置自定义构造器参数的模型,请重写 <code>__init__</code>,并在设置额外属性之前调用 <code>super().__init__(parent=parent)</code>。请参阅 <a href="https://github.com/huggingface/transformers/blob/main/tests/models/youtu/test_modeling_youtu.py" rel="nofollow">tests/models/youtu/test_modeling_youtu.py</a> 中的实际示例。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">YoutuModelTester</span>(<span class="hljs-title class_ inherited__">CausalLMModelTester</span>):
<span class="hljs-keyword">if</span> is_torch_available():
base_model_class = YoutuModel
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, parent, kv_lora_rank=<span class="hljs-number">16</span>, q_lora_rank=<span class="hljs-number">32</span></span>):
<span class="hljs-built_in">super</span>().__init__(parent=parent)
<span class="hljs-variable language_">self</span>.kv_lora_rank = kv_lora_rank
<span class="hljs-variable language_">self</span>.q_lora_rank = q_lora_rank<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="vlmmodeltest" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#vlmmodeltest"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->VLMModelTest<!----></span></h2><!--]--><!----> <p><code>VLMModelTest</code> 是视觉-语言模型的基础类。它继承自三个混入类(<code>ModelTesterMixin</code>、<code>GenerationTesterMixin</code>、<code>PipelineTesterMixin</code>),并设置 <code>_is_composite = True</code> 以处理多个子模型。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> unittest
<span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> is_torch_available
<span class="hljs-keyword">from</span> ...vlm_tester <span class="hljs-keyword">import</span> VLMModelTest, VLMModelTester
<span class="hljs-keyword">if</span> is_torch_available():
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> (
MyVLMConfig,
MyVLMModel,
MyVLMTextConfig,
MyVLMVisionConfig,
MyVLMForConditionalGeneration,
)
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyVLMTester</span>(<span class="hljs-title class_ inherited__">VLMModelTester</span>):
<span class="hljs-keyword">if</span> is_torch_available():
base_model_class = MyVLMModel
config_class = MyVLMConfig
text_config_class = MyVLMTextConfig
vision_config_class = MyVLMVisionConfig
conditional_generation_class = MyVLMForConditionalGeneration
<span class="hljs-meta">@require_torch</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyVLMTest</span>(VLMModelTest, unittest.TestCase):
model_tester_class = MyVLMTester<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="overriding-defaults-in-the-vlmmodeltester" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#overriding-defaults-in-the-vlmmodeltester"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->在 VLMModelTester 中覆盖默认值<!----></span></h3><!--]--><!----> <p>当 VLM 需要自定义视觉参数或非默认配置值时,请重写 <code>__init__</code>。在调用 <code>super().__init__(parent, **kwargs)</code> 之前,使用 <code>setdefault</code> 设置默认值。下面的示例展示了 <a href="https://github.com/huggingface/transformers/blob/main/tests/models/qianfan_ocr/test_modeling_qianfan_ocr.py" rel="nofollow">tests/models/qianfan_ocr/test_modeling_qianfan_ocr.py</a> 中的前几个默认值。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">QianfanOCRVisionText2TextModelTester</span>(<span class="hljs-title class_ inherited__">VLMModelTester</span>):
base_model_class = QianfanOCRModel
config_class = QianfanOCRConfig
text_config_class = Qwen3Config
vision_config_class = QianfanOCRVisionConfig
conditional_generation_class = QianfanOCRForConditionalGeneration
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, parent, **kwargs</span>):
kwargs.setdefault(<span class="hljs-string">&quot;image_token_id&quot;</span>, <span class="hljs-number">1</span>)
kwargs.setdefault(<span class="hljs-string">&quot;image_size&quot;</span>, <span class="hljs-number">32</span>)
kwargs.setdefault(<span class="hljs-string">&quot;patch_size&quot;</span>, <span class="hljs-number">4</span>)
kwargs.setdefault(<span class="hljs-string">&quot;num_channels&quot;</span>, <span class="hljs-number">3</span>)
<span class="hljs-comment"># ... more defaults</span>
<span class="hljs-built_in">super</span>().__init__(parent, **kwargs)<!----></pre></div><!----> <p>VLM 测试与 <code>CausalLMModelTest</code> 在几个方面有所不同。</p> <ul><li>您必须在测试器上设置 <code>config_class</code>、<code>text_config_class</code>、<code>vision_config_class</code> 和 <code>conditional_generation_class</code>。</li> <li><code>VLMModelTest</code> 不包含 <code>TrainingTesterMixin</code> 或 <code>TensorParallelTesterMixin</code>。</li> <li>测试器的 <code>__init__</code> 通过 <code>**kwargs</code> 和 <code>setdefault()</code> 接收视觉参数(<code>image_size</code>、<code>patch_size</code>、<code>num_channels</code>、<code>num_image_tokens</code>)。</li> <li><code>ConfigTester</code> 使用 <code>has_text_modality=False</code>,因为顶层配置是复合配置,而非文本模型配置。</li></ul> <!--[1--><h2 class="relative group"><a id="almmodeltest" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#almmodeltest"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->ALMModelTest<!----></span></h2><!--]--><!----> <p><code>ALMModelTest</code> 是 Qwen2Audio、AudioFlamingo3 和 GraniteSpeech 等音频-语言模型(ALM)的基类。它遵循 VLM 模式,拥有相同的 <code>MultiModalModelTest</code> 父类,并支持自动发现 head 类。视觉侧的机制被替换为音频特征、音频子配置以及音频 token 放置策略。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyALMTester</span>(<span class="hljs-title class_ inherited__">ALMModelTester</span>):
config_class = MyALMConfig
text_config_class = MyALMTextConfig
audio_config_class = MyALMAudioConfig
conditional_generation_class = MyALMForConditionalGeneration
audio_mask_key = <span class="hljs-string">&quot;feature_attention_mask&quot;</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyALMTest</span>(ALMModelTest, unittest.TestCase):
model_tester_class = MyALMTester<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="overriding-defaults-in-the-almmodeltester" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#overriding-defaults-in-the-almmodeltester"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->在 ALMModelTester 中覆盖默认值<!----></span></h3><!--]--><!----> <p>测试者的 <code>__init__</code> 会设置 ALM 特定的默认值(<code>feat_seq_length=128</code>、<code>num_mel_bins=80</code>、<code>audio_token_id=0</code>)。在调用 <code>super().__init__(parent, **kwargs)</code> 之前,请使用 <code>setdefault</code> 覆盖这些默认值。</p> <p>两个类属性告诉测试器你的模型如何命名各项内容。</p> <ul><li><code>audio_mask_key</code>:模型用于接收音频掩码的 kwarg 名称(例如 <code>"feature_attention_mask"</code>、<code>"input_features_mask"</code> 等)。如果模型不使用单独的音频掩码,请将其保留为 <code>None</code>。</li> <li><code>audio_config_key</code>:顶层配置中用于嵌套音频子配置的属性名称。默认值为 <code>"audio_config"</code>,但像 GraniteSpeech 这样的模型使用 <code>"encoder_config"</code>。</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">Qwen2AudioModelTester</span>(<span class="hljs-title class_ inherited__">ALMModelTester</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, parent, **kwargs</span>):
kwargs.setdefault(<span class="hljs-string">&quot;feat_seq_length&quot;</span>, <span class="hljs-number">60</span>)
kwargs.setdefault(<span class="hljs-string">&quot;max_source_positions&quot;</span>, kwargs[<span class="hljs-string">&quot;feat_seq_length&quot;</span>] // <span class="hljs-number">2</span>)
<span class="hljs-built_in">super</span>().__init__(parent, **kwargs)<!----></pre></div><!----> <p><code>ALMModelTester</code> 要求您重写一个钩子 <code>get_audio_embeds_mask(audio_mask)</code>,并额外提供了一些可选钩子以便进行自定义。</p> <ul><li><code>get_audio_embeds_mask(audio_mask)</code>:返回编码器下采样后,每个批次中音频嵌入位置的掩码。测试器会使用该掩码的逐行求和结果,来决定向 <code>input_ids</code> 中插入多少个 <code>audio_token_id</code> 占位符,因此该数量必须与你的编码器输出的数量一致。</li> <li><code>create_audio_features()</code>:返回音频特征张量。默认形状为 <code>[batch_size, num_mel_bins, feat_seq_length]</code>。如果你的模型(例如 GraniteSpeech)期望时间优先的特征(<code>[batch_size, feat_seq_length, num_mel_bins]</code>),请重写此方法。</li> <li><code>create_audio_mask()</code>:返回音频级别的注意力掩码。默认情况下,会为批次中的每一行构建一个随机的连续有效区域。如果您的测试将两次 <code>prepare_config_and_inputs_for_common()</code> 调用的结果相互比较,或者您的音频编码器会分发到一个拒绝非空掩码的后端,请覆盖此方法,使用确定性的全长度掩码。</li> <li><code>place_audio_tokens(input_ids, config, num_audio_tokens)</code>:在 <code>BOS</code> 之后连续放置音频占位符 token。仅当您的模型需要不同的布局时才覆盖此方法。</li> <li><code>get_audio_feature_key()</code>:返回音频特征的输入字典键(默认为 <code>"input_features"</code>)。</li></ul> <p>除了继承的多模态测试外,<code>ALMModelTest</code> 还添加了 <code>test_mismatching_num_audio_tokens</code>。该测试断言:当音频特征的数量与 <code>input_ids</code> 中音频占位符 token 的数量不匹配时,模型会抛出明确的 <code>ValueError</code>;并验证包含多个音频片段的 prompt 仍能成功完成前向传播。</p> <!--[1--><h2 class="relative group"><a id="write-tests-for-other-architectures" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#write-tests-for-other-architectures"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->为其他架构编写测试<!----></span></h2><!--]--><!----> <p>对于仅编码器、编码器-解码器、音频或其他非标准架构,请基于下文描述的两类模式和测试混入直接构建测试基础设施。</p> <!--[2--><h3 class="relative group"><a id="modeltester-and-modeltest" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#modeltester-and-modeltest"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->ModelTester 和 ModelTest<!----></span></h3><!--]--><!----> <p>每个模型测试文件都遵循相同的结构。</p> <ol><li><code>ModelTester</code>(普通类)用于创建小型配置和虚拟输入以进行测试,还可以包含针对特定模型的小型回归测试。</li> <li><code>ModelTest</code>(<code>unittest.TestCase</code> + mixins)继承自动生成的测试,并对每个模型变体运行这些测试。</li></ol> <p><code>ModelTest</code> 会调用 tester 的 <code>prepare_config_and_inputs_for_common()</code> 以获取 <code>(config, inputs_dict)</code> 元组。所有 mixin 都依赖 <code>prepare_config_and_inputs_for_common()</code> 来获取测试数据。</p> <!--[2--><h3 class="relative group"><a id="test-mixins" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#test-mixins"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->测试混入<!----></span></h3><!--]--><!----> <p>选择你的模型所需的 mixins。</p> <table><thead><tr><th>Mixin</th><th>源文件</th><th>测试内容</th></tr></thead><tbody><tr><td><code>ModelTesterMixin</code></td><td><code>tests/test_modeling_common.py</code></td><td>保存/加载、梯度检查点、前向签名、常用属性</td></tr><tr><td><code>GenerationTesterMixin</code></td><td><code>tests/generation/test_utils.py</code></td><td>贪心解码、采样、束搜索、辅助解码</td></tr><tr><td><code>PipelineTesterMixin</code></td><td><code>tests/test_pipeline_mixin.py</code></td><td>每个流水线任务一个测试</td></tr><tr><td><code>TrainingTesterMixin</code></td><td><code>tests/test_training_mixin.py</code></td><td>在小批量上过度拟合</td></tr><tr><td><code>TensorParallelTesterMixin</code></td><td><code>tests/test_tensor_parallel_mixin.py</code></td><td>分布式张量并行</td></tr></tbody></table> <!--[2--><h3 class="relative group"><a id="writing-a-model-test" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#writing-a-model-test"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->编写模型测试<!----></span></h3><!--]--><!----> <p>完整的可运行示例请参见 <a href="https://github.com/huggingface/transformers/blob/main/tests/models/modernbert/test_modeling_modernbert.py" rel="nofollow">tests/models/modernbert/test_modeling_modernbert.py</a>。关键步骤概述如下。</p> <ol><li><p><code>ModelTester</code> 类用于构建小型配置和虚拟输入。请保持维度较小,以便测试能在 CPU 上于数秒内完成。使用以下三个张量辅助函数来构建输入。</p> <ul><li><code>ids_tensor(shape, vocab_size)</code>:位于 <code>[0, vocab_size)</code> 范围内的随机整数张量。用于 <code>input_ids</code>、<code>token_type_ids</code> 以及标签张量。</li> <li><code>random_attention_mask(shape)</code>:一个二进制张量(仅包含 0 和 1),其中第一个 token 始终为 1。用于 <code>attention_mask</code>。</li> <li><code>floats_tensor(shape, scale=1.0)</code>:随机浮点张量。适用于 <code>pixel_values</code> 或 <code>inputs_embeds</code> 等连续输入。</li></ul> <p>测试器必须实现 <code>get_config()</code>、<code>prepare_config_and_inputs()</code> 和 <code>prepare_config_and_inputs_for_common()</code>。为每个任务头(基础模型、序列分类、token 分类等)添加 <code>create_and_check_*</code> 方法。</p></li> <li><p>继承模型所需的 mixins,设置 <code>all_model_classes</code> 和 <code>pipeline_model_mapping</code>,并定义 <code>setUp()</code>。编写 <code>test_*</code> 方法,将其委托给 tester 的 <code>create_and_check_*</code> 方法。</p></li> <li><p>对于每个任务头,在 tester 上添加一个 <code>create_and_check_*</code> 方法,用于实例化模型、执行前向传播并断言输出形状。然后在测试类中添加对应的 <code>test_*</code> 方法。</p></li></ol> <!--[2--><h3 class="relative group"><a id="file-organization" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#file-organization"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->文件组织<!----></span></h3><!--]--><!----> <p>测试文件位于 <code>tests/models/mymodel/</code>,其结构如下所示。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-text "><!---->tests/models/mymodel/
├── __init__.py
├── test_modeling_mymodel.py # model tests (required)
├── test_tokenization_mymodel.py # tokenizer tests (if custom tokenizer)
├── test_image_processing_mymodel.py # image processor tests (if vision model)
├── test_feature_extraction_mymodel.py # feature extractor tests (if audio/speech model)
└── test_processing_mymodel.py # processor tests (if multimodal)<!----></pre></div><!----> <p>Tokenizer 测试遵循相同的模式。从 <code>tests/test_tokenization_common.py</code> 继承 <code>TokenizerTesterMixin</code>,设置几个属性,即可获得自动生成的测试。示例参见 <a href="https://github.com/huggingface/transformers/blob/main/tests/models/llama/test_tokenization_llama.py" rel="nofollow">tests/models/llama/test_tokenization_llama.py</a>。</p> <!--[1--><h2 class="relative group"><a id="config-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#config-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->配置测试<!----></span></h2><!--]--><!----> <p><code>ConfigTester</code> 用于验证 config 类是否正确处理序列化、保存/加载以及标准属性。<code>CausalLMModelTest</code> 和 <code>VLMModelTest</code> 会自动包含 config 测试。对于使用 <code>ModelTester</code> 和 <code>ModelTest</code> 的通用路径,需要在 <code>setUp()</code> 中手动定义 config tester。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> tests.test_configuration_common <span class="hljs-keyword">import</span> ConfigTester
<span class="hljs-keyword">def</span> <span class="hljs-title function_">setUp</span>(<span class="hljs-params">self</span>):
<span class="hljs-variable language_">self</span>.config_tester = ConfigTester(<span class="hljs-variable language_">self</span>, config_class=MyModelConfig, hidden_size=<span class="hljs-number">32</span>)
<span class="hljs-keyword">def</span> <span class="hljs-title function_">test_config</span>(<span class="hljs-params">self</span>):
<span class="hljs-variable language_">self</span>.config_tester.run_common_tests()<!----></pre></div><!----> <p><code>run_common_tests()</code> 会执行多项检查。</p> <ul><li>检查诸如 <code>hidden_size</code>、<code>num_attention_heads</code> 和 <code>num_hidden_layers</code> 等常见属性是否存在(如果 <code>has_text_modality=True</code>,则还需检查 <code>vocab_size</code>)。</li> <li>使用 <code>to_json_string()</code> 和 <code>to_json_file()</code> 测试 JSON 序列化。</li> <li>往返调用 <code>save_pretrained()</code> 和 <code>from_pretrained()</code>。</li> <li>确认 <code>id2label</code> 和 <code>label2id</code> 的一致性。</li> <li>创建一个不带任何参数的配置,以验证默认初始化。</li> <li>设置诸如 <code>output_hidden_states</code> 之类的通用 kwargs,并确认它们已正确存储。</li></ul> <p>对于缺少 <code>vocab_size</code> 的纯视觉模型,请传入 <code>has_text_modality=False</code>,并传入额外的 <code>**kwargs</code> 以覆盖配置默认值。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-variable language_">self</span>.config_tester = ConfigTester(
<span class="hljs-variable language_">self</span>, config_class=MyVisionConfig, has_text_modality=<span class="hljs-literal">False</span>, hidden_size=<span class="hljs-number">64</span>
)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="integration-tests-and-tiny-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#integration-tests-and-tiny-models"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->集成测试与小型模型<!----></span></h2><!--]--><!----> <p>Mixin 测试使用带有随机权重的小型配置,以快速验证模型行为。集成测试使用真实的预训练权重运行推理,以验证输出的正确性。Hub 上的微型模型足够小,适合快速 CI,但其结构与真实的检查点一致。</p> <!--[2--><h3 class="relative group"><a id="writing-integration-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#writing-integration-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->编写集成测试<!----></span></h3><!--]--><!----> <p>将集成测试放在单独的测试类中,使用 <code>@slow</code> 进行标记,并混入 <code>MemoryCleanupMixin</code>。每个测试都会下载真实的权重,运行推理,并将输出与预期值进行比对。该 mixin 会释放每个测试占用的内存,并在 <code>torch.no_grad()</code> 下运行测试方法——有关其具体行为以及如何退出,请参阅 <a href="#releasing-memory-between-tests">在测试之间释放内存</a>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> unittest
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer
<span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch, slow, torch_device
<span class="hljs-keyword">from</span> ...test_memory_cleanup_mixin <span class="hljs-keyword">import</span> MemoryCleanupMixin
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelIntegrationTest</span>(MemoryCleanupMixin, unittest.TestCase):
<span class="hljs-meta"> @slow</span>
<span class="hljs-meta"> @require_torch</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">test_inference</span>(<span class="hljs-params">self</span>):
model = MyModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;myorg/mymodel-base&quot;</span>).to(torch_device)
tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;myorg/mymodel-base&quot;</span>)
inputs = tokenizer(<span class="hljs-string">&quot;Hello, world&quot;</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).to(torch_device)
outputs = model(**inputs)
<span class="hljs-comment"># check against expected values</span>
expected_slice = torch.tensor([[-<span class="hljs-number">0.1234</span>, <span class="hljs-number">0.5678</span>, -<span class="hljs-number">0.9012</span>]])
torch.testing.assert_close(outputs.logits[<span class="hljs-number">0</span>, :<span class="hljs-number">1</span>, :<span class="hljs-number">3</span>], expected_slice, atol=<span class="hljs-number">1e-4</span>, rtol=<span class="hljs-number">1e-4</span>)<!----></pre></div><!----> <p>如果测试会下载权重、加载大型数据集或耗时超过几秒钟,请使用 <code>@slow</code> 对其进行标记。<a href="./pr_checks">pull request CI</a> 会跳过慢速测试,但每日定时任务会运行它们。</p> <!--[3--><h4 class="relative group"><a id="releasing-memory-between-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#releasing-memory-between-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->在测试之间释放内存<!----></span></h4><!--]--><!----> <p>集成测试会加载真实的检查点,因此如果某个测试保留了对模型的引用,可能会导致后续运行的任何测试因内存不足(OOM)而失败。<code>MemoryCleanupMixin</code>(位于 <code>tests/test_memory_cleanup_mixin.py</code> 中)会为整个类处理这一问题:</p> <ul><li>在每个测试前后运行 <code>cleanup(torch_device, gc_collect=True)</code>,以避免残留状态被带入后续测试。</li> <li>删除测试在 <code>self</code>(包括 <code>@cached_property</code> 缓存)和类上添加的属性(例如 <code>setUpClass</code> 中的 <code>cls.model = ...</code>)。Pytest 会在整个会话期间保持测试实例存活,因此 <code>gc.collect()</code> 无法释放这些引用仍持有的对象。</li> <li>在 <code>torch.no_grad()</code> 下运行测试方法,以便前向传播不会保留激活值。</li></ul> <p>用于训练或调用 <code>backward()</code> 的类会将 <code>run_under_no_grad = False</code> 设置为 <code>run_under_no_grad = False</code>。同时混合使用两者的类则会在各个测试方法上添加 <code>@with_grad</code> 或 <code>@with_no_grad</code>。</p> <p>在类体中分配的 Attributes 会被保留,之后添加的内容则会被丢弃。如果您重写了 <code>setUp</code>,请调用 <code>super().setUp()</code>:该 mixin 会在此处对实例属性进行快照,若未调用则会报错。</p> <blockquote class="tip"><p><code>MemoryCleanupTestCase</code> 将该 mixin 与 <code>TestCasePlus</code> 结合,适用于还需要自动清理临时目录的测试。</p></blockquote> <p>要查找存在内存泄漏的测试,请将 <code>TRANSFORMERS_TEST_MEMORY_LEAK_MIB</code> 设置为以 MiB 为单位的预算值;任何在设备上分配的内存超过该预算的测试都会被报告,并附带其峰值使用情况。添加 <code>TRANSFORMERS_TEST_MEMORY_LEAK_MODE=error</code> 可在检测到泄漏时使测试失败,而不是仅发出警告。该检查默认处于关闭状态,在排查残留内存时应将其开启。该 mixin 的常规 teardown 流程会执行 <code>gc.collect</code>,这可能会清除你在复现泄漏时想要检查的内存分配。</p> <!--[3--><h4 class="relative group"><a id="generation-integration-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#generation-integration-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->生成集成测试<!----></span></h4><!--]--><!----> <p>在生成测试中使用 <code>do_sample=False</code>,以确保输出在不同运行和硬件上具有确定性。对于 Mixture-of-Experts 模型,在生成前还需调用 <code>model.set_experts_implementation("eager")</code>,以强制使用稳定的专家调度路径。否则,路由器中的微小数值差异可能导致处理某个 token 的专家发生变化,从而改变输出。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-meta">@slow</span>
<span class="hljs-meta">@require_torch</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">test_generate</span>(<span class="hljs-params">self</span>):
model = MyModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;myorg/mymodel-base&quot;</span>).to(torch_device)
tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">&quot;myorg/mymodel-base&quot;</span>)
inputs = tokenizer(<span class="hljs-string">&quot;Hello, world&quot;</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>).to(torch_device)
<span class="hljs-comment"># model.set_experts_implementation(&quot;eager&quot;) # uncomment for MoE models</span>
generated_ids = model.generate(**inputs, max_new_tokens=<span class="hljs-number">20</span>, do_sample=<span class="hljs-literal">False</span>)
output = tokenizer.batch_decode(generated_ids, skip_special_tokens=<span class="hljs-literal">True</span>)
<span class="hljs-variable language_">self</span>.assertEqual(output, [<span class="hljs-string">&quot;Hello, world! This is the expected continuation...&quot;</span>])<!----></pre></div><!----> <!--[3--><h4 class="relative group"><a id="hardware-specific-expectations" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#hardware-specific-expectations"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->特定硬件的预期<!----></span></h4><!--]--><!----> <p>Transformers CI 在 NVIDIA A10 上运行慢速测试。由于数值结果可能因 GPU 代际不同而略有差异,因此集成测试使用 <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/testing_utils.py#L3247" rel="nofollow">Expectations</a> 类来注册针对每个设备的预期值。<code>Expectations</code> 会根据 <code>(device_type, (major, minor))</code> SM 键为当前硬件选择最佳匹配,若没有匹配项则回退到默认值。</p> <p>运行 <code>torch.cuda.get_device_capability()</code> 以打印本地 SM 版本(例如 A10 为 <code>(8, 6)</code>,H100 为 <code>(9, 0)</code>)。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> Expectations
expected_texts = Expectations(
{
(<span class="hljs-string">&quot;cuda&quot;</span>, (<span class="hljs-number">8</span>, <span class="hljs-number">6</span>)): [<span class="hljs-string">&quot;Hello, world! This is the A10 continuation...&quot;</span>],
(<span class="hljs-string">&quot;cuda&quot;</span>, (<span class="hljs-number">9</span>, <span class="hljs-number">0</span>)): [<span class="hljs-string">&quot;Hello, world! This is the H100 continuation...&quot;</span>],
}
).get_expectation()
<span class="hljs-variable language_">self</span>.assertEqual(output, expected_texts)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="creating-tiny-models" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#creating-tiny-models"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->创建小型模型<!----></span></h3><!--]--><!----> <p>带有随机权重的微型模型托管在 Hub 的 <a href="https://huggingface.co/hf-internal-testing" rel="nofollow">hf-internal-testing</a> 组织下。当 Pipeline 测试需要 Hub 托管的检查点但不关心输出质量时,会使用这些微型模型。快速的冒烟测试也会加载微型模型,以验证前向传播的形状,而无需下载大型检查点。</p> <p>Tiny 模型是集成测试的最后手段。仅当可用的最小检查点超过约 24 GB 显存时,才使用它们。在可能的情况下,使用原始预训练权重,以捕获真实的数值回归问题。</p> <p><code>utils/create_dummy_models.py</code> 脚本会根据 <code>ModelTester.get_config()</code> 生成小型模型。该脚本会从你的 tester 中提取小型超参数,构建一个具有随机权重的模型,并将结果上传到 Hub。</p> <p>在本地生成小型模型。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/create_dummy_models.py output_dir -m your_model_type<!----></pre></div><!----> <p>将它们上传到 Hub。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/create_dummy_models.py output_dir -m your_model_type --upload --organization hf-internal-testing<!----></pre></div><!----> <p>每个模型使用名称 <code>hf-internal-testing/tiny-random-{ModelClassName}</code>,并记录在 <code>tests/utils/tiny_model_summary.json</code> 中。一个 CI 工作流(<code>.github/workflows/check_tiny_models.yml</code>)会每天重新生成小型模型。</p> <!--[1--><h2 class="relative group"><a id="control-what-gets-tested" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#control-what-gets-tested"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->控制测试内容<!----></span></h2><!--]--><!----> <p><code>ModelTesterMixin</code> 上的布尔标志用于切换自动生成的测试。在您的测试类中覆盖任意标志,即可启用或禁用特定的检查。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelTest</span>(CausalLMModelTest, unittest.TestCase):
model_tester_class = MyModelTester
test_resize_embeddings = <span class="hljs-literal">False</span>
test_all_params_have_gradient = <span class="hljs-literal">False</span> <span class="hljs-comment"># when not all parameters are activated in every forward pass</span><!----></pre></div><!----> <table><thead><tr><th>标志</th><th>默认</th><th>它控制的内容</th></tr></thead><tbody><tr><td><code>test_resize_embeddings</code></td><td><code>True</code></td><td>嵌入层大小调整</td></tr><tr><td><code>test_resize_position_embeddings</code></td><td><code>False</code></td><td>位置嵌入调整</td></tr><tr><td><code>test_mismatched_shapes</code></td><td><code>True</code></td><td>不匹配的输入/输出形状处理</td></tr><tr><td><code>test_missing_keys</code></td><td><code>True</code></td><td>加载时缺少键的警告</td></tr><tr><td><code>test_torch_exportable</code></td><td><code>True</code></td><td><code>torch.export</code> 兼容性</td></tr><tr><td><code>test_all_params_have_gradient</code></td><td><code>True</code></td><td>所有参数都会接收梯度(如果并非所有参数在每次前向传播中都被激活,例如 MoE 专家,请将其设置为 <code>False</code>)</td></tr><tr><td><code>is_encoder_decoder</code></td><td><code>False</code></td><td>编码器-解码器特定测试</td></tr><tr><td><code>has_attentions</code></td><td><code>True</code></td><td>注意力输出测试</td></tr><tr><td><code>_is_composite</code></td><td><code>False</code></td><td>复合/多模态模型处理</td></tr><tr><td><code>model_split_percents</code></td><td><code>[0.5, 0.7, 0.9]</code></td><td>模型并行测试的分割百分比</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->后续步骤<!----></span></h2><!--]--><!----> <ul><li>有关测试选择、fixture、日志记录等更多信息,请参阅 <a href="https://docs.pytest.org/en/latest/getting-started.html" rel="nofollow">pytest</a> 文档。</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/testing.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_1szhmpp = {
base: "/docs/transformers/main/en",
assets: "/docs/transformers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/en/_app/immutable/entry/start.C3-W8qqF.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app._pwPQKrO.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 6],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
99 kB
·
Xet hash:
3d9eab762ccfed15ba1bc3ffb7133935a0b4c020a5a083394769b23551d39723

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.