Metadata-Version: 2.5
Name: rampgate-docker
Version: 0.2.0
Summary: Docker integration for the rampgate admission scheduler: runs Start and Stop actions as containers, classifies exits (OOM, preempted, failed), follows Docker events and maps processes to jobs.
Project-URL: Homepage, https://github.com/KebanFiru/rampgate
Project-URL: Documentation, https://kebanfiru.github.io/rampgate/
Project-URL: Source, https://github.com/KebanFiru/rampgate
Project-URL: Changelog, https://github.com/KebanFiru/rampgate/blob/main/packages/rampgate-docker/CHANGELOG.md
Project-URL: Issues, https://github.com/KebanFiru/rampgate/issues
Author: Hakan Bağıç
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: docker,gpu,rampgate,scheduler
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: System :: Distributed Computing
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: docker>=7.0
Requires-Dist: rampgate<0.3,>=0.2
Description-Content-Type: text/markdown

# rampgate-docker

Docker integration for [rampgate](https://pypi.org/project/rampgate/), an admission scheduler for jobs that ramp up their memory. It runs rampgate's `Start` and `Stop` actions as containers, turns container exits into the outcomes the scheduler learns from (OOM, preempted, failed), and attributes process usage to jobs.

## Install

```sh
pip install rampgate-docker
```

Requires access to a Docker daemon. Depends on `rampgate` and the `docker` SDK. **The Docker socket is root-equivalent:** whatever can reach it controls the host.

## Usage

With an `engine`, a `clock` and an NVML `probe` from [`rampgate-nvml`](https://pypi.org/project/rampgate-nvml/):

```python
from rampgate import JobSample
from rampgate_docker import ContainerSpec, ContainerUsageMapper, DockerEventWatcher, DockerExecutor
from rampgate_nvml import pid_to_container_id

executor = DockerExecutor(lambda job, start: ContainerSpec("my-trainer:1", gpus="all"))
watcher = DockerEventWatcher(executor.client, clock, stop_reason=executor.stop_reason)
mapper = ContainerUsageMapper(executor.client, pid_to_container_id)
watcher.start()
while True:
    now = clock()
    for event in watcher.drain():                   # JobStarted / JobExited
        engine.apply(event)
    observed, processes = probe.sample(now)
    jobs = mapper.map(processes, now)
    for dev in observed:                            # one atomic sample per device
        engine.observe_sample(dev.device_id, now, dev.used_total, {
            o.job_id: JobSample(o.attempt, o.usage) for o in jobs if o.device_id == dev.device_id
        })
    decision = engine.tick(now)
    executor.apply(decision.actions, {j: rt.job for j, rt in engine.state.jobs.items()})
```

## What each part does

- **`DockerExecutor`**: `Start` removes containers of older attempts of the job, then creates and starts a container labelled `rampgate.job_id` / `rampgate.attempt`, with `RAMPGATE_JOB_ID`, `RAMPGATE_ATTEMPT` and `RAMPGATE_RESUME` in its environment. `Stop` runs `container.stop(timeout=grace_s)` on a worker thread.
- **`classify_exit`**: `OOMKilled`, an OOM message in the log tail (`CUDA out of memory`, `torch.OutOfMemoryError`, …; configurable), or 137 under memory pressure → `OOM`; 0 → `SUCCESS`; 143/137 after a requested stop → `PREEMPTED`/`CANCELLED`; anything else → `FAILURE`.
- **`DockerEventWatcher`**: follows Docker's event stream on a thread; `drain()` hands the events to the Engine's thread.
- **`ContainerUsageMapper`**: probe process samples → `JobObserved` per job, attempt and device. Usage outside managed containers is left to rampgate's foreign-usage accounting.

## What your training script must do

- **Exit 143 when it stops on SIGTERM,** after saving a checkpoint. Exit 0 means the job finished.
- **Resume when `RAMPGATE_RESUME=1`.**
- **Let the OOM message reach its log.** A GPU OOM sets no `OOMKilled` flag, so `classify_exit` recognises it only from that message.

Demonstrated on one laptop GPU with synthetic memory-hog containers, not with a real framework's caching allocator.

## Links

[Documentation](https://kebanfiru.github.io/rampgate/adapters/#rampgate-docker) · [rampgate core](https://pypi.org/project/rampgate/) · [Source](https://github.com/KebanFiru/rampgate) · [Changelog](https://github.com/KebanFiru/rampgate/blob/main/packages/rampgate-docker/CHANGELOG.md)

Apache-2.0.
