Skip to content

Commit 055ddf1

Browse files
committed
feat(cli): add gpu count requests
Signed-off-by: Evan Lezar <elezar@nvidia.com>
1 parent f1335d2 commit 055ddf1

18 files changed

Lines changed: 407 additions & 26 deletions

File tree

‎architecture/compute-runtimes.md‎

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

8789
## Deployment Shape
8890

‎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>,
@@ -2548,6 +2552,7 @@ async fn main() -> Result<()> {
25482552
editor,
25492553
gpu,
25502554
gpu_device,
2555+
gpu_count,
25512556
cpu,
25522557
memory,
25532558
driver_config_json,
@@ -2629,6 +2634,7 @@ async fn main() -> Result<()> {
26292634
keep,
26302635
gpu,
26312636
gpu_device.as_deref(),
2637+
gpu_count,
26322638
cpu.as_deref(),
26332639
memory.as_deref(),
26342640
driver_config_json.as_deref(),
@@ -4398,6 +4404,52 @@ mod tests {
43984404
}
43994405
}
44004406

4407+
#[test]
4408+
fn sandbox_create_gpu_count_parses_without_gpu_flag() {
4409+
let cli = Cli::try_parse_from(["openshell", "sandbox", "create", "--gpu-count", "2"])
4410+
.expect("sandbox create --gpu-count should parse");
4411+
4412+
match cli.command {
4413+
Some(Commands::Sandbox {
4414+
command: Some(SandboxCommands::Create { gpu, gpu_count, .. }),
4415+
..
4416+
}) => {
4417+
assert!(!gpu);
4418+
assert_eq!(gpu_count, Some(2));
4419+
}
4420+
other => panic!("expected SandboxCommands::Create, got: {other:?}"),
4421+
}
4422+
}
4423+
4424+
#[test]
4425+
fn sandbox_create_gpu_count_rejects_zero() {
4426+
let result = Cli::try_parse_from(["openshell", "sandbox", "create", "--gpu-count", "0"]);
4427+
4428+
assert!(
4429+
result.is_err(),
4430+
"sandbox create --gpu-count 0 should be rejected"
4431+
);
4432+
}
4433+
4434+
#[test]
4435+
fn sandbox_create_gpu_count_conflicts_with_gpu_device() {
4436+
let result = Cli::try_parse_from([
4437+
"openshell",
4438+
"sandbox",
4439+
"create",
4440+
"--gpu",
4441+
"--gpu-device",
4442+
"nvidia.com/gpu=0",
4443+
"--gpu-count",
4444+
"2",
4445+
]);
4446+
4447+
assert!(
4448+
result.is_err(),
4449+
"sandbox create should reject --gpu-count with --gpu-device"
4450+
);
4451+
}
4452+
44014453
#[test]
44024454
fn service_expose_accepts_positional_target_port_and_service() {
44034455
let cli = Cli::try_parse_from([

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ pub async fn sandbox_create(
17451745
keep: bool,
17461746
gpu: bool,
17471747
gpu_device: Option<&str>,
1748+
gpu_count: Option<u32>,
17481749
cpu: Option<&str>,
17491750
memory: Option<&str>,
17501751
driver_config_json: Option<&str>,
@@ -1801,6 +1802,7 @@ pub async fn sandbox_create(
18011802
};
18021803
let requested_gpu = gpu
18031804
|| gpu_device.is_some_and(|device_id| !device_id.is_empty())
1805+
|| gpu_count.is_some()
18041806
|| image.as_deref().is_some_and(image_requests_gpu);
18051807

18061808
let providers_v2_enabled = gateway_providers_v2_enabled(&mut client).await?;
@@ -1838,6 +1840,7 @@ pub async fn sandbox_create(
18381840
spec: Some(SandboxSpec {
18391841
gpu: requested_gpu,
18401842
gpu_device: gpu_device.unwrap_or_default().to_string(),
1843+
gpu_count,
18411844
policy,
18421845
providers: configured_providers,
18431846
template,

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ async fn sandbox_create_keeps_command_sessions_by_default() {
788788
None,
789789
None,
790790
None,
791+
None,
791792
&[],
792793
None,
793794
None,
@@ -827,6 +828,7 @@ async fn sandbox_create_sends_cpu_and_memory_limits_only() {
827828
true,
828829
false,
829830
None,
831+
None,
830832
Some("500m"),
831833
Some("2Gi"),
832834
None,
@@ -908,6 +910,7 @@ async fn sandbox_create_sends_gpu_device_request_without_gpu_flag() {
908910
None,
909911
None,
910912
None,
913+
None,
911914
&[],
912915
None,
913916
None,
@@ -931,6 +934,53 @@ async fn sandbox_create_sends_gpu_device_request_without_gpu_flag() {
931934
assert_eq!(spec.gpu_device, "nvidia.com/gpu=0");
932935
}
933936

937+
#[tokio::test]
938+
async fn sandbox_create_sends_gpu_count_request_without_gpu_flag() {
939+
let server = run_server().await;
940+
let fake_ssh_dir = tempfile::tempdir().unwrap();
941+
let xdg_dir = tempfile::tempdir().unwrap();
942+
let _env = test_env(&fake_ssh_dir, &xdg_dir);
943+
let tls = test_tls(&server);
944+
install_fake_ssh(&fake_ssh_dir);
945+
946+
run::sandbox_create(
947+
&server.endpoint,
948+
Some("gpu-count"),
949+
None,
950+
"openshell",
951+
&[],
952+
true,
953+
false,
954+
None,
955+
Some(2),
956+
None,
957+
None,
958+
None,
959+
None,
960+
&[],
961+
None,
962+
None,
963+
&["echo".to_string(), "OK".to_string()],
964+
Some(false),
965+
Some(false),
966+
&HashMap::new(),
967+
"manual",
968+
&tls,
969+
)
970+
.await
971+
.expect("sandbox create should succeed");
972+
973+
let requests = create_requests(&server).await;
974+
let spec = requests[0]
975+
.spec
976+
.as_ref()
977+
.expect("sandbox spec should be sent");
978+
979+
assert!(spec.gpu);
980+
assert_eq!(spec.gpu_count, Some(2));
981+
assert!(spec.gpu_device.is_empty());
982+
}
983+
934984
#[tokio::test]
935985
async fn sandbox_create_sends_driver_config_json() {
936986
let server = run_server().await;
@@ -951,6 +1001,7 @@ async fn sandbox_create_sends_driver_config_json() {
9511001
None,
9521002
None,
9531003
None,
1004+
None,
9541005
Some(r#"{"kubernetes":{"pod":{"priority_class_name":"batch-low"}}}"#),
9551006
None,
9561007
&[],
@@ -1027,6 +1078,7 @@ async fn sandbox_create_does_not_infer_command_providers_when_v2_enabled() {
10271078
None,
10281079
None,
10291080
None,
1081+
None,
10301082
&[],
10311083
None,
10321084
None,
@@ -1085,6 +1137,7 @@ async fn sandbox_create_returns_vm_error_without_waiting_for_timeout() {
10851137
None,
10861138
None,
10871139
None,
1140+
None,
10881141
&[],
10891142
None,
10901143
None,
@@ -1139,6 +1192,7 @@ async fn sandbox_create_keeps_waiting_while_vm_progress_arrives() {
11391192
None,
11401193
None,
11411194
None,
1195+
None,
11421196
&[],
11431197
None,
11441198
None,
@@ -1185,6 +1239,7 @@ async fn sandbox_create_times_out_when_only_logs_arrive() {
11851239
None,
11861240
None,
11871241
None,
1242+
None,
11881243
&[],
11891244
None,
11901245
None,
@@ -1227,6 +1282,7 @@ async fn sandbox_create_deletes_command_sessions_with_no_keep() {
12271282
None,
12281283
None,
12291284
None,
1285+
None,
12301286
&[],
12311287
None,
12321288
None,
@@ -1273,6 +1329,7 @@ async fn sandbox_create_deletes_shell_sessions_with_no_keep() {
12731329
None,
12741330
None,
12751331
None,
1332+
None,
12761333
&[],
12771334
None,
12781335
None,
@@ -1319,6 +1376,7 @@ async fn sandbox_create_keeps_sandbox_with_hidden_keep_flag() {
13191376
None,
13201377
None,
13211378
None,
1379+
None,
13221380
&[],
13231381
None,
13241382
None,
@@ -1365,6 +1423,7 @@ async fn sandbox_create_keeps_sandbox_with_forwarding() {
13651423
None,
13661424
None,
13671425
None,
1426+
None,
13681427
&[],
13691428
None,
13701429
Some(openshell_core::forward::ForwardSpec::new(forward_port)),

‎crates/openshell-driver-docker/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ contract:
3232
| `apparmor=unconfined` | Avoids Docker's default profile blocking required mount operations. |
3333
| `restart_policy = unless-stopped` | Keeps managed sandboxes resumable across daemon or gateway restarts. |
3434
| `PidsLimit` | Enforces the sandbox PID budget at the Docker cgroup layer. Set `[openshell.drivers.docker].sandbox_pids_limit = 0` to inherit the Docker/runtime default. |
35-
| CDI GPU request | Uses the sandbox `gpu_device` value when set; otherwise requests all NVIDIA GPUs when the sandbox spec asks for GPU support and daemon CDI support is detected. |
35+
| CDI GPU request | Uses the sandbox `gpu_device` value when set; otherwise requests all NVIDIA GPUs when the sandbox spec asks for GPU support and daemon CDI support is detected. Count-based GPU requests are rejected. |
3636

3737
The agent child process does not retain these supervisor privileges.
3838

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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, spec.gpu_count, 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,7 +409,17 @@ impl DockerComputeDriver {
409409
))
410410
}
411411

412-
fn validate_gpu_request(gpu: bool, supports_gpu: bool) -> Result<(), Status> {
412+
fn validate_gpu_request(
413+
gpu: bool,
414+
gpu_count: Option<u32>,
415+
supports_gpu: bool,
416+
) -> Result<(), Status> {
417+
if gpu_count.is_some() {
418+
return Err(Status::invalid_argument(
419+
"docker GPU count requests are not supported; use --gpu or --gpu-device",
420+
));
421+
}
422+
413423
if gpu && !supports_gpu {
414424
return Err(Status::failed_precondition(
415425
"docker GPU sandboxes require Docker CDI support. Enable CDI on the Docker daemon, then restart the OpenShell gateway/server so GPU capability is detected.",

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn test_sandbox() -> DriverSandbox {
4343
}),
4444
gpu: false,
4545
gpu_device: String::new(),
46+
gpu_count: None,
4647
sandbox_token: String::new(),
4748
}),
4849
status: None,
@@ -612,6 +613,24 @@ fn validate_sandbox_rejects_gpu_when_cdi_unavailable() {
612613
assert!(err.message().contains("Docker CDI"));
613614
}
614615

616+
#[test]
617+
fn validate_sandbox_rejects_gpu_count_request() {
618+
let mut config = runtime_config();
619+
config.supports_gpu = true;
620+
let mut sandbox = test_sandbox();
621+
let spec = sandbox.spec.as_mut().unwrap();
622+
spec.gpu = true;
623+
spec.gpu_count = Some(2);
624+
625+
let err = DockerComputeDriver::validate_sandbox(&sandbox, &config).unwrap_err();
626+
627+
assert_eq!(err.code(), tonic::Code::InvalidArgument);
628+
assert!(
629+
err.message()
630+
.contains("GPU count requests are not supported")
631+
);
632+
}
633+
615634
#[test]
616635
fn validate_sandbox_auth_requires_gateway_token() {
617636
let mut sandbox = test_sandbox();

‎crates/openshell-driver-kubernetes/README.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ the supervisor's network namespace mount setup on AppArmor-enabled nodes.
6262
## GPU Support
6363

6464
When a sandbox requests GPU support, the driver checks node allocatable capacity
65-
for `nvidia.com/gpu` and requests one GPU resource in the workload spec. The
66-
sandbox image must provide the user-space libraries needed by the agent
67-
workload.
65+
for `nvidia.com/gpu` and requests the configured GPU count in the workload spec.
66+
When no count is set, the driver requests one GPU resource. The sandbox image
67+
must provide the user-space libraries needed by the agent workload.
6868

6969
## Driver Config POC
7070

@@ -97,5 +97,5 @@ POC parser renders the keys listed above and ignores unknown fields.
9797
`pod.runtime_class_name` maps to PodSpec `runtimeClassName` and overrides the
9898
driver's configured `default_runtime_class_name`; the typed public
9999
`SandboxTemplate.runtime_class_name` still takes precedence when set. Use the
100-
public `gpu` flag for the default GPU request and `driver_config` only for
101-
additional driver-owned resource details.
100+
public `gpu` flag for the default GPU request, `gpu_count` for counted GPU
101+
requests, and `driver_config` only for additional driver-owned resource details.

0 commit comments

Comments
 (0)