|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +from types import SimpleNamespace |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | +from fastvideo.models.encoders.qwen2_5_vl_custom import ( |
| 8 | + Qwen2_5_VisionTransformerPretrainedModel, |
| 9 | + Qwen2_5_VLForConditionalGenerationSimple, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def _vision_config(torch_dtype=None): |
| 14 | + return SimpleNamespace( |
| 15 | + torch_dtype=torch_dtype, |
| 16 | + spatial_merge_size=2, |
| 17 | + patch_size=14, |
| 18 | + fullatt_block_indexes=[], |
| 19 | + window_size=112, |
| 20 | + temporal_patch_size=2, |
| 21 | + in_channels=3, |
| 22 | + hidden_size=8, |
| 23 | + num_heads=2, |
| 24 | + depth=0, |
| 25 | + _attn_implementation="sdpa", |
| 26 | + out_hidden_size=8, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def _full_config(torch_dtype): |
| 31 | + return SimpleNamespace( |
| 32 | + vision_config=_vision_config(), |
| 33 | + torch_dtype=torch_dtype, |
| 34 | + vocab_size=16, |
| 35 | + hidden_size=8, |
| 36 | + num_hidden_layers=0, |
| 37 | + num_attention_heads=2, |
| 38 | + pad_token_id=0, |
| 39 | + _attn_implementation="sdpa", |
| 40 | + rms_norm_eps=1e-6, |
| 41 | + max_position_embeddings=32, |
| 42 | + rope_theta=1_000_000.0, |
| 43 | + rope_scaling=None, |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def test_vision_dtype_uses_parent_bfloat16_string_when_vision_dtype_missing(): |
| 48 | + model = Qwen2_5_VisionTransformerPretrainedModel( |
| 49 | + _vision_config(), |
| 50 | + parent_torch_dtype="bfloat16", |
| 51 | + ) |
| 52 | + |
| 53 | + assert model.dtype == torch.bfloat16 |
| 54 | + |
| 55 | + |
| 56 | +def test_vision_dtype_accepts_parent_torch_dtype_object(): |
| 57 | + model = Qwen2_5_VisionTransformerPretrainedModel( |
| 58 | + _vision_config(), |
| 59 | + parent_torch_dtype=torch.bfloat16, |
| 60 | + ) |
| 61 | + |
| 62 | + assert model.dtype == torch.bfloat16 |
| 63 | + |
| 64 | + |
| 65 | +def test_vision_dtype_strips_parent_dtype_string_whitespace(): |
| 66 | + model = Qwen2_5_VisionTransformerPretrainedModel( |
| 67 | + _vision_config(), |
| 68 | + parent_torch_dtype=" torch.bfloat16 ", |
| 69 | + ) |
| 70 | + |
| 71 | + assert model.dtype == torch.bfloat16 |
| 72 | + |
| 73 | + |
| 74 | +def test_vision_dtype_prefers_explicit_vision_dtype_over_parent_dtype(): |
| 75 | + model = Qwen2_5_VisionTransformerPretrainedModel( |
| 76 | + _vision_config(torch_dtype="float16"), |
| 77 | + parent_torch_dtype="bfloat16", |
| 78 | + ) |
| 79 | + |
| 80 | + assert model.dtype == torch.float16 |
| 81 | + |
| 82 | + |
| 83 | +def test_vision_dtype_falls_back_to_float32_for_missing_or_unknown_dtype(): |
| 84 | + missing = Qwen2_5_VisionTransformerPretrainedModel(_vision_config()) |
| 85 | + unknown = Qwen2_5_VisionTransformerPretrainedModel( |
| 86 | + _vision_config(), |
| 87 | + parent_torch_dtype="not-a-real-dtype", |
| 88 | + ) |
| 89 | + |
| 90 | + assert missing.dtype == torch.float32 |
| 91 | + assert unknown.dtype == torch.float32 |
| 92 | + |
| 93 | + |
| 94 | +def test_conditional_generation_passes_parent_dtype_to_visual_tower(): |
| 95 | + model = Qwen2_5_VLForConditionalGenerationSimple(_full_config("torch.bfloat16")) |
| 96 | + |
| 97 | + assert model.visual.dtype == torch.bfloat16 |
0 commit comments