You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by "cyx-6 (via GitHub)" <gi...@apache.org> on 2023/10/20 17:03:01 UTC

[PR] [Unity] `nn.Mutator` for `nn.Module` level transform [tvm]

cyx-6 opened a new pull request, #15958:
URL: https://github.com/apache/tvm/pull/15958

   This PR introduces the `nn.Mutator` for the `nn.Module` level transform.
   
   cc: @junrushao 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Unity] `nn.Mutator` for `nn.Module` level transform [tvm]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on code in PR #15958:
URL: https://github.com/apache/tvm/pull/15958#discussion_r1367348696


##########
python/tvm/relax/frontend/nn/core.py:
##########
@@ -520,6 +520,87 @@ def forward(self, x):  # pylint: disable=invalid-name
         return x
 
 
+class Matuator:
+    def visit_module(self, name: str, node: Module) -> Any:
+        """The base visiting method for mutation of nn.Module nodes.
+
+        Parameters
+        ----------
+        name : str
+            The name of the current node in parent's attribute.
+
+        node : nn.Module
+            The current node of nn.Module to mutate.
+
+        Returns
+        ------
+        ret_node: Any
+            The new node to replace current node.
+        """
+        return self.visit(name, node)
+
+    def visit_effect(self, name: str, node: Parameter) -> Any:
+        """The base visiting method for mutation of nn.Parameter nodes.
+
+        Parameters
+        ----------
+        name : str
+            The name of the current node in parent's attribute.
+
+        node : nn.Parameter
+            The current node of nn.Parameter to mutate.
+
+        Returns
+        ------
+        ret_node: Any
+            The new node to replace current node.
+        """
+        return self.visit(name, node)
+
+    def visit_param(self, name: str, node: Effect) -> Any:
+        """The base visiting method for mutation of nn.Effect nodes.
+
+        Parameters
+        ----------
+        name : str
+            The name of the current node in parent's attribute.
+
+        node : nn.Effect
+            The current node of nn.Effect to mutate.
+
+        Returns
+        ------
+        ret_node: Any
+            The new node to replace current node.
+        """
+        return self.visit(name, node)
+
+    def visit(self, name: str, node: Any) -> Any:
+        """The base dispatching method for visiting of all nodes.
+
+        Parameters
+        ----------
+        name : str
+            The name of the current node in parent's attribute.
+
+        node : Any
+            The current node to visit.
+
+        Returns
+        ------
+        ret_node: Any
+            The new node to replace current node.
+        """
+        for key, value in node.__dict__.items():
+            if isinstance(value, Module):

Review Comment:
   You might want to check ModuleList as well, which may need some special handling



##########
tests/python/relax/test_frontend_nn_mutator.py:
##########
@@ -0,0 +1,160 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any
+
+import tvm
+from tvm.relax.frontend import nn
+
+
+def test_mutator_naming():

Review Comment:
   Add a test for ModuleList



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Unity] `nn.Mutator` for `nn.Module` level transform [tvm]

Posted by "cyx-6 (via GitHub)" <gi...@apache.org>.
cyx-6 merged PR #15958:
URL: https://github.com/apache/tvm/pull/15958


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Unity] `nn.Mutator` for `nn.Module` level transform [tvm]

Posted by "cyx-6 (via GitHub)" <gi...@apache.org>.
cyx-6 commented on code in PR #15958:
URL: https://github.com/apache/tvm/pull/15958#discussion_r1367429226


##########
tests/python/relax/test_frontend_nn_mutator.py:
##########
@@ -0,0 +1,160 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+from typing import Any
+
+import tvm
+from tvm.relax.frontend import nn
+
+
+def test_mutator_naming():

Review Comment:
   added



##########
python/tvm/relax/frontend/nn/core.py:
##########
@@ -520,6 +520,87 @@ def forward(self, x):  # pylint: disable=invalid-name
         return x
 
 
+class Matuator:

Review Comment:
   moved



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Unity] `nn.Mutator` for `nn.Module` level transform [tvm]

Posted by "junrushao (via GitHub)" <gi...@apache.org>.
junrushao commented on code in PR #15958:
URL: https://github.com/apache/tvm/pull/15958#discussion_r1367354484


##########
python/tvm/relax/frontend/nn/core.py:
##########
@@ -520,6 +520,87 @@ def forward(self, x):  # pylint: disable=invalid-name
         return x
 
 
+class Matuator:

Review Comment:
   move this to `visitor.py`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org