Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Écrire des tests de modèle","local":"writing-model-tests","sections":[{"title":"Exécuter les contrats d’intégration de modèles localement","local":"run-model-integration-contracts-locally","sections":[],"depth":2},{"title":"Choisir une classe de test de base","local":"pick-a-base-test-class","sections":[],"depth":2},{"title":"CausalLMModelTest","local":"causallmmodeltest","sections":[{"title":"Définition des valeurs par défaut dans le CausalLMTester","local":"overriding-defaults-in-the-causallmtester","sections":[],"depth":3}],"depth":2},{"title":"VLMModelTest","local":"vlmmodeltest","sections":[{"title":"Définir des valeurs par défaut personnalisées dans VLMModelTester","local":"overriding-defaults-in-the-vlmmodeltester","sections":[],"depth":3}],"depth":2},{"title":"ALMModelTest","local":"almmodeltest","sections":[{"title":"Réécriture des valeurs par défaut dans ALMModelTester","local":"overriding-defaults-in-the-almmodeltester","sections":[],"depth":3}],"depth":2},{"title":"Écrire des tests pour d'autres architectures","local":"write-tests-for-other-architectures","sections":[{"title":"ModelTester et ModelTest","local":"modeltester-and-modeltest","sections":[],"depth":3},{"title":"Test des mixins","local":"test-mixins","sections":[],"depth":3},{"title":"Écrire un test de modèle","local":"writing-a-model-test","sections":[],"depth":3},{"title":"Organisation des fichiers","local":"file-organization","sections":[],"depth":3}],"depth":2},{"title":"Tests de configuration","local":"config-tests","sections":[],"depth":2},{"title":"Tests d'intégration et petits modèles","local":"integration-tests-and-tiny-models","sections":[{"title":"Rédaction de tests d'intégration","local":"writing-integration-tests","sections":[{"title":"Libération de la mémoire entre les tests","local":"releasing-memory-between-tests","sections":[],"depth":4},{"title":"Tests d'intégration de la génération","local":"generation-integration-tests","sections":[],"depth":4},{"title":"Attentes spécifiques au matériel","local":"hardware-specific-expectations","sections":[],"depth":4}],"depth":3},{"title":"Créer de minuscules modèles","local":"creating-tiny-models","sections":[],"depth":3}],"depth":2},{"title":"Contrôler ce qui est testé","local":"control-what-gets-tested","sections":[],"depth":2},{"title":"Étapes suivantes","local":"next-steps","sections":[],"depth":2}],"depth":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="{"title":"Writing model tests","local":"writing-model-tests","sections":[{"title":"Run Model Integration Contracts locally","local":"run-model-integration-contracts-locally","sections":[],"depth":2},{"title":"Pick a base test class","local":"pick-a-base-test-class","sections":[],"depth":2},{"title":"CausalLMModelTest","local":"causallmmodeltest","sections":[{"title":"Overriding defaults in the CausalLMTester","local":"overriding-defaults-in-the-causallmtester","sections":[],"depth":3}],"depth":2},{"title":"VLMModelTest","local":"vlmmodeltest","sections":[{"title":"Overriding defaults in the VLMModelTester","local":"overriding-defaults-in-the-vlmmodeltester","sections":[],"depth":3}],"depth":2},{"title":"ALMModelTest","local":"almmodeltest","sections":[{"title":"Overriding defaults in the ALMModelTester","local":"overriding-defaults-in-the-almmodeltester","sections":[],"depth":3}],"depth":2},{"title":"Write tests for other architectures","local":"write-tests-for-other-architectures","sections":[{"title":"ModelTester and ModelTest","local":"modeltester-and-modeltest","sections":[],"depth":3},{"title":"Test mixins","local":"test-mixins","sections":[],"depth":3},{"title":"Writing a model test","local":"writing-a-model-test","sections":[],"depth":3},{"title":"File organization","local":"file-organization","sections":[],"depth":3}],"depth":2},{"title":"Config tests","local":"config-tests","sections":[],"depth":2},{"title":"Integration tests and tiny models","local":"integration-tests-and-tiny-models","sections":[{"title":"Writing integration tests","local":"writing-integration-tests","sections":[{"title":"Releasing memory between tests","local":"releasing-memory-between-tests","sections":[],"depth":4},{"title":"Generation integration tests","local":"generation-integration-tests","sections":[],"depth":4},{"title":"Hardware-specific expectations","local":"hardware-specific-expectations","sections":[],"depth":4}],"depth":3},{"title":"Creating tiny models","local":"creating-tiny-models","sections":[],"depth":3}],"depth":2},{"title":"Control what gets tested","local":"control-what-gets-tested","sections":[],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":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><!---->Écrire des tests de modèle<!----></span></h1><!--]--><!----> <p>La suite de tests de Transformers utilise une architecture basée sur des mixins pour générer automatiquement plus de 100 tests à partir d'un code minimal. Vous rédigez une petite quantité de code spécifique au modèle, et les mixins gèrent la sauvegarde/chargement, la génération, les pipelines, l'entraînement et le parallélisme de tenseurs.</p> <p>Exécutez les tests de votre modèle avec les commandes suivantes.</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> Copié</div><!----></button><!----></div> <pre class="language-bash "><!----><span class="hljs-comment"># run your model'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>La CI de Hugging Face exécute les tests de modèles sans <code>@slow</code> à chaque pull request, et les tests lents sont exécutés selon un planning nocturne (voir <a href="./pr_checks">Contrôles de pull request</a> pour savoir ce que la CI valide).</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><!---->Exécuter les contrats d’intégration de modèles localement<!----></span></h2><!--]--><!----> <p>Les MIC vérifient les chemins d'utilisation des modèles publiés par rapport aux lignes de base examinées à l'aide de petits fixtures. | |
| Le runner (<code>transformers-mic</code>) et <code>hf-doc-builder</code> sont fournis avec l'extra <code>testing</code>. Depuis votre checkout de 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> Copié</div><!----></button><!----></div> <pre class="language-bash "><!---->python -m pip install -e <span class="hljs-string">".[testing]"</span><!----></pre></div><!----> <p>Puis exécutez :</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> Copié</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>La commande utilise le code source de ce checkout et la révision de la flotte dans <code>tests/contracts.lock</code>. | |
| Elle exécute la flotte de prototypes par défaut ; <code>FLEET=control</code> sélectionne la flotte de contrôle plus petite. | |
| Comme dans la CI, elle vérifie la flotte de prototypes en <code>bfloat16</code> et la flotte de contrôle à la fois en <code>default</code> et en <code>bfloat16</code>. | |
| Elle télécharge les contrats, les fixtures et les baselines épinglés au besoin, les exécute localement, | |
| et affiche une ligne par fixture ainsi qu’un résumé ; toute fixture en échec fait échouer la commande. | |
| Les contrats sont répartis sur des processus de runner parallèles (<code>JOBS</code>, par défaut jusqu’à 4, <code>JOBS=1</code> pour les exécuter un par un). | |
| Chaque runner utilise <code>OMP_NUM_THREADS=4</code>, le nombre de threads avec lequel les baselines sont enregistrées (définir <code>OMP_NUM_THREADS</code> pour le remplacer), afin que le nombre de threads n’ajoute pas de différences par rapport à la CI. <code>make run-mic</code> est un wrapper autour du vérificateur <code>mic</code> (<code>python utils/checkers.py mic</code>), qui est opt-in | |
| et ne fait pas partie de <code>make check-repo</code> ni de la CI car il nécessite un accès réseau et des temps d’exécution des modèles. Il ne crée ni ne publie de contrats. | |
| Les dépendances d’exécution spécifiques au modèle et une baseline validée pour votre plateforme sont requises ; | |
| le passage d’une fixture ne valide pas le checkpoint complet.</p> <p>Par défaut, les fichiers modifiés correspondent à la différence entre la base de fusion avec <code>origin/main</code> et votre arborescence de travail, ainsi qu'aux fichiers non suivis. Cela inclut les commits de la branche, les modifications en attente et non en attente, ainsi que les deux chemins des renommages. Un clone propre n'a rien à exécuter. | |
| Lorsque les modifications sélectionnent tous les contrats (par exemple <code>setup.py</code>, le code partagé ou le verrou de la flotte dans <code>tests/contracts.lock</code>), rien ne s'exécute : la commande explique pourquoi, et vous choisissez <code>MODEL=<name></code> ou <code>ALL=1</code>.</p> <p>MIC dérive les dépendances des modèles à partir des configurations de fixtures (y compris les modèles imbriqués), des architectures et des classes de tokenizer/processor. Les modifications du répertoire d'un modèle sélectionnent les contrats correspondants ; les modifications du code Transformers partagé, des classes Auto, des déclarations de dépendances ou du pin de la flotte sélectionnent tous les contrats. Les modifications de la documentation et les modifications de tests ordinaires ne sélectionnent rien. L'exécuteur affiche sa sélection et les raisons ; l'absence de contrat correspondant signifie une absence de couverture, et non un contrôle de modèle réussi. La sélection est une heuristique, et non un graphe de dépendances Python complet ; utilisez <code>ALL=1</code> en cas de doute sur la couverture des dépendances indirectes.</p> <p><code>MODEL=bert</code> est plus restreint : il n’exécute que les contrats dont le fichier de référence <code>config.json</code> définit un <code>model_type</code> dans <code>models/bert</code>. Les modèles composites qui intègrent simplement BERT ne sont pas exécutés ; | |
| utilisez la sélection par défaut des fichiers modifiés (ou <code>ALL=1</code>) pour les inclure.</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><!---->Choisir une classe de test de base<!----></span></h2><!--]--><!----> <p>Trois classes de base couvrent les familles de modèles les plus courantes. Choisissez celle qui correspond à la modalité de votre modèle.</p> <table><thead><tr><th>Classe de base</th><th>Utilisation</th><th>Mixins</th></tr></thead><tbody><tr><td><code>CausalLMModelTest</code></td><td>Modèles de langage causaux</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>Modèles de vision-langage</td><td><code>ModelTesterMixin</code>, <code>GenerationTesterMixin</code>, <code>PipelineTesterMixin</code></td></tr><tr><td><code>ALMModelTest</code></td><td>Modèles audio-langage</td><td><code>ModelTesterMixin</code>, <code>GenerationTesterMixin</code>, <code>PipelineTesterMixin</code></td></tr></tbody></table> <p><code>VLMModelTest</code> et <code>ALMModelTest</code> partagent un parent commun, <code>MultiModalModelTest</code>, qui imbrique des sous-configurations dans une configuration de niveau supérieur composite et place les jetons de placeholder de modalité dans <code>input_ids</code> aux côtés des fonctionnalités brutes de la modalité (audio ou vision). <code>CausalLMModelTest</code> n’utilise pas le parent multimodal. Il s’appuie sur les trois mixins partagés et ajoute <code>TrainingTesterMixin</code> et <code>TensorParallelTesterMixin</code> pour la couverture de l’entraînement et du parallélisme de tenseurs.</p> <p>Pour les architectures qui ne correspondent à aucune des trois catégories (encodeur uniquement, encodeur-décodeur, etc.), construisez l’infrastructure de tests directement à partir du <a href="#modeltester-and-modeltest">motif à deux classes</a> et des <a href="#test-mixins">mixins de test</a> décrits ci-dessous.</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> est la classe de base recommandée pour tester les modèles de langage causaux. Elle hérite de cinq <a href="#test-mixins">mixins de test</a> et génère automatiquement des tests pour la sauvegarde/chargement, la génération, les pipelines, l’entraînement et le parallélisme de tenseurs.</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> Copié</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>Ces deux classes assurent une couverture de tests complète pour <code>MyModel</code> et toutes ses classes de tête (<code>MyModelForCausalLM</code>, <code>MyModelForSequenceClassification</code>, etc.). Consultez <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> pour un exemple concret.</p> <p><code>CausalLMModelTester</code> ne requiert que <code>base_model_class</code>. Le tester supprime le suffixe <code>Model</code> pour obtenir un nom de base (<code>LlamaModel</code> devient <code>Llama</code>), puis ajoute des suffixes tels que <code>Config</code> ou <code>ForCausalLM</code> pour découvrir les classes associées. Si une classe n’existe pas dans le module, l’attribut reste <code>None</code> et le tester ignore les tests correspondants.</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><!---->Définition des valeurs par défaut dans le CausalLMTester<!----></span></h3><!--]--><!----> <p>Si votre modèle ne respecte pas la convention de nommage standard, ou si vous devez personnaliser le comportement, surchargez les attributs sur le testeur ou la classe de test.</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> Copié</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'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>Pour les modèles qui nécessitent des paramètres de constructeur personnalisés sur le testeur, surchargez <code>__init__</code> et appelez <code>super().__init__(parent=parent)</code> avant de définir les attributs supplémentaires. Consultez <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> pour un exemple concret.</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> Copié</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> est la classe de base pour les modèles vision-langage. Elle hérite de trois mixins (<code>ModelTesterMixin</code>, <code>GenerationTesterMixin</code>, <code>PipelineTesterMixin</code>) et définit <code>_is_composite = True</code> afin de gérer plusieurs sous-modèles.</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> Copié</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><!---->Définir des valeurs par défaut personnalisées dans VLMModelTester<!----></span></h3><!--]--><!----> <p>Lorsque le VLM nécessite des paramètres de vision personnalisés ou des valeurs de configuration non par défaut, surchargez <code>__init__</code>. Définissez les valeurs par défaut à l'aide de <code>setdefault</code> avant d'appeler <code>super().__init__(parent, **kwargs)</code>. L'exemple ci-dessous présente les premières valeurs par défaut de <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> Copié</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">"image_token_id"</span>, <span class="hljs-number">1</span>) | |
| kwargs.setdefault(<span class="hljs-string">"image_size"</span>, <span class="hljs-number">32</span>) | |
| kwargs.setdefault(<span class="hljs-string">"patch_size"</span>, <span class="hljs-number">4</span>) | |
| kwargs.setdefault(<span class="hljs-string">"num_channels"</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>Les tests VLM diffèrent de <code>CausalLMModelTest</code> à quelques égards.</p> <ul><li>Vous devez définir <code>config_class</code>, <code>text_config_class</code>, <code>vision_config_class</code> et <code>conditional_generation_class</code> sur le testeur.</li> <li><code>VLMModelTest</code> n’inclut pas <code>TrainingTesterMixin</code> ni <code>TensorParallelTesterMixin</code>.</li> <li>Le <code>__init__</code> du testeur accepte les paramètres visuels (<code>image_size</code>, <code>patch_size</code>, <code>num_channels</code>, <code>num_image_tokens</code>) à partir de <code>**kwargs</code> et <code>setdefault()</code>.</li> <li><code>ConfigTester</code> utilise <code>has_text_modality=False</code> car la configuration de niveau supérieur est une configuration composite et non une configuration de modèle de texte.</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> est la classe de base pour les modèles audio-langage (ALM) tels que Qwen2Audio, AudioFlamingo3 et GraniteSpeech. Elle reproduit le schéma des VLM avec le même parent <code>MultiModalModelTest</code> et la même découverte automatique des classes de tête. Le mécanisme du côté vision est remplacé par des caractéristiques audio, une sous-configuration audio et une stratégie de placement des jetons audio.</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> Copié</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">"feature_attention_mask"</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><!---->Réécriture des valeurs par défaut dans ALMModelTester<!----></span></h3><!--]--><!----> <p>Le <code>__init__</code> du testeur définit des valeurs par défaut spécifiques à ALM (<code>feat_seq_length=128</code>, <code>num_mel_bins=80</code>, <code>audio_token_id=0</code>). Remplacez-les à l’aide de <code>setdefault</code> avant d’appeler <code>super().__init__(parent, **kwargs)</code>.</p> <p>Deux attributs de classe indiquent au testeur comment votre modèle nomme les éléments.</p> <ul><li><code>audio_mask_key</code> : le nom de l'argument que votre modèle attend pour le masque audio (<code>"feature_attention_mask"</code>, <code>"input_features_mask"</code>, etc.). Laissez-le à <code>None</code> si votre modèle n'utilise pas de masque audio distinct.</li> <li><code>audio_config_key</code> : le nom de l’attribut utilisé par votre configuration de plus haut niveau pour imbriquer la sous-configuration audio. La valeur par défaut est <code>"audio_config"</code>, mais des modèles comme GraniteSpeech utilisent <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> Copié</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">"feat_seq_length"</span>, <span class="hljs-number">60</span>) | |
| kwargs.setdefault(<span class="hljs-string">"max_source_positions"</span>, kwargs[<span class="hljs-string">"feat_seq_length"</span>] // <span class="hljs-number">2</span>) | |
| <span class="hljs-built_in">super</span>().__init__(parent, **kwargs)<!----></pre></div><!----> <p><code>ALMModelTester</code> exige de surcharger un hook, <code>get_audio_embeds_mask(audio_mask)</code>, et expose quelques hooks optionnels supplémentaires pour la personnalisation.</p> <ul><li><code>get_audio_embeds_mask(audio_mask)</code> : renvoie le masque des positions des embeddings audio par lot après le sous-échantillonnage de l’encodeur. Le testeur utilise la somme par ligne pour déterminer le nombre de placeholders <code>audio_token_id</code> à insérer dans <code>input_ids</code> ; ce nombre doit donc correspondre à ce que votre encodeur émet.</li> <li><code>create_audio_features()</code> : renvoie le tenseur de caractéristiques audio. La forme par défaut est <code>[batch_size, num_mel_bins, feat_seq_length]</code>. À surcharger si votre modèle, comme GraniteSpeech, attend des caractéristiques avec le temps en premier (<code>[batch_size, feat_seq_length, num_mel_bins]</code>).</li> <li><code>create_audio_mask()</code> : renvoie le masque d’attention au niveau audio. Par défaut, il construit une région valide aléatoire et contiguë pour chaque ligne du lot. Remplacez-le par un masque de longueur complète déterministe si vos tests comparent deux appels de <code>prepare_config_and_inputs_for_common()</code> entre eux, ou si votre encodeur audio délègue le traitement à un backend qui rejette les masques non nuls.</li> <li><code>place_audio_tokens(input_ids, config, num_audio_tokens)</code> : place les jetons de remplacement audio de manière contiguë après <code>BOS</code>. À n'implémenter que si votre modèle nécessite une disposition différente.</li> <li><code>get_audio_feature_key()</code> : renvoie la clé du dictionnaire d’entrées pour les caractéristiques audio (<code>"input_features"</code> par défaut).</li></ul> <p>En plus des tests multimodaux hérités, <code>ALMModelTest</code> ajoute <code>test_mismatching_num_audio_tokens</code>. Ce test vérifie que le modèle lève une <code>ValueError</code> claire lorsque le nombre de caractéristiques audio ne correspond pas au nombre de jetons de remplacement audio dans <code>input_ids</code>, et s’assure qu’un prompt contenant plusieurs segments audio est bien transmis avec succès.</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><!---->Écrire des tests pour d'autres architectures<!----></span></h2><!--]--><!----> <p>Pour les architectures encoder-only, encoder-decoder, audio ou autres architectures non standard, construisez l’infrastructure de tests directement à partir du schéma à deux classes et des mixins de tests décrits ci-dessous.</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 et ModelTest<!----></span></h3><!--]--><!----> <p>Chaque fichier de test de modèle suit la même structure.</p> <ol><li><code>ModelTester</code> (classe simple) crée de petites configurations et des entrées factices pour les tests, et peut également contenir de petits tests de régression spécifiques au modèle.</li> <li><code>ModelTest</code> (<code>unittest.TestCase</code> + mixins) hérite des tests générés automatiquement et les exécute sur chaque variante de modèle.</li></ol> <p><code>ModelTest</code> appelle <code>prepare_config_and_inputs_for_common()</code> sur le tester pour obtenir un tuple <code>(config, inputs_dict)</code>. Tous les mixins s’appuient sur <code>prepare_config_and_inputs_for_common()</code> pour les données de test.</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><!---->Test des mixins<!----></span></h3><!--]--><!----> <p>Choisissez les mixins dont votre modèle a besoin.</p> <table><thead><tr><th>Mixin</th><th>Fichier source</th><th>Ce qu'il teste</th></tr></thead><tbody><tr><td><code>ModelTesterMixin</code></td><td><code>tests/test_modeling_common.py</code></td><td>Enregistrement/chargement, point de contrôle des gradients, signature de l'avant, attributs courants</td></tr><tr><td><code>GenerationTesterMixin</code></td><td><code>tests/generation/test_utils.py</code></td><td>Glouton, échantillonnage, recherche par faisceau, décodage assisté</td></tr><tr><td><code>PipelineTesterMixin</code></td><td><code>tests/test_pipeline_mixin.py</code></td><td>Un test par tâche de pipeline</td></tr><tr><td><code>TrainingTesterMixin</code></td><td><code>tests/test_training_mixin.py</code></td><td>Surapprentissage sur un petit lot</td></tr><tr><td><code>TensorParallelTesterMixin</code></td><td><code>tests/test_tensor_parallel_mixin.py</code></td><td>Parallélisme de tenseurs distribué</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><!---->Écrire un test de modèle<!----></span></h3><!--]--><!----> <p>Consultez <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> pour un exemple complet et fonctionnel. Les étapes clés sont décrites ci-dessous.</p> <ol><li><p>La classe <code>ModelTester</code> construit de petites configurations et des entrées factices. Gardez les dimensions petites afin que les tests s’achèvent en quelques secondes sur CPU. Utilisez les trois utilitaires de tenseurs ci-dessous pour construire les entrées.</p> <ul><li><code>ids_tensor(shape, vocab_size)</code> : Tenseur d'entiers aléatoires dans <code>[0, vocab_size)</code>. À utiliser pour <code>input_ids</code>, <code>token_type_ids</code> et les tenseurs d'étiquettes.</li> <li><code>random_attention_mask(shape)</code> : tenseur binaire (0 et 1) où le premier jeton est toujours 1. À utiliser pour <code>attention_mask</code>.</li> <li><code>floats_tensor(shape, scale=1.0)</code> : Tenseur de nombres flottants aléatoires. À utiliser pour des entrées continues telles que <code>pixel_values</code> ou <code>inputs_embeds</code>.</li></ul> <p>Le testeur doit implémenter <code>get_config()</code>, <code>prepare_config_and_inputs()</code> et <code>prepare_config_and_inputs_for_common()</code>. Ajoutez des méthodes <code>create_and_check_*</code> pour chaque tête de tâche (modèle de base, classification de séquence, classification de jeton, etc.).</p></li> <li><p>Héritez des mixins dont votre modèle a besoin, définissez <code>all_model_classes</code> et <code>pipeline_model_mapping</code>, puis implémentez <code>setUp()</code>. Rédigez des méthodes <code>test_*</code> qui délèguent aux méthodes <code>create_and_check_*</code> du testeur.</p></li> <li><p>Pour chaque tête de tâche, ajoutez une méthode <code>create_and_check_*</code> sur le testeur qui instancie le modèle, exécute un passage avant et vérifie les formes des sorties. Ajoutez ensuite une méthode <code>test_*</code> correspondante sur la classe de test.</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><!---->Organisation des fichiers<!----></span></h3><!--]--><!----> <p>Les fichiers de test se trouvent dans <code>tests/models/mymodel/</code> selon la structure présentée ci-dessous.</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> Copié</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>Les tests de tokenizers suivent le même schéma. Héritez de <code>TokenizerTesterMixin</code> depuis <code>tests/test_tokenization_common.py</code>, définissez quelques attributs et obtenez des tests générés automatiquement. Consultez <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> pour un exemple.</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><!---->Tests de configuration<!----></span></h2><!--]--><!----> <p><code>ConfigTester</code> vérifie qu'une classe de configuration gère correctement la sérialisation, la sauvegarde/chargement et les propriétés standard. <code>CausalLMModelTest</code> et <code>VLMModelTest</code> incluent automatiquement les tests de configuration. Pour le parcours général avec <code>ModelTester</code> et <code>ModelTest</code>, définissez le testeur de configuration manuellement dans <code>setUp()</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> Copié</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> exécute plusieurs vérifications.</p> <ul><li>Vérifie que des propriétés courantes comme <code>hidden_size</code>, <code>num_attention_heads</code> et <code>num_hidden_layers</code> existent (et <code>vocab_size</code> si <code>has_text_modality=True</code>).</li> <li>Tests de la sérialisation JSON avec <code>to_json_string()</code> et <code>to_json_file()</code>.</li> <li>Allers-retours entre <code>save_pretrained()</code> et <code>from_pretrained()</code>.</li> <li>Vérifie la cohérence entre <code>id2label</code> et <code>label2id</code>.</li> <li>Crée une configuration sans arguments pour valider l’initialisation par défaut.</li> <li>Définit des arguments communs tels que <code>output_hidden_states</code> et vérifie qu’ils sont correctement stockés.</li></ul> <p>Passez <code>has_text_modality=False</code> pour les modèles uniquement visuels qui ne disposent pas de <code>vocab_size</code>, et transmettez des <code>**kwargs</code> supplémentaires pour remplacer les valeurs par défaut de la configuration.</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> Copié</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><!---->Tests d'intégration et petits modèles<!----></span></h2><!--]--><!----> <p>Les tests de mixin utilisent de petites configurations avec des poids aléatoires pour vérifier rapidement le comportement des modèles. Les tests d’intégration exécutent l’inférence avec de vrais poids pré-entraînés pour valider la justesse des sorties. Les petits modèles sur le Hub sont suffisamment compacts pour un CI rapide, mais structurés comme de vrais points de contrôle.</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><!---->Rédaction de tests d'intégration<!----></span></h3><!--]--><!----> <p>Placez les tests d’intégration dans une classe de test distincte, marquez-les avec <code>@slow</code> et incorporez <code>MemoryCleanupMixin</code>. Chaque test télécharge de vrais poids, exécute une inférence et vérifie les sorties par rapport aux valeurs attendues. Le mixin libère la mémoire utilisée par chaque test et exécute les méthodes de test sous <code>torch.no_grad()</code> — voir <a href="#releasing-memory-between-tests">Libération de la mémoire entre les tests</a> pour en savoir plus sur son fonctionnement et sur la manière de s’en exclure.</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> Copié</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">"myorg/mymodel-base"</span>).to(torch_device) | |
| tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"myorg/mymodel-base"</span>) | |
| inputs = tokenizer(<span class="hljs-string">"Hello, world"</span>, return_tensors=<span class="hljs-string">"pt"</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>Marquez tout test avec <code>@slow</code> s'il télécharge des poids, charge un jeu de données volumineux ou prend plus de quelques secondes. Le <a href="./pr_checks">CI de la pull request</a> ignore les tests lents, mais l'agenda nocturne les exécute.</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><!---->Libération de la mémoire entre les tests<!----></span></h4><!--]--><!----> <p>Les tests d’intégration chargent de vrais points de contrôle, de sorte qu’un test qui conserve une référence à son modèle peut provoquer une erreur de mémoire (OOM) pour tout ce qui s’exécute ensuite. <code>MemoryCleanupMixin</code>, dans <code>tests/test_memory_cleanup_mixin.py</code>, gère ce problème pour l’ensemble de la classe :</p> <ul><li>Exécute <code>cleanup(torch_device, gc_collect=True)</code> avant et après chaque test afin que les résidus ne se transmettent pas.</li> <li>Supprime les attributs ajoutés par le test sur <code>self</code> (y compris les caches <code>@cached_property</code>) et sur la classe (par exemple <code>cls.model = ...</code> dans <code>setUpClass</code>). Pytest conserve les instances de test en vie pour toute la session, de sorte que <code>gc.collect()</code> ne peut pas libérer les objets que ces références continuent de retenir.</li> <li>Exécute les méthodes de test sous <code>torch.no_grad()</code> afin qu'un passage avant ne conserve pas les activations.</li></ul> <p>Les classes qui entraînent ou appellent <code>backward()</code> définissent <code>run_under_no_grad = False</code>. Une classe qui combine les deux place <code>@with_grad</code> ou <code>@with_no_grad</code> sur les méthodes de test individuelles.</p> <p>Les attributs définis dans le corps de la classe sont conservés, tandis que tout ce qui est ajouté ultérieurement est ignoré. Si vous redéfinissez <code>setUp</code>, appelez <code>super().setUp()</code> : le mixin capture les attributs de l'instance à cet endroit et lève une erreur en l'absence de cet appel.</p> <blockquote class="tip"><p><code>MemoryCleanupTestCase</code> combine le mixin avec <code>TestCasePlus</code>, pour les tests qui souhaitent également la suppression automatique des répertoires temporaires.</p></blockquote> <p>Pour détecter un test qui fuit de la mémoire, définissez <code>TRANSFORMERS_TEST_MEMORY_LEAK_MIB</code> sur un budget en Mo ; tout test qui laisse plus que cette quantité allouée sur l’appareil est signalé avec son usage de pic. Ajoutez <code>TRANSFORMERS_TEST_MEMORY_LEAK_MODE=error</code> pour faire échouer le test au lieu d’émettre un avertissement. Cette vérification est désactivée par défaut et vous devez l’activer lorsque vous recherchez des résidus. Le démontage habituel du mixin exécute déjà <code>gc.collect</code>, ce qui peut libérer l’allocation que vous souhaiteriez inspecter lors de la reproduction d’une fuite.</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><!---->Tests d'intégration de la génération<!----></span></h4><!--]--><!----> <p>Utilisez <code>do_sample=False</code> dans les tests de génération afin que la sortie soit déterministe d'une exécution à l'autre et quel que soit le matériel. Pour les modèles Mixture-of-Experts, appelez également <code>model.set_experts_implementation("eager")</code> avant la génération pour forcer un chemin de dispatch des experts stable. Sans cela, de petites différences numériques dans le routeur peuvent modifier l'expert chargé de traiter un jeton et changer la sortie.</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> Copié</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">"myorg/mymodel-base"</span>).to(torch_device) | |
| tokenizer = AutoTokenizer.from_pretrained(<span class="hljs-string">"myorg/mymodel-base"</span>) | |
| inputs = tokenizer(<span class="hljs-string">"Hello, world"</span>, return_tensors=<span class="hljs-string">"pt"</span>).to(torch_device) | |
| <span class="hljs-comment"># model.set_experts_implementation("eager") # 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">"Hello, world! This is the expected continuation..."</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><!---->Attentes spécifiques au matériel<!----></span></h4><!--]--><!----> <p>La CI de Transformers exécute les tests lents sur une NVIDIA A10. Les résultats numériques peuvent varier légèrement d'une génération de GPU à l'autre, les tests d'intégration utilisent donc la classe <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/testing_utils.py#L3247" rel="nofollow">Expectations</a> pour enregistrer les valeurs attendues par appareil. <code>Expectations</code> sélectionne la correspondance optimale pour le matériel actuel en fonction des clés SM <code>(device_type, (major, minor))</code>, et recourt à une valeur par défaut en l'absence de correspondance.</p> <p>Exécutez <code>torch.cuda.get_device_capability()</code> pour afficher la version SM de votre appareil local (par exemple <code>(8, 6)</code> pour une A10, <code>(9, 0)</code> pour une H100).</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> Copié</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">"cuda"</span>, (<span class="hljs-number">8</span>, <span class="hljs-number">6</span>)): [<span class="hljs-string">"Hello, world! This is the A10 continuation..."</span>], | |
| (<span class="hljs-string">"cuda"</span>, (<span class="hljs-number">9</span>, <span class="hljs-number">0</span>)): [<span class="hljs-string">"Hello, world! This is the H100 continuation..."</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><!---->Créer de minuscules modèles<!----></span></h3><!--]--><!----> <p>Les petits modèles aux poids aléatoires sont hébergés sur le Hub sous l’organisation <a href="https://huggingface.co/hf-internal-testing" rel="nofollow">hf-internal-testing</a>. Les tests de pipeline s’appuient sur ces petits modèles lorsqu’ils ont besoin d’un point de contrôle hébergé sur le Hub, sans se soucier de la qualité de la sortie. Les tests de fumée rapides chargent également ces petits modèles pour vérifier les formes du passage avant, sans télécharger de points de contrôle volumineux.</p> <p>Les modèles de petite taille constituent un dernier recours pour les tests d’intégration. N’utilisez-les que lorsque le point de contrôle le plus petit disponible dépasse environ 24 Go de VRAM. Utilisez, lorsque c’est possible, les poids pré-entraînés d’origine afin de détecter les régressions numériques réelles.</p> <p>Le script <code>utils/create_dummy_models.py</code> génère de très petits modèles à partir de <code>ModelTester.get_config()</code>. Le script extrait les hyperparamètres réduits de votre tester, construit un modèle avec des poids aléatoires et télécharge le résultat sur le Hub.</p> <p>Générer de petits modèles localement.</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> Copié</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/create_dummy_models.py output_dir -m your_model_type<!----></pre></div><!----> <p>Téléchargez-les sur le 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> Copié</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>Chaque modèle utilise le nom <code>hf-internal-testing/tiny-random-{ModelClassName}</code> et est enregistré dans <code>tests/utils/tiny_model_summary.json</code>. Un workflow CI (<code>.github/workflows/check_tiny_models.yml</code>) régénère les petits modèles quotidiennement.</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><!---->Contrôler ce qui est testé<!----></span></h2><!--]--><!----> <p>Les drapeaux booléens de <code>ModelTesterMixin</code> activent ou désactivent les tests générés automatiquement. Redéfinissez un drapeau sur votre classe de test pour activer ou désactiver des vérifications spécifiques.</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> Copié</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>Drapeau</th><th>Par défaut</th><th>Ce qu'il contrôle</th></tr></thead><tbody><tr><td><code>test_resize_embeddings</code></td><td><code>True</code></td><td>Redimensionnement de la couche d'embedding</td></tr><tr><td><code>test_resize_position_embeddings</code></td><td><code>False</code></td><td>Redimensionnement des embeddings de position</td></tr><tr><td><code>test_mismatched_shapes</code></td><td><code>True</code></td><td>Gestion des formes d'entrée/sortie non correspondantes</td></tr><tr><td><code>test_missing_keys</code></td><td><code>True</code></td><td>Avertissements de clés manquantes au chargement</td></tr><tr><td><code>test_torch_exportable</code></td><td><code>True</code></td><td>Compatibilité avec <code>torch.export</code></td></tr><tr><td><code>test_all_params_have_gradient</code></td><td><code>True</code></td><td>Tous les paramètres reçoivent des gradients (définissez <code>False</code> si tous les paramètres ne sont pas activés à chaque passe avant, comme pour les experts MoE)</td></tr><tr><td><code>is_encoder_decoder</code></td><td><code>False</code></td><td>Tests spécifiques aux encodeurs-décodeurs</td></tr><tr><td><code>has_attentions</code></td><td><code>True</code></td><td>Tests de la sortie de l'attention</td></tr><tr><td><code>_is_composite</code></td><td><code>False</code></td><td>Gestion des modèles composites/multimodaux</td></tr><tr><td><code>model_split_percents</code></td><td><code>[0.5, 0.7, 0.9]</code></td><td>Pourcentages de répartition pour les tests de parallélisme de modèle</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><!---->Étapes suivantes<!----></span></h2><!--]--><!----> <ul><li>Consultez la documentation de <a href="https://docs.pytest.org/en/latest/getting-started.html" rel="nofollow">pytest</a> pour en savoir plus sur la sélection des tests, les fixtures, la journalisation et bien plus encore.</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:
- 104 kB
- Xet hash:
- 65d7753114da1435c16edd85f9b8c2780736bf17e04c227b4822844b5ce519e4
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.