You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2021/05/11 07:54:11 UTC

[GitHub] [tvm] zotanika opened a new pull request #8015: [Caffe Frontend] adding Reduction op

zotanika opened a new pull request #8015:
URL: https://github.com/apache/tvm/pull/8015


   Thanks for contributing to TVM!   Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @ them in the pull request thread.
   


-- 
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.

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



[GitHub] [tvm] FrozenGene commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r672838980



##########
File path: tests/python/frontend/caffe/test_forward.py
##########
@@ -763,6 +763,85 @@ def test_forward_TanH():
     _test_tanh(np.random.rand(10).astype(np.float32))
 
 
+#######################################################################
+# Reduction
+# -----------

Review comment:
       remove unnecessary redundant `--` and keep align with `Reduction`  




-- 
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



[GitHub] [tvm] zotanika commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
zotanika commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r634073556



##########
File path: python/tvm/relay/frontend/caffe.py
##########
@@ -558,6 +559,36 @@ def convert_tanh(self, op):
         out = _op.tanh(in_expr)
         return out
 
+    def convert_reduction(self, op):
+        """ Convert Reduction layer """
+        reduction_dic = ["NOP", "SUM", "ASUM", "SUMSQ", "MEAN"]
+        inputs = op.bottom
+        in_expr = self.exp_tab.get_expr(inputs[0])
+        method = op.reduction_param.operation
+        axis = op.reduction_param.axis
+        coeff = op.reduction_param.coeff
+        if reduction_dic[method] == "MEAN":
+            coeff /= len(inputs)
+        coeff_expr = self.exp_tab.new_const(np.asarray(coeff, np.float32))
+
+        if reduction_dic[method] == "SUM":
+            out = _op.sum(in_expr, axis=axis)
+        elif reduction_dic[method] == "MEAN":
+            out = _op.mean(in_expr, axis=axis)
+        elif reduction_dic[method] == "ASUM":
+            in_expr = _op.abs(in_expr)
+            out = _op.sum(in_expr, axis=axis)
+        elif reduction_dic[method] == "SUMSQ":
+            in_expr = _op.multiply(in_expr, in_expr)
+            out = _op.sum(in_expr, axis=axis)
+        else:
+            raise tvm.error.OpAttributeInvalid(
+                "reduction method:{} is invalid in Caffe frontend.".format(method)
+            )
+
+        out = _op.multiply(out, coeff_expr)

Review comment:
       this suggestion is included in new commit.




-- 
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.

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



[GitHub] [tvm] zotanika commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
zotanika commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r634074541



##########
File path: tests/python/frontend/caffe/test_forward.py
##########
@@ -763,6 +763,37 @@ def test_forward_TanH():
     _test_tanh(np.random.rand(10).astype(np.float32))
 
 
+#######################################################################
+# Reduction
+# -----------
+
+
+def _test_reduction(data, **kwargs):
+    """ One iteration of Reduction """
+    _test_op(data, L.Reduction, "Reduction", **kwargs)
+
+
+def test_forward_Reduction():
+    """ Reduction """
+    reduction_op = {"SUM": 1, "ASUM": 2, "SUMSQ": 3, "MEAN": 4}
+    _test_reduction(np.random.rand(10).astype(np.float32), operation=reduction_op["SUM"], axis=0)
+    _test_reduction(
+        np.random.rand(10).astype(np.float32), operation=reduction_op["SUM"], axis=0, coeff=0.0

Review comment:
       this suggestion is included in new commit. some more codes were introduced to handle reductions on non-tail axis and give back the result equivalent to Caffe framework.
   




-- 
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.

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



[GitHub] [tvm] zotanika commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
zotanika commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r634065680



##########
File path: python/tvm/relay/frontend/caffe.py
##########
@@ -558,6 +559,36 @@ def convert_tanh(self, op):
         out = _op.tanh(in_expr)
         return out
 
+    def convert_reduction(self, op):
+        """ Convert Reduction layer """
+        reduction_dic = ["NOP", "SUM", "ASUM", "SUMSQ", "MEAN"]

Review comment:
       `NOP` was placed in reduction_dic to remind users(and me) of the attribution of Caffe Reduction op, default operation `SUM` is starting from **1**; [https://caffe.berkeleyvision.org/tutorial/layers/reduction.html](https://caffe.berkeleyvision.org/tutorial/layers/reduction.html).
   If warning against the wrong attribution 0 is considered redundant, I'll remove it..
   




-- 
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.

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



[GitHub] [tvm] masahi merged pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
masahi merged pull request #8015:
URL: https://github.com/apache/tvm/pull/8015


   


-- 
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



[GitHub] [tvm] masahi commented on pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#issuecomment-1008436126


   @zotanika please resolve the conflict.


-- 
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



[GitHub] [tvm] masahi closed pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
masahi closed pull request #8015:
URL: https://github.com/apache/tvm/pull/8015


   


-- 
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



[GitHub] [tvm] FrozenGene commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r672838980



##########
File path: tests/python/frontend/caffe/test_forward.py
##########
@@ -763,6 +763,85 @@ def test_forward_TanH():
     _test_tanh(np.random.rand(10).astype(np.float32))
 
 
+#######################################################################
+# Reduction
+# -----------

Review comment:
       remove unnecessary redundant `--` and keep align with `Reduction`  




-- 
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



[GitHub] [tvm] FrozenGene commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r631511977



##########
File path: python/tvm/relay/frontend/caffe.py
##########
@@ -558,6 +559,36 @@ def convert_tanh(self, op):
         out = _op.tanh(in_expr)
         return out
 
+    def convert_reduction(self, op):
+        """ Convert Reduction layer """
+        reduction_dic = ["NOP", "SUM", "ASUM", "SUMSQ", "MEAN"]
+        inputs = op.bottom
+        in_expr = self.exp_tab.get_expr(inputs[0])
+        method = op.reduction_param.operation
+        axis = op.reduction_param.axis
+        coeff = op.reduction_param.coeff
+        if reduction_dic[method] == "MEAN":
+            coeff /= len(inputs)
+        coeff_expr = self.exp_tab.new_const(np.asarray(coeff, np.float32))
+
+        if reduction_dic[method] == "SUM":
+            out = _op.sum(in_expr, axis=axis)
+        elif reduction_dic[method] == "MEAN":
+            out = _op.mean(in_expr, axis=axis)
+        elif reduction_dic[method] == "ASUM":
+            in_expr = _op.abs(in_expr)
+            out = _op.sum(in_expr, axis=axis)
+        elif reduction_dic[method] == "SUMSQ":
+            in_expr = _op.multiply(in_expr, in_expr)
+            out = _op.sum(in_expr, axis=axis)
+        else:
+            raise tvm.error.OpAttributeInvalid(
+                "reduction method:{} is invalid in Caffe frontend.".format(method)
+            )
+
+        out = _op.multiply(out, coeff_expr)

Review comment:
       Maybe we could do a little optimization for this. For example, if `coeff_expr` is not `1`, we call `multiply`.

##########
File path: python/tvm/relay/frontend/caffe.py
##########
@@ -558,6 +559,36 @@ def convert_tanh(self, op):
         out = _op.tanh(in_expr)
         return out
 
+    def convert_reduction(self, op):
+        """ Convert Reduction layer """
+        reduction_dic = ["NOP", "SUM", "ASUM", "SUMSQ", "MEAN"]

Review comment:
       I don't find logic code to handle `NOP`.

##########
File path: tests/python/frontend/caffe/test_forward.py
##########
@@ -763,6 +763,37 @@ def test_forward_TanH():
     _test_tanh(np.random.rand(10).astype(np.float32))
 
 
+#######################################################################
+# Reduction
+# -----------
+
+
+def _test_reduction(data, **kwargs):
+    """ One iteration of Reduction """
+    _test_op(data, L.Reduction, "Reduction", **kwargs)
+
+
+def test_forward_Reduction():
+    """ Reduction """
+    reduction_op = {"SUM": 1, "ASUM": 2, "SUMSQ": 3, "MEAN": 4}
+    _test_reduction(np.random.rand(10).astype(np.float32), operation=reduction_op["SUM"], axis=0)
+    _test_reduction(
+        np.random.rand(10).astype(np.float32), operation=reduction_op["SUM"], axis=0, coeff=0.0

Review comment:
       Let us add more values for `axis` and `coeff` to cover more conditions.




-- 
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.

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



[GitHub] [tvm] FrozenGene commented on a change in pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
FrozenGene commented on a change in pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#discussion_r672838980



##########
File path: tests/python/frontend/caffe/test_forward.py
##########
@@ -763,6 +763,85 @@ def test_forward_TanH():
     _test_tanh(np.random.rand(10).astype(np.float32))
 
 
+#######################################################################
+# Reduction
+# -----------

Review comment:
       remove unnecessary redundant `--` and keep align with `Reduction`  




-- 
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



[GitHub] [tvm] tqchen commented on pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
tqchen commented on pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#issuecomment-839725793


   cc @jcf94 @kazum @FrozenGene please help to manage this  PR. Thank you!


-- 
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.

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



[GitHub] [tvm] junrushao1994 commented on pull request #8015: [Caffe Frontend] adding Reduction op

Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on pull request #8015:
URL: https://github.com/apache/tvm/pull/8015#issuecomment-881845570


   @FrozenGene Please take another look :-) 


-- 
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