# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""fastdeploy gpu ops"""

import sys

from fastdeploy.import_ops import import_custom_ops

PACKAGE = "fastdeploy.model_executor.ops.gpu"


def decide_module():
    import os

    import paddle

    # Use paddle.device.get_device_properties() instead of paddle.device.cuda.get_device_properties()
    # to support all hardware platforms (NVIDIA, ILUVATAR, HPU, etc.)
    prop = paddle.device.get_device_properties()
    sm_version = prop.major * 10 + prop.minor
    print(f"current sm_version={sm_version}")

    curdir = os.path.dirname(os.path.abspath(__file__))
    sm_version_path = os.path.join(curdir, f"fastdeploy_ops_{sm_version}")
    if os.path.exists(sm_version_path):
        return f".fastdeploy_ops_{sm_version}.fastdeploy_ops"
    return ".fastdeploy_ops"


module_path = ".fastdeploy_ops"
try:
    module_path = decide_module()
except Exception as e:
    print(f"decide_module error, load custom_ops from .fastdeploy_ops: {e}")
    pass
import_custom_ops(PACKAGE, module_path, globals())


def tolerant_import_error():
    class NoneModule:
        def __getattr__(self, name):
            return None

    sys.modules[__name__] = NoneModule()
