MCPcopy Create free account
hub / github.com/eric-mitchell/direct-preference-optimization / concatenated_inputs

Function concatenated_inputs

trainers.py:118–142  ·  view source on GitHub ↗

Concatenate the chosen and rejected inputs into a single tensor. Args: batch: A batch of data. Must contain the keys 'chosen_input_ids' and 'rejected_input_ids', which are tensors of shape (batch_size, sequence_length). Returns: A dictionary containing the conca

(batch: Dict[str, Union[List, torch.LongTensor]])

Source from the content-addressed store, hash-verified

116
117
118def concatenated_inputs(batch: Dict[str, Union[List, torch.LongTensor]]) -> Dict[str, torch.LongTensor]:
119 """Concatenate the chosen and rejected inputs into a single tensor.
120
121 Args:
122 batch: A batch of data. Must contain the keys 'chosen_input_ids' and 'rejected_input_ids', which are tensors of shape (batch_size, sequence_length).
123
124 Returns:
125 A dictionary containing the concatenated inputs under the key 'concatenated_input_ids'.
126 """
127 max_length = max(batch['chosen_input_ids'].shape[1], batch['rejected_input_ids'].shape[1])
128 concatenated_batch = {}
129 for k in batch:
130 if k.startswith('chosen') and isinstance(batch[k], torch.Tensor):
131 pad_value = -100 if 'labels' in k else 0
132 concatenated_key = k.replace('chosen', 'concatenated')
133 concatenated_batch[concatenated_key] = pad_to_length(batch[k], max_length, pad_value=pad_value)
134 for k in batch:
135 if k.startswith('rejected') and isinstance(batch[k], torch.Tensor):
136 pad_value = -100 if 'labels' in k else 0
137 concatenated_key = k.replace('rejected', 'concatenated')
138 concatenated_batch[concatenated_key] = torch.cat((
139 concatenated_batch[concatenated_key],
140 pad_to_length(batch[k], max_length, pad_value=pad_value),
141 ), dim=0)
142 return concatenated_batch
143
144
145class BasicTrainer(object):

Callers 1

concatenated_forwardMethod · 0.85

Calls 1

pad_to_lengthFunction · 0.90

Tested by

no test coverage detected