|
| 1 | +package gpu |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu/common" |
| 11 | + "github.com/1Panel-dev/1Panel/agent/utils/cmd" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + ascendVersionPattern = regexp.MustCompile(`(?i)\bVersion:\s*([^\s|]+)`) |
| 16 | + ascendMemoryPattern = regexp.MustCompile(`([0-9]+(?:\.[0-9]+)?)\s*/\s*([0-9]+(?:\.[0-9]+)?)`) |
| 17 | +) |
| 18 | + |
| 19 | +type AscendSMI struct{} |
| 20 | + |
| 21 | +func (a AscendSMI) LoadGpuInfo() (*common.GpuInfo, error) { |
| 22 | + cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(5 * time.Second)) |
| 23 | + itemData, err := cmdMgr.RunWithStdout("npu-smi", "info") |
| 24 | + if err != nil { |
| 25 | + return nil, fmt.Errorf("calling npu-smi failed, %v", err) |
| 26 | + } |
| 27 | + return parseAscendSMI(itemData), nil |
| 28 | +} |
| 29 | + |
| 30 | +func parseAscendSMI(data string) *common.GpuInfo { |
| 31 | + info := &common.GpuInfo{Type: "ascend"} |
| 32 | + if match := ascendVersionPattern.FindStringSubmatch(data); len(match) == 2 { |
| 33 | + info.DriverVersion = match[1] |
| 34 | + } |
| 35 | + |
| 36 | + processSection := false |
| 37 | + deviceIndexes := make(map[uint]int) |
| 38 | + var pending *common.GPU |
| 39 | + |
| 40 | + for _, line := range strings.Split(data, "\n") { |
| 41 | + if strings.Contains(line, "Process id") && strings.Contains(line, "Process memory") { |
| 42 | + processSection = true |
| 43 | + pending = nil |
| 44 | + continue |
| 45 | + } |
| 46 | + |
| 47 | + cells := ascendTableCells(line) |
| 48 | + if processSection { |
| 49 | + if len(cells) != 4 && len(cells) != 5 { |
| 50 | + continue |
| 51 | + } |
| 52 | + deviceFields := strings.Fields(cells[0]) |
| 53 | + if len(deviceFields) == 0 { |
| 54 | + continue |
| 55 | + } |
| 56 | + deviceID, err := strconv.ParseUint(deviceFields[0], 10, 64) |
| 57 | + if err != nil { |
| 58 | + continue |
| 59 | + } |
| 60 | + index, ok := deviceIndexes[uint(deviceID)] |
| 61 | + if !ok { |
| 62 | + continue |
| 63 | + } |
| 64 | + processOffset := len(cells) - 3 |
| 65 | + info.GPUs[index].Processes = append(info.GPUs[index].Processes, common.Process{ |
| 66 | + Pid: strings.TrimSpace(cells[processOffset]), |
| 67 | + Type: "NPU", |
| 68 | + ProcessName: strings.TrimSpace(cells[processOffset+1]), |
| 69 | + UsedMemory: ascendValueWithUnit(cells[processOffset+2], "MB"), |
| 70 | + }) |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if len(cells) != 3 { |
| 75 | + continue |
| 76 | + } |
| 77 | + if pending == nil { |
| 78 | + fields := strings.Fields(cells[0]) |
| 79 | + if len(fields) < 2 { |
| 80 | + continue |
| 81 | + } |
| 82 | + deviceID, err := strconv.ParseUint(fields[0], 10, 64) |
| 83 | + if err != nil { |
| 84 | + continue |
| 85 | + } |
| 86 | + metrics := strings.Fields(cells[2]) |
| 87 | + if len(metrics) < 2 { |
| 88 | + continue |
| 89 | + } |
| 90 | + pending = &common.GPU{ |
| 91 | + Type: "ascend", |
| 92 | + Index: uint(deviceID), |
| 93 | + ProductName: strings.Join(fields[1:], " "), |
| 94 | + PersistenceMode: "N/A", |
| 95 | + DisplayActive: "N/A", |
| 96 | + ECC: "N/A", |
| 97 | + FanSpeed: "N/A", |
| 98 | + Temperature: ascendValueWithUnit(metrics[1], "C"), |
| 99 | + PerformanceState: strings.TrimSpace(cells[1]), |
| 100 | + PowerDraw: ascendValueWithUnit(metrics[0], "W"), |
| 101 | + MaxPowerLimit: "N/A", |
| 102 | + ComputeMode: "N/A", |
| 103 | + MigMode: "N/A", |
| 104 | + } |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + metrics := strings.Fields(cells[2]) |
| 109 | + if len(metrics) == 0 { |
| 110 | + pending = nil |
| 111 | + continue |
| 112 | + } |
| 113 | + pending.BusID = strings.TrimSpace(cells[1]) |
| 114 | + pending.GPUUtil = ascendValueWithUnit(metrics[0], "%") |
| 115 | + pending.MemUsed, pending.MemTotal = ascendMemoryUsage(cells[2]) |
| 116 | + deviceIndexes[pending.Index] = len(info.GPUs) |
| 117 | + info.GPUs = append(info.GPUs, *pending) |
| 118 | + pending = nil |
| 119 | + } |
| 120 | + |
| 121 | + return info |
| 122 | +} |
| 123 | + |
| 124 | +func ascendTableCells(line string) []string { |
| 125 | + line = strings.TrimSpace(line) |
| 126 | + if !strings.HasPrefix(line, "|") || !strings.HasSuffix(line, "|") { |
| 127 | + return nil |
| 128 | + } |
| 129 | + parts := strings.Split(line, "|") |
| 130 | + if len(parts) < 3 { |
| 131 | + return nil |
| 132 | + } |
| 133 | + cells := make([]string, 0, len(parts)-2) |
| 134 | + for _, part := range parts[1 : len(parts)-1] { |
| 135 | + cells = append(cells, strings.TrimSpace(part)) |
| 136 | + } |
| 137 | + return cells |
| 138 | +} |
| 139 | + |
| 140 | +func ascendMemoryUsage(value string) (string, string) { |
| 141 | + matches := ascendMemoryPattern.FindAllStringSubmatch(value, -1) |
| 142 | + if len(matches) == 0 { |
| 143 | + return "N/A", "N/A" |
| 144 | + } |
| 145 | + // 910B exposes both generic memory and HBM. Prefer the last memory pool |
| 146 | + // with a non-zero capacity, which selects HBM while retaining compatibility |
| 147 | + // with devices that only expose Memory-Usage. |
| 148 | + selected := matches[len(matches)-1] |
| 149 | + for i := len(matches) - 1; i >= 0; i-- { |
| 150 | + if total, err := strconv.ParseFloat(matches[i][2], 64); err == nil && total > 0 { |
| 151 | + selected = matches[i] |
| 152 | + break |
| 153 | + } |
| 154 | + } |
| 155 | + return selected[1] + " MB", selected[2] + " MB" |
| 156 | +} |
| 157 | + |
| 158 | +func ascendValueWithUnit(value, unit string) string { |
| 159 | + value = strings.TrimSpace(value) |
| 160 | + if value == "" || strings.EqualFold(value, "N/A") || strings.EqualFold(value, "NA") { |
| 161 | + return "N/A" |
| 162 | + } |
| 163 | + return value + " " + unit |
| 164 | +} |
0 commit comments