Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix qwen2vl video args #2251

Merged
merged 1 commit into from
Oct 15, 2024
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
31 changes: 23 additions & 8 deletions swift/llm/utils/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ def replace_tag(self, media_type: Literal['image', 'video', 'audio'], index: int

def _process_image_qwen(image):
from qwen_vl_utils.vision_process import IMAGE_FACTOR, MIN_PIXELS, MAX_PIXELS, smart_resize
size_factor = get_env_args('size_factor', int, IMAGE_FACTOR)
size_factor = get_env_args('image_factor', int, IMAGE_FACTOR, ['size_factor'])
# resize
resized_height = get_env_args('resized_height', int, None)
resized_width = get_env_args('resized_width', int, None)
Expand Down Expand Up @@ -1679,6 +1679,10 @@ def data_collator(self, batch: List[Dict[str, Any]], padding_to: Optional[int] =
res['position_ids'] = position_ids.contiguous()
return res

@staticmethod
def _get_generate_ids(generate_ids: List[int], input_token_len: int) -> List[int]:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

FIX: #2248

return generate_ids


class Qwen2VLTemplate(_Qwen2VLTemplateMixin, QwenTemplate):
pass
Expand Down Expand Up @@ -2170,16 +2174,27 @@ def _get_generate_ids(generate_ids: List[int], input_token_len: int) -> List[int
_log_set = set() # log once


def get_env_args(args_name: str, type_func: Callable[[str], _T], default_value: Optional[_T]) -> Optional[_T]:
args_name_upper = args_name.upper()
value = os.getenv(args_name_upper)
if value is None:
def get_env_args(args_name: str,
type_func: Callable[[str], _T],
default_value: Optional[_T],
compat_args_names: Optional[List[str]] = None) -> Optional[_T]:
# compat_args_names: compatibility
if compat_args_names is None:
compat_args_names = []
args_name_list = [args_name] + compat_args_names
for args_name in args_name_list:
args_name_upper = args_name.upper()
value = os.getenv(args_name_upper)
if value is not None:
value = type_func(value)
log_info = f'Using environment variable `{args_name_upper}`, Setting {args_name}: {value}.'
break
else:
args_name = args_name_list[0]
args_name_upper = args_name.upper()
value = default_value
log_info = (f'Setting {args_name}: {default_value}. '
f'You can adjust this hyperparameter through the environment variable: `{args_name_upper}`.')
else:
value = type_func(value)
log_info = f'Using environment variable `{args_name_upper}`, Setting {args_name}: {value}.'
if log_info not in _log_set:
_log_set.add(log_info)
logger.info(log_info)
Expand Down
12 changes: 6 additions & 6 deletions swift/llm/utils/vision_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def load_video_qwen2(video_path: str):
)
nframes = get_env_args('nframes', int, None)
fps = get_env_args('fps', int, None)
size_factor = get_env_args('size_factor', int, FRAME_FACTOR)
size_factor = get_env_args('frame_factor', int, FRAME_FACTOR, ['size_factor'])
assert not (fps and nframes), 'Only accept either `fps` or `nframes`'
if nframes is not None:
nframes = round_by_factor(nframes, size_factor)
Expand All @@ -296,8 +296,8 @@ def load_video_qwen2(video_path: str):
fps = FPS
nframes = video.size(0) / info['video_fps'] * fps
nframes = round_by_factor(nframes, size_factor)
min_frames = get_env_args('min_frames', int, FPS_MIN_FRAMES)
max_frames = get_env_args('max_frames', int, FPS_MAX_FRAMES)
min_frames = get_env_args('fps_min_frames', int, FPS_MIN_FRAMES, ['min_frames'])
max_frames = get_env_args('fps_max_frames', int, FPS_MAX_FRAMES, ['max_frames'])
if nframes < min_frames:
nframes = ceil_by_factor(min_frames, size_factor)
if nframes > max_frames:
Expand All @@ -310,9 +310,9 @@ def load_video_qwen2(video_path: str):
height, width = video.shape[2:]
video = video[idx]

min_pixels = get_env_args('min_pixels', int, VIDEO_MIN_PIXELS)
total_pixels = get_env_args('total_pixels', int, VIDEO_TOTAL_PIXELS)
max_pixels = get_env_args('max_pixels', int, None)
min_pixels = get_env_args('video_min_pixels', int, VIDEO_MIN_PIXELS, ['min_pixels'])
total_pixels = get_env_args('video_total_pixels', int, VIDEO_TOTAL_PIXELS, ['total_pixels'])
max_pixels = get_env_args('video_max_pixels', int, None, ['max_pixels'])
if max_pixels is None:
max_pixels = VIDEO_MAX_PIXELS
max_pixels = max(min(max_pixels, total_pixels / nframes * size_factor), min_pixels * 1.05)
Expand Down
Loading