Metadata-Version: 2.4
Name: efficio
Version: 0.2.0
Summary: Get more training out of every GPU hour on Colab, Kaggle and local GPUs: mixed precision, faster data loading, time budgets and exact resume for PyTorch.
Author: Hassaan Tole, Durvesh Bhadgaonkar
License-Expression: MIT
Project-URL: Homepage, https://github.com/HuNTer8272/Efficio
Project-URL: Repository, https://github.com/HuNTer8272/Efficio
Project-URL: Issues, https://github.com/HuNTer8272/Efficio/issues
Keywords: pytorch,deep-learning,training,gpu,colab,kaggle,mixed-precision,checkpointing,resume,dataloader
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0
Requires-Dist: numpy
Requires-Dist: pillow
Provides-Extra: hub
Requires-Dist: huggingface_hub>=0.23; extra == "hub"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# Efficio

**Get more training out of every GPU hour on Colab, Kaggle and your own GPU.** Efficio is a small PyTorch library that picks the right mixed precision, keeps the GPU fed with data, stops cleanly before your time runs out and resumes exactly where it stopped.

```bash
pip install efficio
```

## Quick start on Colab

```python
!pip install -q efficio
import efficio, torch

run = efficio.Run(budget="2h")                        # save and stop 10 minutes before 2 h
model, optimizer = run.prepare(model, optimizer)      # resumes from the last checkpoint
for x, y in run.loop(train_loader, epochs=20):        # batches arrive on the GPU
    with run.autocast():                              # fp16 on a T4, bf16 on newer GPUs
        loss = torch.nn.functional.cross_entropy(model(x), y)
    run.backward(loss)
    run.step()
run.report()                                          # time split, GPU use and tips
```

Run the same cell again after a disconnect or in a new session and training continues from the last checkpoint. A complete CIFAR-10 example is in the [Colab quick-start notebook](https://github.com/HuNTer8272/Efficio/blob/main/examples/colab_quickstart.ipynb).

## What it does for you

Free GPUs on Colab and Kaggle have strict time limits, and much of that time is wasted: slow data loading, full-precision math and runs lost when a session dies.

- **On Colab and Kaggle:** faster training, less wasted quota, and progress saved before the limit hits, so you can continue after the reset or on the other platform.
- **On your own GPU:** faster training and crash safety (power cuts, restarts, out-of-memory crashes). Local GPUs have no quota, so there the benefit is speed and safety, not extra time.

## Features

### Speed

- **GPU-aware mixed precision:** fp16 with gradient scaling on a T4 and older GPUs, bf16 on Ampere and newer, fp32 on CPU (`run.autocast()`).
- **Automatic DataLoader tuning:** more workers, pinned memory, persistent workers and prefetching, applied by `run.loop()` on Linux (Colab, Kaggle).
- **channels_last** memory format for convolutional models on the GPU, plus `cudnn.benchmark`.
- **GPULoader:** keeps a small dataset (CIFAR, MNIST, FashionMNIST) on the GPU and builds and augments batches there, with no CPU work per batch.
- **ImageCache:** decodes and resizes a large image dataset once, using all CPU cores and parallel file reads, into one uint8 array that later sessions reuse. The build is resumable.
- **GPU augmentation** for any DataLoader: crop, resized crop, affine, flip, brightness and contrast, normalize (`efficio.gpu_augment`, used through `run.loop(..., gpu_transform=...)`).

### Time budget and checkpoints

- **Budget stop:** `Run(budget="2h")` saves and stops before the time runs out. A shutdown signal (SIGTERM) also triggers a save.
- **Full checkpoints:** model, optimizer, scheduler, gradient scaler, RNG states and the exact position in the epoch.
- **Atomic saves**, so a crash in the middle of a save never corrupts the last good checkpoint. Periodic and emergency saves.
- **Exact mid-epoch resume:** the same step count, and the same data order with GPULoader or unshuffled data.
- **Gradient accumulation** (`Run(grad_accum_steps=...)`) and a validation helper (`run.evaluate_mode()`: eval mode, no gradients, mixed precision).
- **Gradient clipping that works with fp16 and gradient accumulation** (`run.clip_grad_norm(max_norm)`). Verified on a Colab T4 with efficio 0.1.1 from PyPI (`benchmarks/verify_gpu_features.py`, 4/4 checks passed).
- **Smaller checkpoints** (`Run(checkpoint_mode="light")` or `"smallest"`): light keeps the weights at full precision and stores the optimizer state in 8 bits; smallest also stores the weights in fp16. Measured on a Colab T4: light is 2.0x smaller for a transformer with AdamW and 1.6x for ResNet-18 with SGD, smallest 2.9x and 2.6x (see [Checkpoint options](#checkpoint-options)). Available since 0.2.0.
- **Trainable-only checkpoints** (`Run(save_trainable_only="auto")`): with a frozen backbone, only the trainable parameters are saved, and resuming checks that the frozen weights are the same. Verified on Colab T4. Available since 0.2.0.
- **Background checkpoint saving** (`Run(async_save=True)`): periodic saves are written in a background thread; budget, shutdown and final saves always finish first. Verified on Colab T4. Available since 0.2.0.
- **Checkpoints saved to the Hugging Face Hub or Google Drive** (`Run(storage=...)`): background upload after each save, a manifest with sha256 checksums, the last 3 checkpoints kept in storage, budget and final saves that wait for the upload. Hugging Face storage needs `pip install "efficio[hub]"`. Verified on Kaggle (2x T4) and Colab (T4). Available since 0.2.0.
- **Resume on another machine or platform:** start on Kaggle and continue on Colab, or the other way round. The latest checkpoint is downloaded and its sha256 checked. Verified from Kaggle (2x T4) to Colab (T4). Available since 0.2.0.
- **Auto-shutdown on Colab** (`Run(shutdown_when_done=True)`): releases the runtime after the final checkpoint is confirmed in storage. Verified on Colab T4. Available since 0.2.0.
- Resume quality of the checkpoint modes was checked on the T4 against a noise baseline (an exact resume with tiny weight noise); see [Checkpoint options](#checkpoint-options).

### Visibility

- **End-of-run report:** setup vs training time, time spent waiting for data, average GPU utilization, GPU minutes used, the optimizations applied, and plain-language tips (for example: use GPULoader, use ImageCache, or try a larger batch).
- **ImageCache tip for image datasets decoded from files every epoch.** Verified on a Colab T4 with efficio 0.1.1 from PyPI (`benchmarks/verify_gpu_features.py`, 4/4 checks passed).

### Available, needs GPU verification

These work and are covered by CPU tests, but haven't been confirmed on a GPU yet:

- **Same data order after a resume with a shuffled DataLoader** (per-epoch seeded shuffle).

The large-image features (ImageCache, CachedImageDataset, GPU augmentation through `gpu_transform`, `evaluate_mode`) have been verified on Kaggle (Tesla T4) but not yet on Colab.

## Usage

### Keep a small dataset on the GPU

```python
from efficio import GPULoader, gpu_augment as A

loader = GPULoader.from_torchvision(train_set, batch_size=128, shuffle=True, transform=A.Compose(
    [A.RandomCrop(32, padding=4), A.RandomHorizontalFlip(), A.Normalize(mean, std)]))
for x, y in run.loop(loader, epochs=20):
    ...
```

`from_torchvision` reads `train_set.data` and `train_set.targets` and ignores the dataset's own CPU transform, so pass the GPU version as `transform`. For other data use `GPULoader.from_tensors(x, y, batch_size)`. GPULoader refuses datasets that would need more than 30% of free GPU memory; use a normal DataLoader for those.

### Large image datasets

```python
from torch.utils.data import DataLoader
import efficio
from efficio import CachedImageDataset, ImageCache, gpu_augment as A

ImageCache.build(paths, labels, "/content/cache/train", size=(224, 224), grayscale=True)
loader = DataLoader(CachedImageDataset("/content/cache/train"), batch_size=64, shuffle=True,
                    num_workers=2)
augment = A.Compose([A.ToFloat(), A.RepeatChannels(3), A.RandomResizedCrop(224, scale=(0.8, 1.0)),
                     A.RandomHorizontalFlip(), A.Normalize(mean, std)])
for x, y in run.loop(loader, epochs=10, gpu_transform=efficio.on_inputs(augment)):
    ...
```

`labels` can be class ids or a multi-hot array. Building the cache needs no GPU: to save GPU quota, build it in a CPU-only session, keep the folder (Google Drive on Colab; notebook output or a private dataset on Kaggle) and train in a GPU session. With `workers > 1` on Windows or macOS, call `ImageCache.build` under `if __name__ == "__main__":`.

### Gradient clipping and validation

```python
run.backward(loss)
run.clip_grad_norm(1.0)          # between backward and step; real units, also with fp16
run.step()

with run.evaluate_mode():        # eval mode, no gradients, mixed precision
    val_loss = loss_fn(model(x_val), y_val)
```

### Checkpoint options

```python
run = efficio.Run(
    budget="2h",
    checkpoint_mode="exact",        # or "light" (~2x smaller) or "smallest" (~3x smaller)
    save_trainable_only="auto",     # "auto": skip frozen parameters when some are frozen
    async_save=True,                # periodic saves written in the background
)
```

Three checkpoint modes. Any mode can resume from a checkpoint written in any other mode.

| Mode | What is stored | Size (Adam) | Resume | Use it when |
|---|---|---|---|---|
| `"exact"` (default) | everything at full precision | 1x | bit-identical | you want the safest choice |
| `"light"` | full-precision weights, 8-bit optimizer state | about 2x smaller | weights exact; optimizer state approximate, a tiny effect on training | you want smaller files without rounding any weights |
| `"smallest"` | fp16 weights, 8-bit optimizer state | about 3x smaller | weights rounded to fp16, a slight effect after resume | file size matters most (slow uploads, little disk space) |

Checkpoint sizes measured on a Colab T4 (`benchmarks/checkpoint_sizes.py`). These are results for these models, not a guarantee; the saving depends on the model and the optimizer.

| Model | Optimizer | exact | light | smallest | trainable only |
|---|---|---|---|---|---|
| ResNet-18 (CIFAR stem) | SGD + momentum | 85.36 MB | 53.75 MB | 32.42 MB | - |
| Small transformer encoder | AdamW | 59.69 MB | 30.26 MB | 20.32 MB | - |
| Frozen ResNet-18 backbone + small head | AdamW | 44.23 MB | 43.47 MB | 21.90 MB | 1.61 MB |

With a frozen backbone, the optimizer state only covers the head, so light saves little; trainable-only saving is what shrinks those checkpoints.

Resume quality was checked on the same T4 with `benchmarks/verify_light_resume.py`: a ResNet-18 run stopped by the time budget mid-epoch and resumed with a fresh `Run`, 27 of 27 checks passed. Compared with an uninterrupted run:

- **Exact:** bit-identical.
- **Restored state:** in every mode the gradient scaler, scheduler and step count came back exactly, and none of the 11,173,962 second-moment values was zeroed or halved.
- **Final loss gaps:** noise baseline (an exact resume with 1e-6 weight noise) 0.0005, light 0.0012, smallest 0.0001. Accuracy gap 0.0 points everywhere.

- **Light and smallest** compress large optimizer state tensors (for example Adam's `exp_avg` and `exp_avg_sq`, SGD momentum) with 8-bit blockwise quantization. For Adam, `exp_avg_sq` is stored as its square root and `exp_avg` as its ratio to it, so the update direction after a resume matches the saved one within one quantization step. With SGD the saving is smaller (1.6x for light in the table above), because momentum is half of the checkpoint.
- **Smallest** also stores floating-point weights in fp16 and casts them back to their original dtype on load. Integer buffers are unchanged.
- In both modes, the scheduler, gradient scaler, RNG states and position are saved exactly.
- **`save_trainable_only="auto"`** (default) leaves frozen parameters (`requires_grad=False`) out of the checkpoint when the model has any, and saves a signature of them instead (names, shapes, dtypes and a checksum). On resume, the model must have the same frozen weights, so load the same pretrained model and freeze the same layers before `run.prepare()`. If they differ, Efficio raises an error instead of resuming. `True` and `False` force it on or off. Buffers such as BatchNorm statistics are always saved.
- **`async_save=True`** (default) copies the state to CPU memory at a periodic save and writes the file in a background thread, so training continues. Only one save runs at a time. Saves for the time budget, a shutdown signal and the end of training wait for any background save and then write synchronously. If a background save fails, the error is raised at the next `run.step()`. `run.save()` called by you waits until the file is written.

Checkpoints written by Efficio 0.1.x still load and resume. The end-of-run report shows the checkpoint mode, the size of the last file and how much save time was hidden by background saving.

### Save to Hugging Face Hub or Google Drive

When a Colab or Kaggle session ends, checkpoints on its disk are lost. With `storage=`, every checkpoint is also copied to a place that lasts. A new session, on the same platform or the other one, continues from there.

```python
run = efficio.Run(budget="2h", storage="hf://your-username/my-run")     # a private HF repo
# or on Colab, after you have mounted Google Drive yourself:
run = efficio.Run(budget="2h", storage="/content/drive/MyDrive/my-run")
```

- **Hugging Face Hub** needs `pip install "efficio[hub]"`. If the repo doesn't exist, Efficio creates it as a private repo. If the repo exists and is public, Efficio prints a warning. With `hf://user/repo/folder`, the run's files go in a folder of the repo.
- **Google Drive, or any other folder.** Mount Drive first with `from google.colab import drive; drive.mount('/content/drive')`. Efficio doesn't mount Drive for you, and it refuses a `/content/drive/...` path while Drive isn't mounted. Every file is copied under a temporary name and then renamed, so you never see a half-copied file under its real name.
- **Background uploads.** After each local save, the file is uploaded in a background thread, one file at a time. A failed upload is tried 3 times, with a pause between tries. If it still fails, the error is raised at the next `run.step()`. The local checkpoint is kept either way.
- **The manifest.** `manifest.json` in the storage lists each checkpoint with its file name, step, epoch, mode, size, sha256, Efficio version, platform and time. A checkpoint is added only after its upload has finished, so a half-uploaded file never becomes the latest. Storage keeps the newest `keep_remote` checkpoints (default 3). Older ones are deleted only after the new one is listed.
- **Budget, shutdown-signal and final saves** wait up to `upload_timeout` seconds (default 120) for their upload. They print whether the upload succeeded, or that the checkpoint is only on this machine.
- **Resuming.** If storage has a newer checkpoint than this machine (in a new session there is none locally), `run.prepare()` downloads the latest one from the manifest. It checks the sha256 and then resumes as usual. This works with all three checkpoint modes and with trainable-only checkpoints. If the sha256 doesn't match, Efficio refuses to resume.
- **Only this run's checkpoints are resumed.** Each checkpoint stores a run fingerprint: the model's parameter names and shapes and the optimizer type. The checkpoint mode isn't part of it, so changing the mode still resumes. Checkpoints from before this check have no fingerprint and still resume.
  - **Storage that holds another run's checkpoints:** `run.prepare()` refuses to start and writes nothing there, whatever `resume` is. The error (`StorageInUseError`, a `CheckpointMismatchError`) names the storage path, the other run's step and its fingerprint summary. Use a different storage path, or pass `overwrite_storage=True` to replace that run's checkpoints. With `overwrite_storage=True`, Efficio prints a warning, never resumes the other run's checkpoints, and deletes them once this run's first checkpoint is uploaded.
  - **Another run's checkpoint on this machine:** with the default `resume="auto"`, Efficio prints a warning naming the file and step, moves the other run's local checkpoints into an `other_run_<time>` subfolder (nothing is deleted) and starts fresh. With `resume=True`, it raises `CheckpointMismatchError` instead.
- **The report** shows the storage target, the last upload's file, size and time, and how much upload time was hidden in the background.

**The Hugging Face token.** Never type the token into your code.

1. Create a token with write access at [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens).
2. Save it as a secret named `HF_TOKEN`:
   - **Colab:** click the key icon in the left sidebar, add `HF_TOKEN`, and turn on notebook access.
   - **Kaggle:** open Add-ons, then Secrets, add `HF_TOKEN`, and tick it for the notebook. Internet must be on in the notebook settings.
   - **Elsewhere:** set the `HF_TOKEN` environment variable.

Efficio looks for the token in this order: `efficio.storage.HuggingFaceHub("user/repo", token=...)`, the `HF_TOKEN` environment variable, Colab secrets, then Kaggle secrets. It never prints, logs or saves the token, never puts it in a checkpoint or the manifest, and removes it from error messages.

**Kaggle to Colab.** Use the same training code on both platforms; only the budget differs.

```python
# Kaggle (GPU on, Internet on, HF_TOKEN secret attached)
run = efficio.Run(budget="11h30m", storage="hf://your-username/my-run")
model, optimizer = run.prepare(model, optimizer)
for x, y in run.loop(train_loader, epochs=50):
    ...
run.report()        # stopped by the budget; the stop checkpoint is uploaded first

# Later on Colab (HF_TOKEN secret with notebook access)
run = efficio.Run(budget="3h", storage="hf://your-username/my-run")
model, optimizer = run.prepare(model, optimizer)   # downloads the latest checkpoint, resumes
for x, y in run.loop(train_loader, epochs=50):
    ...
run.report()
```

**Auto-shutdown on Colab.** `Run(storage=..., shutdown_when_done=True)` releases the Colab runtime (`google.colab.runtime.unassign()`) after `run.report()`, so it stops using GPU quota. Two conditions must hold: training has ended, and the final checkpoint's upload is confirmed. If the upload failed or isn't confirmed, the runtime stays on. Colab removes the machine a few seconds after it is released, and Efficio waits up to 60 s for that, so code after `run.report()` doesn't run. If the runtime is still there after 60 s, Efficio prints a warning. Make `run.report()` the last line. The default is off. Kaggle sessions can't be stopped from code, and on a local machine there is nothing to shut down, so in both places Efficio only prints a note.

**Results on free GPUs** (`benchmarks/verify_cloud_resume.py` and a Colab Drive check, single runs):

| Check | Result |
|---|---|
| Kaggle (2x T4): budget stop, upload to a private HF repo | stopped at step 97 of 192, 128 MB upload confirmed before training returned (3/3 checks) |
| Colab (T4): download from HF, resume, finish | sha256 verified, resumed at step 97, ended at step 192 like the uninterrupted run (7/7 checks) |
| Final loss / accuracy gap to an uninterrupted run | 0.0014 / 0.2 points (noise baseline: 0.0009 / 0.0; limits 0.05 / 3.0) |
| Colab (T4): Google Drive folder, budget stop, resume after deleting the local checkpoints | 7/7 checks; final weights bit-identical to an uninterrupted run |
| Colab (T4): `shutdown_when_done=True` | runtime released after the final checkpoint was confirmed on Drive |

Checkpoints are loaded with Python's pickle, as `torch.load` does. Only resume from a repo or folder you control.

## Results

These are single runs on free notebook GPUs, so read them as examples, not guarantees: speedups depend on the model, the data pipeline and how many CPU cores you get. The baselines are plain PyTorch in fp32 without any Efficio feature. Epoch-time averages leave out the first (warm-up) epoch. The raw results are in [`benchmarks/results/`](https://github.com/HuNTer8272/Efficio/tree/main/benchmarks/results) and the scripts in [`benchmarks/`](https://github.com/HuNTer8272/Efficio/tree/main/benchmarks).

### ResNet-18 on CIFAR-10, Colab, 1x Tesla T4

5 epochs, batch size 128.

| Run | Avg epoch | Epochs per hour | GPU utilization | Test accuracy | Speedup |
|---|---|---|---|---|---|
| Plain PyTorch (fp32) | 47.6 s | 75.6 | 98% | 85.37% | 1.00x |
| Efficio (fp16, DataLoader tuning, channels_last) | 20.7 s | 173.6 | 54% | 85.57% | 2.30x |
| Efficio + GPULoader | 13.3 s | 270.4 | 96% | 85.59% | 3.58x |

With the normal DataLoader the GPU waited for Colab's 2 CPU cores to augment images (22% data-wait). GPULoader keeps the data and augmentation on the GPU.

### ResNet-18 on CIFAR-10, Kaggle, Tesla T4 (one of the two GPUs)

Same settings as on Colab.

| Run | Avg epoch | Epochs per hour | GPU utilization | Test accuracy | Speedup |
|---|---|---|---|---|---|
| Plain PyTorch (fp32) | 44.8 s | 80.3 | 98% | 85.98% | 1.00x |
| Efficio (fp16, DataLoader tuning, channels_last) | 14.7 s | 244.3 | 89% | 85.55% | 3.04x |
| Efficio + GPULoader | 14.7 s | 244.3 | 91% | 85.52% | 3.04x |

Kaggle gives the notebook 4 CPU cores, so the tuned DataLoader already keeps the GPU busy (2% data-wait) and GPULoader adds no speed there.

### Large images from disk: NIH Chest X-rays, Kaggle, Tesla T4

5,000 randomly sampled 1024x1024 PNGs from `/kaggle/input` (4,500 train / 500 validation), 14 findings as a multi-label target, ResNet-18 at 224x224, batch size 64, 4 epochs. The baseline decodes, resizes and augments every PNG on the CPU in each epoch with 4 DataLoader workers; Efficio builds an ImageCache once and augments on the GPU. Both ran in the same Kaggle session.

| Run | Avg epoch | Epochs per hour | GPU utilization | Data-wait | Validation AUROC |
|---|---|---|---|---|---|
| Plain PyTorch (fp32, CPU decoding, 4 workers) | 34.9 s | 103.0 | 37% | 74% | 0.615 |
| Efficio (fp16, ImageCache + GPU augmentation) | 5.8 s | 625.6 | 78% | 0.3% | 0.673 |

Building the cache took about 36 s on cold files (139 images/s); later runs reuse it. Caveats:

- **Warm file cache:** the baseline ran after the cache build, so the PNGs were already in RAM, which makes its file reads faster than on a cold start.
- **Single runs:** each row is one 4-epoch run. Efficio's epoch time varied by about 10-15% between sessions (5.1 to 5.8 s).
- **AUROC, not accuracy:** most labels are 0, so per-label accuracy is about 95% for every run. Across the NIH runs, AUROC ranged from 0.615 to 0.673 for both pipelines, so 4 epochs on 4,500 images can't show an accuracy difference either way.

## Works with

- PyTorch 2.0 or newer, Python 3.10 or newer
- Google Colab and Kaggle notebooks
- A local NVIDIA GPU, or CPU (for development and testing)

## Known limitations

- **PyTorch only.** TensorFlow/Keras support is planned.
- **Single GPU.** On Kaggle's 2x T4 machines, Efficio trains on one GPU.
- **GPULoader** needs a dataset with `.data` and `.targets` (`from_torchvision`), or tensors passed to `GPULoader.from_tensors`.
- **Windows and macOS:** Efficio doesn't add DataLoader workers automatically there, because worker processes need a picklable dataset and an `if __name__ == "__main__":` guard. Set `num_workers` yourself to use them.

## Roadmap

Planned for future releases:

- Automatic batch size with out-of-memory recovery
- A CPU pre-flight check and a budget planner
- Both Kaggle T4 GPUs
- Hugging Face Trainer, PyTorch Lightning and TensorFlow/Keras support
- Telegram and email notifications
- More benchmarks: a DistilBERT fine-tune and a small LoRA run

## Contributing

See [CONTRIBUTING.md](https://github.com/HuNTer8272/Efficio/blob/main/CONTRIBUTING.md) for the development setup, tests and pull request rules. Changes are listed in the [changelog](https://github.com/HuNTer8272/Efficio/blob/main/CHANGELOG.md).

## License

MIT. See [LICENSE](https://github.com/HuNTer8272/Efficio/blob/main/LICENSE).
