Compute Library
 21.02
CLPixelWiseMultiplication.cpp
Go to the documentation of this file.
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  */
25 
30 
31 #include <utility>
32 
33 namespace arm_compute
34 {
35 namespace
36 {
37 ITensorPack select_border_input(ITensorPack &tensors)
38 {
39  ITensorPack pack;
40  if(tensors.get_tensor(TensorType::ACL_DST)->info()->dimension(0) > 1)
41  {
42  if(tensors.get_const_tensor(TensorType::ACL_SRC_1)->info()->dimension(0) == 1)
43  {
44  pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_1));
45  }
46  else
47  {
48  pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(TensorType::ACL_SRC_0));
49  }
50  }
51  return pack;
52 }
53 } // namespace
54 
55 namespace experimental
56 {
58  : _border_handler(std::make_unique<CLFillBorderKernel>())
59 {
60 }
61 
62 void CLPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, float scale,
63  ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
64 {
65  auto k = std::make_unique<CLPixelWiseMultiplicationKernel>();
66  k->configure(compile_context, input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
67  _kernel = std::move(k);
68 
69  if(output->dimension(0) > 1)
70  {
71  ITensorInfo *broadcasted_info = (input1->dimension(0) == 1) ? input1 : input2;
72 
73  if(broadcasted_info->dimension(0) == 1)
74  {
75  _border_handler->configure(compile_context, broadcasted_info, _kernel->border_size(), BorderMode::REPLICATE);
76  }
77  }
78 }
79 
80 Status CLPixelWiseMultiplication::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
81  ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
82 {
83  return CLPixelWiseMultiplicationKernel::validate(input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
84 }
85 
87 {
88  auto border_pack = select_border_input(tensors);
89  CLScheduler::get().enqueue_op(*_border_handler, border_pack);
90  ICLOperator::run(tensors);
91 }
92 
94  : _border_handler(std::make_unique<CLFillBorderKernel>())
95 {
96 }
97 
98 void CLComplexPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info)
99 {
100  auto k = std::make_unique<CLComplexPixelWiseMultiplicationKernel>();
101  k->configure(compile_context, input1, input2, output, act_info);
102  _kernel = std::move(k);
103 
104  if(output->dimension(0) > 1)
105  {
106  ITensorInfo *broadcasted_info = (input1->dimension(0) == 1) ? input1 : input2;
107 
108  if(broadcasted_info->dimension(0) == 1)
109  {
110  _border_handler->configure(compile_context, broadcasted_info, _kernel->border_size(), BorderMode::REPLICATE);
111  }
112  }
113 }
114 
116 {
117  return CLComplexPixelWiseMultiplicationKernel::validate(input1, input2, output, act_info);
118 }
119 
121 {
122  auto border_pack = select_border_input(tensors);
123  CLScheduler::get().enqueue_op(*_border_handler, border_pack);
124  ICLOperator::run(tensors);
125 }
126 } // namespace experimental
127 
128 struct CLPixelWiseMultiplication::Impl
129 {
130  const ICLTensor *src_0{ nullptr };
131  const ICLTensor *src_1{ nullptr };
132  ICLTensor *dst{ nullptr };
133  std::unique_ptr<experimental::CLPixelWiseMultiplication> op{ nullptr };
134 };
135 
137  : _impl(std::make_unique<Impl>())
138 {
139 }
143 
145  ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
146 {
147  configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
148 }
149 
150 void CLPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output, float scale,
151  ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
152 {
153  _impl->src_0 = input1;
154  _impl->src_1 = input2;
155  _impl->dst = output;
156  _impl->op = std::make_unique<experimental::CLPixelWiseMultiplication>();
157  _impl->op->configure(compile_context, input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy, act_info);
158 }
159 
160 Status CLPixelWiseMultiplication::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale,
161  ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info)
162 {
163  return experimental::CLPixelWiseMultiplication::validate(input1, input2, output, scale, overflow_policy, rounding_policy, act_info);
164 }
165 
167 {
168  ITensorPack pack;
169  pack.add_tensor(TensorType::ACL_SRC_0, _impl->src_0);
170  pack.add_tensor(TensorType::ACL_SRC_1, _impl->src_1);
171  pack.add_tensor(TensorType::ACL_DST, _impl->dst);
172 
173  _impl->op->run(pack);
174 }
175 
176 struct CLComplexPixelWiseMultiplication::Impl
177 {
178  const ICLTensor *src_0{ nullptr };
179  const ICLTensor *src_1{ nullptr };
180  ICLTensor *dst{ nullptr };
181  std::unique_ptr<experimental::CLComplexPixelWiseMultiplication> op{ nullptr };
182 };
183 
185  : _impl(std::make_unique<Impl>())
186 {
187 }
191 
193 {
194  configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, act_info);
195 }
196 
197 void CLComplexPixelWiseMultiplication::configure(const CLCompileContext &compile_context, ICLTensor *input1, ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info)
198 {
199  _impl->src_0 = input1;
200  _impl->src_1 = input2;
201  _impl->dst = output;
202  _impl->op = std::make_unique<experimental::CLComplexPixelWiseMultiplication>();
203  _impl->op->configure(compile_context, input1->info(), input2->info(), output->info(), act_info);
204 }
205 
207 {
208  return experimental::CLComplexPixelWiseMultiplication::validate(input1, input2, output, act_info);
209 }
210 
212 {
213  ITensorPack pack;
214  pack.add_tensor(TensorType::ACL_SRC_0, _impl->src_0);
215  pack.add_tensor(TensorType::ACL_SRC_1, _impl->src_1);
216  pack.add_tensor(TensorType::ACL_DST, _impl->dst);
217 
218  _impl->op->run(pack);
219 }
220 } // namespace arm_compute
void configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Initialise the kernel&#39;s inputs, output.
~CLComplexPixelWiseMultiplication()
Default Destructor.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
static CLScheduler & get()
Access the scheduler singleton.
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
void run() override
Run the kernels contained in the function.
Basic function to run CLComplexPixelWiseMultiplicationKernel.
void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Initialise the kernel&#39;s inputs, output and convertion policy.
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Status class.
Definition: Error.h:52
CLPixelWiseMultiplication & operator=(const CLPixelWiseMultiplication &)=delete
Prevent instances of this class from being copied (As this class contains pointers) ...
Activation Layer Information class.
Definition: Types.h:1550
Copyright (c) 2017-2021 Arm Limited.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLPixelWiseMultiplicatio...
void enqueue_op(ICLKernel &kernel, ITensorPack &tensors, bool flush=true)
Schedule the execution of the passed kernel if possible.
RoundingPolicy
Rounding method.
Definition: Rounding.h:30
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLComplexPixelWiseMultip...
Basic function to run CLPixelWiseMultiplicationKernel.
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLPixelWiseMultiplicatio...
CLCompileContext class.
void configure(const CLCompileContext &compile_context, ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Initialise the kernel&#39;s inputs, output and convertion policy.
CLComplexPixelWiseMultiplication & operator=(const CLComplexPixelWiseMultiplication &)=delete
Prevent instances of this class from being copied (As this class contains pointers) ...
Interface for filling the border of a kernel.
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
void run() override
Run the kernels contained in the function.
Pixels outside the image are assumed to have the same value as the closest image pixel.
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLComplexPixelWiseMultip...
Tensor packing service.
Definition: ITensorPack.h:37
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLComplexPixelWiseMultip...
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
Definition: CLOperator.cpp:38
void run(ITensorPack &tensors) override
Run the kernels contained in the function.
void configure(ICLTensor *input1, ICLTensor *input2, ICLTensor *output, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Initialise the kernel&#39;s inputs, output.
ConvertPolicy
Policy to handle overflow.
Definition: Types.h:385
static Status validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy, const ActivationLayerInfo &act_info=ActivationLayerInfo())
Static function to check if given info will lead to a valid configuration of CLPixelWiseMultiplicatio...
void add_tensor(int id, ITensor *tensor)
Add tensor to the pack.
Definition: ITensorPack.cpp:30
~CLPixelWiseMultiplication()
Default Destructor.