1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/contrib/lite/string_util.h" 17 18 #include <string.h> 19 #include <vector> 20 #include "tensorflow/contrib/lite/context.h" 21 #include "tensorflow/contrib/lite/interpreter.h" 22 23 namespace tflite { 24 namespace { 25 26 // Convenient method to get pointer to int32_t. 27 int32_t* GetIntPtr(char* ptr) { return reinterpret_cast<int32_t*>(ptr); } 28 } // namespace 29 30 void DynamicBuffer::AddString(const char* str, size_t len) { 31 data_.resize(data_.size() + len); 32 memcpy(data_.data() + offset_.back(), str, len); 33 offset_.push_back(offset_.back() + len); 34 } 35 36 void DynamicBuffer::AddString(const StringRef& string) { 37 AddString(string.str, string.len); 38 } 39 40 void DynamicBuffer::AddJoinedString(const std::vector<StringRef>& strings, 41 char separator) { 42 // Resize the data buffer. 43 int total_len = strings.size() - 1; 44 for (StringRef ref : strings) { 45 total_len += ref.len; 46 } 47 data_.resize(data_.size() + total_len); 48 49 int current_idx = 0; 50 for (StringRef ref : strings) { 51 char* dst = data_.data() + offset_.back() + current_idx; 52 53 // Fill separator if not first string. 54 if (current_idx != 0) { 55 *dst = separator; 56 ++dst; 57 ++current_idx; 58 } 59 60 // Fill content of the string. 61 memcpy(dst, ref.str, ref.len); 62 current_idx += ref.len; 63 } 64 offset_.push_back(offset_.back() + total_len); 65 } 66 67 void DynamicBuffer::WriteToTensor(TfLiteTensor* tensor) { 68 // Allocate sufficient memory to tensor buffer. 69 int32_t num_strings = offset_.size() - 1; 70 // Total bytes include: 71 // * size of content (data_.size) 72 // * offset of each tensor (sizeof(int32_t) * num_strings) 73 // * length of whole buffer (int32_t) 74 // * num of strings (int32_t). 75 int32_t bytes = data_.size() // size of content 76 + sizeof(int32_t) * (num_strings + 2); // size of header 77 78 // Output tensor will take over the ownership of tensor_buffer, and free it 79 // during Interpreter destruction. 80 char* tensor_buffer = static_cast<char*>(malloc(bytes)); 81 82 // Set num of string 83 memcpy(tensor_buffer, &num_strings, sizeof(int32_t)); 84 85 // Set offset of strings. 86 int32_t start = sizeof(int32_t) * (num_strings + 2); 87 for (int i = 0; i < offset_.size(); i++) { 88 int32_t offset = start + offset_[i]; 89 memcpy(tensor_buffer + sizeof(int32_t) * (i + 1), &offset, sizeof(int32_t)); 90 } 91 92 // Copy data of strings. 93 memcpy(tensor_buffer + start, data_.data(), data_.size()); 94 95 // Set tensor content pointer to tensor_buffer, and release original data. 96 auto dims = TfLiteIntArrayCreate(1); 97 dims->data[0] = num_strings; 98 TfLiteTensorReset(tensor->type, tensor->name, dims, tensor->params, 99 tensor_buffer, bytes, kTfLiteDynamic, tensor->allocation, 100 tensor); 101 } 102 103 int GetStringCount(const TfLiteTensor* tensor) { 104 // The first integers in the raw buffer is the number of strings. 105 return *GetIntPtr(tensor->data.raw); 106 } 107 108 StringRef GetString(const TfLiteTensor* tensor, int string_index) { 109 int32_t* offset = 110 GetIntPtr(tensor->data.raw + sizeof(int32_t) * (string_index + 1)); 111 return { 112 tensor->data.raw + (*offset), 113 (*(offset + 1)) - (*offset), 114 }; 115 } 116 117 } // namespace tflite 118