Repository: OpenNMT/OpenNMT-py

Issue:
torch.no_grad() in validate ? 
"torch.no_grad()" is not used in validate().
I think it should be used ...

https://github.com/OpenNMT/OpenNMT-py/blob/94cc2b62990b7eeb382a6b74a7268230e2a572b5/onmt/trainer.py#L213-L220




Discussion:
I think you are correct, we forgot to include this during the 0.4 to 1.0 migration.
Can you please send PR and show:
- differences in results (if any)
- reduction in memory footprint at validation time.
thanks.

Resolving patch:
diff --git a/onmt/__init__.py b/onmt/__init__.py
--- a/onmt/__init__.py
+++ b/onmt/__init__.py
@@ -17,4 +17,4 @@
 __all__ = [onmt.inputters, onmt.encoders, onmt.decoders, onmt.models,
            onmt.utils, onmt.modules, "Trainer"]
 
-__version__ = "0.6.0"
+__version__ = "0.7.0"
diff --git a/onmt/trainer.py b/onmt/trainer.py
--- a/onmt/trainer.py
+++ b/onmt/trainer.py
@@ -9,6 +9,7 @@
           users of this library) for the strategy things we do.
 """
 
+import torch
 import onmt.inputters as inputters
 import onmt.utils
 
@@ -216,28 +217,29 @@ def validate(self, valid_iter):
         # Set model in validating mode.
         self.model.eval()
 
-        stats = onmt.utils.Statistics()
+        with torch.no_grad():
+            stats = onmt.utils.Statistics()
 
-        for batch in valid_iter:
-            src = inputters.make_features(batch, 'src', self.data_type)
-            if self.data_type == 'text':
-                _, src_lengths = batch.src
-            elif self.data_type == 'audio':
-                src_lengths = batch.src_lengths
-            else:
-                src_lengths = None
+            for batch in valid_iter:
+                src = inputters.make_features(batch, 'src', self.data_type)
+                if self.data_type == 'text':
+                    _, src_lengths = batch.src
+                elif self.data_type == 'audio':
+                    src_lengths = batch.src_lengths
+                else:
+                    src_lengths = None
 
-            tgt = inputters.make_features(batch, 'tgt')
+                tgt = inputters.make_features(batch, 'tgt')
 
-            # F-prop through the model.
-            outputs, attns = self.model(src, tgt, src_lengths)
+                # F-prop through the model.
+                outputs, attns = self.model(src, tgt, src_lengths)
 
-            # Compute loss.
-            batch_stats = self.valid_loss.monolithic_compute_loss(
-                batch, outputs, attns)
+                # Compute loss.
+                batch_stats = self.valid_loss.monolithic_compute_loss(
+                    batch, outputs, attns)
 
-            # Update statistics.
-            stats.update(batch_stats)
+                # Update statistics.
+                stats.update(batch_stats)
 
         # Set model back to training mode.
         self.model.train()
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@
 
 setup(name='OpenNMT-py',
       description='A python implementation of OpenNMT',
-      version='0.6.0',
+      version='0.7.0',
 
       packages=['onmt', 'onmt.encoders', 'onmt.modules', 'onmt.tests',
                 'onmt.translate', 'onmt.decoders', 'onmt.inputters',


Test patch: