Skip to content

[Data] Fix premature termination in Dataset.mix with STOP_ON_SHORTEST - #68630

Open
m4ru1 wants to merge 2 commits into
ray-project:masterfrom
m4ru1:fix/mix-stop-on-shortest
Open

m4ru1 wants to merge 2 commits into
ray-project:masterfrom
m4ru1:fix/mix-stop-on-shortest

Conversation

@m4ru1

@m4ru1 m4ru1 commented Nov 28, 2026 •

Copy link
Copy Markdown

Summary

Allow underrepresented inputs to catch up before Dataset.mix(..., stopping_condition=MixStoppingCondition.STOP_ON_SHORTEST) stops, and release operator-held references to discarded buffered inputs when the operator finishes, closing #68628. Open PR #67506 rejects non-finite Dataset.mix weights at initialization rather than changing runtime stopping or input cleanup.

Before

Calling ds1.mix(ds2, ..., stopping_condition=MixStoppingCondition.STOP_ON_SHORTEST) stops pulling inputs as soon as any completed input's buffer becomes empty, even if slower or finer-grained inputs still owe rows toward their target share (for example, returning [20, 0] instead of [20, 20] at weights=[1, 1]). Before completion is reported, floating-point rounding on decimal weights can also break an exact tie and pull an extra block ([63, 28] instead of [63, 27] at [0.7, 0.3]), and discarded buffered inputs remain referenced by operator metrics during output draining and while the operator stays reachable.

After

MixOperator now computes row deficits with exact rational weights for finite weights and, under STOP_ON_SHORTEST, stops when a completed input has zero rows or once the next deficit-selected input is exhausted. When a zero-row completed input stops the mix while the output buffer is empty, one available empty block is queued first so zero-row results keep their schema; finishing or early shutdown then dequeues remaining buffered inputs from both input queues and runtime metrics.

STOP_ON_LONGEST_DROP still skips exhausted inputs and consumes the remaining datasets, and available blocks from the selected input continue to emit before other upstream tasks finish.

flowchart LR
  subgraph Before
    B1["Execution: MixOperator (stops when any completed input empties)"] -. skips owed pulls & keeps metric refs .-> B2["Dataset.mix truncates owed rows & retains discarded refs"]
  end
  subgraph After
    A1["Execution: MixOperator (stops on empty input or exhausted selected input)"] -. pulls owed blocks & dequeues metric refs .-> A2["Dataset.mix lets owed inputs catch up & releases refs"]
  end
  classDef bad fill:#ffcdd2,stroke:#c62828,color:#000
  classDef changed fill:#fff9c4,stroke:#f9a825,color:#000
  classDef good fill:#c8e6c9,stroke:#2e7d32,color:#000
  class B1,B2 bad
  class A1 changed
  class A2 good
Loading

Subsystems

Execution.

Testing

Local validation passed on Python 3.11.15 / Ray 2.60.1 (arm64 macOS) and Python 3.12.16 / Ray 3.0.0.dev0 (e4462656e8, aarch64 Linux, PyArrow 26.0.1) with the updated ray.data sources loaded on both driver and workers. Tests cover uneven block boundaries across 2- and 3-way mixes, task and gated arrival orders, exact decimal/integer/rational weight ties (including large integer weights), empty-input schema preservation across input positions, and reference/metrics release for discarded input blocks.

for test in test_mix.py; do
  python -m pytest -q "python/ray/data/tests/$test" || exit 1
done

Pre-commit hooks (including pyrefly-data) and pytest lint checks also passed.

AI assistance was used.

Track row deficits in MixOperator with exact rational weights for finite
weights and, under STOP_ON_SHORTEST, stop only when a completed input has
zero rows or the next deficit-selected input is exhausted. Queue one
available empty block before stopping on a zero-row input when the output
buffer is empty so zero-row results retain their schema, and dequeue
remaining buffered inputs from runtime metrics when finishing.

Closes ray-project#68628

Signed-off-by: mora <[email protected]>
@m4ru1
m4ru1 requested a review from a team as a code owner November 28, 2026 20:32

@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 enhances the MixOperator to correctly handle the STOP_ON_SHORTEST stopping condition, stopping execution when the next selected input is exhausted or when any input is known to be empty. It also introduces exact weight tracking using Fraction to prevent rounding errors and deficit ties, and ensures that discarded inputs are properly dequeued to release bundle references. A review comment suggests directly converting weights to strings instead of casting them to floats first when constructing Fraction objects to avoid potential precision loss.

exact_weights = [
Fraction(w)
if isinstance(w, (int, Fraction))
else Fraction(str(float(w)))

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.

medium

Converting w to float before converting to str can cause precision loss for large integer types (such as numpy.int64 or other custom numeric types) that are not standard Python int or Fraction. Directly converting w to str is safer and preserves full precision.

Suggested change
else Fraction(str(float(w)))
else Fraction(str(w))

…egral weights

Override clear_internal_input_queue and clear_internal_output_queue on MixOperator so early executor shutdown drops runtime-metric references alongside normal completion, and accept numbers.Integral weights without float rounding loss.

Signed-off-by: mora <[email protected]>
@ray-gardener ray-gardener Bot added data Ray Data-related issues community-contribution Contributed by the community labels Nov 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant