Skip to content

Commit a8a772e

Browse files
committed
feat(gpu): introduce GPU request spec
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent 48538ed commit a8a772e

12 files changed

Lines changed: 296 additions & 136 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@ use openshell_core::proto::{
3939
GetClusterInferenceRequest, GetDraftHistoryRequest, GetDraftPolicyRequest,
4040
GetGatewayConfigRequest, GetProviderProfileRequest, GetProviderRefreshStatusRequest,
4141
GetProviderRequest, GetSandboxConfigRequest, GetSandboxLogsRequest,
42-
GetSandboxPolicyStatusRequest, GetSandboxRequest, GetServiceRequest, HealthRequest,
43-
ImportProviderProfilesRequest, LintProviderProfilesRequest, ListProviderProfilesRequest,
44-
ListProvidersRequest, ListSandboxPoliciesRequest, ListSandboxProvidersRequest,
45-
ListSandboxesRequest, ListServicesRequest, PlatformEvent, PolicySource, PolicyStatus, Provider,
46-
ProviderCredentialRefreshStatus, ProviderCredentialRefreshStrategy, ProviderProfile,
47-
ProviderProfileDiagnostic, ProviderProfileImportItem, RejectDraftChunkRequest,
48-
RevokeSshSessionRequest, RotateProviderCredentialRequest, Sandbox, SandboxPhase, SandboxPolicy,
49-
SandboxSpec, SandboxTemplate, ServiceEndpointResponse, SetClusterInferenceRequest,
50-
SettingScope, SettingValue, TcpForwardFrame, TcpForwardInit, TcpRelayTarget,
51-
UpdateConfigRequest, UpdateProviderRequest, WatchSandboxRequest, exec_sandbox_event,
52-
setting_value, tcp_forward_init,
42+
GetSandboxPolicyStatusRequest, GetSandboxRequest, GetServiceRequest, GpuRequestSpec,
43+
HealthRequest, ImportProviderProfilesRequest, LintProviderProfilesRequest,
44+
ListProviderProfilesRequest, ListProvidersRequest, ListSandboxPoliciesRequest,
45+
ListSandboxProvidersRequest, ListSandboxesRequest, ListServicesRequest, PlatformEvent,
46+
PolicySource, PolicyStatus, Provider, ProviderCredentialRefreshStatus,
47+
ProviderCredentialRefreshStrategy, ProviderProfile, ProviderProfileDiagnostic,
48+
ProviderProfileImportItem, RejectDraftChunkRequest, RevokeSshSessionRequest,
49+
RotateProviderCredentialRequest, Sandbox, SandboxPhase, SandboxPolicy, SandboxSpec,
50+
SandboxTemplate, ServiceEndpointResponse, SetClusterInferenceRequest, SettingScope,
51+
SettingValue, TcpForwardFrame, TcpForwardInit, TcpRelayTarget, UpdateConfigRequest,
52+
UpdateProviderRequest, WatchSandboxRequest, exec_sandbox_event, setting_value,
53+
tcp_forward_init,
5354
};
5455
use openshell_core::settings::{self, SettingValueKind};
5556
use openshell_core::{ObjectId, ObjectName};
@@ -1767,8 +1768,7 @@ pub async fn sandbox_create(
17671768

17681769
let request = CreateSandboxRequest {
17691770
spec: Some(SandboxSpec {
1770-
gpu: requested_gpu,
1771-
gpu_device: gpu_device.unwrap_or_default().to_string(),
1771+
gpu: gpu_request_from_cli(requested_gpu, gpu_device),
17721772
policy,
17731773
providers: configured_providers,
17741774
template,
@@ -2200,6 +2200,15 @@ pub async fn sandbox_create(
22002200
}
22012201
}
22022202

2203+
fn gpu_request_from_cli(requested_gpu: bool, gpu_device: Option<&str>) -> Option<GpuRequestSpec> {
2204+
requested_gpu.then(|| GpuRequestSpec {
2205+
device_id: gpu_device
2206+
.filter(|device_id| !device_id.is_empty())
2207+
.map(|device_id| vec![device_id.to_string()])
2208+
.unwrap_or_default(),
2209+
})
2210+
}
2211+
22032212
/// Resolved source for the `--from` flag on `sandbox create`.
22042213
#[derive(Debug)]
22052214
enum ResolvedSource {
@@ -7040,14 +7049,14 @@ mod tests {
70407049
dockerfile_sources_supported_for_gateway, format_endpoint, format_gateway_select_header,
70417050
format_gateway_select_items, format_provider_attachment_table, gateway_add,
70427051
gateway_auth_label, gateway_env_override_warning, gateway_select_with, gateway_type_label,
7043-
git_sync_files, http_health_check, image_requests_gpu, import_local_package_mtls_bundle,
7044-
inferred_provider_type, local_upload_path_exists, local_upload_path_is_symlink,
7045-
package_managed_tls_dirs, parse_cli_setting_value, parse_credential_expiry_cli_value,
7046-
parse_credential_expiry_pairs, parse_credential_pairs, plaintext_gateway_is_remote,
7047-
progress_step_from_metadata, provider_profile_allows_refresh_bootstrap,
7048-
provisioning_timeout_message, ready_false_condition_message, refresh_status_header,
7049-
refresh_status_row, resolve_from, sandbox_should_persist, service_expose_status_error,
7050-
service_url_for_gateway,
7052+
git_sync_files, gpu_request_from_cli, http_health_check, image_requests_gpu,
7053+
import_local_package_mtls_bundle, inferred_provider_type, local_upload_path_exists,
7054+
local_upload_path_is_symlink, package_managed_tls_dirs, parse_cli_setting_value,
7055+
parse_credential_expiry_cli_value, parse_credential_expiry_pairs, parse_credential_pairs,
7056+
plaintext_gateway_is_remote, progress_step_from_metadata,
7057+
provider_profile_allows_refresh_bootstrap, provisioning_timeout_message,
7058+
ready_false_condition_message, refresh_status_header, refresh_status_row, resolve_from,
7059+
sandbox_should_persist, service_expose_status_error, service_url_for_gateway,
70517060
};
70527061
use crate::TEST_ENV_LOCK;
70537062
use hyper::StatusCode;
@@ -7569,6 +7578,26 @@ mod tests {
75697578
}
75707579
}
75717580

7581+
#[test]
7582+
fn gpu_request_from_cli_uses_presence_with_empty_device_ids_for_default_gpu() {
7583+
let request = gpu_request_from_cli(true, None).expect("gpu request should be present");
7584+
7585+
assert!(request.device_id.is_empty());
7586+
}
7587+
7588+
#[test]
7589+
fn gpu_request_from_cli_maps_gpu_device_to_one_device_id() {
7590+
let request = gpu_request_from_cli(true, Some("0000:2d:00.0"))
7591+
.expect("gpu request should be present");
7592+
7593+
assert_eq!(request.device_id, vec!["0000:2d:00.0"]);
7594+
}
7595+
7596+
#[test]
7597+
fn gpu_request_from_cli_omits_gpu_request_when_not_requested() {
7598+
assert!(gpu_request_from_cli(false, Some("0")).is_none());
7599+
}
7600+
75727601
#[test]
75737602
fn resolve_from_classifies_existing_dockerfile_path() {
75747603
let temp = tempfile::tempdir().expect("failed to create tempdir");

‎crates/openshell-core/src/gpu.rs‎

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@
44
//! Shared GPU request helpers.
55
66
use crate::config::CDI_GPU_DEVICE_ALL;
7+
use crate::proto::compute::v1::GpuRequestSpec;
78

8-
/// Resolve the existing GPU request fields into CDI device identifiers.
9+
/// Resolve a driver GPU request into CDI device identifiers.
910
///
10-
/// `None` means no GPU was requested. A GPU request with no explicit device
11-
/// ID uses the CDI all-GPU request; otherwise the driver-native ID passes
12-
/// through unchanged.
11+
/// `None` means no GPU was requested. Presence with no explicit device IDs
12+
/// uses the CDI all-GPU request; otherwise the driver-native IDs pass through.
1313
#[must_use]
14-
pub fn cdi_gpu_device_ids(gpu: bool, gpu_device: &str) -> Option<Vec<String>> {
15-
gpu.then(|| {
16-
if gpu_device.is_empty() {
17-
vec![CDI_GPU_DEVICE_ALL.to_string()]
18-
} else {
19-
vec![gpu_device.to_string()]
20-
}
21-
})
14+
pub fn cdi_gpu_device_ids(gpu: Option<&GpuRequestSpec>) -> Option<Vec<String>> {
15+
match gpu {
16+
Some(gpu) if gpu.device_id.is_empty() => Some(vec![CDI_GPU_DEVICE_ALL.to_string()]),
17+
Some(gpu) => Some(gpu.device_id.clone()),
18+
None => None,
19+
}
2220
}
2321

2422
#[cfg(test)]
@@ -27,22 +25,46 @@ mod tests {
2725

2826
#[test]
2927
fn cdi_gpu_device_ids_returns_none_when_absent() {
30-
assert_eq!(cdi_gpu_device_ids(false, ""), None);
28+
assert_eq!(cdi_gpu_device_ids(None), None);
3129
}
3230

3331
#[test]
3432
fn cdi_gpu_device_ids_defaults_empty_request_to_all_gpus() {
33+
let request = GpuRequestSpec { device_id: vec![] };
34+
3535
assert_eq!(
36-
cdi_gpu_device_ids(true, ""),
36+
cdi_gpu_device_ids(Some(&request)),
3737
Some(vec![CDI_GPU_DEVICE_ALL.to_string()])
3838
);
3939
}
4040

4141
#[test]
42-
fn cdi_gpu_device_ids_passes_explicit_device_id_through() {
42+
fn cdi_gpu_device_ids_passes_single_device_id_through() {
43+
let request = GpuRequestSpec {
44+
device_id: vec!["nvidia.com/gpu=0".to_string()],
45+
};
46+
4347
assert_eq!(
44-
cdi_gpu_device_ids(true, "nvidia.com/gpu=0"),
48+
cdi_gpu_device_ids(Some(&request)),
4549
Some(vec!["nvidia.com/gpu=0".to_string()])
4650
);
4751
}
52+
53+
#[test]
54+
fn cdi_gpu_device_ids_passes_multiple_device_ids_through() {
55+
let request = GpuRequestSpec {
56+
device_id: vec![
57+
"nvidia.com/gpu=0".to_string(),
58+
"nvidia.com/gpu=1".to_string(),
59+
],
60+
};
61+
62+
assert_eq!(
63+
cdi_gpu_device_ids(Some(&request)),
64+
Some(vec![
65+
"nvidia.com/gpu=0".to_string(),
66+
"nvidia.com/gpu=1".to_string()
67+
])
68+
);
69+
}
4870
}

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ use openshell_core::proto::compute::v1::{
3232
CreateSandboxRequest, CreateSandboxResponse, DeleteSandboxRequest, DeleteSandboxResponse,
3333
DriverCondition, DriverPlatformEvent, DriverSandbox, DriverSandboxStatus,
3434
DriverSandboxTemplate, GetCapabilitiesRequest, GetCapabilitiesResponse, GetSandboxRequest,
35-
GetSandboxResponse, ListSandboxesRequest, ListSandboxesResponse, StopSandboxRequest,
36-
StopSandboxResponse, ValidateSandboxCreateRequest, ValidateSandboxCreateResponse,
37-
WatchSandboxesDeletedEvent, WatchSandboxesEvent, WatchSandboxesPlatformEvent,
38-
WatchSandboxesRequest, WatchSandboxesSandboxEvent, compute_driver_server::ComputeDriver,
39-
watch_sandboxes_event,
35+
GetSandboxResponse, GpuRequestSpec, ListSandboxesRequest, ListSandboxesResponse,
36+
StopSandboxRequest, StopSandboxResponse, ValidateSandboxCreateRequest,
37+
ValidateSandboxCreateResponse, WatchSandboxesDeletedEvent, WatchSandboxesEvent,
38+
WatchSandboxesPlatformEvent, WatchSandboxesRequest, WatchSandboxesSandboxEvent,
39+
compute_driver_server::ComputeDriver, watch_sandboxes_event,
4040
};
4141
use openshell_core::{Config, Error, Result as CoreResult};
4242
use std::collections::HashMap;
@@ -375,7 +375,7 @@ impl DockerComputeDriver {
375375
"docker sandboxes require a template image",
376376
));
377377
}
378-
Self::validate_gpu_request(spec.gpu, config.supports_gpu)?;
378+
Self::validate_gpu_request(spec.gpu.as_ref(), config.supports_gpu)?;
379379
if !template.agent_socket_path.trim().is_empty() {
380380
return Err(Status::failed_precondition(
381381
"docker compute driver does not support template.agent_socket_path",
@@ -409,8 +409,11 @@ impl DockerComputeDriver {
409409
))
410410
}
411411

412-
fn validate_gpu_request(gpu: bool, supports_gpu: bool) -> Result<(), Status> {
413-
if gpu && !supports_gpu {
412+
fn validate_gpu_request(
413+
gpu: Option<&GpuRequestSpec>,
414+
supports_gpu: bool,
415+
) -> Result<(), Status> {
416+
if gpu.is_some() && !supports_gpu {
414417
return Err(Status::failed_precondition(
415418
"docker GPU sandboxes require Docker CDI support. Enable CDI on the Docker daemon, then restart the OpenShell gateway/server so GPU capability is detected.",
416419
));
@@ -1727,8 +1730,8 @@ fn build_environment(sandbox: &DriverSandbox, config: &DockerDriverRuntimeConfig
17271730
.collect()
17281731
}
17291732

1730-
fn docker_gpu_device_requests(gpu: bool, gpu_device: &str) -> Option<Vec<DeviceRequest>> {
1731-
cdi_gpu_device_ids(gpu, gpu_device).map(|device_ids| {
1733+
fn docker_gpu_device_requests(gpu: Option<&GpuRequestSpec>) -> Option<Vec<DeviceRequest>> {
1734+
cdi_gpu_device_ids(gpu).map(|device_ids| {
17321735
vec![DeviceRequest {
17331736
driver: Some("cdi".to_string()),
17341737
device_ids: Some(device_ids),
@@ -1779,7 +1782,7 @@ fn build_container_create_body(
17791782
nano_cpus: resource_limits.nano_cpus,
17801783
memory: resource_limits.memory_bytes,
17811784
pids_limit: docker_pids_limit(config.sandbox_pids_limit)?,
1782-
device_requests: docker_gpu_device_requests(spec.gpu, &spec.gpu_device),
1785+
device_requests: docker_gpu_device_requests(spec.gpu.as_ref()),
17831786
binds: Some(build_binds(sandbox, config)?),
17841787
restart_policy: Some(RestartPolicy {
17851788
name: Some(RestartPolicyNameEnum::UNLESS_STOPPED),

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use openshell_core::progress::{
1313
PROGRESS_STEP_STARTING_SANDBOX,
1414
};
1515
use openshell_core::proto::compute::v1::{
16-
DriverResourceRequirements, DriverSandboxSpec, DriverSandboxTemplate,
16+
DriverResourceRequirements, DriverSandboxSpec, DriverSandboxTemplate, GpuRequestSpec,
1717
};
1818
use std::fs;
1919
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
@@ -40,8 +40,7 @@ fn test_sandbox() -> DriverSandbox {
4040
resources: None,
4141
platform_config: None,
4242
}),
43-
gpu: false,
44-
gpu_device: String::new(),
43+
gpu: None,
4544
sandbox_token: String::new(),
4645
}),
4746
status: None,
@@ -568,7 +567,7 @@ fn build_container_create_body_clears_inherited_cmd() {
568567
fn validate_sandbox_rejects_gpu_when_cdi_unavailable() {
569568
let config = runtime_config();
570569
let mut sandbox = test_sandbox();
571-
sandbox.spec.as_mut().unwrap().gpu = true;
570+
sandbox.spec.as_mut().unwrap().gpu = Some(GpuRequestSpec { device_id: vec![] });
572571

573572
let err = DockerComputeDriver::validate_sandbox(&sandbox, &config).unwrap_err();
574573

@@ -603,7 +602,7 @@ fn build_container_create_body_maps_gpu_to_all_cdi_device() {
603602
let mut config = runtime_config();
604603
config.supports_gpu = true;
605604
let mut sandbox = test_sandbox();
606-
sandbox.spec.as_mut().unwrap().gpu = true;
605+
sandbox.spec.as_mut().unwrap().gpu = Some(GpuRequestSpec { device_id: vec![] });
607606

608607
let create_body = build_container_create_body(&sandbox, &config).unwrap();
609608
let request = create_body
@@ -621,13 +620,16 @@ fn build_container_create_body_maps_gpu_to_all_cdi_device() {
621620
}
622621

623622
#[test]
624-
fn build_container_create_body_passes_explicit_cdi_device_id_through() {
623+
fn build_container_create_body_passes_explicit_cdi_device_ids_through() {
625624
let mut config = runtime_config();
626625
config.supports_gpu = true;
627626
let mut sandbox = test_sandbox();
628-
let spec = sandbox.spec.as_mut().unwrap();
629-
spec.gpu = true;
630-
spec.gpu_device = "nvidia.com/gpu=0".to_string();
627+
sandbox.spec.as_mut().unwrap().gpu = Some(GpuRequestSpec {
628+
device_id: vec![
629+
"nvidia.com/gpu=0".to_string(),
630+
"nvidia.com/gpu=1".to_string(),
631+
],
632+
});
631633

632634
let create_body = build_container_create_body(&sandbox, &config).unwrap();
633635
let request = create_body
@@ -640,7 +642,10 @@ fn build_container_create_body_passes_explicit_cdi_device_id_through() {
640642
assert_eq!(request.driver.as_deref(), Some("cdi"));
641643
assert_eq!(
642644
request.device_ids.as_ref().unwrap(),
643-
&vec!["nvidia.com/gpu=0".to_string()]
645+
&vec![
646+
"nvidia.com/gpu=0".to_string(),
647+
"nvidia.com/gpu=1".to_string()
648+
]
644649
);
645650
}
646651

0 commit comments

Comments
 (0)