|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import subprocess |
| 6 | +from packaging import version |
| 7 | +from functools import cmp_to_key |
| 8 | + |
| 9 | + |
| 10 | +WHEEL_TEXT_ROOT_URL = 'https://github.com/hpcaitech/public_assets/tree/main/colossalai/torch_build/torch_wheels' |
| 11 | +RAW_TEXT_FILE_PREFIX = 'https://raw.githubusercontent.com/hpcaitech/public_assets/main/colossalai/torch_build/torch_wheels' |
| 12 | +CUDA_HOME = os.environ['CUDA_HOME'] |
| 13 | + |
| 14 | + |
| 15 | +def parse_args(): |
| 16 | + parser = argparse.ArgumentParser() |
| 17 | + parser.add_argument('--torch_version', type=str) |
| 18 | + return parser.parse_args() |
| 19 | + |
| 20 | +def get_cuda_bare_metal_version(): |
| 21 | + raw_output = subprocess.check_output([CUDA_HOME + "/bin/nvcc", "-V"], universal_newlines=True) |
| 22 | + output = raw_output.split() |
| 23 | + release_idx = output.index("release") + 1 |
| 24 | + release = output[release_idx].split(".") |
| 25 | + bare_metal_major = release[0] |
| 26 | + bare_metal_minor = release[1][0] |
| 27 | + |
| 28 | + return bare_metal_major, bare_metal_minor |
| 29 | + |
| 30 | +def all_wheel_info(): |
| 31 | + page_text = requests.get(WHEEL_TEXT_ROOT_URL).text |
| 32 | + soup = BeautifulSoup(page_text) |
| 33 | + |
| 34 | + all_a_links = soup.find_all('a') |
| 35 | + |
| 36 | + wheel_info = dict() |
| 37 | + |
| 38 | + for a_link in all_a_links: |
| 39 | + if 'cuda' in a_link.text and '.txt' in a_link.text: |
| 40 | + filename = a_link.text |
| 41 | + torch_version, cuda_version = filename.rstrip('.txt').split('-') |
| 42 | + cuda_version = cuda_version.lstrip('cuda') |
| 43 | + if float(cuda_version) < 11.1: |
| 44 | + continue |
| 45 | + if torch_version not in wheel_info: |
| 46 | + wheel_info[torch_version] = dict() |
| 47 | + wheel_info[torch_version][cuda_version] = dict() |
| 48 | + |
| 49 | + file_text = requests.get(f'{RAW_TEXT_FILE_PREFIX}/{filename}').text |
| 50 | + lines = file_text.strip().split('\n') |
| 51 | + |
| 52 | + for line in lines: |
| 53 | + parts = line.split('\t') |
| 54 | + method, url, python_version = parts[:3] |
| 55 | + if float(python_version) < 3.8 or method == "conda": |
| 56 | + continue |
| 57 | + wheel_info[torch_version][cuda_version][python_version] = dict(url=url) |
| 58 | + return wheel_info |
| 59 | + |
| 60 | +def build_colossalai(wheel_info): |
| 61 | + cuda_version_major, cuda_version_minor = get_cuda_bare_metal_version() |
| 62 | + cuda_version_on_host = f'{cuda_version_major}.{cuda_version_minor}' |
| 63 | + |
| 64 | + for torch_version, cuda_versioned_wheel_info in wheel_info.items(): |
| 65 | + for cuda_version, python_versioned_wheel_info in cuda_versioned_wheel_info.items(): |
| 66 | + if cuda_version_on_host == cuda_version: |
| 67 | + for python_version, wheel_info in python_versioned_wheel_info.items(): |
| 68 | + url = wheel_info['url'] |
| 69 | + filename = url.split('/')[-1].replace('%2B', '+') |
| 70 | + cmd = f'bash ./build_fastfold_wheel.sh {url} {filename} {cuda_version} {python_version}' |
| 71 | + os.system(cmd) |
| 72 | + |
| 73 | +def main(): |
| 74 | + args = parse_args() |
| 75 | + wheel_info = all_wheel_info() |
| 76 | + |
| 77 | + # filter wheels on condition |
| 78 | + all_torch_versions = list(wheel_info.keys()) |
| 79 | + def _compare_version(a, b): |
| 80 | + if version.parse(a) > version.parse(b): |
| 81 | + return 1 |
| 82 | + else: |
| 83 | + return -1 |
| 84 | + |
| 85 | + all_torch_versions.sort(key=cmp_to_key(_compare_version)) |
| 86 | + |
| 87 | + if args.torch_version != 'all': |
| 88 | + torch_versions = args.torch_version.split(',') |
| 89 | + # only keep the torch versions specified |
| 90 | + for key in all_torch_versions: |
| 91 | + if key not in torch_versions: |
| 92 | + wheel_info.pop(key) |
| 93 | + |
| 94 | + build_colossalai(wheel_info) |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + main() |
0 commit comments