Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
ChannelManager,
ChunkStore,
NormalizedPartitioning,
_sample_chunks,
allgather_reduce,
chunk_to_frame,
chunkwise_evaluate,
Expand Down Expand Up @@ -220,50 +221,38 @@ async def _sample_chunks_for_size_estimate(
metadata_in: ChannelMetadata,
executor: StreamingExecutor,
collective_ids: list[int],
) -> tuple[dict[int, TableChunk], int]:
) -> tuple[ChunkStore, int]:
"""
Sample chunks and estimate total data size to derive num_partitions dynamically.

The sampled chunks are returned keyed by sequence number. The caller is
The sampled chunks are returned in replay order. The caller is
responsible for replaying them into a channel via replay_buffered_channel.
"""
if executor.dynamic_planning is None:
return {}, num_partitions
return ChunkStore(context), num_partitions

size_estimate_id = collective_ids.pop()
target_partition_size = executor.target_partition_size
sample_chunk_count = executor.dynamic_planning.sample_chunk_count

sampled_chunks: dict[int, TableChunk] = {}
sampled_bytes = 0
for _ in range(sample_chunk_count):
msg = await ch_in.recv(context)
if msg is None:
break
chunk = TableChunk.from_message(msg, br=context.br()).make_available_and_spill(
context.br(), allow_overbooking=True
)
sampled_bytes += chunk.data_alloc_size()
sampled_chunks[msg.sequence_number] = chunk
if sampled_bytes >= target_partition_size:
break

# Extrapolate local size estimate from samples
local_count = metadata_in.local_count
local_size = (
int(sampled_bytes / len(sampled_chunks) * local_count) if sampled_chunks else 0
sample = await _sample_chunks(
context,
ch_in,
sample_chunk_count,
target_partition_size,
metadata_in.local_count,
)

# Allgather to get global size estimate across all ranks
if comm.nranks > 1 and not metadata_in.duplicated:
(global_size,) = await allgather_reduce(
context, comm, size_estimate_id, local_size
context, comm, size_estimate_id, sample.total_size
Comment thread
TomAugspurger marked this conversation as resolved.
Resolved
)
else:
global_size = local_size
global_size = sample.total_size

num_partitions = max(1, global_size // target_partition_size)
return sampled_chunks, num_partitions
num_partitions = max(1, -(-global_size // target_partition_size))
Comment thread
rjzamora marked this conversation as resolved.
Resolved
return sample.chunks, num_partitions


async def _receive_and_buffer_chunks(
Expand Down
11 changes: 9 additions & 2 deletions python/cudf_polars/cudf_polars/streaming/actor_graph/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
CUDF_ROW_LIMIT,
MAX_ROWS_PER_PARTITION,
ChannelManager,
ChunkStore,
NormalizedPartitioning,
TableSizeStats,
_sample_chunks,
Expand Down Expand Up @@ -1216,8 +1217,14 @@ async def _choose_strategy(
if left_partitioning.is_aligned_with(right_partitioning, context.br()):
# We can use a chunkwise join
chunkwise = True
left_sample = TableSizeStats(total_chunks=left_metadata.local_count)
right_sample = TableSizeStats(total_chunks=right_metadata.local_count)
left_sample = TableSizeStats(
chunks=ChunkStore(context),
total_chunks=left_metadata.local_count,
)
right_sample = TableSizeStats(
chunks=ChunkStore(context),
total_chunks=right_metadata.local_count,
)
else:
# Need to shuffle or broadcast - Use sampled data to choose a strategy
chunkwise = False
Expand Down
47 changes: 24 additions & 23 deletions python/cudf_polars/cudf_polars/streaming/actor_graph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import time
from collections import deque
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
from dataclasses import dataclass
from functools import reduce
from typing import TYPE_CHECKING, Any, Literal, TypeAlias, cast

Expand Down Expand Up @@ -731,8 +731,8 @@ def indices_to_names(indices: tuple[int, ...], schema: Schema) -> tuple[str, ...
class TableSizeStats:
"""Sampled chunks and aggregate size/row stats for a table channel."""

chunks: dict[int, TableChunk] = field(default_factory=dict)
"""The sampled chunks, keyed by sequence number."""
chunks: ChunkStore
"""The sampled chunks/messages in replay order."""
total_size: int = 0
"""The total estimated size of the table in bytes."""
total_rows: int = 0
Expand All @@ -744,8 +744,8 @@ class TableSizeStats:
async def _sample_chunks(
context: Context,
ch: Channel[TableChunk],
max_sample_chunks: int,
max_sample_bytes: int,
sample_chunk_count: int,
target_partition_size: int,
local_count: int,
) -> TableSizeStats:
"""
Expand All @@ -757,35 +757,36 @@ async def _sample_chunks(
The context.
ch
The channel to sample from.
max_sample_chunks
The maximum number of chunks to sample.
max_sample_bytes
The maximum number of bytes to sample.
sample_chunk_count
Sampling budget multiplier. Sampling continues until
``target_partition_size * sample_chunk_count`` bytes have been sampled
or the local input is exhausted.
target_partition_size
The target partition size used to derive the byte sampling budget.
local_count
The expected number of local chunks (used for extrapolation).

Returns
-------
Sampled chunks and the extrapolated total size/rows for this rank.
"""
sampled_chunks: dict[int, TableChunk] = {}
sampled_chunks = ChunkStore(context)
sample_byte_limit = target_partition_size * sample_chunk_count
sampled_count = 0
total_size = 0
total_rows = 0
for _ in range(max_sample_chunks):
while sampled_count < local_count and total_size < sample_byte_limit:
msg = await ch.recv(context)
if msg is None:
break
chunk = TableChunk.from_message(msg, br=context.br()).make_available_and_spill(
context.br(), allow_overbooking=True
)
sampled_chunks[msg.sequence_number] = chunk
chunk = TableChunk.from_message(msg, br=context.br())
total_size += chunk.data_alloc_size()
total_rows += chunk.shape[0]
if total_size >= max_sample_bytes:
break
if sampled_chunks:
total_size = int((total_size / len(sampled_chunks)) * local_count)
total_rows = int((total_rows / len(sampled_chunks)) * local_count)
sampled_count += 1
sampled_chunks.insert(Message(msg.sequence_number, chunk))
if sampled_count:
total_size = int((total_size / sampled_count) * local_count)
total_rows = int((total_rows / sampled_count) * local_count)
return TableSizeStats(
chunks=sampled_chunks,
total_size=total_size,
Expand All @@ -798,7 +799,7 @@ async def replay_buffered_channel(
context: Context,
ch_out: Channel[TableChunk],
ch_in: Channel[TableChunk],
buffered_chunks: dict[int, TableChunk],
buffered_chunks: ChunkStore,
metadata: ChannelMetadata,
*,
trace_ir: IR,
Expand All @@ -823,8 +824,8 @@ async def replay_buffered_channel(
"""
async with shutdown_on_error(context, ch_out, ch_in, trace_ir=trace_ir):
await send_metadata(ch_out, context, metadata)
for seq_num, chunk in buffered_chunks.items():
await ch_out.send(context, Message(seq_num, chunk))
for msg in buffered_chunks:
await ch_out.send(context, msg)
while (msg := await ch_in.recv(context)) is not None:
await ch_out.send(context, msg)
await ch_out.drain(context)
Expand Down
6 changes: 4 additions & 2 deletions python/cudf_polars/cudf_polars/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,10 @@ class DynamicPlanningOptions:
Parameters
----------
sample_chunk_count
The maximum number of chunks to sample before deciding whether
to shuffle. Default is 2.
Multiplier used to derive the dynamic-planning sampling budget from
the target partition size. For example, the default value of 2 samples
up to roughly two target partitions of data before deciding whether to
shuffle. Default is 2.
join_prefilter_threshold
Row-count ratio (small / large) below which a join key prefilter is
applied. Set to 0 to disable join prefiltering. Default is 0.5.
Expand Down
Loading