Jelajahi Sumber

make the entire test run in inference mode

justheuristic 3 tahun lalu
induk
melakukan
f8f13c7845
1 mengubah file dengan 21 tambahan dan 21 penghapusan
  1. 21 21
      tests/test_full_model.py

+ 21 - 21
tests/test_full_model.py

@@ -23,7 +23,9 @@ if not MODEL_NAME:
 REF_NAME = os.environ.get("REF_NAME")
 
 
+@torch.inference_mode
 def test_full_model_exact_match(atol_forward=1e-3, atol_inference=1e-3):
+    assert not torch.is_grad_enabled()
     tokenizer = transformers.BloomTokenizerFast.from_pretrained(MODEL_NAME)
     model = DistributedBloomForCausalLM.from_pretrained(MODEL_NAME, initial_peers=INITIAL_PEERS)
     assert isinstance(model, DistributedBloomForCausalLM)
@@ -35,29 +37,27 @@ def test_full_model_exact_match(atol_forward=1e-3, atol_inference=1e-3):
     logger.info("Forward outputs are finite")
 
     if REF_NAME:
-        with torch.no_grad():
-            ref_model = transformers.AutoModelForCausalLM.from_pretrained(REF_NAME)
-            dummy_mask = torch.ones_like(test_inputs, dtype=torch.bool)
-            # note: this creates a dummy mask to make the test compatible with older transformer versions
-            # prior to https://github.com/huggingface/transformers/pull/17837
-            ref_outputs = ref_model.forward(test_inputs, attention_mask=dummy_mask).logits
-            assert torch.allclose(ref_outputs, parallel_outputs, rtol=0, atol=atol_forward)
-            del ref_model, ref_outputs
+        ref_model = transformers.AutoModelForCausalLM.from_pretrained(REF_NAME)
+        dummy_mask = torch.ones_like(test_inputs, dtype=torch.bool)
+        # note: this creates a dummy mask to make the test compatible with older transformer versions
+        # prior to https://github.com/huggingface/transformers/pull/17837
+        ref_outputs = ref_model.forward(test_inputs, attention_mask=dummy_mask).logits
+        assert torch.allclose(ref_outputs, parallel_outputs, rtol=0, atol=atol_forward)
+        del ref_model, ref_outputs, dummy_mask
     else:
         logger.warning("Did not test exact match with local model: REF_NAME environment variable is not set")
 
-    with torch.inference_mode():
-        embs = model.transformer.word_embeddings(test_inputs)
-        embs = model.transformer.word_embeddings_layernorm(embs)
-        recurrent_outputs = []
-        with model.transformer.h.inference_session() as sess:
-            for t in range(embs.shape[1]):
-                recurrent_outputs.append(sess.step(embs[:, t : t + 1, :]))
-        recurrent_outputs = torch.cat(recurrent_outputs, dim=1)
-        recurrent_outputs = model.transformer.ln_f(recurrent_outputs)
-
-        dictionary = model.transformer.word_embeddings.weight.t()
-        recurrent_outputs = recurrent_outputs.to(dictionary.dtype)
-        recurrent_outputs = (recurrent_outputs @ dictionary).float()
+    embs = model.transformer.word_embeddings(test_inputs)
+    embs = model.transformer.word_embeddings_layernorm(embs)
+    recurrent_outputs = []
+    with model.transformer.h.inference_session() as sess:
+        for t in range(embs.shape[1]):
+            recurrent_outputs.append(sess.step(embs[:, t : t + 1, :]))
+    recurrent_outputs = torch.cat(recurrent_outputs, dim=1)
+    recurrent_outputs = model.transformer.ln_f(recurrent_outputs)
+
+    dictionary = model.transformer.word_embeddings.weight.t()
+    recurrent_outputs = recurrent_outputs.to(dictionary.dtype)
+    recurrent_outputs = (recurrent_outputs @ dictionary).float()
     assert torch.allclose(recurrent_outputs, parallel_outputs, rtol=0, atol=atol_inference)
     logger.info("Inference is consistent with forward")