| 45 | return self.input_ids.size(0) |
| 46 | |
| 47 | class LMPackDataset(torch.utils.data.Dataset): |
| 48 | def __init__(self, filepath): |
| 49 | self.input_ids, self.attention_masks, self.labels, self.weights, self.nums = self.process_data(filepath) |
| 50 | self.num_gpus = torch.cuda.device_count() |
| 51 | |
| 52 | def process_data(self, filepath): |
| 53 | input_ids = torch.from_numpy(np.load(os.path.join(filepath, 'inputs_pack.npy'))) |
| 54 | labels = torch.from_numpy(np.load(os.path.join(filepath, 'labels_pack.npy'))) |
| 55 | weights = torch.from_numpy(np.load(os.path.join(filepath, 'weights_pack.npy'))) |
| 56 | attention_masks = json.load(open(os.path.join(filepath, 'attention_masks_pack.json'))) |
| 57 | num_gpus = torch.cuda.device_count() |
| 58 | l = (input_ids.size(0) // num_gpus) * num_gpus |
| 59 | input_ids, labels, weights, attention_masks = input_ids[:l, :], labels[:l, :], weights[:l, :], attention_masks[:l] |
| 60 | nums = [weights[i*num_gpus:(i+1)*num_gpus, :].sum() for i in range(l//num_gpus)] |
| 61 | return input_ids, attention_masks, labels, weights, nums |
| 62 | |
| 63 | def __getitem__(self, idx): |
| 64 | return { |
| 65 | 'input_ids': self.input_ids[idx], |
| 66 | 'attention_mask': torch.tensor(self.attention_masks[idx], dtype=torch.int32), |
| 67 | 'labels': (self.labels[idx], self.weights[idx], self.nums[idx//self.num_gpus]) |
| 68 | } |
| 69 | |
| 70 | def __len__(self): |
| 71 | return self.input_ids.size(0) |
no outgoing calls
no test coverage detected