MCPcopy
hub / github.com/ModelTC/LightLLM / encode

Method encode

lightllm/models/vit/model.py:173–198  ·  view source on GitHub ↗
(self, images: List[ImageItem])

Source from the content-addressed store, hash-verified

171
172 @torch.no_grad()
173 def encode(self, images: List[ImageItem]):
174 img_tensors = []
175 valid_ids = []
176 valid_id = 0
177 uuids = []
178 for i, img in enumerate(images):
179 if isinstance(img, ImageItem):
180 uuids.append(img.uuid)
181 image_data = read_shm(get_shm_name_data(img.uuid))
182 image_data = Image.open(BytesIO(image_data))
183 t = self.load_image_func(image_data, max_num=img.extra_params["image_patch_max_num"])
184 img_tensors.append(t)
185 else:
186 raise Exception("Unsupport input types: {} for {}".format(type(img), img))
187
188 cur_num = img_tensors[-1].shape[0]
189 valid_ids.append([valid_id, valid_id + cur_num])
190 valid_id += cur_num
191
192 if len(img_tensors) <= 0:
193 return None
194
195 imgs = torch.cat(img_tensors, dim=0)
196 pixel_values = imgs.cuda().to(dtype=self.data_type)
197 all_img_embeds = self.forward(pixel_values)
198 return all_img_embeds, uuids, valid_ids
199
200 def cuda(self):
201 return self

Callers

nothing calls this directly

Calls 5

forwardMethod · 0.95
read_shmFunction · 0.90
get_shm_name_dataFunction · 0.90
formatMethod · 0.80
cudaMethod · 0.45

Tested by

no test coverage detected