Treelite
quantize.cc
Go to the documentation of this file.
1 
6 #include <treelite/math.h>
7 #include <dmlc/registry.h>
8 #include <cmath>
9 #include "./builder.h"
10 
11 namespace treelite {
12 namespace compiler {
13 
14 DMLC_REGISTRY_FILE_TAG(quantize);
15 
16 template <typename ThresholdType>
17 static void
18 scan_thresholds(ASTNode* node, std::vector<std::set<ThresholdType>>* cut_pts) {
19  NumericalConditionNode<ThresholdType>* num_cond;
20  if ( (num_cond = dynamic_cast<NumericalConditionNode<ThresholdType>*>(node)) ) {
21  CHECK(!num_cond->quantized) << "should not be already quantized";
22  const ThresholdType threshold = num_cond->threshold.float_val;
23  if (std::isfinite(threshold)) {
24  (*cut_pts)[num_cond->split_index].insert(threshold);
25  }
26  }
27  for (ASTNode* child : node->children) {
28  scan_thresholds(child, cut_pts);
29  }
30 }
31 
32 template <typename ThresholdType>
33 static void
34 rewrite_thresholds(ASTNode* node, const std::vector<std::vector<ThresholdType>>& cut_pts) {
35  NumericalConditionNode<ThresholdType>* num_cond;
36  if ( (num_cond = dynamic_cast<NumericalConditionNode<ThresholdType>*>(node)) ) {
37  CHECK(!num_cond->quantized) << "should not be already quantized";
38  const ThresholdType threshold = num_cond->threshold.float_val;
39  if (std::isfinite(threshold)) {
40  const auto& v = cut_pts[num_cond->split_index];
41  {
42  auto loc = math::binary_search(v.begin(), v.end(), threshold);
43  CHECK(loc != v.end());
44  num_cond->threshold.int_val = static_cast<int>(loc - v.begin()) * 2;
45  }
46  {
47  ThresholdType zero = static_cast<ThresholdType>(0);
48  auto loc = std::lower_bound(v.begin(), v.end(), zero);
49  num_cond->zero_quantized = static_cast<int>(loc - v.begin()) * 2;
50  if (loc != v.end() && zero != *loc) {
51  --num_cond->zero_quantized;
52  }
53  }
54  num_cond->quantized = true;
55  } // splits with infinite thresholds will not be quantized
56  }
57  for (ASTNode* child : node->children) {
58  rewrite_thresholds(child, cut_pts);
59  }
60 }
61 
62 template <typename ThresholdType, typename LeafOutputType>
63 void
64 ASTBuilder<ThresholdType, LeafOutputType>::QuantizeThresholds() {
65  this->quantize_threshold_flag = true;
66  std::vector<std::set<ThresholdType>> cut_pts;
67  std::vector<std::vector<ThresholdType>> cut_pts_vec;
68  cut_pts.resize(this->num_feature);
69  cut_pts_vec.resize(this->num_feature);
70  scan_thresholds(this->main_node, &cut_pts);
71  // convert cut_pts into std::vector
72  for (int i = 0; i < this->num_feature; ++i) {
73  std::copy(cut_pts[i].begin(), cut_pts[i].end(), std::back_inserter(cut_pts_vec[i]));
74  }
75 
76  /* revise all numerical splits by quantizing thresholds */
77  rewrite_thresholds(this->main_node, cut_pts_vec);
78 
79  CHECK_EQ(this->main_node->children.size(), 1);
80  ASTNode* top_ac_node = this->main_node->children[0];
81  CHECK(dynamic_cast<AccumulatorContextNode*>(top_ac_node));
82  /* dynamic_cast<> is used here to check node types. This is to ensure
83  that we don't accidentally call QuantizeThresholds() twice. */
84 
85  ASTNode* quantizer_node
86  = AddNode<QuantizerNode<ThresholdType>>(this->main_node, std::move(cut_pts_vec));
87  quantizer_node->children.push_back(top_ac_node);
88  top_ac_node->parent = quantizer_node;
89  this->main_node->children[0] = quantizer_node;
90 }
91 
92 template void ASTBuilder<float, uint32_t>::QuantizeThresholds();
93 template void ASTBuilder<float, float>::QuantizeThresholds();
94 template void ASTBuilder<double, uint32_t>::QuantizeThresholds();
95 template void ASTBuilder<double, double>::QuantizeThresholds();
96 
97 } // namespace compiler
98 } // namespace treelite
Some useful math utilities.
AST Builder class.