codingutf8 2022 the huggingface inc team 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 orglicenseslicense2 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 loading of deformable detr s cuda kernels import os from pathlib import path def loadcudakernels from torch utils cppextension import load root pathfile resolve parent parent parent kernels deformabledetr srcfiles root filename for filename in vision cpp os path joincpu msdeformattncpu cpp os path joincuda msdeformattncuda cu load multiscaledeformableattention srcfiles withcudatrue extraincludepathsstrroot extracflagsdwithcuda1 extracudacflags dcudahasfp161 dcudanohalfoperators dcudanohalfconversions dcudanohalf2operators import multiscaledeformableattention as msda return msda coding utf 8 2022 the huggingface inc team 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 loading of deformable detr s cuda kernels
import os
from pathlib import Path


def load_cuda_kernels():
    from torch.utils.cpp_extension import load

    root = Path(__file__).resolve().parent.parent.parent / "kernels" / "deformable_detr"
    src_files = [
        root / filename
        for filename in [
            "vision.cpp",
            os.path.join("cpu", "ms_deform_attn_cpu.cpp"),
            os.path.join("cuda", "ms_deform_attn_cuda.cu"),
        ]
    ]

    load(
        "MultiScaleDeformableAttention",
        src_files,
        with_cuda=True,
        extra_include_paths=[str(root)],
        extra_cflags=["-DWITH_CUDA=1"],
        extra_cuda_cflags=[
            "-DCUDA_HAS_FP16=1",
            "-D__CUDA_NO_HALF_OPERATORS__",
            "-D__CUDA_NO_HALF_CONVERSIONS__",
            "-D__CUDA_NO_HALF2_OPERATORS__",
        ],
    )

    import MultiScaleDeformableAttention as MSDA

    return MSDA