You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "0xjah (via GitHub)" <gi...@apache.org> on 2026/04/08 16:42:04 UTC

[PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

0xjah opened a new pull request, #19371:
URL: https://github.com/apache/tvm/pull/19371

   This PR adds Relax TFLite frontend test coverage for:
   - L2_NORMALIZATION
   - SLICE
   - REVERSE_V2
   
   Part of fixing #18971.


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

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


##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -800,7 +800,12 @@ def convert_l2_normalization(self, op):
             )
 
         # TFL uses only the default epsilon value
-        out = relax.op.nn.l2_normalize(in_expr, eps=1e-12, axis=[input_tensor_rank - 1])
+        # Implement L2 normalization: output = input / sqrt(sum(input^2) + eps)
+        # L2 normalization is applied along the last axis
+        squared = relax.op.square(in_expr)
+        sum_squared = relax.op.sum(squared, axis=input_tensor_rank - 1, keepdims=True)
+        denom = relax.op.sqrt(sum_squared + 1e-12)

Review Comment:
   `sum_squared` is a Relax Expr, so you can't use the `+` operator with a raw Python float directly. This is what's causing the `TypeError: Please convert 1e-12` failure in the CI. You need to wrap the epsilon:
   ```suggestion
           eps = relax.const(1e-12, dtype="float32")
           denom = relax.op.sqrt(relax.op.add(sum_squared, eps))
   ```



##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -2251,7 +2256,9 @@ def convert_slice(self, op):
             else:
                 end[i] += begin[i]
 
-        out = relax.op.strided_slice(in_expr, begin, end)
+        # Create axes list for all dimensions being sliced
+        axes = list(range(input_tensor_rank))
+        out = relax.op.strided_slice(in_expr, axes=axes, begin=begin, end=end)

Review Comment:
   The `begin` and `end` values read from the TFLite flatbuffer come back as `numpy.int32`, and `strided_slice` doesn't accept those. That's why you get the `TypeError: Cannot convert 1 with type <class 'numpy.int32'>` CI failure. Cast them to plain Python ints before passing:
   
   ```suggestion
           axes = list(range(len(begin)))
           begin = [int(b) for b in begin]
           end = [int(e) for e in end]
           axes = list(range(input_tensor_rank))
           out = relax.op.strided_slice(in_expr, axes=axes, begin=begin, end=end)
   ```



-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on PR #19371:
URL: https://github.com/apache/tvm/pull/19371#issuecomment-4216532188

   Hi @0xjah Can you have a look at the CI error and fix it? Thanks!


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

Posted by "OmarAzizi (via GitHub)" <gi...@apache.org>.
OmarAzizi commented on PR #19371:
URL: https://github.com/apache/tvm/pull/19371#issuecomment-4222817513

   It looks like the Gemini bot suggestions on `test_slice` and `test_reverse_v2` were marked resolved, but weren't actually addressed in the code. 
   
   The parameterization suggestions are worth doing since single-shape tests don't catch edge cases like `size=-1` in slice or multi-axis reversal.


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex commented on PR #19371:
URL: https://github.com/apache/tvm/pull/19371#issuecomment-4229866591

   Thanks @0xjah @OmarAzizi 


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19371:
URL: https://github.com/apache/tvm/pull/19371#discussion_r3052843530


##########
tests/python/relax/test_frontend_tflite.py:
##########
@@ -707,6 +707,67 @@ def main(x: R.Tensor((5, 30), dtype="float32")) -> R.Tensor(out_shape, dtype="in
     verify(TfInput, Expected)
 
 
+def test_l2_normalization():
+    class L2Normalization(tf.Module):
+        @tf.function(input_signature=[tf.TensorSpec(shape=(2, 4), dtype=tf.float32)])
+        def func(self, x):
+            return tf.nn.l2_normalize(x, axis=-1)
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor((2, 4), dtype="float32")) -> R.Tensor((2, 4), dtype="float32"):
+            R.func_attr({"num_input": 1})
+            with R.dataflow():
+                gv: R.Tensor((2, 4), dtype="float32") = R.nn.l2_normalize(
+                    x, eps=1e-12, axis=[1]
+                )
+                R.output(gv)
+            return gv
+
+    verify(L2Normalization, Expected)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   To improve test coverage, consider parameterizing this test to cover various input shapes. This will make the test more robust and help catch potential edge cases.
   
   ```python
   @pytest.mark.parametrize(
       "in_shape",
       [
           (2, 4),
           (1, 10),
           (5, 3, 8),
       ],
   )
   def test_l2_normalization(in_shape):
       class L2Normalization(tf.Module):
           @tf.function(input_signature=[tf.TensorSpec(shape=in_shape, dtype=tf.float32)])
           def func(self, x):
               return tf.nn.l2_normalize(x, axis=-1)
   
       @I.ir_module
       class Expected:
           @R.function
           def main(x: R.Tensor(in_shape, dtype="float32")) -> R.Tensor(in_shape, dtype="float32"):
               R.func_attr({"num_input": 1})
               with R.dataflow():
                   gv: R.Tensor(in_shape, dtype="float32") = R.nn.l2_normalize(
                       x, eps=1e-12, axis=[len(in_shape) - 1]
                   )
                   R.output(gv)
               return gv
   
       verify(L2Normalization, Expected)
   ```



##########
tests/python/relax/test_frontend_tflite.py:
##########
@@ -707,6 +707,67 @@ def main(x: R.Tensor((5, 30), dtype="float32")) -> R.Tensor(out_shape, dtype="in
     verify(TfInput, Expected)
 
 
+def test_l2_normalization():
+    class L2Normalization(tf.Module):
+        @tf.function(input_signature=[tf.TensorSpec(shape=(2, 4), dtype=tf.float32)])
+        def func(self, x):
+            return tf.nn.l2_normalize(x, axis=-1)
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor((2, 4), dtype="float32")) -> R.Tensor((2, 4), dtype="float32"):
+            R.func_attr({"num_input": 1})
+            with R.dataflow():
+                gv: R.Tensor((2, 4), dtype="float32") = R.nn.l2_normalize(
+                    x, eps=1e-12, axis=[1]
+                )
+                R.output(gv)
+            return gv
+
+    verify(L2Normalization, Expected)
+
+
+def test_slice():
+    class Slice(tf.Module):
+        @tf.function(input_signature=[tf.TensorSpec(shape=(3, 4), dtype=tf.float32)])
+        def func(self, x):
+            return tf.slice(x, begin=[1, 1], size=[2, 2])
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor((3, 4), dtype="float32")) -> R.Tensor((2, 2), dtype="float32"):
+            R.func_attr({"num_input": 1})
+            with R.dataflow():
+                gv: R.Tensor((2, 2), dtype="float32") = R.strided_slice(
+                    x, begin=[1, 1], end=[3, 3]
+                )
+                R.output(gv)
+            return gv
+
+    verify(Slice, Expected)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   To improve test coverage, consider parameterizing this test to cover various input shapes, `begin` and `size` parameters, including edge cases like using `-1` for size.
   
   ```python
   @pytest.mark.parametrize(
       "in_shape, begin, size",
       [
           ((3, 4), [1, 1], [2, 2]),
           ((5, 6, 7), [1, 0, 2], [3, 4, 1]),
           ((10,), [2], [-1]),
       ],
   )
   def test_slice(in_shape, begin, size):
       class Slice(tf.Module):
           @tf.function(input_signature=[tf.TensorSpec(shape=in_shape, dtype=tf.float32)])
           def func(self, x):
               return tf.slice(x, begin=begin, size=size)
   
       out_shape = []
       end = []
       for i in range(len(in_shape)):
           s = size[i]
           if s == -1:
               s = in_shape[i] - begin[i]
           out_shape.append(s)
           end.append(begin[i] + s)
   
       @I.ir_module
       class Expected:
           @R.function
           def main(x: R.Tensor(in_shape, dtype="float32")) -> R.Tensor(tuple(out_shape), dtype="float32"):
               R.func_attr({"num_input": 1})
               with R.dataflow():
                   gv: R.Tensor(tuple(out_shape), dtype="float32") = R.strided_slice(
                       x, begin=begin, end=end
                   )
                   R.output(gv)
               return gv
   
       verify(Slice, Expected)
   ```



##########
tests/python/relax/test_frontend_tflite.py:
##########
@@ -707,6 +707,67 @@ def main(x: R.Tensor((5, 30), dtype="float32")) -> R.Tensor(out_shape, dtype="in
     verify(TfInput, Expected)
 
 
+def test_l2_normalization():
+    class L2Normalization(tf.Module):
+        @tf.function(input_signature=[tf.TensorSpec(shape=(2, 4), dtype=tf.float32)])
+        def func(self, x):
+            return tf.nn.l2_normalize(x, axis=-1)
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor((2, 4), dtype="float32")) -> R.Tensor((2, 4), dtype="float32"):
+            R.func_attr({"num_input": 1})
+            with R.dataflow():
+                gv: R.Tensor((2, 4), dtype="float32") = R.nn.l2_normalize(
+                    x, eps=1e-12, axis=[1]
+                )
+                R.output(gv)
+            return gv
+
+    verify(L2Normalization, Expected)
+
+
+def test_slice():
+    class Slice(tf.Module):
+        @tf.function(input_signature=[tf.TensorSpec(shape=(3, 4), dtype=tf.float32)])
+        def func(self, x):
+            return tf.slice(x, begin=[1, 1], size=[2, 2])
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor((3, 4), dtype="float32")) -> R.Tensor((2, 2), dtype="float32"):
+            R.func_attr({"num_input": 1})
+            with R.dataflow():
+                gv: R.Tensor((2, 2), dtype="float32") = R.strided_slice(
+                    x, begin=[1, 1], end=[3, 3]
+                )
+                R.output(gv)
+            return gv
+
+    verify(Slice, Expected)
+
+
+def test_reverse_v2():
+    class ReverseV2(tf.Module):
+        @tf.function(input_signature=[tf.TensorSpec(shape=(2, 3), dtype=tf.float32)])
+        def func(self, x):
+            return tf.reverse(x, axis=[1])
+
+    @I.ir_module
+    class Expected:
+        @R.function
+        def main(x: R.Tensor((2, 3), dtype="float32")) -> R.Tensor((2, 3), dtype="float32"):
+            R.func_attr({"num_input": 1})
+            with R.dataflow():
+                gv: R.Tensor((2, 3), dtype="float32") = R.reverse(x, axis=1)
+                R.output(gv)
+            return gv
+
+    verify(ReverseV2, Expected)

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   To improve test coverage, consider parameterizing this test to cover various input shapes and axes. This will make the test more robust.
   
   ```python
   @pytest.mark.parametrize(
       "in_shape, axis",
       [
           ((2, 3), [1]),
           ((4, 5, 6), [0]),
           ((10,), [0]),
       ],
   )
   def test_reverse_v2(in_shape, axis):
       class ReverseV2(tf.Module):
           @tf.function(input_signature=[tf.TensorSpec(shape=in_shape, dtype=tf.float32)])
           def func(self, x):
               return tf.reverse(x, axis=axis)
   
       @I.ir_module
       class Expected:
           @R.function
           def main(x: R.Tensor(in_shape, dtype="float32")) -> R.Tensor(in_shape, dtype="float32"):
               R.func_attr({"num_input": 1})
               with R.dataflow():
                   gv: R.Tensor(in_shape, dtype="float32") = R.reverse(x, axis=axis[0])
                   R.output(gv)
               return gv
   
       verify(ReverseV2, Expected)
   ```



-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [relax][frontend][tflite] Add tests for l2_normalization/slice/reverse_v2 [tvm]

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


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org