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

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/kernels/device/normalize_device.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/device/normalize_device.hpp Source File
normalize_device.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::Device {
37 template <bool ScaleStddev, typename SrcWrapper, typename DstWrapper, typename ScaleWrapper, typename BaseWrapper>
38 __global__ 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  const int x = blockDim.x * blockIdx.x + threadIdx.x;
45  const int y = blockDim.y * blockIdx.y + threadIdx.y;
46  const int b = blockIdx.z;
47 
48  if (x >= output.width() || y >= output.height()) return;
49 
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 by zero.
62  scaleVal = math::vrsqrtf((s * s) + epsilon);
63  } else {
64  // Scale tensor remains normal, calculate assuming the values in the scale tensor are indeed the scale
65  scaleVal = s;
66  }
67  work_type result = (StaticCast<work_type>(input.at(b, y, x, 0)) -
68  StaticCast<work_type>(base.at(baseBatchIdx, baseHeightIdx, baseWidthIdx, 0))) *
69  scaleVal * globalScale +
70  shift;
71 
72  // Saturate cast value back into the output tensor's value type
73  output.at(b, y, x, 0) = SaturateCast<result_type>(result);
74 }
75 } // namespace Kernels::Device
Definition: bilateral_filter_device.hpp:35
__global__ void normalize(SrcWrapper input, BaseWrapper base, ScaleWrapper scale, DstWrapper output, float globalScale, float shift, float epsilon)
Definition: normalize_device.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.