---
title: thunder.recipes.HFTransformers
description: Recipe tuned for Hugging Face transformers models.
canonical: >-
  https://lightning.ai/docs/thunder/stable/api-reference/generated/thunder.recipes.HFTransformers
---
# thunder.recipes.HFTransformers {#thunder-recipes-hftransformers}

### `class thunder.recipes.HFTransformers(show_progress=False, interpreter='thunder.jit', plugins=None)` {#thunder.recipes.HFTransformers}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L189-L345)

Bases: [`BaseRecipe`](/docs/thunder/0.2.6/api-reference/generated/thunder.recipes.BaseRecipe.md#thunder.recipes.BaseRecipe "thunder.recipes.base.BaseRecipe")

Recipe tuned for Hugging Face `transformers` models.

Parameters:

*   **show\_progress** ([_bool_](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")_,_ _optional_) – Forwarded to [`BaseRecipe`](/docs/thunder/0.2.6/api-reference/generated/thunder.recipes.BaseRecipe.md#thunder.recipes.BaseRecipe "thunder.recipes.BaseRecipe").
    
*   **interpreter** ([_str_](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)")_,_ _optional_) – Thunder interpreter to use.
    
*   **plugins** (_Iterable_ _|_ _None__,_ _optional_) – Extra Thunder plugins.
    

#### `__init__(show_progress=False, interpreter='thunder.jit', plugins=None)` {#thunder.recipes.HFTransformers.__init__}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L200-L212)

Methods

- [`__init__([show_progress, interpreter, plugins])`](#thunder.recipes.HFTransformers.__init__)
- `add_plugins(plugins)`
- [`apply(model)`](#thunder.recipes.HFTransformers.apply) — Apply the recipe (compile the model) and patch generate / _sample so they work after tracing.
- `get_for_model(model)`
- `register(key)`
- [`setup_config()`](#thunder.recipes.HFTransformers.setup_config) — Enable NV-kernelised linear, matmul and SDPA ops on top of the base recipe’s debug configuration.
- `setup_executors()` — Resolves executor names to concrete Executor objects.
- `setup_fuser()` — Reconciles the requested fusion backend with self.executor_names.
- [`setup_lookasides()`](#thunder.recipes.HFTransformers.setup_lookasides) — Swap out the warning helper when running under the non Thunder-FX interpreter.
- [`setup_transforms()`](#thunder.recipes.HFTransformers.setup_transforms) — Prepend the InplaceIndexCopyTransform to the default transform list.
- [`validate(model)`](#thunder.recipes.HFTransformers.validate) — Emit warnings (or errors) if model falls outside the supported transformer versions or base classes.

#### `apply(model)` {#thunder.recipes.HFTransformers.apply}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L312-L345)

Apply the recipe (compile the model) and patch `generate` / `_sample` so they work after tracing.

Parameters:

**model** (_transformers.PreTrainedModel_) – The model to compile.

Returns:

Thunder-compiled model ready for inference.

Return type:

transformers.PreTrainedModel

#### `setup_config()` {#thunder.recipes.HFTransformers.setup_config}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L253-L264)

Enable NV-kernelised linear, matmul and SDPA ops on top of the base recipe’s debug configuration.

Returns:

Thunder config dictionary augmented with `nv_enable_*` flags.

Return type:

[dict](https://docs.python.org/3/builtins/stdtypes.html#dict "(in Python v3.15)")\[[str](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)"), Any\]

#### `setup_lookasides()` {#thunder.recipes.HFTransformers.setup_lookasides}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L266-L299)

Swap out the warning helper when running under the non Thunder-FX interpreter.

Returns:

list\[thunder.core.recipe.Lookaside\] | None

#### `setup_transforms()` {#thunder.recipes.HFTransformers.setup_transforms}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L301-L310)

Prepend the `InplaceIndexCopyTransform` to the default transform list.

Returns:

transform list.

Return type:

[list](https://docs.python.org/3/builtins/stdtypes.html#list "(in Python v3.15)")\[thunder.Transform\]

#### `classmethod validate(model)` {#thunder.recipes.HFTransformers.validate}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/recipes/hf_transformers.py#L214-L251)

Emit warnings (or errors) if _model_ falls outside the supported transformer versions or base classes.

Parameters:

**model** (_transformers.PreTrainedModel_) – Model instance to vet.

Raises:

[**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError "(in Python v3.15)") – If _model_ is not a `PreTrainedModel`.

---

**Navigation**

- Parent: [Generated](/docs/thunder/0.2.6/api-reference/generated.md)
