#!/usr/bin/env python3
# coding: utf-8
# Copyright (c) 2025-2026 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------
"""
"""
import os
from contextlib import contextmanager
from enum import IntEnum
from typing import List, overload

import pypto
import torch

from . import pypto_impl
from .converter import _gen_pto_tensor, from_torch
from ._utils import BuildOnlineManager

__all__ = [
    "_device_init",
    "_device_fini",
    "_device_run_once_data_from_host",
    "_device_synchronize",
    "jit",
    "verify",
    "set_verify_data",
]

_device_init = pypto_impl.DeviceInit
_device_fini = pypto_impl.DeviceFini


class RunMode(IntEnum):
    NPU = 0
    SIM = 1


class _CachedVerifyData:

    def __init__(self):
        self._data = []
        self._ori_data = []

    def reset(self):
        self._data = []
        self._ori_data = []

    def set_data(self, goldens, ori_goldens):
        self._data = goldens
        self._ori_data = ori_goldens

    def get_data(self):
        return self._data


_pto_verify_datas = _CachedVerifyData()


def _current_stream():
    npu = getattr(torch, 'npu', None)
    if npu:
        return npu.current_stream().npu_stream
    else:
        return 0


def _pto_to_tensor_data(tensors: List[pypto.Tensor]) -> List[pypto_impl.DeviceTensorData]:
    datas = []
    for t in tensors:
        if t.ori_shape is None:
            raise RuntimeError("The ori_shape of the tensor is not specified.")
        data = pypto_impl.DeviceTensorData(
            t.dtype,
            t.data_ptr,
            list(t.ori_shape),
        )
        datas.append(data)
    return datas


def _device_run_once_data_from_host(*args):
    for i, t in enumerate(args):
        if not isinstance(t, pypto.Tensor):
            raise TypeError(f"Expected a pypto.Tensor at inputs[{i}], but got {type(t).__name__}.")
    pypto_impl.DeviceRunOnceDataFromHost(
        _pto_to_tensor_data(args), [])


class _JIT:
    def __init__(self, dyn_func, codegen_options=None, host_options=None,
                 pass_options=None, runtime_options=None, verify_options=None,
                 debug_options=None, infer_controlflow_shape=None):
        self.dyn_func = dyn_func
        self._codegen_options = codegen_options
        self._host_options = host_options
        self._pass_options = pass_options
        self._runtime_options = runtime_options or {}
        self._verify_options = verify_options or {}
        self._debug_options = debug_options
        self._infer_controlflow_shape = infer_controlflow_shape
        self._run_mode = self.init_run_mode()
        self.kwargs = None

        # if infer cache shape supported, also use full cache mode
        if self._infer_controlflow_shape:
            # set to max cfgcache size 100000000
            self._runtime_options['stitch_cfgcache_size'] = 100000000

        self.kmodule = pypto_impl.KernelModule(self)

    def __call__(self, *args, **kwargs):
        if len(args) < 1:
            raise ValueError("at least one tensor is required")
        self.kwargs = kwargs
        if self._run_mode == RunMode.NPU:
            pypto_impl.LaunchKernel(self, _current_stream(), *args)
        else:
            self.run_cpu(*args)

    @staticmethod
    def verify_end():
        _pto_verify_datas.reset()

    @staticmethod
    def alloc(size):
        return torch.empty(size, dtype=torch.int8, device='npu').data_ptr()

    def verify_begin(self, tensors):
        if isinstance(self._verify_options, dict) and self._verify_options.get("enable_pass_verify"):
            # Compile and load calculator
            mgr = BuildOnlineManager()
            mgr.build_and_load_calculator()

            host_pto_tensors, _ = _gen_pto_tensor(tensors)
            host_pto_t_datas = _pto_to_tensor_data(host_pto_tensors)
            for i, dev_tensor in enumerate(_pto_to_tensor_data(tensors)):
                pypto_impl.CopyToHost(dev_tensor, host_pto_t_datas[i])
            pypto_impl.SetVerifyData(
                host_pto_t_datas, [], _pto_verify_datas.get_data())

    def compile(self, args):
        tensors = [item for item in args if isinstance(item, pypto.Tensor)]
        self.verify_begin(tensors)

        with pypto.options("jit_scope"):
            self._set_config_option()
            # flowverify begin
            self.verify_begin(tensors)

            with pypto.function(self.dyn_func.__name__, *tensors) as rlf:
                for _ in rlf:
                    self.dyn_func(*args, **self.kwargs)
                del rlf

        # flowverify end
        self.verify_end()

    def init_run_mode(self):
        is_cann_enable = bool(os.environ.get("ASCEND_HOME_PATH"))
        if "run_mode" in self._runtime_options:
            run_mode = RunMode(self._runtime_options["run_mode"])
        else:
            run_mode = RunMode.NPU if is_cann_enable else RunMode.SIM
        if run_mode == RunMode.NPU and not is_cann_enable:
            raise RuntimeError(
                "Please source cann environment while run mode is NPU.")
        self._runtime_options["run_mode"] = int(run_mode)
        return RunMode(run_mode)

    def run_cpu(self, *args):
        # call cost_model interface
        from .cost_model import _cost_model_run_once_data_from_host
        tensors = [item for item in args if isinstance(item, pypto.Tensor)]
        with pypto.options("jit_scope"):
            self._set_config_option()
            pypto_impl.DeviceInit()
            self.compile(args)
            _cost_model_run_once_data_from_host(tensors, [])

    def _set_config_option(self):
        if isinstance(self._codegen_options, dict):
            pypto.set_codegen_options(**self._codegen_options)

        if isinstance(self._host_options, dict):
            pypto.set_host_options(**self._host_options)

        if isinstance(self._pass_options, dict):
            pypto.set_pass_options(**self._pass_options)

        if isinstance(self._runtime_options, dict):
            options_dict = {k: v for k, v in self._runtime_options.items() if v is not None}
            pypto.set_options(runtime_options=options_dict)

        if isinstance(self._verify_options, dict):
            pypto.set_verify_options(**self._verify_options)

        if isinstance(self._debug_options, dict):
            pypto.set_debug_options(**self._debug_options)


@overload
def jit(dyn_func=None):
    ...


@overload
def jit(
        *,
        codegen_options=None,
        host_options=None,
        pass_options=None,
        runtime_options=None,
        verify_options=None,
        debug_options=None,
        infer_controlflow_shape=None
):
    ...


def jit(dyn_func=None,
        *,
        codegen_options=None,
        host_options=None,
        pass_options=None,
        runtime_options=None,
        verify_options=None,
        debug_options=None,
        infer_controlflow_shape=None):

    def decorator(func):
        return _JIT(func,
                    codegen_options=codegen_options,
                    host_options=host_options,
                    pass_options=pass_options,
                    runtime_options=runtime_options,
                    verify_options=verify_options,
                    debug_options=debug_options,
                    infer_controlflow_shape=infer_controlflow_shape)

    if dyn_func is not None:
        return _JIT(dyn_func)
    else:
        return decorator


def _device_synchronize():
    pypto_impl.OperatorDeviceSynchronize(_current_stream())


def verify(func, inputs, outputs, goldens, *args,
           codegen_options=None,
           host_options=None,
           pass_options=None,
           verify_options=None, **kwargs):
    """
    Verify the tensor graph of the function.

    Args:
        func: The function to verify.
        inputs: The input tensors.
        outputs: The output tensors.
        goldens: The golden tensors.
        *args: The extra arguments for func.
        verify_options: dict
            see :func:`set_verify_options`.
        codegen_options: dict
            see :func:`set_codegen_options`.
        host_options: dict
            see :func:`set_host_options`.
        pass_options: dict
            see :func:`set_pass_options`.
        **kwargs: The extra keyword arguments for func.
    Returns:
        None
    """
    pypto_impl.DeviceInit()

    if pass_options is None:
        pass_options = {}
    pypto.set_pass_options(**pass_options)

    if verify_options is None:
        verify_options = {"enable_pass_verify": True}
    pypto.set_verify_options(**verify_options)

    pypto_impl.SetVerifyData(_pto_to_tensor_data(inputs),
                             _pto_to_tensor_data(outputs),
                             _pto_to_tensor_data(goldens))

    inputs = [from_torch(t, f"IN_{idx}") for idx, t in enumerate(inputs)]
    outputs = [from_torch(t, f"OUT_{idx}") for idx, t in enumerate(outputs)]
    handler = pypto_impl.OperatorBegin()
    func(inputs, outputs, *args, **kwargs)
    pypto_impl.OperatorEnd(handler)


def _check_tensor_on_cpu(tensor, kind: str, index: int):
    """Raise if tensor (torch.Tensor or pypto.Tensor) is on device; must be CPU-side."""
    if isinstance(tensor, torch.Tensor):
        if tensor.device.type != 'cpu':
            raise ValueError(
                f"set_verify_golden_data: {kind} must be on CPU, but {kind}[{index}] "
                f"(torch.Tensor) is on device '{tensor.device}'. Please call .cpu() before passing."
            )
        return
    if isinstance(tensor, pypto.Tensor) and tensor.device is not None:
        if getattr(tensor.device, 'type', None) != 'cpu':
            raise ValueError(
                f"set_verify_golden_data: {kind} must be on CPU, but {kind}[{index}] "
                f"(pypto.Tensor) is on device '{tensor.device}'. Please use CPU data, e.g. pypto.from_torch(t.cpu())."
            )


def set_verify_golden_data(in_out_tensors=None, goldens=None):
    from .enum import DT_FP16
    pto_goldens = []
    if goldens:
        for idx, golden in enumerate(goldens):
            if golden is None:
                data = pypto_impl.DeviceTensorData(DT_FP16, 0, [0, 0])
                pto_goldens.append(data)
                continue
            _check_tensor_on_cpu(golden, "goldens", idx)
            if not isinstance(golden, pypto.Tensor):
                t = pypto.from_torch(golden)
            else:
                t = golden

            data = pypto_impl.DeviceTensorData(
                t.dtype,
                t.data_ptr,
                list(t.ori_shape),
            )
            pto_goldens.append(data)
        _pto_verify_datas.set_data(pto_goldens, goldens)

    if in_out_tensors:
        pto_in_out = []
        for idx, t in enumerate(in_out_tensors):
            _check_tensor_on_cpu(t, "in_out_tensors", idx)
            pto_in_out.append(t if isinstance(t, pypto.Tensor)
                              else pypto.from_torch(t))

        pypto_impl.SetVerifyData(_pto_to_tensor_data(pto_in_out),
                                 [], pto_goldens)