anijain2305 commented on a change in pull request #4790: Fast exponent
URL: https://github.com/apache/incubator-tvm/pull/4790#discussion_r377958380
##########
File path: topi/include/topi/elemwise.h
##########
@@ -360,5 +360,85 @@ inline Tensor full_like(const Tensor& x,
}, name, tag);
}
+/*!
+ * \brief Fast exponential function implementation
+ *
+ * \param _x The input tensor
+ * \param name The name of the operation
+ * \param tag The tag to mark the operation
+ *
+ * \return A Tensor whose op member is exponent operation
+ *
+ * Function computes:
+ * log2(e^x) = x * log2(e) * log2(2) =>
+ * log2(e^x) = log2(2^(x*log2(e))) =>
+ * e^x = 2^(x*log2(e))
+ * Splitting power x*log2(e) into integer and fractional parts:
+ * e^(n+f) = e^n * e^f
+ * n = floor(x*log2(e) + 1/2)
+ * f = x - n * ln(2)
+ * exp(x) = 2^n * exp(y)
+ * Approximation for fractional part:
+ * y = exp(f) = 1 + 2 * P(x**2)/(Q(x**2) - P(x**2))
+ */
+inline Tensor fast_exp_float32(const Tensor& _x,
+ std::string name,
+ std::string tag) {
+ auto x_hi = make_const(DataType::Float(32), 88.3762626647950f);
+ auto x_lo = make_const(DataType::Float(32), -88.3762626647949f);
+ auto log2e = make_const(DataType::Float(32), 1.44269504088896341f);
+ auto ln2 = make_const(DataType::Float(32), 0.6931471805599453f);
+ PrimExpr p[6] = {make_const(DataType::Float(32), 1.9875691500E-4f),
+ make_const(DataType::Float(32), 1.3981999507E-3f),
+ make_const(DataType::Float(32), 8.3334519073E-3f),
+ make_const(DataType::Float(32), 4.1665795894E-2f),
+ make_const(DataType::Float(32), 1.6666665459E-1f),
+ make_const(DataType::Float(32), 5.0000001201E-1f)};
+ auto one = make_const(DataType::Float(32), 1.0f);
+ auto one_half = make_const(DataType::Float(32), 0.5f);
+ auto b = make_const(DataType::Float(32), 127.0f);
+
+ return compute(_x->shape,
Review comment:
A high-level design question - Can we do this at Relay level? Relay can then
fuse things accordingly.
@masahi
----------------------------------------------------------------
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