|
| 1 | +"""Setup a host platform that takes into account current GPU hardware""" |
| 2 | + |
| 3 | +def _verbose_log(rctx, msg): |
| 4 | + if rctx.getenv("MOJO_VERBOSE_GPU_DETECT"): |
| 5 | + # buildifier: disable=print |
| 6 | + print(msg) |
| 7 | + |
| 8 | +def _log_result(rctx, binary, result): |
| 9 | + _verbose_log( |
| 10 | + rctx, |
| 11 | + "\n------ {}:\nexit status: {}\nstdout: {}\nstderr: {}\n------ end gpu-query info" |
| 12 | + .format(binary, result.return_code, result.stdout, result.stderr), |
| 13 | + ) |
| 14 | + |
| 15 | +def _get_amdgpu_constraint(series, gpu_mapping): |
| 16 | + for gpu_name, constraint in gpu_mapping.items(): |
| 17 | + if gpu_name in series: |
| 18 | + if constraint: |
| 19 | + return "@mojo_gpu_toolchains//:{}_gpu".format(constraint) |
| 20 | + else: |
| 21 | + return None |
| 22 | + |
| 23 | + fail("Unrecognized amd-smi/rocm-smi output, please add it to your gpu_mapping in the MODULE.bazel file: {}".format(series)) |
| 24 | + |
| 25 | +def _get_rocm_constraint(blob, gpu_mapping): |
| 26 | + for value in blob.values(): |
| 27 | + series = value["Card Series"] |
| 28 | + return _get_amdgpu_constraint(series, gpu_mapping) |
| 29 | + fail("Unrecognized rocm-smi output, please report: {}".format(blob)) |
| 30 | + |
| 31 | +def _get_amd_constraint(blob, gpu_mapping): |
| 32 | + for value in blob: |
| 33 | + series = value["asic"]["market_name"] |
| 34 | + return _get_amdgpu_constraint(series, gpu_mapping) |
| 35 | + fail("Unrecognized amd-smi output, please report: {}".format(blob)) |
| 36 | + |
| 37 | +def _get_nvidia_constraint(lines, gpu_mapping): |
| 38 | + line = lines[0] |
| 39 | + for gpu_name, constraint in gpu_mapping.items(): |
| 40 | + if gpu_name in line: |
| 41 | + if constraint: |
| 42 | + return "@mojo_gpu_toolchains//:{}_gpu".format(constraint) |
| 43 | + else: |
| 44 | + return None |
| 45 | + |
| 46 | + fail("Unrecognized nvidia-smi output, please add it to your gpu_mapping in the MODULE.bazel file: {}".format(lines)) |
| 47 | + |
| 48 | +def _impl(rctx): |
| 49 | + constraints = [] |
| 50 | + |
| 51 | + if rctx.os.name == "linux" and rctx.os.arch == "amd64": |
| 52 | + # A system may have both rocm-smi and nvidia-smi installed, check both. |
| 53 | + nvidia_smi = rctx.which("nvidia-smi") |
| 54 | + |
| 55 | + # amd-smi supersedes rocm-smi |
| 56 | + amd_smi = rctx.which("amd-smi") |
| 57 | + rocm_smi = rctx.which("rocm-smi") |
| 58 | + |
| 59 | + _verbose_log(rctx, "nvidia-smi path: {}, rocm-smi path: {}, amd-smi path: {}".format(nvidia_smi, rocm_smi, amd_smi)) |
| 60 | + |
| 61 | + # NVIDIA |
| 62 | + if nvidia_smi: |
| 63 | + result = rctx.execute([nvidia_smi, "--query-gpu=gpu_name", "--format=csv,noheader"]) |
| 64 | + _log_result(rctx, nvidia_smi, result) |
| 65 | + if result.return_code == 0: |
| 66 | + lines = result.stdout.splitlines() |
| 67 | + if len(lines) == 0: |
| 68 | + fail("nvidia-smi succeeded but had no GPUs, please report this issue") |
| 69 | + |
| 70 | + constraint = _get_nvidia_constraint(lines, rctx.attr.gpu_mapping) |
| 71 | + if constraint: |
| 72 | + constraints.extend([ |
| 73 | + "@mojo_gpu_toolchains//:nvidia_gpu", |
| 74 | + "@mojo_gpu_toolchains//:has_gpu", |
| 75 | + constraint, |
| 76 | + ]) |
| 77 | + |
| 78 | + if len(lines) > 1: |
| 79 | + constraints.append("@mojo_gpu_toolchains//:has_multi_gpu") |
| 80 | + if len(lines) >= 4: |
| 81 | + constraints.append("@mojo_gpu_toolchains//:has_4_gpus") |
| 82 | + |
| 83 | + # AMD |
| 84 | + if amd_smi: |
| 85 | + result = rctx.execute([amd_smi, "static", "--json"]) |
| 86 | + _log_result(rctx, amd_smi, result) |
| 87 | + |
| 88 | + if result.return_code == 0: |
| 89 | + constraints.extend([ |
| 90 | + "@mojo_gpu_toolchains//:amd_gpu", |
| 91 | + "@mojo_gpu_toolchains//:has_gpu", |
| 92 | + ]) |
| 93 | + |
| 94 | + blob = json.decode(result.stdout) |
| 95 | + if len(blob) == 0: |
| 96 | + fail("amd-smi succeeded but didn't actually have any GPUs, please report this issue") |
| 97 | + |
| 98 | + constraints.append(_get_amd_constraint(blob, rctx.attr.gpu_mapping)) |
| 99 | + if len(blob) > 1: |
| 100 | + constraints.append("@mojo_gpu_toolchains//:has_multi_gpu") |
| 101 | + if len(blob) >= 4: |
| 102 | + constraints.append("@mojo_gpu_toolchains//:has_4_gpus") |
| 103 | + |
| 104 | + elif rocm_smi: |
| 105 | + result = rctx.execute([rocm_smi, "--json", "--showproductname"]) |
| 106 | + _log_result(rctx, rocm_smi, result) |
| 107 | + |
| 108 | + if result.return_code == 0: |
| 109 | + constraints.extend([ |
| 110 | + "@mojo_gpu_toolchains//:amd_gpu", |
| 111 | + "@mojo_gpu_toolchains//:has_gpu", |
| 112 | + ]) |
| 113 | + |
| 114 | + blob = json.decode(result.stdout) |
| 115 | + if len(blob.keys()) == 0: |
| 116 | + fail("rocm-smi succeeded but didn't actually have any GPUs, please report this issue") |
| 117 | + |
| 118 | + constraints.append(_get_rocm_constraint(blob, rctx.attr.gpu_mapping)) |
| 119 | + if len(blob.keys()) > 1: |
| 120 | + constraints.append("@mojo_gpu_toolchains//:has_multi_gpu") |
| 121 | + if len(blob.keys()) >= 4: |
| 122 | + constraints.append("@mojo_gpu_toolchains//:has_4_gpus") |
| 123 | + |
| 124 | + rctx.file("WORKSPACE.bazel", "workspace(name = {})".format(rctx.attr.name)) |
| 125 | + rctx.file("BUILD.bazel", """ |
| 126 | +platform( |
| 127 | + name = "mojo_host_platform", |
| 128 | + parents = ["@platforms//host"], |
| 129 | + visibility = ["//visibility:public"], |
| 130 | + constraint_values = [{constraints}], |
| 131 | + exec_properties = {{ |
| 132 | + "no-remote-exec": "1", |
| 133 | + }}, |
| 134 | +) |
| 135 | +""".format(constraints = ", ".join(['"{}"'.format(x) for x in constraints]))) |
| 136 | + |
| 137 | +mojo_host_platform = repository_rule( |
| 138 | + implementation = _impl, |
| 139 | + configure = True, |
| 140 | + environ = [ |
| 141 | + "MOJO_VERBOSE_GPU_DETECT", |
| 142 | + ], |
| 143 | + attrs = { |
| 144 | + "gpu_mapping": attr.string_dict( |
| 145 | + doc = "A dictionary of GPU strings from nvidia-smi or amd-smi, mapped to supported GPUs defined by mojo.gpu_toolchains()", |
| 146 | + ), |
| 147 | + }, |
| 148 | +) |
0 commit comments