"""Dynamic allocations and partial SRAM copies with externally visible outputs."""

import os
from pathlib import Path

import torch

import legato
import legato.model.bertha as bertha
from legato.language import arith

# Store compiler artifacts and optional simulator logs in a configurable directory.
OUTPUT = Path(os.environ.get("LEGATO_EXAMPLE_OUTPUT", "legato_example_output"))


@legato.compile(backend=lambda ctx: bertha.Bertha(ctx, "bertha"), param_kinds={"input": "universal", "out": "core"})
def dynamic_to_sram(
    input: legato.get_model().tensor_type(legato.types.float("bfloat16"), [16, 256], "shared_dram"),
    out: legato.get_model().tensor_type(legato.types.float("bfloat16"), [16, 256], "shared_dram"),
    count: legato.types.integer("int64"),
):
    """Use a runtime dimension for allocation and copy the result back to DRAM."""
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(input, device.get_core(0), "sram")
    with device.get_core(0):
        loaded = legato.tensor.receive(input, 0, "sram")
        rows = legato.range(0, arith.cast(count, legato.types.index()))
        loaded_view = legato.tensor.view(loaded, (rows, legato.range(0, 256)))
        dim0 = legato.tensor.get_dimension(loaded_view, 0)
        tmp = legato.tensor.allocate(legato.types.tensor(legato.types.float("bfloat16"), ((-1, 16), 256)), (dim0,))
        legato.tensor.memcpy(tmp, loaded_view)
        out_view = legato.tensor.view(out, (rows, legato.range(0, 256)))
        legato.tensor.memcpy(out_view, tmp)


@legato.compile(backend=lambda ctx: bertha.Bertha(ctx, "bertha"))
def copy_view(
    src: legato.get_model().tensor_type(legato.types.float("float32"), [16, 32], "shared_dram"),
    dst: legato.get_model().tensor_type(legato.types.float("float32"), [16, 32], "shared_dram"),
):
    """Copy eight full rows within SRAM, then store the modified destination."""
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(src, device.get_core(0), "sram")
        legato.tensor.request_load(dst, device.get_core(0), "sram")
    with device.get_core(0):
        loaded_src = legato.tensor.receive(src, 0, "sram")
        loaded_dst = legato.tensor.receive(dst, 0, "sram")
        src_view = legato.tensor.view(loaded_src, (legato.range(4, 12), legato.range(0, 32)))
        dst_view = legato.tensor.view(loaded_dst, (legato.range(0, 8), legato.range(0, 32)))
        legato.tensor.memcpy(dst_view, src_view)
        legato.tensor.memcpy(dst, loaded_dst)


def verify_dynamic_to_sram():
    """Compile the runtime-sized allocation without launching it."""
    cpu = torch.empty(16, 256, dtype=torch.bfloat16)
    # Compile-only: runtime-sized allocate + memcpy can block in current EVT0 Torch FSim.
    # Compile a binary without initializing a device or launching.
    with legato.session(output_type=legato.OutputType.BINARY, output_path=str(OUTPUT), launch=False):
        dynamic_to_sram(cpu, legato.Arg(cpu, bertha.core(0)), 10)


def verify_copy_view():
    """Check copied rows and every untouched destination element."""
    # Register the LPU PyTorch backend before allocating device tensors.
    import hart
    import torch_ha

    torch_ha._autoload()
    # HA_BACKEND=sim selects EVT0 Torch FSim; otherwise use the hardware runtime.
    if os.environ.get("HA_BACKEND") == "sim":
        OUTPUT.mkdir(parents=True, exist_ok=True)
        hart.set_fsim_env(
            hart.HaDeviceVersion.BERTHA_CLOUD_EVT0,
            hart.HaFSimComputeMode.TORCH,
            str(OUTPUT / "fsim.log"),
            "error",
        )
    hart.set_device(0)
    # Keep CPU inputs reproducible without initializing a device RNG.
    torch.random.default_generator.manual_seed(0)
    src_cpu = torch.randn(16, 32)
    dst_cpu = torch.randn(16, 32)
    # Allocate the physical layout expected by the kernel and copy contiguous host data.
    src = torch.ops.lpu.empty_with_kind(
        list(src_cpu.shape), "dram_word_interleaved", dtype=src_cpu.dtype, device="lpu:0"
    )
    src.copy_(src_cpu.contiguous())
    # Allocate the physical layout expected by the kernel and copy contiguous host data.
    dst = torch.ops.lpu.empty_with_kind(
        list(dst_cpu.shape), "dram_word_interleaved", dtype=dst_cpu.dtype, device="lpu:0"
    )
    dst.copy_(dst_cpu.contiguous())
    # Compile a binary and launch it on the selected device.
    with legato.session(output_type=legato.OutputType.BINARY, output_path=str(OUTPUT), launch=True):
        copy_view(src, dst)
    # Surface asynchronous execution errors before reading the result.
    hart.stream_synchronize()
    expected = dst_cpu.clone()
    expected[:8] = src_cpu[4:12]
    # Read back the synchronized result and compare every element with the CPU reference.
    actual = dst.cpu().float()
    assert torch.isfinite(actual).all(), "Non-finite device result"
    torch.testing.assert_close(actual, expected.float(), atol=0, rtol=0)


def main():
    """Compile the dynamic pattern; execute and verify the partial-copy destination."""
    verify_dynamic_to_sram()
    verify_copy_view()


if __name__ == "__main__":
    main()
