Skip to content

Context Management

Legato operations are emitted under an executor context. A context describes where the following operations run: the top executor, a single core, or all cores.

Getting The Current Context

Use legato.get_context() inside a compiled function to access the active compiler context and its device.

1
device = legato.get_context().get_device()

Most user code immediately selects an executor with a Python with statement.

1
2
3
4
5
6
7
8
device = legato.get_context().get_device()

with device.get_top():
    legato.tensor.request_load(input_data, device.get_core(0), "sram")

with device.get_core(0):
    loaded = legato.tensor.receive(input_data, 0, "sram")
    result = loaded + loaded

Executor Scopes

  • device.get_top() is used for orchestration and top-side load requests.
  • device.get_core(index) runs the region on one core.
  • device.get_all_cores() runs the region for every core.

The object yielded by with ... as name is the executor selected by that context.

1
2
with device.get_core(0) as core0:
    copied = legato.tensor.copy(input_tensor, core0)

Yielding Values Across Regions

Legato scopes are stricter than Python scopes. If a value produced inside a region is needed by a later region, yield it from that region.

1
2
3
4
5
6
7
with device.get_core(0):
    loaded = legato.tensor.receive(input_data, 0, "sram")
    q = loaded @ weight
    yield q

with device.get_core(0):
    legato.tensor.memcpy(output, q)

For conditional regions, both branches must yield compatible values when the outer scope uses those values.

Sessions And Contexts

Calling a @legato.compile function requires an active legato.session.

1
2
with legato.session(output_type=legato.OutputType.MLIR, output_path="test-output"):
    compiled_kernel(*args)

The session controls compiler output and logging. Executor contexts control the IR regions emitted inside the compiled kernel.