gemini-code-assist[bot] commented on code in PR #19827:
URL: https://github.com/apache/tvm/pull/19827#discussion_r3433454256


##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -2759,6 +2759,38 @@ def _impl_v11(cls, bb, inputs, attr, params):
             # edge mode - replicate border values
             return bb.emit_te(topi.nn.replicate_pad, inputs[0], pad_before, 
pad_after)
 
+    @classmethod
+    def _impl_v19(cls, bb, inputs, attr, params):
+        pads = get_constant(inputs[1], params)
+        constant_value = get_constant(inputs[2], params)
+        if constant_value is not None:
+            constant_value = constant_value.data.numpy().item()
+        else:
+            constant_value = 0.0
+
+        if isinstance(pads, relax.Constant):
+            pad_before, pad_after = _np.split(pads.data.numpy(), 2)
+            pad_before = _np.ndarray.tolist(pad_before)
+            pad_after = _np.ndarray.tolist(pad_after)
+        else:
+            raise ValueError("Dynamic pads are not supported yet.")
+
+        pad_mode = attr.get("mode", b"constant").decode("utf-8")
+        if pad_mode not in ["constant", "edge", "reflect", "wrap"]:
+            raise tvm.error.OpAttributeInvalid(
+                "Value " + pad_mode + ' in attribute "mode" is invalid for 
operator Pad.'
+            )
+
+        if pad_mode == "constant":
+            return bb.emit_te(topi.nn.pad, inputs[0], pad_before, pad_after, 
constant_value)
+        elif pad_mode == "reflect":
+            return bb.emit_te(topi.nn.mirror_pad, inputs[0], pad_before, 
pad_after, "REFLECT")
+        elif pad_mode == "wrap":
+            return bb.emit_te(topi.nn.circular_pad, inputs[0], pad_before, 
pad_after)
+        else:
+            # edge mode - replicate border values
+            return bb.emit_te(topi.nn.replicate_pad, inputs[0], pad_before, 
pad_after)
+

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The implementation of `_impl_v19` is almost identical to `_impl_v11`, 
introducing significant code duplication. Since the only difference is the 
support for `mode="wrap"`, we can simplify `_impl_v19` by handling the `"wrap"` 
mode directly and delegating all other modes to `_impl_v11`. This reduces 
duplication and improves maintainability.
   
   ```python
       @classmethod
       def _impl_v19(cls, bb, inputs, attr, params):
           pad_mode = attr.get("mode", b"constant").decode("utf-8")
           if pad_mode == "wrap":
               pads = get_constant(inputs[1], params)
               if isinstance(pads, relax.Constant):
                   pad_before, pad_after = _np.split(pads.data.numpy(), 2)
                   pad_before = _np.ndarray.tolist(pad_before)
                   pad_after = _np.ndarray.tolist(pad_after)
               else:
                   raise ValueError("Dynamic pads are not supported yet.")
               return bb.emit_te(topi.nn.circular_pad, inputs[0], pad_before, 
pad_after)
   
           return cls._impl_v11(bb, inputs, attr, params)
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to