cpu.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
11 #include <cuml/fil/infer_kind.hpp>
12 
13 #ifdef _OPENMP
14 #include <omp.h>
15 #else
16 #ifdef omp_get_max_threads
17 #if omp_get_max_threads() != 1
18 #error "Inconsistent placeholders for omp_get_max_threads"
19 #endif
20 #else
21 #define omp_get_max_threads() 1
22 #endif
23 #endif
24 
25 #include <algorithm>
26 #include <cstddef>
27 #include <iostream>
28 #include <new>
29 #include <numeric>
30 #include <vector>
31 
32 namespace ML {
33 namespace fil {
34 namespace detail {
35 
70 template <bool has_categorical_nodes,
71  bool predict_leaf,
72  typename forest_t,
73  typename vector_output_t = std::nullptr_t,
74  typename categorical_data_t = std::nullptr_t>
75 void infer_kernel_cpu(forest_t const& forest,
77  typename forest_t::io_type* output,
78  typename forest_t::io_type const* input,
79  index_type row_count,
80  index_type col_count,
81  index_type num_outputs,
82  index_type chunk_size = hardware_constructive_interference_size,
83  index_type grove_size = hardware_constructive_interference_size,
84  vector_output_t vector_output_p = nullptr,
85  categorical_data_t categorical_data = nullptr,
87 {
88  auto constexpr has_vector_leaves = !std::is_same_v<vector_output_t, std::nullptr_t>;
89  auto constexpr has_nonlocal_categories = !std::is_same_v<categorical_data_t, std::nullptr_t>;
90 
91  using node_t = typename forest_t::node_type;
92 
93  using output_t = typename forest_t::template raw_output_type<vector_output_t>;
94 
95  auto const num_tree = forest.tree_count();
96  auto const num_grove = raft_proto::ceildiv(num_tree, grove_size);
97  auto const num_chunk = raft_proto::ceildiv(row_count, chunk_size);
98 
99  auto output_workspace = std::vector<output_t>(row_count * num_outputs * num_grove, output_t{});
100  auto const task_count = num_grove * num_chunk;
101 
102 #pragma omp parallel num_threads(std::min(index_type(omp_get_max_threads()), task_count))
103  {
104  // Infer on each grove and chunk
105 #pragma omp for
106  for (auto task_index = index_type{}; task_index < task_count; ++task_index) {
107  auto const grove_index = task_index / num_chunk;
108  auto const chunk_index = task_index % num_chunk;
109  auto const start_row = chunk_index * chunk_size;
110  auto const end_row = std::min(start_row + chunk_size, row_count);
111  auto const start_tree = grove_index * grove_size;
112  auto const end_tree = std::min(start_tree + grove_size, num_tree);
113 
114  for (auto row_index = start_row; row_index < end_row; ++row_index) {
115  for (auto tree_index = start_tree; tree_index < end_tree; ++tree_index) {
116  auto tree_output =
117  std::conditional_t<predict_leaf,
118  index_type,
119  std::conditional_t<has_vector_leaves,
120  typename node_t::index_type,
121  typename node_t::threshold_type>>{};
122  tree_output = evaluate_tree<has_vector_leaves,
123  has_categorical_nodes,
124  has_nonlocal_categories,
125  predict_leaf>(
126  forest, tree_index, input + row_index * col_count, categorical_data);
127  if constexpr (predict_leaf) {
128  output_workspace[row_index * num_outputs * num_grove + tree_index * num_grove +
129  grove_index] = static_cast<typename forest_t::io_type>(tree_output);
130  } else {
131  auto const default_num_outputs = forest.num_outputs();
132  if constexpr (has_vector_leaves) {
133  auto output_offset = (row_index * num_outputs * num_grove +
134  tree_index * default_num_outputs * num_grove *
135  (infer_type == infer_kind::per_tree) +
136  grove_index);
137  for (auto output_index = index_type{}; output_index < default_num_outputs;
138  ++output_index) {
139  output_workspace[output_offset + output_index * num_grove] +=
140  vector_output_p[tree_output * default_num_outputs + output_index];
141  }
142  } else {
143  auto output_offset =
144  (row_index * num_outputs * num_grove +
145  (tree_index % default_num_outputs) * num_grove *
146  (infer_type == infer_kind::default_kind) +
147  tree_index * num_grove * (infer_type == infer_kind::per_tree) + grove_index);
148  output_workspace[output_offset] += tree_output;
149  }
150  }
151  } // Trees
152  } // Rows
153  } // Tasks
154 
155  // Sum over grove and postprocess
156 #pragma omp for
157  for (auto row_index = index_type{}; row_index < row_count; ++row_index) {
158  for (auto output_index = index_type{}; output_index < num_outputs; ++output_index) {
159  auto grove_offset = (row_index * num_outputs * num_grove + output_index * num_grove);
160 
161  output_workspace[grove_offset] =
162  std::accumulate(std::begin(output_workspace) + grove_offset,
163  std::begin(output_workspace) + grove_offset + num_grove,
164  output_t{});
165  }
166  postproc(infer_type,
167  output_workspace.data() + row_index * num_outputs * num_grove,
168  num_outputs,
169  forest.bias(),
170  output + row_index * num_outputs,
171  num_grove);
172  }
173  } // End omp parallel
174 }
175 
176 } // namespace detail
177 } // namespace fil
178 } // namespace ML
void infer_kernel_cpu(forest_t const &forest, postprocessor< typename forest_t::io_type > const &postproc, typename forest_t::io_type *output, typename forest_t::io_type const *input, index_type row_count, index_type col_count, index_type num_outputs, index_type chunk_size=hardware_constructive_interference_size, index_type grove_size=hardware_constructive_interference_size, vector_output_t vector_output_p=nullptr, categorical_data_t categorical_data=nullptr, infer_kind infer_type=infer_kind::default_kind)
Definition: cpu.hpp:75
HOST DEVICE auto evaluate_tree(forest_t const &forest, index_type tree_index, io_t const *__restrict__ row, categorical_data_t categorical_data)
Definition: evaluate_tree.hpp:162
infer_kind
Definition: infer_kind.hpp:8
uint32_t index_type
Definition: index_type.hpp:9
Definition: dbscan.hpp:18
HOST DEVICE constexpr auto ceildiv(T dividend, U divisor)
Definition: ceildiv.hpp:10
Definition: forest.hpp:24
HOST DEVICE auto num_outputs() const
Definition: forest.hpp:65
HOST DEVICE auto tree_count() const
Definition: forest.hpp:61
HOST DEVICE const auto * bias() const
Definition: forest.hpp:58
Definition: postprocessor.hpp:135