|
| 1 | +#include <torch/nativert/graph/TensorMeta.h> |
| 2 | + |
| 3 | +#include <c10/util/Logging.h> |
| 4 | + |
| 5 | +namespace torch::nativert { |
| 6 | + |
| 7 | +c10::ScalarType convertJsonScalarType( |
| 8 | + const torch::_export::ScalarType& scalarType) { |
| 9 | + switch (scalarType) { |
| 10 | + case torch::_export::ScalarType::UNKNOWN: |
| 11 | + TORCH_CHECK(false, "scalar type is not properly set"); |
| 12 | + case torch::_export::ScalarType::BYTE: |
| 13 | + return c10::ScalarType::Byte; |
| 14 | + case torch::_export::ScalarType::CHAR: |
| 15 | + return c10::ScalarType::Char; |
| 16 | + case torch::_export::ScalarType::SHORT: |
| 17 | + return c10::ScalarType::Short; |
| 18 | + case torch::_export::ScalarType::INT: |
| 19 | + return c10::ScalarType::Int; |
| 20 | + case torch::_export::ScalarType::LONG: |
| 21 | + return c10::ScalarType::Long; |
| 22 | + case torch::_export::ScalarType::HALF: |
| 23 | + return c10::ScalarType::Half; |
| 24 | + case torch::_export::ScalarType::FLOAT: |
| 25 | + return c10::ScalarType::Float; |
| 26 | + case torch::_export::ScalarType::DOUBLE: |
| 27 | + return c10::ScalarType::Double; |
| 28 | + case torch::_export::ScalarType::COMPLEXHALF: |
| 29 | + return c10::ScalarType::ComplexHalf; |
| 30 | + case torch::_export::ScalarType::COMPLEXFLOAT: |
| 31 | + return c10::ScalarType::ComplexFloat; |
| 32 | + case torch::_export::ScalarType::COMPLEXDOUBLE: |
| 33 | + return c10::ScalarType::ComplexDouble; |
| 34 | + case torch::_export::ScalarType::BOOL: |
| 35 | + return c10::ScalarType::Bool; |
| 36 | + case torch::_export::ScalarType::BFLOAT16: |
| 37 | + return c10::ScalarType::BFloat16; |
| 38 | + case torch::_export::ScalarType::UINT16: |
| 39 | + return c10::ScalarType::UInt16; |
| 40 | + case torch::_export::ScalarType::FLOAT8E4M3FN: |
| 41 | + return c10::ScalarType::Float8_e4m3fn; |
| 42 | + case torch::_export::ScalarType::FLOAT8E5M2: |
| 43 | + return c10::ScalarType::Float8_e5m2; |
| 44 | + default: |
| 45 | + TORCH_CHECK(false, "unknown scalar type", static_cast<int>(scalarType)); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +c10::MemoryFormat convertJsonMemoryFormat( |
| 50 | + const torch::_export::MemoryFormat& memoryFormat) { |
| 51 | + switch (memoryFormat) { |
| 52 | + case torch::_export::MemoryFormat::Unknown: |
| 53 | + TORCH_CHECK(false, "got unknown scalar type"); |
| 54 | + case torch::_export::MemoryFormat::ContiguousFormat: |
| 55 | + return c10::MemoryFormat::Contiguous; |
| 56 | + case torch::_export::MemoryFormat::ChannelsLast: |
| 57 | + return c10::MemoryFormat::ChannelsLast; |
| 58 | + case torch::_export::MemoryFormat::ChannelsLast3d: |
| 59 | + return c10::MemoryFormat::ChannelsLast3d; |
| 60 | + case torch::_export::MemoryFormat::PreserveFormat: |
| 61 | + return c10::MemoryFormat::Preserve; |
| 62 | + default: |
| 63 | + TORCH_CHECK( |
| 64 | + false, "unknown memory format", static_cast<int>(memoryFormat)); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +c10::Layout convertJsonLayout(const torch::_export::Layout& layout) { |
| 69 | + switch (layout) { |
| 70 | + case torch::_export::Layout::Unknown: |
| 71 | + TORCH_CHECK(false, "got unknown layout"); |
| 72 | + case torch::_export::Layout::SparseCoo: |
| 73 | + // TODO is this the right translation |
| 74 | + return c10::Layout::Sparse; |
| 75 | + case torch::_export::Layout::SparseCsr: |
| 76 | + return c10::Layout::SparseCsr; |
| 77 | + case torch::_export::Layout::SparseCsc: |
| 78 | + return c10::Layout::SparseCsc; |
| 79 | + case torch::_export::Layout::SparseBsr: |
| 80 | + return c10::Layout::SparseBsr; |
| 81 | + case torch::_export::Layout::SparseBsc: |
| 82 | + return c10::Layout::SparseBsc; |
| 83 | + case torch::_export::Layout::_mkldnn: |
| 84 | + return c10::Layout::Mkldnn; |
| 85 | + case torch::_export::Layout::Strided: |
| 86 | + return c10::Layout::Strided; |
| 87 | + default: |
| 88 | + TORCH_CHECK(false, "unknown layout", static_cast<int>(layout)); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +c10::Device convertJsonDevice(const torch::_export::Device& device) { |
| 93 | + c10::Device d(device.get_type()); |
| 94 | + if (auto index = device.get_index()) { |
| 95 | + d.set_index(*index); |
| 96 | + } |
| 97 | + return d; |
| 98 | +} |
| 99 | + |
| 100 | +TensorMeta::TensorMeta(const torch::_export::TensorMeta& tensorMeta) |
| 101 | + : device_(convertJsonDevice(tensorMeta.get_device())) { |
| 102 | + dtype_ = convertJsonScalarType(tensorMeta.get_dtype()); |
| 103 | + layout_ = convertJsonLayout(tensorMeta.get_layout()); |
| 104 | + requiresGrad_ = tensorMeta.get_requires_grad(); |
| 105 | + |
| 106 | + if (tensorMeta.get_storage_offset().tag() == |
| 107 | + torch::_export::SymInt::Tag::AS_INT) { |
| 108 | + storage_offset_ = tensorMeta.get_storage_offset().get_as_int(); |
| 109 | + } else { |
| 110 | + CHECK(false) << "SymInt not supported yet"; |
| 111 | + } |
| 112 | + |
| 113 | + for (const auto& size : tensorMeta.get_sizes()) { |
| 114 | + if (size.tag() == torch::_export::SymInt::Tag::AS_INT) { |
| 115 | + int64_t val = size.get_as_int(); |
| 116 | + sizes_.emplace_back(val); |
| 117 | + numel_ *= val; |
| 118 | + } else if (size.tag() == torch::_export::SymInt::Tag::AS_EXPR) { |
| 119 | + // TODO: it's still unclear how SymInt shape should be used in runtime |
| 120 | + // One potential use cases is for verifing inputs shape matches constrain |
| 121 | + // This would require unpacking the serialized constrain, which is NYI |
| 122 | + // |
| 123 | + // For the time being, we just set the symbolic dim to -1 |
| 124 | + hasSymbolicShape_ = true; |
| 125 | + sizes_.emplace_back(-1); |
| 126 | + numel_ = -1; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + for (const auto& stride : tensorMeta.get_strides()) { |
| 131 | + if (stride.tag() == torch::_export::SymInt::Tag::AS_INT) { |
| 132 | + strides_.emplace_back(stride.get_as_int()); |
| 133 | + } else if (stride.tag() == torch::_export::SymInt::Tag::AS_EXPR) { |
| 134 | + // TODO: it's still unclear how SymInt shape should be used in runtime |
| 135 | + // Setting symbolic shape to -1 for now |
| 136 | + hasSymbolicShape_ = true; |
| 137 | + strides_.emplace_back(-1); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace torch::nativert |
0 commit comments