Skip to content

Commit 0511ac7

Browse files
authored
Fix set_external_context() during state kInit (#3102)
@tensorflow/micro Fixes MicroInterpreterContext::set_external_context so that it can be called during InterpreterState::kInit. Add unit test for kInit state. Cleanup other external context tests. bug=fixes #3101
1 parent 3c0b1e3 commit 0511ac7

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

tensorflow/lite/micro/micro_interpreter_context.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ void MicroInterpreterContext::SetScratchBufferHandles(
112112

113113
TfLiteStatus MicroInterpreterContext::set_external_context(
114114
void* external_context_payload) {
115-
TFLITE_DCHECK(state_ == InterpreterState::kPrepare ||
115+
TFLITE_DCHECK(state_ == InterpreterState::kInit ||
116+
state_ == InterpreterState::kPrepare ||
116117
state_ == InterpreterState::kInvoke);
117118
if (external_context_payload == nullptr ||
118119
external_context_payload_ != nullptr) {

tensorflow/lite/micro/micro_interpreter_context_test.cc

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ struct TestExternalContextPayloadData {
5454

5555
TF_LITE_MICRO_TESTS_BEGIN
5656

57-
// Ensures that a regular set and get pair works ok.
58-
TF_LITE_MICRO_TEST(TestSetGetExternalContextSuccess) {
57+
// Ensures that a regular set and get pair works ok during state kInvoke.
58+
TF_LITE_MICRO_TEST(TestSetGetExternalContextSuccessInvoke) {
5959
tflite::MicroInterpreterContext micro_context =
6060
tflite::CreateMicroInterpreterContext();
6161
micro_context.SetInterpreterState(
@@ -70,19 +70,36 @@ TF_LITE_MICRO_TEST(TestSetGetExternalContextSuccess) {
7070
micro_context.external_context());
7171

7272
// What is returned should be the same as what is set.
73-
TF_LITE_MICRO_EXPECT((void*)returned_external_context == (void*)(&payload));
73+
TF_LITE_MICRO_EXPECT(returned_external_context == &payload);
7474
}
7575

76-
TF_LITE_MICRO_TEST(TestGetExternalContextWithoutSetShouldReturnNull) {
76+
// Ensures that a regular set and get pair works ok during state kInit.
77+
TF_LITE_MICRO_TEST(TestSetGetExternalContextSuccessInit) {
7778
tflite::MicroInterpreterContext micro_context =
7879
tflite::CreateMicroInterpreterContext();
80+
micro_context.SetInterpreterState(
81+
tflite::MicroInterpreterContext::InterpreterState::kInit);
82+
83+
tflite::TestExternalContextPayloadData payload;
84+
TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk,
85+
micro_context.set_external_context(&payload));
7986

8087
tflite::TestExternalContextPayloadData* returned_external_context =
8188
reinterpret_cast<tflite::TestExternalContextPayloadData*>(
8289
micro_context.external_context());
8390

91+
// What is returned should be the same as what is set.
92+
TF_LITE_MICRO_EXPECT(returned_external_context == &payload);
93+
}
94+
95+
TF_LITE_MICRO_TEST(TestGetExternalContextWithoutSetShouldReturnNull) {
96+
tflite::MicroInterpreterContext micro_context =
97+
tflite::CreateMicroInterpreterContext();
98+
99+
void* returned_external_context = micro_context.external_context();
100+
84101
// Return a null if nothing is set before.
85-
TF_LITE_MICRO_EXPECT((void*)returned_external_context == (nullptr));
102+
TF_LITE_MICRO_EXPECT(returned_external_context == nullptr);
86103
}
87104

88105
TF_LITE_MICRO_TEST(TestSetExternalContextCanOnlyBeCalledOnce) {
@@ -98,6 +115,15 @@ TF_LITE_MICRO_TEST(TestSetExternalContextCanOnlyBeCalledOnce) {
98115
// Another set should fail.
99116
TF_LITE_MICRO_EXPECT_EQ(kTfLiteError,
100117
micro_context.set_external_context(&payload));
118+
119+
// Null set should fail.
120+
TF_LITE_MICRO_EXPECT_EQ(kTfLiteError,
121+
micro_context.set_external_context(nullptr));
122+
tflite::TestExternalContextPayloadData* returned_external_context =
123+
reinterpret_cast<tflite::TestExternalContextPayloadData*>(
124+
micro_context.external_context());
125+
// Payload should be unchanged.
126+
TF_LITE_MICRO_EXPECT(&payload == returned_external_context);
101127
}
102128

103129
TF_LITE_MICRO_TEST(TestSetExternalContextToNullShouldFail) {

0 commit comments

Comments
 (0)