|
| 1 | +package org.pytorch.serve.device; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.LinkedHashSet; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import org.pytorch.serve.device.interfaces.IAcceleratorUtility; |
| 11 | +import org.pytorch.serve.device.utils.*; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +public class SystemInfo { |
| 16 | + static final Logger logger = LoggerFactory.getLogger(SystemInfo.class); |
| 17 | + // |
| 18 | + // Contains information about the system (physical or virtual machine) |
| 19 | + // we are running the workload on. |
| 20 | + // Specifically how many accelerators and info about them. |
| 21 | + // |
| 22 | + |
| 23 | + public AcceleratorVendor acceleratorVendor; |
| 24 | + ArrayList<Accelerator> accelerators; |
| 25 | + private IAcceleratorUtility acceleratorUtil; |
| 26 | + |
| 27 | + public SystemInfo() { |
| 28 | + // Detect and set the vendor of any accelerators in the system |
| 29 | + this.acceleratorVendor = detectVendorType(); |
| 30 | + this.accelerators = new ArrayList<Accelerator>(); |
| 31 | + |
| 32 | + // If accelerators are present (vendor != UNKNOWN), |
| 33 | + // initialize accelerator utilities |
| 34 | + Optional.of(hasAccelerators()) |
| 35 | + // Only proceed if hasAccelerators() returns true |
| 36 | + .filter(Boolean::booleanValue) |
| 37 | + // Execute this block if accelerators are present |
| 38 | + .ifPresent( |
| 39 | + hasAcc -> { |
| 40 | + // Create the appropriate utility class based on vendor |
| 41 | + this.acceleratorUtil = createAcceleratorUtility(); |
| 42 | + // Populate the accelerators list based on environment |
| 43 | + // variables and available devices |
| 44 | + populateAccelerators(); |
| 45 | + }); |
| 46 | + |
| 47 | + // Safely handle accelerator metrics update |
| 48 | + Optional.ofNullable(accelerators) |
| 49 | + // Only proceed if the accelerators list is not empty |
| 50 | + .filter(list -> !list.isEmpty()) |
| 51 | + // Update metrics (utilization, memory, etc.) for all accelerators if list |
| 52 | + // exists and not empty |
| 53 | + .ifPresent(list -> updateAcceleratorMetrics()); |
| 54 | + } |
| 55 | + |
| 56 | + private IAcceleratorUtility createAcceleratorUtility() { |
| 57 | + switch (this.acceleratorVendor) { |
| 58 | + case AMD: |
| 59 | + return new ROCmUtil(); |
| 60 | + case NVIDIA: |
| 61 | + return new CudaUtil(); |
| 62 | + case INTEL: |
| 63 | + return new XpuUtil(); |
| 64 | + case APPLE: |
| 65 | + return new AppleUtil(); |
| 66 | + default: |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void populateAccelerators() { |
| 72 | + if (this.acceleratorUtil != null) { |
| 73 | + String envVarName = this.acceleratorUtil.getGpuEnvVariableName(); |
| 74 | + String requestedAcceleratorIds = System.getenv(envVarName); |
| 75 | + LinkedHashSet<Integer> availableAcceleratorIds = |
| 76 | + IAcceleratorUtility.parseVisibleDevicesEnv(requestedAcceleratorIds); |
| 77 | + this.accelerators = |
| 78 | + this.acceleratorUtil.getAvailableAccelerators(availableAcceleratorIds); |
| 79 | + } else { |
| 80 | + this.accelerators = new ArrayList<>(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + boolean hasAccelerators() { |
| 85 | + return this.acceleratorVendor != AcceleratorVendor.UNKNOWN; |
| 86 | + } |
| 87 | + |
| 88 | + public Integer getNumberOfAccelerators() { |
| 89 | + // since we instance create `accelerators` as an empty list |
| 90 | + // in the constructor, the null check should be redundant. |
| 91 | + // leaving it to be sure. |
| 92 | + return (accelerators != null) ? accelerators.size() : 0; |
| 93 | + } |
| 94 | + |
| 95 | + public static AcceleratorVendor detectVendorType() { |
| 96 | + if (isCommandAvailable("rocm-smi")) { |
| 97 | + return AcceleratorVendor.AMD; |
| 98 | + } else if (isCommandAvailable("nvidia-smi")) { |
| 99 | + return AcceleratorVendor.NVIDIA; |
| 100 | + } else if (isCommandAvailable("xpu-smi")) { |
| 101 | + return AcceleratorVendor.INTEL; |
| 102 | + } else if (isCommandAvailable("system_profiler")) { |
| 103 | + return AcceleratorVendor.APPLE; |
| 104 | + } else { |
| 105 | + return AcceleratorVendor.UNKNOWN; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean isCommandAvailable(String command) { |
| 110 | + String operatingSystem = System.getProperty("os.name").toLowerCase(); |
| 111 | + String commandCheck = operatingSystem.contains("win") ? "where" : "which"; |
| 112 | + ProcessBuilder processBuilder = new ProcessBuilder(commandCheck, command); |
| 113 | + try { |
| 114 | + Process process = processBuilder.start(); |
| 115 | + int exitCode = process.waitFor(); |
| 116 | + return exitCode == 0; |
| 117 | + } catch (IOException | InterruptedException e) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public ArrayList<Accelerator> getAccelerators() { |
| 123 | + return this.accelerators; |
| 124 | + } |
| 125 | + |
| 126 | + private void updateAccelerators(List<Accelerator> updatedAccelerators) { |
| 127 | + // Create a map of existing accelerators with ID as key |
| 128 | + Map<Integer, Accelerator> existingAcceleratorsMap = |
| 129 | + this.accelerators.stream().collect(Collectors.toMap(acc -> acc.id, acc -> acc)); |
| 130 | + |
| 131 | + // Update existing accelerators and add new ones |
| 132 | + this.accelerators = |
| 133 | + updatedAccelerators.stream() |
| 134 | + .map( |
| 135 | + updatedAcc -> { |
| 136 | + Accelerator existingAcc = |
| 137 | + existingAcceleratorsMap.get(updatedAcc.id); |
| 138 | + if (existingAcc != null) { |
| 139 | + existingAcc.updateDynamicAttributes(updatedAcc); |
| 140 | + return existingAcc; |
| 141 | + } else { |
| 142 | + return updatedAcc; |
| 143 | + } |
| 144 | + }) |
| 145 | + .collect(Collectors.toCollection(ArrayList::new)); |
| 146 | + } |
| 147 | + |
| 148 | + public void updateAcceleratorMetrics() { |
| 149 | + if (this.acceleratorUtil != null) { |
| 150 | + List<Accelerator> updatedAccelerators = |
| 151 | + this.acceleratorUtil.getUpdatedAcceleratorsUtilization(this.accelerators); |
| 152 | + |
| 153 | + updateAccelerators(updatedAccelerators); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public AcceleratorVendor getAcceleratorVendor() { |
| 158 | + return this.acceleratorVendor; |
| 159 | + } |
| 160 | + |
| 161 | + public String getVisibleDevicesEnvName() { |
| 162 | + if (this.accelerators.isEmpty() || this.accelerators == null) { |
| 163 | + return null; |
| 164 | + } |
| 165 | + return this.accelerators.get(0).acceleratorUtility.getGpuEnvVariableName(); |
| 166 | + } |
| 167 | +} |
0 commit comments