/* Copyright 2024 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 #include "absl/types/span.h" #include "tensorflow/lite/c/common.h" #include "tensorflow/lite/delegates/xnnpack/xnnpack_delegate.h" #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/internal/tensor_ctypes.h" #include "tensorflow/lite/kernels/kernel_util.h" #include "tensorflow/lite/kernels/subgraph_test_util.h" #include "tensorflow/lite/kernels/test_util.h" using testing::ElementsAreArray; using testing::FloatEq; using testing::Pointwise; namespace tflite { namespace { class CompositeTest : public subgraph_test_util::ControlFlowOpTest { protected: template TfLiteTensor* GetTensorWithIndirection(int id, const IndirectionVector& tensor_map) { return interpreter_->tensor(tensor_map[id]); } TfLiteTensor* GetInputTensor(int id) { return GetTensorWithIndirection(id, interpreter_->inputs()); } TfLiteTensor* GetOutputTensor(int id) { return GetTensorWithIndirection(id, interpreter_->outputs()); } template absl::Span GetTensorDataWithIndirection( int id, const IndirectionVector& tensor_map) { TfLiteTensor* const tensor = GetTensorWithIndirection(id, tensor_map); const size_t size = NumElements(tensor); return absl::Span(GetTensorData(tensor), size); } template absl::Span GetInputData(int id) { return GetTensorDataWithIndirection(id, interpreter_->inputs()); } template absl::Span GetOutputData(int id) { return GetTensorDataWithIndirection(id, interpreter_->outputs()); } }; TEST_F(CompositeTest, TestInvokeWorks) { AddSubgraphs(1); builder_->BuildAddSubgraph(interpreter_->subgraph(1)); builder_->BuildCompositeSubgraph(&interpreter_->primary_subgraph(), interpreter_->subgraph(1)); interpreter_->ResizeInputTensor(interpreter_->inputs()[0], {2, 3}); interpreter_->ResizeInputTensor(interpreter_->inputs()[1], {2, 3}); ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk); subgraph_test_util::FillIntTensor(GetInputTensor(0), {1, 2, 3, 4, 5, 6}); subgraph_test_util::FillIntTensor(GetInputTensor(1), {7, 8, 9, 10, 11, 12}); ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk); ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk); const TfLiteTensor* const output = GetOutputTensor(0); ASSERT_THAT(output, DimsAre({2, 3})); EXPECT_THAT(GetOutputData(0), ElementsAreArray({8, 10, 12, 14, 16, 18})); } TEST_F(CompositeTest, TestXNNPACKDelegation) { interpreter_ = std::make_unique(); AddSubgraphs(1); builder_->BuildXNNPACKSubgraph(interpreter_->subgraph(1)); builder_->BuildCompositeSubgraph(&interpreter_->primary_subgraph(), interpreter_->subgraph(1)); const auto opt = TfLiteXNNPackDelegateOptionsDefault(); std::unique_ptr xnnpack_delegate( TfLiteXNNPackDelegateCreate(&opt), TfLiteXNNPackDelegateDelete); interpreter_->primary_subgraph().MarkAsDelegationSkippable(); ASSERT_EQ(interpreter_->ModifyGraphWithDelegate(std::move(xnnpack_delegate)), kTfLiteOk); ASSERT_EQ(interpreter_->ResizeInputTensor(interpreter_->inputs()[0], {2, 3}), kTfLiteOk); ASSERT_EQ(interpreter_->ResizeInputTensor(interpreter_->inputs()[1], {2, 3}), kTfLiteOk); ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk); absl::Span input0 = GetInputData(0); std::iota(input0.begin(), input0.end(), 1.0f); absl::Span input1 = GetInputData(1); std::iota(input1.begin(), input1.end(), 7.0f); ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk); const std::vector expected_values = {16, 20, 24, 28, 32, 36}; TfLiteTensor* output0 = interpreter_->tensor(interpreter_->outputs()[0]); const absl::Span output0_data(GetTensorData(output0), 6); ASSERT_THAT(output0, DimsAre({2, 3})); EXPECT_THAT(output0_data, Pointwise(FloatEq(), expected_values)); TfLiteTensor* output1 = interpreter_->tensor(interpreter_->outputs()[1]); const absl::Span output1_data(GetTensorData(output1), 6); ASSERT_THAT(output1, DimsAre({2, 3})); EXPECT_THAT(output1_data, Pointwise(FloatEq(), expected_values)); ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk); ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk); } } // namespace } // namespace tflite