// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include #include #include "tensorflow/lite/kernels/test_util.h" #include "tensorflow/lite/schema/schema_generated.h" #include "tensorflow/lite/types/half.h" namespace tflite { namespace { template tflite::TensorType GetTTEnum(); template <> tflite::TensorType GetTTEnum() { return tflite::TensorType_FLOAT32; } template <> tflite::TensorType GetTTEnum() { return tflite::TensorType_FLOAT64; } template <> tflite::TensorType GetTTEnum() { return tflite::TensorType_FLOAT16; } template <> tflite::TensorType GetTTEnum() { return tflite::TensorType_BFLOAT16; } class Atan2Model : public tflite::SingleOpModel { public: Atan2Model(tflite::TensorData y, tflite::TensorData x, tflite::TensorData output) { y_ = AddInput(y); x_ = AddInput(x); output_ = AddOutput(output); SetBuiltinOp(BuiltinOperator_ATAN2, BuiltinOptions_NONE, 0); BuildInterpreter({GetShape(y_), GetShape(x_)}); } template std::vector GetOutput(const std::vector& y, const std::vector& x) { PopulateTensor(y_, y); PopulateTensor(x_, x); Invoke(); return ExtractVector(output_); } private: int y_; int x_; int output_; }; template class Atan2Test : public ::testing::Test { public: using FloatType = Float; }; using TestTypes = ::testing::Types; TYPED_TEST_SUITE(Atan2Test, TestTypes); TYPED_TEST(Atan2Test, TestScalar) { using Float = typename TestFixture::FloatType; tflite::TensorData y = {GetTTEnum(), {}}; tflite::TensorData x = {GetTTEnum(), {}}; tflite::TensorData output = {GetTTEnum(), {}}; Atan2Model m(y, x, output); auto got = m.GetOutput({Float(0.0f)}, {Float(0.0f)}); ASSERT_EQ(got.size(), 1); EXPECT_FLOAT_EQ(got[0], 0.0); ASSERT_FLOAT_EQ(m.GetOutput({Float(1.0f)}, {Float(0.0f)})[0], Float(static_cast(M_PI / 2))); ASSERT_FLOAT_EQ(m.GetOutput({Float(0.0f)}, {Float(1.0f)})[0], Float(0.0f)); ASSERT_FLOAT_EQ(m.GetOutput({Float(-1.0f)}, {Float(0.0f)})[0], Float(-static_cast(M_PI / 2))); } TYPED_TEST(Atan2Test, TestBatch) { using Float = typename TestFixture::FloatType; tflite::TensorData y = {GetTTEnum(), {4, 2, 1}}; tflite::TensorData x = {GetTTEnum(), {4, 2, 1}}; tflite::TensorData output = {GetTTEnum(), {4, 2, 1}}; Atan2Model m(y, x, output); std::vector y_data = {Float(0.1f), Float(0.2f), Float(0.3f), Float(0.4f), Float(0.5f), Float(0.6f), Float(0.7f), Float(0.8f)}; std::vector x_data = {Float(0.8f), Float(0.7f), Float(0.6f), Float(0.5f), Float(0.4f), Float(0.3f), Float(0.2f), Float(0.1f)}; auto got = m.GetOutput(y_data, x_data); ASSERT_EQ(got.size(), 8); for (int i = 0; i < 8; ++i) { EXPECT_FLOAT_EQ(got[i], Float(std::atan2(y_data[i], x_data[i]))); } } } // namespace } // namespace tflite