| 29 | namespace py = pybind11; |
| 30 | |
| 31 | static std::string FormatStrFromType(DALIDataType type) { |
| 32 | // handle common types in a switch |
| 33 | switch (type) { |
| 34 | case DALI_INT8: |
| 35 | return "=b"; |
| 36 | case DALI_UINT8: |
| 37 | return "=B"; |
| 38 | case DALI_INT16: |
| 39 | return "=h"; |
| 40 | case DALI_UINT16: |
| 41 | return "=H"; |
| 42 | case DALI_INT32: |
| 43 | return "=i"; |
| 44 | case DALI_UINT32: |
| 45 | return "=I"; |
| 46 | case DALI_INT64: |
| 47 | return "=q"; |
| 48 | case DALI_UINT64: |
| 49 | return "=Q"; |
| 50 | case DALI_FLOAT: |
| 51 | return "=f"; |
| 52 | case DALI_FLOAT16: |
| 53 | return "=e"; |
| 54 | case DALI_FLOAT64: |
| 55 | return "=d"; |
| 56 | case DALI_BOOL: |
| 57 | return "=?"; |
| 58 | case DALI_DATA_TYPE: |
| 59 | case DALI_IMAGE_TYPE: |
| 60 | case DALI_INTERP_TYPE: |
| 61 | throw DaliTypeError( |
| 62 | "DALI enum types cannot be used with buffer protocol " |
| 63 | "when they are returned as Tensors or TensorLists from DALI pipeline. " |
| 64 | "You can use `nvidia.dali.fn.cast` to convert those values to an integral type."); |
| 65 | // As an alternative, to allow the usage of tensors containing DALI enums (printing, use with |
| 66 | // buffer protocol, numpy conversion etc), we can return format specifier for the underlying |
| 67 | // type here. This would allow access to the actual numeric values, for example: |
| 68 | // case DALI_DATA_TYPE: |
| 69 | // return |
| 70 | // FormatStrFromType(TypeTable::GetTypeInfo<std::underlying_type_t<DALIDataType>>().id()); |
| 71 | default: |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | // fall back to 'if' ladder |
| 76 | |
| 77 | if (IsType<int8_t>(type)) { |
| 78 | return "=b"; |
| 79 | } else if (IsType<uint8_t>(type)) { |
| 80 | return "=B"; |
| 81 | } else if (IsType<char>(type)) { |
| 82 | return "=c"; |
| 83 | } else if (IsType<int16_t>(type)) { |
| 84 | return "=h"; |
| 85 | } else if (IsType<uint16_t>(type)) { |
| 86 | return "=H"; |
| 87 | } else if (IsType<int32_t>(type) || |
| 88 | (IsType<long>(type) && sizeof(long) == sizeof(int32_t))) { // NOLINT |
no test coverage detected