"""Compile a PyTorch addition for Bertha EVT0 and compare the full output."""

import torch
import torch_ha  # Registers the LPU device and memory-allocation operations.


def add(a, b):
    return a + b


def main():
    shape = [64, 256]
    generator = torch.Generator(device="cpu").manual_seed(0)
    a_cpu = torch.randint(-8, 9, shape, generator=generator).to(torch.bfloat16)
    b_cpu = torch.randint(-8, 9, shape, generator=generator).to(torch.bfloat16)
    a, b = [
        torch.ops.lpu.empty_with_kind(
            shape, "dram_word_interleaved", dtype=torch.bfloat16, device="lpu:0"
        )
        for _ in range(2)
    ]
    a.copy_(a_cpu)
    b.copy_(b_cpu)

    compiled_add = torch.compile(
        add,
        backend="legato",
        dynamic=False,
        options={"legato.device": "bertha"},
    )
    with torch.no_grad():
        result = compiled_add(a, b)

    assert result.device.type == "lpu"
    torch.testing.assert_close(result.cpu(), a_cpu + b_cpu, rtol=0, atol=0)
    print("PASS: torch_legato add [64, 256], BF16")


if __name__ == "__main__":
    main()
