forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputs.cpp
More file actions
205 lines (188 loc) · 6.64 KB
/
inputs.cpp
File metadata and controls
205 lines (188 loc) · 6.64 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
* Copyright 2025 Arm Limited and/or its affiliates.
*
* 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/runner_util/inputs.h>
#include <executorch/runtime/executor/method.h>
#include <executorch/runtime/executor/method_meta.h>
#include <executorch/runtime/platform/log.h>
using executorch::ET_RUNTIME_NAMESPACE::Method;
using executorch::ET_RUNTIME_NAMESPACE::MethodMeta;
using executorch::ET_RUNTIME_NAMESPACE::TensorInfo;
using executorch::runtime::Error;
using executorch::runtime::Result;
using executorch::runtime::Tag;
namespace executorch {
namespace extension {
Result<BufferCleanup> prepare_input_tensors(
Method& method,
PrepareInputTensorsOptions options,
const std::vector<std::pair<char*, size_t>>& input_buffers) {
MethodMeta method_meta = method.method_meta();
size_t num_inputs = method_meta.num_inputs();
bool hard_code_inputs_to_ones = true;
if (input_buffers.size() > 0) {
hard_code_inputs_to_ones = false;
ET_CHECK_OR_RETURN_ERROR(
num_inputs == input_buffers.size(),
InvalidArgument,
"Wrong number of inputs allocated compared to method %zu ? %zu",
num_inputs,
input_buffers.size());
}
// A large number of small allocations could exhaust the heap even if the
// total size is smaller than the limit.
ET_CHECK_OR_RETURN_ERROR(
num_inputs <= options.max_inputs,
InvalidProgram,
"Too many inputs: %zu > %zu",
num_inputs,
options.max_inputs);
// Allocate memory for the inputs array
void** inputs = (void**)malloc(num_inputs * sizeof(void*));
ET_CHECK_OR_RETURN_ERROR(
inputs != nullptr,
MemoryAllocationFailed,
"malloc(%zd) failed",
num_inputs * sizeof(void*));
// Allocate memory for each input tensor.
size_t total_size = 0;
size_t num_allocated = 0;
for (size_t i = 0; i < num_inputs; i++) {
auto tag = method_meta.input_tag(i);
if (!tag.ok()) {
// The BufferCleanup will free the inputs when it goes out of scope.
BufferCleanup cleanup({inputs, num_allocated});
return tag.error();
}
if (tag.get() == Tag::None) {
Error err = method.set_input(runtime::EValue(), i);
if (err != Error::Ok) {
BufferCleanup cleanup({inputs, num_allocated});
return err;
}
continue;
}
if (tag.get() != Tag::Tensor) {
if (!hard_code_inputs_to_ones) {
Error err = Error::Ok;
auto [buffer, buffer_size] = input_buffers.at(i);
ET_LOG(
Debug, "Verifying and setting input for non-tensor input %zu", i);
if (tag.get() == Tag::Int) {
if (buffer_size != sizeof(int64_t)) {
ET_LOG(
Error,
"Int input at index %zu has size %zu, expected sizeof(int64_t) %zu",
i,
buffer_size,
sizeof(int64_t));
err = Error::InvalidArgument;
} else {
int64_t int_input;
std::memcpy(&int_input, buffer, buffer_size);
err = method.set_input(runtime::EValue(int_input), i);
}
} else if (tag.get() == Tag::Double) {
if (buffer_size != sizeof(double)) {
ET_LOG(
Error,
"Double input at index %zu has size %zu, expected sizeof(double) %zu",
i,
buffer_size,
sizeof(double));
err = Error::InvalidArgument;
} else {
double double_input;
std::memcpy(&double_input, buffer, buffer_size);
err = method.set_input(runtime::EValue(double_input), i);
}
} else if (tag.get() == Tag::Bool) {
if (buffer_size != sizeof(bool)) {
ET_LOG(
Error,
"Bool input at index %zu has size %zu, expected sizeof(bool) %zu",
i,
buffer_size,
sizeof(bool));
err = Error::InvalidArgument;
} else {
bool bool_input;
std::memcpy(&bool_input, buffer, buffer_size);
err = method.set_input(runtime::EValue(bool_input), i);
}
} else {
ET_LOG(
Error,
"Input %zu of type %zu not supported",
i,
static_cast<size_t>(tag.get()));
err = Error::InvalidArgument;
}
if (err != Error::Ok) {
BufferCleanup cleanup({inputs, num_allocated});
return err;
}
} else {
ET_LOG(Debug, "Skipping non-tensor input %zu", i);
}
continue;
}
Result<TensorInfo> tensor_meta = method_meta.input_tensor_meta(i);
if (!tensor_meta.ok()) {
BufferCleanup cleanup({inputs, num_allocated});
return tensor_meta.error();
}
// This input is a tensor. Allocate a buffer for it.
size_t tensor_size = tensor_meta->nbytes();
total_size += tensor_size;
if (total_size > options.max_total_allocation_size) {
ET_LOG(
Error,
"Allocating %zu bytes for input %zu would exceed "
"max_total_allocation_size %zu",
tensor_size,
i,
options.max_total_allocation_size);
BufferCleanup cleanup({inputs, num_allocated});
return Error::InvalidProgram;
}
void* data_ptr = malloc(tensor_size);
if (data_ptr == nullptr) {
ET_LOG(Error, "malloc(%zu) failed for input %zu", tensor_size, i);
BufferCleanup cleanup({inputs, num_allocated});
return Error::MemoryAllocationFailed;
}
inputs[num_allocated++] = data_ptr;
// Write input data for input tensor
if (!hard_code_inputs_to_ones) {
auto [buffer, buffer_size] = input_buffers.at(i);
if (buffer_size != tensor_meta->nbytes()) {
ET_LOG(
Error,
"input size (%zu) and tensor size (%zu) mismatch!",
buffer_size,
tensor_meta->nbytes());
BufferCleanup cleanup({inputs, num_allocated});
return Error::InvalidArgument;
}
std::memcpy(data_ptr, buffer, buffer_size);
}
// Create the tensor and set it as the input.
Error err = internal::fill_and_set_input(
method, tensor_meta.get(), i, data_ptr, hard_code_inputs_to_ones);
if (err != Error::Ok) {
ET_LOG(
Error, "Failed to prepare input %zu: 0x%" PRIx32, i, (uint32_t)err);
BufferCleanup cleanup({inputs, num_allocated});
return err;
}
}
return BufferCleanup({inputs, num_allocated});
}
} // namespace extension
} // namespace executorch