|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from .builder import Builder |
| 6 | + |
| 7 | + |
| 8 | +class CPUAdamBuilder(Builder): |
| 9 | + NAME = "cpu_adam" |
| 10 | + BASE_DIR = "cuda_native" |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + self.name = CPUAdamBuilder.NAME |
| 14 | + super().__init__() |
| 15 | + |
| 16 | + self.sources = [self.colossalai_src_path(path) for path in self.sources_files()] |
| 17 | + self.extra_include_paths = [self.colossalai_src_path(path) for path in self.include_paths()] |
| 18 | + self.extra_cxx_flags = ['-std=c++14', '-lcudart', '-lcublas', '-g', '-Wno-reorder', '-fopenmp', '-march=native'] |
| 19 | + self.extra_cuda_flags = [ |
| 20 | + '-std=c++14', '-U__CUDA_NO_HALF_OPERATORS__', '-U__CUDA_NO_HALF_CONVERSIONS__', |
| 21 | + '-U__CUDA_NO_HALF2_OPERATORS__', '-DTHRUST_IGNORE_CUB_VERSION_CHECK' |
| 22 | + ] |
| 23 | + self.version_dependent_macros = ['-DVERSION_GE_1_1', '-DVERSION_GE_1_3', '-DVERSION_GE_1_5'] |
| 24 | + |
| 25 | + def sources_files(self): |
| 26 | + return [ |
| 27 | + os.path.join(CPUAdamBuilder.BASE_DIR, "csrc/cpu_adam.cpp"), |
| 28 | + ] |
| 29 | + |
| 30 | + def include_paths(self): |
| 31 | + import torch |
| 32 | + from torch.utils.cpp_extension import CUDA_HOME |
| 33 | + cuda_include = os.path.join(CUDA_HOME, "include") |
| 34 | + return [os.path.join(CPUAdamBuilder.BASE_DIR, "includes"), cuda_include] |
| 35 | + |
| 36 | + def colossalai_src_path(self, code_path): |
| 37 | + if os.path.isabs(code_path): |
| 38 | + return code_path |
| 39 | + else: |
| 40 | + return os.path.join(Path(__file__).parent.parent.absolute(), code_path) |
| 41 | + |
| 42 | + def strip_empty_entries(self, args): |
| 43 | + ''' |
| 44 | + Drop any empty strings from the list of compile and link flags |
| 45 | + ''' |
| 46 | + return [x for x in args if len(x) > 0] |
| 47 | + |
| 48 | + def builder(self): |
| 49 | + from torch.utils.cpp_extension import CUDAExtension |
| 50 | + return CUDAExtension( |
| 51 | + name=self.name, |
| 52 | + sources=[os.path.join('colossalai/kernel/cuda_native/csrc', path) for path in self.sources], |
| 53 | + include_dirs=self.extra_include_paths, |
| 54 | + extra_compile_args={ |
| 55 | + 'cxx': ['-O3'] + self.version_dependent_macros + self.extra_cxx_flags, |
| 56 | + 'nvcc': ['-O3', '--use_fast_math'] + self.extra_cuda_flags |
| 57 | + }) |
| 58 | + |
| 59 | + def load(self, verbose=True): |
| 60 | + """ |
| 61 | +
|
| 62 | + load and compile cpu_adam lib at runtime |
| 63 | +
|
| 64 | + Args: |
| 65 | + verbose (bool, optional): show detailed info. Defaults to True. |
| 66 | + """ |
| 67 | + import time |
| 68 | + |
| 69 | + from torch.utils.cpp_extension import load |
| 70 | + start_build = time.time() |
| 71 | + |
| 72 | + op_module = load(name=self.name, |
| 73 | + sources=self.strip_empty_entries(self.sources), |
| 74 | + extra_include_paths=self.strip_empty_entries(self.extra_include_paths), |
| 75 | + extra_cflags=self.extra_cxx_flags, |
| 76 | + extra_cuda_cflags=self.extra_cuda_flags, |
| 77 | + extra_ldflags=[], |
| 78 | + verbose=verbose) |
| 79 | + |
| 80 | + build_duration = time.time() - start_build |
| 81 | + if verbose: |
| 82 | + print(f"Time to load {self.name} op: {build_duration} seconds") |
| 83 | + |
| 84 | + return op_module |
0 commit comments