• 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 #include "src/common/ops/populate/populate_register.h"
17 #include "nnacl/op_base.h"
18 #include "nnacl/tensor_array_parameter.h"
19 
20 using mindspore::schema::PrimitiveType_TensorArray;
21 using mindspore::schema::PrimitiveType_TensorArrayRead;
22 using mindspore::schema::PrimitiveType_TensorArrayWrite;
23 
24 namespace mindspore {
25 namespace lite {
PopulateTensorArrayParameter(const void * prim)26 OpParameter *PopulateTensorArrayParameter(const void *prim) {
27   MS_CHECK_TRUE_RET(prim != nullptr, nullptr);
28   auto primitive = static_cast<const schema::Primitive *>(prim);
29   auto value = primitive->value_as_TensorArray();
30   MS_CHECK_TRUE_RET(value != nullptr, nullptr);
31   MS_CHECK_TRUE_RET(value->element_shape() != nullptr, nullptr);
32 
33   auto param = reinterpret_cast<TensorArrayParameter *>(malloc(sizeof(TensorArrayParameter)));
34   if (param == nullptr) {
35     MS_LOG(ERROR) << "malloc TensorArray nnacl Parameter failed.";
36     return nullptr;
37   }
38   memset(param, 0, sizeof(TensorArrayParameter));
39 
40   param->op_parameter_.type_ = primitive->value_type();
41   bool dynamic_size = value->dynamic_size();
42   param->dynamic_size_ = dynamic_size;
43   bool identical_element_shapes = value->identical_element_shapes();
44   param->identical_element_shapes_ = identical_element_shapes;
45   std::vector<int> primitive_element_shape(value->element_shape()->begin(), value->element_shape()->end());
46   param->element_shape_size_ = static_cast<int>(primitive_element_shape.size());
47   auto size = sizeof(int) * static_cast<size_t>(param->element_shape_size_);
48   MS_CHECK_LE(size, MAX_SHAPE_SIZE, nullptr);
49   memset(param->element_shape_, 0, size);
50   memcpy(param->element_shape_, primitive_element_shape.data(), size);
51   param->data_type_ = value->data_type();
52   return reinterpret_cast<OpParameter *>(param);
53 }
54 
PopulateTACommonParameter(const void * prim)55 OpParameter *PopulateTACommonParameter(const void *prim) {
56   MS_CHECK_TRUE_RET(prim != nullptr, nullptr);
57   auto primitive = static_cast<const schema::Primitive *>(prim);
58 
59   auto *param = reinterpret_cast<OpParameter *>(malloc(sizeof(OpParameter)));
60   if (param == nullptr) {
61     MS_LOG(ERROR) << "malloc OpParameter failed.";
62     return nullptr;
63   }
64   memset(param, 0, sizeof(OpParameter));
65 
66   param->type_ = primitive->value_type();
67   return reinterpret_cast<OpParameter *>(param);
68 }
69 
70 REG_POPULATE(PrimitiveType_TensorArray, PopulateTensorArrayParameter, SCHEMA_CUR)
71 REG_POPULATE(PrimitiveType_TensorArrayRead, PopulateTACommonParameter, SCHEMA_CUR)
72 REG_POPULATE(PrimitiveType_TensorArrayWrite, PopulateTACommonParameter, SCHEMA_CUR)
73 }  // namespace lite
74 }  // namespace mindspore
75