Skip to content

Commit 581a2f4

Browse files
ai-edge-botcopybara-github
authored andcommitted
Update interpreter and subgraph to provide external buffer id and tensor mapping
PiperOrigin-RevId: 822152535
1 parent bfba34f commit 581a2f4

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

tflite/core/interpreter_builder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ TfLiteStatus InterpreterBuilder::ParseTensors(
725725
if (subgraph->SetTensorParametersReadOnly(
726726
i, type, get_name(tensor), dims, quantization, buffer_ptr,
727727
buffer_size, allocation_, sparsity,
728-
/*buffer_identifier=*/tensor->buffer()) != kTfLiteOk) {
728+
/*buffer_identifier=*/tensor->buffer(),
729+
/*external_buffer_id=*/tensor->external_buffer()) != kTfLiteOk) {
729730
TF_LITE_REPORT_ERROR(error_reporter_,
730731
"Tensor %d is invalidly specified in schema.\n",
731732
i);

tflite/core/subgraph.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,7 @@ TfLiteStatus Subgraph::SetTensorParametersReadOnly(
19171917
int tensor_index, TfLiteType type, const char* name, const size_t ndims,
19181918
const int* dims, TfLiteQuantization quantization, const char* buffer,
19191919
size_t bytes, const Allocation* allocation, TfLiteSparsity* sparsity,
1920-
const size_t buffer_identifier) {
1920+
const size_t buffer_identifier, const size_t external_buffer_id) {
19211921
// Ensure quantization cleanup on failure.
19221922
ScopedTfLiteQuantization scoped_quantization(&quantization);
19231923
ScopedTfLiteSparsity scoped_sparsity(sparsity);
@@ -1968,6 +1968,10 @@ TfLiteStatus Subgraph::SetTensorParametersReadOnly(
19681968
if (buffer_identifier != kTfLiteNoBufferIdentifier) {
19691969
tensor_buffer_identifiers_[tensor_index] = buffer_identifier;
19701970
}
1971+
if (external_buffer_id != kTfLiteNoBufferIdentifier &&
1972+
external_buffer_id != 0) {
1973+
tensor_external_buffer_ids_[tensor_index] = external_buffer_id;
1974+
}
19711975
return kTfLiteOk;
19721976
}
19731977

tflite/core/subgraph.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,32 @@ class Subgraph {
130130
// This variant assumes an external buffer has been allocated of size
131131
// bytes. The lifetime of buffer must be ensured to be greater or equal
132132
// to Interpreter. `quantization` ownership is passed to the subgraph.
133+
// `buffer_identifier`: An optional value to identify the buffer. If set to
134+
// a value other than kTfLiteNoBufferIdentifier, this tensor is considered a
135+
// constant tensor shared across multiple subgraphs / interpreters.
136+
// `external_buffer_id`: An optional value to identify the external buffer. If
137+
// set to a value other than kTfLiteNoBufferIdentifier, this tensor is
138+
// considered a tensor using an external buffer shared across multiple
139+
// subgraphs / interpreters.
133140
inline TfLiteStatus SetTensorParametersReadOnly(
134141
int tensor_index, TfLiteType type, const char* name,
135142
const std::vector<int>& dims, TfLiteQuantization quantization,
136143
const char* buffer, size_t bytes, const Allocation* allocation = nullptr,
137144
TfLiteSparsity* sparsity = nullptr,
138-
size_t buffer_identifier = kTfLiteNoBufferIdentifier) {
145+
size_t buffer_identifier = kTfLiteNoBufferIdentifier,
146+
size_t external_buffer_id = kTfLiteNoBufferIdentifier) {
139147
return SetTensorParametersReadOnly(tensor_index, type, name, dims.size(),
140148
dims.data(), quantization, buffer, bytes,
141-
allocation, sparsity, buffer_identifier);
149+
allocation, sparsity, buffer_identifier,
150+
external_buffer_id);
142151
}
143152
TfLiteStatus SetTensorParametersReadOnly(
144153
int tensor_index, TfLiteType type, const char* name, size_t ndims,
145154
const int* dims, TfLiteQuantization quantization, const char* buffer,
146155
size_t bytes, const Allocation* allocation = nullptr,
147156
TfLiteSparsity* sparsity = nullptr,
148-
size_t buffer_identifier = kTfLiteNoBufferIdentifier);
157+
size_t buffer_identifier = kTfLiteNoBufferIdentifier,
158+
size_t external_buffer_id = kTfLiteNoBufferIdentifier);
149159

150160
// Set description of inputs/outputs/data/fptrs for node `node_index`.
151161
// This variant assumes an external buffer has been allocated of size
@@ -611,6 +621,11 @@ class Subgraph {
611621
return tensor_buffer_identifiers_;
612622
}
613623

624+
const std::unordered_map<size_t, size_t>& GetExternalTensorBufferIdentifiers()
625+
const {
626+
return tensor_external_buffer_ids_;
627+
}
628+
614629
// Replaces the node for the given execution index with the subgraph.
615630
//
616631
// - The node and subgraph tensor counts must match.
@@ -1220,6 +1235,10 @@ class Subgraph {
12201235
// Maps tensor constant buffers used in the subgraph to a model-wide
12211236
// identifiers.
12221237
std::unordered_map<size_t, size_t> tensor_buffer_identifiers_;
1238+
1239+
// Maps tensor external buffer ids used in the subgraph to a model-wide
1240+
// identifiers.
1241+
std::unordered_map<size_t, size_t> tensor_external_buffer_ids_;
12231242
};
12241243

12251244
} // namespace tflite

0 commit comments

Comments
 (0)