Repository: pfnet/pytorch-pfn-extras

Issue:
Consumption of 'autocast' option not properly performed due to 'get' operation on line 195 not being 'pop'
https://github.com/pfnet/pytorch-pfn-extras/blob/master/pytorch_pfn_extras/handler/_logic.py#L195

Since the 'get' operation on this line (line 195) is not 'pop', the consumption of the 'autocast' option is not being performed properly.


Discussion:
is this causing errors?
small example
```python
from pytorch_pfn_extras.engine import create_trainer
import pytorch_pfn_extras as ppe
import torch.nn as nn
import torch.optim as o

class Model(nn.Module):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.l0 = nn.Linear(64, 64)

    def forward(self, x):
        return self.l0(x)

model = Model()
optimizer = o.SGD(model.parameters(), lr=0.1, momentum=0.9)
trainer = create_trainer(
    models=model,
    optimizers=optimizer,
    max_epochs=2,
    options={
        "autocast": True,
    }
)
```

result

```
ValueError: ('Unknown options: ', {'autocast': True})
```

An error occurs at the following point because the option is not being consumed properly.

https://github.com/pfnet/pytorch-pfn-extras/blob/1b11cafc535d57aeb75f37b54cc2a634fce53382/pytorch_pfn_extras/engine.py#L110-L111

Resolving patch:
diff --git a/pytorch_pfn_extras/handler/_logic.py b/pytorch_pfn_extras/handler/_logic.py
--- a/pytorch_pfn_extras/handler/_logic.py
+++ b/pytorch_pfn_extras/handler/_logic.py
@@ -192,7 +192,7 @@ def consume_options(self, options: Dict[str, Any]) -> None:
         self._grad_scaler = options.pop('grad_scaler', None)
 
         self._backward_fn = options.pop('backward_function', None)
-        autocast_options = options.get("autocast", False)
+        autocast_options = options.pop("autocast", False)
         if isinstance(autocast_options, bool):
             autocast_options = {"enabled": autocast_options, "device_type": "cuda"}
         self._autocast = _autocast._AutocastManager(


Test patch:
diff --git a/tests/pytorch_pfn_extras_tests/training_tests/test_trainer.py b/tests/pytorch_pfn_extras_tests/training_tests/test_trainer.py
--- a/tests/pytorch_pfn_extras_tests/training_tests/test_trainer.py
+++ b/tests/pytorch_pfn_extras_tests/training_tests/test_trainer.py
@@ -531,3 +531,24 @@ def test_trainer_with_clousure_logic(device, progress_bar, path):
         out_dir=path, logic=ppe.handler.ClousureLogic(options={"backward_outputs": ["loss"]})
     )
     trainer.run(data, data)
+
+
+@pytest.mark.gpu
+def test_trainer_with_autocast(path):
+    if not torch.cuda.is_available():
+        pytest.skip()
+    model = MyModel()
+    model_with_loss = MyModelWithLossFn(model)
+    ppe.to(model_with_loss, "cuda")
+    optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
+    extensions = []
+    autocast_options = {"autocast": True}
+    evaluator = engine.create_evaluator(
+        model_with_loss, device="cuda",
+        options=autocast_options)
+
+    engine.create_trainer(
+        model_with_loss, optimizer, 20,
+        device="cuda", evaluator=evaluator, extensions=extensions,
+        out_dir=path, options=autocast_options
+    )