aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/experimental
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-09-13 14:53:31 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-09-13 14:57:25 -0700
commit25d8c732dcf7fa82d086c5da46408838fa0f04f1 (patch)
tree5a54a82c8450f714d7c01344830139946a7b1407 /tensorflow/contrib/lite/experimental
parent4999d856d2953aee56fa9759f995038edf3ff566 (diff)
Add ability to skip serializing selected tensors in interpreter serializer.
PiperOrigin-RevId: 212883697
Diffstat (limited to 'tensorflow/contrib/lite/experimental')
-rw-r--r--tensorflow/contrib/lite/experimental/writer/writer_lib.cc14
-rw-r--r--tensorflow/contrib/lite/experimental/writer/writer_lib.h7
2 files changed, 16 insertions, 5 deletions
diff --git a/tensorflow/contrib/lite/experimental/writer/writer_lib.cc b/tensorflow/contrib/lite/experimental/writer/writer_lib.cc
index 52b17faf82..555a9cc4b0 100644
--- a/tensorflow/contrib/lite/experimental/writer/writer_lib.cc
+++ b/tensorflow/contrib/lite/experimental/writer/writer_lib.cc
@@ -117,6 +117,8 @@ Offset<Vector<Offset<Operator>>> InterpreterWriter::ExportOperators(
Offset<Vector<Offset<Tensor>>> InterpreterWriter::ExportTensors(
FlatBufferBuilder* fbb) {
+ // Initialized to -1.
+ // A value of -1 means this tensor will not be exported.
tensor_to_written_tensor_.resize(interpreter_->tensors_size(), -1);
std::vector<Offset<Tensor>> tensors;
@@ -135,15 +137,17 @@ Offset<Vector<Offset<Tensor>>> InterpreterWriter::ExportTensors(
int curr_output_index = 0;
for (int tensor_index = 0; tensor_index < interpreter_->tensors_size();
tensor_index++) {
- if (!tensor_is_temporary[tensor_index]) {
+ // Temporary tensors and unused tensors will not be written.
+ if (!tensor_is_temporary[tensor_index] &&
+ unused_tensors_.find(tensor_index) == unused_tensors_.end()) {
tensor_to_written_tensor_[tensor_index] = curr_output_index++;
}
}
for (int tensor_index = 0; tensor_index < interpreter_->tensors_size();
++tensor_index) {
- // Skip temporaries.
- if (tensor_is_temporary[tensor_index]) continue;
+ // Tensor not exported.
+ if (tensor_to_written_tensor_[tensor_index] == -1) continue;
if (TfLiteTensor* tensor = interpreter_->tensor(tensor_index)) {
// We only need to convert non temporaries
@@ -215,7 +219,9 @@ std::vector<int> InterpreterWriter::RemapTensorIndicesToWritten(
std::vector<int> output;
output.reserve(input.size());
for (int x : input) {
- output.push_back(tensor_to_written_tensor_[x]);
+ if (tensor_to_written_tensor_[x] != -1) {
+ output.push_back(tensor_to_written_tensor_[x]);
+ }
}
return output;
}
diff --git a/tensorflow/contrib/lite/experimental/writer/writer_lib.h b/tensorflow/contrib/lite/experimental/writer/writer_lib.h
index a98108b496..a5f14697cf 100644
--- a/tensorflow/contrib/lite/experimental/writer/writer_lib.h
+++ b/tensorflow/contrib/lite/experimental/writer/writer_lib.h
@@ -62,6 +62,10 @@ class InterpreterWriter {
// caller to change the custom data.
TfLiteStatus RegisterCustomWriter(const std::string& custom_name,
CustomWriter custom_writer);
+ // Tensors that are unused and shouldn't be written.
+ void SetUnusedTensors(const std::set<int>& unused_tensors) {
+ unused_tensors_ = unused_tensors;
+ }
private:
template <class T>
@@ -111,8 +115,9 @@ class InterpreterWriter {
int builtin;
std::string custom;
};
+ std::set<int> unused_tensors_;
// For every tensor index in the interpreter, the index in the written.
- // This is different due to temporary tensors not being written.
+ // This is different due to temporary and unused tensors not being written.
std::vector<int> tensor_to_written_tensor_;
// List of used opcodes
std::vector<OpCode> opcodes_;