()
| 140 | return final_dataset |
| 141 | |
| 142 | def main(): |
| 143 | args = parse_args() |
| 144 | |
| 145 | raw_datasets = datasets.load_dataset(args.dataset_name, "wikitext-103-raw-v1") |
| 146 | |
| 147 | tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_path) |
| 148 | |
| 149 | lm_datasets = tokenize_and_group_text(raw_datasets, tokenizer, args.block_size, args.stride, args.padding_index, args.num_proc) |
| 150 | |
| 151 | # Dictionary to hold the summary info for each split |
| 152 | dstore_summary = {} |
| 153 | |
| 154 | # Iterate over all splits in the lm_datasets (each key is a split name, e.g., "train", "validation", etc.) |
| 155 | for split_name, split_dataset in lm_datasets.items(): |
| 156 | dstore_size = 0 |
| 157 | dataset_cnt = [] |
| 158 | |
| 159 | # Compute dataset count and dstore_size for the current split. |
| 160 | for chunk in split_dataset['labels']: |
| 161 | cur_len = len([x for x in chunk[1:] if x != args.padding_index]) |
| 162 | dstore_size += cur_len |
| 163 | dataset_cnt.append(cur_len) |
| 164 | |
| 165 | # Log and print the computed dstore_size for this split. |
| 166 | logger.info(f"Split '{split_name}': Setting dstore size to {dstore_size}!") |
| 167 | print(f"Split '{split_name}': dstore size = {dstore_size}") |
| 168 | |
| 169 | # Store the results in our summary dictionary. |
| 170 | dstore_summary[split_name] = { |
| 171 | "dstore_size": dstore_size, |
| 172 | "dataset_cnt_len": len(dataset_cnt) |
| 173 | } |
| 174 | |
| 175 | # Compute the dstore_range for each example in the split. |
| 176 | # The dstore_range is a list of (start, end) indices for each chunk. |
| 177 | idx = 0 |
| 178 | dstore_range = [] |
| 179 | for cnt in dataset_cnt: |
| 180 | dstore_range.append((idx, idx + cnt)) |
| 181 | idx += cnt |
| 182 | |
| 183 | # Add the computed dstore_range column to the current dataset split. |
| 184 | lm_datasets[split_name] = split_dataset.add_column("dstore_range", dstore_range) |
| 185 | |
| 186 | if not os.path.exists(args.output_dir): |
| 187 | os.makedirs(args.output_dir) |
| 188 | |
| 189 | # Finally, save the lm_datasets (with the new 'dstore_range' column added to each split) to disk. |
| 190 | lm_datasets.save_to_disk(args.output_dir) |
| 191 | logger.info(f"lm_datasets saved to {args.output_dir}") |
| 192 | |
| 193 | # Save the summary information (which includes dstore_size for each split) to a JSON file. |
| 194 | json_file_path = os.path.join(args.output_dir, "dstore_summary.json") |
| 195 | with open(json_file_path, "w") as f: |
| 196 | json.dump(dstore_summary, f, indent=4) |
| 197 | logger.info(f"Saved dstore summary to {json_file_path}") |
| 198 | |
| 199 | if __name__ == "__main__": |
no test coverage detected