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 2022/03/02 17:55:41 UTC

[GitHub] [tvm] ShuKun-coder opened a new pull request #10452: @shukun.net: Complete pytorch grid_sample

ShuKun-coder opened a new pull request #10452:
URL: https://github.com/apache/tvm/pull/10452


   @masahi 
   


-- 
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] shukun-ziqiangxu closed pull request #10452: @shukun.net: Complete pytorch grid_sample

Posted by GitBox <gi...@apache.org>.
shukun-ziqiangxu closed pull request #10452:
URL: https://github.com/apache/tvm/pull/10452


   


-- 
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] ShuKun-coder commented on pull request #10452: @shukun.net: Complete pytorch grid_sample

Posted by GitBox <gi...@apache.org>.
ShuKun-coder commented on pull request #10452:
URL: https://github.com/apache/tvm/pull/10452#issuecomment-1058199297


   Pytorch's grid_sample() support various interploation options:
   (1) data dimension: 2D / 3D
   (2) interploation method: nearest / bilinear / bicubic
   (3) padding_mode: zeros / border / reflection
   (4) align_corners: True / False
   
   However, TVM only support a part of above options:
   (1) data dimension: 2D
   (2) interploation method: bilinear
   (3) padding_mode: zeros / border
   (4) align_corners: True
   
   My PR completes the options not supported by TVM, and keep existing grid_sample of onnx/pytorch unchanged. 
   Thanks for your comment,I will correct my PR's format soon.
   


-- 
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] gromero commented on a change in pull request #10452: @shukun.net: Complete pytorch grid_sample

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



##########
File path: include/tvm/relay/attrs/image.h
##########
@@ -291,8 +292,25 @@ struct GridSampleAttrs : public tvm::AttrsNode<GridSampleAttrs> {
     TVM_ATTR_FIELD(padding_mode)
         .set_default("zeros")
         .describe(
-            "Specify the padding mode to use."
-            "zeros, border etc.");
+            "If :attr:'grid' has values outside the range of '[-1, 1]', the corresponding"
+            "outputs are handled as defined by padding_mode. Options are"
+            "padding_mode='zeros': use '0' for out-of-bound grid locations,"
+            "padding_mode='border': use border values for out-of-bound grid locations"
+            "padding_mode='reflection': use values at locations reflected by"
+            "the border for out-of-bound grid locations. For location far away"
+            "from the border, it will keep being reflected until becoming in bound,"
+            "e.g., (normalized) pixel location 'x = -3.5' reflects by border '-1'"
+            "and becomes 'x' = 1.5, then reflects by border '1' and becomes"
+            "'x' = -0.5");
+    TVM_ATTR_FIELD(align_corners)
+        .set_default(true)
+        .describe(
+            "Geometrically, we consider the pixels of the"
+            "input  as squares rather than points."

Review comment:
       There is a double space here, hence one should be removed.

##########
File path: python/tvm/relay/op/image/_image.py
##########
@@ -366,19 +366,41 @@ def compute_grid_sample(attrs, inputs, out_dtype):
     method = attrs.method
     layout = attrs.layout
     padding_mode = attrs.padding_mode
-    return [topi.image.grid_sample(inputs[0], inputs[1], method, layout, padding_mode)]
+    align_corners = attrs.align_corners
+    return [
+            topi.image.grid_sample(
+                inputs[0],
+                inputs[1],
+                method,
+                layout,
+                padding_mode,
+                align_corners
+            )
+        ]
 
 
 reg.register_injective_schedule("image.grid_sample")
 
 
 @script
 def _grid_sample_func(data, grid):
-    out = output_tensor((4,), "int64")
-    out[0] = int64(data[0])
-    out[1] = int64(data[1])
-    out[2] = int64(grid[2])
-    out[3] = int64(grid[3])
+    if len(data) == 4:
+        out = output_tensor((4,), "int64")
+        out[0] = int64(data[0])
+        out[1] = int64(data[1])
+        out[2] = int64(grid[2])
+        out[3] = int64(grid[3])
+    elif len(data) == 5:
+        out = output_tensor((5,), "int64")
+        out[0] = int64(data[0])
+        out[1] = int64(data[1])
+        out[2] = int64(grid[2])
+        out[3] = int64(grid[3])
+        out[4] = int64(grid[4])
+    else:
+        msg = f"dimension is not surpported"

Review comment:
       s/surpported/supported/ here

##########
File path: python/tvm/relay/frontend/pytorch.py
##########
@@ -2913,23 +2913,47 @@ def mv(self, inputs, _):
         return _op.transform.squeeze(dense_result)
 
     def grid_sampler(self, inputs, input_types):
-        if inputs[2] == 0:
-            mode = "bilinear"
-        else:
-            msg = "Only bilinear mode is supported in grid_sampler"
-            raise NotImplementedError(msg)
-
-        if inputs[3] == 0:
-            padding_mode = "zeros"
-        elif inputs[3] == 1:
-            padding_mode = "border"
-        else:
-            msg = "Only zeros and border padding mode are supported in grid_sampler"
-            raise NotImplementedError(msg)
+        interpolate_mode = inputs[2]
+        padding_mode = inputs[3]
+        align_corners = inputs[4]
+        data_shape = self.infer_shape_with_prelude(inputs[0])
+
+        if len(data_shape) == 4:
+            layout = "NCHW"
+            axes = [0, 3, 1, 2]
+            grid = _op.transform.transpose(inputs[1], axes)
+        elif len(data_shape) == 5:
+            layout = "NCDHW"
+            axes = [0, 4, 1, 2, 3]
+            grid = _op.transform.transpose(inputs[1], axes)
+        else:
+            msg = f"only 4D and 5D are surpported."
+            raise ValueError(msg)
+
+        if interpolate_mode == 0:
+            interpolate_str = "bilinear"
+        elif interpolate_mode == 1:
+            interpolate_str = "nearest"
+        elif interpolate_mode == 2:
+            interpolate_str = "bicubic"
+        else:
+            msg = f"interpolation method {interpolate_mode} is not surpported"

Review comment:
       s/surpported/supported/ here

##########
File path: include/tvm/relay/attrs/image.h
##########
@@ -291,8 +292,25 @@ struct GridSampleAttrs : public tvm::AttrsNode<GridSampleAttrs> {
     TVM_ATTR_FIELD(padding_mode)
         .set_default("zeros")
         .describe(
-            "Specify the padding mode to use."
-            "zeros, border etc.");
+            "If :attr:'grid' has values outside the range of '[-1, 1]', the corresponding"
+            "outputs are handled as defined by padding_mode. Options are"
+            "padding_mode='zeros': use '0' for out-of-bound grid locations,"
+            "padding_mode='border': use border values for out-of-bound grid locations"
+            "padding_mode='reflection': use values at locations reflected by"
+            "the border for out-of-bound grid locations. For location far away"
+            "from the border, it will keep being reflected until becoming in bound,"
+            "e.g., (normalized) pixel location 'x = -3.5' reflects by border '-1'"
+            "and becomes 'x' = 1.5, then reflects by border '1' and becomes"
+            "'x' = -0.5");
+    TVM_ATTR_FIELD(align_corners)
+        .set_default(true)
+        .describe(
+            "Geometrically, we consider the pixels of the"
+            "input  as squares rather than points."

Review comment:
       Shouldn't also the options `nearest` and `bicubic` be added/listed/explained for the `method` attribute field? Similarly to what you did for `padding_mode` and `align_corners`  options? 

##########
File path: python/tvm/relay/frontend/pytorch.py
##########
@@ -2913,23 +2913,47 @@ def mv(self, inputs, _):
         return _op.transform.squeeze(dense_result)
 
     def grid_sampler(self, inputs, input_types):
-        if inputs[2] == 0:
-            mode = "bilinear"
-        else:
-            msg = "Only bilinear mode is supported in grid_sampler"
-            raise NotImplementedError(msg)
-
-        if inputs[3] == 0:
-            padding_mode = "zeros"
-        elif inputs[3] == 1:
-            padding_mode = "border"
-        else:
-            msg = "Only zeros and border padding mode are supported in grid_sampler"
-            raise NotImplementedError(msg)
+        interpolate_mode = inputs[2]
+        padding_mode = inputs[3]
+        align_corners = inputs[4]
+        data_shape = self.infer_shape_with_prelude(inputs[0])
+
+        if len(data_shape) == 4:
+            layout = "NCHW"
+            axes = [0, 3, 1, 2]
+            grid = _op.transform.transpose(inputs[1], axes)
+        elif len(data_shape) == 5:
+            layout = "NCDHW"
+            axes = [0, 4, 1, 2, 3]
+            grid = _op.transform.transpose(inputs[1], axes)
+        else:
+            msg = f"only 4D and 5D are surpported."
+            raise ValueError(msg)
+
+        if interpolate_mode == 0:
+            interpolate_str = "bilinear"
+        elif interpolate_mode == 1:
+            interpolate_str = "nearest"
+        elif interpolate_mode == 2:
+            interpolate_str = "bicubic"
+        else:
+            msg = f"interpolation method {interpolate_mode} is not surpported"
+            raise ValueError(msg)
+
+        if padding_mode == 0:
+            padding_mode_str = "zeros"
+        elif padding_mode == 1:
+            padding_mode_str = "border"
+        elif padding_mode == 2:
+            padding_mode_str = 'reflection'
+        else:
+            msg = f"padding_mode {padding_mode} is not surpportted"

Review comment:
       s/surpported/supported/ here

##########
File path: python/tvm/relay/frontend/pytorch.py
##########
@@ -2913,23 +2913,47 @@ def mv(self, inputs, _):
         return _op.transform.squeeze(dense_result)
 
     def grid_sampler(self, inputs, input_types):
-        if inputs[2] == 0:
-            mode = "bilinear"
-        else:
-            msg = "Only bilinear mode is supported in grid_sampler"
-            raise NotImplementedError(msg)
-
-        if inputs[3] == 0:
-            padding_mode = "zeros"
-        elif inputs[3] == 1:
-            padding_mode = "border"
-        else:
-            msg = "Only zeros and border padding mode are supported in grid_sampler"
-            raise NotImplementedError(msg)
+        interpolate_mode = inputs[2]
+        padding_mode = inputs[3]
+        align_corners = inputs[4]
+        data_shape = self.infer_shape_with_prelude(inputs[0])
+
+        if len(data_shape) == 4:
+            layout = "NCHW"
+            axes = [0, 3, 1, 2]
+            grid = _op.transform.transpose(inputs[1], axes)
+        elif len(data_shape) == 5:
+            layout = "NCDHW"
+            axes = [0, 4, 1, 2, 3]
+            grid = _op.transform.transpose(inputs[1], axes)
+        else:
+            msg = f"only 4D and 5D are surpported."

Review comment:
       s/surpported/supported/ here




-- 
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] gromero commented on pull request #10452: @shukun.net: Complete pytorch grid_sample

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


   @shukun-ziqiangxu Thanks for the clarifications.
   
   BTW, the CI failed but it seems like a CI glitch and not related to your changes. Since you're about to submit a v2 the CI will be re-triggered and hopefully it will work fine that time. 
   
   


-- 
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] ShuKun-coder edited a comment on pull request #10452: @shukun.net: Complete pytorch grid_sample

Posted by GitBox <gi...@apache.org>.
ShuKun-coder edited a comment on pull request #10452:
URL: https://github.com/apache/tvm/pull/10452#issuecomment-1058199297


   Hi, @gromero 
   
   Pytorch's grid_sample() support various interploation options:
   (1) data dimension: 2D / 3D
   (2) interploation method: nearest / bilinear / bicubic
   (3) padding_mode: zeros / border / reflection
   (4) align_corners: True / False
   
   However, TVM only support a part of above options:
   (1) data dimension: 2D
   (2) interploation method: bilinear
   (3) padding_mode: zeros / border
   (4) align_corners: True
   
   My PR completes the options not supported by TVM, and keep existing grid_sample of onnx/pytorch unchanged. 
   Thanks for your comment,I will correct my PR's format soon.
   


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