diff --git a/tests/test_predict_no_grad.py b/tests/test_predict_no_grad.py new file mode 100644 index 0000000..89e6842 --- /dev/null +++ b/tests/test_predict_no_grad.py @@ -0,0 +1,62 @@ +import numpy as np +import pytest +import torch + +from torchTextClassifiers import ModelConfig, torchTextClassifiers +from torchTextClassifiers.tokenizers import NGramTokenizer + + +def _build_ttc(texts): + tokenizer = NGramTokenizer( + min_count=1, min_n=2, max_n=4, num_tokens=50, len_word_ngrams=2, output_dim=20 + ) + tokenizer.train(list(texts)) + model_config = ModelConfig(embedding_dim=8, num_classes=2) + return torchTextClassifiers(tokenizer=tokenizer, model_config=model_config) + + +def test_predict_does_not_track_gradients(sample_text_data): + """predict() must run its forward pass without building an autograd graph (issue #111).""" + ttc = _build_ttc(sample_text_data) + + recorded = {} + + def hook(module, inputs, output): + recorded["grad_enabled"] = torch.is_grad_enabled() + recorded["requires_grad"] = output.requires_grad + + handle = ttc.pytorch_model.register_forward_hook(hook) + try: + assert torch.is_grad_enabled() + out = ttc.predict(np.asarray(sample_text_data), raw_categorical_inputs=False) + finally: + handle.remove() + + assert recorded["grad_enabled"] is False + assert recorded["requires_grad"] is False + assert out["prediction"].shape[0] == len(sample_text_data) + # Global grad mode is restored after predict() + assert torch.is_grad_enabled() + + +def test_predict_captum_works_under_user_no_grad(sample_text_data): + """Captum attributions still work when predict() is called inside torch.no_grad().""" + pytest.importorskip("captum") + torch.manual_seed(0) + ttc = _build_ttc(sample_text_data) + # The classification head is zero-initialized, which would give all-zero + # attributions on an untrained model: randomize it so the check is meaningful. + with torch.no_grad(): + ttc.pytorch_model.classification_head.net.weight.normal_() + X = np.asarray(sample_text_data) + + ref = ttc.predict(X, raw_categorical_inputs=False, explain_with_captum=True) + with torch.no_grad(): + out = ttc.predict(X, raw_categorical_inputs=False, explain_with_captum=True) + assert not torch.is_grad_enabled() # caller's grad mode untouched + + attributions = out["captum_attributions"] + assert attributions.shape[:2] == (len(sample_text_data), 1) + assert torch.isfinite(attributions).all() + assert attributions.abs().sum() > 0 + torch.testing.assert_close(attributions, ref["captum_attributions"]) diff --git a/torchTextClassifiers/torchTextClassifiers.py b/torchTextClassifiers/torchTextClassifiers.py index cc0fdc4..026b2a9 100644 --- a/torchTextClassifiers/torchTextClassifiers.py +++ b/torchTextClassifiers/torchTextClassifiers.py @@ -769,12 +769,16 @@ def predict( (encoded_text.shape[0], 0), dtype=torch.float32, device=device ) - model_output = self.pytorch_model( - encoded_text, - attention_mask, - categorical_vars, - return_label_attention_matrix=explain_with_label_attention, - ) # forward pass, contains the prediction scores (len(text), num_classes) + # Inference only: no need to build the autograd graph (saves memory and time). + # Captum attributions below are computed outside this block, and Captum + # re-enables gradients internally for its own forward/backward passes. + with torch.no_grad(): + model_output = self.pytorch_model( + encoded_text, + attention_mask, + categorical_vars, + return_label_attention_matrix=explain_with_label_attention, + ) # forward pass, contains the prediction scores (len(text), num_classes) # Multilevel custom model: returns a list of per-level logit tensors. # Each level may have a different number of classes, so we process them @@ -827,10 +831,13 @@ def predict( # Captum explanations captum_attributions = [] for k in range(top_k): - attributions = lig.attribute( - (encoded_text, attention_mask, categorical_vars), - target=integer_predictions[:, k].to(device), - ) # (batch_size, seq_len) + # Integrated gradients need autograd: enable it explicitly, even if + # predict() is called from within a user-level torch.no_grad() context. + with torch.enable_grad(): + attributions = lig.attribute( + (encoded_text, attention_mask, categorical_vars), + target=integer_predictions[:, k].to(device), + ) # (batch_size, seq_len) attributions = attributions.sum(dim=-1) captum_attributions.append(attributions.detach().cpu())