jackwish commented on a change in pull request #4351: [QNN] Lowering for
Depthwise Convolution.
URL: https://github.com/apache/incubator-tvm/pull/4351#discussion_r347757555
##########
File path: src/relay/qnn/op/convolution.cc
##########
@@ -166,6 +181,130 @@ Expr Conv2DPadInput(const Expr& data, const
QnnConv2DAttrs* param) {
return padded_data;
}
+/*
+ * \brief Calculates the second term in the qnn.conv2d depthwise lowering
sequence.
+ * \param padded_data The padded data expr.
+ * \param param The qnn conv2d attributes.
+ * \param kernel_h The height of kernel.
+ * \param kernel_w The width of kernel.
+ * \param channel_multiplier The channel/depth multiplier.
+ * \return The sequence of Relay operators for term2.
+ * \note The term2 looks like this
+ *
+ * Sigma(r, s) zp_w * Qa(n, oc/cm, oh + r, ow + s)
+ *
+ * Second term is not directly representable by one Relay operator.
+ * However, deeper analysis shows that we can reduce r,s using
avg_pool2d,
+ * followed by repeat on the C axis by cm times.
+ */
+Expr DepthwiseConv2DSecondTerm(const Expr& padded_data, const QnnConv2DAttrs*
param, int kernel_h,
+ int kernel_w, int channel_multiplier) {
+ // Constant Expr for the kernel zero point.
+ auto zp_kernel = MakeConstantScalar(Int(32), param->kernel_zero_point);
+
+ auto casted_t2 = Cast(padded_data, Int(32));
+
+ // We can reduce the H and W axis by using avg_pool2d. However, avg_pool2d
averages the sum.
+ // Since, this is integer division (floor), we can first multiply the data
by the pool_size and
+ // then perform avg_pool2d. Reversing this causes inaccuracy due to floor
division.
+ auto scaled_hw_t2 = Multiply(casted_t2, MakeConstantScalar(Int(32), kernel_h
* kernel_w));
Review comment:
If this is unneeded (like L215) when `kernel_h * kernel_w == 1`?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services