For the complete documentation index, see llms.txt. This page is also available as Markdown.

@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 run. The function generates N samples from a set of simulation parameters and returns them as a PreprocessResponse.

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

name

The name of the simulation, shown in the UI when selecting simulations for a run. Must be unique within the project.

sim_params

A dictionary describing the parameter space the platform searches. Each key is a parameter name and each value has a type and bounds:

  • "float" with {"min": ..., "max": ...}

  • "int" with {"min": ..., "max": ...}

  • "str" with {"values": [...]}

At least one parameter is required. min must be smaller than max, and values must not be empty.

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.

Examples

Basic Usage

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

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.

The full workflow, from writing the function to reading the results, is described in Synthetic Data Pipeline.

Last updated

Was this helpful?