Skip to content

Commit 9dcc269

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

20 files changed

Lines changed: 704 additions & 177 deletions

File tree

‎architecture/compute-runtimes.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ users.
7777
Custom sandbox images must include the agent runtime and any system
7878
dependencies, but they should not need to include the gateway. GPU-capable
7979
images must include the user-space libraries required by the workload. The
80-
runtime still owns GPU device injection.
80+
runtime still owns GPU device injection. GPU requests can include explicit
81+
driver-native device IDs or a requested count; the gateway validates the public
82+
request shape and each runtime enforces the GPU allocation modes it supports.
8183

8284
## Deployment Shape
8385

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,9 +1217,13 @@ enum SandboxCommands {
12171217
/// (for example "nvidia.com/gpu=0"); VM uses a PCI BDF or index.
12181218
/// Specifying --gpu-device also requests GPU resources.
12191219
/// When omitted with --gpu, the driver uses its default GPU selection.
1220-
#[arg(long)]
1220+
#[arg(long, conflicts_with = "gpu_count")]
12211221
gpu_device: Option<String>,
12221222

1223+
/// Request a specific number of GPUs. Mutually exclusive with --gpu-device.
1224+
#[arg(long, value_parser = clap::value_parser!(u32).range(1..), conflicts_with = "gpu_device")]
1225+
gpu_count: Option<u32>,
1226+
12231227
/// CPU limit for the sandbox (for example: 500m, 1, 2.5).
12241228
#[arg(long)]
12251229
cpu: Option<String>,
@@ -2540,6 +2544,7 @@ async fn main() -> Result<()> {
25402544
editor,
25412545
gpu,
25422546
gpu_device,
2547+
gpu_count,
25432548
cpu,
25442549
memory,
25452550
providers,
@@ -2609,6 +2614,7 @@ async fn main() -> Result<()> {
26092614
keep,
26102615
gpu,
26112616
gpu_device.as_deref(),
2617+
gpu_count,
26122618
cpu.as_deref(),
26132619
memory.as_deref(),
26142620
editor,
@@ -4314,6 +4320,52 @@ mod tests {
43144320
}
43154321
}
43164322

4323+
#[test]
4324+
fn sandbox_create_gpu_count_parses_without_gpu_flag() {
4325+
let cli = Cli::try_parse_from(["openshell", "sandbox", "create", "--gpu-count", "2"])
4326+
.expect("sandbox create --gpu-count should parse");
4327+
4328+
match cli.command {
4329+
Some(Commands::Sandbox {
4330+
command: Some(SandboxCommands::Create { gpu, gpu_count, .. }),
4331+
..
4332+
}) => {
4333+
assert!(!gpu);
4334+
assert_eq!(gpu_count, Some(2));
4335+
}
4336+
other => panic!("expected SandboxCommands::Create, got: {other:?}"),
4337+
}
4338+
}
4339+
4340+
#[test]
4341+
fn sandbox_create_gpu_count_rejects_zero() {
4342+
let result = Cli::try_parse_from(["openshell", "sandbox", "create", "--gpu-count", "0"]);
4343+
4344+
assert!(
4345+
result.is_err(),
4346+
"sandbox create --gpu-count 0 should be rejected"
4347+
);
4348+
}
4349+
4350+
#[test]
4351+
fn sandbox_create_gpu_count_conflicts_with_gpu_device() {
4352+
let result = Cli::try_parse_from([
4353+
"openshell",
4354+
"sandbox",
4355+
"create",
4356+
"--gpu",
4357+
"--gpu-device",
4358+
"nvidia.com/gpu=0",
4359+
"--gpu-count",
4360+
"2",
4361+
]);
4362+
4363+
assert!(
4364+
result.is_err(),
4365+
"sandbox create should reject --gpu-count with --gpu-device"
4366+
);
4367+
}
4368+
43174369
#[test]
43184370
fn service_expose_accepts_positional_target_port_and_service() {
43194371
let cli = Cli::try_parse_from([

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

Lines changed: 73 additions & 17 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};
@@ -1679,6 +1680,7 @@ pub async fn sandbox_create(
16791680
keep: bool,
16801681
gpu: bool,
16811682
gpu_device: Option<&str>,
1683+
gpu_count: Option<u32>,
16821684
cpu: Option<&str>,
16831685
memory: Option<&str>,
16841686
editor: Option<Editor>,
@@ -1734,6 +1736,7 @@ pub async fn sandbox_create(
17341736
};
17351737
let requested_gpu = gpu
17361738
|| gpu_device.is_some_and(|device_id| !device_id.is_empty())
1739+
|| gpu_count.is_some()
17371740
|| image.as_deref().is_some_and(image_requests_gpu);
17381741

17391742
let providers_v2_enabled = gateway_providers_v2_enabled(&mut client).await?;
@@ -1765,8 +1768,7 @@ pub async fn sandbox_create(
17651768

17661769
let request = CreateSandboxRequest {
17671770
spec: Some(SandboxSpec {
1768-
gpu: requested_gpu,
1769-
gpu_device: gpu_device.unwrap_or_default().to_string(),
1771+
gpu: gpu_request_from_cli(requested_gpu, gpu_device, gpu_count),
17701772
policy,
17711773
providers: configured_providers,
17721774
template,
@@ -2191,6 +2193,20 @@ pub async fn sandbox_create(
21912193
}
21922194
}
21932195

2196+
fn gpu_request_from_cli(
2197+
requested_gpu: bool,
2198+
gpu_device: Option<&str>,
2199+
gpu_count: Option<u32>,
2200+
) -> Option<GpuRequestSpec> {
2201+
requested_gpu.then(|| GpuRequestSpec {
2202+
device_id: gpu_device
2203+
.filter(|device_id| !device_id.is_empty())
2204+
.map(|device_id| vec![device_id.to_string()])
2205+
.unwrap_or_default(),
2206+
count: gpu_count,
2207+
})
2208+
}
2209+
21942210
/// Resolved source for the `--from` flag on `sandbox create`.
21952211
#[derive(Debug)]
21962212
enum ResolvedSource {
@@ -7440,10 +7456,10 @@ mod tests {
74407456
dockerfile_sources_supported_for_gateway, format_endpoint, format_gateway_select_header,
74417457
format_gateway_select_items, format_provider_attachment_table, gateway_add,
74427458
gateway_auth_label, gateway_env_override_warning, gateway_select_with, gateway_type_label,
7443-
git_sync_files, http_health_check, image_requests_gpu, import_local_package_mtls_bundle,
7444-
inferred_provider_type, package_managed_tls_dirs, parse_cli_setting_value,
7445-
parse_credential_expiry_cli_value, parse_credential_expiry_pairs, parse_credential_pairs,
7446-
plaintext_gateway_is_remote, progress_step_from_metadata,
7459+
git_sync_files, gpu_request_from_cli, http_health_check, image_requests_gpu,
7460+
import_local_package_mtls_bundle, inferred_provider_type, package_managed_tls_dirs,
7461+
parse_cli_setting_value, parse_credential_expiry_cli_value, parse_credential_expiry_pairs,
7462+
parse_credential_pairs, plaintext_gateway_is_remote, progress_step_from_metadata,
74477463
provider_profile_allows_refresh_bootstrap, provisioning_timeout_message,
74487464
ready_false_condition_message, refresh_status_header, refresh_status_row, resolve_from,
74497465
sandbox_should_persist, sandbox_upload_plan, service_expose_status_error,
@@ -7926,6 +7942,46 @@ mod tests {
79267942
}
79277943
}
79287944

7945+
#[test]
7946+
fn gpu_request_from_cli_uses_presence_with_empty_device_ids_for_default_gpu() {
7947+
let request =
7948+
gpu_request_from_cli(true, None, None).expect("gpu request should be present");
7949+
7950+
assert!(request.device_id.is_empty());
7951+
assert_eq!(request.count, None);
7952+
}
7953+
7954+
#[test]
7955+
fn gpu_request_from_cli_maps_gpu_device_to_one_device_id() {
7956+
let request = gpu_request_from_cli(true, Some("0000:2d:00.0"), None)
7957+
.expect("gpu request should be present");
7958+
7959+
assert_eq!(request.device_id, vec!["0000:2d:00.0"]);
7960+
assert_eq!(request.count, None);
7961+
}
7962+
7963+
#[test]
7964+
fn gpu_request_from_cli_maps_gpu_count() {
7965+
let request = gpu_request_from_cli(true, None, Some(2)).expect("gpu request should exist");
7966+
7967+
assert!(request.device_id.is_empty());
7968+
assert_eq!(request.count, Some(2));
7969+
}
7970+
7971+
#[test]
7972+
fn gpu_request_from_cli_preserves_device_and_gpu_count_for_gateway_validation() {
7973+
let request = gpu_request_from_cli(true, Some("nvidia.com/gpu=0"), Some(2))
7974+
.expect("gpu request should exist");
7975+
7976+
assert_eq!(request.device_id, vec!["nvidia.com/gpu=0"]);
7977+
assert_eq!(request.count, Some(2));
7978+
}
7979+
7980+
#[test]
7981+
fn gpu_request_from_cli_omits_gpu_request_when_not_requested() {
7982+
assert!(gpu_request_from_cli(false, Some("0"), None).is_none());
7983+
}
7984+
79297985
#[test]
79307986
fn resolve_from_classifies_existing_dockerfile_path() {
79317987
let temp = tempfile::tempdir().expect("failed to create tempdir");

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ async fn sandbox_create_keeps_command_sessions_by_default() {
787787
None,
788788
None,
789789
None,
790+
None,
790791
&[],
791792
None,
792793
None,
@@ -826,6 +827,7 @@ async fn sandbox_create_sends_cpu_and_memory_limits_only() {
826827
true,
827828
false,
828829
None,
830+
None,
829831
Some("500m"),
830832
Some("2Gi"),
831833
None,
@@ -919,13 +921,60 @@ async fn sandbox_create_sends_gpu_device_request_without_gpu_flag() {
919921
.expect("sandbox create should succeed");
920922

921923
let requests = create_requests(&server).await;
922-
let spec = requests[0]
924+
let gpu = requests[0]
925+
.spec
926+
.as_ref()
927+
.and_then(|spec| spec.gpu.as_ref())
928+
.expect("GPU request should be sent");
929+
930+
assert_eq!(gpu.device_id, vec!["nvidia.com/gpu=0"]);
931+
assert_eq!(gpu.count, None);
932+
}
933+
934+
#[tokio::test]
935+
async fn sandbox_create_sends_gpu_count_request() {
936+
let server = run_server().await;
937+
let fake_ssh_dir = tempfile::tempdir().unwrap();
938+
let xdg_dir = tempfile::tempdir().unwrap();
939+
let _env = test_env(&fake_ssh_dir, &xdg_dir);
940+
let tls = test_tls(&server);
941+
install_fake_ssh(&fake_ssh_dir);
942+
943+
run::sandbox_create(
944+
&server.endpoint,
945+
Some("gpu-count"),
946+
None,
947+
"openshell",
948+
None,
949+
true,
950+
false,
951+
None,
952+
Some(2),
953+
None,
954+
None,
955+
None,
956+
&[],
957+
None,
958+
None,
959+
&["echo".to_string(), "OK".to_string()],
960+
Some(false),
961+
Some(false),
962+
&HashMap::new(),
963+
"manual",
964+
&tls,
965+
)
966+
.await
967+
.expect("sandbox create should succeed");
968+
969+
let requests = create_requests(&server).await;
970+
let gpu = requests[0]
923971
.spec
924972
.as_ref()
925-
.expect("sandbox spec should be sent");
973+
.and_then(|spec| spec.gpu.as_ref())
974+
.expect("GPU request should be sent");
926975

927-
assert!(spec.gpu);
928-
assert_eq!(spec.gpu_device, "nvidia.com/gpu=0");
976+
assert!(gpu.device_id.is_empty());
977+
assert_eq!(gpu.count, Some(2));
929978
}
930979

931980
#[tokio::test]
@@ -950,6 +999,7 @@ async fn sandbox_create_does_not_infer_command_providers_when_v2_enabled() {
950999
None,
9511000
None,
9521001
None,
1002+
None,
9531003
&[],
9541004
None,
9551005
None,
@@ -1007,6 +1057,7 @@ async fn sandbox_create_returns_vm_error_without_waiting_for_timeout() {
10071057
None,
10081058
None,
10091059
None,
1060+
None,
10101061
&[],
10111062
None,
10121063
None,
@@ -1060,6 +1111,7 @@ async fn sandbox_create_keeps_waiting_while_vm_progress_arrives() {
10601111
None,
10611112
None,
10621113
None,
1114+
None,
10631115
&[],
10641116
None,
10651117
None,
@@ -1105,6 +1157,7 @@ async fn sandbox_create_times_out_when_only_logs_arrive() {
11051157
None,
11061158
None,
11071159
None,
1160+
None,
11081161
&[],
11091162
None,
11101163
None,
@@ -1146,6 +1199,7 @@ async fn sandbox_create_deletes_command_sessions_with_no_keep() {
11461199
None,
11471200
None,
11481201
None,
1202+
None,
11491203
&[],
11501204
None,
11511205
None,
@@ -1191,6 +1245,7 @@ async fn sandbox_create_deletes_shell_sessions_with_no_keep() {
11911245
None,
11921246
None,
11931247
None,
1248+
None,
11941249
&[],
11951250
None,
11961251
None,
@@ -1236,6 +1291,7 @@ async fn sandbox_create_keeps_sandbox_with_hidden_keep_flag() {
12361291
None,
12371292
None,
12381293
None,
1294+
None,
12391295
&[],
12401296
None,
12411297
None,
@@ -1281,6 +1337,7 @@ async fn sandbox_create_keeps_sandbox_with_forwarding() {
12811337
None,
12821338
None,
12831339
None,
1340+
None,
12841341
&[],
12851342
None,
12861343
Some(openshell_core::forward::ForwardSpec::new(forward_port)),

0 commit comments

Comments
 (0)