<jupyter_start><jupyter_text>BackgroundThis notebook walks through the creation of a fastai [DataBunch](https://docs.fast.ai/basic_data.htmlDataBunch) object.  This object contains a pytorch dataloader for the train, valid and test sets. From the documentation:```Bind train_dl,valid_dl and test_dl in a data object.It also ensures all the dataloaders are on device and applies to them dl_tfms as batch are drawn (like normalization). path is used internally to store temporary files, collate_fn is passed to the pytorch Dataloader (replacing the one there) to explain how to collate the samples picked for a batch.```Because we are training the language model, we want our dataloader to construct the target variable from the input data.  The target variable for language models are the next word in a sentence.  Furthermore, there are other optimizations with regard to the sequence length and concatenating texts together that avoids wasteful padding.  Luckily the [TextLMDataBunch](https://docs.fast.ai/text.data.htmlTextLMDataBunch) does all this work for us (and more) automatically.<jupyter_code>from fastai.text import TextLMDataBunch as lmdb
from fastai.text.transform import Tokenizer
import pandas as pd
from pathlib import Path<jupyter_output><empty_output><jupyter_text>Read in Data You can download the above saved dataframes (in pickle format) from Google Cloud Storage:**train_df.hdf (9GB)**: `https://storage.googleapis.com/issue_label_bot/pre_processed_data/2_partitioned_df/train_df.hdf`**valid_df.hdf (1GB)**`https://storage.googleapis.com/issue_label_bot/pre_processed_data/2_partitioned_df/valid_df.hdf`<jupyter_code># note: download the data and place in right directory before running this code!

valid_df = pd.read_hdf(Path('../data/2_partitioned_df/valid_df.hdf'))
train_df = pd.read_hdf(Path('../data/2_partitioned_df/train_df.hdf'))
print(f'rows in train_df:, {train_df.shape[0]:,}')
print(f'rows in valid_df:, {valid_df.shape[0]:,}')
train_df.head(3)<jupyter_output><empty_output><jupyter_text>Create The [DataBunch](https://docs.fast.ai/basic_data.htmlDataBunch)  Instantiate The Tokenizer<jupyter_code>def pass_through(x):
    return x
# only thing is we are changing pre_rules to be pass through since we have already done all of the pre-rules.  
# you don't want to accidentally apply pre-rules again otherwhise it will corrupt the data.
tokenizer = Tokenizer(pre_rules=[pass_through], n_cpus=31)<jupyter_output><empty_output><jupyter_text>Specify path for saving language model artifacts<jupyter_code>path = Path('../model/lang_model/')<jupyter_output><empty_output><jupyter_text>Create  The Language Model Data Bunch**Warning**: this steps builds the vocabulary and tokenizes the data.  This procedure consumes an incredible amount of memory. This took 1 hour on a machine with 72 cores and 400GB of Memory.<jupyter_code># Note you want your own tokenizer, without pre-rules
data_lm = lmdb.from_df(path=path,
                       train_df=train_df,
                       valid_df=valid_df,
                       text_cols='text',
                       tokenizer=tokenizer,
                       chunksize=6000000)
data_lm.save() # saves to self.path/data_save.hdf<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08379715522572666, 'fraction_numerical': 0.0074211502782931356, 'mean_word_length': 5.0130111524163565, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '17749', 'license': 'MIT', 'n_tokens_mistral': 999, 'n_tokens_neox': 963, 'n_words': 315, 'path': 'Issue_Embeddings/notebooks/02_fastai_DataBunch.ipynb', 'repo_name': 'Bobgy/code-intelligence', 'size': '3234'}