forked from mindspore-Ecosystem/mindspore
!49640 c_api: Strings Attribute, Get op shape & dimension, Get Tensor by file bug fix.
Merge pull request !49640 from liyejun/c_api_hl
This commit is contained in:
commit
13546bd18e
|
@ -173,6 +173,15 @@ MIND_C_API AttrHandle MSNewAttrFloat32(ResMgrHandle res_mgr, const float v);
|
|||
/// \return Attribute value handle.
|
||||
MIND_C_API AttrHandle MSNewAttrBool(ResMgrHandle res_mgr, const bool v);
|
||||
|
||||
/// \brief Create new vector of Strings attribute value.
|
||||
///
|
||||
/// \param[in] res_mgr Resource Handle that manages the nodes of the funcGraph.
|
||||
/// \param[in] strs Given value.
|
||||
/// \param[in] vec_len Length of the string vector.
|
||||
///
|
||||
/// \return Attribute value handle.
|
||||
MIND_C_API AttrHandle MSNewAttrStrings(ResMgrHandle res_mgr, const char *strs[], size_t vec_len);
|
||||
|
||||
/// \brief Create new attribute value with array.
|
||||
///
|
||||
/// \param[in] res_mgr Resource Handle that manages the nodes of the funcGraph.
|
||||
|
|
|
@ -155,6 +155,26 @@ MIND_C_API size_t MSOpGetInputsNum(ResMgrHandle res_mgr, ConstNodeHandle op, STA
|
|||
/// \return Error code that indicate whether the functions executed successfully.
|
||||
MIND_C_API STATUS MSOpGetInputs(ResMgrHandle res_mgr, ConstNodeHandle op, NodeHandle inputs[], size_t input_num);
|
||||
|
||||
/// \brief Get dimension value of the infer shape from the given operator.
|
||||
///
|
||||
/// \param[in] res_mgr Resource manager that saves allocated instance resources.
|
||||
/// \param[in] op The Operator.
|
||||
/// \param[in] ret A pointer to the Error code that indicates whether the functions executed successfully.
|
||||
///
|
||||
/// \return Dimension Value.
|
||||
MIND_C_API size_t MSOpGetOutputDimension(ResMgrHandle res_mgr, ConstNodeHandle op, size_t output_index, STATUS *ret);
|
||||
|
||||
/// \brief Get shape vector of the infer shape from the given operator.
|
||||
///
|
||||
/// \param[in] res_mgr Resource manager that saves allocated instance resources.
|
||||
/// \param[in] op The Operator.
|
||||
/// \param[in] shape_ret The provided array for shape value storage.
|
||||
/// \param[in] dim Dimenesion of shape, which also indicates the numbers of elements in the shape vector.
|
||||
///
|
||||
/// \return Error code that indicates whether the functions executed successfully.
|
||||
MIND_C_API STATUS MSOpGetOutputShape(ResMgrHandle res_mgr, ConstNodeHandle op, int64_t shape_ret[], size_t dim,
|
||||
size_t output_index);
|
||||
|
||||
/// \brief Create a subgraph node.
|
||||
///
|
||||
/// \param[in] res_mgr Resource manager that saves allocated instance resources.
|
||||
|
|
|
@ -298,11 +298,32 @@ AttrHandle MSNewAttrBool(ResMgrHandle res_mgr, const bool v) {
|
|||
return GetRawPtr(res_mgr, value);
|
||||
}
|
||||
|
||||
AttrHandle MSNewAttrArray(ResMgrHandle res_mgr, void *value, size_t vec_size, DataTypeC data_type) {
|
||||
if (res_mgr == nullptr || value == nullptr) {
|
||||
MS_LOG(ERROR) << "Input Handle [res_mgr] or [value] is nullptr.";
|
||||
AttrHandle MSNewAttrStrings(ResMgrHandle res_mgr, const char *strs[], size_t vec_len) {
|
||||
if (res_mgr == nullptr) {
|
||||
MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
|
||||
return nullptr;
|
||||
}
|
||||
std::vector<std::string> converted_strs;
|
||||
for (size_t i = 0; i < vec_len; i++) {
|
||||
std::string converted_ele(strs[i]);
|
||||
converted_strs.push_back(converted_ele);
|
||||
}
|
||||
auto value = mindspore::MakeValue(converted_strs);
|
||||
return GetRawPtr(res_mgr, value);
|
||||
}
|
||||
|
||||
AttrHandle MSNewAttrArray(ResMgrHandle res_mgr, void *value, size_t vec_size, DataTypeC data_type) {
|
||||
if (res_mgr == nullptr) {
|
||||
MS_LOG(ERROR) << "Input Handle [res_mgr] is nullptr.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Allow empty attrbute value
|
||||
if (value == nullptr && vec_size != 0) {
|
||||
MS_LOG(ERROR) << "Input Handle [value] is nullptr.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
mindspore::ValuePtr value_ptr;
|
||||
switch (data_type) {
|
||||
case MS_BOOL: {
|
||||
|
|
|
@ -676,6 +676,42 @@ STATUS MSOpGetInputs(ResMgrHandle res_mgr, ConstNodeHandle op, NodeHandle inputs
|
|||
return RET_OK;
|
||||
}
|
||||
|
||||
size_t MSOpGetOutputDimension(ResMgrHandle res_mgr, ConstNodeHandle op, size_t output_index, STATUS *ret) {
|
||||
if (res_mgr == nullptr || op == nullptr) {
|
||||
MS_LOG(ERROR) << "Input Handle [res_mgr] or [op] is nullptr.";
|
||||
*ret = RET_NULL_PTR;
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
auto src_cnode = GetSrcPtr<CNodePtr>(res_mgr, op);
|
||||
MS_EXCEPTION_IF_NULL(src_cnode);
|
||||
std::vector<int64_t> shape = mindspore::common::AnfAlgo::GetOutputInferShape(src_cnode, output_index);
|
||||
return shape.size();
|
||||
} catch (const std::exception &e) {
|
||||
MS_LOG(ERROR) << "Get Shape from OP/CNode failed. Error info: " << e.what();
|
||||
*ret = RET_ERROR;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
STATUS MSOpGetOutputShape(ResMgrHandle res_mgr, ConstNodeHandle op, int64_t shape_ret[], size_t dim,
|
||||
size_t output_index) {
|
||||
if (res_mgr == nullptr || op == nullptr) {
|
||||
MS_LOG(ERROR) << "Input Handle [res_mgr] or [op] is nullptr.";
|
||||
return RET_NULL_PTR;
|
||||
}
|
||||
try {
|
||||
auto src_cnode = GetSrcPtr<CNodePtr>(res_mgr, op);
|
||||
MS_EXCEPTION_IF_NULL(src_cnode);
|
||||
std::vector<int64_t> shape = mindspore::common::AnfAlgo::GetOutputInferShape(src_cnode, output_index);
|
||||
std::copy(shape.begin(), shape.end(), shape_ret);
|
||||
} catch (const std::exception &e) {
|
||||
MS_LOG(ERROR) << "Get Shape from OP/CNode failed. Error info: " << e.what();
|
||||
return RET_ERROR;
|
||||
}
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
NodeHandle MSNewFuncCallNode(ResMgrHandle res_mgr, GraphHandle graph, ConstGraphHandle sub_graph, Handle const inputs[],
|
||||
size_t input_num) {
|
||||
if (res_mgr == nullptr || graph == nullptr || sub_graph == nullptr) {
|
||||
|
|
|
@ -22,13 +22,14 @@
|
|||
#include "ir/dtype.h"
|
||||
|
||||
template <typename T>
|
||||
void GetDataByFile(std::vector<T> data, const char *path, size_t *elem_size) {
|
||||
std::vector<T> GetDataByFile(const char *path, size_t *elem_size) {
|
||||
std::string fileName(path);
|
||||
MS_LOG(INFO) << "Reading File: " << fileName << std::endl;
|
||||
std::ifstream fin(fileName, std::ios::in);
|
||||
std::vector<T> data;
|
||||
if (!fin.is_open()) {
|
||||
MS_LOG(ERROR) << "Open file failed, File path: %s " << fileName << std::endl;
|
||||
return;
|
||||
return data;
|
||||
}
|
||||
T t;
|
||||
while (fin >> t) {
|
||||
|
@ -36,6 +37,7 @@ void GetDataByFile(std::vector<T> data, const char *path, size_t *elem_size) {
|
|||
}
|
||||
fin.close();
|
||||
*elem_size = data.size() * sizeof(T);
|
||||
return data;
|
||||
}
|
||||
|
||||
TensorHandle MSNewTensor(ResMgrHandle res_mgr, void *data, DataTypeC type, const int64_t shape[], size_t shape_size,
|
||||
|
@ -65,22 +67,19 @@ TensorHandle MSNewTensorFromFile(ResMgrHandle res_mgr, DataTypeC type, const int
|
|||
ShapeVector shape_vec(shape, shape + shape_size);
|
||||
try {
|
||||
size_t data_len;
|
||||
switch (type) {
|
||||
case MS_INT32: {
|
||||
std::vector<int32_t> data;
|
||||
(void)GetDataByFile<int32_t>(data, path, &data_len);
|
||||
switch (mindspore::TypeId(type)) {
|
||||
case mindspore::TypeId::kNumberTypeInt32: {
|
||||
std::vector<int32_t> data = GetDataByFile<int32_t>(path, &data_len);
|
||||
tensor = std::make_shared<TensorImpl>(mindspore::TypeId(type), shape_vec, data.data(), data_len);
|
||||
break;
|
||||
}
|
||||
case MS_INT64: {
|
||||
std::vector<int64_t> data;
|
||||
(void)GetDataByFile<int64_t>(data, path, &data_len);
|
||||
case mindspore::TypeId::kNumberTypeInt64: {
|
||||
std::vector<int64_t> data = GetDataByFile<int64_t>(path, &data_len);
|
||||
tensor = std::make_shared<TensorImpl>(mindspore::TypeId(type), shape_vec, data.data(), data_len);
|
||||
break;
|
||||
}
|
||||
case MS_FLOAT32: {
|
||||
std::vector<float> data;
|
||||
(void)GetDataByFile<float>(data, path, &data_len);
|
||||
case mindspore::TypeId::kNumberTypeFloat32: {
|
||||
std::vector<float> data = GetDataByFile<float>(path, &data_len);
|
||||
tensor = std::make_shared<TensorImpl>(mindspore::TypeId(type), shape_vec, data.data(), data_len);
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue