diff --git a/internal/config/image/cuda_image_test.go b/internal/config/image/cuda_image_test.go index 858034a41..ec73f6574 100644 --- a/internal/config/image/cuda_image_test.go +++ b/internal/config/image/cuda_image_test.go @@ -417,6 +417,22 @@ func TestGetDevicesFromEnvvar(t *testing.T) { }, expectedDevices: []string{anotherGPUID}, }, + { + description: "repeated device in NVIDIA_VISIBLE_DEVICES is returned once", + env: map[string]string{ + EnvVarNvidiaVisibleDevices: gpuID + "," + anotherGPUID + "," + gpuID, + }, + expectedDevices: []string{gpuID, anotherGPUID}, + }, + { + description: "device repeated across swarm resource envvars is returned once", + preferredVisibleDeviceEnvVars: []string{"DOCKER_RESOURCE_GPUS", "DOCKER_RESOURCE_GPUS_ADDITIONAL"}, + env: map[string]string{ + "DOCKER_RESOURCE_GPUS": gpuID, + "DOCKER_RESOURCE_GPUS_ADDITIONAL": gpuID, + }, + expectedDevices: []string{gpuID}, + }, } for _, tc := range tests { diff --git a/internal/config/image/devices.go b/internal/config/image/devices.go index 96b11d24a..86f59f243 100644 --- a/internal/config/image/devices.go +++ b/internal/config/image/devices.go @@ -93,6 +93,9 @@ func newDevices(idOrCommaSeparated ...string) devices { i := 0 for _, commaSeparated := range idOrCommaSeparated { for id := range strings.SplitSeq(commaSeparated, ",") { + if _, exists := lookup[id]; exists { + continue + } lookup[id] = i i++ }