Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/twinkle/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,13 @@ def to_transformers_dict(inputs: List[InputFeature], **kwargs) -> List[InputFeat
for _input in inputs:
output = {}
_keys = [
'input_ids', 'input_embeddings', 'attention_mask', 'position_ids', 'labels', 'completion_mask',
'pixel_values', 'image_grid_thw'
]
'input_ids',
'input_embeddings',
'attention_mask',
'position_ids',
'labels',
'completion_mask',
] + list(InputProcessor.VLM_CONCAT_FIELDS)
Comment on lines 313 to +320
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better performance, this _keys list should be defined once outside the for _input in inputs: loop, preferably as a set for O(1) average time complexity for membership testing. It is constant for all iterations, so there is no need to reconstruct it every time.

for key in list(_input.keys()):
if key in _keys:
output[key] = np.array(_input[key]) if not isinstance(_input[key], torch.Tensor) else _input[key]
Expand Down
2 changes: 1 addition & 1 deletion src/twinkle/template/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _build_mm_messages(self, trajectory: Trajectory) -> List[Trajectory]:
message['images'] = self.preprocess_images(msg_images)
assert len(message['images']) == content.count(self.image_placeholder)
if msg_videos:
message['videos'] = self.preprocess_images(msg_videos)
message['videos'] = self.preprocess_videos(msg_videos)
assert len(message['videos']) == content.count(self.video_placeholder)
if msg_audios:
message['audios'] = self.preprocess_audios(msg_audios)
Expand Down
Loading