From: 이상규/On-Device Lab(SR)/Principal Engineer/삼성전자 Date: Tue, 13 Aug 2019 05:57:57 +0000 (+0900) Subject: [neurun] Implement nnfw_input/output_tensorinfo (#6530) X-Git-Tag: accepted/tizen/unified/20190903.052428~440 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f02033bd46a44eb7ce110cca90b133a0c75888cc;p=platform%2Fcore%2Fml%2Fnnfw.git [neurun] Implement nnfw_input/output_tensorinfo (#6530) nnfw_input_tensorinfo and nnfw_output_tensorinfo are implemented. Signed-off-by: Sanggyu Lee --- diff --git a/runtimes/neurun/frontend/api/nnfw_dev.cc b/runtimes/neurun/frontend/api/nnfw_dev.cc index 629642b..3c8178b 100644 --- a/runtimes/neurun/frontend/api/nnfw_dev.cc +++ b/runtimes/neurun/frontend/api/nnfw_dev.cc @@ -133,3 +133,33 @@ NNFW_STATUS nnfw_output_size(nnfw_session *session, uint32_t *number) { return session->output_size(number); } + +/* + * Get i-th input tensor info + * + * @param[in] session session from input information is to be extracted + * @param[in] index index of input + * @param[out] tensor_info nnfw_tensor_info + * + * @return NNFW_STATUS_NO_ERROR if successful + */ +NNFW_STATUS nnfw_input_tensorinfo(nnfw_session *session, uint32_t index, + nnfw_tensorinfo *tensor_info) +{ + return session->input_tensorinfo(index, tensor_info); +} + +/* + * Get i-th output tensor info + * + * @param[in] session session from output information is to be extracted + * @param[in] index index of output + * @param[out] tensor_info nnfw_tensor_info + * + * @return NNFW_STATUS_NO_ERROR if successful + */ +NNFW_STATUS nnfw_output_tensorinfo(nnfw_session *session, uint32_t index, + nnfw_tensorinfo *tensor_info) +{ + return session->output_tensorinfo(index, tensor_info); +} diff --git a/runtimes/neurun/frontend/api/wrapper/nnfw_api.cc b/runtimes/neurun/frontend/api/wrapper/nnfw_api.cc index 06c44be..d6beeb7 100644 --- a/runtimes/neurun/frontend/api/wrapper/nnfw_api.cc +++ b/runtimes/neurun/frontend/api/wrapper/nnfw_api.cc @@ -141,8 +141,7 @@ NNFW_STATUS nnfw_session::input_size(uint32_t *number) std::cerr << "Error during nnfw_session::input_size, number is null pointer." << std::endl; return NNFW_STATUS_ERROR; } - uint32_t sz = _graph->getInputs().size(); - *number = sz; + *number = _graph->getInputs().size(); } catch (...) { @@ -161,8 +160,7 @@ NNFW_STATUS nnfw_session::output_size(uint32_t *number) std::cerr << "Error during nnfw_session::output_size, number is null pointer." << std::endl; return NNFW_STATUS_ERROR; } - uint32_t sz = _graph->getOutputs().size(); - *number = sz; + *number = _graph->getOutputs().size(); } catch (...) { @@ -171,3 +169,89 @@ NNFW_STATUS nnfw_session::output_size(uint32_t *number) } return NNFW_STATUS_NO_ERROR; } + +static NNFW_TYPE datatype_to_nnfw_dtype(neurun::model::DataType dt) +{ + using neurun::model::DataType; + switch (dt) + { + case DataType::FLOAT32: + return NNFW_TYPE_TENSOR_FLOAT32; + case DataType::INT32: + return NNFW_TYPE_TENSOR_INT32; + case DataType::QUANT8_ASYMM: + return NNFW_TYPE_TENSOR_QUANT8_ASYMM; + case DataType::BOOL8: + return NNFW_TYPE_TENSOR_BOOL; + case DataType::UINT32: + default: + std::cerr << "Error: Model has type that runtime API does not support." << std::endl; + exit(-1); + } +} + +NNFW_STATUS nnfw_session::input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti) +{ + try + { + if (ti == nullptr) + { + std::cerr << "Error during nnfw_session::input_tensorinfo, tensorinfo is null pointer." + << std::endl; + return NNFW_STATUS_ERROR; + } + if (index >= _graph->getInputs().size()) + { + std::cerr << "Error during nnfw_session::input_tensorinfo, index is out of range." + << std::endl; + return NNFW_STATUS_ERROR; + } + auto opidx = _graph->getInputs().at(index); + auto shape = _graph->operands().at(opidx).shape(); + ti->rank = shape.rank(); + for (int j = 0; j < ti->rank; ++j) + { + ti->dims[j] = shape.dim(j); + } + ti->dtype = datatype_to_nnfw_dtype(_graph->operands().at(opidx).typeInfo().type()); + } + catch (...) + { + std::cerr << "Error during nnfw_session::input_tensorinfo." << std::endl; + return NNFW_STATUS_ERROR; + } + return NNFW_STATUS_NO_ERROR; +} + +NNFW_STATUS nnfw_session::output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti) +{ + try + { + if (ti == nullptr) + { + std::cerr << "Error during nnfw_session::output_tensorinfo, tensorinfo is null pointer." + << std::endl; + return NNFW_STATUS_ERROR; + } + if (index >= _graph->getInputs().size()) + { + std::cerr << "Error during nnfw_session::output_tensorinfo, index is out of range." + << std::endl; + return NNFW_STATUS_ERROR; + } + auto opidx = _graph->getOutputs().at(index); + auto shape = _graph->operands().at(opidx).shape(); + ti->rank = shape.rank(); + for (int j = 0; j < ti->rank; ++j) + { + ti->dims[j] = shape.dim(j); + } + ti->dtype = datatype_to_nnfw_dtype(_graph->operands().at(opidx).typeInfo().type()); + } + catch (...) + { + std::cerr << "Error during nnfw_session::output_tensorinfo." << std::endl; + return NNFW_STATUS_ERROR; + } + return NNFW_STATUS_NO_ERROR; +} diff --git a/runtimes/neurun/frontend/api/wrapper/nnfw_api.hpp b/runtimes/neurun/frontend/api/wrapper/nnfw_api.hpp index 977e0d8..8efdd77 100644 --- a/runtimes/neurun/frontend/api/wrapper/nnfw_api.hpp +++ b/runtimes/neurun/frontend/api/wrapper/nnfw_api.hpp @@ -37,6 +37,9 @@ public: NNFW_STATUS input_size(uint32_t *number); NNFW_STATUS output_size(uint32_t *number); + NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti); + NNFW_STATUS output_tensorinfo(uint32_t index, nnfw_tensorinfo *ti); + private: std::shared_ptr _graph; std::shared_ptr _execution;