2026-08-20 15:01:15 +08:00
|
|
|
|
package accelerator
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"sync"
|
2026-09-28 18:32:12 +08:00
|
|
|
|
"time"
|
2026-08-20 15:01:15 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/gpu"
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/npu"
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/ai_tools/xpu"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
|
providers []Provider
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type providerResult struct {
|
2026-11-12 23:27:43 +08:00
|
|
|
|
kind Kind
|
2026-08-20 15:01:15 +08:00
|
|
|
|
snapshot *ProviderSnapshot
|
|
|
|
|
|
err error
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func New() (bool, Client) {
|
|
|
|
|
|
client := Client{}
|
|
|
|
|
|
if available, gpuClient := gpu.New(); available {
|
|
|
|
|
|
client.providers = append(client.providers, gpuProvider{client: gpuClient})
|
|
|
|
|
|
}
|
|
|
|
|
|
if available, npuClient := npu.New(); available {
|
|
|
|
|
|
client.providers = append(client.providers, npuProvider{client: npuClient})
|
|
|
|
|
|
}
|
|
|
|
|
|
if available, xpuClient := xpu.New(); available {
|
|
|
|
|
|
client.providers = append(client.providers, xpuProvider{client: xpuClient})
|
|
|
|
|
|
}
|
|
|
|
|
|
return len(client.providers) > 0, client
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c Client) LoadInfo() (*Info, error) {
|
|
|
|
|
|
snapshot, err := c.Collect(context.Background())
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &snapshot.Info, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (c Client) Collect(ctx context.Context) (*Snapshot, error) {
|
2026-09-28 18:32:12 +08:00
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-11-12 23:27:43 +08:00
|
|
|
|
collectCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
2026-09-28 18:32:12 +08:00
|
|
|
|
defer cancel()
|
2026-08-20 15:01:15 +08:00
|
|
|
|
results := make([]providerResult, len(c.providers))
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
for index, item := range c.providers {
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
defer wg.Done()
|
2026-11-12 23:27:43 +08:00
|
|
|
|
providerSnapshot, err := item.Collect(collectCtx)
|
2026-08-20 15:01:15 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
err = fmt.Errorf("%s provider failed: %w", item.Kind(), err)
|
|
|
|
|
|
}
|
2026-11-12 23:27:43 +08:00
|
|
|
|
results[index] = providerResult{kind: item.Kind(), snapshot: providerSnapshot, err: err}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
}()
|
|
|
|
|
|
}
|
|
|
|
|
|
wg.Wait()
|
2026-09-28 18:32:12 +08:00
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
|
2026-11-12 23:27:43 +08:00
|
|
|
|
snapshot := &Snapshot{Info: Info{VendorWarnings: map[string][]string{}}}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
var (
|
|
|
|
|
|
errs []error
|
|
|
|
|
|
active []*ProviderSnapshot
|
|
|
|
|
|
xpuVersion string
|
|
|
|
|
|
)
|
2026-11-12 23:27:43 +08:00
|
|
|
|
if err := collectCtx.Err(); err != nil {
|
|
|
|
|
|
snapshot.Info.Warnings = append(snapshot.Info.Warnings, err.Error())
|
|
|
|
|
|
}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
for _, result := range results {
|
2026-11-12 23:27:43 +08:00
|
|
|
|
vendor := ""
|
|
|
|
|
|
switch result.kind {
|
|
|
|
|
|
case KindXPU:
|
|
|
|
|
|
vendor = "intel"
|
|
|
|
|
|
case KindNPU:
|
|
|
|
|
|
vendor = "ascend"
|
|
|
|
|
|
}
|
|
|
|
|
|
if result.err != nil && (result.snapshot == nil || len(result.snapshot.VendorWarnings) == 0) {
|
|
|
|
|
|
if vendor == "" {
|
|
|
|
|
|
snapshot.Info.Warnings = append(snapshot.Info.Warnings, result.err.Error())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
snapshot.Info.VendorWarnings[vendor] = append(snapshot.Info.VendorWarnings[vendor], result.err.Error())
|
|
|
|
|
|
}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
}
|
2026-09-28 18:32:12 +08:00
|
|
|
|
if result.snapshot != nil {
|
2026-11-12 23:27:43 +08:00
|
|
|
|
for key, warnings := range result.snapshot.VendorWarnings {
|
|
|
|
|
|
if len(warnings) > 0 {
|
|
|
|
|
|
snapshot.Info.VendorWarnings[key] = append(snapshot.Info.VendorWarnings[key], warnings...)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if vendor != "" && len(result.snapshot.Warnings) > 0 {
|
|
|
|
|
|
snapshot.Info.VendorWarnings[vendor] = append(snapshot.Info.VendorWarnings[vendor], result.snapshot.Warnings...)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
snapshot.Info.Warnings = append(snapshot.Info.Warnings, result.snapshot.Warnings...)
|
2026-09-28 18:32:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
if result.snapshot == nil || len(result.snapshot.Devices) == 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
item := result.snapshot
|
|
|
|
|
|
active = append(active, item)
|
|
|
|
|
|
snapshot.Devices = append(snapshot.Devices, item.Devices...)
|
|
|
|
|
|
snapshot.Info.GPUs = append(snapshot.Info.GPUs, item.GPUs...)
|
|
|
|
|
|
snapshot.Info.NPUs = append(snapshot.Info.NPUs, item.NPUs...)
|
|
|
|
|
|
snapshot.Info.XPUs = append(snapshot.Info.XPUs, item.XPUs...)
|
|
|
|
|
|
if item.CudaVersion != "" {
|
|
|
|
|
|
snapshot.Info.CudaVersion = item.CudaVersion
|
|
|
|
|
|
}
|
2026-09-28 18:32:12 +08:00
|
|
|
|
if item.Type == "xpu" {
|
|
|
|
|
|
xpuVersion = item.DriverVersion
|
2026-08-20 15:01:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-12 23:27:43 +08:00
|
|
|
|
warnings := append([]string{}, snapshot.Info.Warnings...)
|
|
|
|
|
|
for _, vendorWarnings := range snapshot.Info.VendorWarnings {
|
|
|
|
|
|
warnings = append(warnings, vendorWarnings...)
|
2026-08-20 15:01:15 +08:00
|
|
|
|
}
|
2026-11-12 23:27:43 +08:00
|
|
|
|
for _, device := range snapshot.Devices {
|
|
|
|
|
|
if device.ParentID != "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
switch {
|
|
|
|
|
|
case device.GPU != nil:
|
|
|
|
|
|
warnings = append(warnings, device.GPU.Warnings...)
|
|
|
|
|
|
case device.NPU != nil:
|
|
|
|
|
|
warnings = append(warnings, device.NPU.Warnings...)
|
|
|
|
|
|
case device.XPU != nil:
|
|
|
|
|
|
warnings = append(warnings, device.XPU.Warnings...)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, warning := range warnings {
|
|
|
|
|
|
errs = append(errs, errors.New(warning))
|
|
|
|
|
|
}
|
|
|
|
|
|
snapshot.Warnings = errs
|
2026-09-28 18:32:12 +08:00
|
|
|
|
snapshot.Info.CollectedAt = time.Now()
|
2026-08-20 15:01:15 +08:00
|
|
|
|
snapshot.Info.XPUDriverVersion = xpuVersion
|
|
|
|
|
|
snapshot.Info.Type, snapshot.Info.DriverVersion = mergeProviderMetadata(active)
|
2026-11-12 23:27:43 +08:00
|
|
|
|
if len(snapshot.Devices) == 0 && len(errs) > 0 {
|
|
|
|
|
|
return snapshot, fmt.Errorf("calling accelerator monitoring tools failed: %w", errors.Join(errs...))
|
|
|
|
|
|
}
|
2026-08-20 15:01:15 +08:00
|
|
|
|
return snapshot, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func mergeProviderMetadata(items []*ProviderSnapshot) (string, string) {
|
|
|
|
|
|
if len(items) == 0 {
|
|
|
|
|
|
return "", ""
|
|
|
|
|
|
}
|
|
|
|
|
|
resultType := items[0].Type
|
|
|
|
|
|
if len(items) > 1 || resultType == "mixed" {
|
|
|
|
|
|
resultType = "mixed"
|
|
|
|
|
|
}
|
|
|
|
|
|
driverVersions := make([]string, 0, len(items))
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
|
if item.DriverVersion == "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(items) == 1 && item.Type != "mixed" {
|
|
|
|
|
|
driverVersions = append(driverVersions, item.DriverVersion)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if item.Type == "mixed" {
|
|
|
|
|
|
driverVersions = append(driverVersions, item.DriverVersion)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
driverVersions = append(driverVersions, fmt.Sprintf("%s: %s", strings.ToUpper(item.Type), item.DriverVersion))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return resultType, strings.Join(driverVersions, ";")
|
|
|
|
|
|
}
|