Skip to content

Commit 9a44d5e

Browse files
committed
implement API
1 parent e8ca14c commit 9a44d5e

File tree

9 files changed

+71
-11
lines changed

9 files changed

+71
-11
lines changed

runtime/onert/api/nnfw/include/nnfw_experimental.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ NNFW_STATUS nnfw_set_signature_prepare(nnfw_session *session, const char *signat
226226
* If this function is not called, default entry (ex. 0th subgraph in circle/tflite model)
227227
* will be selected.
228228
*
229-
* All input and output setting after {@link nnfw_prepare} and before calling this function
229+
* All input, output, and config setting after {@link nnfw_prepare} and before calling this function
230230
* will be invalidated.
231231
*
232232
* @param[in] session session to set the entry signature

runtime/onert/api/nnfw/src/nnfw_api.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ NNFW_STATUS nnfw_set_workspace(nnfw_session *session, const char *dir)
245245
return session->set_workspace(dir);
246246
}
247247

248+
NNFW_STATUS nnfw_set_signature_run(nnfw_session *session, const char *signature)
249+
{
250+
NNFW_RETURN_ERROR_IF_NULL(session);
251+
return session->set_signature_run(signature);
252+
}
253+
248254
// Training
249255

250256
NNFW_STATUS nnfw_train_get_traininfo(nnfw_session *session, nnfw_train_info *info)

runtime/onert/api/nnfw/src/nnfw_session.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ nnfw_session::nnfw_session()
242242
: _nnpkg{nullptr}, _coptions{onert::compiler::CompilerOptions::fromGlobalConfig()},
243243
_compiler_artifact{nullptr}, _execution{nullptr}, _kernel_registry{nullptr},
244244
_train_info{nullptr}, _quant_manager{std::make_unique<onert::odc::QuantizeManager>()},
245-
_codegen_manager{std::make_unique<onert::odc::CodegenManager>()}, _model_path{}
245+
_codegen_manager{std::make_unique<onert::odc::CodegenManager>()}, _model_path{},
246+
_signature_map{}
246247
{
247248
// DO NOTHING
248249
}
@@ -970,6 +971,30 @@ NNFW_STATUS nnfw_session::set_workspace(const char *dir)
970971
return NNFW_STATUS_NO_ERROR;
971972
}
972973

974+
NNFW_STATUS nnfw_session::set_signature_run(const char *signature)
975+
{
976+
if (!signature)
977+
return NNFW_STATUS_UNEXPECTED_NULL;
978+
979+
if (!isStatePreparedOrFinishedRun())
980+
{
981+
std::cerr << "Error during nnfw_session::set_signature_run : invalid state" << std::endl;
982+
return NNFW_STATUS_INVALID_STATE;
983+
}
984+
985+
for (auto &sig : _signature_map)
986+
{
987+
if (sig.second == std::string(signature))
988+
{
989+
_execution = std::make_unique<onert::exec::Execution>(_compiler_artifact->_executors,
990+
sig.second, sig.first);
991+
return NNFW_STATUS_NO_ERROR;
992+
}
993+
}
994+
995+
return NNFW_STATUS_ERROR;
996+
}
997+
973998
NNFW_STATUS nnfw_session::deprecated(const char *msg)
974999
{
9751000
std::cerr << msg << std::endl;
@@ -1072,6 +1097,7 @@ NNFW_STATUS nnfw_session::loadModelFile(const std::string &model_file_path,
10721097
if (model == nullptr)
10731098
return NNFW_STATUS_ERROR;
10741099

1100+
_signature_map = model->signatureMap();
10751101
_nnpkg = std::make_unique<onert::ir::NNPkg>(std::move(model));
10761102
_model_path = std::filesystem::path(model_file_path);
10771103
_compiler_artifact.reset();

runtime/onert/api/nnfw/src/nnfw_session.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <string>
3636
#include <thread>
3737
#include <vector>
38+
#include <unordered_map>
3839

3940
struct nnfw_session
4041
{
@@ -132,6 +133,8 @@ struct nnfw_session
132133

133134
NNFW_STATUS set_workspace(const char *dir);
134135

136+
NNFW_STATUS set_signature_run(const char *signature);
137+
135138
static NNFW_STATUS deprecated(const char *msg);
136139

137140
//
@@ -230,6 +233,7 @@ struct nnfw_session
230233
// const uint8 *buf;
231234
// }
232235
std::filesystem::path _model_path;
236+
std::unordered_map<onert::ir::SubgraphIndex, std::string> _signature_map;
233237
};
234238

235239
#endif // __API_NNFW_SESSION_H__

runtime/onert/core/include/exec/Execution.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ class Execution
4747
*/
4848
Execution(const std::shared_ptr<IExecutors> &executors);
4949

50+
/**
51+
* @brief Construct a new Execution object for signature
52+
* @param[in] executors Model executors
53+
* @param[in] signature Signature name
54+
* @param[in] entry_index Entry subgraph index
55+
*/
56+
Execution(const std::shared_ptr<IExecutors> &executors, const std::string &signature,
57+
const ir::SubgraphIndex &entry_index);
58+
5059
public:
5160
/**
5261
* @brief Returns primary graph object

runtime/onert/core/include/ir/Model.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ class Model
197197
_signature_map[index] = name;
198198
}
199199

200+
const std::unordered_map<ir::SubgraphIndex, std::string> &signatureMap() const
201+
{
202+
return _signature_map;
203+
}
204+
200205
private:
201206
// TODO: Apply Heterogeneous lookup for unordered containers (transparent hashing) since C++20
202207
// to use `std::string_view` with lookup functions in unordered containers

runtime/onert/core/src/exec/Execution.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "exec/Execution.h"
1818

19+
#include "SignatureExecutors.h"
1920
#include "../backend/builtin/IOTensor.h"
2021
#include "ir/DataType.h"
2122
#include "train/TrainableExecutors.h"
@@ -43,7 +44,7 @@ Execution::Execution(const std::shared_ptr<IExecutors> &executors) : _executors{
4344
for (uint32_t i = 0; i < _executors->outputSize(); ++i)
4445
{
4546
const auto output_tensor =
46-
dynamic_cast<const backend::builtin::IOTensor *>(executors->outputTensor(ir::IOIndex{i}));
47+
dynamic_cast<const backend::builtin::IOTensor *>(_executors->outputTensor(ir::IOIndex{i}));
4748
if (!output_tensor)
4849
throw std::runtime_error("Output tensor must be IOTensor");
4950

@@ -54,6 +55,13 @@ Execution::Execution(const std::shared_ptr<IExecutors> &executors) : _executors{
5455
ExecutionOptions::fromGlobalConfig(_ctx.options);
5556
}
5657

58+
Execution::Execution(const std::shared_ptr<IExecutors> &executors, const std::string &signature,
59+
const ir::SubgraphIndex &entry_index)
60+
: Execution(std::make_shared<SignatureExecutors>(executors, signature, entry_index))
61+
{
62+
// DO NOTHING
63+
}
64+
5765
void Execution::changeInputShape(const ir::IOIndex &index, const ir::Shape &new_shape)
5866
{
5967
// This will be used later to set input tensor dynamic

runtime/onert/core/src/exec/SignatureExecutors.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
namespace onert::exec
2020
{
2121

22-
SignatureExecutors::SignatureExecutors(std::shared_ptr<SingleModelExecutors> &executors,
22+
SignatureExecutors::SignatureExecutors(const std::shared_ptr<IExecutors> &executors,
2323
const std::string &signature,
2424
const ir::SubgraphIndex &entry_index)
2525
: _executors(executors), _signature(signature), _entry_index(entry_index)
2626
{
27-
// TODO Set _entry_index
27+
// Check single model
28+
// TODO Support multimodel
29+
assert(dynamic_cast<SingleModelExecutors *>(executors.get()) != nullptr);
2830
}
2931

3032
IExecutor *SignatureExecutors::entryExecutor() const

runtime/onert/core/src/exec/SignatureExecutors.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ namespace onert::exec
2727

2828
/**
2929
* @brief Class to gather executor set for signature entry
30-
* Actually it is wrapper of SingleModelExecutors
30+
* Actually it is wrapper of IExecutors(SignatureExecutors)
3131
*/
3232
class SignatureExecutors : public SingleModelExecutors
3333
{
3434
public:
3535
/**
36-
* @brief Construct a new SingleModelExecutors object
36+
* @brief Construct a new SignatureExecutors object
3737
*/
3838
SignatureExecutors(void) = default;
3939
SignatureExecutors(const SignatureExecutors &) = delete;
@@ -45,19 +45,19 @@ class SignatureExecutors : public SingleModelExecutors
4545
~SignatureExecutors() = default;
4646

4747
/**
48-
* @brief Convert SingleModelExecutors to SignatureExecutors
49-
* @param[in] executors SingleModelExecutors object to convert
48+
* @brief Convert IExecutors to SignatureExecutors
49+
* @param[in] executors Executors object to convert
5050
* @param[in] signature signature name of the executors
5151
* @param[in] index subgraph index of the signature
5252
*/
53-
SignatureExecutors(std::shared_ptr<SingleModelExecutors> &executors, const std::string &signature,
53+
SignatureExecutors(const std::shared_ptr<IExecutors> &executors, const std::string &signature,
5454
const ir::SubgraphIndex &entry_index);
5555

5656
public:
5757
IExecutor *entryExecutor() const override;
5858

5959
private:
60-
std::shared_ptr<SingleModelExecutors> _executors;
60+
const std::shared_ptr<IExecutors> _executors;
6161
std::string _signature;
6262
ir::SubgraphIndex _entry_index;
6363
};

0 commit comments

Comments
 (0)