manupa-arm commented on a change in pull request #9576:
URL: https://github.com/apache/tvm/pull/9576#discussion_r760836351
##########
File path: src/relay/op/contrib/ethosu/binary_elementwise.cc
##########
@@ -128,6 +128,21 @@ struct EthosuBinaryElementwiseAttrs : public
tvm::AttrsNode<EthosuBinaryElementw
TVM_REGISTER_NODE_TYPE(EthosuBinaryElementwiseAttrs);
+bool IsScalarTensor(const Array<PrimExpr>& ifm_shape, const DataType&
ifm_dtype) {
+ if (ifm_dtype != DataType::UInt(8)) {
Review comment:
This does not qualify as not being scalar I suppose. May be worth
extracting that out for clarity ?
##########
File path: src/relay/op/contrib/ethosu/binary_elementwise.cc
##########
@@ -166,11 +181,11 @@ bool EthosuBinaryElementwiseRel(const Array<Type>& types,
int num_inputs, const
if (operator_type == "ADD" || operator_type == "SUB" || operator_type ==
"MUL") {
if (ifm_dtype != DataType::UInt(8) && ifm_dtype != DataType::Int(8) &&
- ifm_dtype != DataType::Int(32)) {
+ ifm_dtype != DataType::Int(16) && ifm_dtype != DataType::Int(32)) {
Review comment:
nit : It might be worth putting these into a disallowed list and check
the presense in the list ?
##########
File path: src/relay/op/contrib/ethosu/depthwise.cc
##########
@@ -156,6 +172,15 @@ bool EthosuDepthwiseConv2DRel(const Array<Type>& types,
int num_inputs, const At
return false;
}
+ if (ofm_dtype != DataType::UInt(8) && ofm_dtype != DataType::Int(8) &&
Review comment:
nit : again a disallowed a list might helps things to be more clearer
##########
File path: src/relay/op/contrib/ethosu/depthwise.cc
##########
@@ -132,6 +136,18 @@ bool EthosuDepthwiseConv2DRel(const Array<Type>& types,
int num_inputs, const At
const auto* param = attrs.as<EthosuDepthwiseConv2DAttrs>();
ICHECK(param != nullptr) << "EthosuDepthwiseConv2DAttrs cannot be nullptr.";
+ DataType ofm_dtype;
+
+ if (param->ofm_dtype == "int8") {
Review comment:
It might be clearer if we use a dictionary here
##########
File path: python/tvm/relay/backend/contrib/ethosu/legalize.py
##########
@@ -961,6 +962,170 @@ def __call__(self, *args, **kwargs):
pass
+class MeanRewriter(DFPatternCallback):
+ """Convert ethosu.mean composite functions to to an equivalent
legalization:
+ - Case 1 (axis == [1, 2] and keepsdims == True):
+ ethosu_depthwise_conv2d + ethosu_binary_elementwise
+ - Case 2 (ifm qparams == ofm qparams): ethosu_pooling
+ - Case 3 (else): ethosu_depthwise_conv2d
+ """
+
+ def __init__(self):
+ super().__init__(require_type=True)
+ self.pattern = (
+ wildcard().has_attr({"Composite":
ethosu_patterns.MeanParams.composite_name})
+ )(wildcard())
+
+ def callback(
+ self, pre: tvm.relay.Expr, post: tvm.relay.Expr, node_map:
tvm.ir.container.Map
+ ) -> tvm.relay.Expr:
+ params = ethosu_patterns.MeanParams(post.op.body)
+ params.ifm.tensor = post.args[0]
+
+ ifm_shape = params.ifm.shape
+ ofm_shape = params.ofm.shape
+ lut = relay.const([], "int8")
+ axis = params.axis
+ reduced_op = params.ifm.tensor
+
+ # Enforce 4d input
+ if len(ifm_shape) < 4:
+ axis = [x + 1 for x in axis]
+ if len(ifm_shape) == 3:
+ ifm_shape = [1, params.height, params.width, ifm_shape[2]]
+ else:
+ ifm_shape = [1, params.height, params.width, 1]
+ reduced_op = relay.reshape(reduced_op, ifm_shape)
+
+ filter_height = ifm_shape[1] if 1 in axis else 1
+ filter_width = ifm_shape[2] if 2 in axis else 1
+ in_channels = out_channels = ifm_shape[-1]
+
+ # If the height is greater than max kernel height, reshape the input
+ # from [filter_height, filter_width] to [1,
(filter_height*filter_width)]
+ # only in the case the axis is [1, 2].
+ if axis == [1, 2] and filter_height > 64:
+ ifm_shape = (ifm_shape[0], 1, filter_height * filter_width,
in_channels)
+ filter_width = filter_height * filter_width
+ filter_height = 1
+ reduced_op = relay.reshape(reduced_op, ifm_shape)
+
+ if axis == [1, 2] and params.keepdims:
+ weight_scale = 1
+ weight_values = np.ones([out_channels, filter_height,
filter_width, in_channels])
+ scale_bias = vela_api.pack_biases(
+ biases=np.zeros(ifm_shape[-1]),
+ ifm_scale=params.ifm.q_params.scale_f32,
+ ifm_dtype=np.dtype(params.ifm.dtype),
+ weight_scales=np.array([weight_scale], dtype=np.float),
+ ofm_scale=params.ofm.q_params.scale_f32,
+ is_activation_tanh_or_sigmoid=False,
+ )
+
+ reduced_op = ethosu_ops.ethosu_depthwise_conv2d(
+ ifm=reduced_op,
+ weight=relay.const(weight_values, params.ifm.dtype),
+ scale_bias=relay.const(scale_bias, "uint8"),
+ lut=lut,
+ ifm_scale=float(params.ifm.q_params.scale_f32),
+ ifm_zero_point=int(params.ifm.q_params.zero_point),
+ weight_zero_point=0,
+ ofm_scale=float(params.ofm.q_params.scale_f32),
+ ofm_zero_point=int(params.ofm.q_params.zero_point),
+ kernel_shape=(filter_height, filter_width),
+ ofm_channels=out_channels,
+ ofm_dtype="int16",
+ )
+
+ n = int(filter_height * filter_width)
+ eps = 1 / (256 * (n + 1)) if n % 2 == 0 else 0
Review comment:
It might be worth using variables for numbers to say what they mean.
--
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]