From 8936cc223453252aaa73fc329cc18c6d1414eb7e Mon Sep 17 00:00:00 2001 From: "serge[bot]" Date: Thu, 29 Oct 2026 22:40:47 +0000 Subject: [PATCH 1/4] [serge] Fix 5 integration tests for model `qwen3_vl_moe` failing with `other`, raising `RuntimeError` (other (5)) --- .../test_modeling_qwen3_vl_moe.py | 70 ++++++++++++++----- 1 file changed, 52 insertions(+), 18 deletions(-) diff --git a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py index 63e64de60b93..d61c0f7bd964 100644 --- a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py +++ b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py @@ -13,6 +13,7 @@ # limitations under the License. """Testing suite for the PyTorch Qwen3VLMoe model.""" +import tempfile import unittest import pytest @@ -27,6 +28,8 @@ from transformers.models.qwen3_vl_moe.configuration_qwen3_vl_moe import Qwen3VLMoeTextConfig, Qwen3VLMoeVisionConfig from transformers.testing_utils import ( Expectations, + backend_device_count, + get_cpu_ram_total_gib, require_flash_attn, require_torch, require_torch_accelerator, @@ -301,6 +304,49 @@ def test_reverse_loading_mapping(self, check_keys_were_modified=False): class Qwen3VLMoeIntegrationTest(MemoryCleanupMixin, unittest.TestCase): maxDiff = None + @classmethod + def setUpClass(cls): + super().setUpClass() + # Assigned here rather than in the class body: `MemoryCleanupMixin` snapshots the class body and + # protects it from teardown, so a checkpoint parked on a class-body `model` is never released. + cls.model = None + cls.offload_dir = None + + @classmethod + def get_model(cls): + if cls.model is None: + cls.offload_dir = tempfile.TemporaryDirectory() + # device_map="auto" fills GPUs to ~100%, leaving no room for the transpose temporary + # buffer that fuses per-expert weight shards into a single gate_up_proj tensor during + # from_pretrained — causing CUDA OOM on multi-GPU runners. A 70% per-GPU max_memory cap + # reserves the headroom. + n = backend_device_count(torch_device) + if n > 0 and torch_device != "cpu": + torch_accel = getattr(torch, torch_device) + per_device = int( + min(torch_accel.get_device_properties(i).total_memory for i in range(n)) * 0.70 / 1024**3 + ) + max_memory = dict.fromkeys(range(n), f"{per_device}GiB") + max_memory["cpu"] = ( + f"{int(get_cpu_ram_total_gib() * 0.9)}GiB" # To avoid runner failing with exit code 137. + ) + else: + max_memory = None + cls.model = Qwen3VLMoeForConditionalGeneration.from_pretrained( + "Qwen/Qwen3-VL-30B-A3B-Instruct", + dtype="auto", + device_map="auto", + max_memory=max_memory, + offload_folder=cls.offload_dir.name, + ) + return cls.model + + @classmethod + def tearDownClass(cls): + if cls.offload_dir is not None: + cls.offload_dir.cleanup() + super().tearDownClass() + def setUp(self): super().setUp() @@ -351,9 +397,7 @@ def setUp(self): @slow def test_small_model_integration_test(self): - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto" - ) + model = self.get_model() inputs = self.processor.apply_chat_template( self.message, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt" @@ -394,9 +438,7 @@ def test_small_model_integration_test(self): @slow def test_small_model_integration_test_batch(self): - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto" - ) + model = self.get_model() batch_messages = [self.message] * 2 inputs = self.processor.apply_chat_template( batch_messages, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt" @@ -462,9 +504,7 @@ def test_small_model_integration_test_with_video(self): @slow def test_small_model_integration_test_expand(self): - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto" - ) + model = self.get_model() inputs = self.processor.apply_chat_template( self.message, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt" ).to(torch_device) @@ -490,9 +530,7 @@ def test_small_model_integration_test_expand(self): @slow def test_small_model_integration_test_expand_with_video(self): - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto" - ) + model = self.get_model() inputs = self.processor.apply_chat_template( self.message3, tokenize=True, add_generation_prompt=True, return_dict=True, return_tensors="pt" ).to(torch_device) @@ -511,9 +549,7 @@ def test_small_model_integration_test_expand_with_video(self): @slow def test_small_model_integration_test_batch_wo_image(self): - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto" - ) + model = self.get_model() message_wo_image = [ {"role": "user", "content": [{"type": "text", "text": "Who are you?"}]}, ] @@ -541,9 +577,7 @@ def test_small_model_integration_test_batch_wo_image(self): @slow def test_small_model_integration_test_batch_different_resolutions(self): - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto" - ) + model = self.get_model() batched_messages = [self.message, self.message2] inputs = self.processor.apply_chat_template( batched_messages, From 8f2d5fc6446005bb49e1747d738681394b2af1ec Mon Sep 17 00:00:00 2001 From: ydshieh Date: Mon, 16 Nov 2026 17:51:30 +0100 Subject: [PATCH 2/4] Use the shared capped model in with_video; cap the FA2 loads and run them first - `test_small_model_integration_test_with_video` loaded its own uncapped copy (fp16) and still OOMed while converting `experts.gate_up_proj` on 2xA10G. It now uses the shared `get_model()` (bf16). The fp16 choice had no stated reason (added in #41277); the bf16 output matches the existing expected text. - The two FA2 tests keep their own load (different `attn_implementation`), now with the same 70% per-GPU `max_memory` cap and an `offload_folder`, and are marked `@run_first` so the shared model is not yet loaded. - The cap computation is moved to `get_max_memory()` so both load paths use it. Co-Authored-By: Claude Opus 5.5 --- .../test_modeling_qwen3_vl_moe.py | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py index 75d06ec6293e..6469ed1d7129 100644 --- a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py +++ b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py @@ -32,6 +32,7 @@ require_flash_attn, require_torch, require_torch_accelerator, + run_first, slow, torch_device, ) @@ -297,31 +298,32 @@ def setUpClass(cls): cls.model = None cls.offload_dir = None + @staticmethod + def get_max_memory(): + # device_map="auto" fills GPUs to ~100%, leaving no room for the transpose temporary + # buffer that fuses per-expert weight shards into a single gate_up_proj tensor during + # from_pretrained — causing CUDA OOM on multi-GPU runners. A 70% per-GPU max_memory cap + # reserves the headroom. + n = backend_device_count(torch_device) + if n > 0 and torch_device != "cpu": + torch_accel = getattr(torch, torch_device) + per_device = int(min(torch_accel.get_device_properties(i).total_memory for i in range(n)) * 0.70 / 1024**3) + max_memory = dict.fromkeys(range(n), f"{per_device}GiB") + max_memory["cpu"] = ( + f"{int(get_cpu_ram_total_gib() * 0.9)}GiB" # To avoid runner failing with exit code 137. + ) + return max_memory + return None + @classmethod def get_model(cls): if cls.model is None: cls.offload_dir = tempfile.TemporaryDirectory() - # device_map="auto" fills GPUs to ~100%, leaving no room for the transpose temporary - # buffer that fuses per-expert weight shards into a single gate_up_proj tensor during - # from_pretrained — causing CUDA OOM on multi-GPU runners. A 70% per-GPU max_memory cap - # reserves the headroom. - n = backend_device_count(torch_device) - if n > 0 and torch_device != "cpu": - torch_accel = getattr(torch, torch_device) - per_device = int( - min(torch_accel.get_device_properties(i).total_memory for i in range(n)) * 0.70 / 1024**3 - ) - max_memory = dict.fromkeys(range(n), f"{per_device}GiB") - max_memory["cpu"] = ( - f"{int(get_cpu_ram_total_gib() * 0.9)}GiB" # To avoid runner failing with exit code 137. - ) - else: - max_memory = None cls.model = Qwen3VLMoeForConditionalGeneration.from_pretrained( "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto", - max_memory=max_memory, + max_memory=cls.get_max_memory(), offload_folder=cls.offload_dir.name, ) return cls.model @@ -459,9 +461,7 @@ def test_small_model_integration_test_with_video(self): processor = AutoProcessor.from_pretrained( "Qwen/Qwen3-VL-30B-A3B-Instruct", max_image_size={"longest_edge": 50176} ) - model = Qwen3VLMoeForConditionalGeneration.from_pretrained( - "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype=torch.float16, device_map="auto" - ) + model = self.get_model() questions = ["How long is the video? Describe the it in short."] video_urls = ["https://huggingface.co/datasets/hf-internal-testing/fixtures_videos/resolve/main/tennis.mp4"] messages = [ @@ -610,16 +610,24 @@ def test_small_model_integration_test_batch_different_resolutions(self): EXPECTED_DECODED_TEXT, ) + # Run this test first because it needs to load the model with `flash_attention_2`. For other tests, we keep the + # loaded model (without FA) in `cls.model`. If this test is not run first, there is already a loaded model in + # `cls.model` when loading the flash attention model here, and we will get GPU OOM. + @run_first @slow @require_flash_attn @require_torch_accelerator @pytest.mark.flash_attn_test def test_small_model_integration_test_batch_flashatt2(self): + offload_dir = tempfile.TemporaryDirectory() + self.addCleanup(offload_dir.cleanup) model = Qwen3VLMoeForConditionalGeneration.from_pretrained( "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype=torch.bfloat16, attn_implementation="flash_attention_2", device_map="auto", + max_memory=self.get_max_memory(), + offload_folder=offload_dir.name, ) batched_messages = [self.message, self.message2] inputs = self.processor.apply_chat_template( @@ -659,16 +667,24 @@ def test_small_model_integration_test_batch_flashatt2(self): EXPECTED_DECODED_TEXT, ) + # Run this test first because it needs to load the model with `flash_attention_2`. For other tests, we keep the + # loaded model (without FA) in `cls.model`. If this test is not run first, there is already a loaded model in + # `cls.model` when loading the flash attention model here, and we will get GPU OOM. + @run_first @slow @require_flash_attn @require_torch_accelerator @pytest.mark.flash_attn_test def test_small_model_integration_test_batch_wo_image_flashatt2(self): + offload_dir = tempfile.TemporaryDirectory() + self.addCleanup(offload_dir.cleanup) model = Qwen3VLMoeForConditionalGeneration.from_pretrained( "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype=torch.bfloat16, attn_implementation="flash_attention_2", device_map="auto", + max_memory=self.get_max_memory(), + offload_folder=offload_dir.name, ) message_wo_image = [ {"role": "user", "content": [{"type": "text", "text": "Who are you?"}]}, From 3cc4fcfa602c963fe94202dce21ce58a370bbe75 Mon Sep 17 00:00:00 2001 From: ydshieh Date: Mon, 16 Nov 2026 17:55:25 +0100 Subject: [PATCH 3/4] Compute max_memory once in setUpClass instead of a helper Same pattern as `CwmIntegrationTest` (#50824): `cls.max_memory` is computed once in `setUpClass` and used by `get_model()` and the two FA2 tests. Values are unchanged. Co-Authored-By: Claude Opus 5.5 --- .../qwen3_vl_moe/test_modeling_qwen3_vl_moe.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py index 6469ed1d7129..af35ef94ece9 100644 --- a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py +++ b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py @@ -297,23 +297,19 @@ def setUpClass(cls): # protects it from teardown, so a checkpoint parked on a class-body `model` is never released. cls.model = None cls.offload_dir = None - - @staticmethod - def get_max_memory(): # device_map="auto" fills GPUs to ~100%, leaving no room for the transpose temporary # buffer that fuses per-expert weight shards into a single gate_up_proj tensor during # from_pretrained — causing CUDA OOM on multi-GPU runners. A 70% per-GPU max_memory cap # reserves the headroom. + cls.max_memory = None n = backend_device_count(torch_device) if n > 0 and torch_device != "cpu": torch_accel = getattr(torch, torch_device) per_device = int(min(torch_accel.get_device_properties(i).total_memory for i in range(n)) * 0.70 / 1024**3) - max_memory = dict.fromkeys(range(n), f"{per_device}GiB") - max_memory["cpu"] = ( + cls.max_memory = dict.fromkeys(range(n), f"{per_device}GiB") + cls.max_memory["cpu"] = ( f"{int(get_cpu_ram_total_gib() * 0.9)}GiB" # To avoid runner failing with exit code 137. ) - return max_memory - return None @classmethod def get_model(cls): @@ -323,7 +319,7 @@ def get_model(cls): "Qwen/Qwen3-VL-30B-A3B-Instruct", dtype="auto", device_map="auto", - max_memory=cls.get_max_memory(), + max_memory=cls.max_memory, offload_folder=cls.offload_dir.name, ) return cls.model @@ -626,7 +622,7 @@ def test_small_model_integration_test_batch_flashatt2(self): dtype=torch.bfloat16, attn_implementation="flash_attention_2", device_map="auto", - max_memory=self.get_max_memory(), + max_memory=self.max_memory, offload_folder=offload_dir.name, ) batched_messages = [self.message, self.message2] @@ -683,7 +679,7 @@ def test_small_model_integration_test_batch_wo_image_flashatt2(self): dtype=torch.bfloat16, attn_implementation="flash_attention_2", device_map="auto", - max_memory=self.get_max_memory(), + max_memory=self.max_memory, offload_folder=offload_dir.name, ) message_wo_image = [ From f8ef53d95e5fb5605523ce690cb2fe23bdc42b8a Mon Sep 17 00:00:00 2001 From: ydshieh Date: Mon, 16 Nov 2026 18:24:02 +0100 Subject: [PATCH 4/4] Use the full CPU RAM budget, as in phimoe / qwen3_omni_moe / flex_olmo Co-Authored-By: Claude Opus 5.5 --- tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py index af35ef94ece9..a9ff66e85b43 100644 --- a/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py +++ b/tests/models/qwen3_vl_moe/test_modeling_qwen3_vl_moe.py @@ -307,9 +307,7 @@ def setUpClass(cls): torch_accel = getattr(torch, torch_device) per_device = int(min(torch_accel.get_device_properties(i).total_memory for i in range(n)) * 0.70 / 1024**3) cls.max_memory = dict.fromkeys(range(n), f"{per_device}GiB") - cls.max_memory["cpu"] = ( - f"{int(get_cpu_ram_total_gib() * 0.9)}GiB" # To avoid runner failing with exit code 137. - ) + cls.max_memory["cpu"] = f"{int(get_cpu_ram_total_gib())}GiB" @classmethod def get_model(cls):