diff options
author | Manuel Bottini <manuel.bottini@arm.com> | 2021-06-09 16:37:32 +0100 |
---|---|---|
committer | Manuel Bottini <manuel.bottini@arm.com> | 2021-06-15 16:31:27 +0000 |
commit | 94f799e8f6f605333d40472860fb472e8ba6d83d (patch) | |
tree | ce528244814463ed42dc86a84d54ea870c75d592 /src/runtime/gpu/cl | |
parent | 36dff9f81e3a95aea19fcc7246a4896930a14bc6 (diff) | |
download | ComputeLibrary-94f799e8f6f605333d40472860fb472e8ba6d83d.tar.gz |
Fix incorrect memory handling in ported functions
Details of the functions:
- ClSoftmax
- CpuSoftmax
- CpuPool2d
Change-Id: Icd2c14d5df010c3b2301e2693ce6f414d7c61916
Resolves: COMPMID-4404
Signed-off-by: Manuel Bottini <manuel.bottini@arm.com>
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/5797
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'src/runtime/gpu/cl')
-rw-r--r-- | src/runtime/gpu/cl/operators/ClSoftmax.cpp | 194 | ||||
-rw-r--r-- | src/runtime/gpu/cl/operators/ClSoftmax.h | 36 |
2 files changed, 59 insertions, 171 deletions
diff --git a/src/runtime/gpu/cl/operators/ClSoftmax.cpp b/src/runtime/gpu/cl/operators/ClSoftmax.cpp index c3ec7cc0da..975bb0b932 100644 --- a/src/runtime/gpu/cl/operators/ClSoftmax.cpp +++ b/src/runtime/gpu/cl/operators/ClSoftmax.cpp @@ -24,82 +24,30 @@ #include "src/runtime/gpu/cl/operators/ClSoftmax.h" #include "arm_compute/core/utils/misc/ShapeCalculator.h" #include "src/core/gpu/cl/kernels/ClSoftmaxKernel.h" +#include "src/core/helpers/MemoryHelpers.h" #include "src/core/helpers/SoftmaxHelpers.h" #include "src/runtime/gpu/cl/operators/ClPermute.h" +#include "src/runtime/gpu/cl/utils/ClAuxTensorHandler.h" #include "support/Cast.h" +using namespace arm_compute::experimental; + namespace arm_compute { namespace opencl { -namespace -{ -void run_permute(ClPermute *op, const ITensor *src, ITensor *dst) -{ - ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst, op); - ITensorPack pack; - pack.add_const_tensor(TensorType::ACL_SRC, src); - pack.add_tensor(TensorType::ACL_DST, dst); - op->run(pack); -} -} // namespace - ClSoftmax::ClSoftmax() : _permute_input(std::make_unique<ClPermute>()), _permute_output(std::make_unique<ClPermute>()), _max_shift_exp_sum_kernel(std::make_unique<kernels::ClLogits1DMaxShiftExpSumKernel>()), _norm_kernel(std::make_unique<kernels::ClLogits1DNormKernel>()), - _max_info(_internal_info[static_cast<uint32_t>(InternalTensorIdx::MAX)]), - _sum_info(_internal_info[static_cast<uint32_t>(InternalTensorIdx::SUM)]), - _tmp_info(_internal_info[static_cast<uint32_t>(InternalTensorIdx::TMP)]), - _permuted_src_info(_internal_info[static_cast<uint32_t>(InternalTensorIdx::PERMUTED_SRC)]), - _permuted_dst_info(_internal_info[static_cast<uint32_t>(InternalTensorIdx::PERMUTED_DST)]) -{ -} - -TensorType ClSoftmax::convert_internal_idx_to_tensor_type(InternalTensorIdx idx) const -{ - switch(idx) - { - case InternalTensorIdx::MAX: - return TensorType::ACL_INT_0; - case InternalTensorIdx::SUM: - return TensorType::ACL_INT_1; - case InternalTensorIdx::TMP: - return TensorType::ACL_INT_2; - case InternalTensorIdx::PERMUTED_SRC: - return TensorType::ACL_INT_3; - case InternalTensorIdx::PERMUTED_DST: - return TensorType::ACL_INT_4; - default: - ARM_COMPUTE_ERROR("invalid internal tensor index is given."); - break; - }; - return TensorType::ACL_UNKNOWN; -} - -void ClSoftmax::create_internal_tensor(TensorInfo &info, InternalTensorIdx idx) -{ - const auto tensor_idx = static_cast<uint32_t>(idx); - if(!_internal_tensor[tensor_idx]) - { - _internal_tensor[tensor_idx] = std::make_unique<CLTensor>(); - } - _internal_tensor[tensor_idx]->allocator()->init(info); -} - -void ClSoftmax::create_internal_tensor() + _max_info(), + _sum_info(), + _tmp_info(), + _permuted_src_info(), + _permuted_dst_info(), + _aux_mem(InternalTensorIdx::COUNT) { - for(uint32_t i = 0; i < static_cast<uint32_t>(InternalTensorIdx::COUNT); i++) - { - const auto tensor_idx = static_cast<InternalTensorIdx>(i); - - if(!_needs_permute && (tensor_idx == InternalTensorIdx::PERMUTED_DST || tensor_idx == InternalTensorIdx::PERMUTED_SRC)) - { - continue; - } - create_internal_tensor(_internal_info[i], static_cast<InternalTensorIdx>(i)); - } } void ClSoftmax::configure(const CLCompileContext &compile_context, const ITensorInfo &src, ITensorInfo &dst, const SoftmaxKernelInfo &info) @@ -137,6 +85,13 @@ void ClSoftmax::configure(const CLCompileContext &compile_context, const ITensor const auto perm_info = softmax_helpers::get_permutation_vector_from_softmax_axis(actual_axis); _permute_output->configure(compile_context, &_permuted_dst_info, &dst, perm_info); } + + _aux_mem[InternalTensorIdx::SUM] = MemoryInfo(offset_int_vec(InternalTensorIdx::SUM), MemoryLifetime::Temporary, _sum_info.total_size()); + _aux_mem[InternalTensorIdx::TMP] = MemoryInfo(offset_int_vec(InternalTensorIdx::TMP), MemoryLifetime::Temporary, _tmp_info.total_size()); + _aux_mem[InternalTensorIdx::MAX] = MemoryInfo(offset_int_vec(InternalTensorIdx::MAX), MemoryLifetime::Temporary, _max_info.total_size()); + + _aux_mem[InternalTensorIdx::PERMUTED_SRC] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), MemoryLifetime::Temporary, _permuted_src_info.total_size()); + _aux_mem[InternalTensorIdx::PERMUTED_DST] = MemoryInfo(offset_int_vec(InternalTensorIdx::PERMUTED_DST), MemoryLifetime::Temporary, _permuted_dst_info.total_size()); } Status ClSoftmax::validate(const ITensorInfo &src, const ITensorInfo &dst, const SoftmaxKernelInfo &info) @@ -172,105 +127,60 @@ Status ClSoftmax::validate(const ITensorInfo &src, const ITensorInfo &dst, const return Status{}; } -void ClSoftmax::import_workspace_memory(ITensorPack &tensors) +void ClSoftmax::run(ITensorPack &tensors) { - auto import_workspace_memory = [this, &tensors](InternalTensorIdx idx) - { - const auto workspace_idx = convert_internal_idx_to_tensor_type(idx); - auto imported_tensor = tensors.get_tensor(workspace_idx); - if(imported_tensor) - { - auto imported_memory = utils::cast::polymorphic_downcast<ICLTensor *>(imported_tensor)->cl_buffer(); - _internal_tensor[static_cast<uint32_t>(idx)].get()->allocator()->import_memory(imported_memory); - } - }; + auto src = tensors.get_const_tensor(TensorType::ACL_SRC); + auto dst = tensors.get_tensor(TensorType::ACL_DST); - import_workspace_memory(InternalTensorIdx::PERMUTED_SRC); - import_workspace_memory(InternalTensorIdx::PERMUTED_DST); - import_workspace_memory(InternalTensorIdx::MAX); - import_workspace_memory(InternalTensorIdx::SUM); - import_workspace_memory(InternalTensorIdx::TMP); -} + CLAuxTensorHandler sum(offset_int_vec(InternalTensorIdx::SUM), _sum_info, tensors, false); + CLAuxTensorHandler tmp(offset_int_vec(InternalTensorIdx::TMP), _tmp_info, tensors, false); + CLAuxTensorHandler max(offset_int_vec(InternalTensorIdx::MAX), _max_info, tensors, false); + + CLAuxTensorHandler permuted_src(offset_int_vec(InternalTensorIdx::PERMUTED_SRC), _permuted_src_info, tensors, false); + CLAuxTensorHandler permuted_dst(offset_int_vec(InternalTensorIdx::PERMUTED_DST), _permuted_dst_info, tensors, false); -void ClSoftmax::run_source_permute(const ITensor *src) -{ if(_needs_permute) { - auto permuted_src = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::PERMUTED_SRC)].get(); - run_permute(_permute_input.get(), src, permuted_src); + ITensorPack pack; + pack.add_const_tensor(TensorType::ACL_SRC, src); + pack.add_tensor(TensorType::ACL_DST, permuted_src.get()); + _permute_input.get()->run(pack); } -} -void ClSoftmax::run_destination_permute(ITensor *dst) -{ + ITensorPack sum_pack; + ITensorPack norm_pack; if(_needs_permute) { - auto permuted_dst = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::PERMUTED_DST)].get(); - run_permute(_permute_output.get(), permuted_dst, dst); + sum_pack.add_const_tensor(TensorType::ACL_SRC, permuted_src.get()); + norm_pack.add_tensor(TensorType::ACL_DST, permuted_dst.get()); } -} - -void ClSoftmax::run_max_sum(const ITensor *src) -{ - auto max = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::MAX)].get(); - auto sum = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::SUM)].get(); - auto tmp = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::TMP)].get(); - - ARM_COMPUTE_ERROR_ON_NULLPTR(src, tmp, max, sum); + else + { + sum_pack.add_const_tensor(TensorType::ACL_SRC, src); + norm_pack.add_tensor(TensorType::ACL_DST, dst); + } + sum_pack.add_tensor(TensorType::ACL_DST, tmp.get()); + sum_pack.add_tensor(TensorType::ACL_INT_0, max.get()); + sum_pack.add_tensor(TensorType::ACL_INT_1, sum.get()); - ITensorPack sum_pack; - sum_pack.add_const_tensor(TensorType::ACL_SRC, src); - sum_pack.add_tensor(TensorType::ACL_DST, tmp); - sum_pack.add_tensor(TensorType::ACL_INT_0, max); - sum_pack.add_tensor(TensorType::ACL_INT_1, sum); + norm_pack.add_const_tensor(TensorType::ACL_SRC, tmp.get()); + norm_pack.add_tensor(TensorType::ACL_INT_0, sum.get()); CLScheduler::get().enqueue_op(*_max_shift_exp_sum_kernel.get(), sum_pack, false); -} - -void ClSoftmax::run_norm(ITensor *dst) -{ - auto sum = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::SUM)].get(); - auto tmp = _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::TMP)].get(); - - ARM_COMPUTE_ERROR_ON_NULLPTR(tmp, sum, dst); - - ITensorPack norm_pack; - norm_pack.add_const_tensor(TensorType::ACL_SRC, tmp); - norm_pack.add_tensor(TensorType::ACL_DST, dst); - norm_pack.add_tensor(TensorType::ACL_INT_0, sum); - CLScheduler::get().enqueue_op(*_norm_kernel.get(), norm_pack, false); -} - -void ClSoftmax::run(ITensorPack &tensors) -{ - create_internal_tensor(); - - auto src = tensors.get_const_tensor(TensorType::ACL_SRC); - auto dst = tensors.get_tensor(TensorType::ACL_DST); - - import_workspace_memory(tensors); - run_source_permute(src); - run_max_sum(!_needs_permute ? src : _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::PERMUTED_SRC)].get()); - run_norm(!_needs_permute ? dst : _internal_tensor[static_cast<uint32_t>(InternalTensorIdx::PERMUTED_DST)].get()); - run_destination_permute(dst); -} - -experimental::MemoryRequirements ClSoftmax::workspace() const -{ - experimental::MemoryRequirements req{}; - - req.emplace_back(convert_internal_idx_to_tensor_type(InternalTensorIdx::SUM), _sum_info.total_size(), 0); - req.emplace_back(convert_internal_idx_to_tensor_type(InternalTensorIdx::TMP), _tmp_info.total_size(), 0); - req.emplace_back(convert_internal_idx_to_tensor_type(InternalTensorIdx::MAX), _max_info.total_size(), 0); if(_needs_permute) { - req.emplace_back(convert_internal_idx_to_tensor_type(InternalTensorIdx::PERMUTED_SRC), _permuted_src_info.total_size(), 0); - req.emplace_back(convert_internal_idx_to_tensor_type(InternalTensorIdx::PERMUTED_DST), _permuted_dst_info.total_size(), 0); + ITensorPack pack; + pack.add_const_tensor(TensorType::ACL_SRC, permuted_dst.get()); + pack.add_tensor(TensorType::ACL_DST, dst); + _permute_output.get()->run(pack); } +} - return req; +experimental::MemoryRequirements ClSoftmax::workspace() const +{ + return _aux_mem; } } // namespace opencl } // namespace arm_compute
\ No newline at end of file diff --git a/src/runtime/gpu/cl/operators/ClSoftmax.h b/src/runtime/gpu/cl/operators/ClSoftmax.h index e38b7c595a..f19a51fc5e 100644 --- a/src/runtime/gpu/cl/operators/ClSoftmax.h +++ b/src/runtime/gpu/cl/operators/ClSoftmax.h @@ -67,7 +67,7 @@ public: experimental::MemoryRequirements workspace() const override; private: - enum class InternalTensorIdx + enum InternalTensorIdx { MAX = 0, SUM, @@ -77,41 +77,19 @@ private: COUNT }; - /** Create a single internal tensor - * - * @param[in] info The information used to create a tensor - * @param[in] idx The index within the internal array the created tensor will be held - */ - void create_internal_tensor(TensorInfo &info, InternalTensorIdx idx); - /** Create all required internal tensors */ - void create_internal_tensor(); - /** Function to convert from internal tensor index to @ref TensorType used externally */ - TensorType convert_internal_idx_to_tensor_type(InternalTensorIdx idx) const; - /** Function to import workspace memory allocated by the caller into internal tensor instances */ - void import_workspace_memory(ITensorPack &tensors); - /** Function to permute the given source tensor when permutation is required */ - void run_source_permute(const ITensor *src); - /** Function to permute the intemediate tensor to the final destination tensor when permutation is required */ - void run_destination_permute(ITensor *dst); - /** Function to run @ref arm_compute::opencl::kernels::ClLogits1DMaxShiftExpSumKernel */ - void run_max_sum(const ITensor *src); - /** Function to run @ref kernels::ClLogits1DNormKernel */ - void run_norm(ITensor *dst); - std::unique_ptr<ClPermute> _permute_input; std::unique_ptr<ClPermute> _permute_output; std::unique_ptr<kernels::ClLogits1DMaxShiftExpSumKernel> _max_shift_exp_sum_kernel; std::unique_ptr<kernels::ClLogits1DNormKernel> _norm_kernel; bool _needs_permute{ false }; - std::array<TensorInfo, static_cast<uint32_t>(InternalTensorIdx::COUNT)> _internal_info{}; - std::array<std::unique_ptr<CLTensor>, static_cast<uint32_t>(InternalTensorIdx::COUNT)> _internal_tensor{}; + TensorInfo _max_info; + TensorInfo _sum_info; + TensorInfo _tmp_info; + TensorInfo _permuted_src_info; + TensorInfo _permuted_dst_info; - TensorInfo &_max_info; - TensorInfo &_sum_info; - TensorInfo &_tmp_info; - TensorInfo &_permuted_src_info; - TensorInfo &_permuted_dst_info; + experimental::MemoryRequirements _aux_mem{}; }; } // opencl |