(examples, padding_value=0, max_length=2048)
| 102 | |
| 103 | |
| 104 | def data_collator(examples, padding_value=0, max_length=2048): |
| 105 | def trim_and_pad(seq, batch_first, padding_value): |
| 106 | return pad_sequence([s[:max_length] for s in seq], batch_first=True, padding_value=padding_value) |
| 107 | |
| 108 | input_ids = trim_and_pad( |
| 109 | [example["input_ids"] for example in examples], |
| 110 | batch_first=True, |
| 111 | padding_value=padding_value, |
| 112 | ) |
| 113 | position_ids = trim_and_pad( |
| 114 | [example["position_ids"] for example in examples], |
| 115 | batch_first=True, |
| 116 | padding_value=padding_value, |
| 117 | ) |
| 118 | targets = trim_and_pad( |
| 119 | [example["labels"] for example in examples], |
| 120 | batch_first=True, |
| 121 | padding_value=-100, |
| 122 | ) |
| 123 | attention_mask = trim_and_pad( |
| 124 | [example["attention_mask"] for example in examples], |
| 125 | batch_first=True, |
| 126 | padding_value=padding_value, |
| 127 | ) |
| 128 | pixel_values = [example["pixel_values"] for example in examples] |
| 129 | image_bound = [example["image_bound"] for example in examples] |
| 130 | tgt_sizes = [example["tgt_sizes"] for example in examples] |
| 131 | return { |
| 132 | "input_ids": input_ids, |
| 133 | "position_ids": position_ids, |
| 134 | "labels": targets, |
| 135 | "attention_mask": attention_mask, |
| 136 | "image_bound": image_bound, |
| 137 | "tgt_sizes": tgt_sizes, |
| 138 | "pixel_values": pixel_values, |
| 139 | } |
| 140 | |
| 141 | |
| 142 | def conversation_to_ids(conversation, tokenizer, llm_type=None, new_schema=False, max_length=2048): |
nothing calls this directly
no test coverage detected