1 /**
2 * Copyright 2021-2023 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/schema_tensor_wrapper.h"
18 #include "src/common/log_adapter.h"
19 #include "src/common/file_utils.h"
20 #include "nnacl/op_base.h"
21
22 namespace mindspore {
23 namespace lite {
24 // don't check data_size and shape_size: bit_pack or huffman_code
25 // don't check tensor category: variable-tensor-list may have data
26 #ifdef ENABLE_LITE_HELPER
Init(const schema::Tensor & tensor,const SCHEMA_VERSION schema_version,const std::string & base_path,mindspore::infer::helper::InferHelpers * infer_helpers)27 bool SchemaTensorWrapper::Init(const schema::Tensor &tensor, const SCHEMA_VERSION schema_version,
28 const std::string &base_path, mindspore::infer::helper::InferHelpers *infer_helpers) {
29 #else
30 bool SchemaTensorWrapper::Init(const schema::Tensor &tensor, const SCHEMA_VERSION schema_version,
31 const std::string &base_path) {
32 #endif
33 // add magic-num-check and checksum-check here
34 this->handler_ = &tensor;
35 if (tensor.data() != nullptr && tensor.data()->data() != nullptr) {
36 auto data = tensor.data()->data();
37 auto data_size = tensor.data()->size();
38 this->length_ = data_size;
39 this->data_ = const_cast<unsigned char *>(data);
40 this->if_own_data_ = false;
41 return true;
42 }
43 if (schema_version == SCHEMA_V0) {
44 return true;
45 }
46 if (tensor.externalData() == nullptr) {
47 return true;
48 }
49 if (tensor.externalData()->size() != 1) {
50 MS_LOG(ERROR) << "Only support tensor saved in one file now";
51 return false;
52 }
53 auto external_data = tensor.externalData()->Get(0);
54 this->length_ = static_cast<size_t>(external_data->length());
55 #ifdef ENABLE_LITE_HELPER
56 if (infer_helpers != nullptr && infer_helpers->GetExternalTensorHelper() != nullptr) {
57 this->data_ = infer_helpers->GetExternalTensorHelper()->GetExternalTensorData(external_data);
58 this->if_own_data_ = false;
59 } else {
60 this->data_ =
61 ReadFileSegment(base_path + external_data->location()->str(), external_data->offset(), external_data->length());
62 this->if_own_data_ = true;
63 }
64 #else
65 this->data_ =
66 ReadFileSegment(base_path + external_data->location()->str(), external_data->offset(), external_data->length());
67 this->if_own_data_ = true;
68 #endif
69 if (this->length_ > 0 && this->data_ == nullptr) {
70 MS_LOG(ERROR) << "Read tensor data from msw file failed";
71 return false;
72 }
73 return true;
74 }
75 } // namespace lite
76 } // namespace mindspore
77