Batch chip reads within a memory budget#

Read a tiled image in GPU batches small enough to leave room for the work that consumes them. Raster’s iterator groups storage-tile decoding separately from the chips handed to your consumer.

Set a budget that leaves room for the consumer#

A read plan’s memory budget covers its retained memory. Other GPU allocations in the process are outside the plan budget, including a separately owned cache and allocations made by your consumer. Leave room for both. A plan that fits its limit can still leave too little memory for preprocessing or inference.

The following checked path uses CuPy and an nvCOMP-enabled Raster build. The Cargo nvcomp feature requires cuda. From cugeo-raster/, run the tiled, single-band UInt16 Deflate example.

python examples/geotiff_chip_batches.py \
  tests/data/nvtiff_c1/deflate_tile16_p2.tif \
  --chip-size 16 --batch-size 3 --decode-tiles 8 --max-batches 1

--batch-size limits chips delivered to the consumer. --decode-tiles controls storage tiles decoded together in a wave. The command-line example does not take a memory budget. For an explicit iterator limit, use the same fixture and parameters in Python.

from contextlib import closing
import cugeo.raster

path = "tests/data/nvtiff_c1/deflate_tile16_p2.tif"
with cugeo.raster.open(path) as dataset, closing(
    dataset.iter_chips(
        16, batch_size=3, decode_tiles=8, memory_budget_bytes=8 << 20
    )
) as batches:
    for batch in batches:
        for window, chip in zip(batch.windows, batch.data):
            print(window, chip.shape)
            # Consume chip here before retaining the next batch.

Each yield provides NCHW CuPy pixels in batch.data, source windows in batch.windows, and batch.receipt. The iterator generates its windows, including edge coverage. Inspect the returned windows to associate each chip with its source area. The GPU test exercises this fixture with 8 and 32 MiB limits and compares windows with checked pixel data.

Read and consume one batch#

Replace the comment with your processing step. A chip can be a view into a larger decode wave, so retaining that view can retain the entire wave’s allocation. Finish consuming it and release references when you no longer need its pixels.

For a tensor consumer, the example’s optional --torch path converts pixels to Float32 on the GPU, which allocates a new array, and then shares that allocation with PyTorch through DLPack without a host transfer. See Feed GPU chip batches to an array or tensor consumer for the handoff. JPEG2000 has a separate chip batch example.

Reuse outputs and recover a partial read#

For repeated reads with caller-provided outputs, a reusable read plan keeps those output objects and revalidates their capacity before execution. After cancellation or a runtime I/O failure, inspect the typed per-request outcomes to identify completed outputs, then resubmit the outstanding work. Keep this accounting separate from the iterator’s consumer batch size and decode-wave size.