|
| 1 | +from dataclasses import dataclass |
| 2 | +from logging import info |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | + |
| 8 | +@dataclass(frozen=True) |
| 9 | +class DeviceInfo: |
| 10 | + """ |
| 11 | + Theoretical Numbers from data sheet. If two numbers are given, Tensor/Matrix Core vs not, |
| 12 | + then the higher number is reported. Sparsity is not considered. |
| 13 | +
|
| 14 | +
|
| 15 | + Bandwidth numbers are tricky, because there are platform differences that may not show up in the profiler trace. |
| 16 | + For example, |
| 17 | + """ |
| 18 | + |
| 19 | + tops: dict[torch.dtype, float] |
| 20 | + dram_bw_gbs: float |
| 21 | + dram_gb: float |
| 22 | + |
| 23 | + |
| 24 | +# Indexing is based on `torch.cuda.get_device_name()` |
| 25 | +# TODO investigate profiler support for tf32 and allow device to report correct number when it's turned on. |
| 26 | +_device_mapping: dict[str, DeviceInfo] = { |
| 27 | + # Source: https://resources.nvidia.com/en-us-tensor-core/nvidia-tensor-core-gpu-datasheet |
| 28 | + "NVIDIA H100": DeviceInfo( |
| 29 | + tops={ |
| 30 | + torch.float64: 9.7, |
| 31 | + torch.float32: 19.5, |
| 32 | + torch.bfloat16: 1979.0, |
| 33 | + torch.float16: 1979.0, |
| 34 | + torch.float8_e8m0fnu: 3958.0, |
| 35 | + torch.float8_e8m0fnu: 3958.0, |
| 36 | + torch.float8_e4m3fnuz: 3958.0, |
| 37 | + torch.float8_e5m2: 3958.0, |
| 38 | + torch.float8_e5m2fnuz: 3958.0, |
| 39 | + torch.float8_e8m0fnu: 3958.0, |
| 40 | + torch.int8: 3958.0, |
| 41 | + }, |
| 42 | + dram_bw_gbs=3350, |
| 43 | + dram_gb=80, |
| 44 | + ), |
| 45 | + # Source: https://resources.nvidia.com/en-us-tensor-core/nvidia-tensor-core-gpu-datasheet |
| 46 | + "NVIDIA A100": DeviceInfo( |
| 47 | + tops={ |
| 48 | + torch.float64: 19.5, |
| 49 | + torch.float32: 19.5, |
| 50 | + torch.bfloat16: 312.5, |
| 51 | + torch.float16: 312.5, |
| 52 | + # Not in datasheet: float8 |
| 53 | + torch.int8: 624.0, |
| 54 | + }, |
| 55 | + dram_bw_gbs=2039.0, |
| 56 | + dram_gb=80.0, |
| 57 | + ), |
| 58 | + # Source: https://resources.nvidia.com/en-us-gpu-resources/l4-tensor-datasheet |
| 59 | + "NVIDIA L4": DeviceInfo( |
| 60 | + tops={ |
| 61 | + # This is a guess, not in datasheet |
| 62 | + torch.float64: 15.1, |
| 63 | + torch.float32: 30.3, |
| 64 | + torch.bfloat16: 242.0, |
| 65 | + torch.float16: 242.0, |
| 66 | + torch.float8_e8m0fnu: 485.0, |
| 67 | + torch.float8_e8m0fnu: 485.0, |
| 68 | + torch.float8_e4m3fnuz: 485.0, |
| 69 | + torch.float8_e5m2: 485.0, |
| 70 | + torch.float8_e5m2fnuz: 485.0, |
| 71 | + torch.float8_e8m0fnu: 485.0, |
| 72 | + torch.int8: 485.0, |
| 73 | + }, |
| 74 | + dram_bw_gbs=3350, |
| 75 | + dram_gb=24, |
| 76 | + ), |
| 77 | + # Source: https://www.amd.com/content/dam/amd/en/documents/instinct-tech-docs/data-sheets/amd-instinct-mi300a-data-sheet.pdf |
| 78 | + "AMD MI300A": DeviceInfo( |
| 79 | + tops={ |
| 80 | + torch.float64: 122.6, |
| 81 | + torch.float32: 122.6, |
| 82 | + # torch.tf32: 490.3, |
| 83 | + torch.bfloat16: 980.6, |
| 84 | + torch.float16: 980.6, |
| 85 | + torch.float8_e8m0fnu: 1961.2, |
| 86 | + torch.float8_e8m0fnu: 1961.2, |
| 87 | + torch.float8_e4m3fnuz: 1961.2, |
| 88 | + torch.float8_e5m2: 1961.2, |
| 89 | + torch.float8_e5m2fnuz: 1961.2, |
| 90 | + torch.float8_e8m0fnu: 1961.2, |
| 91 | + torch.int8: 1961.2, |
| 92 | + }, |
| 93 | + dram_bw_gbs=5300.0, |
| 94 | + dram_gb=128.0, |
| 95 | + ), |
| 96 | + # Source: https://www.amd.com/content/dam/amd/en/documents/instinct-tech-docs/data-sheets/amd-instinct-mi300x-data-sheet.pdf |
| 97 | + "AMD MI300X": DeviceInfo( |
| 98 | + tops={ |
| 99 | + torch.float64: 163.4, |
| 100 | + torch.float32: 163.4, |
| 101 | + torch.bfloat16: 1307.4, |
| 102 | + torch.float16: 1307.4, |
| 103 | + torch.float8_e8m0fnu: 2614.9, |
| 104 | + torch.float8_e8m0fnu: 2614.9, |
| 105 | + torch.float8_e4m3fnuz: 2614.9, |
| 106 | + torch.float8_e5m2: 2614.9, |
| 107 | + torch.float8_e5m2fnuz: 2614.9, |
| 108 | + torch.float8_e8m0fnu: 2614.9, |
| 109 | + torch.int8: 2614.9, |
| 110 | + }, |
| 111 | + dram_bw_gbs=5300.0, |
| 112 | + dram_gb=192.0, |
| 113 | + ), |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +def lookup_device_info(name: str) -> Optional[DeviceInfo]: |
| 118 | + """ |
| 119 | + Problem: when diffing profiles between amd and nvidia, we don't have access to the device information |
| 120 | + of the other one. Also, since the analysis is static, we should be able to do it on another device unrelated |
| 121 | + to the recorded device. Therefore, _device_mapping statically contains the information for lots of devices. |
| 122 | + If one is missing, please run DeviceInfo.get_device_info() and add it to _device_mapping. |
| 123 | + name (str): name of the device to lookup. Should map onto torch.cuda.get_device_name(). |
| 124 | + """ |
| 125 | + if name not in _device_mapping: |
| 126 | + return None |
| 127 | + return _device_mapping[name] |
| 128 | + |
| 129 | + |
| 130 | +def datasheet_tops(dtype: torch.dtype) -> Optional[float]: |
| 131 | + """ |
| 132 | + Get the theoretical TFLOPS of the device for a given dtype. This can throw an exception if the device |
| 133 | + is not in the datasheet list above. |
| 134 | + """ |
| 135 | + name: Optional[str] = torch.cuda.get_device_name() |
| 136 | + if name is None: |
| 137 | + info("No device found, returning None") |
| 138 | + return None |
| 139 | + device_info = lookup_device_info(name) |
| 140 | + if device_info is None: |
| 141 | + log_str = f"Device {name} not in datasheet, returning None" |
| 142 | + info(log_str) |
| 143 | + return None |
| 144 | + if dtype not in device_info.tops: |
| 145 | + log_str = ( |
| 146 | + f"Device {name} does not have a datasheet entry for {dtype}, returning None" |
| 147 | + ) |
| 148 | + info(log_str) |
| 149 | + return None |
| 150 | + return device_info.tops[dtype] |
0 commit comments