Repository: microsoft/onnxruntime
File: onnxruntime/core/framework/tensorprotoutils.cc (C++)

Code before:
    };
    auto pos = tensorproto_data_size.find(tensor.data_type());
    if (pos == tensorproto_data_size.end()) {
      return;
    }
    element_size = pos->second;
    if (element_size <= 1) {
      return;
    }
    num_elements = tensor.raw_data().size() / element_size;
    bytes = tensor.mutable_raw_data()->data();
  } else {  // tensor.has_raw_data()

    switch (tensor.data_type()) {
      case TensorProto_DataType_FLOAT:
        bytes = tensor.mutable_float_data()->mutable_data();
        num_elements = tensor.float_data_size();
        element_size = sizeof(float);
        break;

      case TensorProto_DataType_UINT8:
      case TensorProto_DataType_INT8:
        bytes = tensor.mutable_int32_data()->mutable_data();
        num_elements = tensor.int32_data_size();
        element_size = sizeof(uint8_t);
        break;

      case TensorProto_DataType_UINT16:
      case TensorProto_DataType_INT16:
      case TensorProto_DataType_FLOAT16:
      case TensorProto_DataType_BFLOAT16:
      case TensorProto_DataType_INT32:
        bytes = tensor.mutable_int32_data()->mutable_data();
        num_elements = tensor.int32_data_size();
        // We are setting this to int32_t size because we need to swap all 4 bytes
        // to represent 16 bits within 32 bits correctly on a LE/BE system.
        element_size = sizeof(int32_t);
        break;

      // uint32_t is stored in uint64_t
      case TensorProto_DataType_UINT32:
      case TensorProto_DataType_UINT64:
        bytes = tensor.mutable_uint64_data()->mutable_data();
        num_elements = tensor.uint64_data_size();
        element_size = sizeof(uint64_t);
        break;

      case TensorProto_DataType_INT64:
        bytes = tensor.mutable_int64_data()->mutable_data();
        num_elements = tensor.int64_data_size();
        element_size = sizeof(int64_t);

Review comment (bug):
I think all datatypes with size == 1 and stored in int32 data, must be swapped. This includes BOOL.
Reason being, the meaningful byte happens to be on the wrong end. So this case must be merged with the below.

Size == 1 exception is only for raw data where they are stored contiguously.

Code after:
        {TensorProto_DataType_FLOAT16, sizeof(uint16_t)},
        {TensorProto_DataType_BFLOAT16, sizeof(uint16_t)},
        {TensorProto_DataType_INT32, sizeof(int32_t)},
        {TensorProto_DataType_UINT32, sizeof(uint32_t)},
        {TensorProto_DataType_UINT64, sizeof(uint64_t)},
        {TensorProto_DataType_INT64, sizeof(int64_t)},
        {TensorProto_DataType_DOUBLE, sizeof(double)},
        {TensorProto_DataType_BOOL, sizeof(uint8_t)},
        {TensorProto_DataType_FLOAT8E4M3FN, sizeof(uint8_t)},
        {TensorProto_DataType_FLOAT8E4M3FNUZ, sizeof(uint8_t)},
        {TensorProto_DataType_FLOAT8E5M2, sizeof(uint8_t)},
        {TensorProto_DataType_FLOAT8E5M2FNUZ, sizeof(uint8_t)},
        {TensorProto_DataType_UINT4, sizeof(uint8_t)},
        {TensorProto_DataType_INT4, sizeof(uint8_t)},
    };
    auto pos = tensorproto_data_size.find(tensor.data_type());
    if (pos == tensorproto_data_size.end()) {
      return;
    }
    element_size = pos->second;
    if (element_size == 1) {
      return;
    }
    num_elements = tensor.raw_data().size() / element_size;
    bytes = tensor.mutable_raw_data()->data();
  } else {  // HasRawData(tensor)

    switch (tensor.data_type()) {
      case TensorProto_DataType_FLOAT:
        bytes = tensor.mutable_float_data()->mutable_data();
        num_elements = tensor.float_data_size();
        element_size = sizeof(float);
        break;

      case TensorProto_DataType_BOOL:
      case TensorProto_DataType_UINT4:
      case TensorProto_DataType_INT4:
      case TensorProto_DataType_UINT8:
      case TensorProto_DataType_INT8:
      case TensorProto_DataType_UINT16:
      case TensorProto_DataType_INT16:
      case TensorProto_DataType_FLOAT16:
      case TensorProto_DataType_BFLOAT16:
      case TensorProto_DataType_FLOAT8E4M3FN:
      case TensorProto_DataType_FLOAT8E4M3FNUZ:
      case TensorProto_DataType_FLOAT8E5M2:
      case TensorProto_DataType_FLOAT8E5M2FNUZ:
      case TensorProto_DataType_INT32:
        bytes = tensor.mutable_int32_data()->mutable_data();
        num_elements = tensor.int32_data_size();
        // We are setting this to int32_t size because we need to swap all 4 bytes