Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ The mock **derives the registered resources from the annotation, not from the co
- **Number of fake cards = the number of entries in the annotation array** (not the count value).
- Registered `...-memory` = **sum of `devmem`** over all entries.
- Registered `...-cores` / `...-core` = **sum of `devcore`** over all entries.
- The **count extended resource is only a health gate**: its value just needs to be `> 0`. It does **not** affect the registered memory/cores. By convention it is set to `cards x splits-per-card` (e.g. Ascend `2 x VDeviceCount(4) = 8`), but `1` would work equally well for the memory/cores to appear.
- The **count extended resource is only a health gate**: its value just needs to be `> 0`. It does **not** affect the registered memory/cores. For a production-shaped scheduling test, set it to `cards x splits-per-card` (for example, one AMD card with the default split count advertises `10`), even though `1` is enough for the mock memory/core reducer to appear.

Annotation entry fields:

| field | meaning |
| :-- | :-- |
| `id` | unique device UUID (any string) |
| `devmem` | per-card memory in MB -- **summed** into `...-memory` |
| `devcore` | per-card cores. **NVIDIA/Hygon:** summed into `...-cores` (NVIDIA: percentage, 100 = a whole card). **Ascend:** ignored -- `huawei.com/<chip>-core` is percentage-based, registered as **100 per card**. |
| `count` | per-card split count (informational for the mock) |
| `devmem` | per-card memory in MiB -- **summed** into `...-memory` |
| `devcore` | per-card cores. **NVIDIA/Hygon:** summed into `...-cores` (NVIDIA: percentage, 100 = a whole card). **AMD:** physical CU count used by HAMi when converting percentage requests, but the mock registers **100 per healthy card**. **Ascend:** ignored -- `huawei.com/<chip>-core` is percentage-based, registered as **100 per card**. |
| `count` | per-card split/concurrency limit used by HAMi Scheduler; the mock memory/core reducer does not sum it |
| `type` | device model string |
| `health` | must be `true` to be counted |
| `index` | card index `0,1,2,...` (`0` may be omitted) |
Expand Down Expand Up @@ -85,6 +85,42 @@ kubectl get node <node> -o json | jq '.status.allocatable|with_entries(select(.k
# expect: nvidia.com/gpu=10, nvidia.com/gpumem=81920, nvidia.com/gpucores=100, nvidia.com/gpumem-percentage=100
```

### AMD GPU

- config block: `amd:` | annotation: `hami.io/node-amd-register` (JSON) | count: `amd.com/gpu`
- mock registers: `amd.com/gpumem`, `amd.com/gpucores`

The AMD config must declare all three distinct resource names under the same vendor namespace:

```yaml
amd:
resourceCountName: amd.com/gpu
resourceMemoryName: amd.com/gpumem
resourceCoreName: amd.com/gpucores
```

```bash
# (2) external count resource: one card x the AMD plugin's default split count (10)
kubectl patch node <node> --subresource=status --type=json \
-p '[{"op":"add","path":"/status/capacity/amd.com~1gpu","value":"10"}]'
# (1) transport-safe fake inventory for a Scheduler smoke test. devmem is
# intentionally reduced to 32 GiB; see the MI300X limitation below.
kubectl annotate node <node> \
'hami.io/node-amd-register=[{"id":"AMD-MOCK-0","count":10,"devmem":32768,"devcore":304,"type":"AMD-MOCK-32G","health":true,"numa":0}]'
# verify (~30s later)
kubectl get node <node> -o json | jq '.status.allocatable|with_entries(select(.key|test("amd.com")))'
# expect: amd.com/gpu=10, amd.com/gpumem=32768, amd.com/gpucores=100
```

HAMi expresses AMD core requests as percentages. Each healthy physical card therefore contributes `100` to `amd.com/gpucores`; the annotation's `devcore` remains the real CU count that HAMi uses to convert the requested percentage into CUs. Unhealthy annotation entries contribute neither memory nor core capacity.

The released AMD device plugin reports one MI300X VF with `count: 10`, about `196288` MiB of memory, and `devcore: 304`. The current generic mock expands every MiB of scalar capacity into one Device Plugin API object; a full MI300X response is therefore about 6.76 MiB and exceeds kubelet's default 4 MiB gRPC receive limit.

> [!IMPORTANT]
> This is a mock-only transport limitation. The released AMD device plugin registers only ten logical `amd.com/gpu` splits per physical card and carries the full memory/CU values in the Node annotation; it does not send 196288 memory Device objects to kubelet. Do not use this PR to claim full-capacity MI300X mock coverage.

HAMi's AMD configuration does not yet expose an end-to-end `memoryFactor`, so do not use a full MI300X memory value as a working mock example. Supporting that capacity without changing resource semantics requires a matching factor in both HAMi Scheduler and this mock. The reduced 32 GiB inventory above verifies only annotation decoding, resource registration, and Scheduler Filter/Bind behavior.

### Ascend NPU (e.g. 910B4)

- config block: `vnpus.configs` (entry for `910B4`) | annotation: `hami.io/node-register-Ascend910B4` (JSON) | count: `huawei.com/Ascend910B4`
Expand Down Expand Up @@ -153,10 +189,11 @@ The new nested format is tried first; if that fails it falls back to the legacy
| Devices | Mocking Resources |
| :--- | :---- |
| Nvidia GPU | `nvidia.com/gpumem`, `nvidia.com/gpumem-percentage`, `nvidia.com/gpucores` |
| AMD GPU | `amd.com/gpumem`, `amd.com/gpucores` |
| Hygon DCU | `hygon.com/dcumem`, `hygon.com/dcucores` (when `resourceCoreName` is set) |
| Ascend | `huawei.com/Ascend{chip}-memory`, `huawei.com/Ascend{chip}-core` (when `resourceCoreName` is set **and** the node is in `hami-vnpu-core` mode) |

**Note:** If the counted memory is too large (e.g. > 120GB) it may display as 0. Set `memoryFactor` in the `hami-scheduler-device` ConfigMap (default 1).
**Note:** If counted memory is too large (e.g. > 120GB), the Device Plugin response may exceed kubelet's receive limit and the resource can display as `0`. Device families whose HAMi configuration exposes `memoryFactor` can reduce the advertised scalar count with the same factor. AMD does not currently have that end-to-end setting; use the reduced smoke-test inventory above until the Scheduler and mock contracts are extended together.

## Build

Expand Down
142 changes: 102 additions & 40 deletions internal/pkg/api/device/amd/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,134 @@ limitations under the License.
package amd

import (
"errors"
"fmt"
"strings"

"github.com/HAMi/mock-device-plugin/internal/pkg/api/device"
"github.com/HAMi/mock-device-plugin/internal/pkg/mock"
"github.com/kubevirt/device-plugin-manager/pkg/dpm"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/klog/v2"
)

const (
AMDDevice = "AMDGPU"
AMDCommonWord = "AMDGPU"
AMDDeviceSelection = "amd.com/gpu-index"
AMDUseUUID = "amd.com/use-gpu-uuid"
AMDNoUseUUID = "amd.com/nouse-gpu-uuid"
AMDAssignedNode = "amd.com/predicate-node"
Mi300xMemory = 192000
AMDDevice = "AMD"
AMDCommonWord = "AMD"
RegisterAnnos = "hami.io/node-amd-register"
)

type AMDConfig struct {
ResourceCountName string `yaml:"resourceCountName"`
ResourceMemoryName string `yaml:"resourceMemoryName"`
ResourceCoreName string `yaml:"resourceCoreName"`
}

type AMDDevices struct {
resourceCountName string
resourceMemoryName string
config AMDConfig
}

func InitAMDGPUDevice(config AMDConfig) *AMDDevices {
return &AMDDevices{
resourceCountName: config.ResourceCountName,
resourceMemoryName: config.ResourceMemoryName,
func InitAMDGPUDevice(config AMDConfig) (*AMDDevices, error) {
if config.ResourceCountName == "" && config.ResourceMemoryName == "" && config.ResourceCoreName == "" {
return nil, nil
}
resourceNames := []string{
config.ResourceCountName,
config.ResourceMemoryName,
config.ResourceCoreName,
}
for _, resourceName := range resourceNames {
if resourceName == "" {
return nil, errors.New("amd resource count, memory, and core names are required")
}
vendor, resource, qualified := strings.Cut(resourceName, "/")
if !qualified || vendor == "" || resource == "" || len(utilvalidation.IsQualifiedName(resourceName)) > 0 {
return nil, fmt.Errorf("amd resource name %q must be a qualified extended resource name", resourceName)
}
}
if config.ResourceCountName == config.ResourceMemoryName ||
config.ResourceCountName == config.ResourceCoreName ||
config.ResourceMemoryName == config.ResourceCoreName {
return nil, errors.New("amd resource count, memory, and core names must be distinct")
}
vendor := device.GetVendorName(config.ResourceCountName)
if device.GetVendorName(config.ResourceMemoryName) != vendor ||
device.GetVendorName(config.ResourceCoreName) != vendor {
return nil, errors.New("amd resource count, memory, and core names must use the same vendor namespace")
}

return &AMDDevices{config: config}, nil
}

func (dev *AMDDevices) CommonWord() string {
return AMDCommonWord
}

func (dev *AMDDevices) GetNodeDevices(n corev1.Node) ([]*device.DeviceInfo, error) {
nodedevices := []*device.DeviceInfo{}
i := 0
counts, ok := n.Status.Capacity.Name(corev1.ResourceName(dev.resourceCountName), resource.DecimalSI).AsInt64()
if !ok || counts == 0 {
return []*device.DeviceInfo{}, fmt.Errorf("device not found %s", dev.resourceCountName)
func (dev *AMDDevices) GetNodeDevices(n *corev1.Node) ([]*device.DeviceInfo, error) {
devEncoded, ok := n.Annotations[RegisterAnnos]
if !ok {
return []*device.DeviceInfo{}, errors.New("annos not found " + RegisterAnnos)
}
nodeDevices, err := device.UnMarshalNodeDevices(devEncoded)
if err != nil {
klog.ErrorS(err, "failed to decode node devices", "node", n.Name, "device annotation", devEncoded)
return []*device.DeviceInfo{}, err
}
if len(nodeDevices) == 0 {
return []*device.DeviceInfo{}, fmt.Errorf("no amd gpu found on node %s", n.Name)
}
for idx, nodeDevice := range nodeDevices {
if nodeDevice == nil {
return []*device.DeviceInfo{}, fmt.Errorf("amd device annotation contains null entry at index %d", idx)
}
if nodeDevice.Count < 0 || nodeDevice.Devmem < 0 || nodeDevice.Devcore < 0 {
return []*device.DeviceInfo{}, fmt.Errorf("amd device annotation contains negative capacity at index %d", idx)
}
nodeDevice.DeviceVendor = dev.CommonWord()
}
return nodeDevices, nil
}

func (dev *AMDDevices) GetResource(n *corev1.Node) map[string]int {
memoryResourceName := device.GetResourceName(dev.config.ResourceMemoryName)
coreResourceName := device.GetResourceName(dev.config.ResourceCoreName)
resourceMap := map[string]int{
memoryResourceName: 0,
coreResourceName: 0,
}
if !device.CheckHealthy(n, dev.config.ResourceCountName) {
klog.Infof("device %s is unhealthy on this node", dev.CommonWord())
return resourceMap
}
for int64(i) < counts {
nodedevices = append(nodedevices, &device.DeviceInfo{
Index: uint(i),
ID: n.Name + "-" + AMDDevice + "-" + fmt.Sprint(i),
Count: 1,
Devmem: Mi300xMemory,
Devcore: 100,
Type: AMDDevice,
Numa: 0,
Health: true,
CustomInfo: make(map[string]any),
DeviceVendor: AMDCommonWord,
})
i++

nodeDevices, err := dev.GetNodeDevices(n)
if err != nil {
klog.Infof("no device %s on this node", dev.CommonWord())
return resourceMap
}
i = 0
for i < len(nodedevices) {
klog.V(4).Infoln("Registered AMD nodedevices:", nodedevices[i])
i++
for _, nodeDevice := range nodeDevices {
if !nodeDevice.Health {
continue
}
resourceMap[memoryResourceName] += int(nodeDevice.Devmem)
// HAMi exposes AMD core requests as percentages. The annotation's
// Devcore remains the physical CU count used by the scheduler to turn
// a percentage request into CUs; every healthy card contributes 100.
resourceMap[coreResourceName] += 100
}
return nodedevices, nil
}
klog.InfoS("Add resources",
memoryResourceName, resourceMap[memoryResourceName],
coreResourceName, resourceMap[coreResourceName],
)
return resourceMap
}

func (dev *AMDDevices) RunManager() {
lmock := mock.NewMockLister(device.GetVendorName(dev.config.ResourceMemoryName))
go device.Register(lmock, dev)
mockManager := dpm.NewManager(lmock)
klog.Infof("Running mocking dp: %s", dev.CommonWord())
mockManager.Run()
}
Loading