""" Filtering rules """ def gopher_rules_pass(sample, quality_filtering=True, repetition_removal=True) -> bool: """ Returns True iff the sample complies with Gopher rules; https://arxiv.org/pdf/2112.11446.pdf can avoid multiple conditionals by bitwise operations if slow All numbers are from processing random 100 shards; Retained (document, character) count Total head (2.53M, 12.9B) """ signals = sample["quality_signals"] if quality_filtering: """ Below with each rule, retained doc/char count mentioned with rules applied incrementally: (1), (1 and 2), ... With all rules, retained 67.4% docs and 78.6% chars """ # head: (2.264M, 12.719B) # number of words between 50 and 100'000 -- 100'000 or 10'000? word_count = signals["rps_doc_word_count"][0][2] if word_count < 50 or word_count > 100_000: return False # head (2.258M, 12.647B) # mean word length between 3 and 10 mean_word_length = signals["rps_doc_mean_word_length"][0][2] if mean_word_length < 3 or mean_word_length > 10: return False # head (2.255M, 12.621B) # symbol to word ratio below 0.1 symbol_word_ratio = signals["rps_doc_symbol_to_word_ratio"][0][2] if symbol_word_ratio > 0.1: return False # head (2.255M, 12.621B) - only 64 docs filtered-out # lines starting with bullet point <= 10% n_lines = signals["ccnet_nlines"][0][2] n_lines_bulletpoint_start = sum(map(lambda ln: ln[2], signals["rps_lines_start_with_bulletpoint"])) if n_lines_bulletpoint_start / n_lines > 0.9: return False # head (2.190M, 12.457B) # lines ending with ellipsis <= 30% frac_lines_ellipsis_end = signals["rps_doc_frac_lines_end_with_ellipsis"][0][2] if frac_lines_ellipsis_end > 0.3: return False # head (1.705M, 10.146B) # words with no alphabetic character <= 20% # this is done on the raw data - with punctuation frac_no_alph_words = signals["rps_doc_frac_no_alph_words"][0][2] if frac_no_alph_words > 0.2: return False # stop-word filter: contain at least two of English stop-words # stop_words_list = ["the", "be", "to", "of", "and", "that", "have", "with"] # not present in red-pajama signals # can work with RedPajama-Data/app/src # (words obtained by splitting normalized string: RedPajama-Data/app/src/core/document.py) if repetition_removal: """ With all rules (quality filtering included), retained 60.6% docs and 68.2% chars """ # head (1.697M, 10.136B) # fraction of characters in the most frequent n-gram top_thresholds = {2: 0.2, 3: 0.18, 4: 0.16} for n, t in top_thresholds.items(): top_n_gram_frac = signals[f"rps_doc_frac_chars_top_{n}gram"][0][2] if top_n_gram_frac > t: return False # head (1.534M, 8.803B) # fraction of characters contained within all duplicate n-grams dup_thresholds = {5: 0.15, 6: 0.14, 7: 0.13, 8: 0.12, 9: 0.11, 10: 0.10} for n, t in dup_thresholds.items(): dup_n_gram_frac = signals[f"rps_doc_frac_chars_dupe_{n}grams"][0][2] if dup_n_gram_frac > t: return False # remaining from gopher repetition: # duplicate line/para/line-char/para-char fraction return True """ other: Pre-trainer's guide pg-34 - readability test - all-caps fraction Perplexity by model like llama? for filtering """