Skip to content

[TOSA] FFT2D/RFFT2D accuracy increased #88510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 42 additions & 24 deletions mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "mlir/Conversion/TosaToLinalg/TosaToLinalg.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Arith/Utils/Utils.h"
#include "mlir/Dialect/Index/IR/IndexOps.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
Expand Down Expand Up @@ -2364,16 +2365,25 @@ struct RFFT2dConverter final : public OpRewritePattern<RFFT2dOp> {
Value sumImag = args[2];

// Indices for angle computation
auto oy = createLinalgIndex(builder, loc, elementType, 1);
auto ox = createLinalgIndex(builder, loc, elementType, 2);
auto iy = createLinalgIndex(builder, loc, elementType, 3);
auto ix = createLinalgIndex(builder, loc, elementType, 4);

// angle = 2 * pi() * ((iy * oy) / H + (ix * ox) / W)
auto iyXoy = builder.create<arith::MulFOp>(loc, iy, oy);
auto ixXox = builder.create<arith::MulFOp>(loc, ix, ox);
auto yComponent = builder.create<arith::DivFOp>(loc, iyXoy, constH);
auto xComponent = builder.create<arith::DivFOp>(loc, ixXox, constW);
Value oy = builder.create<linalg::IndexOp>(loc, 1);
Value ox = builder.create<linalg::IndexOp>(loc, 2);
Value iy = builder.create<linalg::IndexOp>(loc, 3);
Value ix = builder.create<linalg::IndexOp>(loc, 4);

// Calculating angle without integer parts of components as sin/cos are
// periodic: angle = 2 * pi() * ( ( (iy * oy) % H) / H + ( (ix * ox) % W )
// / W);
auto iyXoy = builder.create<index::MulOp>(loc, iy, oy);
auto ixXox = builder.create<index::MulOp>(loc, ix, ox);

auto iyRem = builder.create<index::RemUOp>(loc, iyXoy, dimH);
auto ixRem = builder.create<index::RemUOp>(loc, ixXox, dimW);

auto iyRemFloat = castIndexToFloat(builder, loc, elementType, iyRem);
auto ixRemFloat = castIndexToFloat(builder, loc, elementType, ixRem);

auto yComponent = builder.create<arith::DivFOp>(loc, iyRemFloat, constH);
auto xComponent = builder.create<arith::DivFOp>(loc, ixRemFloat, constW);
auto sumXY = builder.create<arith::AddFOp>(loc, yComponent, xComponent);
auto angle = builder.create<arith::MulFOp>(loc, twoPi, sumXY);

Expand Down Expand Up @@ -2478,22 +2488,30 @@ struct FFT2dConverter final : OpRewritePattern<FFT2dOp> {
Value sumImag = args[3];

// Indices for angle computation
Value oy =
RFFT2dConverter::createLinalgIndex(builder, loc, real_el_ty, 1);
Value ox =
RFFT2dConverter::createLinalgIndex(builder, loc, real_el_ty, 2);
Value iy =
RFFT2dConverter::createLinalgIndex(builder, loc, real_el_ty, 3);
Value ix =
RFFT2dConverter::createLinalgIndex(builder, loc, real_el_ty, 4);

// float_t angle = sign_val * 2 * pi() * ((iy * oy) / H + (ix * ox) / W);
auto iyXoy = builder.create<arith::MulFOp>(loc, iy, oy);
auto ixXox = builder.create<arith::MulFOp>(loc, ix, ox);
auto yComponent = builder.create<arith::DivFOp>(loc, iyXoy, constH);
auto xComponent = builder.create<arith::DivFOp>(loc, ixXox, constW);
Value oy = builder.create<linalg::IndexOp>(loc, 1);
Value ox = builder.create<linalg::IndexOp>(loc, 2);
Value iy = builder.create<linalg::IndexOp>(loc, 3);
Value ix = builder.create<linalg::IndexOp>(loc, 4);

// float_t angle = sign_val * 2 * pi() * ( ( (iy * oy) % H) / H + ( (ix *
// ox) % W ) / W);
auto iyXoy = builder.create<index::MulOp>(loc, iy, oy);
auto ixXox = builder.create<index::MulOp>(loc, ix, ox);

auto iyRem = builder.create<index::RemUOp>(loc, iyXoy, dimH);
auto ixRem = builder.create<index::RemUOp>(loc, ixXox, dimW);

auto iyRemFloat =
RFFT2dConverter::castIndexToFloat(builder, loc, real_el_ty, iyRem);
auto ixRemFloat =
RFFT2dConverter::castIndexToFloat(builder, loc, real_el_ty, ixRem);

auto yComponent = builder.create<arith::DivFOp>(loc, iyRemFloat, constH);
auto xComponent = builder.create<arith::DivFOp>(loc, ixRemFloat, constW);

auto sumXY = builder.create<arith::AddFOp>(loc, yComponent, xComponent);
auto angle = builder.create<arith::MulFOp>(loc, twoPi, sumXY);

if (inverse.getValue()) {
angle = builder.create<arith::MulFOp>(
loc, angle,
Expand Down
3 changes: 2 additions & 1 deletion mlir/lib/Conversion/TosaToLinalg/TosaToLinalgPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Index/IR/IndexDialect.h"
#include "mlir/Dialect/Linalg/IR/Linalg.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
Expand All @@ -40,7 +41,7 @@ struct TosaToLinalg : public impl::TosaToLinalgBase<TosaToLinalg> {
void getDependentDialects(DialectRegistry &registry) const override {
registry
.insert<arith::ArithDialect, linalg::LinalgDialect, math::MathDialect,
tensor::TensorDialect, scf::SCFDialect>();
index::IndexDialect, tensor::TensorDialect, scf::SCFDialect>();
}

void runOnOperation() override {
Expand Down
Loading