Skip to content

[data] Tag refbundles with the physcial operator ids that produced it - #62277

Closed
justinvyu wants to merge 17 commits into
ray-project:masterfrom
justinvyu:union-memory-attribution-v2
Closed

justinvyu wants to merge 17 commits into
ray-project:masterfrom
justinvyu:union-memory-attribution-v2

Conversation

@justinvyu

@justinvyu justinvyu commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds a per-block tuple to RefBundle that tracks which operator produced each block. This is plumbing for per-producer memory attribution when Union becomes an ineligible (pass-through) operator in a follow-up PR.

Production Sites

Where producer_op_ids is stamped

  • DataOpTask.on_data_ready() — Streaming map/filter operators stamp each output block via the new operator_id param.
  • AllToAllOperator.all_inputs_done() — Bulk operators (shuffle, sort, repartition, randomize_blocks) stamp output bundles with self.id after _bulk_fn returns.
  • ZipOperator — Stamps output bundles at construction.
  • HashShuffleOperator / GpuHashShuffleOperator — Pass operator_id to DataOpTask.

Propagation Sites

Where producer_op_ids is preserved

  • RefBundle.slice() — Tracks producer_op_ids parallel to blocks in consumed/remaining halves.
  • RefBundle.merge_ref_bundles() — Concatenates tuples from all input bundles.
  • LimitOperator._add_input_inner() — Preserves per-block producer_op_ids through limiting.
  • OutputSplitter._split() — Preserves per-block producer_op_ids through splitting.
  • dedupe_schemas_with_validation() — Preserves when reconstructing a bundle with a unified schema.
  • stream_split_iterator.py — Preserves when extracting/forwarding blocks to consumers.
  • plan.py — Preserves when merging output bundles.

Where producer_op_ids Entries Are None

  • InputDataBuffer — Root-of-DAG bundles with no upstream operator.
  • Shuffle intermediate outputs (pull/push-based shuffle, split repartition) — Intermediate bundles inside _bulk_fn before AllToAllOperator stamps them.
  • Metrics-only bundles in HashShuffleOperator — Not real data flow.
  • equalize.py — Post-execution repackaging in Dataset.split().

Why add to RefBundle rather than BlockMetadata?

We chose RefBundle over BlockMetadata because:

  1. Producer tracking is a driver-side concern. DataOpTask on the driver already knows the operator ID — stamping it there avoids plumbing it through to workers who don't need it.
  2. BlockMetadata has much more surface area compared to RefBundles which are mostly covered by DataOpTask.on_data_ready(). There are 30+ construction sites across datasources, planners, exchange schedulers, block accessors, etc. — all would need updating for the same result.
  3. We can follow up with Goutam's BlockEntry suggestion to simplify the parallel tuple slicing/merging into a single per-block dataclass, keeping it on the driver side.

Justin Yu and others added 3 commits March 31, 2026 17:54
Add per-block producer tracking on RefBundle and per-producer byte tracking
in bundle queues to correctly attribute memory through ineligible operators
(Union, Limit, etc.) back to their upstream eligible operators.

- Add producer_op_ids tuple on RefBundle, propagated through slice/merge
- Stamp producer_op_id on DataOpTask output bundles
- Add estimate_size_bytes_for_producer to BundleQueue hierarchy
- Add per-producer filtering to OpBufferQueue and resource manager
- Collapse _internal_inqueues to single _internal_inqueue
- Remove input_index from on_input_queued/on_input_dequeued
- Mark Union as throttling_disabled (ineligible)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Revert the input_index -> producer_op_id migration on internal inqueues
to reduce scope. Keep only the changes needed for ineligible op attribution:
- producer_op_ids on RefBundle and propagation in merge/slice/split
- Per-producer byte tracking in BundleQueue (combined estimate_size_bytes API)
- OpBufferQueue.memory_usage_for_producer for external output queues
- Resource manager ineligible op attribution via producer filtering
- Union throttling_disabled
- Add TODOs for future input_index -> producer_op_id migration

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add a per-block `producer_op_ids` tuple to RefBundle that tracks which
eligible operator produced each block. This is plumbing for future
per-producer memory attribution in ineligible operators (Union, etc.).

- Add `producer_op_ids: Optional[Tuple[Optional[str], ...]]` to RefBundle
- Initialize to `(None,) * len(blocks)` by default in __post_init__
- Add `size_bytes_per_producer()` method for per-producer byte accounting
- Stamp producer_op_ids in DataOpTask.on_data_ready via new operator_id param
- Propagate through all RefBundle construction sites:
  - RefBundle.slice() and RefBundle.merge_ref_bundles()
  - _merge_ref_bundles in map_operator
  - _split in output_splitter
  - LimitOperator._add_input_inner
  - dedupe_schemas_with_validation
  - stream_split_iterator block extraction
  - randomize_blocks shuffle

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@justinvyu
justinvyu requested a review from a team as a code owner April 1, 2026 20:06

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces producer_op_ids to RefBundle and DataOpTask to enable per-producer memory attribution in Ray Data. The changes ensure that the UUID of the operator producing a block is tracked and propagated through various execution stages, including mapping, shuffling, limiting, and splitting operations. This metadata is utilized by a new method, size_bytes_per_producer, to calculate memory usage for downstream queue management. Feedback was provided regarding the duplication of block size estimation logic in RefBundle, suggesting that this logic be refactored into a shared helper method to improve maintainability and consistency.

Signed-off-by: Justin Yu <[email protected]>
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Apr 2, 2026
@goutamvenkat-anyscale

Copy link
Copy Markdown
Contributor

Some quick thoughts:
Does it make sense to create a new Dataclass to encapsulate all the new state:

@dataclass(frozen=True)                                                                                                                                               
  class BlockEntry:                                                                                                                                                     
      ref: ObjectRef[Block]                                                                                                                                             
      metadata: BlockMetadata                                                                                                                                           
      slice: Optional[BlockSlice] = None                                                                                                                                
      producer_op_id: Optional[ProducerId] = None

Should simplify the change overall

] = lambda metadata_ref: None,
task_resource_bundle: Optional[ExecutionResources] = None,
operator_name: str = "Unknown",
operator_id: Optional[str] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we use an alias here for operator_id type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to keep as string since the variable name is already clear.

else:
if not isinstance(self.producer_op_ids, tuple):
object.__setattr__(self, "producer_op_ids", tuple(self.producer_op_ids))
assert len(self.producer_op_ids) == len(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this be false for 1 producer to many blocks cases?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still one producer id per block. Ex: A single producer with 3 blocks would be ("uuid", "uuid", "uuid") — length 3 matches 3 blocks.

A single producer is the typical case, but I need to also account for merged bundles which could contain blocks from multiple producers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, the reason for storing the tuple is:

  • In the typical case, we output a bundle with a single block from a single producer: ex: ("uuid",)
  • However, operators like MapOperator and StreamingRepartition can merge multiple input bundles into a single task input via BlockRefBundler. When this happens downstream of a Union, the merged bundle may contain blocks from different producers, e.g., ("uuid_a", "uuid_b").
  • Per-block tracking ensures memory is correctly attributed to each producer even after merging.

@justinvyu

justinvyu commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@goutamvenkat-anyscale I think the BlockEntry is a good idea especially since we have 4 parallel tuples now (blocks, metadata, slices, and producer_op_ids).

However, this change would be a huge refactor since we use the blocks as Tuple[(ObjectRef, BlockMetadata)] in a lot of places. I want to get the functionality working first, then I can do the cleanup in a followup PR.

These bulk operators create new output blocks without going through
DataOpTask.on_data_ready(), so they need to stamp producer_op_ids
directly. This ensures correct attribution when their output flows
into downstream ineligible operators like Union.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- DataOpTask.on_data_ready stamps producer_op_ids with operator_id
- AllToAllOperator.all_inputs_done stamps output bundles
- RefBundle.slice() preserves producer_op_ids
- RefBundle.merge_ref_bundles() concatenates producer_op_ids
- LimitOperator preserves producer_op_ids
- OutputSplitter._split() preserves producer_op_ids
- Default, explicit, and length mismatch cases

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Justin Yu and others added 7 commits April 3, 2026 11:52
Remove the standalone _merge_ref_bundles function in map_operator.py
and use the RefBundle.merge_ref_bundles classmethod instead. This avoids
duplicating the merge logic including producer_op_ids concatenation.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
OutputSplitter is a terminal consumer (streaming_split), so producer_op_ids
propagation there doesn't affect Union attribution behavior.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Justin Yu <[email protected]>
AllToAll is the producer of its output bundles, so unconditionally
stamping with self.id is correct. Remove the unnecessary per-block
propagation in randomize_blocks since AllToAll overwrites it anyway.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Signed-off-by: Justin Yu <[email protected]>

@bveeramani bveeramani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

I don't think it's obvious from the PR description why we went with storing this on the RefBundle rather than on BlockMetadata. Could you include that context so it's accessible to future readers?

Comment on lines +72 to +74
# Per-block producer operator IDs, parallel to `blocks`.
# Each entry is the UUID of the eligible operator that produced that block.
# Used for per-producer memory attribution in downstream operators' queues.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what the expected meaning is if a block's producer_op_id is None? Does it mean that the producer is unknown, or that the block was created outside of the scope of this execution?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • InputDataBuffer — Root-of-DAG bundles with no upstream operator.
  • Shuffle intermediate outputs (pull/push-based shuffle, split repartition) — Intermediate bundles inside _bulk_fn before AllToAllOperator stamps them.
  • Metrics-only bundles in HashShuffleOperator — Not real data flow.
  • equalize.py — Post-execution repackaging in Dataset.split().

# Per-block producer operator IDs, parallel to `blocks`.
# Each entry is the UUID of the eligible operator that produced that block.
# Used for per-producer memory attribution in downstream operators' queues.
producer_op_ids: Optional[Tuple[Optional[str], ...]] = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a developer forgets to specify this field, this will silently default to None and possibly introduce subtle accounting bugs. There are only a few call sites where this is None -- should we just make this field required and explicitly pass in None there? Would be a tradeoff against boilerplate.

Think it'd also clarify the type. The double optional is a bit complicated: Optional[Tuple[Optional[str], ...]]

operator_name: The name of the physical operator that created this task.
Used for logging the operator name in warnings/errors.
operator_id: The UUID of the operator that created this task.
Stamped on output RefBundles for per-producer memory attribution.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Felt confused by "for per-producer memory attribution." at a first glance. Think this will be confusing to other readers with less context

] = lambda metadata_ref: None,
task_resource_bundle: Optional[ExecutionResources] = None,
operator_name: str = "Unknown",
operator_id: Optional[str] = None,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When can this be None? DataOpTask are always constructed by operators I thought?

Justin Yu and others added 2 commits April 7, 2026 20:16
- Move dataclasses import to top of test_limit_operator.py
- Remove _limit_reached() usage in test
- Use cleaner assertion pattern
- Improve docstrings on RefBundle.producer_op_id and DataOpTask.operator_id

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 17a67ca. Configure here.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Apr 22, 2026
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown

This pull request has been automatically closed because there has been no more activity in the 14 days
since being marked stale.

Please feel free to reopen or open a new pull request if you'd still like this to be addressed.

Again, you can always ask for help on our discussion forum or Ray's public slack channel.

Thanks again for your contribution!

@github-actions github-actions Bot closed this May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues stale The issue is stale. It will be closed within 7 days unless there are further conversation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants