postprocessor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
21 
22 #include <stddef.h>
23 
24 #include <limits>
25 #include <type_traits>
26 
27 #ifndef __CUDACC__
28 #include <math.h>
29 #endif
30 
31 namespace ML {
32 namespace experimental {
33 namespace fil {
34 
35 /* Convert the postprocessing operations into a single value
36  * representing what must be done in the inference kernel
37  */
38 HOST DEVICE inline auto constexpr ops_to_val(row_op row_wise, element_op elem_wise)
39 {
40  return (static_cast<std::underlying_type_t<row_op>>(row_wise) |
41  static_cast<std::underlying_type_t<element_op>>(elem_wise));
42 }
43 
44 /*
45  * Perform postprocessing on raw forest output
46  *
47  * @param val Pointer to the raw forest output
48  * @param output_count The number of output values per row
49  * @param out Pointer to the output buffer
50  * @param stride Number of elements between the first element that must be
51  * summed for a particular output element and the next. This is typically
52  * equal to the number of "groves" of trees over which the computation
53  * was divided.
54  * @param average_factor The factor by which to divide during the
55  * normalization step of postprocessing
56  * @param bias The bias factor to subtract off during the
57  * normalization step of postprocessing
58  * @param constant If the postprocessing operation requires a constant,
59  * it can be passed here.
60  */
61 template <row_op row_wise_v, element_op elem_wise_v, typename io_t>
62 HOST DEVICE void postprocess(io_t* val,
63  index_type output_count,
64  io_t* out,
65  index_type stride = index_type{1},
66  io_t average_factor = io_t{1},
67  io_t bias = io_t{0},
68  io_t constant = io_t{1})
69 {
70 #pragma GCC diagnostic push
71 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
72  auto max_index = index_type{};
73  auto max_value = std::numeric_limits<io_t>::lowest();
74 #pragma GCC diagnostic pop
75  for (auto output_index = index_type{}; output_index < output_count; ++output_index) {
76  auto workspace_index = output_index * stride;
77  val[workspace_index] = val[workspace_index] / average_factor + bias;
78  if constexpr (elem_wise_v == element_op::signed_square) {
79  val[workspace_index] =
80  copysign(val[workspace_index] * val[workspace_index], val[workspace_index]);
81  } else if constexpr (elem_wise_v == element_op::hinge) {
82  val[workspace_index] = io_t(val[workspace_index] > io_t{});
83  } else if constexpr (elem_wise_v == element_op::sigmoid) {
84  val[workspace_index] = io_t{1} / (io_t{1} + exp(-constant * val[workspace_index]));
85  } else if constexpr (elem_wise_v == element_op::exponential) {
86  val[workspace_index] = exp(val[workspace_index] / constant);
87  } else if constexpr (elem_wise_v == element_op::logarithm_one_plus_exp) {
88  val[workspace_index] = log1p(exp(val[workspace_index] / constant));
89  }
90  if constexpr (row_wise_v == row_op::softmax || row_wise_v == row_op::max_index) {
91  auto is_new_max = val[workspace_index] > max_value;
92  max_index = is_new_max * output_index + (!is_new_max) * max_index;
93  max_value = is_new_max * val[workspace_index] + (!is_new_max) * max_value;
94  }
95  }
96 
97  if constexpr (row_wise_v == row_op::max_index) {
98  *out = max_index;
99  } else {
100 #pragma GCC diagnostic push
101 #pragma GCC diagnostic ignored "-Wunused-but-set-variable"
102  auto softmax_normalization = io_t{};
103 #pragma GCC diagnostic pop
104  if constexpr (row_wise_v == row_op::softmax) {
105  for (auto workspace_index = index_type{}; workspace_index < output_count * stride;
106  workspace_index += stride) {
107  val[workspace_index] = exp(val[workspace_index] - max_value);
108  softmax_normalization += val[workspace_index];
109  }
110  }
111 
112  for (auto output_index = index_type{}; output_index < output_count; ++output_index) {
113  auto workspace_index = output_index * stride;
114  if constexpr (row_wise_v == row_op::softmax) {
115  out[output_index] = val[workspace_index] / softmax_normalization;
116  } else {
117  out[output_index] = val[workspace_index];
118  }
119  }
120  }
121 }
122 
123 /*
124  * Struct which holds all data necessary to perform postprocessing on raw
125  * output of a forest model
126  *
127  * @tparam io_t The type used for input and output to/from the model
128  * (typically float/double)
129  * @param row_wise Enum value representing the row-wise post-processing
130  * operation to perform on the output
131  * @param elem_wise Enum value representing the element-wise post-processing
132  * operation to perform on the output
133  * @param average_factor The factor by which to divide during the
134  * normalization step of postprocessing
135  * @param bias The bias factor to subtract off during the
136  * normalization step of postprocessing
137  * @param constant If the postprocessing operation requires a constant,
138  * it can be passed here.
139  */
140 template <typename io_t>
143  element_op elem_wise = element_op::disable,
144  io_t average_factor = io_t{1},
145  io_t bias = io_t{0},
146  io_t constant = io_t{1})
147  : average_factor_{average_factor},
148  bias_{bias},
149  constant_{constant},
150  row_wise_{row_wise},
151  elem_wise_{elem_wise}
152  {
153  }
154 
155  HOST DEVICE void operator()(io_t* val,
156  index_type output_count,
157  io_t* out,
158  index_type stride = index_type{1}) const
159  {
160  switch (ops_to_val(row_wise_, elem_wise_)) {
163  val, output_count, out, stride, average_factor_, bias_, constant_);
164  break;
167  val, output_count, out, stride, average_factor_, bias_, constant_);
168  break;
171  val, output_count, out, stride, average_factor_, bias_, constant_);
172  break;
175  val, output_count, out, stride, average_factor_, bias_, constant_);
176  break;
179  val, output_count, out, stride, average_factor_, bias_, constant_);
180  break;
182  postprocess<row_op::softmax, element_op::disable>(
183  val, output_count, out, stride, average_factor_, bias_, constant_);
184  break;
187  val, output_count, out, stride, average_factor_, bias_, constant_);
188  break;
190  postprocess<row_op::softmax, element_op::hinge>(
191  val, output_count, out, stride, average_factor_, bias_, constant_);
192  break;
194  postprocess<row_op::softmax, element_op::sigmoid>(
195  val, output_count, out, stride, average_factor_, bias_, constant_);
196  break;
199  val, output_count, out, stride, average_factor_, bias_, constant_);
200  break;
203  val, output_count, out, stride, average_factor_, bias_, constant_);
204  break;
207  val, output_count, out, stride, average_factor_, bias_, constant_);
208  break;
211  val, output_count, out, stride, average_factor_, bias_, constant_);
212  break;
215  val, output_count, out, stride, average_factor_, bias_, constant_);
216  break;
219  val, output_count, out, stride, average_factor_, bias_, constant_);
220  break;
223  val, output_count, out, stride, average_factor_, bias_, constant_);
224  break;
227  val, output_count, out, stride, average_factor_, bias_, constant_);
228  break;
229  default:
230  postprocess<row_op::disable, element_op::disable>(
231  val, output_count, out, stride, average_factor_, bias_, constant_);
232  }
233  }
234 
235  private:
236  io_t average_factor_;
237  io_t bias_;
238  io_t constant_;
239  row_op row_wise_;
240  element_op elem_wise_;
241 };
242 } // namespace fil
243 } // namespace experimental
244 } // namespace ML
#define DEVICE
Definition: gpu_support.hpp:35
#define HOST
Definition: gpu_support.hpp:34
element_op
Definition: postproc_ops.hpp:29
uint32_t index_type
Definition: index_type.hpp:21
HOST DEVICE constexpr auto ops_to_val(row_op row_wise, element_op elem_wise)
Definition: postprocessor.hpp:38
row_op
Definition: postproc_ops.hpp:22
HOST DEVICE void postprocess(io_t *val, index_type output_count, io_t *out, index_type stride=index_type{1}, io_t average_factor=io_t{1}, io_t bias=io_t{0}, io_t constant=io_t{1})
Definition: postprocessor.hpp:62
Definition: dbscan.hpp:30
Definition: postprocessor.hpp:141
HOST DEVICE postprocessor(row_op row_wise=row_op::disable, element_op elem_wise=element_op::disable, io_t average_factor=io_t{1}, io_t bias=io_t{0}, io_t constant=io_t{1})
Definition: postprocessor.hpp:142
HOST DEVICE void operator()(io_t *val, index_type output_count, io_t *out, index_type stride=index_type{1}) const
Definition: postprocessor.hpp:155