Skip to content
Merged
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
25 changes: 25 additions & 0 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,31 @@ The peak may include other jobs, display usage, and driver usage. To estimate
a job's own usage, sample it on an otherwise idle GPU. If sampling fails the
peak is empty. Unpinned fallback jobs record `vram_peak_scope=unavailable`.

**Reading the budget inside the job (#185).** A gpu job container gets its
share as environment variables, set on the container and never written to the
JIT credential file:

| Variable | Pinned job | Unpinned fallback job | cpu job |
|---|---|---|---|
| `RUNNER_VRAM_GB` | GiB charged on its GPU | `unknown` | not set |
| `RUNNER_VRAM_TOTAL_GB` | GiB of that card, rounded down | not set | not set |
| `NVIDIA_VISIBLE_DEVICES` | the pinned GPU index | image default | image default |

A step can turn the budget into a fraction of the card it sees, for example
with PyTorch:

```python
import os, torch
total = int(os.environ["RUNNER_VRAM_TOTAL_GB"])
share = int(os.environ["RUNNER_VRAM_GB"])
torch.cuda.set_per_process_memory_fraction(share / total, device=0)
```

This is cooperative, not a limit (see below). Guard for `unknown` or an unset
variable and skip the call there. Steps that run as separate containers through
the host Docker socket do not inherit these variables: pass them on with
`docker run -e RUNNER_VRAM_GB -e RUNNER_VRAM_TOTAL_GB -e NVIDIA_VISIBLE_DEVICES ...`.

```sh
sudo install -m 0644 deploy/runner-types.sample.yaml \
/etc/github-runner-listener/runner-types.yaml
Expand Down
8 changes: 8 additions & 0 deletions doc/changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- **The charged VRAM reaches the gpu job container** (#185): the container
engine argv carries `RUNNER_VRAM_GB` (the charged GiB, or `unknown` for an
unpinned fail-safe job) and `RUNNER_VRAM_TOTAL_GB` (the card GiB) when set.
`RUNNER_VRAM_TOTAL_GB` is delivered only with a known (numeric) budget.
Neither is written to the JIT env-file; a cpu job gets neither. The
listener provisioner exports `RUNNER_VRAM_TOTAL_GB` (the pinned card's
total, floored to whole GiB; empty otherwise). `deploy/README.md` shows how
a step reads them.
- **GPU jobs declare their VRAM and share a GPU** (ADR-0007, #180): a gpu type
with `concurrency: mode: vram` admits a job that adds `vram-gb-<N>` or
`vram-gb-all` to `runs-on` on the first GPU where the committed VRAM plus its
Expand Down
15 changes: 15 additions & 0 deletions lib/runner-container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
# non-empty value adds `--env NVIDIA_VISIBLE_DEVICES=<index>` to override it.
# EMPTY (every cpu job, and the GPU fail-safe) adds nothing.
: "${RUNNER_GPU:=}"
# #185: charged VRAM (GiB or "unknown") and the card total (GiB), passed to the
# job container as RUNNER_VRAM_GB / RUNNER_VRAM_TOTAL_GB; empty adds no flag.
: "${RUNNER_VRAM_GB:=}"
: "${RUNNER_VRAM_TOTAL_GB:=}"

# The runner's home INSIDE the image: where the bundled actions/runner (run.sh,
# config.sh, bin/) lives and where the job runs from. Both the self-built images
Expand Down Expand Up @@ -245,6 +249,17 @@ runner_container_run() {
if [[ -n "${RUNNER_GPU}" ]]; then
gpu_args=(--env "NVIDIA_VISIBLE_DEVICES=${RUNNER_GPU}")
fi
# Charged VRAM budget and card total (#185): engine flags on the argv, never
# the env-file. Independent of RUNNER_GPU so "unknown" (unpinned) is delivered.
if [[ -n "${RUNNER_VRAM_GB}" ]]; then
gpu_args+=(--env "RUNNER_VRAM_GB=${RUNNER_VRAM_GB}")
fi
# The card total only goes with a known budget: "unknown" or no budget means
# there is nothing to take a fraction of, so no total is delivered.
if [[ -n "${RUNNER_VRAM_GB}" && "${RUNNER_VRAM_GB}" != unknown &&
-n "${RUNNER_VRAM_TOTAL_GB}" ]]; then
gpu_args+=(--env "RUNNER_VRAM_TOTAL_GB=${RUNNER_VRAM_TOTAL_GB}")
fi
# Deliver the single-use JIT credential to the container OFF the host argv
# (#136, restores the ADR-0003 invariant). Splicing `--jitconfig "${encoded}"`
# directly onto the host `<cli> run ...` argv put the credential in the HOST
Expand Down
20 changes: 14 additions & 6 deletions listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type ProvisionRequest struct {
// VRAMGB is the VRAM, in GiB, charged for the job on that GPU; 0 when
// not pinned. Recorded in the job's ledger record.
VRAMGB int64
// VRAMTotalMiB is the total memory, in MiB, of the card the job is
// pinned to (#185); 0 when not pinned.
VRAMTotalMiB int64
}

// Provisioner runs one ephemeral job in a throwaway container. The production
Expand Down Expand Up @@ -525,10 +528,10 @@ func (l *Listener) reconcile(ctx context.Context, stats *scaleset.RunnerScaleSet
return nil
}

gpu, minor, vramGB := -1, -1, int64(0)
gpu, minor, vramGB, vramTotalMiB := -1, -1, int64(0), int64(0)
if l.gpuProbe != nil {
var ok bool
gpu, minor, vramGB, ok = l.admitGPU(ctx, labels)
gpu, minor, vramGB, vramTotalMiB, ok = l.admitGPU(ctx, labels)
if !ok || ctx.Err() != nil {
<-l.sem
return nil
Expand Down Expand Up @@ -566,6 +569,7 @@ func (l *Listener) reconcile(ctx context.Context, stats *scaleset.RunnerScaleSet
GPU: gpu,
GPUFallback: l.gpuProbe != nil && gpu < 0,
VRAMGB: vramGB,
VRAMTotalMiB: vramTotalMiB,
},
}
if l.cfg.DockerSocket != "" {
Expand Down Expand Up @@ -924,15 +928,18 @@ func (l *Listener) committedSize() jobSize {
// admitGPU is the per-job shared-GPU gate (#180, ADR-0007): it blocks until a
// GPU can take the job's declared VRAM (pickGPU), re-probing after the settle
// window while none can, and returns the GPU index, device minor and GiB to
// charge there. It returns false only when the context is cancelled.
// charge there, plus that card's total MiB (#185; 0 when not pinned). The total
// is taken from the selected reading under the same lock, never looked up later
// in a snapshot the capacity reporter may have replaced. It returns false only
// when the context is cancelled.
//
// It fails safe when the probe errors: whole-GPU, one job per GPU, over the
// last good GPU list (only a GPU with nothing committed takes a job, charged
// its whole budget). With no reading ever, it admits only while nothing is in
// flight, and the unpinned job holds the host until it exits.
// Run serializes dispatch, so only releases can change commitments between
// this selection and chargeGPU.
func (l *Listener) admitGPU(ctx context.Context, labels []string) (gpu, minor int, gb int64, ok bool) {
func (l *Listener) admitGPU(ctx context.Context, labels []string) (gpu, minor int, gb, totalMiB int64, ok bool) {
for {
want := int64(jobVRAM(labels, int(wholeGPU)))
gpus, err := l.gpuProbe.Probe(ctx)
Expand All @@ -947,18 +954,19 @@ func (l *Listener) admitGPU(ctx context.Context, labels []string) (gpu, minor in
case len(gpus) > 0:
selected, cost, fits := pickGPU(gpus, l.committedVRAM, want, l.reserve)
gpu, minor, gb, ok = selected.Index, selected.Minor, cost, fits
totalMiB = selected.TotalMiB
case l.inFlight.Load() == 0:
gpu, minor, ok = -1, -1, true
}
}
l.vramMu.Unlock()
if ok {
return gpu, minor, gb, true
return gpu, minor, gb, totalMiB, true
}
select {
case <-time.After(l.settle):
case <-ctx.Done():
return 0, 0, 0, false
return 0, 0, 0, 0, false
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions listener/provision-job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
# empty = not pinned
# RUNNER_VRAM_GB VRAM GiB charged, or "unknown" for an unpinned
# vram-mode job
# RUNNER_VRAM_TOTAL_GB whole GiB of the pinned card (#185); empty when
# not pinned. Both reach the job container as env of
# the same names, by case:
# pinned job: RUNNER_VRAM_GB=<GiB> and
# RUNNER_VRAM_TOTAL_GB=<GiB>
# unpinned fallback: RUNNER_VRAM_GB=unknown, no
# RUNNER_VRAM_TOTAL_GB
# ordinary job: neither
#
# Each invocation runs EXACTLY ONE ephemeral job in a throwaway container that
# is torn down on exit (--rm), then returns the in-container job's exit status
Expand Down
14 changes: 11 additions & 3 deletions listener/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type ContainerProvisioner struct {
// RUNNER_JOB_WORKDIR absolute same-path work folder or explicit empty (off)
// RUNNER_GPU GPU index a vram job is pinned to, or explicit empty
// RUNNER_VRAM_GB VRAM GiB charged for the pinned job, "unknown" for unpinned VRAM mode, or empty
// RUNNER_VRAM_TOTAL_GB whole GiB of the pinned card (floored), or explicit empty (#185)
//
// The script's stdout/stderr are inherited so the container's job log streams
// straight through, and a non-zero exit becomes a non-nil error so the listener
Expand Down Expand Up @@ -92,18 +93,25 @@ func (c *ContainerProvisioner) Provision(ctx context.Context, req ProvisionReque
if req.DockerSocket != "" {
workDir = req.JobWorkDir
}
// A job pinned to a GPU (#180) passes its index and charged VRAM; an
// unpinned one exports both EMPTY so nothing inherited pins it.
gpu, vram := "", ""
// Three cases, each exporting every value explicitly so nothing inherited
// leaks through:
// - pinned job (#180): GPU index, charged GiB and the card total GiB.
// - unpinned fallback (GPUFallback): GPU index and total empty, budget
// "unknown" (#185).
// - ordinary job: all three empty.
gpu, vram, vramTotal := "", "", ""
if req.VRAMGB > 0 && req.GPU >= 0 {
gpu, vram = strconv.Itoa(req.GPU), strconv.FormatInt(req.VRAMGB, 10)
// The card total in whole GiB, floored (#185).
vramTotal = strconv.FormatInt(req.VRAMTotalMiB/1024, 10)
} else if req.GPUFallback {
vram = "unknown"
}
cmd.Env = append(os.Environ(),
"RUNNER_DEVICES="+strings.Join(req.Devices, " "),
"RUNNER_GPU="+gpu,
"RUNNER_VRAM_GB="+vram,
"RUNNER_VRAM_TOTAL_GB="+vramTotal,
"RUNNER_RUNTIME="+req.Runtime,
"RUNNER_BUILD_TOOL="+req.BuildTool,
"RUNNER_DOCKER_SOCKET="+req.DockerSocket,
Expand Down
36 changes: 36 additions & 0 deletions listener/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,39 @@ func TestContainerProvisionerUnpinnedGPUEnv(t *testing.T) {
})
}
}

// A pinned job (#185) passes the card's total VRAM in whole GiB, floored, so
// the job can compute its share; an unpinned job exports it EMPTY so an
// inherited value never leaks into the container.
func TestContainerProvisionerPassesVRAMTotalAsEnv(t *testing.T) {
t.Setenv("RUNNER_VRAM_TOTAL_GB", "99")
capFile := filepath.Join(t.TempDir(), "env")
script := writeScript(t, "#!/usr/bin/env bash\n"+
"printf 'TOTAL=[%s]\\n' \"${RUNNER_VRAM_TOTAL_GB-<unset>}\" > '"+capFile+"'\n")
p := &ContainerProvisioner{Script: script}
for _, tc := range []struct {
name string
request ProvisionRequest
want string
}{
// 32607 MiB / 1024 = 31.84 GiB, floored to 31.
{name: "pinned", request: ProvisionRequest{GPU: 1, VRAMGB: 8, VRAMTotalMiB: 32607}, want: "TOTAL=[31]"},
{name: "CPU", request: ProvisionRequest{GPU: -1, VRAMTotalMiB: 32607}, want: "TOTAL=[]"},
{name: "VRAM fallback", request: ProvisionRequest{GPU: -1, GPUFallback: true}, want: "TOTAL=[]"},
} {
t.Run(tc.name, func(t *testing.T) {
req := tc.request
req.JobID, req.Image, req.EncodedJITConfig = "job", "img", "ENC"
if err := p.Provision(context.Background(), req); err != nil {
t.Fatalf("Provision: %v", err)
}
got, err := os.ReadFile(capFile)
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(string(got)) != tc.want {
t.Errorf("env = %q, want %q", got, tc.want)
}
})
}
}
58 changes: 49 additions & 9 deletions listener/reactive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,46 @@ func TestVRAMJobsShareOneGPU(t *testing.T) {
}
}

// The request carries the total VRAM of the card the job was admitted onto
// (#185), so the job can turn its charge into a fraction of that card. GPU 0
// is busy, so the job lands on GPU 1, a 16380 MiB card, not the 32607 MiB one.
func TestPinnedJobRequestCarriesAdmittedCardTotal(t *testing.T) {
prov, stop := heldGPUListen(t, []GPU{rtx5090(0, 30000), rtx2000(1, 0)},
assigned(1, "four", "gpu", "vram-gb-4"),
)
defer stop()
req, ok := prov.next(t, 2*time.Second)
if !ok {
t.Fatal("the vram-gb-4 job never started")
}
if req.GPU != 1 {
t.Fatalf("job pinned to GPU %d, want GPU 1", req.GPU)
}
if req.VRAMTotalMiB != 16380 {
t.Errorf("VRAMTotalMiB = %d, want 16380 (the admitted card, not 32607)", req.VRAMTotalMiB)
}
}

// admitGPU hands back the admitted card's own total (#185) rather than leaving
// the caller to look it up in a snapshot the reporter may replace; a job with
// no pin (no reading ever) gets 0.
func TestAdmitGPUReturnsAdmittedCardTotal(t *testing.T) {
l := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{},
Config{GPUProbe: stubGPUProbe{gpus: []GPU{rtx5090(0, 30000), rtx2000(1, 0)}},
Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100})
gpu, _, _, total, ok := l.admitGPU(context.Background(), []string{"vram-gb-4"})
if !ok || gpu != 1 || total != 16380 {
t.Fatalf("admitGPU = (gpu %d, total %d, %v), want (1, 16380, true)", gpu, total, ok)
}

idle := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{},
Config{GPUProbe: stubGPUProbe{err: errors.New("no nvidia-smi")}, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100})
gpu, _, _, total, ok = idle.admitGPU(context.Background(), []string{"vram-gb-1"})
if !ok || gpu != -1 || total != 0 {
t.Fatalf("unpinned admitGPU = (gpu %d, total %d, %v), want (-1, 0, true)", gpu, total, ok)
}
}

// A job with no vram-gb label holds the whole GPU, so a second one waits for
// the first to finish: today's one-job-per-GPU behaviour is the default.
func TestUndeclaredGPUJobsRunOnePerGPU(t *testing.T) {
Expand Down Expand Up @@ -478,7 +518,7 @@ func TestAdmitGPUSettlesThenAdmits(t *testing.T) {
p := &countingGPUProbe{overUntil: 2, over: []GPU{rtx5090(0, 30000)}, ok: []GPU{rtx5090(0, 0)}}
l := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{},
Config{GPUProbe: p, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100})
gpu, _, gb, ok := l.admitGPU(context.Background(), []string{"vram-gb-4"})
gpu, _, gb, _, ok := l.admitGPU(context.Background(), []string{"vram-gb-4"})
if !ok || gpu != 0 || gb != 4 {
t.Fatalf("admitGPU = (%d, %d, %v), want (0, 4, true) once the GPU frees", gpu, gb, ok)
}
Expand All @@ -496,7 +536,7 @@ func TestAdmitGPUCtxCancelUnblocks(t *testing.T) {
time.Sleep(10 * time.Millisecond)
cancel()
}()
if _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok {
if _, _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok {
t.Fatal("admitGPU must return false when cancelled while no GPU has room")
}
}
Expand Down Expand Up @@ -620,27 +660,27 @@ func TestGPUAdmissionFailSafeUsesLastKnownGPUs(t *testing.T) {
Config{GPUProbe: p, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100})
ctx := context.Background()

gpu, _, gb, ok := l.admitGPU(ctx, []string{"vram-gb-4"})
gpu, _, gb, _, ok := l.admitGPU(ctx, []string{"vram-gb-4"})
if !ok || gpu != 0 || gb != 4 {
t.Fatalf("healthy admitGPU = (%d, %d, %v), want (0, 4, true)", gpu, gb, ok)
}
l.chargeGPU(gpu, gb)

p.set(nil, errors.New("nvidia-smi failed"))
gpu, _, gb, ok = l.admitGPU(ctx, []string{"vram-gb-1"})
gpu, _, gb, _, ok = l.admitGPU(ctx, []string{"vram-gb-1"})
if !ok || gpu != 1 || gb != 25 {
t.Fatalf("fail-safe admitGPU = (%d, %d, %v), want (1, 25, true): the empty GPU, whole", gpu, gb, ok)
}
l.chargeGPU(gpu, gb)

short, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
defer cancel()
if gpu, _, gb, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok {
if gpu, _, gb, _, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok {
t.Fatalf("fail-safe admitted (%d, %d) although every known GPU holds a job", gpu, gb)
}

l.releaseGPU(0, 4)
gpu, _, gb, ok = l.admitGPU(ctx, []string{"vram-gb-1"})
gpu, _, gb, _, ok = l.admitGPU(ctx, []string{"vram-gb-1"})
if !ok || gpu != 0 || gb != 25 {
t.Fatalf("fail-safe admitGPU after release = (%d, %d, %v), want (0, 25, true)", gpu, gb, ok)
}
Expand All @@ -651,14 +691,14 @@ func TestGPUAdmissionFailSafeUsesLastKnownGPUs(t *testing.T) {
func TestGPUAdmissionFailSafeWithoutAnyReading(t *testing.T) {
l := New(&fakeSession{}, &recordingMinter{}, &recordingProvisioner{},
Config{GPUProbe: stubGPUProbe{err: errors.New("no nvidia-smi")}, Reserve: 20, SettleWindow: time.Millisecond, MaxRunners: 100})
gpu, _, _, ok := l.admitGPU(context.Background(), []string{"vram-gb-1"})
gpu, _, _, _, ok := l.admitGPU(context.Background(), []string{"vram-gb-1"})
if !ok || gpu != -1 {
t.Fatalf("admitGPU on an idle listener = (gpu %d, %v), want (-1, true)", gpu, ok)
}
l.inFlight.Store(1)
short, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
if _, _, _, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok {
if _, _, _, _, ok := l.admitGPU(short, []string{"vram-gb-1"}); ok {
t.Fatal("admitGPU without any reading admitted a second job beside a running one")
}
}
Expand Down Expand Up @@ -769,7 +809,7 @@ func TestGPUFailSafeRecoveryWaitsForUnpinnedJob(t *testing.T) {
}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
if _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok {
if _, _, _, _, ok := l.admitGPU(ctx, []string{"vram-gb-1"}); ok {
t.Fatal("probe recovery admitted work beside an unpinned job")
}
}
Expand Down
Loading