Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / backend / xnnpack / ops / Layer.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 #ifndef __ONERT_BACKEND_XNNPACK_OPS_LAYER_H__
18 #define __ONERT_BACKEND_XNNPACK_OPS_LAYER_H__
19
20 #include <exec/IFunction.h>
21 #include <backend/IPortableTensor.h>
22 #include "OperationUtils.h"
23 #include "../ExternalContext.h"
24 #include "../Tensor.h"
25
26 #include <cassert>
27 #include <memory>
28
29 #include <xnnpack.h>
30
31 namespace onert
32 {
33 namespace backend
34 {
35 namespace xnnpack
36 {
37 namespace ops
38 {
39
40 class Layer : public ::onert::exec::IFunction
41 {
42 public:
43   Layer(const std::shared_ptr<ExternalContext> external_context)
44       : _kernel_op{nullptr}, _create{false}, _setup{false}, _external_context{external_context}
45   {
46     // DO NOTHING
47   }
48
49   ~Layer()
50   {
51     if (_kernel_op)
52       xnn_delete_operator(_kernel_op);
53   }
54
55 public:
56   void prepare() override
57   {
58     if (_create)
59       return;
60
61     _create = create();
62     assert(_create);
63
64     _setup = setup();
65   }
66   virtual bool create() = 0;
67   virtual bool setup() = 0;
68
69 protected:
70   xnn_operator_t _kernel_op;
71   bool _create;
72   bool _setup;
73   const std::shared_ptr<ExternalContext> _external_context;
74 };
75
76 } // namespace ops
77 } // namespace xnnpack
78 } // namespace backend
79 } // namespace onert
80
81 #endif // __ONERT_BACKEND_XNNPACK_OPS_LAYER_H__