Skip to content

Commit

Permalink
Provide TfLiteOpaqueContextAddTensors, an API to add additional ten…
Browse files Browse the repository at this point in the history
…sors to a supplied context.

This functionality is used by existing delegates, e.g.: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/delegates/nnapi/nnapi_delegate.cc that aren't using the opaque delegate API.

PiperOrigin-RevId: 553726768
  • Loading branch information
Matt Kreileder authored and tensorflower-gardener committed Aug 4, 2023
1 parent 4b22c55 commit 144e78d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tensorflow/lite/core/c/c_api_opaque.cc
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,17 @@ TfLiteStatus TfLiteOpaqueContextGetNodeInitDataMmapInfo(
custom_initial_data_size);
}

TfLiteStatus TfLiteOpaqueContextAddTensors(TfLiteOpaqueContext* context,
int tensors_to_add,
int* first_new_tensor_index) {
if (tensors_to_add <= 0) {
return kTfLiteError;
}
auto* tflite_context = Convert(context);
return tflite_context->AddTensors(tflite_context, tensors_to_add,
first_new_tensor_index);
}

void TfLiteOpaqueContextReportError(struct TfLiteOpaqueContext* opaque_context,
const char* format, ...) {
va_list vlist;
Expand Down
11 changes: 11 additions & 0 deletions tensorflow/lite/core/c/c_api_opaque.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,17 @@ TfLiteStatus TfLiteOpaqueContextGetNodeInitDataMmapInfo(
int64_t* custom_initial_data_offset_in_file,
int64_t* custom_initial_data_size);

// Add 'tensors_to_add' tensors, preserving pre-existing Tensor entries. If
// non-null, the value pointed to by 'first_new_tensor_index' will be set to
// the index of the first new tensor. Returns 'kTfLiteOk' when the tensors have
// been added successfully. Returns 'kTfLiteError' in case of failure. Suppling
// a value equal to or smaller than 0 for 'tensors_to_add' will result in
// 'kTfLiteError' being returned.
TFL_CAPI_EXPORT
TfLiteStatus TfLiteOpaqueContextAddTensors(TfLiteOpaqueContext* context,
int tensors_to_add,
int* first_new_tensor_index);

/// Reports an error message formed by using the provided 'format' string in
/// combination with the data provided via the unnamed arguments following
/// the 'format' parameter ('...'). The intended usage and behavior is the same
Expand Down
27 changes: 27 additions & 0 deletions tensorflow/lite/core/c/c_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,33 @@ TEST(CApiSimple, OpaqueApiAccessors) {
EXPECT_STREQ(kSubgraphName,
TfLiteOpaqueContextGetName(opaque_context));
EXPECT_EQ(4, TfLiteOpaqueContextGetNumTensors(opaque_context));

int first_new_tensor_index = -1;
EXPECT_EQ(kTfLiteOk, TfLiteOpaqueContextAddTensors(
opaque_context, 1, &first_new_tensor_index));
EXPECT_EQ(5, TfLiteOpaqueContextGetNumTensors(opaque_context));
EXPECT_EQ(4, first_new_tensor_index);
TfLiteOpaqueTensor* new_tensor = TfLiteOpaqueContextGetOpaqueTensor(
opaque_context, first_new_tensor_index);
EXPECT_NE(new_tensor, nullptr);

EXPECT_EQ(kTfLiteOk, TfLiteOpaqueContextAddTensors(
opaque_context, 2, &first_new_tensor_index));
EXPECT_EQ(7, TfLiteOpaqueContextGetNumTensors(opaque_context));
EXPECT_EQ(5, first_new_tensor_index);
new_tensor = TfLiteOpaqueContextGetOpaqueTensor(
opaque_context, first_new_tensor_index);
EXPECT_NE(new_tensor, nullptr);
new_tensor = TfLiteOpaqueContextGetOpaqueTensor(
opaque_context, first_new_tensor_index + 1);
EXPECT_NE(new_tensor, nullptr);

EXPECT_EQ(kTfLiteError,
TfLiteOpaqueContextAddTensors(opaque_context, 0,
&first_new_tensor_index));
EXPECT_EQ(kTfLiteError,
TfLiteOpaqueContextAddTensors(opaque_context, -1,
&first_new_tensor_index));
EXPECT_EQ(-1,
TfLiteOpaqueTensorNumDims(
TfLiteOpaqueContextGetOpaqueTensor(opaque_context, 3)));
Expand Down

0 comments on commit 144e78d

Please sign in to comment.