Vector Addition: Running Your First Kernel¶
Document status
Owner: CL Team · Baseline: Legato 0.14.1+public, PyTorch 2.10.0+cpu, Bertha EVT0 · Last verified: 2026-10-19
Add two BF16 [1, 256] inputs on core 0 and store the result with the same shape.
Save the complete executable in the SDK environment and run it:
1 2 | |
Run in an HW runtime environment connected to a Bertha card. On success, the program prints PASS: vector_add [1, 256], BF16, core 0.
Complete code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
Steps¶
get_bertha: configure the target device. This example uses the 32-core Bertha EVT0 configuration.@legato.compile: connect the kernel function to the backend. Specify dtype, shape, and memory space in the argument types.- Request input loads in
get_top(), then receive, compute, and store the output inget_core(0). - Copy CPU inputs to LPU memory and call the kernel inside
legato.session(...). - Retrieve the result with the synchronous
out.cpu()copy and compare it with the CPU result.
| Item | Configuration in this example |
|---|---|
| Device memory | Allocate with dram_word_interleaved, corresponding to shared_dram in the type |
| Argument declaration and passing | Default universal when param_kinds is omitted; pass tensors directly |
receive_type indices |
Order of SRAM inputs requested for the core: 0 for a, 1 for b |
request_load transfers data from DRAM to SRAM; memcpy stores the result in the output.
Keep input and output tensors alive until readback. Inputs in this example are exactly representable in binary, so the comparison uses zero tolerance.
Compile without launching¶
Set launch=False in the session to generate a binary only. This does not verify LPU execution or numerical correctness.
If changing only this option in the complete example, the result comparison still runs. Omit the readback and comparison when checking compilation alone.
1 2 | |
Troubleshooting¶
| Symptom | Check first |
|---|---|
| Call outside a session | Move the kernel call inside with legato.session(...): |
| Argument placement error | Correspondence between param_kinds and the top or core locations in Arg |
| Compilation or memory layout error | Dtype, shape, and allocation kind match between type declarations and tensors |
| Incorrect result | Input receive order, output storage, and CPU reference |
See Compiler Session for output paths and intermediate IR.