|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +// BSD 3-Clause License |
| 3 | +// |
| 4 | +// Copyright (c) 2021, NVIDIA Corporation |
| 5 | +// sum rights reserved. |
| 6 | +// |
| 7 | +// Redistribution and use in source and binary forms, with or without |
| 8 | +// modification, are permitted provided that the following conditions are met: |
| 9 | +// |
| 10 | +// 1. Redistributions of source code must retain the above copyright notice, this |
| 11 | +// list of conditions and the following disclaimer. |
| 12 | +// |
| 13 | +// 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | +// this list of conditions and the following disclaimer in the documentation |
| 15 | +// and/or other materials provided with the distribution. |
| 16 | +// |
| 17 | +// 3. Neither the name of the copyright holder nor the names of its |
| 18 | +// contributors may be used to endorse or promote products derived from |
| 19 | +// this software without specific prior written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COpBRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | +// DISCLAIMED. IN NO EVENT SHsum THE COpBRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 25 | +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 27 | +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 29 | +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +///////////////////////////////////////////////////////////////////////////////// |
| 32 | + |
| 33 | +#pragma once |
| 34 | + |
| 35 | + |
| 36 | +#include "matx/core/type_utils.h" |
| 37 | +#include "matx/operators/base_operator.h" |
| 38 | +#include "matx/operators/permute.h" |
| 39 | +#include "matx/transforms/find_peaks.h" |
| 40 | + |
| 41 | +namespace matx { |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +namespace detail { |
| 46 | + template<typename OpA> |
| 47 | + class FindPeaksOp : public BaseOp<FindPeaksOp<OpA>> |
| 48 | + { |
| 49 | + private: |
| 50 | + typename detail::base_type_t<OpA> a_; |
| 51 | + typename remove_cvref_t<OpA>::value_type height_; |
| 52 | + typename remove_cvref_t<OpA>::value_type threshold_; |
| 53 | + |
| 54 | + public: |
| 55 | + using matxop = bool; |
| 56 | + using value_type = typename remove_cvref_t<OpA>::value_type; |
| 57 | + using matx_transform_op = bool; |
| 58 | + using find_peaks_xform_op = bool; |
| 59 | + |
| 60 | + __MATX_INLINE__ std::string str() const { return "find_peaks(" + get_type_str(a_) + ")"; } |
| 61 | + __MATX_INLINE__ FindPeaksOp(const OpA &a, value_type height, |
| 62 | + value_type threshold) : |
| 63 | + a_(a), height_(height), threshold_(threshold) { |
| 64 | + } |
| 65 | + |
| 66 | + template <typename... Is> |
| 67 | + __MATX_INLINE__ __MATX_DEVICE__ __MATX_HOST__ decltype(auto) operator()(Is... indices) const = delete; |
| 68 | + |
| 69 | + template <OperatorCapability Cap> |
| 70 | + __MATX_INLINE__ __MATX_HOST__ auto get_capability() const { |
| 71 | + auto self_has_cap = capability_attributes<Cap>::default_value; |
| 72 | + return combine_capabilities<Cap>(self_has_cap, detail::get_operator_capability<Cap>(a_)); |
| 73 | + } |
| 74 | + |
| 75 | + template <typename Out, typename Executor> |
| 76 | + void Exec(Out &&out, Executor &&ex) const { |
| 77 | + static_assert(cuda::std::tuple_size_v<remove_cvref_t<Out>> == 3, "Must use mtie with 2 outputs on find_peaks(). ie: (mtie(O, num_found) = find_peaks(A, height, threshold))"); |
| 78 | + static_assert(remove_cvref_t<decltype(cuda::std::get<1>(out))>::Rank() == 0 && |
| 79 | + std::is_same_v<typename remove_cvref_t<decltype(cuda::std::get<1>(out))>::value_type, int>, |
| 80 | + "Num elements output must be a scalar integer tensor"); |
| 81 | + static_assert(std::is_same_v<typename remove_cvref_t<decltype(cuda::std::get<0>(out))>::value_type, index_t>, |
| 82 | + "Peak indices output must be a 1D matx::index_t tensor"); |
| 83 | + find_peaks_impl(cuda::std::get<0>(out), cuda::std::get<1>(out), a_, height_, threshold_, ex); |
| 84 | + } |
| 85 | + |
| 86 | + static __MATX_INLINE__ constexpr __MATX_HOST__ __MATX_DEVICE__ int32_t Rank() |
| 87 | + { |
| 88 | + return remove_cvref_t<OpA>::Rank(); |
| 89 | + } |
| 90 | + |
| 91 | + template <typename ShapeType, typename Executor> |
| 92 | + __MATX_INLINE__ void InnerPreRun([[maybe_unused]] ShapeType &&shape, Executor &&ex) const noexcept |
| 93 | + { |
| 94 | + if constexpr (is_matx_op<OpA>()) { |
| 95 | + a_.PreRun(std::forward<ShapeType>(shape), std::forward<Executor>(ex)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + template <typename ShapeType, typename Executor> |
| 100 | + __MATX_INLINE__ void PreRun([[maybe_unused]] ShapeType &&shape, Executor &&ex) const noexcept |
| 101 | + { |
| 102 | + InnerPreRun(std::forward<ShapeType>(shape), std::forward<Executor>(ex)); |
| 103 | + } |
| 104 | + |
| 105 | + template <typename ShapeType, typename Executor> |
| 106 | + __MATX_INLINE__ void PostRun(ShapeType &&shape, Executor &&ex) const noexcept |
| 107 | + { |
| 108 | + if constexpr (is_matx_op<OpA>()) { |
| 109 | + a_.PostRun(std::forward<ShapeType>(shape), std::forward<Executor>(ex)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + constexpr __MATX_INLINE__ __MATX_HOST__ __MATX_DEVICE__ index_t Size(int dim) const |
| 114 | + { |
| 115 | + return a_.Size(dim); |
| 116 | + } |
| 117 | + |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +/** |
| 123 | + * Compute peak search of input |
| 124 | + * |
| 125 | + * Returns a tensor representing the indices of peaks found in the input operator. The first output parameter holds the indices |
| 126 | + * while the second holds the number of indices/peaks found. The output index tensor must be large enough to hold all of the peaks |
| 127 | + * found or the behavior is undefined. |
| 128 | + * |
| 129 | + * @tparam InType |
| 130 | + * Input data type |
| 131 | + * @tparam D |
| 132 | + * Number of right-most dimensions to reduce over |
| 133 | + * |
| 134 | + * @param in |
| 135 | + * Input data to reduce |
| 136 | + * @param height |
| 137 | + * Height threshold for peak detection. Values below this threshold are not considered peaks. |
| 138 | + * @param threshold |
| 139 | + * Threshold for peak detection. Neighboring values must be larger in vertical distance than this threshold |
| 140 | + * @returns Operator with reduced values of peak search computed |
| 141 | + */ |
| 142 | +template <typename InType> |
| 143 | +__MATX_INLINE__ auto find_peaks(const InType &in, |
| 144 | + typename InType::value_type height, |
| 145 | + typename InType::value_type threshold) |
| 146 | +{ |
| 147 | + static_assert(InType::Rank() == 1, "Input to find_peaks() must be rank 1"); |
| 148 | + return detail::FindPeaksOp<decltype(in)>(in, height, threshold); |
| 149 | +} |
| 150 | + |
| 151 | +} |
0 commit comments