aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/ignite/kernels/ignite_binary_object_parser.cc
blob: bf0ef8766edf5b6c68d815a493a30e6c645b2f01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "ignite_binary_object_parser.h"

namespace ignite {

tensorflow::Status BinaryObjectParser::Parse(
    uint8_t*& ptr, std::vector<tensorflow::Tensor>& out_tensors,
    std::vector<int32_t>& types) {
  uint8_t object_type_id = *ptr;
  ptr += 1;

  switch (object_type_id) {
    case BYTE: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_UINT8, {});
      tensor.scalar<tensorflow::uint8>()() = *((uint8_t*)ptr);
      ptr += 1;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case SHORT: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT16, {});
      tensor.scalar<tensorflow::int16>()() = *((int16_t*)ptr);
      ptr += 2;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case INT: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT32, {});
      tensor.scalar<tensorflow::int32>()() = *((int32_t*)ptr);
      ptr += 4;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case LONG: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT64, {});
      tensor.scalar<tensorflow::int64>()() = *((int64_t*)ptr);
      ptr += 8;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case FLOAT: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_FLOAT, {});
      tensor.scalar<float>()() = *((float*)ptr);
      ptr += 4;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case DOUBLE: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_DOUBLE, {});
      tensor.scalar<double>()() = *((double*)ptr);
      ptr += 8;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case UCHAR: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_UINT16, {});
      tensor.scalar<tensorflow::uint16>()() = *((uint16_t*)ptr);
      ptr += 2;
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case BOOL: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_BOOL, {});
      tensor.scalar<bool>()() = *((bool*)ptr);
      ptr += 1;
      out_tensors.emplace_back(std::move(tensor));

      break;
    }
    case STRING: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_STRING, {});
      tensor.scalar<std::string>()() = std::string((char*)ptr, length);
      ptr += length;
      out_tensors.emplace_back(std::move(tensor));

      break;
    }
    case DATE: {
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT64, {});
      tensor.scalar<tensorflow::int64>()() = *((int64_t*)ptr);
      ptr += 8;
      out_tensors.emplace_back(std::move(tensor));

      break;
    }
    case BYTE_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_UINT8,
                                tensorflow::TensorShape({length}));

      uint8_t* arr = (uint8_t*)ptr;
      ptr += length;

      std::copy_n(arr, length, tensor.flat<tensorflow::uint8>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case SHORT_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT16,
                                tensorflow::TensorShape({length}));

      int16_t* arr = (int16_t*)ptr;
      ptr += length * 2;

      std::copy_n(arr, length, tensor.flat<tensorflow::int16>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case INT_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT32,
                                tensorflow::TensorShape({length}));

      int32_t* arr = (int32_t*)ptr;
      ptr += length * 4;

      std::copy_n(arr, length, tensor.flat<tensorflow::int32>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case LONG_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT64,
                                tensorflow::TensorShape({length}));

      int64_t* arr = (int64_t*)ptr;
      ptr += length * 8;

      std::copy_n(arr, length, tensor.flat<tensorflow::int64>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case FLOAT_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_FLOAT,
                                tensorflow::TensorShape({length}));

      float* arr = (float*)ptr;
      ptr += 4 * length;

      std::copy_n(arr, length, tensor.flat<float>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case DOUBLE_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_DOUBLE,
                                tensorflow::TensorShape({length}));

      double* arr = (double*)ptr;
      ptr += 8 * length;

      std::copy_n(arr, length, tensor.flat<double>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case UCHAR_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_UINT16,
                                tensorflow::TensorShape({length}));

      uint16_t* arr = (uint16_t*)ptr;
      ptr += length * 2;

      std::copy_n(arr, length, tensor.flat<tensorflow::uint16>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case BOOL_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_BOOL,
                                tensorflow::TensorShape({length}));

      bool* arr = (bool*)ptr;
      ptr += length;

      std::copy_n(arr, length, tensor.flat<bool>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case STRING_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_STRING,
                                tensorflow::TensorShape({length}));

      for (int32_t i = 0; i < length; i++) {
        int32_t str_length = *((int32_t*)ptr);
        ptr += 4;
        const int8_t* str = (const int8_t*)ptr;
        ptr += str_length;
        tensor.vec<std::string>()(i) = std::string((char*)str, str_length);
      }

      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case DATE_ARR: {
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      tensorflow::Tensor tensor(tensorflow::cpu_allocator(),
                                tensorflow::DT_INT64,
                                tensorflow::TensorShape({length}));
      int64_t* arr = (int64_t*)ptr;
      ptr += length * 8;

      std::copy_n(arr, length, tensor.flat<tensorflow::int64>().data());
      out_tensors.emplace_back(std::move(tensor));
      break;
    }
    case WRAPPED_OBJ: {
      int32_t byte_arr_size = *((int32_t*)ptr);
      ptr += 4;

      tensorflow::Status status = Parse(ptr, out_tensors, types);
      if (!status.ok()) return status;

      int32_t offset = *((int32_t*)ptr);
      ptr += 4;

      break;
    }
    case COMPLEX_OBJ: {
      uint8_t version = *ptr;
      ptr += 1;
      int16_t flags = *((int16_t*)ptr);  // USER_TYPE = 1, HAS_SCHEMA = 2
      ptr += 2;
      int32_t type_id = *((int32_t*)ptr);
      ptr += 4;
      int32_t hash_code = *((int32_t*)ptr);
      ptr += 4;
      int32_t length = *((int32_t*)ptr);
      ptr += 4;
      int32_t schema_id = *((int32_t*)ptr);
      ptr += 4;
      int32_t schema_offset = *((int32_t*)ptr);
      ptr += 4;

      uint8_t* end = ptr + schema_offset - 24;
      int32_t i = 0;
      while (ptr < end) {
        i++;
        tensorflow::Status status = Parse(ptr, out_tensors, types);
        if (!status.ok()) return status;
      }

      ptr += (length - schema_offset);

      break;
    }
    default: {
      return tensorflow::errors::Internal("Unknowd binary type (type id ",
                                          (int)object_type_id, ")");
    }
  }

  return tensorflow::Status::OK();
}

}  // namespace ignite