Feed GPU chip batches to an array or tensor consumer#
Use chip batches to pass raster pixels to GPU preprocessing or inference while keeping track of the source window for each chip.
Check the first chip#
Start with a tiled, single-band UInt16 Deflate GeoTIFF, CuPy, and an nvCOMP-enabled Raster build. The GeoTIFF chip-batch example opens the source and iterates over batches. Supply the path to your own image.
from contextlib import closing
import cugeo.raster
source_path = "<path to your tiled single-band UInt16 Deflate GeoTIFF>"
with cugeo.raster.open(source_path) as source, closing(
source.iter_chips(256, batch_size=128, decode_tiles=128)
) as batches:
batch = next(batches)
chips = batch.data
chip = chips[0]
print("batch shape:", chips.shape, "batch strides:", chips.strides)
print("chip shape:", chip.shape, "chip strides:", chip.strides)
print("source window:", batch.windows[0])
The example delivers NCHW CuPy batches, so one chip has CHW dimensions. A single-band consumer may instead use an HW view. Inspect the actual strides as well as the dimensions: strides describe how to reach successive samples, and a shape alone does not establish the BIP or BSQ packing a consumer may require. Check the consumer’s packing requirements before passing the view.
Hand the chip to the consumer#
For a CuPy consumer that accepts the view’s dtype, shape and strides, pass the chip while keeping its batch owner alive for as long as the consumer uses it. Keep the matching batch.windows[0] to identify its source samples.
For PyTorch, convert to Float32 on the GPU, then share that converted device allocation through DLPack.
import torch
float_chip = chip.astype("float32") # allocates on the GPU
tensor = torch.from_dlpack(float_chip) # shares the converted device data
print(tensor.device, tensor.dtype, tensor.shape)
Check that the tensor is on the intended device and has the expected dtype. Compare its values with the corresponding samples from the source window. The conversion does not normalize them. Keep the shared data alive until the consumer has finished with it.
Release or reuse the storage#
A retained chip view can keep its whole physical batch allocation alive, and multiple deliveries can share a backing allocation. The JPEG2000 chip-batch example shows the iterator pattern and its decode receipt. Inspect each batch’s receipt, including route and fallback, to record the realized route. Deliveries may share a receipt. Release views and their owners when the consumer is finished.
When an operation accepts caller-owned output, check that the destination extent and strides cover the requested result. Account for the source-decode budget separately from the destination allocation. Before reusing storage, establish that both the producer has finished writing and the consumer has finished reading it. See when a device buffer can be reused for the completion and ownership boundary.