Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# --------------------------------------------------------------------------
 
import os
import subprocess
import sys
from glob import glob
from shutil import copyfile
 
import torch
from packaging import version
 
from onnxruntime.training import ortmodule
 
 
def _list_extensions(path):
    extensions = []
    for root, _, files in os.walk(path):
        for name in files:
            if name.lower() == "setup.py":
                extensions.append(os.path.join(root, name))
    return extensions
 
 
def _list_cpu_extensions():
    return _list_extensions(os.path.join(ortmodule.ORTMODULE_TORCH_CPP_DIR, "cpu"))
 
 
def _list_cuda_extensions():
    return _list_extensions(os.path.join(ortmodule.ORTMODULE_TORCH_CPP_DIR, "cuda"))
 
 
def _install_extension(ext_name, ext_path, cwd):
    ret_code = subprocess.call((sys.executable, ext_path, "build"), cwd=cwd)
    if ret_code != 0:
        print(f"There was an error compiling '{ext_name}' PyTorch CPP extension")
        sys.exit(ret_code)
 
 
def _get_cuda_extra_build_params():
    nvcc_extra_args = ["-lineinfo", "-O3", "--use_fast_math"]
    cuda_version = torch.version.cuda
    if cuda_version is not None and version.parse(cuda_version) > version.parse("11.2"):
        # If number is 0, the number of threads used is the number of CPUs on the machine.
        nvcc_extra_args += ["--threads", "0"]
 
    os.environ["ONNXRUNTIME_CUDA_NVCC_EXTRA_ARGS"] = ",".join(nvcc_extra_args)
 
 
def build_torch_cpp_extensions():
    """Builds PyTorch CPP extensions and returns metadata."""
    # Run this from within onnxruntime package folder
    is_gpu_available = (torch.version.cuda is not None or torch.version.hip is not None) and (
        ortmodule.ONNXRUNTIME_CUDA_VERSION is not None
    )
 
    # Docker build don't have CUDA support, but Torch C++ extensions with CUDA may be forced
    force_cuda = bool(os.environ.get("ONNXRUNTIME_FORCE_CUDA", None))
 
    os.chdir(ortmodule.ORTMODULE_TORCH_CPP_DIR)
 
    # Extensions might leverage CUDA versions internally
    os.environ["ONNXRUNTIME_CUDA_VERSION"] = (
        ortmodule.ONNXRUNTIME_CUDA_VERSION if ortmodule.ONNXRUNTIME_CUDA_VERSION is not None else ""
    )
 
    if torch.version.cuda is not None and ortmodule.ONNXRUNTIME_CUDA_VERSION is not None:
        _get_cuda_extra_build_params()
 
    ############################################################################
    # Pytorch CPP Extensions that DO require CUDA
    ############################################################################
    if is_gpu_available or force_cuda:
        for ext_setup in _list_cuda_extensions():
            _install_extension(ext_setup.split(os.sep)[-2], ext_setup, ortmodule.ORTMODULE_TORCH_CPP_DIR)
 
    ############################################################################
    # Pytorch CPP Extensions that DO NOT require CUDA
    ############################################################################
    for ext_setup in _list_cpu_extensions():
        _install_extension(ext_setup.split(os.sep)[-2], ext_setup, ortmodule.ORTMODULE_TORCH_CPP_DIR)
 
    ############################################################################
    # Install Pytorch CPP Extensions into local onnxruntime package folder
    ############################################################################
    torch_cpp_exts = glob(os.path.join(ortmodule.ORTMODULE_TORCH_CPP_DIR, "build", "lib.*", "*.so"))
    torch_cpp_exts.extend(glob(os.path.join(ortmodule.ORTMODULE_TORCH_CPP_DIR, "build", "lib.*", "*.dll")))
    torch_cpp_exts.extend(glob(os.path.join(ortmodule.ORTMODULE_TORCH_CPP_DIR, "build", "lib.*", "*.dylib")))
    for ext in torch_cpp_exts:
        dest_ext = os.path.join(ortmodule.ORTMODULE_TORCH_CPP_DIR, os.path.basename(ext))
        print(f"Installing {ext} -> {dest_ext}")
        copyfile(ext, dest_ext)
 
    # Tear down
    os.environ.pop("ONNXRUNTIME_CUDA_VERSION")
 
 
if __name__ == "__main__":
    build_torch_cpp_extensions()