|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | +# |
| 6 | +# ----------------------------------------------------------------------------- |
| 7 | + |
| 8 | +import pytest |
| 9 | +from transformers import AutoConfig, AutoModelForCausalLM |
| 10 | + |
| 11 | +from QEfficient.transformers.models.modeling_auto import QEFFAutoModelForCausalLM |
| 12 | + |
| 13 | +# Simple test config for memory reduction testing |
| 14 | +test_config = AutoConfig.for_model( |
| 15 | + "gpt2", |
| 16 | + max_position_embeddings=256, |
| 17 | + num_hidden_layers=2, |
| 18 | + num_attention_heads=4, |
| 19 | + hidden_size=128, |
| 20 | + intermediate_size=512, |
| 21 | + vocab_size=127, |
| 22 | + num_key_value_heads=2, |
| 23 | +) |
| 24 | + |
| 25 | +model_kwargs = {"attn_implementation": "eager"} |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def tmp_cache(tmp_path, monkeypatch): |
| 30 | + monkeypatch.setattr("QEfficient.utils._utils.QEFF_HOME", tmp_path) |
| 31 | + yield tmp_path |
| 32 | + |
| 33 | + |
| 34 | +def test_offload_weights_method(): |
| 35 | + """Test the _offload_model_weights method with both True and False values.""" |
| 36 | + model = AutoModelForCausalLM.from_config(test_config, **model_kwargs) |
| 37 | + qeff_model = QEFFAutoModelForCausalLM(model, continuous_batching=False) |
| 38 | + |
| 39 | + # Initially weights should not be offloaded |
| 40 | + assert not qeff_model._is_weights_offloaded |
| 41 | + assert not any(param.is_meta for param in qeff_model.model.parameters()) |
| 42 | + |
| 43 | + # Test with offload_pt_weights=True |
| 44 | + success = qeff_model._offload_model_weights(offload_pt_weights=True) |
| 45 | + assert success |
| 46 | + assert qeff_model._is_weights_offloaded |
| 47 | + assert all(param.is_meta for param in qeff_model.model.parameters()) |
| 48 | + |
| 49 | + # Reset for next test |
| 50 | + model2 = AutoModelForCausalLM.from_config(test_config, **model_kwargs) |
| 51 | + qeff_model2 = QEFFAutoModelForCausalLM(model2, continuous_batching=False) |
| 52 | + |
| 53 | + # Test with offload_pt_weights=False |
| 54 | + success = qeff_model2._offload_model_weights(offload_pt_weights=False) |
| 55 | + assert not success |
| 56 | + assert not qeff_model2._is_weights_offloaded |
| 57 | + assert not any(param.is_meta for param in qeff_model2.model.parameters()) |
| 58 | + |
| 59 | + |
| 60 | +def test_re_export_behavior_with_offloaded_weights(tmp_cache): |
| 61 | + """Test that re-export fails when weights are offloaded.""" |
| 62 | + model = AutoModelForCausalLM.from_config(test_config, **model_kwargs) |
| 63 | + qeff_model = QEFFAutoModelForCausalLM(model, continuous_batching=False) |
| 64 | + |
| 65 | + # First export should succeed |
| 66 | + _ = qeff_model.export() |
| 67 | + assert qeff_model.onnx_path is not None |
| 68 | + |
| 69 | + # Manually offload weights |
| 70 | + qeff_model._offload_model_weights(offload_pt_weights=True) |
| 71 | + assert qeff_model._is_weights_offloaded |
| 72 | + |
| 73 | + # Force a new export by removing the file |
| 74 | + import os |
| 75 | + |
| 76 | + os.remove(qeff_model.onnx_path) |
| 77 | + qeff_model.onnx_path = None |
| 78 | + |
| 79 | + # Re-export should fail with RuntimeError due to offloaded weights |
| 80 | + with pytest.raises(RuntimeError, match="weights have been offloaded"): |
| 81 | + qeff_model.export() |
| 82 | + |
| 83 | + |
| 84 | +def test_vlm_dual_qpc_memory_offload_behavior(): |
| 85 | + """Test asymmetric memory offload behavior for VLM dual QPC models.""" |
| 86 | + |
| 87 | + # Mock vision model (should NOT offload weights) |
| 88 | + class MockVisionModel: |
| 89 | + def __init__(self): |
| 90 | + self._is_weights_offloaded = False |
| 91 | + |
| 92 | + def export(self, inputs, output_names, dynamic_axes, export_dir=None, offload_pt_weights=True): |
| 93 | + if offload_pt_weights: |
| 94 | + self._is_weights_offloaded = True |
| 95 | + return "vision_export_path" |
| 96 | + |
| 97 | + # Mock language model (should offload weights) |
| 98 | + class MockLangModel: |
| 99 | + def __init__(self): |
| 100 | + self._is_weights_offloaded = False |
| 101 | + |
| 102 | + def export(self, inputs, output_names, dynamic_axes, export_dir=None, offload_pt_weights=True): |
| 103 | + if offload_pt_weights: |
| 104 | + self._is_weights_offloaded = True |
| 105 | + return "lang_export_path" |
| 106 | + |
| 107 | + # Test dual QPC behavior |
| 108 | + vision_model = MockVisionModel() |
| 109 | + lang_model = MockLangModel() |
| 110 | + |
| 111 | + # Simulate dual QPC export behavior |
| 112 | + vision_model.export({}, [], {}, offload_pt_weights=False) # Vision model doesn't offload |
| 113 | + lang_model.export({}, [], {}, offload_pt_weights=True) # Language model offloads |
| 114 | + |
| 115 | + # Verify asymmetric behavior |
| 116 | + assert not vision_model._is_weights_offloaded # Vision model should NOT be offloaded |
| 117 | + assert lang_model._is_weights_offloaded # Language model should be offloaded |
| 118 | + |
| 119 | + |
| 120 | +def test_vlm_single_qpc_memory_offload_behavior(): |
| 121 | + """Test memory offload behavior for VLM single QPC models with both True and False.""" |
| 122 | + |
| 123 | + class MockParam: |
| 124 | + def __init__(self, is_meta=False): |
| 125 | + self.is_meta = is_meta |
| 126 | + |
| 127 | + class MockModel: |
| 128 | + def __init__(self): |
| 129 | + self._params = [MockParam(is_meta=False)] |
| 130 | + |
| 131 | + def parameters(self): |
| 132 | + return self._params |
| 133 | + |
| 134 | + class MockSingleQPCModel: |
| 135 | + def __init__(self): |
| 136 | + self._is_weights_offloaded = False |
| 137 | + self.model = MockModel() |
| 138 | + |
| 139 | + def _offload_model_weights(self): |
| 140 | + self._is_weights_offloaded = True |
| 141 | + for param in self.model.parameters(): |
| 142 | + param.is_meta = True |
| 143 | + return True |
| 144 | + |
| 145 | + def export(self, export_dir=None, offload_pt_weights=True): |
| 146 | + if offload_pt_weights: |
| 147 | + self._offload_model_weights() |
| 148 | + return "single_qpc_export_path" |
| 149 | + |
| 150 | + # Test with offload_pt_weights=True |
| 151 | + qeff_model = MockSingleQPCModel() |
| 152 | + qeff_model.export(offload_pt_weights=True) |
| 153 | + assert qeff_model._is_weights_offloaded |
| 154 | + assert all(param.is_meta for param in qeff_model.model.parameters()) |
| 155 | + |
| 156 | + # Test with offload_pt_weights=False |
| 157 | + qeff_model2 = MockSingleQPCModel() |
| 158 | + qeff_model2.export(offload_pt_weights=False) |
| 159 | + assert not qeff_model2._is_weights_offloaded |
| 160 | + assert not any(param.is_meta for param in qeff_model2.model.parameters()) |
0 commit comments