Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions examples/specdec_bench/specdec_bench/datasets/speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,10 +737,40 @@ def _load_dataset(self, config_name_or_dataset_path: config_type | str) -> "Data
}
table = table.replace_schema_metadata(new_meta or None)
dataset = HFDataset(table)
if self.num_samples is not None:
dataset = dataset.select(range(self.num_samples))
if self.num_samples is not None and self.num_samples < len(dataset):
dataset = self._stratified_select(dataset, self.num_samples)
return dataset

@staticmethod
def _stratified_select(dataset: "Dataset", n: int) -> "Dataset":
"""Select ``n`` samples uniformly across the ``category`` column.

Round-robin across categories until ``n`` rows are collected. The
resulting prefix is balanced; once a smaller category is exhausted
the remaining categories continue contributing, so exactly ``n``
rows are returned whenever ``n`` does not exceed the dataset size.
Falls back to ``range(n)`` when ``category`` is absent or there is
only one category. Indices come from ``range(category_size)`` (not
random) so behavior is deterministic.
"""
if "category" not in dataset.column_names:
return dataset.select(range(n))
cat_to_rows: dict[str, list[int]] = {}
for i, c in enumerate(dataset["category"]):
cat_to_rows.setdefault(c, []).append(i)
if len(cat_to_rows) <= 1:
return dataset.select(range(n))
cat_lists = list(cat_to_rows.values())
interleaved: list[int] = []
max_len = max(len(c) for c in cat_lists)
for i in range(max_len):
for c in cat_lists:
if i < len(c):
interleaved.append(c[i])
if len(interleaved) == n:
return dataset.select(interleaved)
return dataset.select(interleaved)
Comment thread
milesial marked this conversation as resolved.

def _resolve_external_data(
self, dataset: "Dataset", speed_config: config_type | str
) -> "Dataset":
Expand Down
Loading