Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support calculate custom gpu resource #2477

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion ray-operator/controllers/ray/raycluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
controller "sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand Down Expand Up @@ -1619,10 +1619,18 @@ func (r *RayClusterReconciler) updateRayClusterStatus(ctx context.Context, origi
func sumGPUs(resources map[corev1.ResourceName]resource.Quantity) resource.Quantity {
totalGPUs := resource.Quantity{}

var customKeys []string
if accelerators := os.Getenv(utils.CUSTOM_GPU_ACCELERATOR); accelerators != "" {
customKeys = strings.Split(accelerators, ",")
}

for key, val := range resources {
if strings.HasSuffix(string(key), "gpu") && !val.IsZero() {
totalGPUs.Add(val)
}
if utils.Contains(customKeys, string(key)) && !val.IsZero() {
totalGPUs.Add(val)
}
}

return totalGPUs
Expand Down
20 changes: 18 additions & 2 deletions ray-operator/controllers/ray/raycluster_controller_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2935,13 +2935,15 @@ func TestSumGPUs(t *testing.T) {
googleTPUResourceName := corev1.ResourceName("google.com/tpu")

tests := map[string]struct {
input map[corev1.ResourceName]resource.Quantity
expected resource.Quantity
input map[corev1.ResourceName]resource.Quantity
customGPUAcceleratorEnvInput string
expected resource.Quantity
}{
"no GPUs specified": {
map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("1"),
},
"",
resource.MustParse("0"),
},
"one GPU type specified": {
Expand All @@ -2950,6 +2952,7 @@ func TestSumGPUs(t *testing.T) {
nvidiaGPUResourceName: resource.MustParse("1"),
googleTPUResourceName: resource.MustParse("1"),
},
"",
resource.MustParse("1"),
},
"multiple GPUs specified": {
Expand All @@ -2959,12 +2962,25 @@ func TestSumGPUs(t *testing.T) {
corev1.ResourceName("foo.bar/gpu"): resource.MustParse("2"),
googleTPUResourceName: resource.MustParse("1"),
},
"",
resource.MustParse("5"),
},
"custom GPUs specified": {
map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("1"),
nvidiaGPUResourceName: resource.MustParse("3"),
corev1.ResourceName("foo.bar/gpu"): resource.MustParse("2"),
corev1.ResourceName("gpu.intel.com/i915"): resource.MustParse("4"),
googleTPUResourceName: resource.MustParse("1"),
},
"gpu.intel.com/i915",
resource.MustParse("9"),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Setenv(utils.CUSTOM_GPU_ACCELERATOR, tc.customGPUAcceleratorEnvInput)
result := sumGPUs(tc.input)
assert.True(t, tc.expected.Equal(result), "GPU number is wrong")
})
Expand Down
3 changes: 3 additions & 0 deletions ray-operator/controllers/ray/utils/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ const (
// If set to true, the RayJob CR itself will be deleted if shutdownAfterJobFinishes is set to true. Note that all resources created by the RayJob CR will be deleted, including the K8s Job.
DELETE_RAYJOB_CR_AFTER_JOB_FINISHES = "DELETE_RAYJOB_CR_AFTER_JOB_FINISHES"

// If not empty, the value will use to calculate ray cluster gpu status. The value must be some valid GPU resource keys, separated by commas.
CUSTOM_GPU_ACCELERATOR = "CUSTOM_GPU_ACCELERATOR"

// Ray core default configurations
DefaultWorkerRayGcsReconnectTimeoutS = "600"

Expand Down
Loading