-
Notifications
You must be signed in to change notification settings - Fork 531
/
Copy pathivalue_util.cpp
217 lines (188 loc) · 5.32 KB
/
ivalue_util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/extension/pytree/aten_util/ivalue_util.h>
#include <executorch/runtime/platform/assert.h>
namespace executorch {
namespace extension {
using namespace c10;
using namespace at;
using namespace executorch::extension::pytree;
ContainerHandle<IValue> getContainerHandle(const IValue& data) {
if (data.isList()) {
const auto& values = data.toList();
auto c = ContainerHandle<IValue>(Kind::List, values.size());
for (size_t i = 0; i < values.size(); ++i) {
c[i] = getContainerHandle(values[i]);
}
return c;
}
if (data.isTuple()) {
const auto& values = data.toTupleRef().elements();
auto c = ContainerHandle<IValue>(Kind::Tuple, values.size());
for (size_t i = 0; i < values.size(); ++i) {
c[i] = getContainerHandle(values[i]);
}
return c;
}
if (data.isGenericDict()) {
const auto& dict = data.toGenericDict();
auto c = ContainerHandle<IValue>(Kind::Dict, dict.size());
size_t i = 0;
for (const auto& entry : dict) {
const auto& key = entry.key().toStringRef();
const auto& value = entry.value();
c.key(i) = Key(key);
c[i] = getContainerHandle(value);
++i;
}
return c;
}
return const_cast<IValue*>(&data);
}
template <std::size_t... Is>
auto create_tuple_impl(
std::index_sequence<Is...>,
const std::vector<IValue>& arguments) {
return std::make_tuple(arguments[Is]...);
}
template <std::size_t N>
auto create_tuple(const std::vector<IValue>& arguments) {
return create_tuple_impl(std::make_index_sequence<N>{}, arguments);
}
IValue constructTuple(const std::vector<IValue>& ivalues) {
switch (ivalues.size()) {
case 1:
return create_tuple<1>(ivalues);
case 2:
return create_tuple<2>(ivalues);
case 3:
return create_tuple<3>(ivalues);
case 4:
return create_tuple<4>(ivalues);
case 5:
return create_tuple<5>(ivalues);
case 6:
return create_tuple<6>(ivalues);
case 7:
return create_tuple<7>(ivalues);
case 8:
return create_tuple<8>(ivalues);
case 9:
return create_tuple<9>(ivalues);
case 10:
return create_tuple<10>(ivalues);
}
ET_ASSERT_UNREACHABLE_MSG("Supports at most 10 inputs");
return {};
}
IValue toIValue(const ContainerHandle<IValue>& c) {
if (c.isList()) {
auto ivalues = c10::impl::GenericList(c10::AnyType::get());
for (size_t i = 0; i < c.size(); ++i) {
ivalues.emplace_back(toIValue(c[i]));
}
return ivalues;
}
if (c.isTuple()) {
std::vector<IValue> ivalues;
for (size_t i = 0; i < c.size(); ++i) {
ivalues.emplace_back(toIValue(c[i]));
}
return constructTuple(ivalues);
}
if (c.isDict()) {
auto dict =
c10::impl::GenericDict(c10::StringType::get(), c10::AnyType::get());
for (size_t i = 0; i < c.size(); ++i) {
dict.insert(std::string(c.key(i)), toIValue(c[i]));
}
return dict;
}
ET_CHECK(c.isLeaf());
return {*c.leaf_ptr()};
}
std::pair<std::vector<at::Tensor>, std::unique_ptr<TreeSpec<Empty>>> flatten(
const IValue& data) {
auto c = getContainerHandle(data);
auto p = flatten(c);
std::vector<at::Tensor> tensors;
for (const auto& item : p.first) {
tensors.emplace_back(item->toTensor());
}
return {tensors, std::move(p.second)};
}
IValue unflatten(
const std::vector<at::Tensor>& tensors,
const std::unique_ptr<TreeSpec<Empty>>& tree_spec) {
std::vector<IValue> ivalues;
for (const auto& tensor : tensors) {
ivalues.emplace_back(tensor);
}
ContainerHandle<IValue> c = unflatten(*tree_spec, ivalues.data());
return toIValue(c);
}
bool is_same(
const std::vector<at::Tensor>& a,
const std::vector<at::Tensor>& b) {
for (int i = 0; i < a.size(); ++i) {
if (!at::all(a[i] == b[i]).item<bool>()) {
return false;
}
}
return true;
}
bool is_same(const IValue& lhs, const IValue& rhs) {
if (lhs.isList() && rhs.isList()) {
const auto& l = lhs.toList();
const auto& r = rhs.toList();
if (l.size() != r.size()) {
return false;
}
for (size_t i = 0; i < l.size(); ++i) {
if (!is_same(l[i], r[i])) {
return false;
}
}
return true;
}
if (lhs.isTuple() && rhs.isTuple()) {
const auto& l = lhs.toTupleRef().elements();
const auto& r = rhs.toTupleRef().elements();
if (l.size() != r.size()) {
return false;
}
for (size_t i = 0; i < l.size(); ++i) {
if (!is_same(l[i], r[i])) {
return false;
}
}
return true;
}
if (lhs.isGenericDict() && rhs.isGenericDict()) {
const auto& lhs_dict = lhs.toGenericDict();
const auto& rhs_dict = rhs.toGenericDict();
if (lhs_dict.size() != rhs_dict.size()) {
return false;
}
for (const auto& entry : lhs_dict) {
if (!rhs_dict.contains(entry.key())) {
return false;
}
if (!is_same(entry.value(), rhs_dict.at(entry.key()))) {
return false;
}
}
return true;
}
ET_CHECK(lhs.isTensor() && rhs.isTensor());
const auto& l = lhs.toTensor();
const auto& r = rhs.toTensor();
return at::all(l == r).item<bool>();
}
} // namespace extension
} // namespace executorch