Skip to content

Commit bf520bd

Browse files
committed
Fix review notes
1 parent 4678cbf commit bf520bd

File tree

6 files changed

+91
-142
lines changed

6 files changed

+91
-142
lines changed

python/tvm/relay/backend/contrib/ethosu/codegen.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
)
3333
from tvm.relay.backend.contrib.ethosu.legalize import LegalizeEthosU
3434
from tvm.relay.backend.contrib.ethosu import tir_to_cs_translator, util, vela_api
35-
from tvm.relay.expr_functor import ExprMutator, ExprVisitor
35+
from tvm.relay.expr_functor import ExprMutator, ExprVisitor, Call
36+
from tvm.relay import expr as _expr
3637

3738
# pylint: disable=unused-import
3839
from tvm.relay.backend.contrib.ethosu.op import op_attrs

python/tvm/relay/op/contrib/ethosu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,7 @@ def partition_for_ethosu(
23412341
mod : IRModule
23422342
The partitioned IRModule with external global functions
23432343
"""
2344-
from tvm.relay.backend.contrib.ethosu import preprocess
2344+
from tvm.relay.backend.contrib.ethosu import preprocess, codegen
23452345

23462346
if params:
23472347
mod["main"] = bind_params_by_name(mod["main"], params)

python/tvm/relay/transform/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"""The Relay IR namespace containing transformations."""
1919
# transformation passes
2020
from .transform import *
21-
from .replicate_pads_with_multiple_consumers import *
2221
from .recast import recast
2322
from . import fake_quantization_to_integer, mixed_precision
2423
from .flexible_shape import FlexibleShapeDispatch

python/tvm/relay/transform/replicate_pads_with_multiple_consumers.py

Lines changed: 0 additions & 106 deletions
This file was deleted.

tests/python/contrib/test_ethosu/test_codegen.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,76 @@ def conv2d_double(x):
157157
infra.compare_tvm_with_tflite(conv2d_double, [ifm_shape], accel_type)
158158

159159

160+
@pytest.mark.parametrize("accel_type", ACCEL_TYPES)
161+
@pytest.mark.parametrize("ifm_shape", [(1, 55, 32, 3)])
162+
@pytest.mark.parametrize(
163+
"kernel_shape, activation_function",
164+
[((3, 3), "RELU"), ((1, 2), "NONE")],
165+
)
166+
@pytest.mark.parametrize("strides, dilation", [((3, 2), (1, 1))])
167+
@pytest.mark.parametrize("op_padding", ["SAME", "VALID"])
168+
@pytest.mark.parametrize("sep_padding", [(0, 0, 1, 1), (7, 5, 4, 5)])
169+
@pytest.mark.parametrize(
170+
"op_pairs", [("conv2d", "conv2d"), ("depthwise", "depthwise"), ("conv2d", "depthwise")]
171+
)
172+
def test_tflite_shared_pad(
173+
accel_type,
174+
ifm_shape,
175+
kernel_shape,
176+
activation_function,
177+
strides,
178+
dilation,
179+
op_padding,
180+
sep_padding,
181+
op_pairs,
182+
):
183+
np.random.seed(0)
184+
185+
@tf.function
186+
def tf_function(x):
187+
def make_depthwise_or_conv2d(pair_idx, x):
188+
# The input strides to the TensorFlow API needs to be of shape 1x4
189+
tf_strides = [1, strides[0], strides[1], 1]
190+
if op_pairs[pair_idx] == "depthwise":
191+
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 1]
192+
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
193+
op = tf.nn.depthwise_conv2d(
194+
x, weight, strides=tf_strides, padding=op_padding, dilations=dilation
195+
)
196+
else:
197+
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 3]
198+
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
199+
op = tf.nn.conv2d(
200+
x,
201+
weight,
202+
strides=tf_strides,
203+
padding=op_padding,
204+
dilations=dilation,
205+
)
206+
if activation_function == "RELU":
207+
op = tf.nn.relu(op)
208+
return op
209+
210+
x = tf.pad(
211+
x,
212+
[
213+
[0, 0],
214+
[sep_padding[0], sep_padding[2]],
215+
[sep_padding[1], sep_padding[3]],
216+
[0, 0],
217+
],
218+
"CONSTANT",
219+
)
220+
221+
x1 = make_depthwise_or_conv2d(0, x)
222+
x2 = make_depthwise_or_conv2d(1, x)
223+
224+
x3 = tf.math.add(x1, x2)
225+
return x3
226+
227+
infra.compare_tvm_with_tflite(tf_function, [ifm_shape], accel_type)
228+
229+
160230
@pytest.mark.parametrize("weight_min, weight_max", [(0.0, 1e-11), (-1e10, 1e10)])
161231
def test_out_of_range_scaling(weight_min, weight_max):
162232
np.random.seed(0)

tests/python/contrib/test_ethosu/test_legalize.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3671,6 +3671,22 @@ def create_tflite_graph():
36713671
class Model(tf.Module):
36723672
@tf.function
36733673
def tf_function(self, x):
3674+
def make_depthwise_or_conv2d(pair_idx):
3675+
if op_pairs[pair_idx] == "depthwise":
3676+
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 1]
3677+
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
3678+
return tf.nn.depthwise_conv2d(
3679+
x, weight, strides=tf_strides, padding=op_padding, dilations=dilation
3680+
)
3681+
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 3]
3682+
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
3683+
return tf.nn.conv2d(
3684+
x,
3685+
weight,
3686+
strides=tf_strides,
3687+
padding=op_padding,
3688+
dilations=dilation,
3689+
)
36743690

36753691
x = tf.pad(
36763692
x,
@@ -3686,39 +3702,8 @@ def tf_function(self, x):
36863702
# The input strides to the TensorFlow API needs to be of shape 1x4
36873703
tf_strides = [1, strides[0], strides[1], 1]
36883704

3689-
if op_pairs[0] == "depthwise":
3690-
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 1]
3691-
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
3692-
x1 = tf.nn.depthwise_conv2d(
3693-
x, weight, strides=tf_strides, padding=op_padding, dilations=dilation
3694-
)
3695-
else:
3696-
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 3]
3697-
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
3698-
x1 = tf.nn.conv2d(
3699-
x,
3700-
weight,
3701-
strides=tf_strides,
3702-
padding=op_padding,
3703-
dilations=dilation,
3704-
)
3705-
3706-
if op_pairs[1] == "depthwise":
3707-
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 1]
3708-
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
3709-
x2 = tf.nn.depthwise_conv2d(
3710-
x, weight, strides=tf_strides, padding=op_padding, dilations=dilation
3711-
)
3712-
else:
3713-
weight_shape = [kernel_shape[0], kernel_shape[1], ifm_shape[3], 3]
3714-
weight = tf.constant(np.random.uniform(size=weight_shape), dtype=tf.float32)
3715-
x2 = tf.nn.conv2d(
3716-
x,
3717-
weight,
3718-
strides=tf_strides,
3719-
padding=op_padding,
3720-
dilations=dilation,
3721-
)
3705+
x1 = make_depthwise_or_conv2d(0)
3706+
x2 = make_depthwise_or_conv2d(1)
37223707

37233708
x3 = tf.math.add(x1, x2)
37243709
return x3

0 commit comments

Comments
 (0)