File size: 8,165 Bytes
fe1c232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
- Used in RPv2 exploration, including:
    - Plot certain quality signals
    - Get doc/char counts (e.g. after filtering)
    - Store minhashes of filtered documents for further dedup
"""

import gzip
import orjson
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
from tqdm import tqdm
import copy
import multiprocessing
import random
import pathlib
import seaborn as sns
from rules.rules import gopher_rules_pass
import pyarrow

ROOT_PATH = "/home1/BharatGPT_Data/RedPajamaV2"
DATA_ROOT_PATH = "/home1/BharatGPT_Data/RedPajamaV2/data"
PLOTS_ROOT_PATH = "/home1/BharatGPT_Data/RedPajamaV2/plots"
SNAPSHOT = "2023-14"
LANGUAGE = "en"
PARTITION_KEY = "head"
SIGNALS_DIR = os.path.join(DATA_ROOT_PATH, "quality_signals", SNAPSHOT)
DUPLICATES_DIR = os.path.join(DATA_ROOT_PATH, "duplicates", SNAPSHOT)
MINHASH_DIR = os.path.join(DATA_ROOT_PATH, "minhash", SNAPSHOT)
NUM_CORES = 60
SEED = 2024
DO_PLOT = False
COUNT_ONLY = True
STORE_SIGNALS = False
OUTPUT_FILES = False

# constants for random 100
NUM_SHARDS_PROCESSED = 100
if DO_PLOT:
    PLOTS_DIR = os.path.join(PLOTS_ROOT_PATH, PARTITION_KEY, f"random_{NUM_SHARDS_PROCESSED}_gopher", "quality_rep_log") # makes dir if not present
NUM_DOCS_HEAD = 2533743
NUM_DOCS_MIDDLE = 3722022
NUM_CHARS_HEAD = 12932890455
NUM_CHARS_MIDDLE = 17883781733

NUM_SHARDS_PROCESSED = 5000
if OUTPUT_FILES:
    OUT_DIR = os.path.join(DATA_ROOT_PATH, f"minhash_filtered", SNAPSHOT) # makes dir if not present

random.seed(SEED)
np.random.seed(SEED)

assert sorted(os.listdir(SIGNALS_DIR)) == sorted(os.listdir(DUPLICATES_DIR))
assert (not DO_PLOT) or STORE_SIGNALS # DO_PLOT implies STORE_SIGNALS

if COUNT_ONLY:
    assert DO_PLOT == False, "Plotting requires storing signal values"
    all_shards_counts = {
        "doc_count": 0,
        "char_count": 0
    }
elif STORE_SIGNALS:
    all_shards_signals = {
        "ccnet_perplexity": [],
        # https://huggingface.co/datasets/togethercomputer/RedPajama-Data-V2
        # mentions that data is raw (no deduplicated)
        # but based on doc length it looks line-deduped
        # so we use ccnet_length and not ccnet_original_length
        "ccnet_length": [],
        "rps_doc_stop_word_fraction": [],
        "rps_doc_lorem_ipsum": []
    }
    all_shards_signals_empty = copy.deepcopy(all_shards_signals)

# non-parallelized
# for shard in tqdm(sorted(os.listdir(SIGNALS_DIR))[:10]):
#     signals_path = os.path.join(SIGNALS_DIR, shard, f"{LANGUAGE}_{PARTITION}.signals.json.gz")
#     duplicates_path = os.path.join(DUPLICATES_DIR, shard, f"{LANGUAGE}_{PARTITION}.duplicates.parquet")
#     shard_dups = pd.read_parquet(duplicates_path)
#     shard_dups_set = set(shard_dups["doc_id"].tolist())
#     with gzip.open(signals_path, 'r') as signals_file:
#         for line in signals_file:
#             signals_dict = json.loads(line)
#             if signals_dict["id"] not in shard_dups_set:
#                 # print(json.dumps(signals_dict, indent=4))
#                 for k in list(all_shards_signals.keys()):
#                     assert len(signals_dict["quality_signals"][k]) == 1
#                     assert len(signals_dict["quality_signals"][k][0]) == 3
#                     all_shards_signals[k].append(signals_dict["quality_signals"][k][0][-1])

partitions_dict = {
    "head": ["head"],
    "middle": ["middle"],
    "head_middle": ["head", "middle"],
}

for partition in partitions_dict[PARTITION_KEY]:
    def process_shard(shard):

        # check if done already
        if OUTPUT_FILES:
            out_path_shard = os.path.join(OUT_DIR, shard)
            out_file_path = os.path.join(out_path_shard, f"{LANGUAGE}_{partition}_filtered.minhash.parquet")
            if os.path.exists(out_file_path):
                raise Exception("ERROR: output file already present")
                return []
            minhash_path = os.path.join(MINHASH_DIR, shard, f"{LANGUAGE}_{partition}.minhash.parquet")
            try:
                df = pd.read_parquet(minhash_path)
            except pyarrow.lib.ArrowInvalid as __e:
                # occurs with empty file
                print(f"ERROR with shard {shard}: empty minhash file")
                return []

        signals_path = os.path.join(SIGNALS_DIR, shard, f"{LANGUAGE}_{partition}.signals.json.gz")
        try:
            duplicates_path = os.path.join(DUPLICATES_DIR, shard, f"{LANGUAGE}_{partition}.duplicates.parquet")
            shard_dups = pd.read_parquet(duplicates_path, columns=["doc_id"])
            shard_dups_set = set(shard_dups["doc_id"].tolist())
        except pyarrow.lib.ArrowInvalid as __e:
            # occurs with empty file
            shard_dups_set = set()

        results = None
        if COUNT_ONLY:
            results = {
                "doc_count": 0,
                "char_count": 0
            }
        elif STORE_SIGNALS:
            results = copy.deepcopy(all_shards_signals_empty)
        elif OUTPUT_FILES:
            results = [] # filtered indices
            idx = -1

        with gzip.open(signals_path, 'r') as signals_file:
            for line in signals_file:
                idx += 1
                signals_dict = orjson.loads(line)
                if signals_dict["id"] not in shard_dups_set and gopher_rules_pass(signals_dict):
                    """
                    Note about exact duplicates:
                    https://github.com/togethercomputer/RedPajama-Data/issues/84#issuecomment-1840299911
                    One copy remains with this method
                    """
                    if COUNT_ONLY:
                        results["doc_count"] += 1
                        results["char_count"] += signals_dict["quality_signals"]["ccnet_length"][0][2]
                    elif STORE_SIGNALS:
                        for k in list(results.keys()):
                            assert len(signals_dict["quality_signals"][k]) == 1
                            assert len(signals_dict["quality_signals"][k][0]) == 3
                            results[k].append(signals_dict["quality_signals"][k][0][2])
                    elif OUTPUT_FILES:
                        results.append(idx)

        if OUTPUT_FILES:
            df = df.iloc[results, :]
            pathlib.Path(out_path_shard).mkdir(parents=True, exist_ok=True)
            df.to_parquet(os.path.join(out_path_shard, f"{LANGUAGE}_{partition}_filtered.minhash.parquet"))

        return results

    with multiprocessing.Pool(NUM_CORES) as pool:
        shards_list = os.listdir(SIGNALS_DIR)
        all_results = list(tqdm(pool.imap(process_shard, random.sample(sorted(shards_list), k=NUM_SHARDS_PROCESSED)), total=NUM_SHARDS_PROCESSED))

    for results in all_results:
        if COUNT_ONLY:
            for k in list(all_shards_counts.keys()):
                all_shards_counts[k] += results[k]
        elif STORE_SIGNALS:
            for k in list(all_shards_signals.keys()):
                all_shards_signals[k].extend(results[k])

# print(json.dumps(all_shards_signals))
if COUNT_ONLY:
    print(all_shards_counts)

if DO_PLOT:
    pathlib.Path(PLOTS_DIR).mkdir(parents=True, exist_ok=True)
    for k, v in all_shards_signals.items():
        # plt.hist(v, bins=100) # linear

        # log with plt
        # hist, bins = np.histogram(v, bins=100)
        # logbins = np.logspace(np.log10(bins[0]),np.log10(bins[-1]),len(bins))
        # plt.hist(v, weights=np.ones(len(v))/(NUM_DOCS_HEAD+NUM_DOCS_MIDDLE), bins=logbins) # log
        # plt.xscale("log")

        # unweighted
        sns.histplot(x=v, bins=100, log_scale=True)

        # weights: docs percentage of head_middle
        # sns.histplot(x=v, weights=np.ones(len(v))*100/(NUM_DOCS_HEAD+NUM_DOCS_MIDDLE), bins=100, log_scale=True)
        # weight by char percentage
        # sns.histplot(x=v, weights=np.array(all_shards_signals["ccnet_length"])*100/(NUM_CHARS_HEAD+NUM_CHARS_MIDDLE), bins=100, log_scale=True)
        plt.savefig(os.path.join(PLOTS_DIR, f"{k}.png"))
        plt.close()

# print(len(all_shards_signals["ccnet_perplexity"]))
# print(sum(all_shards_signals["ccnet_length"]))
# print(type(all_shards_signals["ccnet_length"][0]))