Skip to content

compute_episode_data_index silently returns wrong boundaries when episode lengths are corrupted #4143

Description

@Kunal-Somani

Problem

compute_episode_data_index() in lerobot/common/datasets/utils.py builds dataset_from_index and dataset_to_index boundaries from a cumulative sum of episode["length"] metadata values without validating that those lengths are positive or internally consistent.

If episode metadata lengths are wrong for any reason (converter bug, manual edit, partial write, corruption), the function silently returns wrong boundaries. Frames then load without error but belong to the wrong episodes during training — a silent data quality failure with no diagnostic signal.

Reproduction

from lerobot.common.datasets.utils import compute_episode_data_index
import torch

# Simulate metadata where episode 1 has a wrong length
episodes = {
    0: {"length": 50},
    1: {"length": 0},   # corrupted: should be 50
    2: {"length": 50},
}
total_frames = 150  # actual data has 150 frames

# Returns boundaries as if episode 1 has 0 frames
# Episode 2 gets frames that belong to episode 1
result = compute_episode_data_index(episodes)
print(result)
# from_index: [0, 50, 50]  <- episode 1 gets no frames
# to_index:   [50, 50, 100] <- episode 2 starts where episode 1 should

Suggested fix

Add validation at the start of compute_episode_data_index that raises a clear ValueError when any episode length is <= 0, naming the episode index and declared length in the message so the caller can diagnose the source dataset.

Context

I found this while building trajlens (https://github.com/Kunal-Somani/trajlens), a dataset validation and repair tool for LeRobot datasets. The trajlens episode_reindex fixer detects this class of corruption by comparing declared boundaries against per-row episode_index values in the actual data shards. This upstream validation would provide an earlier, clearer signal at dataset load time rather than silent wrong-frame loading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn’t working correctlydatasetIssues regarding data inputs, processing, or datasetstrainingIssues related at training time

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions