4
4
5
5
#include " openvino/runtime/string_aligned_buffer.hpp"
6
6
7
+ #include < numeric>
8
+
9
+ #include " openvino/core/type/element_type.hpp"
7
10
#include " openvino/runtime/aligned_buffer.hpp"
8
11
12
+ namespace {
13
+ void aux_unpack_string_tensor (const char * data, size_t size, std::shared_ptr<ov::StringAlignedBuffer>& string_buffer) {
14
+ // unpack string tensor
15
+ // packed format is the following:
16
+ // <num_string>, <1st string offset>,..., <nth string offset>, <1st string raw format>,..., <nth string raw format>
17
+ // check the format of the input bitstream representing the string tensor
18
+ OPENVINO_ASSERT (size >= 4 , " Incorrect packed string tensor format: no batch size in the packed string tensor" );
19
+ const int32_t * pindices = reinterpret_cast <const int32_t *>(data);
20
+ int32_t num_strings = pindices[0 ];
21
+ OPENVINO_ASSERT (int32_t (size) >= 4 + 4 + 4 * num_strings,
22
+ " Incorrect packed string tensor format: the packed string tensor must contain first "
23
+ " string offset and end indices" );
24
+ const int32_t * begin_ids = pindices + 1 ;
25
+ const int32_t * end_ids = pindices + 2 ;
26
+ const char * symbols = reinterpret_cast <const char *>(pindices + 2 + num_strings);
27
+
28
+ // allocate StringAlignedBuffer to store unpacked strings in std::string objects
29
+ // SharedBuffer to read byte stream is not applicable because we need unpacked format for strings
30
+ string_buffer = std::make_shared<ov::StringAlignedBuffer>(
31
+ num_strings,
32
+ ov::element::string.size () * num_strings,
33
+ 64 , // host alignment used the same as in creation of buffer for Constant
34
+ true );
35
+ std::string* src_strings = static_cast <std::string*>(string_buffer->get_ptr ());
36
+ for (int32_t idx = 0 ; idx < num_strings; ++idx) {
37
+ src_strings[idx] = std::string (symbols + begin_ids[idx], symbols + end_ids[idx]);
38
+ }
39
+ }
40
+
41
+ void aux_get_header (const std::shared_ptr<ov::StringAlignedBuffer>& string_aligned_buffer_ptr,
42
+ std::shared_ptr<uint8_t >& header,
43
+ size_t & header_size) {
44
+ OPENVINO_ASSERT (string_aligned_buffer_ptr, " StringAlignedBuffer pointer is nullptr" );
45
+ // packed format is the following:
46
+ // <num_string>, <1st string offset>,..., <nth string offset>, <1st string raw format>,..., <nth rawformat>
47
+ auto num_elements = string_aligned_buffer_ptr->get_num_elements ();
48
+ auto strings = reinterpret_cast <std::string*>(string_aligned_buffer_ptr->get_ptr ());
49
+
50
+ // first run over all elements: calculate total memory required to hold all strings
51
+ header_size = sizeof (int32_t ) * (1 + 1 + num_elements);
52
+ header = std::shared_ptr<uint8_t >(new uint8_t [header_size], std::default_delete<uint8_t []>());
53
+
54
+ int32_t * pindices = reinterpret_cast <int32_t *>(header.get ());
55
+ pindices[0 ] = int32_t (num_elements);
56
+ pindices[1 ] = 0 ;
57
+ pindices += 2 ;
58
+ size_t current_symbols_pos = 0 ;
59
+
60
+ for (size_t ind = 0 ; ind < num_elements; ++ind) {
61
+ auto str = strings[ind];
62
+ current_symbols_pos += str.size ();
63
+ *pindices = int32_t (current_symbols_pos);
64
+ ++pindices;
65
+ }
66
+ }
67
+
68
+ void aux_get_raw_string_by_index (const std::shared_ptr<ov::StringAlignedBuffer>& string_aligned_buffer_ptr,
69
+ const char *& raw_string_ptr,
70
+ size_t & raw_string_size,
71
+ size_t string_ind) {
72
+ OPENVINO_ASSERT (string_aligned_buffer_ptr, " StringAlignedBuffer pointer is nullptr" );
73
+ OPENVINO_ASSERT (string_ind < string_aligned_buffer_ptr->get_num_elements (),
74
+ " Incorrect packed string tensor format: no batch size in the packed string tensor" );
75
+ const std::string* strings = reinterpret_cast <const std::string*>(string_aligned_buffer_ptr->get_ptr ());
76
+ raw_string_ptr = strings[string_ind].data ();
77
+ raw_string_size = strings[string_ind].size ();
78
+ }
79
+ } // namespace
80
+
9
81
namespace ov {
10
82
StringAlignedBuffer::StringAlignedBuffer (size_t num_elements, size_t byte_size, size_t alignment, bool initialize)
11
83
: AlignedBuffer(byte_size, alignment),
@@ -29,4 +101,62 @@ StringAlignedBuffer::~StringAlignedBuffer() {
29
101
}
30
102
}
31
103
104
+ SharedStringAlignedBuffer::SharedStringAlignedBuffer (char * ptr, size_t size) {
105
+ m_allocated_buffer = ptr;
106
+ m_aligned_buffer = ptr;
107
+ m_byte_size = size;
108
+ m_num_elements = size / ov::element::string.size ();
109
+ }
110
+
111
+ AttributeAdapter<std::shared_ptr<ov::StringAlignedBuffer>>::AttributeAdapter(
112
+ std::shared_ptr<ov::StringAlignedBuffer>& value)
113
+ : DirectValueAccessor<std::shared_ptr<ov::StringAlignedBuffer>>(value),
114
+ m_header (nullptr ),
115
+ m_header_size(0 ) {}
116
+
117
+ std::shared_ptr<ov::StringAlignedBuffer>
118
+ AttributeAdapter<std::shared_ptr<ov::StringAlignedBuffer>>::unpack_string_tensor(const char * packed_string_tensor_ptr,
119
+ size_t packed_string_tensor_size) {
120
+ std::shared_ptr<ov::StringAlignedBuffer> string_aligned_buffer;
121
+ aux_unpack_string_tensor (packed_string_tensor_ptr, packed_string_tensor_size, string_aligned_buffer);
122
+ return string_aligned_buffer;
123
+ }
124
+
125
+ void AttributeAdapter<std::shared_ptr<ov::StringAlignedBuffer>>::get_header(std::shared_ptr<uint8_t >& header,
126
+ size_t & header_size) {
127
+ if (!m_header) {
128
+ aux_get_header (m_ref, m_header, m_header_size);
129
+ }
130
+ header = m_header;
131
+ header_size = m_header_size;
132
+ }
133
+
134
+ void AttributeAdapter<std::shared_ptr<ov::StringAlignedBuffer>>::get_raw_string_by_index(const char *& raw_string_ptr,
135
+ size_t & raw_string_size,
136
+ size_t string_ind) {
137
+ aux_get_raw_string_by_index (m_ref, raw_string_ptr, raw_string_size, string_ind);
138
+ }
139
+
140
+ AttributeAdapter<std::shared_ptr<ov::SharedStringAlignedBuffer>>::AttributeAdapter(
141
+ std::shared_ptr<ov::SharedStringAlignedBuffer>& value)
142
+ : DirectValueAccessor<std::shared_ptr<ov::SharedStringAlignedBuffer>>(value),
143
+ m_header (nullptr ),
144
+ m_header_size(0 ) {}
145
+
146
+ void AttributeAdapter<std::shared_ptr<ov::SharedStringAlignedBuffer>>::get_header(std::shared_ptr<uint8_t >& header,
147
+ size_t & header_size) {
148
+ if (!m_header) {
149
+ aux_get_header (m_ref, m_header, m_header_size);
150
+ }
151
+ header = m_header;
152
+ header_size = m_header_size;
153
+ }
154
+
155
+ void AttributeAdapter<std::shared_ptr<ov::SharedStringAlignedBuffer>>::get_raw_string_by_index(
156
+ const char *& raw_string_ptr,
157
+ size_t & raw_string_size,
158
+ size_t string_ind) {
159
+ aux_get_raw_string_by_index (m_ref, raw_string_ptr, raw_string_size, string_ind);
160
+ }
161
+
32
162
} // namespace ov
0 commit comments