ray icon indicating copy to clipboard operation
ray copied to clipboard

[Data] Dataset.mix with STOP_ON_SHORTEST stops prematurely before reaching target row ratios

Open m4ru1 opened this issue 6 days ago • 0 comments

What happened + What you expected to happen

Calling Dataset.mix(..., weights=..., stopping_condition=MixStoppingCondition.STOP_ON_SHORTEST) stops pulling new blocks as soon as any completed input's buffer becomes empty, even when that input's last block put it ahead of its target weight and other inputs still owe matching rows. As a result, output row counts vary with upstream task timing and block sizes (and can drop slower inputs to 0 rows), while unconsumed buffered blocks from truncated longer datasets stay referenced during output draining and while the executor remains reachable (such as on an iterated Dataset).

# ds_short has one 20-row block; ds_long has ten 10-row blocks:
ds_short.mix(ds_long, weights=[1, 1], stopping_condition=MixStoppingCondition.STOP_ON_SHORTEST).take_all()  # -> {0: 20, 1: 20}

# Same data, when ds_long comes from map_batches -> {0: 20} (0 rows from ds_long)
ds_short.mix(ds_long.map_batches(lambda b: b), weights=[1, 1], stopping_condition=MixStoppingCondition.STOP_ON_SHORTEST).take_all()

Affected STOP_ON_SHORTEST pipelines include:

  • Mixing a materialized or coarse-block dataset with a task-computed (map_batches / read_*) dataset, which can truncate the slower dataset to 0 rows
  • Mixing 3+ datasets where a single-block non-shortest dataset finishes before task-computed datasets emit blocks, halting the mix before the shortest dataset produces any rows
  • Materialized mixes with decimal weights such as [0.7, 0.3] (for example, one 63-row block mixed with 100 single-row blocks), where floating-point rounding pulls an extra block past an exact 7:3 ratio ([63, 28] instead of [63, 27])

Expected behavior: For fixed input blocks, Dataset.mix(..., weights=..., stopping_condition=MixStoppingCondition.STOP_ON_SHORTEST) should allow underrepresented inputs to catch up before stopping—producing identical row counts regardless of upstream task timing (such as [20, 20] and [63, 27] above)—and release references to unconsumed buffered blocks as soon as mixing stops.

Cause: In python/ray/data/_internal/execution/operators/mix_operator.py, MixOperator._try_output stops as soon as any input is exhausted before underrepresented inputs catch up (and uses floating-point normalized weights that can shift exact-ratio ties), while MixOperator.mark_execution_finished clears buffered inputs without dequeuing them from operator metrics.

Versions / Dependencies

  • Ray 3.0.0.dev0 (master e4462656e8) and 2.60.1
  • Python 3.12.16 / 3.11.15, PyArrow 26.0.1, Linux (aarch64) and macOS (arm64)

Reproduction script

import numpy as np
import pyarrow as pa
import ray

ray.init(num_cpus=4, include_dashboard=False)
ray.data.DataContext.get_current().enable_progress_bars = False


def dataset(source, sizes):
    return ray.data.from_blocks(
        [pa.table({"source": np.full(n, source, dtype=np.int64)}) for n in sizes]
    )


try:
    for name, sizes, weights in [
        ("equal", [[20], [10] * 10], [1, 1]),
        ("decimal", [[63], [1] * 100], [0.7, 0.3]),
        ("unnormalized", [[30], [1] * 100], [0.1, 0.2]),
    ]:
        left = dataset(0, sizes[0])
        right = dataset(1, sizes[1]).map_batches(lambda b: b, batch_size=None)
        rows = left.mix(
            right,
            weights=weights,
            stopping_condition=ray.data.MixStoppingCondition.STOP_ON_SHORTEST,
        ).take_all()
        counts = [sum(row["source"] == i for row in rows) for i in range(2)]
        print(name, counts, flush=True)
finally:
    ray.shutdown()

Output:

equal [20, 0]
decimal [63, 0]
unnormalized [30, 1]

Issue Severity

Medium: It is a significant difficulty but I can work around it.

m4ru1 avatar Nov 28 '26 20:11 m4ru1