Skip to content

Commit 30f2abe

Browse files
tarun292facebook-github-bot
authored andcommitted
Add get_inputs to method.cpp (#3775)
Summary: There are use cases where users would like to access the input EValues after they've been memory planned and allocated buffers so that they can directly copy their inputs to these buffers. Adding a method to support that. Differential Revision: D57970255
1 parent 62cdfb9 commit 30f2abe

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

runtime/executor/method.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,28 @@ Method::get_outputs(EValue* output_evalues, size_t length) {
998998
return Error::Ok;
999999
}
10001000

1001+
__ET_NODISCARD Error Method::get_inputs(EValue* input_evalues, size_t length) {
1002+
ET_CHECK_OR_RETURN_ERROR(
1003+
initialized(),
1004+
InvalidState,
1005+
"Inputs can not be retrieved until method has been initialized.");
1006+
1007+
ET_CHECK_OR_RETURN_ERROR(
1008+
length >= inputs_size(),
1009+
InvalidArgument,
1010+
"The given array is not large enough to hold all inputs.");
1011+
1012+
for (size_t i = 0; i < inputs_size(); i++) {
1013+
input_evalues[i] = values_[get_input_index(i)];
1014+
}
1015+
1016+
for (size_t i = inputs_size(); i < length; i++) {
1017+
input_evalues[i] = EValue();
1018+
}
1019+
1020+
return Error::Ok;
1021+
}
1022+
10011023
Error Method::execute_instruction() {
10021024
auto& chain = chains_[step_state_.chain_idx];
10031025
auto instructions = chain.s_chain_->instructions();

runtime/executor/method.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,22 @@ class Method final {
162162
*/
163163
__ET_NODISCARD Error get_outputs(EValue* output_evalues, size_t length);
164164

165+
/**
166+
* Copies the method's inputs into the provided array.
167+
*
168+
* WARNING: The input contains shallow copies of internal tensor inputs.
169+
* Please do not mutate returned Tensor elements.
170+
*
171+
* @param[in] input_evalues The array to copy the inputs into. The first
172+
* `inputs_size()` elements will be set to the corresponding input
173+
* values. The rest of the array will be set to the EValue value None.
174+
* @param[in] length The size of the `input_evalues` array in elements. Must
175+
* be greater than or equal to `inputs_size()`.
176+
*
177+
* @returns Error::Ok on success, non-Ok on failure.
178+
*/
179+
__ET_NODISCARD Error get_inputs(EValue* input_evalues, size_t length);
180+
165181
/**
166182
* Execute the method.
167183
*

0 commit comments

Comments
 (0)