MCPcopy Create free account
hub / github.com/SpatialVLA/SpatialVLA / concat_pad_data_collator

Function concat_pad_data_collator

train/monkey_patch.py:21–75  ·  view source on GitHub ↗
(features, pad_id=0)

Source from the content-addressed store, hash-verified

19
20# data patch
21def concat_pad_data_collator(features, pad_id=0):
22 first = features[0]
23 batch = {}
24
25 batch_lens = [feat['input_ids'].shape for feat in features]
26 max_item_length = max(batch_lens)[0]
27 for idx in range(len(features)):
28 feat = features[idx]
29 temp_input_ids = torch.LongTensor([pad_id] * max_item_length)
30 temp_input_ids[:feat['input_ids'].shape[0]] = feat['input_ids']
31 feat['input_ids'] = temp_input_ids
32
33 temp_labels = torch.LongTensor([IGNORE_INDEX] * max_item_length)
34 temp_labels[:feat['labels'].shape[0]] = feat['labels']
35 feat['labels'] = temp_labels
36 feat['attention_mask'] = feat['input_ids'].ne(pad_id)
37
38 # handel temp_token_type_ids for gemma
39 temp_token_type_ids = torch.LongTensor([0] * max_item_length) # pad with 0 to indicate first scentence
40 temp_token_type_ids[:feat['token_type_ids'].shape[0]] = feat['token_type_ids']
41 feat['token_type_ids'] = temp_token_type_ids
42
43 # Special handling for labels.
44 # Ensure that tensor is created with the correct type
45 # (it should be automatically the case, but let's make sure of it.)
46 if 'label' in first and first['label'] is not None:
47 label = first['label'].item() if isinstance(first['label'], torch.Tensor) else first['label']
48 dtype = torch.long if isinstance(label, int) else torch.float
49 batch['labels'] = torch.tensor([f['label'] for f in features], dtype=dtype)
50 elif 'label_ids' in first and first['label_ids'] is not None:
51 if isinstance(first['label_ids'], torch.Tensor):
52 batch['labels'] = torch.stack([f['label_ids'] for f in features])
53 else:
54 dtype = torch.long if isinstance(first['label_ids'][0], int) else torch.float
55 batch['labels'] = torch.tensor([f['label_ids'] for f in features], dtype=dtype)
56
57 # Handling of all other possible keys.
58 # Again, we will use the first element to figure out which key/values are not None for this model.
59 for k, v in first.items():
60 if k not in ('label', 'label_ids', 'pixel_values', 'image_flags') and \
61 v is not None and not isinstance(v, str):
62 if isinstance(v, torch.Tensor):
63 batch[k] = torch.stack([f[k] for f in features])
64 elif isinstance(v, np.ndarray):
65 batch[k] = torch.tensor(np.stack([f[k] for f in features]))
66 else:
67 batch[k] = torch.tensor([f[k] for f in features])
68 if k in ('pixel_values', 'image_flags'):
69 if isinstance(v, torch.Tensor):
70 batch[k] = torch.concat([f[k] for f in features])
71 elif isinstance(v, np.ndarray):
72 batch[k] = torch.concat(np.stack([f[k] for f in features]))
73 else:
74 batch[k] = torch.concat([f[k] for f in features])
75 return batch
76
77# copy from https://github.com/haotian-liu/LLaVA/blob/main/llava/train/llava_trainer.py#L38
78def split_to_even_chunks(indices, lengths, num_chunks):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected