Read into a caller-owned PyTorch tensor#

Allocate a CUDA tensor with PyTorch and pass it as out to read raster pixels into its storage.

Requirements#

Use an NVIDIA GPU, CUDA-enabled PyTorch, CuPy and cugeo-raster built with the nvtiff native feature. Follow the installation guide and check the supported input profiles.

The example uses the tutorial’s RGB UInt8 TIFF. Run the sample download snippet to define source_path, and keep its temporary directory alive.

Allocate the output and read a window#

Allocate a tensor with (bands, rows, columns) axes and torch.uint8 samples to match the source. Use the same CUDA device and stream for allocation, the read and subsequent PyTorch operations:

import torch
import cugeo.raster as raster
from cugeo.raster.windows import Window

device = torch.device("cuda:0")
with torch.cuda.device(device):
    stream = torch.cuda.Stream(device=device)
    with torch.cuda.stream(stream), raster.open(source_path) as src:
        pixels = torch.empty((src.count, 256, 256),
                             dtype=torch.uint8, device=device)
        pointer = pixels.data_ptr()
        result = src.read(
            window=Window(17, 23, 256, 256), out=pixels,
            stream=stream.cuda_stream,
        )

        assert result is pixels
        assert pixels.data_ptr() == pointer

CUDA tensors expose the CUDA Array Interface, which describes their pointer, dtype, shape and byte strides. cugeo-raster uses that description to write into the supplied storage and returns the same tensor. No conversion to a CuPy array is required. The decoder can still allocate temporary storage; caller-owned output does not imply that the complete decode is allocation-free.

CUDA operations are asynchronous: returning to Python does not necessarily mean the GPU has finished. Operations on one stream execute in order, while work on different streams needs an explicit dependency to share storage safely.

torch.cuda.stream(stream) selects the stream for PyTorch operations. stream.cuda_stream is the integer handle for that underlying CUDA stream. Passing it as stream= tells cugeo-raster to order the read with work on the same stream: earlier PyTorch operations finish before the read writes the tensor, and subsequent PyTorch operations can consume the result. Omitting stream= selects CuPy’s current stream, which can differ from PyTorch’s. Entering a PyTorch stream context does not select CuPy’s current stream.

Keep the tensor and stream alive through completion. Use a tensor that does not require gradients: raster decoding does not create a PyTorch autograd operation.

The supplied output shape determines the sampling grid. Do not also pass out_shape. Other dtypes and layouts depend on the selected read route; the caller-output reference states their restrictions. For ordinary CuPy storage, allocate with cupy.empty and pass it as out in the same way. A NumPy out instead selects a supported host TIFF read.

See PyTorch’s CUDA array interface and the cugeo-raster stream contract for interoperability details.