Practical overview
When it fits
Consider Toma for otherwise costly PyTorch operations where available CUDA memory varies or the initial batch or step size may be too large. The project cautions that its retry and cache machinery adds overhead.
Who it is for
- PyTorch developers handling workloads that may exceed available CUDA memory.
- Practitioners who want batch, range or chunked processing to retry at smaller sizes after memory failures.
Documented uses
- Retry an inference or training function with a reduced batch size after an out-of-memory error.
- Process tensor data in chunks and reduce the chunk step when memory errors occur.
- Iterate over a range with a step size that can be reduced after an out-of-memory error.
- Reuse successful batch sizes through a configurable cache.
Requirements & limits
The public repository identifies Toma as open source under the MIT license and documents installation with pip.
- The batch helper retries a function with a lower batch size; range and chunked helpers adapt iteration step sizes.
- Three cache modes are documented: stacktrace and available-memory based by default, global per function, or none.
- The default cache assumes memory usage stabilizes after a few iterations.
- The library adds overhead and is recommended for operations that are otherwise time- and memory-consuming.
Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.
Install Toma and prepare an adaptive batch function
Install the library and structure a PyTorch function to use Toma's documented batch helper.
Before you start
- Python environment with pip
- A PyTorch workload function that accepts a batchsize argument
Steps
Install the package with the command below.
pip install tomaImport
tomawithfrom toma import toma.Decorate the workload function with
@toma.batch(initial_batchsize=512)or another chosen initial batch size.Call the function with its model and dataset arguments.
Expected outcome
The helper attempts the initial batch size and, after a memory error, reduces the batch size and retries.
Guide limitations
- Toma adds overhead; its documentation recommends using it with otherwise time- and memory-consuming operations.
- The documented batch size can differ from the batch size used for gradient accumulation.
Choose a batch-size cache
Configure how Toma remembers successful batch sizes.
Before you start
- Toma installed in a Python project
Steps
Choose the default
StacktraceMemoryBatchsizeCache,GlobalBatchsizeCache, orNoBatchsizeCache.Pass the selected type through the call's
cache_typeargument, or settoma.DEFAULT_CACHE_TYPE.For example, use
@toma.batch(initial_batchsize=512, cache_type=toma.GlobalBatchsizeCache)for a per-function cache.
Expected outcome
The selected cache behavior is used for batch-size retries: the default considers call trace and available memory, the global cache reuses a successful size per function, and no-cache mode starts from the suggested size each time.
Guide limitations
- The default cache assumes GPU and CPU memory use reaches a stable state after a few iterations.
- No-cache mode does not reuse a successful batch size.
Sources & review
Source record details
- Source availability
- Active
- Sources checked
- 14 November 2026
- Original website title
- BlackHC/toma · Official homepage title · retained in its source language

