---
title: thunder.dynamo.ThunderCompiler
description: |-
  A class that compiles a torch.fx.GraphModule to a thunder.ThunderModule.
  This class is meant to be used as a backend for the torch.compile()
  function.
canonical: >-
  https://lightning.ai/docs/thunder/stable/api-reference/thunder.dynamo.ThunderCompiler
---
# thunder.dynamo.ThunderCompiler {#thunder-dynamo-thundercompiler}

### `class thunder.dynamo.ThunderCompiler(**thunder_options)` {#thunder.dynamo.ThunderCompiler}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/dynamo/compiler.py#L87-L242)

Bases: [`object`](https://docs.python.org/3/builtins/functions.html#object "(in Python v3.15)")

A class that compiles a [`torch.fx.GraphModule`](https://docs.pytorch.org/docs/stable/fx.html#torch.fx.GraphModule "(in PyTorch v2.15)") to a [`thunder.ThunderModule`](/docs/thunder/thunder#thunder.ThunderModule "thunder.ThunderModule"). This class is meant to be used as a backend for the [`torch.compile()`](https://docs.pytorch.org/docs/stable/generated/torch.compile.html#torch.compile "(in PyTorch v2.15)") function.

Keyword Arguments:

**thunder\_options** – a dictionary of options to pass to [`thunder.jit()`](/docs/thunder/generated/thunder.jit#thunder.jit "thunder.jit").

Example

```python
>>> import torch
>>> from thunder.dynamo import ThunderCompiler
>>> backend = ThunderCompiler()
>>> x = torch.ones(2, requires_grad=True)
>>> @torch.compile(backend=backend)
... def func(x):
...     x = torch.sin(x)
...     if x.sum() > 0:
...         return x + 1
...     else:
...         return x - 1
>>> out = func(x)
```

#### `__init__(**thunder_options)` {#thunder.dynamo.ThunderCompiler.__init__}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/dynamo/compiler.py#L88-L130)

A class that compiles a [`torch.fx.GraphModule`](https://docs.pytorch.org/docs/stable/fx.html#torch.fx.GraphModule "(in PyTorch v2.15)") to a [`thunder.ThunderModule`](/docs/thunder/thunder#thunder.ThunderModule "thunder.ThunderModule"). This class is meant to be used as a backend for the [`torch.compile()`](https://docs.pytorch.org/docs/stable/generated/torch.compile.html#torch.compile "(in PyTorch v2.15)") function.

Keyword Arguments:

**thunder\_options** – a dictionary of options to pass to [`thunder.jit()`](/docs/thunder/generated/thunder.jit#thunder.jit "thunder.jit").

Example

```python
>>> import torch
>>> from thunder.dynamo import ThunderCompiler
>>> backend = ThunderCompiler()
>>> x = torch.ones(2, requires_grad=True)
>>> @torch.compile(backend=backend)
... def func(x):
...     x = torch.sin(x)
...     if x.sum() > 0:
...         return x + 1
...     else:
...         return x - 1
>>> out = func(x)
```

Methods

- [`__init__(**thunder_options)`](#thunder.dynamo.ThunderCompiler.__init__) — A class that compiles a torch.fx.GraphModule to a thunder.ThunderModule.
- [`save_reproducer_to_folder(reproducer_folder)`](#thunder.dynamo.ThunderCompiler.save_reproducer_to_folder) — Save the reproducer script for the GraphModule executed by Thunder to the specified reproducer_folder.

#### `__call__(gm, sample_args)` {#thunder.dynamo.ThunderCompiler.__call__}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/dynamo/compiler.py#L132-L155)

Call self as a function.

Parameters:

*   **gm** ([_GraphModule_](https://docs.pytorch.org/docs/stable/fx.html#torch.fx.GraphModule "(in PyTorch v2.15)")) –
    
*   **sample\_args** ([_list_](https://docs.python.org/3/builtins/stdtypes.html#list "(in Python v3.15)")_\[_[_torch.SymInt_](https://docs.pytorch.org/docs/stable/torch.html#torch.SymInt "(in PyTorch v2.15)")_,_ [_torch.Tensor_](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor "(in PyTorch v2.15)")_\]_) –
    

#### `save_reproducer_to_folder(reproducer_folder, use_pytest_benchmark=False, serialize_inputs=False)` {#thunder.dynamo.ThunderCompiler.save_reproducer_to_folder}

[\[source\]](https://github.com/Lightning-AI/lightning-thunder/blob/0.2.6/thunder/dynamo/compiler.py#L157-L242)

Save the reproducer script for the GraphModule executed by Thunder to the specified `reproducer_folder`. Each saved script is named as “graph\[graph\_id\]\_thunder\_\[module\_id\]”, where:

*   `graph_id` indexes the graph generated by Dynamo, which is then passed to Thunder.
    
*   `module_id` indexes the submodule split by the `thunder.dynamo.utils._splitter()`.
    

Parameters:

*   **reproducer\_folder** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)") | [`PathLike`](https://docs.python.org/3/library/os.html#os.PathLike "(in Python v3.15)")) – The folder where the reproducer code will be written. Can be specified as an absolute or relative path.
    
*   **use\_pytest\_benchmark** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Determines the type of script to create. When [`False`](https://docs.python.org/3/builtins/constants.html#False "(in Python v3.15)"), create a reproducer script. Otherwise, creats a benchmark script to compare the reproducer’s performance with other backends, including Torch eager, torch.compile.
    
*   **reproducer\_folder** ([_str_](https://docs.python.org/3/builtins/stdtypes.html#str "(in Python v3.15)") _|_ [_os.PathLike_](https://docs.python.org/3/library/os.html#os.PathLike "(in Python v3.15)")) –
    
*   **use\_pytest\_benchmark** ([_bool_](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) –

---

**Navigation**

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