MCPcopy Create free account
hub / github.com/THUDM/GLM / my_collate

Function my_collate

tasks/data_utils.py:310–346  ·  view source on GitHub ↗
(batch)

Source from the content-addressed store, hash-verified

308
309
310def my_collate(batch):
311 new_batch = [{key: value for key, value in sample.items() if key != 'uid'} for sample in batch]
312 text_list = [sample['text'] for sample in batch]
313
314 def pad_choice_dim(data, choice_num):
315 if len(data) < choice_num:
316 data = np.concatenate([data] + [data[0:1]] * (choice_num - len(data)))
317 return data
318
319 if len(text_list[0].shape) == 2:
320 choice_nums = list(map(len, text_list))
321 max_choice_num = max(choice_nums)
322 for i, sample in enumerate(new_batch):
323 for key, value in sample.items():
324 if key != 'label':
325 sample[key] = pad_choice_dim(value, max_choice_num)
326 else:
327 sample[key] = value
328 sample['loss_mask'] = np.array([1] * choice_nums[i] + [0] * (max_choice_num - choice_nums[i]),
329 dtype=np.int64)
330
331 if 'dec_text' in new_batch[0]:
332 choice_nums = [len(sample['dec_text']) for sample in new_batch]
333 if choice_nums.count(choice_nums[0]) != len(choice_nums):
334 max_choice_num = max(choice_nums)
335 for i, sample in enumerate(new_batch):
336 for key, value in sample.items():
337 if key.startswith('dec_'):
338 sample[key] = pad_choice_dim(value, max_choice_num)
339 sample['loss_mask'] = np.array([1] * choice_nums[i] + [0] * (max_choice_num - choice_nums[i]),
340 dtype=np.int64)
341
342 new_batch = default_collate(new_batch)
343 if 'uid' in batch[0]:
344 uid_list = [sample['uid'] for sample in batch]
345 new_batch['uid'] = uid_list
346 return new_batch
347
348
349class FakeDataloader:

Callers

nothing calls this directly

Calls 1

pad_choice_dimFunction · 0.85

Tested by

no test coverage detected