|
| 1 | +#include "ATen/core/ivalue.h" |
| 2 | +#include "ATen/core/List.h" |
| 3 | +#include "core/util/prelude.h" |
| 4 | +#include "ATen/core/functional.h" |
| 5 | + |
| 6 | +namespace trtorch { |
| 7 | +namespace core { |
| 8 | +namespace conversion { |
| 9 | +namespace evaluators { |
| 10 | + |
| 11 | +//TODO: Switch back to PyTorch canonical implimentation |
| 12 | +c10::optional<torch::jit::IValue> toIValue(const torch::jit::Value* v) { |
| 13 | + if (v->node()->kind() != torch::jit::prim::Constant || v->type()->cast<c10::FunctionType>()) { |
| 14 | + return c10::nullopt; |
| 15 | + } |
| 16 | + const torch::jit::Node* node = v->node(); |
| 17 | + const c10::TypePtr& type = v->type(); |
| 18 | + if (type->isSubtypeOf(c10::TensorType::get())) { |
| 19 | + return node->t(c10::attr::value); |
| 20 | + } else if (type->isSubtypeOf(c10::BoolType::get())) { |
| 21 | + return (bool)node->i(c10::attr::value); |
| 22 | + } else if ( |
| 23 | + type->isSubtypeOf(c10::NumberType::get()) && |
| 24 | + node->kindOf(c10::attr::value) == torch::jit::AttributeKind::i) { |
| 25 | + return node->i(c10::attr::value); |
| 26 | + } else if ( |
| 27 | + type->isSubtypeOf(c10::NumberType::get()) && |
| 28 | + node->kindOf(c10::attr::value) == torch::jit::AttributeKind::f) { |
| 29 | + return node->f(c10::attr::value); |
| 30 | + } else if (type->isSubtypeOf(c10::ListType::ofInts())) { |
| 31 | + try { |
| 32 | + const auto& is = node->is(c10::attr::value); |
| 33 | + return is; |
| 34 | + } catch (const std::exception& ex) { |
| 35 | + const auto& ival = node->ival(c10::attr::value); |
| 36 | + return ival; |
| 37 | + } |
| 38 | + } else if (type->isSubtypeOf(c10::ListType::ofFloats())) { |
| 39 | + try { |
| 40 | + const auto& fs = node->fs(c10::attr::value); |
| 41 | + return fs; |
| 42 | + } catch (const std::exception& ex) { |
| 43 | + const auto& ival = node->ival(c10::attr::value); |
| 44 | + return ival; |
| 45 | + } |
| 46 | + } else if (type->isSubtypeOf(c10::ListType::ofBools())) { |
| 47 | + const auto bs = c10::fmap<bool>(node->is(c10::attr::value)); |
| 48 | + return bs; |
| 49 | + } else if (type->isSubtypeOf(c10::ListType::ofTensors())) { |
| 50 | + try { |
| 51 | + const auto& ts = node->ts(c10::attr::value); |
| 52 | + return ts; |
| 53 | + } catch (const std::exception& ex) { |
| 54 | + const auto& ival = node->ival(c10::attr::value); |
| 55 | + return ival; |
| 56 | + } |
| 57 | + } else if (type->isSubtypeOf(c10::ListType::ofStrings())) { |
| 58 | + try { |
| 59 | + const auto& ss = node->ss(c10::attr::value); |
| 60 | + auto vals = c10::impl::GenericList(c10::StringType::get()); |
| 61 | + for (const auto& str : ss) { |
| 62 | + vals.push_back(str); |
| 63 | + } |
| 64 | + return vals; |
| 65 | + } catch (const std::exception& ex) { |
| 66 | + const auto& ival = node->ival(c10::attr::value); |
| 67 | + return ival; |
| 68 | + } |
| 69 | + } else if ( |
| 70 | + type->cast<c10::ListType>() && |
| 71 | + node->kindOf(c10::attr::value) == torch::jit::AttributeKind::ival) { |
| 72 | + const auto& list = node->ival(c10::attr::value); |
| 73 | + TRTORCH_ASSERT(list.isList(), "Is not a list"); |
| 74 | + return list; |
| 75 | + } else if ( |
| 76 | + type->cast<c10::DictType>() && |
| 77 | + node->kindOf(c10::attr::value) == torch::jit::AttributeKind::ival) { |
| 78 | + const auto& dict = node->ival(c10::attr::value); |
| 79 | + TRTORCH_ASSERT(dict.isGenericDict(), "Is not a dict"); |
| 80 | + return dict; |
| 81 | + } else if ( |
| 82 | + type->cast<c10::TupleType>() && |
| 83 | + node->kindOf(c10::attr::value) == torch::jit::AttributeKind::ival) { |
| 84 | + const auto& tup = node->ival(c10::attr::value); |
| 85 | + TRTORCH_ASSERT(tup.isTuple(), "Is not a tuple"); |
| 86 | + return tup; |
| 87 | + } else if (type == c10::StringType::get()) { |
| 88 | + const auto& s = node->s(c10::attr::value); |
| 89 | + return s; |
| 90 | + } else if (type == c10::DeviceObjType::get()) { |
| 91 | + auto d = c10::Device(node->s(c10::attr::value)); |
| 92 | + return d; |
| 93 | + } else if (node->mustBeNone()) { |
| 94 | + return torch::jit::IValue(); |
| 95 | + } else { |
| 96 | + std::stringstream ss; |
| 97 | + ss << "constant literal not supported for: " << type->str(); |
| 98 | + throw std::runtime_error(ss.str()); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace evaluators |
| 103 | +} // namespace conversion |
| 104 | +} // namespace core |
| 105 | +} // namespace trtorch |
0 commit comments