332 std::ofstream out(outputTensorFileName, std::ofstream::binary);
337 std::string shapeStr =
"(";
340 shapeStr = shapeStr + std::to_string(shape[i]) +
", ";
342 shapeStr = shapeStr + std::to_string(shape[shape.
GetNumDimensions()-1]) +
")";
345 std::string endianChar = (*(
reinterpret_cast<char *
>(&i))) ?
"<" :
">";
347 std::string fortranOrder =
"False";
348 std::string headerStr =
"{'descr': '" + endianChar + dataTypeStr +
349 "', 'fortran_order': " + fortranOrder +
350 ", 'shape': " + shapeStr +
", }";
361 uint8_t major_version = 1;
364 if (length >= 255 * 255)
371 size_t padding_length = 16 - length % 16;
372 std::string padding(padding_length,
' ');
376 out.put(major_version);
380 if (major_version == 1)
382 auto header_len =
static_cast<uint16_t
>(headerStr.length() + padding.length() + 1);
384 std::array<uint8_t, 2> header_len_16{
static_cast<uint8_t
>((header_len >> 0) & 0xff),
385 static_cast<uint8_t
>((header_len >> 8) & 0xff)};
386 out.write(
reinterpret_cast<char *
>(header_len_16.data()), 2);
390 auto header_len =
static_cast<uint32_t
>(headerStr.length() + padding.length() + 1);
392 std::array<uint8_t, 4> header_len_32{
393 static_cast<uint8_t
>((header_len >> 0) & 0xff),
static_cast<uint8_t
>((header_len >> 8) & 0xff),
394 static_cast<uint8_t
>((header_len >> 16) & 0xff),
static_cast<uint8_t
>((header_len >> 24) & 0xff)};
395 out.write(
reinterpret_cast<char *
>(header_len_32.data()), 4);
398 out << headerStr << padding <<
'\n';
402 out.write(
reinterpret_cast<const char *
>(array),
sizeof(T) * numElements);