|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import transformers |
| 7 | +from QEfficient.finetune.experimental.core import model |
| 8 | +from QEfficient.finetune.experimental.core.model import BaseModel, HFModel |
| 9 | + |
| 10 | + |
| 11 | +class TestMockModel(nn.Module): |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + self.linear = nn.Linear(2, 2) |
| 15 | + |
| 16 | + def forward(self, x): |
| 17 | + return self.linear(x) |
| 18 | + |
| 19 | + |
| 20 | +class TestCustomModel(BaseModel): |
| 21 | + def __init__(self, model_name): |
| 22 | + super().__init__(model_name) |
| 23 | + print("init of custom class") |
| 24 | + |
| 25 | + def load_model(self) -> nn.Module: |
| 26 | + return TestMockModel() |
| 27 | + |
| 28 | + def load_tokenizer(self): |
| 29 | + return "dummy-tokenizer" |
| 30 | + |
| 31 | + |
| 32 | +# BaseModel tests |
| 33 | +def test_model_property_errors_if_not_created(): |
| 34 | + m = TestCustomModel("dummy") |
| 35 | + with pytest.raises(RuntimeError): |
| 36 | + _ = m.model # must call .create() |
| 37 | + |
| 38 | + |
| 39 | +def test_create_builds_and_registers(): |
| 40 | + breakpoint() |
| 41 | + m = TestCustomModel.create("dummy") |
| 42 | + # inner model exists and registered |
| 43 | + assert "_model" in m._modules |
| 44 | + assert isinstance(m.model, TestMockModel) |
| 45 | + # forward works |
| 46 | + out = m(torch.zeros(1, 2)) |
| 47 | + assert out.shape == (1, 2) |
| 48 | + |
| 49 | + |
| 50 | +def test_tokenizer_lazy_loading(): |
| 51 | + m = TestCustomModel.create("dummy") |
| 52 | + assert m._tokenizer is None |
| 53 | + tok = m.tokenizer |
| 54 | + assert tok == "dummy-tokenizer" |
| 55 | + assert m._tokenizer == tok |
| 56 | + |
| 57 | + |
| 58 | +def test_to_moves_inner_and_returns_self(): |
| 59 | + m = TestCustomModel.create("dummy") |
| 60 | + with mock.patch.object(TestMockModel, "to", autospec=True) as mocked_to: |
| 61 | + ret = m.to("cuda:0") |
| 62 | + mocked_to.assert_called_once_with(m.model, "cuda:0") |
| 63 | + assert ret is m |
| 64 | + |
| 65 | + |
| 66 | +def test_train_eval_sync_flags(): |
| 67 | + m = TestCustomModel.create("dummy") |
| 68 | + m.eval() |
| 69 | + assert m.training is False |
| 70 | + assert m.model.training is False |
| 71 | + m.train() |
| 72 | + assert m.training is True |
| 73 | + assert m.model.training is True |
| 74 | + |
| 75 | + |
| 76 | +def test_resize_token_embeddings_and_get_input_embeddings_warn(monkeypatch): |
| 77 | + m = TestCustomModel.create("dummy") |
| 78 | + |
| 79 | + # resize_token_embeddings: underlying model lacks the method, should warn and not raise |
| 80 | + with mock.patch("QEfficient.finetune.experimental.core.model.logger.info") as mocked_log: |
| 81 | + m.resize_token_embeddings(10) |
| 82 | + mocked_log.assert_called_once() |
| 83 | + |
| 84 | + # get_input_embeddings: underlying model lacks method, should warn and return None |
| 85 | + with mock.patch("QEfficient.finetune.experimental.core.model.logger.info") as mocked_log: |
| 86 | + assert m.get_input_embeddings() is None |
| 87 | + mocked_log.assert_called_once() |
| 88 | + |
| 89 | + |
| 90 | +def test_state_dict_contains_inner_params(): |
| 91 | + m = TestCustomModel.create("dummy") |
| 92 | + sd = m.state_dict() |
| 93 | + # should contain params from TestMockModel.linear |
| 94 | + assert any("linear.weight" in k for k in sd) |
| 95 | + assert any("linear.bias" in k for k in sd) |
| 96 | + |
| 97 | + |
| 98 | +# HFModel tests |
| 99 | +def test_hfmodel_invalid_auto_class_raises(): |
| 100 | + with pytest.raises(ValueError): |
| 101 | + HFModel.create("hf-name", auto_class_name="AutoDoesNotExist") |
| 102 | + |
| 103 | + |
| 104 | +def test_hfmodel_loads_auto_and_tokenizer(monkeypatch): |
| 105 | + # fake HF Auto class |
| 106 | + class FakeAuto(nn.Module): |
| 107 | + @classmethod |
| 108 | + def from_pretrained(cls, name, **kwargs): |
| 109 | + inst = cls() |
| 110 | + inst.loaded = (name, kwargs) |
| 111 | + return inst |
| 112 | + |
| 113 | + def forward(self, x): |
| 114 | + return x |
| 115 | + |
| 116 | + fake_tok = mock.Mock() |
| 117 | + |
| 118 | + # Monkeypatch transformer classes used in HFModel |
| 119 | + monkeypatch.setattr( |
| 120 | + "QEfficient.finetune.experimental.core.model.transformers.AutoModelForCausalLM", |
| 121 | + FakeAuto, |
| 122 | + raising=False, |
| 123 | + ) |
| 124 | + monkeypatch.setattr( |
| 125 | + model, |
| 126 | + "AutoTokenizer", |
| 127 | + mock.Mock(from_pretrained=mock.Mock(return_value=fake_tok)), |
| 128 | + ) |
| 129 | + monkeypatch.setattr( |
| 130 | + "QEfficient.finetune.experimental.core.model.insert_pad_token", |
| 131 | + mock.Mock(), |
| 132 | + raising=False, |
| 133 | + ) |
| 134 | + |
| 135 | + m = HFModel.create("hf-name") |
| 136 | + assert isinstance(m.model, FakeAuto) |
| 137 | + |
| 138 | + # load tokenizer |
| 139 | + tok = m.load_tokenizer() |
| 140 | + |
| 141 | + # tokenizer was loaded and pad token inserted |
| 142 | + model.AutoTokenizer.from_pretrained.assert_called_once_with("hf-name") |
| 143 | + model.insert_pad_token.assert_called_once_with(fake_tok) |
0 commit comments