WilliamSotoM
commited on
Commit
•
38bbc9d
1
Parent(s):
5dcfdfb
Handle Batch Sizes
Browse filesI hope I'm not overstepping boundaries. I tried the fix on #16 to handle batch sizes larger than 1 but stumbled upon a few issues. For example, issues processing single strings as the text input or not handling padding when the input texts are of different size.
I tried to address all the issues I had. Hope this can be of use.
@gugarosa
feel free to have a look and let me know if I'm overlooking something.
Best,
William
- processing_phi3_v.py +25 -11
processing_phi3_v.py
CHANGED
@@ -73,9 +73,7 @@ class Phi3VProcessor(ProcessorMixin):
|
|
73 |
|
74 |
Args:
|
75 |
text (`str`, `List[str]`, `List[List[str]]`):
|
76 |
-
The sequence or batch of sequences to be encoded. Each sequence
|
77 |
-
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
|
78 |
-
`is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
|
79 |
images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
|
80 |
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
|
81 |
tensor. Both channels-first and channels-last formats are supported.
|
@@ -150,7 +148,15 @@ class Phi3VProcessor(ProcessorMixin):
|
|
150 |
return BatchFeature(data={**model_inputs})
|
151 |
|
152 |
pattern = r"<\|image_\d+\|>"
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
if 'num_img_tokens' in images:
|
156 |
num_img_tokens = images['num_img_tokens']
|
@@ -162,30 +168,38 @@ class Phi3VProcessor(ProcessorMixin):
|
|
162 |
images, image_sizes = images['pixel_values'], images['image_sizes']
|
163 |
|
164 |
# image_tags needs to start from 1 to n
|
165 |
-
image_tags = re.findall(pattern, texts)
|
166 |
# image_ids = [int(s.split("|")[1].split("_")[-1]) * -1 for s in image_tags]
|
167 |
# image_ids_pad = [[iid]*num_img_tokens[i] for i, iid in enumerate(image_ids)]
|
168 |
-
image_ids = [int(s.split("|")[1].split("_")[-1]) for s in image_tags]
|
169 |
-
unique_image_ids = sorted(list(set(image_ids)))
|
170 |
# image_ids must start from 1, and must be continuous int, e.g. [1, 2, 3], cannot be [1, 4, 5]
|
171 |
# check the condition
|
172 |
assert unique_image_ids == list(range(1, len(unique_image_ids)+1)), f"image_ids must start from 1, and must be continuous int, e.g. [1, 2, 3], cannot be {unique_image_ids}"
|
173 |
# total images must be the same as the number of image tags
|
174 |
assert len(unique_image_ids) == len(images), f"total images must be the same as the number of image tags, got {len(unique_image_ids)} image tags and {len(images)} images"
|
175 |
|
176 |
-
image_ids_pad = [[-iid]*num_img_tokens[iid-1] for iid in image_ids]
|
177 |
|
178 |
def insert_separator(X, sep_list):
|
179 |
if len(X) > len(sep_list):
|
180 |
sep_list.append([])
|
181 |
return [ele for sublist in zip(X, sep_list) for ele in sublist]
|
182 |
input_ids = []
|
183 |
-
|
184 |
-
|
185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
input_ids = torch.tensor(input_ids, dtype=torch.long).unsqueeze(0)
|
188 |
attention_mask = (input_ids > -1000000).to(torch.long)
|
|
|
189 |
|
190 |
return BatchFeature(data={"input_ids": input_ids,
|
191 |
"attention_mask": attention_mask,
|
|
|
73 |
|
74 |
Args:
|
75 |
text (`str`, `List[str]`, `List[List[str]]`):
|
76 |
+
The sequence or batch of sequences to be encoded. Each sequence must be a string.
|
|
|
|
|
77 |
images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
|
78 |
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
|
79 |
tensor. Both channels-first and channels-last formats are supported.
|
|
|
148 |
return BatchFeature(data={**model_inputs})
|
149 |
|
150 |
pattern = r"<\|image_\d+\|>"
|
151 |
+
|
152 |
+
if isinstance(texts, str):
|
153 |
+
texts = [texts]
|
154 |
+
|
155 |
+
prompt_chunks = []
|
156 |
+
image_tags = []
|
157 |
+
for text in texts:
|
158 |
+
prompt_chunks.append([self.tokenizer(chunk).input_ids for chunk in re.split(pattern, text)])
|
159 |
+
image_tags.append(re.findall(pattern, text))
|
160 |
|
161 |
if 'num_img_tokens' in images:
|
162 |
num_img_tokens = images['num_img_tokens']
|
|
|
168 |
images, image_sizes = images['pixel_values'], images['image_sizes']
|
169 |
|
170 |
# image_tags needs to start from 1 to n
|
171 |
+
# image_tags = re.findall(pattern, texts)
|
172 |
# image_ids = [int(s.split("|")[1].split("_")[-1]) * -1 for s in image_tags]
|
173 |
# image_ids_pad = [[iid]*num_img_tokens[i] for i, iid in enumerate(image_ids)]
|
174 |
+
image_ids = [[int(s.split("|")[1].split("_")[-1]) for s in tags] for tags in image_tags]
|
175 |
+
unique_image_ids = sorted(list(set([iid for ids in image_ids for iid in ids])))
|
176 |
# image_ids must start from 1, and must be continuous int, e.g. [1, 2, 3], cannot be [1, 4, 5]
|
177 |
# check the condition
|
178 |
assert unique_image_ids == list(range(1, len(unique_image_ids)+1)), f"image_ids must start from 1, and must be continuous int, e.g. [1, 2, 3], cannot be {unique_image_ids}"
|
179 |
# total images must be the same as the number of image tags
|
180 |
assert len(unique_image_ids) == len(images), f"total images must be the same as the number of image tags, got {len(unique_image_ids)} image tags and {len(images)} images"
|
181 |
|
182 |
+
image_ids_pad = [[[-iid]*num_img_tokens[iid-1] for iid in ids] for ids in image_ids]
|
183 |
|
184 |
def insert_separator(X, sep_list):
|
185 |
if len(X) > len(sep_list):
|
186 |
sep_list.append([])
|
187 |
return [ele for sublist in zip(X, sep_list) for ele in sublist]
|
188 |
input_ids = []
|
189 |
+
for sub_prompt_chunks, sub_image_ids_pad in zip(prompt_chunks, image_ids_pad):
|
190 |
+
input_ids.append([])
|
191 |
+
offset = 0
|
192 |
+
for x in insert_separator(sub_prompt_chunks, sub_image_ids_pad):
|
193 |
+
input_ids[-1].extend(x[offset:])
|
194 |
+
|
195 |
+
max_length = max(len(ids) for ids in input_ids)
|
196 |
+
for i in range(len(input_ids)):
|
197 |
+
while len(input_ids[i]) < max_length:
|
198 |
+
input_ids[i] = [self.tokenizer.pad_token_id]+input_ids[i]
|
199 |
|
200 |
input_ids = torch.tensor(input_ids, dtype=torch.long).unsqueeze(0)
|
201 |
attention_mask = (input_ids > -1000000).to(torch.long)
|
202 |
+
attention_mask[input_ids == self.tokenizer.pad_token_id] = 0
|
203 |
|
204 |
return BatchFeature(data={"input_ids": input_ids,
|
205 |
"attention_mask": attention_mask,
|