aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/interpreter_test.cc
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-06-19 10:02:11 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-06-19 10:05:35 -0700
commit7f449920f8910561a4e57cc35b96fb7faf08ef98 (patch)
tree036ee28b10d59da6bb38a1e59625a5620b94924f /tensorflow/contrib/lite/interpreter_test.cc
parent5fc2bdd2d5f624a6bad9e83b992029e3799ab64e (diff)
Refresh allocations in the presence of dynamic tensors
PiperOrigin-RevId: 201193941
Diffstat (limited to 'tensorflow/contrib/lite/interpreter_test.cc')
-rw-r--r--tensorflow/contrib/lite/interpreter_test.cc59
1 files changed, 59 insertions, 0 deletions
diff --git a/tensorflow/contrib/lite/interpreter_test.cc b/tensorflow/contrib/lite/interpreter_test.cc
index b977cb089c..21cdf87d1e 100644
--- a/tensorflow/contrib/lite/interpreter_test.cc
+++ b/tensorflow/contrib/lite/interpreter_test.cc
@@ -23,6 +23,12 @@ limitations under the License.
#include "tensorflow/contrib/lite/testing/util.h"
namespace tflite {
+namespace ops {
+namespace builtin {
+TfLiteRegistration* Register_PADV2();
+TfLiteRegistration* Register_NEG();
+} // namespace builtin
+} // namespace ops
namespace {
// Make an interpreter that has no tensors and no nodes
@@ -615,6 +621,59 @@ TEST(BasicInterpreter, TestUnsupportedDelegateFunctions) {
EXPECT_EQ(interpreter.AllocateTensors(), kTfLiteError);
}
+TEST(BasicInterpreter, DynamicTensorsResizeDescendants) {
+ // Assemble a graph with a node that has dynamically sized output (via the
+ // pad op), followed by a node with a standard element-wise op (negate).
+ Interpreter interpreter;
+ interpreter.AddTensors(4);
+ interpreter.SetInputs({0, 1});
+ interpreter.SetOutputs({3});
+ TfLiteQuantizationParams quant;
+ interpreter.SetTensorParametersReadWrite(0, kTfLiteFloat32, "", {2, 2, 1, 1},
+ quant);
+ interpreter.SetTensorParametersReadWrite(1, kTfLiteInt32, "", {4, 2}, quant);
+ interpreter.SetTensorParametersReadWrite(2, kTfLiteFloat32, "", {}, quant);
+ interpreter.SetTensorParametersReadWrite(3, kTfLiteFloat32, "", {}, quant);
+
+ TfLiteRegistration* pad_op = tflite::ops::builtin::Register_PADV2();
+ TfLiteRegistration* neg_op = tflite::ops::builtin::Register_NEG();
+ interpreter.AddNodeWithParameters({0, 1}, {2}, nullptr, 0, nullptr, pad_op);
+ interpreter.AddNodeWithParameters({2}, {3}, nullptr, 0, nullptr, neg_op);
+ ASSERT_EQ(interpreter.AllocateTensors(), kTfLiteOk);
+
+ // Configure [[2,2],[4,4]] padding and execute the graph.
+ interpreter.typed_tensor<int>(1)[0] = 2;
+ interpreter.typed_tensor<int>(1)[1] = 2;
+ interpreter.typed_tensor<int>(1)[2] = 2;
+ interpreter.typed_tensor<int>(1)[3] = 2;
+ interpreter.typed_tensor<int>(1)[4] = 0;
+ interpreter.typed_tensor<int>(1)[5] = 0;
+ interpreter.typed_tensor<int>(1)[6] = 0;
+ interpreter.typed_tensor<int>(1)[7] = 0;
+ ASSERT_EQ(interpreter.Invoke(), kTfLiteOk);
+
+ // Both the output and intermediate tensor sizes should reflect the output
+ // from the dynamic pad operation.
+ ASSERT_EQ(interpreter.tensor(2)->bytes, sizeof(float) * 6 * 6);
+ ASSERT_EQ(interpreter.tensor(3)->bytes, sizeof(float) * 6 * 6);
+
+ // Now configure [[4,4],[6,6]] padding and execute the graph.
+ interpreter.typed_tensor<int>(1)[0] = 4;
+ interpreter.typed_tensor<int>(1)[1] = 4;
+ interpreter.typed_tensor<int>(1)[2] = 6;
+ interpreter.typed_tensor<int>(1)[3] = 6;
+ interpreter.typed_tensor<int>(1)[4] = 0;
+ interpreter.typed_tensor<int>(1)[5] = 0;
+ interpreter.typed_tensor<int>(1)[6] = 0;
+ interpreter.typed_tensor<int>(1)[7] = 0;
+ ASSERT_EQ(interpreter.Invoke(), kTfLiteOk);
+
+ // Again, the output and intermediate tensor sizes should reflect the *new*
+ // resize from the latest pad operation.
+ ASSERT_EQ(interpreter.tensor(2)->bytes, sizeof(float) * 10 * 14);
+ ASSERT_EQ(interpreter.tensor(3)->bytes, sizeof(float) * 10 * 14);
+}
+
TEST(InterpreterTensorsCapacityTest, TestWithinHeadroom) {
Interpreter interpreter;
ASSERT_EQ(interpreter.AddTensors(Interpreter::kTensorsReservedCapacity),