Treelite
qnode_template.h
Go to the documentation of this file.
1 
8 #ifndef TREELITE_COMPILER_NATIVE_QNODE_TEMPLATE_H_
9 #define TREELITE_COMPILER_NATIVE_QNODE_TEMPLATE_H_
10 
11 namespace treelite {
12 namespace compiler {
13 namespace native {
14 
15 const char* const qnode_template =
16 R"TREELITETEMPLATE(
17 #include <stdlib.h>
18 
19 /*
20  * \brief function to convert a feature value into bin index.
21  * \param val feature value, in floating-point
22  * \param fid feature identifier
23  * \return bin index corresponding to given feature value
24  */
25 static inline int quantize({threshold_type} val, unsigned fid) {{
26  const size_t offset = th_begin[fid];
27  const {threshold_type}* array = &threshold[offset];
28  int len = th_len[fid];
29  int low = 0;
30  int high = len;
31  int mid;
32  {threshold_type} mval;
33  // It is possible th_begin[i] == [total_num_threshold]. This means that
34  // all features i, (i+1), ... are not used for any of the splits in the model.
35  // So in this case, just return something
36  if (offset == {total_num_threshold} || val < array[0]) {{
37  return -10;
38  }}
39  while (low + 1 < high) {{
40  mid = (low + high) / 2;
41  mval = array[mid];
42  if (val == mval) {{
43  return mid * 2;
44  }} else if (val < mval) {{
45  high = mid;
46  }} else {{
47  low = mid;
48  }}
49  }}
50  if (array[low] == val) {{
51  return low * 2;
52  }} else if (high == len) {{
53  return len * 2;
54  }} else {{
55  return low * 2 + 1;
56  }}
57 }}
58 )TREELITETEMPLATE";
59 
60 const char* const quantize_loop_template =
61 R"TREELITETEMPLATE(
62 for (int i = 0; i < {num_feature}; ++i) {{
63  if (data[i].missing != -1 && !is_categorical[i]) {{
64  data[i].qvalue = quantize(data[i].fvalue, i);
65  }}
66 }}
67 )TREELITETEMPLATE";
68 
69 } // namespace native
70 } // namespace compiler
71 } // namespace treelite
72 #endif // TREELITE_COMPILER_NATIVE_QNODE_TEMPLATE_H_