"""BF16 matrix multiplication on one Bertha EVT0 core."""

import torch
import torch_ha
import legato
import legato.model.bertha as bertha


def get_bertha(ctx):
    return bertha.Bertha(ctx, "bertha", num_cores=32)


@legato.compile(backend=get_bertha)
def matmul(
    a: legato.get_model().tensor_type(legato.types.float("bfloat16"), [16, 256], "shared_dram"),
    weight: legato.get_model().tensor_type(legato.types.float("bfloat16"), [256, 128], "mpu_dram"),
    out: legato.get_model().tensor_type(legato.types.float("bfloat16"), [16, 128], "shared_dram"),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(a, device.get_core(0), "sram")
        legato.tensor.request_load(weight, device.get_core(0), "mpu_buffer")
    with device.get_core(0):
        x = legato.tensor.receive(a, 0, "sram")
        weight_type = legato.get_model().tensor_type(
            legato.types.float("bfloat16"), [256, 128], "mpu_weight"
        )
        w = legato.tensor.receive_type(weight_type, 0, None, "mpu_buffer")
        legato.tensor.memcpy(out, x @ w)


def main():
    generator = torch.Generator(device="cpu").manual_seed(0)
    a_cpu = torch.randn(16, 256, dtype=torch.bfloat16, generator=generator) / 16
    w_cpu = torch.randn(256, 128, dtype=torch.bfloat16, generator=generator) / 16
    a = torch.ops.lpu.empty_with_kind(
        [16, 256], "dram_word_interleaved", dtype=torch.bfloat16, device="lpu:0"
    )
    weight = torch.ops.lpu.empty_with_kind(
        [256, 128], "mpu_tensor", dtype=torch.bfloat16, device="lpu:0"
    )
    out = torch.ops.lpu.empty_with_kind(
        [16, 128], "dram_word_interleaved", dtype=torch.bfloat16, device="lpu:0"
    )
    a.copy_(a_cpu)
    weight.copy_(w_cpu)
    with legato.session(output_type=legato.OutputType.BINARY, output_path="matmul"):
        matmul(a, weight, out)
    actual = out.cpu()
    expected = (a_cpu.float() @ w_cpu.float()).to(torch.bfloat16)
    torch.testing.assert_close(actual, expected, rtol=0.02, atol=0.002)
    print("PASS: matmul [16, 256] @ [256, 128], BF16, core 0")


if __name__ == "__main__":
    main()
