|
| 1 | +import shutil |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pytest |
| 7 | + |
| 8 | +from autointent.context.data_handler import DataHandler |
| 9 | +from autointent.modules import BERTLoRAScorer |
| 10 | + |
| 11 | + |
| 12 | +def test_lora_scorer_dump_load(dataset): |
| 13 | + """Test that BERTLoRAScorer can be saved and loaded while preserving predictions.""" |
| 14 | + data_handler = DataHandler(dataset) |
| 15 | + |
| 16 | + # Create and train scorer |
| 17 | + scorer_original = BERTLoRAScorer( |
| 18 | + classification_model_config="prajjwal1/bert-tiny", |
| 19 | + num_train_epochs=1, |
| 20 | + batch_size=8 |
| 21 | + ) |
| 22 | + scorer_original.fit(data_handler.train_utterances(0), data_handler.train_labels(0)) |
| 23 | + |
| 24 | + # Test data |
| 25 | + test_data = [ |
| 26 | + "why is there a hold on my account", |
| 27 | + "why is my bank account frozen", |
| 28 | + ] |
| 29 | + |
| 30 | + # Get predictions before saving |
| 31 | + predictions_before = scorer_original.predict(test_data) |
| 32 | + |
| 33 | + # Create temp directory and save model |
| 34 | + temp_dir_path = Path(tempfile.mkdtemp(prefix="lora_scorer_test_")) |
| 35 | + try: |
| 36 | + # Save the model |
| 37 | + scorer_original.dump(str(temp_dir_path)) |
| 38 | + |
| 39 | + # Create a new scorer and load saved model |
| 40 | + scorer_loaded = BERTLoRAScorer( |
| 41 | + classification_model_config="prajjwal1/bert-tiny", |
| 42 | + num_train_epochs=1, |
| 43 | + batch_size=8 |
| 44 | + ) |
| 45 | + scorer_loaded.load(str(temp_dir_path)) |
| 46 | + |
| 47 | + # Verify model and tokenizer are loaded |
| 48 | + assert hasattr(scorer_loaded, "_model") |
| 49 | + assert scorer_loaded._model is not None |
| 50 | + assert hasattr(scorer_loaded, "_tokenizer") |
| 51 | + assert scorer_loaded._tokenizer is not None |
| 52 | + |
| 53 | + # Get predictions after loading |
| 54 | + predictions_after = scorer_loaded.predict(test_data) |
| 55 | + |
| 56 | + # Verify predictions match |
| 57 | + assert predictions_before.shape == predictions_after.shape |
| 58 | + np.testing.assert_allclose(predictions_before, predictions_after, atol=1e-6) |
| 59 | + |
| 60 | + finally: |
| 61 | + # Clean up |
| 62 | + shutil.rmtree(temp_dir_path, ignore_errors=True) # workaround for windows permission error |
| 63 | + |
| 64 | + |
| 65 | +def test_lora_prediction(dataset): |
| 66 | + """Test that the lora model can fit and make predictions.""" |
| 67 | + data_handler = DataHandler(dataset) |
| 68 | + |
| 69 | + scorer = BERTLoRAScorer(classification_model_config="prajjwal1/bert-tiny", num_train_epochs=1, batch_size=8) |
| 70 | + |
| 71 | + scorer.fit(data_handler.train_utterances(0), data_handler.train_labels(0)) |
| 72 | + |
| 73 | + test_data = [ |
| 74 | + "why is there a hold on my american saving bank account", |
| 75 | + "i am not sure why my account is blocked", |
| 76 | + "why is there a hold on my capital one checking account", |
| 77 | + "i think my account is blocked but i do not know the reason", |
| 78 | + "can you tell me why is my bank account frozen", |
| 79 | + ] |
| 80 | + |
| 81 | + predictions = scorer.predict(test_data) |
| 82 | + |
| 83 | + # Verify prediction shape |
| 84 | + assert predictions.shape[0] == len(test_data) |
| 85 | + assert predictions.shape[1] == len(set(data_handler.train_labels(0))) |
| 86 | + |
| 87 | + # Verify predictions are probabilities |
| 88 | + assert 0.0 <= np.min(predictions) <= np.max(predictions) <= 1.0 |
| 89 | + |
| 90 | + # Verify probabilities sum to 1 for multiclass |
| 91 | + if not scorer._multilabel: |
| 92 | + for pred_row in predictions: |
| 93 | + np.testing.assert_almost_equal(np.sum(pred_row), 1.0, decimal=5) |
| 94 | + |
| 95 | + # Test metadata function if available |
| 96 | + if hasattr(scorer, "predict_with_metadata"): |
| 97 | + predictions, metadata = scorer.predict_with_metadata(test_data) |
| 98 | + assert len(predictions) == len(test_data) |
| 99 | + assert metadata is None |
| 100 | + |
| 101 | + |
| 102 | +def test_lora_cache_clearing(dataset): |
| 103 | + """Test that the lora model properly handles cache clearing.""" |
| 104 | + data_handler = DataHandler(dataset) |
| 105 | + |
| 106 | + scorer = BERTLoRAScorer(classification_model_config="prajjwal1/bert-tiny", num_train_epochs=1, batch_size=8) |
| 107 | + |
| 108 | + scorer.fit(data_handler.train_utterances(0), data_handler.train_labels(0)) |
| 109 | + |
| 110 | + test_data = ["test text"] |
| 111 | + |
| 112 | + # Should work before clearing cache |
| 113 | + scorer.predict(test_data) |
| 114 | + |
| 115 | + # Clear the cache |
| 116 | + scorer.clear_cache() |
| 117 | + |
| 118 | + # Verify model and tokenizer are removed |
| 119 | + assert not hasattr(scorer, "_model") or scorer._model is None |
| 120 | + assert not hasattr(scorer, "_tokenizer") or scorer._tokenizer is None |
| 121 | + |
| 122 | + # Should raise exception after clearing cache |
| 123 | + with pytest.raises(RuntimeError): |
| 124 | + scorer.predict(test_data) |
0 commit comments