[Data] read_mcap: a sample step takes the nearest message however far away it is
Problem
- In
read_mcap(samples=...), a step takes the nearest message on its side, however far from the step that message is. - The step is still marked as real data (
is_padis False). A training job then gets an old frame as if it were current. match.backward()andsteps.acrossbehave the same way.- On a topic that went quiet,
match.backward()reads back chunk by chunk to the last message. A run lets those chunks go when it ends, so the next run reads them again.
Repro
This runs on the #67189 branch (read_mcap(samples=...) is not merged yet).
A 10 fps camera that stops logging at 1.0 s, and one sample anchored at 4.0 s that reads the 0.3 s before it.
import json, os, tempfile
import ray
from mcap.writer import Writer
from ray.data.datasource.mcap import TopicSpec, steps
SECOND = 1_000_000_000
path = os.path.join(tempfile.mkdtemp(), "camera.mcap")
# A 10 fps camera that stops logging at 1.0 s.
with open(path, "wb") as f:
writer = Writer(f)
writer.start()
schema = writer.register_schema("Frame", "jsonschema", b"{}")
cam = writer.register_channel("/cam", "json", schema)
for i in range(11): # 0.0, 0.1, ..., 1.0 s
t = i * SECOND // 10
writer.add_message(cam, log_time=t, publish_time=t, data=json.dumps({"t": t}).encode())
writer.finish()
samples = ray.data.from_items(
[{"files": [path], "clip_start": 0, "clip_end": 5 * SECOND, "anchor": 4 * SECOND}]
)
ds = ray.data.read_mcap(
samples=samples, topics={"/cam": TopicSpec(steps.before(0.3, fps=10))}
)
row = ds.take(1)[0]
print("frame times (s):", [t / SECOND for t in row["times:/cam"]])
print("is_pad: ", list(row["is_pad:/cam"]))
Output:
frame times (s): [1.0, 1.0, 1.0]
is_pad: [False, False, False]
| Current | Expected |
|---|---|
| The steps at 3.8, 3.9 and 4.0 s all hold the frame logged at 1.0 s. | No frame lies within half a step (50 ms) of those steps, so all three are padded. |
is_pad:/cam is [False, False, False]. |
is_pad:/cam is [True, True, True], and the payload follows pad=, as for a step past the clip's edge. |
With match.backward() on a topic that went quiet, every run reads that topic's old chunks again. In a 1,003-chunk file without message indexes, 99 samples parse 50,095 chunks. |
A step reads only the chunks within its tolerance, so a quiet topic's old chunks are never read. |
The same file read with match.backward() or steps.across(5) also gives 1.0 s frames with is_pad False. The chunk count comes from the case in the second review comment below, read as one task with the chunk parses counted.
Proposal
match.nearest()andmatch.backward()take atolerance: the most a message may be from its step.- The default is half a step,
0.5 / fps, asread_lerobot'sframe_tolerance_sis and as pandasmerge_asof(tolerance=)allows. Forsteps.across(n), a step is the spacing between steps. - A step with no message within the tolerance is padded as
pad=says, andis_padis True for it. tolerance=Nonekeeps today's behaviour, for topics that log rarely, such as a map or a calibration.- The reader stops looking back at the tolerance, so
match.backward()no longer reads chunks far before its step. - To decide: whether
match.backward()should default to one full step instead. A camera whose frames sit between the steps has its latest frame up to one step old, and half a step would pad it.steps.athas no spacing, so it needs an explicit tolerance or none.
Follow-up to #67189 (review comments https://github.com/ray-project/ray/pull/67925#discussion_r4516962691 and https://github.com/ray-project/ray/pull/67927#discussion_r4517229336).
🤖 Generated with Claude Code
Hi @marwan116, I'd like to work on this. I'd add a max-distance tolerance for read_mcap(samples=...) steps: when the nearest message is farther than that, the step is padded (is_pad=True) instead of reusing an old frame. The same would apply to match.backward() and steps.across, with tests. Okay to pick it up?