import os import json import gzip import gc import numpy as np from datasets import load_from_disk from tqdm import tqdm RAW_DATASET_PATH = "/home/ubuntu/Complete-HF-Dataset-3" TMP_JSONL = "/home/ubuntu/tmp_batch.jsonl.gz" start_chunk = 0 num_chunks = 100 CHUNK_SIZE = 300 split_name = "train" raw_ds = load_from_disk(RAW_DATASET_PATH) dataset_split = raw_ds[split_name] dataset_split.set_format(type="numpy", columns=["input_features", "labels"]) total_len = len(dataset_split) start_idx = start_chunk * CHUNK_SIZE end_idx = min((start_chunk + num_chunks) * CHUNK_SIZE, total_len) print(f"Processing indices {start_idx} → {end_idx}") kept = 0 with gzip.open(TMP_JSONL, "wt") as f: for i in tqdm(range(start_idx, end_idx)): try: sample = dataset_split[i] feats = np.array(sample["input_features"], copy=True) labels = np.array(sample["labels"], copy=True) if ( feats.ndim == 2 and feats.size > 0 and np.all(np.isfinite(feats)) # Slightly faster numpy check and feats.shape[-1] >= 10 and labels.size > 1 ): f.write( json.dumps( {"input_features": feats.tolist(), "labels": labels.tolist()} ) + "\n" ) kept += 1 del feats, labels, sample except Exception as e: print(f"Error at index {i}: {e}") print(f"✅ Kept {kept} samples")