Download code/multi-hop/utils/training/collator/collator.py from fhyfhy/Implicit-Reasoning-checkpoints: direct link, hf CLI and curl.
- Browser
- Download file 1.81 kB
-
https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/utils/training/collator/collator.py
- Command line
-
hf download hf://fhyfhy/Implicit-Reasoning-checkpoints/code/multi-hop/utils/training/collator/collator.py
-
curl -L -o collator.py https://huggingface.co/fhyfhy/Implicit-Reasoning-checkpoints/resolve/main/code/multi-hop/utils/training/collator/collator.py
1.81 kB
| from itertools import chain | |
| from transformers import DefaultDataCollator | |
| import torch | |
| from typing import Optional | |
| import os | |
| class DynamicCollator(DefaultDataCollator): | |
| def __init__(self, tokenizer, block_size): | |
| self.tokenizer = tokenizer | |
| self.block_size = block_size | |
| def concat(self, examples): | |
| concat_input_ids = list(chain(*[e["input_ids"] for e in examples])) | |
| concat_mask = list(chain(*[e["attention_mask"] for e in examples])) | |
| total_length = len(concat_input_ids) | |
| # Truncate to multiple of block_size | |
| if total_length >= self.block_size: | |
| total_length = (total_length // self.block_size) * self.block_size | |
| concat_input_ids = concat_input_ids[:total_length] | |
| concat_mask = concat_mask[:total_length] | |
| # print(torch.sum(torch.stack(concat_weights[:self.block_size]))) | |
| # print(512 / torch.sum(torch.stack(concat_weights[:self.block_size]))) | |
| # print([x * (512 / torch.sum(torch.stack(concat_weights[:self.block_size]))) for x in concat_weights[:self.block_size]]) | |
| result = [{"input_ids": concat_input_ids[i:i+self.block_size], | |
| "attention_mask": concat_mask[i:i+self.block_size], | |
| "labels": concat_input_ids[i:i+self.block_size], | |
| } for i in range(0, total_length, self.block_size) | |
| ] | |
| return result | |
| def __call__(self, examples): | |
| # print("*" * 50) | |
| # rank = os.getenv("LOCAL_RANK") | |
| # decoded = [self.tokenizer.decode(e["input_ids"]) for e in examples] | |
| # print(f"Rank {rank}") | |
| # print(decoded) | |
| examples = self.concat(examples) | |
| # use the default data collator to convert the list of features into a batch | |
| return super().__call__(examples) |