From c8330811352f753e36f2ee7be4c7d0e6002f21e7 Mon Sep 17 00:00:00 2001 From: Jeremy Johnson Date: Thu, 18 Jan 2024 16:57:28 +0000 Subject: Main Compliance: FFT2D support Improve access to DOT_PRODUCT generator index and location for debugging. Enable multiple result files for compliance and improve output. Fix up precise and abs modes for FFT2D in ref model to produce correct results and bounds using abs weights. Signed-off-by: Jeremy Johnson Change-Id: Ide0c9f9f80397e5f1e07ca30a1036d6014b5784d --- .../src/generate/generate_dot_product.cc | 88 ++++++++++++++++++++++ .../src/generate/generate_dot_product.h | 3 +- .../src/generate/generate_dot_product_states.cc | 48 ++++++++++-- reference_model/src/generate/generate_utils.cc | 1 + reference_model/src/ops/tensor_ops.cc | 35 +++++++-- reference_model/src/verify/verify_dot_product.cc | 40 ++++++---- 6 files changed, 186 insertions(+), 29 deletions(-) (limited to 'reference_model/src') diff --git a/reference_model/src/generate/generate_dot_product.cc b/reference_model/src/generate/generate_dot_product.cc index fe829e3..046007e 100644 --- a/reference_model/src/generate/generate_dot_product.cc +++ b/reference_model/src/generate/generate_dot_product.cc @@ -736,6 +736,92 @@ bool generateTransposeConv2D(const TosaReference::GenerateConfig& cfg, return false; } } +//---------------------------------------------------------------------------// +// FFT2D // +//---------------------------------------------------------------------------// + +template +bool generateFFT2DReal(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(generator(k)); + } + return true; +} + +template +bool generateFFT2DImag(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]; + + // The index expression of ((1*N+n)*H+y)*W+x in the spec equates to + // using the values after those used for the Real tensor, but we need + // to iterate through all those values to get to the Imaginary data + for (int64_t n = 0; n < 2; ++n) + { + 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(generator(k)); + } + } + return true; +} + +bool generateFFT2D(const TosaReference::GenerateConfig& cfg, + TosaReference::IDotProductGenerator& generator, + void* data, + size_t size) +{ + if (cfg.shape.size() != 3) + { + WARNING("[Generator][DP][FFT2D] Tensor shape expected 3 dimensions."); + return false; + } + + switch (cfg.dataType) + { + case DType::DType_FP32: { + float* outData = reinterpret_cast(data); + switch (cfg.inputPos) + { + case 0: + return generateFFT2DReal(cfg, generator, outData, size); + case 1: + return generateFFT2DImag(cfg, generator, outData, size); + default: + WARNING("[Generator][DP][FFT2D] Invalid input tensor slot position to operator."); + return false; + } + break; + } + default: + WARNING("[Generator][DP][FFT2D] Only supports FP32."); + return false; + } + + return true; +} } // namespace namespace TosaReference @@ -772,6 +858,8 @@ bool generateDotProduct(const GenerateConfig& cfg, void* data, size_t size) return generateDepthwiseConv2D(cfg, *generator, data, size); case tosa::Op_TRANSPOSE_CONV2D: return generateTransposeConv2D(cfg, *generator, data, size); + case tosa::Op_FFT2D: + return generateFFT2D(cfg, *generator, data, size); default: WARNING("[Generator][DP] Unsupported operator."); return false; diff --git a/reference_model/src/generate/generate_dot_product.h b/reference_model/src/generate/generate_dot_product.h index cd9d4ba..bf1b1ff 100644 --- a/reference_model/src/generate/generate_dot_product.h +++ b/reference_model/src/generate/generate_dot_product.h @@ -1,4 +1,4 @@ -// Copyright (c) 2023, ARM Limited. +// Copyright (c) 2023-2024, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ class IDotProductGenerator public: virtual float operator()(uint32_t k) = 0; virtual ~IDotProductGenerator() = default; + virtual uint32_t nextIndex() = 0; }; /// \brief Dot-product stage generator selector diff --git a/reference_model/src/generate/generate_dot_product_states.cc b/reference_model/src/generate/generate_dot_product_states.cc index 9ce32ff..b78be71 100644 --- a/reference_model/src/generate/generate_dot_product_states.cc +++ b/reference_model/src/generate/generate_dot_product_states.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2023, ARM Limited. +// Copyright (c) 2023-2024, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -60,7 +60,7 @@ public: return pseudo; } - uint32_t index() + uint32_t nextIndex() { return _index; } @@ -101,6 +101,11 @@ public: else return 0.f; } + uint32_t nextIndex() + { + ASSERT_MSG(_set_data0.nextIndex() == _set_data1.nextIndex(), "Internal index inconsistency in GeneratorS0") + return _set_data0.nextIndex(); + } private: uint32_t _p; @@ -129,6 +134,10 @@ public: else return (_B * _B / (_KS + 1)) * v; } + uint32_t nextIndex() + { + return _set_data.nextIndex(); + } private: uint32_t _p; @@ -158,6 +167,10 @@ public: else return 0.f; } + uint32_t nextIndex() + { + return _set_data.nextIndex(); + } private: uint32_t _p; @@ -186,6 +199,10 @@ public: else return 0.f; } + uint32_t nextIndex() + { + return _set_data.nextIndex(); + } private: uint32_t _p; @@ -229,6 +246,11 @@ public: else return 0.f; } + uint32_t nextIndex() + { + ASSERT_MSG(_set_data0.nextIndex() == _set_data1.nextIndex(), "Internal index inconsistency in GeneratorS4") + return _set_data0.nextIndex(); + } private: uint32_t _p; @@ -258,6 +280,10 @@ public: else return 0.f; } + uint32_t nextIndex() + { + return _set_data.nextIndex(); + } private: uint32_t _p; @@ -307,21 +333,27 @@ std::unique_ptr pickDotProductGenerator(const GenerateConf float B = getBoundParameter(cfg.dataType, dpinfo.accType); if (B > 0.f) { + auto param = cfg.inputPos; + if (cfg.opType == Op_FFT2D) + { + // We only use param of zero for FFT2D tensors + param = 0; + } // Create the generator switch (dpinfo.s) { case 0: - return std::make_unique(cfg.inputPos); + return std::make_unique(param); case 1: - return std::make_unique(cfg.inputPos, dpinfo.ks, B); + return std::make_unique(param, dpinfo.ks, B); case 2: - return std::make_unique(cfg.inputPos, dpinfo.ks); + return std::make_unique(param, dpinfo.ks); case 3: - return std::make_unique(cfg.inputPos); + return std::make_unique(param); case 4: - return std::make_unique(cfg.inputPos, dpinfo.ks, B); + return std::make_unique(param, dpinfo.ks, B); case 5: - return std::make_unique(cfg.inputPos, dpinfo.ks, B); + return std::make_unique(param, dpinfo.ks, B); default: WARNING("[Generator][DP] Unsupported dot product test series for generator."); return nullptr; diff --git a/reference_model/src/generate/generate_utils.cc b/reference_model/src/generate/generate_utils.cc index a8b472a..2e40b04 100644 --- a/reference_model/src/generate/generate_utils.cc +++ b/reference_model/src/generate/generate_utils.cc @@ -54,6 +54,7 @@ NLOHMANN_JSON_SERIALIZE_ENUM(Op, { Op::Op_ERF, "ERF" }, { Op::Op_EXP, "EXP" }, { Op::Op_FLOOR, "FLOOR" }, + { Op::Op_FFT2D, "FFT2D" }, { Op::Op_FULLY_CONNECTED, "FULLY_CONNECTED" }, { Op::Op_GATHER, "GATHER" }, { Op::Op_GREATER, "GREATER" }, diff --git a/reference_model/src/ops/tensor_ops.cc b/reference_model/src/ops/tensor_ops.cc index b9e2fbe..8d8dac7 100644 --- a/reference_model/src/ops/tensor_ops.cc +++ b/reference_model/src/ops/tensor_ops.cc @@ -1,5 +1,5 @@ -// Copyright (c) 2020-2023, ARM Limited. +// Copyright (c) 2020-2024, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -1684,7 +1684,8 @@ int OpFFT2d::eval() in_real_batch, in_real_height, in_real_width, in_imag_batch, in_imag_height, in_imag_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, sign_val = 1.0; + OutEigenType sum_real, sum_imag, sign_val = 1.0; + OutEigenType a, a_cos, a_sin, v_ir; if (attribute->inverse()) { @@ -1715,11 +1716,33 @@ int OpFFT2d::eval() { OutEigenType val_real = in_real_val(n, iy, ix); OutEigenType val_imag = in_imag_val(n, iy, ix); - // Use explicit cast to ensure intermmediate calculations are completed using OutEigenType + // 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(iy) * static_cast(oy)) % in_real_height; + int32_t ax = (static_cast(ix) * static_cast(ox)) % in_real_width; + + // Use explicit cast to ensure intermediate calculations are completed using OutEigenType a = sign_val * 2 * M_PI * - ((iy * (OutEigenType)oy) / in_real_height + (ix * (OutEigenType)ox) / in_real_width); - sum_real += val_real * cos(a) + val_imag * sin(a); - sum_imag += -val_real * sin(a) + val_imag * cos(a); + ((OutEigenType)ay / in_real_height + (OutEigenType)ax / in_real_width); + // Calculate weight values + 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_real; + } + else + { + // Normal op - Use negative real value for imaginary calc + v_ir = -val_real; + } + sum_real += val_real * a_cos + val_imag * a_sin; + sum_imag += v_ir * a_sin + val_imag * a_cos; } } this->out_real->getTensor()(n, oy, ox) = sum_real; diff --git a/reference_model/src/verify/verify_dot_product.cc b/reference_model/src/verify/verify_dot_product.cc index a036cba..ea50573 100644 --- a/reference_model/src/verify/verify_dot_product.cc +++ b/reference_model/src/verify/verify_dot_product.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2023, ARM Limited. +// Copyright (c) 2023-2024, ARM Limited. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ #include "half.hpp" #include "verifiers.h" +#include #include #include #include @@ -43,7 +44,8 @@ std::optional validateElement(size_t index, double ref, double bnd, AccT is_valid = (ref == 0.0) && (imp == 0.0); if (!is_valid) { - WARNING("[Verifier][DP] index %d - bound is zero, but ref (%g) or imp (%f) is not.", index, ref, imp); + WARNING("[Verifier][DP] index %d: bound is zero, but ref (%.*g) or imp (%.*g) is not.", index, DBL_DIG, ref, + FLT_DIG, imp); } err = 0.0; } @@ -57,7 +59,7 @@ std::optional validateElement(size_t index, double ref, double bnd, AccT is_valid = std::abs(err) <= KS; if (!is_valid) { - WARNING("[Verifier][DP] index %d - out_err (%g) is not within KS (%d).", index, err, KS); + WARNING("[Verifier][DP] index %d: out_err (abs(%.*g)) is not within KS (%d).", index, DBL_DIG, err, KS); } } @@ -66,8 +68,15 @@ std::optional validateElement(size_t index, double ref, double bnd, AccT // Generic data validation function template -bool validateData(const double* ref, const double* bnd, const AccType* imp, size_t T, const DotProductVerifyInfo& cfg) +bool validateData(const double* ref, + const double* bnd, + const AccType* imp, + const std::vector& shape, + const DotProductVerifyInfo& cfg) { + const size_t T = static_cast(numElements(shape)); + TOSA_REF_REQUIRE(T > 0, "[DP] Invalid shape for reference tensor"); + const int32_t S = cfg.s; // NOTE: KS in the compliance config MUST have already been updated to (KS + 1) if the bias // tensor is non-zero @@ -79,7 +88,12 @@ bool validateData(const double* ref, const double* bnd, const AccType* imp, size for (size_t i = 0; i < T; ++i) { auto out_err = validateElement(i, ref[i], bnd[i], imp[i], KS); - TOSA_REF_REQUIRE(out_err, "[DP] Data required to be zero or error within range"); + if (!out_err) + { + auto pos = indexToPosition(i, shape); + TOSA_REF_REQUIRE(out_err, "[DP] Location %s: Data required to be zero or error within range", + positionToString(pos).c_str()); + } out_err_sum += out_err.value(); out_err_sumsq += out_err.value() * out_err.value(); } @@ -88,13 +102,13 @@ bool validateData(const double* ref, const double* bnd, const AccType* imp, size { const double max_bias = 2 * sqrt(KS * T); // Check error bias magnitude for data sets S which are not positive biased - TOSA_REF_REQUIRE(std::abs(out_err_sum) <= max_bias, "[DP] Bias magnitude (abs(%g)) is out of range (%g)", - out_err_sum, max_bias); + TOSA_REF_REQUIRE(std::abs(out_err_sum) <= max_bias, "[DP] Bias magnitude (abs(%.*g)) is out of range (%.*g)", + DBL_DIG, out_err_sum, DBL_DIG, max_bias); } // Check error variance magnitude const double max_error = 0.4 * KS * T; - TOSA_REF_REQUIRE(out_err_sumsq <= max_error, "[DP] Error variance magnitude (%g) is out of range (%g)", - out_err_sumsq, max_error); + TOSA_REF_REQUIRE(out_err_sumsq <= max_error, "[DP] Error variance magnitude (%.*g) is out of range (%.*g)", DBL_DIG, + out_err_sumsq, DBL_DIG, max_error); return true; } } // namespace @@ -106,9 +120,7 @@ bool verifyDotProduct(const CTensor* ref, const CTensor* refBnd, const CTensor* TOSA_REF_REQUIRE(refBnd != nullptr, "[DP] Reference bounds tensor is missing"); TOSA_REF_REQUIRE(imp != nullptr, "[DP] Implementation tensor is missing"); - // Get number of dot-product elements - const int64_t T = numElements(std::vector(ref->shape, ref->shape + ref->num_dims)); - TOSA_REF_REQUIRE(T > 0, "[DP] Invalid shape for reference tensor"); + const std::vector refShape(ref->shape, ref->shape + ref->num_dims); const double* refData = reinterpret_cast(ref->data); const double* refBndData = reinterpret_cast(refBnd->data); @@ -119,13 +131,13 @@ bool verifyDotProduct(const CTensor* ref, const CTensor* refBnd, const CTensor* case tosa_datatype_fp32_t: { const float* impData = reinterpret_cast(imp->data); TOSA_REF_REQUIRE(impData != nullptr, "[DP] Missing data for implementation"); - return validateData(refData, refBndData, impData, static_cast(T), dpInfo); + return validateData(refData, refBndData, impData, refShape, dpInfo); break; } case tosa_datatype_fp16_t: { const half_float::half* impData = reinterpret_cast(imp->data); TOSA_REF_REQUIRE(impData != nullptr, "[DP] Missing data for implementation"); - return validateData(refData, refBndData, impData, static_cast(T), dpInfo); + return validateData(refData, refBndData, impData, refShape, dpInfo); break; } default: { -- cgit v1.2.1