File size: 1,563 Bytes
6a125ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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")