Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions python/tvm/relax/frontend/tflite/tflite_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(relax.op.add(sum_squared, relax.const(1e-12, "float32")))
out = relax.op.divide(in_expr, denom)

# if we have fused activation fn
if output_tensor.qnn_params:
Expand Down Expand Up @@ -2251,8 +2256,11 @@ 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))
begin = [int(v) for v in begin]
end = [int(v) for v in end]
out = relax.op.strided_slice(in_expr, axes=axes, begin=begin, end=end)
Comment on lines +2260 to +2263
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
axes = list(range(input_tensor_rank))
out = relax.op.strided_slice(in_expr, axes=axes, begin=begin, end=end)
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)

return out

def convert_select(self, op):
Expand Down Expand Up @@ -3425,7 +3433,7 @@ def convert_expand_dims(self, op):
axis = self.get_tensor_value(input_tensors[1])
if isinstance(axis, np.ndarray):
assert axis.size == 1, "only one value is expected."
axis = int(axis)
axis = int(axis.flat[0])

ndims = len(input_tensors[0].tensor.ShapeAsNumpy())
assert -1 - ndims <= axis <= ndims, "axis out of range"
Expand Down Expand Up @@ -3492,9 +3500,9 @@ def convert_reverse_v2(self, op):
axis = self.get_tensor_value(input_tensors[1])
if isinstance(axis, np.ndarray):
assert len(axis) == 1, "TFLite does not support multi-axis yet"
axis = int(axis)
axis = int(axis.flat[0])

out = relax.op.reverse(input_expr, axis)
out = relax.op.flip(input_expr, axis)
return out

def convert_matrix_set_diag(self, op):
Expand Down
49 changes: 49 additions & 0 deletions tests/python/relax/test_frontend_tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,55 @@ 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)

verify(L2Normalization)


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, axes=[0, 1], begin=[1, 1], end=[3, 3]
)
R.output(gv)
return gv

verify(Slice, Expected)
Comment thread
0xjah marked this conversation as resolved.


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.flip(x, axis=1)
R.output(gv)
return gv

verify(ReverseV2, Expected)
Comment thread
0xjah marked this conversation as resolved.


def _make_conv2d_module(data_shape, kernel_shape, data_format, strides, padding):
class Conv2DModule(tf.Module):
@tf.function(
Expand Down