• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 "src/litert/cxx_api/tensor_utils.h"
18 #include "src/common/log_adapter.h"
19 #include "src/tensor.h"
20 
21 namespace mindspore {
CalTensorDataSize(const std::vector<int64_t> & shape,enum DataType type)22 size_t MS_API CalTensorDataSize(const std::vector<int64_t> &shape, enum DataType type) {
23   size_t element_size = lite::DataTypeSize(static_cast<enum TypeId>(type));
24   for (size_t i = 0; i < shape.size(); i++) {
25     auto dim = shape[i];
26     element_size *= static_cast<size_t>(dim);
27   }
28   return element_size;
29 }
30 
TruncateShape(const std::vector<int64_t> & shape,enum TypeId type,size_t data_len,bool verify_size)31 std::vector<int32_t> TruncateShape(const std::vector<int64_t> &shape, enum TypeId type, size_t data_len,
32                                    bool verify_size) {
33   std::vector<int32_t> empty;
34   if (shape.empty()) {
35     return empty;
36   }
37   std::vector<int32_t> truncated_shape;
38   truncated_shape.resize(shape.size());
39   size_t element_size = lite::DataTypeSize(type);
40   for (size_t i = 0; i < shape.size(); i++) {
41     auto dim = shape[i];
42     if (dim < 0 || dim > INT_MAX || (dim != 0 && element_size > INT_MAX / static_cast<size_t>(dim))) {
43       MS_LOG(ERROR) << "Invalid shape!dim: " << dim << ", element_size: " << element_size;
44       return empty;
45     } else {
46       element_size *= static_cast<size_t>(dim);
47       truncated_shape[i] = static_cast<int32_t>(dim);
48     }
49   }
50   if (verify_size) {
51     if (element_size != data_len) {
52       MS_LOG(ERROR) << "Invalid data size!element_size: " << element_size << ", data_len: " << data_len;
53       return empty;
54     }
55   }
56   return truncated_shape;
57 }
LiteTensorToMSTensor(lite::Tensor * srcTensor,MSTensor * dstTensor,bool fromSession)58 Status LiteTensorToMSTensor(lite::Tensor *srcTensor, MSTensor *dstTensor, bool fromSession) {
59   auto impl = std::make_shared<LiteTensorImpl>(srcTensor);
60   if (impl == nullptr || impl->lite_tensor() == nullptr) {
61     MS_LOG(ERROR) << "Create tensor failed.";
62     return kLiteError;
63   }
64   impl->set_from_session(fromSession);
65   auto tensor = MSTensor(impl);
66   if (tensor == nullptr) {
67     MS_LOG(ERROR) << "Create tensor failed.";
68     return kLiteError;
69   }
70   *dstTensor = tensor;
71   return kSuccess;
72 }
73 
LiteTensorsToMSTensors(const std::vector<mindspore::lite::Tensor * > & srcTensors,bool fromSession)74 std::vector<MSTensor> LiteTensorsToMSTensors(const std::vector<mindspore::lite::Tensor *> &srcTensors,
75                                              bool fromSession) {
76   std::vector<MSTensor> dstTensors;
77   dstTensors.reserve(srcTensors.size());
78   for (auto inTensor : srcTensors) {
79     MSTensor tensor;
80     auto status = LiteTensorToMSTensor(inTensor, &tensor, fromSession);
81     if (status != kSuccess) {
82       return {};
83     }
84     dstTensors.emplace_back(tensor);
85   }
86   return dstTensors;
87 }
88 }  // namespace mindspore
89