summaryrefslogtreecommitdiff
path: root/runtimes/nn/runtime/ExecutionBuilder.h
diff options
context:
space:
mode:
Diffstat (limited to 'runtimes/nn/runtime/ExecutionBuilder.h')
-rw-r--r--runtimes/nn/runtime/ExecutionBuilder.h147
1 files changed, 147 insertions, 0 deletions
diff --git a/runtimes/nn/runtime/ExecutionBuilder.h b/runtimes/nn/runtime/ExecutionBuilder.h
new file mode 100644
index 000000000..2b05ef4be
--- /dev/null
+++ b/runtimes/nn/runtime/ExecutionBuilder.h
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __NNFW_RT_EXECUTION_BUILDER_H__
+#define __NNFW_RT_EXECUTION_BUILDER_H__
+
+#include "Callbacks.h"
+#include "HalInterfaces.h"
+#include "Memory.h"
+#include "ModelBuilder.h"
+#include "NeuralNetworks.h"
+
+#include <vector>
+
+using namespace android;
+
+namespace nnfw {
+namespace rt {
+
+class Memory;
+class ModelBuilder;
+class StepExecutor;
+
+// TODO-NNRT: Consider removing ModelArgumentInfo completely if it's not necessary
+// TODO move length out of DataLocation
+struct ModelArgumentInfo {
+ // Whether the argument was specified as being in a Memory, as a pointer,
+ // has no value, or has not been specified.
+ // If POINTER then:
+ // locationAndLength.length is valid.
+ // dimensions is valid.
+ // buffer is valid
+ // If MEMORY then:
+ // locationAndLength.location.{poolIndex, offset, length} is valid.
+ // dimensions is valid.
+ enum { POINTER, MEMORY, HAS_NO_VALUE, UNSPECIFIED } state = UNSPECIFIED;
+
+ DataLocation locationAndLength;
+
+ std::vector<uint32_t> dimensions;
+ void* buffer;
+
+ int setFromPointer(const Operand& operand, const ANeuralNetworksOperandType* type, void* buffer,
+ uint32_t length);
+ int setFromMemory(const Operand& operand, const ANeuralNetworksOperandType* type,
+ uint32_t poolIndex, uint32_t offset, uint32_t length);
+ int updateDimensionInfo(const Operand& operand, const ANeuralNetworksOperandType* newType);
+};
+
+// TODO-NNRT: Consider removing StepExecutor completely if it's not necessary
+class ExecutionBuilder {
+ friend class StepExecutor;
+public:
+ ExecutionBuilder(const CompilationBuilder* compilation);
+
+ int setInput(uint32_t index, const ANeuralNetworksOperandType* type, const void* buffer,
+ size_t length);
+ int setInputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type,
+ const Memory* memory, size_t offset, size_t length);
+ int setOutput(uint32_t index, const ANeuralNetworksOperandType* type, void* buffer,
+ size_t length);
+ int setOutputFromMemory(uint32_t index, const ANeuralNetworksOperandType* type,
+ const Memory* memory, size_t offset, size_t length);
+ int startCompute(sp<ExecutionCallback>* synchronizationCallback);
+ const ModelBuilder* getModel() const { return mModel; }
+
+private:
+ const ModelBuilder* mModel;
+ // The information we'll send to the driver about the inputs and outputs.
+ // Note that we build this in two steps:
+ // 1. As the arguments are specified, set the corresponding mInputs or mOutputs element.
+ // If set from a pointer, don't set the location in the RequestArgument but store it
+ // instead in mInputBuffers or mOutputBuffers.
+ // 2. Once we have all the inputs and outputs, if needed, allocate shared memory for
+ // the m*Buffers entries. Copy the input values into the shared memory.
+ // We do this to avoid creating a lot of shared memory objects if we have a lot of
+ // parameters specified via pointers. We also avoid copying in the case where
+ // some of the nodes will interpreted on the CPU anyway.
+ std::vector<ModelArgumentInfo> mInputs;
+ std::vector<ModelArgumentInfo> mOutputs;
+ MemoryTracker mMemories;
+};
+
+// class StepExecutor is used to execute a single "step" in a
+// potentially multiple step execution process. The graph associated
+// with that step is executed in its entirety on a single device (or
+// on the CPU).
+class StepExecutor {
+// TODO-NNRT: Consider removing StepExecutor completely if it's not necessary
+public:
+ // executionBuilder
+ // Describes the full (possibly multiple-"step") execution.
+ // model
+ // The model to be executed by the executor. Possibly a
+ // submodel of the model from executionBuilder.
+ StepExecutor(const ExecutionBuilder* executionBuilder, const ModelBuilder* model);
+ // Map inputs and outputs from ExecutionBuilder to StepExecutor,
+ // in the case where we have a single-"step" execution (i.e., the executor
+ // is executing the entire model from the ExecutionBuilder).
+ void mapInputsAndOutputsTrivially();
+ // Executes using the (driver, preparedModel) specified at construction time.
+ int startCompute(sp<ExecutionCallback>* synchronizationCallback);
+
+ // Executes using the CPU, regardless of the (driver,
+ // preparedModel) specified at construction time.
+ int startComputeOnCpu(sp<ExecutionCallback>* synchronizationCallback);
+
+private:
+ const ExecutionBuilder* mExecutionBuilder;
+
+ // model to be executed on the executor, in both original and
+ // compiled forms; and device on which to execute it
+ const ModelBuilder* mModel;
+
+ // The information we'll send to the driver about the inputs and outputs.
+ // Note that we build this in two steps:
+ // 1. As the arguments are specified, set the corresponding mInputs or mOutputs element.
+ // If set from a pointer, don't set the location in the RequestArgument but store it
+ // instead in mInputBuffers or mOutputBuffers.
+ // 2. Once we have all the inputs and outputs, if needed, allocate shared memory for
+ // the m*Buffers entries. Copy the input values into the shared memory.
+ // We do this to avoid creating a lot of shared memory objects if we have a lot of
+ // parameters specified via pointers. We also avoid copying in the case where
+ // some of the nodes will interpreted on the CPU anyway.
+ std::vector<ModelArgumentInfo> mInputs;
+ std::vector<ModelArgumentInfo> mOutputs;
+ MemoryTracker mMemories;
+};
+
+} // namespace rt
+} // namespace nnfw
+
+#endif // __NNFW_RT_EXECUTION_BUILDER_H__