added Rcp op & modified test_utils

This commit is contained in:
Feiyue Chen 2022-09-20 18:23:48 +08:00 committed by Sven
parent 1b07b022e2
commit 8b8d09aea3
5 changed files with 40 additions and 1 deletions

View File

@ -90,6 +90,9 @@ namespace ops {
*
* Change the format from input tensor to output tensor. This operation ignores
* the scale and zeroPoint of quanized tensors.
*
* ## Rcp
* Computes the reciprocal of input element-wise.
*/
DECLARE_SIMPLE_OP(DataConvert)
@ -108,6 +111,7 @@ DECLARE_SIMPLE_OP(Floor)
DECLARE_SIMPLE_OP(Ceil)
DECLARE_SIMPLE_OP(Round)
DECLARE_SIMPLE_OP(Cast)
DECLARE_SIMPLE_OP(Rcp)
#undef DECLARE_SIMPLE_OP

View File

@ -116,6 +116,7 @@ GroupedConv1d|GROUPED_CONV1D|Mapped|[tf.keras.layers.Conv1D](https://tensorflow.
Mod|MOD|Mapped|[Onnx.mod](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Mod)
Selu|SELU|Mapped|[tf.keras.activations.selu](https://www.tensorflow.org/api_docs/python/tf/keras/activations/selu)
Celu|CELU|Mapped|[Onnx.celu](https://github.com/onnx/onnx/blob/main/docs/Operators.md#Celu)
Rcp|RCP|Mapped|[tf.math.reciprocal](https://www.tensorflow.org/api_docs/python/tf/math/reciprocal)
|UnidirectionalSequenceRNN|UNIDIRECTIONAL_SEQUENCE_RNN|Planned 22Q3|[ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_RNN](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0ae11aa1d461d2abaa117f6ee2cb503dd8)
|BidirectionalSequenceRNN|BIDIRECTIONAL_SEQUENCE_RNN|Planned 22Q3|[ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_RNN](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a487fc5ae247de828f13e62b99f259f3c)
|BidirectionalSequenceLSTM|BIDIRECTIONAL_SEQUENCE_LSTM|Mapped|[ANEURALNETWORKS_BIDIRECTIONAL_SEQUENCE_LSTM](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a492a71cb7aa50b9a1a834a3cb269d778)

View File

@ -52,6 +52,7 @@ DEFINE_SIMPLE_OP(Floor, VSI_NN_OP_FLOOR)
DEFINE_SIMPLE_OP(Ceil, VSI_NN_OP_CEIL)
DEFINE_SIMPLE_OP(Round, VSI_NN_OP_ROUND)
DEFINE_SIMPLE_OP(Cast, VSI_NN_OP_CAST)
DEFINE_SIMPLE_OP(Rcp, VSI_NN_OP_RCP)
#undef DEFINE_SIMPLE_OP

View File

@ -24,6 +24,7 @@
#include "tim/vx/context.h"
#include "tim/vx/graph.h"
#include "tim/vx/ops/simple_operations.h"
#include "test_utils.h"
#include "gtest/gtest.h"
#include <cstdlib>
@ -232,4 +233,32 @@ TEST(DataConvert, requantize_shape_2_3_asym_u8) {
std::vector<uint8_t> output(6, 0);
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
EXPECT_EQ(golden, output);
}
TEST(Rcp, shape_5_1_fp32) {
auto ctx = tim::vx::Context::Create();
auto graph = ctx->CreateGraph();
tim::vx::ShapeType io_shape({5, 1});
tim::vx::TensorSpec input_spec(tim::vx::DataType::FLOAT32,
io_shape, tim::vx::TensorAttribute::INPUT);
tim::vx::TensorSpec output_spec(tim::vx::DataType::FLOAT32,
io_shape, tim::vx::TensorAttribute::OUTPUT);
auto input_tensor = graph->CreateTensor(input_spec);
auto output_tensor = graph->CreateTensor(output_spec);
std::vector<float> in_data = { -2.5, -0.1, 0, 0.55, std::numeric_limits<float>::infinity() };
std::vector<float> golden = {-0.4, -10, std::numeric_limits<float>::infinity(), 1.81818, 0.};
EXPECT_TRUE(input_tensor->CopyDataToTensor(in_data.data(), in_data.size()*4));
auto add = graph->CreateOperation<tim::vx::ops::Rcp>();
(*add).BindInputs({input_tensor}).BindOutputs({output_tensor});
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> output(5, 0);
EXPECT_TRUE(output_tensor->CopyDataFromTensor(output.data()));
EXPECT_TRUE(ArraysMatch(golden, output, 1e-5f));
}

View File

@ -115,7 +115,11 @@ template<typename T>
const std::vector<T>& actual,
T abs_error){
for (size_t i = 0; i < expected.size(); ++i){
EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i;
// If expected and actual are INF, "==" operation will return true while EXPEC_NEAR return false
if (expected[i] == actual[i]) continue;
else {
EXPECT_NEAR(expected[i], actual[i], abs_error) << "at index:" << i;
}
}
return ::testing::AssertionSuccess();