• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021-2022 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 "extendrt/mindir_loader/model_loader.h"
18 
19 namespace mindspore::infer {
20 constexpr size_t kMaxModelBufferSize = static_cast<size_t>(1024) * 1024 * 1024 * 2;
21 
InitModelBuffer(AbstractBaseModel * model,const char * model_buf,size_t size,bool take_buf)22 int ModelLoader::InitModelBuffer(AbstractBaseModel *model, const char *model_buf, size_t size, bool take_buf) {
23   if (model_buf == nullptr || size == 0) {
24     MS_LOG(ERROR) << "Input model buffer is nullptr.";
25     return mindspore::lite::RET_INPUT_PARAM_INVALID;
26   }
27   MS_ASSERT(model != nullptr);
28   if (take_buf) {
29     model->buf = const_cast<char *>(model_buf);
30   } else {
31     if (size > kMaxModelBufferSize) {
32       MS_LOG(ERROR) << "Input model buffer size invalid, require (0, 2GB].";
33       return mindspore::lite::RET_ERROR;
34     }
35     model->buf = new char[size];
36     if (model->buf == nullptr) {
37       MS_LOG(ERROR) << "new inner model buf fail!";
38       return mindspore::lite::RET_NULL_PTR;
39     }
40     memcpy(model->buf, model_buf, size);
41   }
42   model->buf_size_ = size;
43   return mindspore::lite::RET_OK;
44 }
45 
ModelLoaderRegistry()46 ModelLoaderRegistry::ModelLoaderRegistry() {}
47 
~ModelLoaderRegistry()48 ModelLoaderRegistry::~ModelLoaderRegistry() {}
49 
GetInstance()50 ModelLoaderRegistry *ModelLoaderRegistry::GetInstance() {
51   static ModelLoaderRegistry instance;
52   return &instance;
53 }
54 }  // namespace mindspore::infer
55