/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/host/normalize_host.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/host/normalize_host.hpp Source File#

4 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/host/normalize_host.hpp Source File
normalize_host.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
27 #pragma once
28 
29 #include <hip/hip_runtime.h>
30 
31 #include "core/detail/casting.hpp"
35 
36 namespace Kernels::Host {
37 template <bool ScaleStddev, typename SrcWrapper, typename DstWrapper, typename ScaleWrapper, typename BaseWrapper>
38 void normalize(SrcWrapper input, BaseWrapper base, ScaleWrapper scale, DstWrapper output, float globalScale,
39  float shift, float epsilon) {
40  using namespace roccv::detail;
42  using result_type = DstWrapper::ValueType;
43 
44 // Split work across available threads for the batch dimension of the images. By default, OpenMP will use the maximum
45 // available threads on the system unless set otherwise.
46 #pragma omp parallel for
47  for (int b = 0; b < output.batches(); b++) {
48  for (int y = 0; y < output.height(); y++) {
49  for (int x = 0; x < output.width(); x++) {
50  const int baseBatchIdx = base.batches() == 1 ? 0 : b;
51  const int baseHeightIdx = base.height() == 1 ? 0 : y;
52  const int baseWidthIdx = base.width() == 1 ? 0 : x;
53 
54  const int scaleBatchIdx = scale.batches() == 1 ? 0 : b;
55  const int scaleHeightIdx = scale.height() == 1 ? 0 : y;
56  const int scaleWidthIdx = scale.width() == 1 ? 0 : x;
57 
58  work_type scaleVal;
59  work_type s = StaticCast<work_type>(scale.at(scaleBatchIdx, scaleHeightIdx, scaleWidthIdx, 0));
60  if constexpr (ScaleStddev) {
61  // Scale tensor is the standard deviation, invert back to scale with epsilon added to avoid division
62  // by zero.
63  scaleVal = 1.0f / (math::vsqrtf((s * s) + epsilon));
64  } else {
65  // Scale tensor remains normal, calculate assuming the values in the scale tensor are indeed the
66  // scale
67  scaleVal = s;
68  }
69  work_type result = (StaticCast<work_type>(input.at(b, y, x, 0)) -
70  StaticCast<work_type>(base.at(baseBatchIdx, baseHeightIdx, baseWidthIdx, 0))) *
71  scaleVal * globalScale +
72  shift;
73 
74  // Saturate cast value back into the output tensor's value type
75  output.at(b, y, x, 0) = SaturateCast<result_type>(result);
76  }
77  }
78  }
79 }
80 } // namespace Kernels::Host
Definition: bilateral_filter_host.hpp:34
void normalize(SrcWrapper input, BaseWrapper base, ScaleWrapper scale, DstWrapper output, float globalScale, float shift, float epsilon)
Definition: normalize_host.hpp:38
Definition: strided_data_wrap.hpp:33
MakeType_t< T, C >::type MakeType
Definition: type_traits.hpp:97
This header defines common arithmetic operators for HIP vectorized types.