Repository: pfnet/pytorch-pfn-extras

Issue:
Align terminology (`option` v.s. `config`)



Discussion:

Resolving patch:
diff --git a/pytorch_pfn_extras/runtime/_to.py b/pytorch_pfn_extras/runtime/_to.py
--- a/pytorch_pfn_extras/runtime/_to.py
+++ b/pytorch_pfn_extras/runtime/_to.py
@@ -13,8 +13,9 @@ def to(
         module_or_tensor: ModuleOrTensor,
         device: DeviceLike,
         *,
-        config: Optional[Dict[str, Any]] = None,
+        options: Optional[Dict[str, Any]] = None,
         runtime_class: Optional[Type[BaseRuntime]] = None,
+        config: Optional[Dict[str, Any]] = None,
 ) -> ModuleOrTensor:
     """A function to transfer the given object to the given device.
 
@@ -33,23 +34,30 @@ def to(
             An object to be transferred.
         device (torch.device or str):
             The device that the input object is transferred to.
-        config (dict, optional):
-            A config of dictionary type that is passed to
+        options (dict, optional):
+            An options of dictionary type that is passed to
             ``runtime_class.__init__`` as an argument.
         runtime_class:
             A runtime class inherited from `BaseRuntime` class.
             If ``None``, a runtime class is automatically selected
             based on the ``device`` argument from the runtime registry.
+        config (dict, optional):
+            DEPRECATED. Use `options`.
 
     Returns:
         A `torch.Tensor` with the specified device.
     """
-    if config is None:
-        config = {}
+    if options is None:
+        options = {}
+        if config is not None:
+            options = config
+    elif config is not None:
+        raise ValueError('options and config cannot be specified together')
+
     if runtime_class is None:
         registry = ppe.runtime.runtime_registry
         runtime_class = registry.get_runtime_class_for_device_spec(device)
-    runtime = runtime_class(device, config)
+    runtime = runtime_class(device, options)
     obj = module_or_tensor
     if isinstance(obj, torch.nn.Module):
         ppe.runtime._runtime._set_module_runtime_tag(obj, runtime)


Test patch:
diff --git a/tests/pytorch_pfn_extras_tests/runtime_tests/test_to.py b/tests/pytorch_pfn_extras_tests/runtime_tests/test_to.py
--- a/tests/pytorch_pfn_extras_tests/runtime_tests/test_to.py
+++ b/tests/pytorch_pfn_extras_tests/runtime_tests/test_to.py
@@ -34,35 +34,42 @@ def test_invalid_ppe_to():
         ppe.to(object(), device)
 
 
-@pytest.mark.gpu
-def test_module_split_ppe_to():
-    class TestRuntime(ppe.runtime.BaseRuntime):
-        def move_module(self, module):
-            # Don't do the actual move
-            return module
+class MyRuntime(ppe.runtime.BaseRuntime):
+    def move_module(self, module):
+        # Don't do the actual move
+        return module
 
-        def initialize_module(self, module, loader_or_batch):
-            pass
+    def initialize_module(self, module, loader_or_batch):
+        pass
 
+
+def test_module_split_ppe_to():
     module = MyModule()
-    ppe.to(module.layer2, 'dummy', runtime_class=TestRuntime)
+    ppe.to(module.layer2, 'dummy', runtime_class=MyRuntime,
+           options={'opt': 1})
+    rt_layer1 = ppe.runtime._runtime._module_runtime_tag(module.layer1)
+    rt_layer2 = ppe.runtime._runtime._module_runtime_tag(module.layer2)
     assert str(next(iter(module.layer1.parameters())).device) == "cpu"
-    assert ppe.runtime._runtime._module_runtime_tag(module.layer1) is None
-    assert ppe.runtime._runtime._module_runtime_tag(module.layer2) is not None
+    assert rt_layer1 is None
+    assert isinstance(rt_layer2, MyRuntime)
+    assert rt_layer2.device_spec == 'dummy'
+    assert rt_layer2.options['opt'] == 1
 
 
-def test_runtime_nested():
-    class TestRuntime(ppe.runtime.BaseRuntime):
-        def move_module(self, module):
-            # Don't do the actual move
-            return module
+def test_module_split_ppe_to_config():
+    # Deprecated "config" option.
+    module = MyModule()
+    ppe.to(module, 'dummy', runtime_class=MyRuntime, config={'opt': 1})
+    ppe.to(module, 'dummy', runtime_class=MyRuntime, config={'opt': 1})
+    rt_layer1 = ppe.runtime._runtime._module_runtime_tag(module)
+    assert isinstance(rt_layer1, MyRuntime)
+    assert rt_layer1.options['opt'] == 1
 
-        def initialize_module(self, module, loader_or_batch):
-            pass
 
+def test_runtime_nested():
     module = MyModule()
-    ppe.to(module, 'dummy', runtime_class=TestRuntime)
-    ppe.to(module.layer2, 'dummy', runtime_class=TestRuntime)
+    ppe.to(module, 'dummy', runtime_class=MyRuntime)
+    ppe.to(module.layer2, 'dummy', runtime_class=MyRuntime)
     with pytest.raises(ValueError, match="nested"):
         for _ in ppe.runtime._runtime.named_runtime_modules(module):
             pass