import os
import threading

from torch._inductor.kernel.vendored_templates.flydsl.kernels import (
    get_grouped_gemm_persistent_grid_size,
    infer_has_k_tail,
    launch_gemm_gfx950_grouped,
    make_gemm_param_and_validate,
)
from torch._inductor.runtime.flydsl_cache import (
    configure_flydsl_cache_dir,
    run_cached_flydsl,
    temporary_env,
)

{{gen_defines()}}


_grouped_param = None
_grouped_param_lock = threading.Lock()


def _inductor_tensor_arg(tensor):
    return flyc.from_torch_tensor(tensor).mark_layout_dynamic()


def _flydsl_grouped_mm(output, mat1, mat2, offs, stream):
    global _grouped_param
    group_count, k, n = mat2.shape
    total_m = mat1.shape[0]
    if total_m == 0:
        return output

    param = _grouped_param
    if param is None:
        with _grouped_param_lock:
            param = _grouped_param
            if param is None:
                has_k_tail = infer_has_k_tail(GEMM_K, TILE_K, STAGES, USE_HALF_TILE_INTERLEAVED)
                param = make_gemm_param_and_validate(
                    GEMM_M,
                    GEMM_N,
                    GEMM_K,
                    {
                        "dtype_id": GEMM_DTYPE_ID,
                        "tile_m": TILE_M,
                        "tile_n": TILE_N,
                        "tile_k": TILE_K,
                        "stages": STAGES,
                        "m_waves": M_WAVES,
                        "n_waves": N_WAVES,
                        "group_m": GROUP_M,
                        "use_half_tile_interleaved": USE_HALF_TILE_INTERLEAVED,
                        "a_is_transposed": False,
                        "b_is_transposed": False,
                        "has_bias": False,
                        "has_k_tail": has_k_tail,
                    },
                )
                if param is None:
                    raise RuntimeError(
                        f"unsupported FlyDSL grouped GEMM shape/config: M={GEMM_M}, "
                        f"N={GEMM_N}, K={GEMM_K}, "
                        f"tile=({TILE_M}, {TILE_N}, {TILE_K}), stages={STAGES}"
                    )
                _grouped_param = param

    group_count = int(group_count)
    total_m = int(total_m)
    n_int = int(n)
    k_int = int(k)
    flydsl_compile_only = os.environ.get("FLYDSL_COMPILE_ONLY") == "1"
    compile_only = os.environ.get("COMPILE_ONLY", "").lower() in (
        "1", "true", "yes", "on"
    )
    if compile_only != flydsl_compile_only:
        raise RuntimeError(
            "COMPILE_ONLY and FLYDSL_COMPILE_ONLY must only be enabled together "
            "by the FlyDSL precompile path"
        )
    if flydsl_compile_only:
        # Precompilation runs on fake tensors that have no device to query, and
        # the persistent kernel reads its grid from gridDim, so the size the disk
        # cache is warmed with does not have to match the one used at runtime.
        grid_size = 1
    else:
        # grid_size only configures the launch grid; the kernel reads gridDim.x
        # at runtime, so varying it does not require a separate compile cache entry.
        grid_size = get_grouped_gemm_persistent_grid_size(
            param,
            total_m,
            n_int,
            group_count,
            torch.cuda.get_device_properties(mat1.device),
        )
    group_count_arg = fx.Int32(group_count)
    n_arg = fx.Int32(n_int)
    k_arg = fx.Int32(k_int)
    compile_args = (
        _inductor_tensor_arg(output),
        _inductor_tensor_arg(mat1),
        _inductor_tensor_arg(mat2),
        _inductor_tensor_arg(offs),
        group_count_arg,
        n_arg,
        k_arg,
        grid_size,
        param,
        stream,
    )
    configure_flydsl_cache_dir()
    if flydsl_compile_only:
        flyc.compile(launch_gemm_gfx950_grouped, *compile_args)
    else:
        run_cached_flydsl(
            launch_gemm_gfx950_grouped,
            *compile_args,
            constexpr_param=param,
            compiler=flyc.compile,
            dispatch_args=(
                output,
                mat1,
                mat2,
                offs,
                group_count_arg,
                n_arg,
                k_arg,
                grid_size,
                param,
                stream,
            ),
        )
    return output


{{def_kernel("mat1", "mat2", "offs")}}
    output = {{get_output()}}
    return _flydsl_grouped_mm(output, mat1, mat2, offs, stream)


def {{kernel_name}}_precompile(
    precompile_shapes,
    precompile_strides,
    precompile_dtypes,
    device_index=0,
    device_capability=None,
    flydsl_gpu_arch=None,
):
    """Warm FlyDSL's disk cache using metadata-only fake tensors."""
    dtype_mat1 = getattr(torch, precompile_dtypes["mat1"])
    dtype_mat2 = getattr(torch, precompile_dtypes["mat2"])
    dtype_offs = getattr(torch, precompile_dtypes["offs"])
    dtype_output = getattr(torch, precompile_dtypes["output"])

    shape_mat1 = tuple(precompile_shapes["mat1"])
    shape_mat2 = tuple(precompile_shapes["mat2"])
    shape_offs = tuple(precompile_shapes["offs"])
    shape_output = tuple(precompile_shapes["output"])

    stride_mat1 = tuple(precompile_strides["mat1"])
    stride_mat2 = tuple(precompile_strides["mat2"])
    stride_offs = tuple(precompile_strides["offs"])
    stride_output = tuple(precompile_strides["output"])

    env_updates = {"COMPILE_ONLY": "1", "FLYDSL_COMPILE_ONLY": "1"}
    if flydsl_gpu_arch is not None:
        env_updates["FLYDSL_GPU_ARCH"] = flydsl_gpu_arch

    with temporary_env(env_updates):
        from torch._subclasses.fake_tensor import FakeTensorMode

        with FakeTensorMode():

            def _fake_proxy(shape, stride, dtype):
                return torch.empty_strided(shape, stride, dtype=dtype, device="cpu")

            mat1 = _fake_proxy(shape_mat1, stride_mat1, dtype_mat1)
            mat2 = _fake_proxy(shape_mat2, stride_mat2, dtype_mat2)
            offs = _fake_proxy(shape_offs, stride_offs, dtype_offs)
            output = _fake_proxy(shape_output, stride_output, dtype_output)
            {{kernel_name}}_main(mat1, mat2, offs, output, stream=0)
