Advertisement
Guest User

Untitled

a guest
Oct 26th, 2022
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. import ctypes as ct
  2. import torch
  3.  
  4. from pathlib import Path
  5. from warnings import warn
  6.  
  7.  
  8. class CUDASetup(object):
  9. _instance = None
  10.  
  11. def __init__(self):
  12. raise RuntimeError("Call get_instance() instead")
  13.  
  14. def generate_instructions(self):
  15. if self.cuda is None:
  16. self.add_log_entry('CUDA SETUP: Problem: The main issue seems to be that the main CUDA library was not detected.')
  17. self.add_log_entry('CUDA SETUP: Solution 1): Your paths are probably not up-to-date. You can update them via: sudo ldconfig.')
  18. self.add_log_entry('CUDA SETUP: Solution 2): If you do not have sudo rights, you can do the following:')
  19. self.add_log_entry('CUDA SETUP: Solution 2a): Find the cuda library via: find / -name libcuda.so 2>/dev/null')
  20. self.add_log_entry('CUDA SETUP: Solution 2b): Once the library is found add it to the LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FOUND_PATH_FROM_2a')
  21. self.add_log_entry('CUDA SETUP: Solution 2c): For a permanent solution add the export from 2b into your .bashrc file, located at ~/.bashrc')
  22. return
  23.  
  24. if self.cudart_path is None:
  25. self.add_log_entry('CUDA SETUP: Problem: The main issue seems to be that the main CUDA runtime library was not detected.')
  26. self.add_log_entry('CUDA SETUP: Solution 1: To solve the issue the libcudart.so location needs to be added to the LD_LIBRARY_PATH variable')
  27. self.add_log_entry('CUDA SETUP: Solution 1a): Find the cuda runtime library via: find / -name libcudart.so 2>/dev/null')
  28. self.add_log_entry('CUDA SETUP: Solution 1b): Once the library is found add it to the LD_LIBRARY_PATH: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:FOUND_PATH_FROM_1a')
  29. self.add_log_entry('CUDA SETUP: Solution 1c): For a permanent solution add the export from 1b into your .bashrc file, located at ~/.bashrc')
  30. self.add_log_entry('CUDA SETUP: Solution 2: If no library was found in step 1a) you need to install CUDA.')
  31. self.add_log_entry('CUDA SETUP: Solution 2a): Download CUDA install script: wget https://github.com/TimDettmers/bitsandbytes/blob/main/cuda_install.sh')
  32. self.add_log_entry('CUDA SETUP: Solution 2b): Install desired CUDA version to desired location. The syntax is bash cuda_install.sh CUDA_VERSION PATH_TO_INSTALL_INTO.')
  33. self.add_log_entry('CUDA SETUP: Solution 2b): For example, "bash cuda_install.sh 113 ~/local/" will download CUDA 11.3 and install into the folder ~/local')
  34. return
  35.  
  36. make_cmd = f'CUDA_VERSION={self.cuda_version_string}'
  37. if len(self.cuda_version_string) < 3:
  38. make_cmd += ' make cuda92'
  39. elif self.cuda_version_string == '110':
  40. make_cmd += ' make cuda110'
  41. elif self.cuda_version_string[:2] == '11' and int(self.cuda_version_string[2]) > 0:
  42. make_cmd += ' make cuda11x'
  43.  
  44. has_cublaslt = self.cc in ["7.5", "8.0", "8.6"]
  45. if not has_cublaslt:
  46. make_cmd += '_nomatmul'
  47.  
  48. self.add_log_entry('CUDA SETUP: Something unexpected happened. Please compile from source:')
  49. self.add_log_entry('git clone git@github.com:TimDettmers/bitsandbytes.git')
  50. self.add_log_entry('cd bitsandbytes')
  51. self.add_log_entry(make_cmd)
  52. self.add_log_entry('python setup.py install')
  53.  
  54. def initialize(self):
  55. self.cuda_setup_log = []
  56. self.lib = None
  57.  
  58. from .cuda_setup.main import evaluate_cuda_setup
  59. # binary_name, cudart_path, cuda, cc, cuda_version_string = evaluate_cuda_setup()
  60. binary_name = evaluate_cuda_setup()
  61. # self.cudart_path = cudart_path
  62. # self.cuda = cuda
  63. # self.cc = cc
  64. # self.cuda_version_string = cuda_version_string
  65.  
  66. package_dir = Path(__file__).parent
  67. binary_path = package_dir / binary_name
  68.  
  69. try:
  70. if not binary_path.exists():
  71. self.add_log_entry(f"CUDA SETUP: Required library version not found: {binary_name}. Maybe you need to compile it from source?")
  72. legacy_binary_name = "libbitsandbytes.so"
  73. self.add_log_entry(f"CUDA SETUP: Defaulting to {legacy_binary_name}...")
  74. binary_path = package_dir / legacy_binary_name
  75. if not binary_path.exists():
  76. self.add_log_entry('')
  77. self.add_log_entry('='*48 + 'ERROR' + '='*37)
  78. self.add_log_entry('CUDA SETUP: CUDA detection failed! Possible reasons:')
  79. self.add_log_entry('1. CUDA driver not installed')
  80. self.add_log_entry('2. CUDA not installed')
  81. self.add_log_entry('3. You have multiple conflicting CUDA libraries')
  82. self.add_log_entry('4. Required library not pre-compiled for this bitsandbytes release!')
  83. self.add_log_entry('CUDA SETUP: If you compiled from source, try again with `make CUDA_VERSION=DETECTED_CUDA_VERSION` for example, `make CUDA_VERSION=113`.')
  84. self.add_log_entry('='*80)
  85. self.add_log_entry('')
  86. self.generate_instructions()
  87. self.print_log_stack()
  88. raise Exception('CUDA SETUP: Setup Failed!')
  89. self.lib = ct.cdll.LoadLibrary(str(binary_path))
  90. else:
  91. self.add_log_entry(f"CUDA SETUP: Loading binary {binary_path}...")
  92. self.lib = ct.cdll.LoadLibrary(str(binary_path))
  93. except:
  94. self.print_log_stack()
  95.  
  96. def add_log_entry(self, msg, is_warning=False):
  97. self.cuda_setup_log.append((msg, is_warning))
  98.  
  99. def print_log_stack(self):
  100. for msg, is_warning in self.cuda_setup_log:
  101. if is_warning:
  102. warn(msg)
  103. else:
  104. print(msg)
  105.  
  106. @classmethod
  107. def get_instance(cls):
  108. if cls._instance is None:
  109. cls._instance = cls.__new__(cls)
  110. cls._instance.initialize()
  111. return cls._instance
  112.  
  113.  
  114. lib = CUDASetup.get_instance().lib
  115. try:
  116. if lib is None and torch.cuda.is_available():
  117. CUDASetup.get_instance().generate_instructions()
  118. CUDASetup.get_instance().print_log_stack()
  119. raise RuntimeError('''
  120. CUDA Setup failed despite GPU being available. Inspect the CUDA SETUP outputs to fix your environment!
  121. If you cannot find any issues and suspect a bug, please open an issue with detals about your environment:
  122. https://github.com/TimDettmers/bitsandbytes/issues''')
  123. lib.cadam32bit_g32
  124. lib.get_context.restype = ct.c_void_p
  125. lib.get_cusparse.restype = ct.c_void_p
  126. COMPILED_WITH_CUDA = True
  127. except AttributeError:
  128. warn(
  129. "The installed version of bitsandbytes was compiled without GPU support. "
  130. "8-bit optimizers and GPU quantization are unavailable."
  131. )
  132. COMPILED_WITH_CUDA = False
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement