aboutsummaryrefslogtreecommitdiff
path: root/reference_model/src
diff options
context:
space:
mode:
Diffstat (limited to 'reference_model/src')
-rw-r--r--reference_model/src/generate/generate_dot_product.cc59
-rw-r--r--reference_model/src/generate/generate_utils.cc1
-rw-r--r--reference_model/src/ops/tensor_ops.cc45
3 files changed, 100 insertions, 5 deletions
diff --git a/reference_model/src/generate/generate_dot_product.cc b/reference_model/src/generate/generate_dot_product.cc
index 7337969..117d49d 100644
--- a/reference_model/src/generate/generate_dot_product.cc
+++ b/reference_model/src/generate/generate_dot_product.cc
@@ -963,6 +963,63 @@ bool generateFFT2D(const TosaReference::GenerateConfig& cfg,
return true;
}
+//---------------------------------------------------------------------------//
+// RFFT2D //
+//---------------------------------------------------------------------------//
+
+template <typename DataType>
+bool generateRFFT2DReal(const TosaReference::GenerateConfig& cfg,
+ TosaReference::IDotProductGenerator& generator,
+ DataType* data,
+ size_t size)
+{
+ const int64_t T = TosaReference::numElementsFromShape(cfg.shape);
+ const uint32_t H = cfg.shape[1];
+ const uint32_t W = cfg.shape[2];
+
+ for (int64_t t = 0; t < T; ++t)
+ {
+ uint32_t x = t % W;
+ uint32_t y = (t / W) % H;
+ uint32_t k = y * W + x;
+
+ data[t] = static_cast<DataType>(generator(k));
+ }
+ return true;
+}
+
+bool generateRFFT2D(const TosaReference::GenerateConfig& cfg,
+ TosaReference::IDotProductGenerator& generator,
+ void* data,
+ size_t size)
+{
+ if (cfg.shape.size() != 3)
+ {
+ WARNING("[Generator][DP][RFFT2D] Tensor shape expected 3 dimensions.");
+ return false;
+ }
+
+ switch (cfg.dataType)
+ {
+ case DType::DType_FP32: {
+ float* outData = reinterpret_cast<float*>(data);
+ switch (cfg.inputPos)
+ {
+ case 0:
+ return generateRFFT2DReal(cfg, generator, outData, size);
+ default:
+ WARNING("[Generator][DP][RFFT2D] Invalid input tensor slot position to operator.");
+ return false;
+ }
+ break;
+ }
+ default:
+ WARNING("[Generator][DP][RFFT2D] Only supports FP32.");
+ return false;
+ }
+
+ return true;
+}
} // namespace
namespace TosaReference
@@ -1003,6 +1060,8 @@ bool generateDotProduct(const GenerateConfig& cfg, void* data, size_t size)
return generateConv3D(cfg, *generator, data, size);
case tosa::Op_FFT2D:
return generateFFT2D(cfg, *generator, data, size);
+ case tosa::Op_RFFT2D:
+ return generateRFFT2D(cfg, *generator, data, size);
default:
WARNING("[Generator][DP] Unsupported operator.");
return false;
diff --git a/reference_model/src/generate/generate_utils.cc b/reference_model/src/generate/generate_utils.cc
index c495fb6..cf5308b 100644
--- a/reference_model/src/generate/generate_utils.cc
+++ b/reference_model/src/generate/generate_utils.cc
@@ -78,6 +78,7 @@ NLOHMANN_JSON_SERIALIZE_ENUM(Op,
{ Op::Op_REDUCE_PRODUCT, "REDUCE_PRODUCT" },
{ Op::Op_REDUCE_SUM, "REDUCE_SUM" },
{ Op::Op_REVERSE, "REVERSE" },
+ { Op::Op_RFFT2D, "RFFT2D" },
{ Op::Op_SCATTER, "SCATTER" },
{ Op::Op_SELECT, "SELECT" },
{ Op::Op_SIGMOID, "SIGMOID" },
diff --git a/reference_model/src/ops/tensor_ops.cc b/reference_model/src/ops/tensor_ops.cc
index 8d8dac7..dd66f79 100644
--- a/reference_model/src/ops/tensor_ops.cc
+++ b/reference_model/src/ops/tensor_ops.cc
@@ -1820,6 +1820,9 @@ int OpRFFT2d<Dtype>::eval()
int32_t out_imag_height = out_imag->getShape()[1];
int32_t out_imag_width = out_imag->getShape()[2];
+ int32_t half_in_height = in_height / 2;
+ int32_t half_in_width = in_width / 2;
+
// Check Tosa Level
auto tosa_level = g_func_config.tosa_level;
LEVEL_CHECK(in_height <= tosa_level.MAX_KERNEL, "H should be smaller than or equal to MAX_KERNEL");
@@ -1831,7 +1834,8 @@ int OpRFFT2d<Dtype>::eval()
in_batch, in_height, in_width, out_real_batch, out_real_height, out_real_width, out_imag_batch,
out_imag_height, out_imag_width);
- OutEigenType sum_real, sum_imag, a;
+ OutEigenType sum_real, sum_imag;
+ OutEigenType a, a_cos, a_sin, v_ir;
TIn in_val = this->in->getTensor();
@@ -1853,10 +1857,41 @@ int OpRFFT2d<Dtype>::eval()
{
for (int ix = 0; ix < in_width; ix++)
{
- // Use explicit cast to ensure intermmediate calculations are completed using OutEigenType
- a = 2 * M_PI * ((iy * (OutEigenType)oy) / in_height + (ix * (OutEigenType)ox) / in_width);
- sum_real += in_val(n, iy, ix) * cos(a);
- sum_imag += -in_val(n, iy, ix) * sin(a);
+ OutEigenType val = in_val(n, iy, ix);
+ // Perform the periodic calculation in integer maths to keep
+ // the accuracy of the co-efficients similar for FP32 normal
+ // and FP64 precise mode
+ int32_t ay = (static_cast<int64_t>(iy) * static_cast<int64_t>(oy)) % in_height;
+ int32_t ax = (static_cast<int64_t>(ix) * static_cast<int64_t>(ox)) % in_width;
+
+ // Use explicit cast to ensure intermediate calculations are completed using OutEigenType
+ a = 2 * M_PI * ((OutEigenType)ay / in_height + (OutEigenType)ax / in_width);
+
+ // Calculate weight values (co-efficients)
+ a_cos = cos(a);
+ a_sin = sin(a);
+
+ if (g_func_config.abs_mode)
+ {
+ // Bounded op - Use abs weight values
+ a_cos = std::abs(a_cos);
+ a_sin = std::abs(a_sin);
+ // Bounded op - Use abs real value for imaginary calc
+ v_ir = val;
+ }
+ else
+ {
+ // Normal op - Use negative real value for imaginary calc
+ v_ir = -val;
+ }
+ sum_real += val * a_cos;
+ // Imaginary values with locations (0,0), (0,W/2), (H/2,0) and (H/2,W/2) are zero.
+ // But due to sin(M_PI) not returning 0 because of M_PI being approximate, only
+ // add to the imaginary sum when not processing these locations.
+ if ((ay % (half_in_height)) + (ax % (half_in_width)) > 0)
+ {
+ sum_imag += v_ir * a_sin;
+ }
}
}
this->out_real->getTensor()(n, oy, ox) = sum_real;