This change is for adding CallOnce op in TFLite builtin op set. CallOnce operator is a control flow op to invoke other subgraph in the graph in order to conduct the given graph's initialization tasks, for example, hash table initialization and variable initialization. This new operator will invoke the subgraph for initialization in the first run and become no-op after the first run in an interpreter's life cycle. PiperOrigin-RevId: 339763662 Change-Id: I8c2ae7213e749b76b9294175562389ebe79b542e
88 lines
2.7 KiB
C++
88 lines
2.7 KiB
C++
/* Copyright 2020 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 <stdint.h>
|
|
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#include <gtest/gtest.h>
|
|
#include "tensorflow/lite/interpreter.h"
|
|
#include "tensorflow/lite/kernels/kernel_util.h"
|
|
#include "tensorflow/lite/kernels/subgraph_test_util.h"
|
|
|
|
namespace tflite {
|
|
|
|
using subgraph_test_util::ControlFlowOpTest;
|
|
|
|
namespace {
|
|
|
|
class CallOnceTest : public ControlFlowOpTest {
|
|
protected:
|
|
void SetUp() override {
|
|
interpreter_->AddSubgraphs(1);
|
|
builder_->BuildCallOnceAndReadVariableSubgraph(
|
|
&interpreter_->primary_subgraph());
|
|
builder_->BuildAssignRandomValueToVariableSubgraph(
|
|
interpreter_->subgraph(1));
|
|
|
|
ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk);
|
|
}
|
|
};
|
|
|
|
TEST_F(CallOnceTest, TestSimple) {
|
|
ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
|
|
|
|
TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
|
|
ASSERT_EQ(output->dims->size, 1);
|
|
ASSERT_EQ(output->dims->data[0], 1);
|
|
ASSERT_EQ(output->type, kTfLiteInt32);
|
|
ASSERT_EQ(NumElements(output), 1);
|
|
|
|
// The value of the variable must be non-zero, which will be assigned by the
|
|
// initialization subgraph.
|
|
EXPECT_GT(output->data.i32[0], 0);
|
|
}
|
|
|
|
TEST_F(CallOnceTest, TestInvokeMultipleTimes) {
|
|
ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
|
|
|
|
TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
|
|
ASSERT_EQ(output->dims->size, 1);
|
|
ASSERT_EQ(output->dims->data[0], 1);
|
|
ASSERT_EQ(output->type, kTfLiteInt32);
|
|
ASSERT_EQ(NumElements(output), 1);
|
|
|
|
// The value of the variable must be non-zero, which will be assigned by the
|
|
// initialization subgraph.
|
|
int value = output->data.i32[0];
|
|
EXPECT_GT(value, 0);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
// Make sure that no more random value assignment in the initialization
|
|
// subgraph.
|
|
ASSERT_EQ(interpreter_->Invoke(), kTfLiteOk);
|
|
TfLiteTensor* output = interpreter_->tensor(interpreter_->outputs()[0]);
|
|
ASSERT_EQ(output->dims->size, 1);
|
|
ASSERT_EQ(output->dims->data[0], 1);
|
|
ASSERT_EQ(output->type, kTfLiteInt32);
|
|
ASSERT_EQ(NumElements(output), 1);
|
|
ASSERT_EQ(output->data.i32[0], value);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace tflite
|