| 48 | } |
| 49 | |
| 50 | Status ByteSwapTensor(Tensor* t) { |
| 51 | size_t bytes_per_elem = 0; |
| 52 | int array_len = t->NumElements(); |
| 53 | |
| 54 | switch (t->dtype()) { |
| 55 | // Types that don't need byte-swapping |
| 56 | case DT_STRING: |
| 57 | case DT_QINT8: |
| 58 | case DT_QUINT8: |
| 59 | case DT_BOOL: |
| 60 | case DT_UINT8: |
| 61 | case DT_INT8: |
| 62 | return Status::OK(); |
| 63 | |
| 64 | // 16-bit types |
| 65 | case DT_BFLOAT16: |
| 66 | case DT_HALF: |
| 67 | case DT_QINT16: |
| 68 | case DT_QUINT16: |
| 69 | case DT_UINT16: |
| 70 | case DT_INT16: |
| 71 | bytes_per_elem = 2; |
| 72 | break; |
| 73 | |
| 74 | // 32-bit types |
| 75 | case DT_FLOAT: |
| 76 | case DT_INT32: |
| 77 | case DT_QINT32: |
| 78 | case DT_UINT32: |
| 79 | bytes_per_elem = 4; |
| 80 | break; |
| 81 | |
| 82 | // 64-bit types |
| 83 | case DT_INT64: |
| 84 | case DT_DOUBLE: |
| 85 | case DT_UINT64: |
| 86 | bytes_per_elem = 8; |
| 87 | break; |
| 88 | |
| 89 | // Complex types need special handling |
| 90 | case DT_COMPLEX64: |
| 91 | bytes_per_elem = 4; |
| 92 | array_len *= 2; |
| 93 | break; |
| 94 | |
| 95 | case DT_COMPLEX128: |
| 96 | bytes_per_elem = 8; |
| 97 | array_len *= 2; |
| 98 | break; |
| 99 | |
| 100 | // Types that ought to be supported in the future |
| 101 | case DT_RESOURCE: |
| 102 | case DT_VARIANT: |
| 103 | return errors::Unimplemented( |
| 104 | "Byte-swapping not yet implemented for tensors with dtype ", |
| 105 | t->dtype()); |
| 106 | |
| 107 | // Byte-swapping shouldn't make sense for other dtypes. |