• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2020 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/runtime/CL/functions/CLPixelWiseMultiplication.h"
25 
26 #include "arm_compute/core/CL/ICLTensor.h"
27 #include "arm_compute/runtime/CL/CLScheduler.h"
28 #include "src/core/CL/kernels/CLFillBorderKernel.h"
29 #include "src/core/CL/kernels/CLPixelWiseMultiplicationKernel.h"
30 #include "support/MemorySupport.h"
31 
32 #include <utility>
33 
34 namespace arm_compute
35 {
36 namespace
37 {
select_border_input(ITensorPack & tensors)38 ITensorPack select_border_input(ITensorPack &tensors)
39 {
40     ITensorPack pack;
41     if(tensors.get_tensor(TensorType::ACL_DST)->info()->dimension(0) > 1)
42     {
43         if(tensors.get_const_tensor(TensorType::ACL_SRC_1)->info()->dimension(0) == 1)
44         {
45             pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_1));
46         }
47         else
48         {
49             pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_0));
50         }
51     }
52     return pack;
53 }
54 } // namespace
55 
56 namespace experimental
57 {
CLPixelWiseMultiplication()58 CLPixelWiseMultiplication::CLPixelWiseMultiplication()
59     : _border_handler(support::cpp14::make_unique<CLFillBorderKernel>())
60 {
61 }
62 
configure(const CLCompileContext & compile_context,ITensorInfo * input1,ITensorInfo * input2,ITensorInfo * output,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)63 void CLPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, float scale,
64                                           ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
65 {
66     auto k = arm_compute::support::cpp14::make_unique<CLPixelWiseMultiplicationKernel>();
67     k->configure(compile_context, input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
68     _kernel = std::move(k);
69 
70     if(output->dimension(0) > 1)
71     {
72         ITensorInfo *broadcasted_info = (input1->dimension(0) == 1) ? input1 : input2;
73 
74         if(broadcasted_info->dimension(0) == 1)
75         {
76             _border_handler->configure(compile_context, broadcasted_info, _kernel->border_size(), BorderMode::REPLICATE);
77         }
78     }
79 }
80 
validate(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * output,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)81 Status CLPixelWiseMultiplication::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
82                                            ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
83 {
84     return CLPixelWiseMultiplicationKernel::validate(input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
85 }
86 
run(ITensorPack & tensors)87 void CLPixelWiseMultiplication::run(ITensorPack &tensors)
88 {
89     auto border_pack = select_border_input(tensors);
90     CLScheduler::get().enqueue_op(*_border_handler, border_pack);
91     ICLOperator::run(tensors);
92 }
93 
CLComplexPixelWiseMultiplication()94 CLComplexPixelWiseMultiplication::CLComplexPixelWiseMultiplication()
95     : _border_handler(support::cpp14::make_unique<CLFillBorderKernel>())
96 {
97 }
98 
configure(const CLCompileContext & compile_context,ITensorInfo * input1,ITensorInfo * input2,ITensorInfo * output,const ActivationLayerInfo & act_info)99 void CLComplexPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info)
100 {
101     auto k = arm_compute::support::cpp14::make_unique<CLComplexPixelWiseMultiplicationKernel>();
102     k->configure(compile_context, input1, input2, output, act_info);
103     _kernel = std::move(k);
104 
105     if(output->dimension(0) > 1)
106     {
107         ITensorInfo *broadcasted_info = (input1->dimension(0) == 1) ? input1 : input2;
108 
109         if(broadcasted_info->dimension(0) == 1)
110         {
111             _border_handler->configure(compile_context, broadcasted_info, _kernel->border_size(), BorderMode::REPLICATE);
112         }
113     }
114 }
115 
validate(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * output,const ActivationLayerInfo & act_info)116 Status CLComplexPixelWiseMultiplication::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info)
117 {
118     return CLComplexPixelWiseMultiplicationKernel::validate(input1, input2, output, act_info);
119 }
120 
run(ITensorPack & tensors)121 void CLComplexPixelWiseMultiplication::run(ITensorPack &tensors)
122 {
123     auto border_pack = select_border_input(tensors);
124     CLScheduler::get().enqueue_op(*_border_handler, border_pack);
125     ICLOperator::run(tensors);
126 }
127 } // namespace experimental
128 
129 struct CLPixelWiseMultiplication::Impl
130 {
131     const ICLTensor                                         *src_0{ nullptr };
132     const ICLTensor                                         *src_1{ nullptr };
133     ICLTensor                                               *dst{ nullptr };
134     std::unique_ptr<experimental::CLPixelWiseMultiplication> op{ nullptr };
135 };
136 
CLPixelWiseMultiplication()137 CLPixelWiseMultiplication::CLPixelWiseMultiplication()
138     : _impl(support::cpp14::make_unique<Impl>())
139 {
140 }
141 CLPixelWiseMultiplication::CLPixelWiseMultiplication(CLPixelWiseMultiplication &&) = default;
142 CLPixelWiseMultiplication &CLPixelWiseMultiplication::operator=(CLPixelWiseMultiplication &&) = default;
143 CLPixelWiseMultiplication::~CLPixelWiseMultiplication()                                       = default;
144 
configure(ICLTensor * input1,ICLTensor * input2,ICLTensor * output,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)145 void CLPixelWiseMultiplication::configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, float scale,
146                                           ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
147 {
148     configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
149 }
150 
configure(const CLCompileContext & compile_context,ICLTensor * input1,ICLTensor * input2,ICLTensor * output,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)151 void CLPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output, float scale,
152                                           ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
153 {
154     _impl->src_0 = input1;
155     _impl->src_1 = input2;
156     _impl->dst   = output;
157     _impl->op    = arm_compute::support::cpp14::make_unique<experimental::CLPixelWiseMultiplication>();
158     _impl->op->configure(compile_context, input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy, act_info);
159 }
160 
validate(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * output,float scale,ConvertPolicy overflow_policy,RoundingPolicy rounding_policy,const ActivationLayerInfo & act_info)161 Status CLPixelWiseMultiplication::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
162                                            ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
163 {
164     return experimental::CLPixelWiseMultiplication::validate(input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
165 }
166 
run()167 void CLPixelWiseMultiplication::run()
168 {
169     ITensorPack pack;
170     pack.add_tensor(TensorType::ACL_SRC_0, _impl->src_0);
171     pack.add_tensor(TensorType::ACL_SRC_1, _impl->src_1);
172     pack.add_tensor(TensorType::ACL_DST, _impl->dst);
173 
174     _impl->op->run(pack);
175 }
176 
177 struct CLComplexPixelWiseMultiplication::Impl
178 {
179     const ICLTensor                                                *src_0{ nullptr };
180     const ICLTensor                                                *src_1{ nullptr };
181     ICLTensor                                                      *dst{ nullptr };
182     std::unique_ptr<experimental::CLComplexPixelWiseMultiplication> op{ nullptr };
183 };
184 
CLComplexPixelWiseMultiplication()185 CLComplexPixelWiseMultiplication::CLComplexPixelWiseMultiplication()
186     : _impl(support::cpp14::make_unique<Impl>())
187 {
188 }
189 CLComplexPixelWiseMultiplication::CLComplexPixelWiseMultiplication(CLComplexPixelWiseMultiplication &&) = default;
190 CLComplexPixelWiseMultiplication &CLComplexPixelWiseMultiplication::operator=(CLComplexPixelWiseMultiplication &&) = default;
191 CLComplexPixelWiseMultiplication::~CLComplexPixelWiseMultiplication()                                              = default;
192 
configure(ICLTensor * input1,ICLTensor * input2,ICLTensor * output,const ActivationLayerInfo & act_info)193 void CLComplexPixelWiseMultiplication::configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info)
194 {
195     configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, act_info);
196 }
197 
configure(const CLCompileContext & compile_context,ICLTensor * input1,ICLTensor * input2,ICLTensor * output,const ActivationLayerInfo & act_info)198 void CLComplexPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info)
199 {
200     _impl->src_0 = input1;
201     _impl->src_1 = input2;
202     _impl->dst   = output;
203     _impl->op    = arm_compute::support::cpp14::make_unique<experimental::CLComplexPixelWiseMultiplication>();
204     _impl->op->configure(compile_context, input1->info(), input2->info(), output->info(), act_info);
205 }
206 
validate(const ITensorInfo * input1,const ITensorInfo * input2,const ITensorInfo * output,const ActivationLayerInfo & act_info)207 Status CLComplexPixelWiseMultiplication::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info)
208 {
209     return experimental::CLComplexPixelWiseMultiplication::validate(input1, input2, output, act_info);
210 }
211 
run()212 void CLComplexPixelWiseMultiplication::run()
213 {
214     ITensorPack pack;
215     pack.add_tensor(TensorType::ACL_SRC_0, _impl->src_0);
216     pack.add_tensor(TensorType::ACL_SRC_1, _impl->src_1);
217     pack.add_tensor(TensorType::ACL_DST, _impl->dst);
218 
219     _impl->op->run(pack);
220 }
221 } // namespace arm_compute
222