MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ConvertFloatBuffer

Function ConvertFloatBuffer

tensorflow/compiler/mlir/lite/flatbuffer_import.cc:357–401  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

355}
356
357StatusOr<mlir::ElementsAttr> ConvertFloatBuffer(
358 mlir::RankedTensorType shaped_type, mlir::FloatType elem_type,
359 const std::vector<uint8_t>& buffer) {
360 size_t bytes_len = buffer.size();
361
362 // The bytes of floats are stored little-endian.
363 switch (elem_type.getWidth()) {
364 case 16: {
365 assert(bytes_len % 2 == 0);
366 size_t elem_count = bytes_len / 2;
367 std::vector<llvm::APFloat> values;
368 values.reserve(elem_count);
369
370 const char* data = reinterpret_cast<const char*>(buffer.data());
371 auto& semantics = elem_type.getFloatSemantics();
372
373 for (int i = 0; i < elem_count; i++) {
374 uint16_t bit_repr =
375 llvm::support::endian::readNext<uint16_t, llvm::support::little,
376 llvm::support::unaligned>(data);
377 llvm::APInt int_repr(16, bit_repr);
378 values.emplace_back(semantics, int_repr);
379 }
380
381 return DenseElementsAttr::get(shaped_type, values);
382 }
383 case 32: {
384 assert(bytes_len % 4 == 0);
385 size_t elem_count = bytes_len / 4;
386 std::vector<float> values;
387 values.reserve(elem_count);
388
389 const char* data = reinterpret_cast<const char*>(buffer.data());
390
391 for (int i = 0; i < elem_count; i++) {
392 uint32_t bit_repr =
393 llvm::support::endian::readNext<uint32_t, llvm::support::little,
394 llvm::support::unaligned>(data);
395 values.push_back(absl::bit_cast<float>(bit_repr));
396 }
397 return DenseElementsAttr::get(shaped_type, ArrayRef<float>(values));
398 }
399 }
400 return errors::InvalidArgument("unsupported bit width", elem_type.getWidth());
401}
402
403StatusOr<mlir::ElementsAttr> ConvertIntBuffer(
404 mlir::RankedTensorType shaped_type, mlir::Type elem_type,

Callers 1

BuildConstOpFunction · 0.85

Calls 7

InvalidArgumentFunction · 0.85
getFunction · 0.50
sizeMethod · 0.45
reserveMethod · 0.45
dataMethod · 0.45
emplace_backMethod · 0.45
push_backMethod · 0.45

Tested by

no test coverage detected