load("@fbcode_macros//build_defs:build_file_migration.bzl", "fbcode_target")
load("@fbsource//third-party/llama.cpp:ggml_ci.bzl", "ggml_ci_labels")
load("@fbsource//tools/target_determinator/macros:ci.bzl", "ci")
load("@fbsource//xplat/executorch/backends/native/runtime/engine:targets.bzl", "CPU_COMPAT", "CUDA_COMPAT", "HIP_COMPAT")
load("@fbcode_macros//build_defs/lib:re_test_utils.bzl", "re_test_utils")
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")

oncall("executorch")

fbcode_target(
    _kind = runtime.cxx_test,
    name = "ggml_engine_test",
    srcs = ["test_ggml_engine.cpp"],
    deps = [
        "//executorch/backends/native/runtime:native_graph_schema",
        "//executorch/backends/native/runtime:runtime",
        "//executorch/backends/native/runtime/deserialize:owned_bytes",
        "//executorch/backends/native/runtime/deserialize:package",
        "//executorch/backends/native/runtime/deserialize:package_test_data",
        "//executorch/backends/native/runtime/engine:engine",
        "//executorch/backends/native/runtime/engine:ggml_engine_cpu",
    ],
    **CPU_COMPAT
)

# The op suite runs once per ggml backend set; only the engine variant, the
# runner's --device, link deps and test env, the test host, and the CI
# configuration differ.
_VARIANTS = [
    {"suffix": "", "engine": "ggml_engine_cpu", "compat": CPU_COMPAT, "device": "cpu"},
    {
        "suffix": "_cuda",
        "engine": "ggml_engine_cuda",
        "compat": CUDA_COMPAT,
        "labels": ggml_ci_labels("cuda"),
        "device": "gpu",
        "remote_execution": re_test_utils.remote_execution(
            platform = "gpu-remote-execution",
        ),
        # The CUDA backend is a shared library that needs the ggml core shared
        # beside it, and its VMM pool calls the CUDA driver API.
        "deps": ["fbsource//third-party/llama.cpp:ggml_base[prefer-shared]"],
        "external_deps": [("cuda", None, "cuda_driver_api-lazy")],
        "env": {
            # Buck does not put the CUDA driver stub's library on the loader path.
            # @oss-disable[end= ]: "LD_PRELOAD": "$(location //tools/lazify:lazy_stub_binder[shared])",
        },
        # ggml-CUDA computes f16 BLAS GEMMs in f16 and f32 ones in TF32 by default.
        "sample_env": {
            "GGML_CUDA_CUBLAS_COMPUTE_TYPE": "f32",
            "NVIDIA_TF32_OVERRIDE": "0",
        },
    },
    {
        "suffix": "_hip",
        "engine": "ggml_engine_hip",
        "compat": HIP_COMPAT,
        "labels": ggml_ci_labels("hip"),
        "device": "gpu",
        "remote_execution": re_test_utils.remote_execution(
            platform = "amd-gpu",
            subplatform = "MI300",
        ),
    },
]

[
    fbcode_target(
        _kind = runtime.cxx_library,
        name = "ggml_engine_host" + v["suffix"],
        srcs = ["ggml_ptn_runner.cpp"],
        link_whole = True,
        visibility = ["//executorch/backends/native/test/..."],
        exported_deps = ["//executorch/backends/native/test/runtime:engine_host"],
        deps = ["//executorch/backends/native/runtime/engine:" + v["engine"]] +
               v.get("deps", []),
        external_deps = ["gflags"] + v.get("external_deps", []),
        labels = v.get("labels", []),
        **v.get("compat", {})
    )
    for v in _VARIANTS
]

[
    fbcode_target(
        _kind = runtime.cxx_binary,
        name = "ggml_ptn_runner" + v["suffix"],
        visibility = ["//executorch/backends/native/test/..."],
        deps = [
            ":ggml_engine_host" + v["suffix"],
            "//executorch/backends/native/test/runtime:ptn_runner_main",
        ],
        labels = v.get("labels", []),
        **v.get("compat", {})
    )
    for v in _VARIANTS
]

# ggml_sample_test_act_quant lets ggml quantize the activations of weight-only
# quantized linears, judged against golden-int8_act.
[
    fbcode_target(
        _kind = runtime.cxx_test,
        name = "ggml_sample_test" + name + v["suffix"],
        args = [
            "--packages=$(location //executorch/backends/native/test/samples:op_samples)",
            "--device=" + v["device"],
        ] + flags,
        env = v.get("env", {}) | v.get("sample_env", {}),
        remote_execution = v.get("remote_execution"),
        # TODO: run the others in CI once they pass.
        labels = [] if v["suffix"] + name == "_act_quant" else ci.labels(ci.map(ci.skip_test())),
        test_main = "//executorch/backends/native/test/samples:sample_test_main",
        deps = [":ggml_engine_host" + v["suffix"]],
        **v.get("compat", {})
    )
    for v in _VARIANTS
    for name, flags in [
        ("", []),
        ("_act_quant", ["--allow_activation_quant_for_weight_only"]),
    ]
]

[
    fbcode_target(
        _kind = runtime.python_test,
        name = "test_ggml_ops" + v["suffix"],
        srcs = ["test_ggml_ops.py"],
        env = {"GGML_PTN_DEVICE": v["device"]} | v.get("env", {}),
        labels = v.get("labels", []),
        remote_execution = v.get("remote_execution"),
        resources = {":ggml_ptn_runner" + v["suffix"]: "ggml_ptn_runner"},
        deps = [
            "//caffe2:torch",
            "//executorch/exir/native:native",
            "//executorch/extension/llm/export:gguf",
            "//pytorch/ao:torchao",
            "fbsource//third-party/pypi/transformers:transformers",
        ],
        **v.get("compat", {})
    )
    for v in _VARIANTS
]
