• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "common/fetch_content.h"
18 #include <string>
19 #include <vector>
20 #include "common/anf_util.h"
21 #include "common/check_base.h"
22 #include "third_party/securec/include/securec.h"
23 
24 namespace mindspore {
25 namespace dpico {
26 namespace {
27 constexpr size_t kTensorListMinSize = 3 * sizeof(int32_t);
28 }  // namespace
29 
FetchFromDefaultParam(const api::ParameterPtr & param_node,DataInfo * data_info)30 int FetchFromDefaultParam(const api::ParameterPtr &param_node, DataInfo *data_info) {
31   MS_ASSERT(param_node != nullptr && data_info != nullptr);
32   ShapeVector shape_vector;
33   TypeId data_type;
34   auto status = GetDataTypeAndShape(param_node, &data_type, &shape_vector);
35   if (status != RET_OK) {
36     MS_LOG(ERROR) << "get data type and shape from param node failed.";
37     return RET_ERROR;
38   }
39   data_info->data_type_ = data_type;
40   auto tensor_info = param_node->default_param()->cast<api::TensorPtr>();
41   size_t offset = 0;
42   if (!shape_vector.empty() && data_type == kObjectTypeString) {
43     status = GetShapeVectorFromStringTensor(tensor_info, &shape_vector, &offset);
44     if (status != RET_OK) {
45       MS_LOG(ERROR) << "get shape vector from string tensor failed.";
46       return RET_ERROR;
47     }
48   }
49   std::vector<int32_t> dims(shape_vector.begin(), shape_vector.end());
50   data_info->shape_ = dims;
51   if (tensor_info != nullptr && tensor_info->Size() != 0) {
52     if (data_type != kObjectTypeTensorType || tensor_info->Size() >= kTensorListMinSize) {
53       if (tensor_info->Size() < offset) {
54         MS_LOG(ERROR) << "tensor_info elem size should be greater than offset.";
55         return RET_ERROR;
56       }
57       data_info->data_.resize(tensor_info->Size() - offset);
58       if (EOK != memcpy_s(data_info->data_.data(), data_info->data_.size(),
59                           static_cast<uint8_t *>(tensor_info->data()) + offset, tensor_info->Size() - offset)) {
60         MS_LOG(ERROR) << "memcpy_s failed.";
61         return RET_ERROR;
62       }
63     }
64   }
65 
66   return RET_OK;
67 }
68 
FetchDataFromParameterNode(const api::CNodePtr & cnode,size_t index,DataInfo * data_info)69 int FetchDataFromParameterNode(const api::CNodePtr &cnode, size_t index, DataInfo *data_info) {
70   MS_ASSERT(cnode != nullptr && data_info != nullptr);
71   if (index >= cnode->size()) {
72     MS_LOG(ERROR) << "input index: " << index << " is greater than cnode inputs size " << cnode->size();
73     return RET_ERROR;
74   }
75   auto param_node = cnode->input(index)->cast<api::ParameterPtr>();
76   if (param_node == nullptr) {
77     MS_LOG(ERROR) << "input node is not parameter node.";
78     return RET_ERROR;
79   }
80 
81   if (FetchFromDefaultParam(param_node, data_info) != RET_OK) {
82     MS_LOG(ERROR) << "fetch information from default param failed.";
83     return RET_ERROR;
84   }
85 
86   return RET_OK;
87 }
88 
GetDataSizeFromTensor(DataInfo * data_info,int * data_size)89 int GetDataSizeFromTensor(DataInfo *data_info, int *data_size) {
90   if (data_info == nullptr) {
91     MS_LOG(ERROR) << "data info is nullptr. ";
92     return RET_ERROR;
93   }
94   if (data_size == nullptr) {
95     MS_LOG(ERROR) << "elem size is nullptr";
96     return RET_ERROR;
97   }
98   *data_size = 1;
99   for (size_t i = 0; i < data_info->shape_.size(); i++) {
100     if (INT_MUL_OVERFLOW(*data_size, data_info->shape_.at(i))) {
101       MS_LOG(ERROR) << "int mul overflow.";
102       return RET_ERROR;
103     }
104     *data_size *= data_info->shape_.at(i);
105   }
106   return RET_OK;
107 }
108 }  // namespace dpico
109 }  // namespace mindspore
110