string_util.cc 4.3 KB

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