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
65 changes: 64 additions & 1 deletion python/tvm/relax/frontend/tflite/tflite_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def __init__(self, model, subgraph, exp_tab, ctx, conversion_state=None):
"BROADCAST_ARGS": self.convert_broadcast_args,
"CALL": self.convert_call,
"CALL_ONCE": self.convert_call_once,
"COMPLEX_ABS": self.convert_complex_abs,
"CAST": self.convert_cast,
"CEIL": functools.partial(self._convert_unary_elemwise, relax_op=_op.ceil),
"CONCATENATION": self.convert_concatenation,
Expand Down Expand Up @@ -252,6 +253,7 @@ def __init__(self, model, subgraph, exp_tab, ctx, conversion_state=None):
"HASHTABLE_LOOKUP": self.convert_hashtable_lookup,
"HASHTABLE_SIZE": self.convert_hashtable_size,
"IF": self.convert_if,
"IMAG": self.convert_imag,
"L2_NORMALIZATION": self.convert_l2_normalization,
"L2_POOL_2D": functools.partial(self.convert_pool2d, pool_type="l2"),
"LEAKY_RELU": self.convert_leaky_relu,
Expand Down Expand Up @@ -295,6 +297,7 @@ def __init__(self, model, subgraph, exp_tab, ctx, conversion_state=None):
"RANDOM_STANDARD_NORMAL": self.convert_random_standard_normal,
"RANDOM_UNIFORM": self.convert_random_uniform,
"READ_VARIABLE": self.convert_read_variable,
"REAL": self.convert_real,
"REDUCE_ALL": functools.partial(self._convert_reduce_bool, relax_op=_op.min),
"REDUCE_ANY": functools.partial(self._convert_reduce_bool, relax_op=_op.max),
"REDUCE_MAX": functools.partial(self._convert_reduce, relax_op=_op.max),
Expand Down Expand Up @@ -1006,6 +1009,7 @@ def get_tensor_type_as_numpy(self, tensor_wrapper):
TensorType.UINT32: np.uint32,
TensorType.UINT64: np.uint64,
TensorType.BOOL: np.bool_,
TensorType.COMPLEX64: np.complex64,
}[tensor_wrapper.tensor.Type()]

# pylint: disable=no-else-return
Expand Down Expand Up @@ -1051,6 +1055,8 @@ def get_tensor_type_str(self, tensor_type):
return "uint64"
if tensor_type == TensorType.BOOL:
return "bool"
if tensor_type == TensorType.COMPLEX64:
return "complex64"
raise NotImplementedError(f"Tensor type {tensor_type!s} is currently not supported")

def _get_shape_expr_from_tensor(self, shape_tensor, prefix):
Expand Down Expand Up @@ -7580,6 +7586,52 @@ def convert_fake_quant(self, op):
rounded = relax.op.floor(_op.add(_op.multiply(clamped_shifted, inv_scale), half))
return relax.op.add(_op.multiply(rounded, scale_expr), nudged_min_expr)

def convert_real(self, op):
"""Convert TFLite REAL op.

TFLite complex64 tensors are represented as float32[..., 2] in Relax,
where index 0 = real part, index 1 = imaginary part along the last axis
"""
input_tensors = self.get_input_tensors(op)
assert len(input_tensors) == 1, "input tensors length should be 1"
input_tensor = self.get_expr(input_tensors[0].tensor_idx)
# slice last axis at index 0, and squeeze to remove the last axis
real = _op.strided_slice(input_tensor, begin=[0], end=[1], strides=[1], axes=[-1])
return _op.squeeze(real, axis=[-1])

def convert_imag(self, op):
"""Convert TFLite IMAG op.

See convert_real for representation of complex64 tensors in Relax.
"""
input_tensors = self.get_input_tensors(op)
assert len(input_tensors) == 1, "input tensors length should be 1"
input_tensor = self.get_expr(input_tensors[0].tensor_idx)
# slice last axis at index 1, and squeeze to remove the last axis
imag = _op.strided_slice(input_tensor, begin=[1], end=[2], strides=[1], axes=[-1])
return _op.squeeze(imag, axis=[-1])

def convert_complex_abs(self, op):
"""Convert TFLite COMPLEX_ABS op: sqrt(real^2 + imag^2)

See convert_real for the float32[..., 2] complex representation convention.
"""
input_tensors = self.get_input_tensors(op)
assert len(input_tensors) == 1, "input tensors length should be 1"
input_tensor = self.get_expr(input_tensors[0].tensor_idx)
real = self.bb.emit(
_op.strided_slice(input_tensor, begin=[0], end=[1], strides=[1], axes=[-1])
)
real = self.bb.emit(_op.squeeze(real, axis=[-1]))
imag = self.bb.emit(
_op.strided_slice(input_tensor, begin=[1], end=[2], strides=[1], axes=[-1])
)
imag = self.bb.emit(_op.squeeze(imag, axis=[-1]))
real_sq = self.bb.emit(_op.multiply(real, real))
imag_sq = self.bb.emit(_op.multiply(imag, imag))
sum_expr = self.bb.emit(_op.add(real_sq, imag_sq))
return _op.sqrt(sum_expr)

def get_expr(self, input_tensor_idx):
return self.exp_tab.get_expr(get_tensor_name(self.subgraph, input_tensor_idx))

Expand Down Expand Up @@ -7609,6 +7661,12 @@ def get_tensor_expr(self, tensor, is_sparse=False):

type_str = self.get_tensor_type_str(tensor.tensor.Type())
value = self.get_tensor_value_or_prefetched(tensor, is_sparse)
# complex64 constants have no native Relax dtype. Reinterpret the
# interleaved float32 storage as float32[..., 2] to match the
# convention used for input tensors.
if type_str == "complex64":
value = value.view(np.float32).reshape(value.shape + (2,))
type_str = "float32"
return self.exp_tab.new_const(value, dtype=type_str, source_name=tensor.tensor.Name())

def get_tensor_shape(self, tensor_wrapper):
Expand Down Expand Up @@ -8044,8 +8102,9 @@ def _input_type(model):
input_shape = tuple(tensor.ShapeAsNumpy())
tensor_type = tensor.Type()
input_name = get_tensor_name(subgraph, input_)
input_dtype = _decode_type(tensor_type)
shape_dict[input_name] = input_shape
dtype_dict[input_name] = _decode_type(tensor_type)
dtype_dict[input_name] = input_dtype

return shape_dict, dtype_dict

Expand Down Expand Up @@ -8183,6 +8242,10 @@ def func(self, data):
dtype = (
_dtype_dict[model_input_name] if model_input_name in _dtype_dict else "float32"
)
if dtype == "complex64":
dtype = "float32"
if shape is not None:
shape = tuple(shape) + (2,)
input_var = relax.Var(
name_hint=model_input_name,
struct_info=relax.TensorStructInfo(shape=shape, dtype=dtype),
Expand Down
94 changes: 94 additions & 0 deletions tests/python/relax/test_frontend_tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -13020,5 +13020,99 @@ def test_unidirectional_sequence_rnn_time_major():
assert tuple(int(d) for d in out_shape) == (batch, time, num_units)


def test_real():
class Real(tf.Module):
@tf.function(input_signature=[tf.TensorSpec(shape=(2, 4), dtype=tf.complex64)])
def func(self, x):
return tf.math.real(x)

@I.ir_module
class Expected:
@R.function
def main(x: R.Tensor((2, 4, 2), dtype="float32")) -> R.Tensor((2, 4), dtype="float32"):
R.func_attr({"num_input": 1})
with R.dataflow():
lv: R.Tensor((2, 4, 1), dtype="float32") = R.strided_slice(
x,
(R.prim_value(-1),),
(R.prim_value(0),),
(R.prim_value(1),),
(R.prim_value(1),),
assume_inbound=False,
)
gv: R.Tensor((2, 4), dtype="float32") = R.squeeze(lv, axis=[-1])
R.output(gv)
return gv

verify(Real, Expected)


def test_imag():
class Imag(tf.Module):
@tf.function(input_signature=[tf.TensorSpec(shape=(2, 4), dtype=tf.complex64)])
def func(self, x):
return tf.math.imag(x)

@I.ir_module
class Expected:
@R.function
def main(x: R.Tensor((2, 4, 2), dtype="float32")) -> R.Tensor((2, 4), dtype="float32"):
R.func_attr({"num_input": 1})
with R.dataflow():
lv: R.Tensor((2, 4, 1), dtype="float32") = R.strided_slice(
x,
(R.prim_value(-1),),
(R.prim_value(1),),
(R.prim_value(2),),
(R.prim_value(1),),
assume_inbound=False,
)
gv: R.Tensor((2, 4), dtype="float32") = R.squeeze(lv, axis=[-1])
R.output(gv)
return gv

verify(Imag, Expected)


def test_complex_abs():
class ComplexAbs(tf.Module):
@tf.function(input_signature=[tf.TensorSpec(shape=(2, 4), dtype=tf.complex64)])
def func(self, x):
return tf.math.abs(x)

@I.ir_module
class Expected:
@R.function
def main(x: R.Tensor((2, 4, 2), dtype="float32")) -> R.Tensor((2, 4), dtype="float32"):
R.func_attr({"num_input": 1})
with R.dataflow():
lv: R.Tensor((2, 4, 1), dtype="float32") = R.strided_slice(
x,
(R.prim_value(-1),),
(R.prim_value(0),),
(R.prim_value(1),),
(R.prim_value(1),),
assume_inbound=False,
)
lv1: R.Tensor((2, 4), dtype="float32") = R.squeeze(lv, axis=[-1])
lv2: R.Tensor((2, 4, 1), dtype="float32") = R.strided_slice(
x,
(R.prim_value(-1),),
(R.prim_value(1),),
(R.prim_value(2),),
(R.prim_value(1),),
assume_inbound=False,
)
lv3: R.Tensor((2, 4), dtype="float32") = R.squeeze(lv2, axis=[-1])
lv4: R.Tensor((2, 4), dtype="float32") = R.multiply(lv1, lv1)
lv5: R.Tensor((2, 4), dtype="float32") = R.multiply(lv3, lv3)
lv6: R.Tensor((2, 4), dtype="float32") = R.add(lv4, lv5)
gv: R.Tensor((2, 4), dtype="float32") = R.sqrt(lv6)
R.output(gv)
return gv

verify(ComplexAbs, Expected)


if __name__ == "__main__":
pytest.main(["-s", __file__])
Loading