Metadata-Version: 2.4
Name: euler-connector
Version: 0.1.1
Summary: How the euler-* packages talk to euler-view: the launch environment, records, transports and structured progress
License-Expression: MIT
Keywords: euler-view,slurm,progress,telemetry,machine-learning
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: tqdm
Requires-Dist: tqdm>=4.62; extra == "tqdm"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: tqdm>=4.62; extra == "dev"
Dynamic: license-file

# euler-connector

How the euler-* packages talk to [euler-view](https://github.com/d-rothen): one source of truth for the connection.

When euler-view launches a job, it tells the job's processes where they belong and how they may reach it. A package reports through `euler_connector` and never handles that connection itself.

- **Transports.** Records go to a file on the shared filesystem, which euler-view reads over SSH and which works on every cluster. Where compute nodes reach euler-view, they are also posted over HTTP.
- **Who speaks.** Only rank 0 speaks for a job. Forked workers stay silent; workers started with `torch.multiprocessing.spawn` must [set `RANK`](#several-processes).
- **Safety.** Nothing ever blocks or breaks the run: no exceptions, no unbounded queues, and a daemon thread for the network.
- **Dependencies.** None beyond the standard library (Python ≥ 3.9). `tqdm` is optional.

The first channel is **structured progress**. A record says what a counter counts (the whole run, one task, one epoch) and where the run is (task i of n, epoch e of E). euler-view can therefore show one overall percentage and ETA for training, inference and evaluation alike.

## Install

```sh
pip install euler-connector          # or: euler-connector[tqdm]
```

## Report progress

```python
from euler_connector import progress

p = progress.start("inference")                       # train | inference | eval | preprocess | other

# A tqdm bar that also reports (same arguments as tqdm, plus `scope`):
for sample in progress.tqdm(samples, desc="Inference", scope="overall", unit="sample"):
    ...

p.close()              # done; a failed run calls p.close(done=False)
```

Without a bar:

```python
p = progress.start("train")
for epoch in range(num_epochs):
    p.set_epoch(epoch + 1, num_epochs)                # 1-based
    for step, batch in enumerate(loader, 1):
        ...
        p.update(step, len(loader), scope="epoch", unit="step")
p.close()
```

Nested work, e.g. evaluating several datasets:

```python
p = progress.start("eval")
for d, dataset in enumerate(datasets, 1):
    p.set_level("dataset", d, len(datasets), dataset.name)
    for t, task in enumerate(tasks_of(dataset), 1):
        p.set_task(t, None, task.name)                # the count may be unknown
        for sample in progress.tqdm(task.samples, desc=task.label):   # scope="task"
            ...
```

Records are written at most every 10 s. A new task, epoch or level is written at once, at most once per second.

`p.update(current, total)` counts the whole run unless given a `scope` (`task`, `epoch`, `step`); a bar counts one task unless given one. Positions are 1-based.

No call raises. A call progress cannot use, such as a 0-based position or an unknown scope, is ignored with a warning, so it never stops a run.

## Several processes

Only the global rank 0 reports. The rank comes from `RANK` (set by torchrun and accelerate), `OMPI_COMM_WORLD_RANK`, `PMI_RANK` or `SLURM_PROCID`, else it is 0. Forked workers, such as DataLoader workers, stay silent.

Workers started with `torch.multiprocessing.spawn` must set `RANK` to their global rank before their first progress call (the connector reads it once). `spawn` sets no rank variable, so without it each worker counts as rank 0, and they overwrite each other's progress.

```python
def train(rank, world_size):                  # spawn passes each worker its index
    os.environ["RANK"] = str(rank)            # on several nodes: node_rank * nprocs + rank
    p = progress.start("train")
    ...

torch.multiprocessing.spawn(train, args=(world_size,), nprocs=world_size)
```

## The launch environment

euler-view sets these variables for every launch; outside euler-view nothing is sent.

| Variable | Meaning |
|---|---|
| `EULER_LAUNCH_ID` | the launch |
| `EULER_CONNECTOR_DIR` | where the file transport writes `<channel>.json`; it also holds the HTTP token |
| `EULER_VIEW_BASE_URL` | set only where compute nodes reach euler-view: enables HTTP |
| `EULER_CONNECTOR_MODE` | `auto` (default), `file`, `http`, `off` |
| `EULER_CONNECTOR_INTERVAL` | seconds between records of one channel (default 10) |

[`docs/protocol.md`](docs/protocol.md) is the full protocol: the envelope, both transports, throttling, the `progress` schema, and how to add a channel.

## For more uses

Progress is one domain module on a general base:

- `Connector`: the process's connection, from the environment;
- channels: named, each with a versioned payload schema;
- transports: file, HTTP, memory, callback.

A new use (a heartbeat, a status, metrics from inference) adds a channel and a small module next to `progress`, and the package code stays one call. See "Adding a channel" in the protocol.

```python
from euler_connector import connect

connector = connect()
print(connector.describe())    # enabled, launch, rank, transports and their health (no secrets)
```

## Development

```sh
uv sync --extra dev
uv run pytest
```
