Skip to content

Multi-Core Addition

Document status

Owner: CL Team · Baseline: Legato 0.14.1+public, PyTorch 2.10.0+cpu, Bertha EVT0 · Last verified: 2026-10-19

Split BF16 [32, 256] inputs by row and add them across 32 cores. Core i processes row i and stores it in a separate [1, 256] output. Save the complete executable in the SDK environment and run it:

1
HA_TOOLCHAIN=1 python3 multi_core_add.py

Load inputs for each core

1
2
3
4
5
6
7
with device.get_top():
    def load_row(i):
        region = (legato.range(i, i + 1), legato.range(0, 256))
        legato.tensor.request_load(legato.tensor.view(a, region), device.get_core(i), "sram")
        legato.tensor.request_load(legato.tensor.view(b, region), device.get_core(i), "sram")

    legato.static.for_each(legato.range(0, 32), load_row)

static.for_each generates the load requests for all 32 cores at compile time. For each core, it requests one row of a followed by one row of b.

Common computation and output passing

1
2
3
4
5
with device.get_all_cores():
    tile_type = legato.types.tensor(legato.types.float("bfloat16"), [1, 256])
    x = legato.tensor.receive_type(tile_type, 0, None, "sram")
    y = legato.tensor.receive_type(tile_type, 1, None, "sram")
    legato.tensor.memcpy(out, x + y)

The kernel's param_kinds declares inputs a and b as top, and output out as core. Outputs are allocated separately for each core. Passing legato.Args(outputs, bertha.all_cores) connects tensor i in the list to the out argument of core i. The dtype and shape of all 32 tensors must match the output type declaration.

1
2
3
4
5
6
with legato.session(output_type=legato.OutputType.BINARY, output_path="multi_core_add"):
    multi_core_add(
        legato.Arg(a, bertha.top),
        legato.Arg(b, bertha.top),
        legato.Args(outputs, bertha.all_cores),
    )

After execution, retrieve all core outputs to the CPU and concatenate them in order to compare the entire [32, 256] result. Each core uses different inputs, so the comparison also checks for incorrect row assignment. On success, the program prints PASS: multi_core_add [32, 256], BF16, 32 cores.