Skip to content

Commit

Permalink
[python] Speed up memory allocation of empty numpy arrays for inputs …
Browse files Browse the repository at this point in the history
…and outputs (Samsung#13447)

This commit is replacing slower list initialization with faster numpy call to create empty inputs and outputs for a nnfw_session.

ONE-DCO-1.0-Signed-off-by: Jan Iwaszkiewicz <[email protected]>
  • Loading branch information
jiwaszki authored Jul 18, 2024
1 parent 19c259d commit ca85d7d
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions runtime/onert/api/python/package/libnnfw_api_pybind.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,31 +30,26 @@ def set_inputs(self, size, inputs_array=[]):
"""Set inputs for each index"""
for i in range(size):
input_tensorinfo = self.input_tensorinfo(i)
ti_dtype = input_tensorinfo.dtype

if len(inputs_array) > i:
input_array = inputs_array[i]
input_array = np.array(inputs_array[i], dtype=input_tensorinfo.dtype)
else:
print(
f"model's input size is {size} but given inputs_array size is {len(inputs_array)}.\n{i}-th index input is replaced by an array filled with 0."
)
input_array = [0.] * num_elems(input_tensorinfo)
input_array = np.zeros(
(num_elems(input_tensorinfo)), dtype=input_tensorinfo.dtype)

input_array = np.array(input_array, dtype=ti_dtype)
self.set_input(i, input_array)

self.inputs.append(input_array)

def set_outputs(self, size):
"""Set outputs for each index"""
for i in range(size):
output_tensorinfo = self.output_tensorinfo(i)
ti_dtype = output_tensorinfo.dtype

output_array = [0.] * num_elems(output_tensorinfo)
output_array = np.array(output_array, dtype=ti_dtype)
output_array = np.zeros(
(num_elems(output_tensorinfo)), dtype=output_tensorinfo.dtype)
self.set_output(i, output_array)

self.outputs.append(output_array)

def inference(self):
Expand Down

0 comments on commit ca85d7d

Please sign in to comment.