/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. 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 #include #include #include #include "tensorflow/lite/kernels/test_util.h" #include "tensorflow/lite/schema/schema_generated.h" namespace tflite { namespace { using ::testing::ElementsAreArray; // std::bit_cast is only available since c++20, provide a handrolled version // of bit_cast here. Implementation modified from abseil bit_cast. template < typename Dest, typename Source, typename std::enable_if::value && std::is_trivially_copyable::value && std::is_default_constructible::value, int>::type = 0> inline Dest bit_cast(const Source& source) { Dest dest; memcpy(static_cast(std::addressof(dest)), static_cast(std::addressof(source)), sizeof(dest)); return dest; } class BitcastOpModel : public SingleOpModel { public: BitcastOpModel(const TensorData& input, const TensorData& output) { input_ = AddInput(input); output_ = AddOutput(output); SetBuiltinOp(BuiltinOperator_BITCAST, BuiltinOptions_BitcastOptions, CreateBitcastOptions(builder_).Union()); BuildInterpreter({GetShape(input_)}); } int input() const { return input_; } int output() const { return output_; } protected: int input_; int output_; }; TEST(BitcastOpModel, BitcastInt32ToUint32) { BitcastOpModel m({TensorType_INT32, {2, 3}}, {TensorType_UINT32, {2, 3}}); std::vector input = {INT32_MIN, -100, -1, 0, 100, INT32_MAX}; m.PopulateTensor(m.input(), input); ASSERT_EQ(m.Invoke(), kTfLiteOk); std::vector output; std::transform(input.cbegin(), input.cend(), std::back_inserter(output), [](int32_t a) { return bit_cast(a); }); EXPECT_THAT(m.ExtractVector(m.output()), ElementsAreArray(output)); } TEST(BitcastOpModel, BitcastUInt32ToInt32Inplace) { BitcastOpModel m({TensorType_UINT32, {2, 3}}, {TensorType_INT32, {2, 3}}); std::vector input = {0, 1, 100, bit_cast(INT32_MAX), bit_cast(INT32_MIN), UINT32_MAX}; m.PopulateTensor(m.input(), input); const int kInplaceTensorIdx = 0; const TfLiteTensor* input_tensor = m.GetInputTensor(kInplaceTensorIdx); TfLiteTensor* output_tensor = m.GetOutputTensor(kInplaceTensorIdx); output_tensor->data.data = input_tensor->data.data; ASSERT_EQ(m.Invoke(), kTfLiteOk); std::vector output; std::transform(input.cbegin(), input.cend(), std::back_inserter(output), [](uint32_t a) { return bit_cast(a); }); EXPECT_THAT(m.ExtractVector(m.output()), ElementsAreArray(output)); EXPECT_EQ(output_tensor->data.data, input_tensor->data.data); } TEST(BitcastOpModel, BitcastUInt32ToInt32) { BitcastOpModel m({TensorType_UINT32, {2, 3}}, {TensorType_INT32, {2, 3}}); std::vector input = {0, 1, 100, bit_cast(INT32_MAX), bit_cast(INT32_MIN), UINT32_MAX}; m.PopulateTensor(m.input(), input); ASSERT_EQ(m.Invoke(), kTfLiteOk); std::vector output; std::transform(input.cbegin(), input.cend(), std::back_inserter(output), [](uint32_t a) { return bit_cast(a); }); EXPECT_THAT(m.ExtractVector(m.output()), ElementsAreArray(output)); } TEST(BitcastOpModel, BitcastUInt32Toint16) { BitcastOpModel m({TensorType_UINT32, {2, 1}}, {TensorType_INT16, {2, 1, 2}}); std::vector input = {(uint32_t)UINT16_MAX + 1, (uint32_t)UINT16_MAX}; m.PopulateTensor(m.input(), input); ASSERT_EQ(m.Invoke(), kTfLiteOk); #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ // 00..01 00..00 // 00..00 11..11 std::vector output = {1, 0, 0, -1}; #else // 00..00 00..01 // 11..11 00..00 std::vector output = {0, 1, -1, 0}; #endif EXPECT_THAT(m.ExtractVector(m.output()), ElementsAreArray(output)); } TEST(BitcastOpModel, BitcastInt16ToUint32) { BitcastOpModel m({TensorType_INT16, {2, 1, 2}}, {TensorType_UINT32, {2, 1}}); #if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ std::vector input = {1, 0, 0, -1}; #else std::vector input = {0, 1, -1, 0}; #endif m.PopulateTensor(m.input(), input); ASSERT_EQ(m.Invoke(), kTfLiteOk); std::vector output = {(uint32_t)UINT16_MAX + 1, (uint32_t)UINT16_MAX}; EXPECT_THAT(m.ExtractVector(m.output()), ElementsAreArray(output)); } TEST(BitcastOpModel, BitcastInt16ToUint32WrongShape) { #if GTEST_HAS_DEATH_TEST EXPECT_DEATH(BitcastOpModel m({TensorType_INT16, {2, 2, 7}}, {TensorType_UINT32, {2, 7}}), "7 != 2"); #endif } } // namespace } // namespace tflite