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/lite/string_util.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <vector>
21 #include "tensorflow/lite/c/c_api_internal.h"
22
23 namespace tflite {
24 namespace {
25
26 // Convenient method to get pointer to int32_t.
GetIntPtr(const char * ptr)27 const int32_t* GetIntPtr(const char* ptr) {
28 return reinterpret_cast<const int32_t*>(ptr);
29 }
30
31 } // namespace
32
AddString(const char * str,size_t len)33 void DynamicBuffer::AddString(const char* str, size_t len) {
34 data_.resize(data_.size() + len);
35 memcpy(data_.data() + offset_.back(), str, len);
36 offset_.push_back(offset_.back() + len);
37 }
38
AddString(const StringRef & string)39 void DynamicBuffer::AddString(const StringRef& string) {
40 AddString(string.str, string.len);
41 }
42
AddJoinedString(const std::vector<StringRef> & strings,char separator)43 void DynamicBuffer::AddJoinedString(const std::vector<StringRef>& strings,
44 char separator) {
45 // Resize the data buffer.
46 int total_len = strings.size() - 1;
47 for (StringRef ref : strings) {
48 total_len += ref.len;
49 }
50 data_.resize(data_.size() + total_len);
51
52 int current_idx = 0;
53 for (StringRef ref : strings) {
54 char* dst = data_.data() + offset_.back() + current_idx;
55
56 // Fill separator if not first string.
57 if (current_idx != 0) {
58 *dst = separator;
59 ++dst;
60 ++current_idx;
61 }
62
63 // Fill content of the string.
64 memcpy(dst, ref.str, ref.len);
65 current_idx += ref.len;
66 }
67 offset_.push_back(offset_.back() + total_len);
68 }
69
WriteToBuffer(char ** buffer)70 int DynamicBuffer::WriteToBuffer(char** buffer) {
71 // Allocate sufficient memory to tensor buffer.
72 int32_t num_strings = offset_.size() - 1;
73 // Total bytes include:
74 // * size of content (data_.size)
75 // * offset of each tensor (sizeof(int32_t) * num_strings)
76 // * length of whole buffer (int32_t)
77 // * num of strings (int32_t).
78 int32_t bytes = data_.size() // size of content
79 + sizeof(int32_t) * (num_strings + 2); // size of header
80
81 // Caller will take ownership of buffer.
82 *buffer = reinterpret_cast<char*>(malloc(bytes));
83
84 // Set num of string
85 memcpy(*buffer, &num_strings, sizeof(int32_t));
86
87 // Set offset of strings.
88 int32_t start = sizeof(int32_t) * (num_strings + 2);
89 for (int i = 0; i < offset_.size(); i++) {
90 int32_t offset = start + offset_[i];
91 memcpy(*buffer + sizeof(int32_t) * (i + 1), &offset, sizeof(int32_t));
92 }
93
94 // Copy data of strings.
95 memcpy(*buffer + start, data_.data(), data_.size());
96 return bytes;
97 }
98
WriteToTensorAsVector(TfLiteTensor * tensor)99 void DynamicBuffer::WriteToTensorAsVector(TfLiteTensor* tensor) {
100 auto dims = TfLiteIntArrayCreate(1);
101 dims->data[0] = offset_.size() - 1; // Store number of strings.
102 WriteToTensor(tensor, dims);
103 }
104
WriteToTensor(TfLiteTensor * tensor,TfLiteIntArray * new_shape)105 void DynamicBuffer::WriteToTensor(TfLiteTensor* tensor,
106 TfLiteIntArray* new_shape) {
107 char* tensor_buffer;
108 int bytes = WriteToBuffer(&tensor_buffer);
109
110 if (new_shape == nullptr) {
111 new_shape = TfLiteIntArrayCopy(tensor->dims);
112 }
113
114 // Set tensor content pointer to tensor_buffer, and release original data.
115 TfLiteTensorReset(tensor->type, tensor->name, new_shape, tensor->params,
116 tensor_buffer, bytes, kTfLiteDynamic, tensor->allocation,
117 tensor->is_variable, tensor);
118 }
119
GetStringCount(const char * raw_buffer)120 int GetStringCount(const char* raw_buffer) {
121 // The first integers in the raw buffer is the number of strings.
122 return *GetIntPtr(raw_buffer);
123 }
124
GetStringCount(const TfLiteTensor * tensor)125 int GetStringCount(const TfLiteTensor* tensor) {
126 // The first integers in the raw buffer is the number of strings.
127 return GetStringCount(tensor->data.raw);
128 }
129
GetString(const char * raw_buffer,int string_index)130 StringRef GetString(const char* raw_buffer, int string_index) {
131 const int32_t* offset =
132 GetIntPtr(raw_buffer + sizeof(int32_t) * (string_index + 1));
133 return {
134 raw_buffer + (*offset),
135 (*(offset + 1)) - (*offset),
136 };
137 }
138
GetString(const TfLiteTensor * tensor,int string_index)139 StringRef GetString(const TfLiteTensor* tensor, int string_index) {
140 return GetString(tensor->data.raw, string_index);
141 }
142
143 } // namespace tflite
144