> For the complete documentation index, see [llms.txt](https://docs.tensorleap.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_simulation.md).

# @tensorleap\_simulation

code\_loader.inner\_leap\_binder.leapbinder\_decorators.tensorleap\_simulation

The `tensorleap_simulation` decorator registers a **simulation function** that the platform calls during an automatic [**Synthetic Data Pipeline**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/synthetic-data-pipeline.md) run. The function generates `N` samples from a set of simulation parameters and returns them as a [`PreprocessResponse`](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/preprocessresponse.md).

```python
@tensorleap_simulation(name='my_sim', sim_params={...})
def my_sim(<one argument per key in sim_params>, N: int, seed: int) -> PreprocessResponse:
    pass
```

<table><thead><tr><th width="158">Args</th><th></th></tr></thead><tbody><tr><td><code>name</code></td><td>The name of the simulation, shown in the UI when selecting simulations for a run. Must be unique within the project.</td></tr><tr><td><code>sim_params</code></td><td><p>A dictionary describing the parameter space the platform searches. Each key is a parameter name and each value has a <code>type</code> and <code>bounds</code>:</p><ul><li><code>"float"</code> with <code>{"min": ..., "max": ...}</code></li><li><code>"int"</code> with <code>{"min": ..., "max": ...}</code></li><li><code>"str"</code> (or <code>"string"</code>) with <code>{"values": [...]}</code></li></ul><p>At least one parameter is required. <code>min</code> must be smaller than <code>max</code>, and <code>values</code> must not be empty.</p></td></tr></tbody></table>

#### Simulation Function inputs:

One keyword argument per key in `sim_params`, holding the value chosen by the optimizer, plus `N` (the number of samples to generate, chosen by the platform on every call, no default) and `seed` (an integer that must make the output reproducible). The function must declare exactly these arguments and is always called with keyword arguments.

#### Simulation Function outputs:

A flat `PreprocessResponse` with at least one sample. Its `state` is set to `DataStateType.additional` by the platform. Every returned sample id is passed, together with the response, to the project's input encoders. The check calls each input encoder twice on a simulated sample and fails if the two results differ, so input encoders must be deterministic.

### Examples

#### Basic Usage

```python
import numpy as np
from code_loader.contract.datasetclasses import PreprocessResponse
from code_loader.contract.enums import DataStateType
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_simulation

SIM_PARAMS = {
    "brightness": {"type": "float", "bounds": {"min": 0.2, "max": 1.0}},
    "n_objects":  {"type": "int",   "bounds": {"min": 1,   "max": 6}},
    "weather":    {"type": "str",   "bounds": {"values": ["clear", "fog", "rain"]}},
}

@tensorleap_simulation(name="warehouse_sim", sim_params=SIM_PARAMS)
def warehouse_sim(brightness, n_objects, weather, N, seed) -> PreprocessResponse:
    rng = np.random.default_rng(seed)
    frames = [render_frame(rng, brightness, n_objects, weather) for _ in range(N)]
    return PreprocessResponse(
        data={i: frame for i, frame in enumerate(frames)},
        sample_ids=list(range(len(frames))),
        sample_id_type=int,
        state=DataStateType.additional,
    )
```

#### Calling it from the integration test

```python
@tensorleap_integration_test()
def integration_test():
    ...
    sim = warehouse_sim(brightness=0.6, n_objects=3, weather="fog", N=2, seed=0)
    for idx in sim.sample_ids:
        image_encoder(idx, sim)
```

{% hint style="info" %}
Use positional integer sample ids, `list(range(N))`, and key `data` by position. These ids are local to one call; the platform assigns every stored synthetic sample its own unique string id.
{% endhint %}

The full workflow, from writing the function to reading the results, is described in [**Synthetic Data Pipeline**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/synthetic-data-pipeline.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_simulation.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
