Emit tosa::erf during lowering gelu op #4151
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The PR adds support to use tosa::erf during lowering of aten.gelu op
Currently, aten.gelu uses its own custom implementation of erf which was designed before tosa support for tosa::erf : llvm/llvm-project@1fef1f9
aten.erf is already mapped to tosa::ErfOp https://github.com/vinitdeodhar/torch-mlir/blob/c785435a049a694a1814a7304ed0c34abe8b2580/lib/Conversion/TorchToTosa/TorchToTosa.cpp#L9176
The two implementations (Existing aten.gelu erf and tosa::erf) produce numerically different results.
tosa::erf is more precise approximation of the erf function. tosa::erf approximation is based on the following stackoverflow post:
https://stackoverflow.com/questions/35966695/vectorizable-implementation-of-complementary-error-function-erfcf
The stackoverflow post is in turn based on:
M. M. Shepherd and J. G. Laframboise, "Chebyshev Approximation of
(1+2x)exp(x^2)erfc x in 0 <= x < INF", Mathematics of Computation, Vol. 36,
No. 153, January 1981, pp. 249-253.
Maximum error: 2.65 ulps