Process a result that strains GPU memory#
When a query approaches its GPU memory limit, release results you no longer need and consume the next result in chunks where the query supports streaming.
Find the bytes held by earlier results#
Check the device_memory_limit used to create the session that ran out of memory, session.retained_device_bytes, and session.device_reservation. Live results and the running query share the session limit on one device. device_reservation reports what the session’s reserve request did, if applicable, and it can be None. Free pool reserve is available to queries and is distinct from bytes retained by live results.
Record retained_device_bytes before and after releasing a finished result and any exports that still hold it. The decrease measures bytes freed from retention. Retry the original query to find out whether that release made enough room.
Consume the result one chunk at a time#
Use Session.sql_chunked or LazyFrame.collect_chunked to get a reader. Inspect its schema and streamed flag before consuming it. When streamed is True, process chunks as they arrive. When it is False, the default fallback delivers the whole result in one chunk, which must fit in memory.
import pyarrow as pa
import cugeo.geometry as geo
with geo.Session() as session:
observations = session.table_from_arrow(
pa.table({"id": pa.array([10, 20, 30], type=pa.int64())})
)
reader = session.sql_chunked(
"SELECT id FROM observations", tables={"observations": observations}
)
try:
print("schema:", reader.schema)
print("streamed:", reader.streamed)
total = 0
while reader.has_next():
chunk = reader.read_chunk()
count = len(chunk)
print("chunk rows:", count)
total += count
# Finish consuming this chunk before releasing it.
del chunk
print("total rows:", total)
assert total == 3
finally:
reader.close()
Expect three rows overall, and use the flag and chunk sizes from your run. A chunk the consumer still holds has its own lifetime, even after the reader closes. For your query, compare the total with the expected row count when one is available.
If the query still exceeds its limit#
An eligible file query can partition at most one input up front or retry with smaller input pieces after exhaustion, but its output can still require a whole-result allocation. If the consumer can use a smaller answer, request fewer columns or rows. Otherwise, choose a session limit the device can support.
For measured pool growth and host settings on integrated devices, see the device pool growth investigation and query resource measurements.