Compute Library
 21.11
CLGEMMConvolutionLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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 
29 #include "arm_compute/core/Utils.h"
37 #include "support/Cast.h"
38 
39 #include <cmath>
40 #include <memory>
41 #include <tuple>
42 
43 namespace arm_compute
44 {
46 using namespace arm_compute::utils::cast;
47 using namespace arm_compute::experimental;
48 
49 struct CLGEMMConvolutionLayer::Impl
50 {
51  const ITensor *weights{ nullptr };
52  std::unique_ptr<opencl::ClGemmConv2d> op{ nullptr };
53  ITensorPack run_pack{};
54  ITensorPack prep_pack{};
55  MemoryGroup memory_group{};
56  IWeightsManager *weights_manager{ nullptr };
57  MemoryRequirements aux_mem_req{};
58  WorkspaceData<CLTensor> workspace_tensors{};
59  bool is_prepared{ false };
60 };
61 
62 CLGEMMConvolutionLayer::CLGEMMConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
63  : _impl(std::make_unique<Impl>())
64 {
65  _impl->memory_group = MemoryGroup(memory_manager);
66  _impl->weights_manager = weights_manager;
67 }
68 
70 
72  const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
73 {
74  configure(CLKernelLibrary::get().get_compile_context(), input, weights, biases, output, conv_info, weights_info, dilation, act_info, num_groups, post_ops);
75 }
76 
77 void CLGEMMConvolutionLayer::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output,
78  const PadStrideInfo &conv_info,
79  const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups, const experimental::PostOpList<ICLTensor *> &post_ops)
80 {
81  ARM_COMPUTE_ERROR_ON_NULLPTR(input, weights, output);
82  _impl->weights = weights;
83  _impl->op = std::make_unique<opencl::ClGemmConv2d>();
84  // Convert post op arguments to ITensorInfo
85  auto transformed_post_ops = experimental::transform_post_op_list_arguments<ICLTensor *, ITensorInfo *>(post_ops, [](auto tensor)
86  {
87  return tensor->info();
88  });
89  const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, false, num_groups, transformed_post_ops);
90  _impl->op->configure(compile_context, input->info(), weights->info(), (biases != nullptr ? biases->info() : nullptr), output->info(), conv2d_info, weights_info);
91 
92  _impl->run_pack =
93  {
94  { TensorType::ACL_SRC_0, input },
95  { TensorType::ACL_SRC_1, weights },
96  { TensorType::ACL_SRC_2, biases },
97  { TensorType::ACL_DST, output }
98  };
99  // Add post op tensors
100  size_t post_op_tensor_index = 0;
101  for(const auto &op : post_ops.get_list())
102  {
103  for(auto &tensor : op->arguments())
104  {
105  _impl->run_pack.add_const_tensor(experimental::get_post_op_arg_type(post_op_tensor_index++), *tensor);
106  }
107  }
108  _impl->prep_pack =
109  {
110  { TensorType::ACL_SRC_1, weights },
111  { TensorType::ACL_SRC_2, biases },
112  };
113  _impl->aux_mem_req = _impl->op->workspace();
114  _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
115 }
116 
118  const WeightsInfo &weights_info, const Size2D &dilation, const ActivationLayerInfo &act_info, unsigned int num_groups, const experimental::PostOpList<ITensorInfo *> &post_ops)
119 {
120  const Conv2dInfo conv2d_info = Conv2dInfo(conv_info, dilation, act_info, false, num_groups, post_ops);
121  return opencl::ClGemmConv2d::validate(input, weights, biases, output, conv2d_info, weights_info);
122 }
123 
125 {
126  prepare();
127  MemoryGroupResourceScope scope_mg(_impl->memory_group);
128  _impl->op->run(_impl->run_pack);
129 }
130 
132 {
133  if(!_impl->is_prepared)
134  {
135  _impl->op->prepare(_impl->prep_pack);
136  auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
137  _impl->aux_mem_req.end(),
138  [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
139 
140  if(has_reshape != std::end(_impl->aux_mem_req))
141  {
142  _impl->weights->mark_as_unused();
143  }
144  else
145  {
146  // Pack the B matrix to be used as the underlying GEMM performs no reshapes
147  _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->weights);
148  }
149  release_temporaries(_impl->aux_mem_req, _impl->workspace_tensors);
150  _impl->is_prepared = true;
151  }
152 }
153 } // namespace arm_compute
experimental::PostOpList< ITensorInfo * > post_ops
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Status class.
Definition: Error.h:52
Activation Layer Information class.
Definition: Types.h:1509
Copyright (c) 2017-2021 Arm Limited.
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:132
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const Conv2dInfo &conv2d_info, const WeightsInfo &weights_info=WeightsInfo())
Static function to check if given info will lead to a valid configuration.
TensorType get_post_op_arg_type(size_t index)
Get post op argument TensorType from post op argument index in a flattened, ordered post op argument ...
Definition: PostOpUtils.h:79
Convolution Layer Weights Information class.
Definition: Types.h:1728
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
CLGEMMConvolutionLayer(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Constructor.
const unsigned int num_groups
Definition: Im2Col.cpp:153
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
Padding and stride information class.
Definition: Types.h:656
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
void run() override
Run the kernels contained in the function.
Weights manager interface to handle weights transformations.
void release_temporaries(const experimental::MemoryRequirements &mem_reqs, WorkspaceData< TensorType > &workspace)
Utility function to release tensors with lifetime marked as Prepare.
Descriptor used by the 2d Convolution function.
CLCompileContext class.
static Status validate(const ITensorInfo *input, const ITensorInfo *weights, const ITensorInfo *biases, const ITensorInfo *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), unsigned int num_groups=1, const experimental::PostOpList< ITensorInfo *> &post_ops=experimental::PostOpList< ITensorInfo *> {})
Static function to check if given info will lead to a valid configuration of CLGEMMConvolutionLayer.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
Class for specifying the size of an image or rectangle.
Definition: Size2D.h:34
void prepare() override
Prepare the function for executing.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void configure(const ICLTensor *input, const ICLTensor *weights, const ICLTensor *biases, ICLTensor *output, const PadStrideInfo &conv_info, const WeightsInfo &weights_info=WeightsInfo(), const Size2D &dilation=Size2D(1U, 1U), const ActivationLayerInfo &act_info=ActivationLayerInfo(), unsigned int num_groups=1, const experimental::PostOpList< ICLTensor *> &post_ops=experimental::PostOpList< ICLTensor *> {})
Set the input and output tensors.
A sequence of PostOps that can be appended to the end of other operators.
Definition: IPostOp.h:119
~CLGEMMConvolutionLayer()
Default destructor.