aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/vespa/eval/eval/gbdt.cpp
blob: 3422228b03c8b48c7e8fe2dbe80e64301243c1d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "gbdt.h"
#include "vm_forest.h"
#include "node_traverser.h"
#include <vespa/eval/eval/basic_nodes.h>
#include <vespa/eval/eval/call_nodes.h>
#include <vespa/eval/eval/operator_nodes.h>

namespace vespalib {
namespace eval {
namespace gbdt {

//-----------------------------------------------------------------------------

std::vector<const nodes::Node *> extract_trees(const nodes::Node &node) {
    std::vector<const nodes::Node *> trees;
    std::vector<const nodes::Node *> todo;
    if (node.is_tree()) {
        trees.push_back(&node);
    } else if (node.is_forest()) {
        todo.push_back(&node);
    }
    while (!todo.empty()) {
        const nodes::Node &forest = *todo.back(); todo.pop_back();
        for (size_t i = 0; i < forest.num_children(); ++i) {
            const nodes::Node &child = forest.get_child(i);
            if (child.is_tree()) {
                trees.push_back(&child);
            } else if (child.is_forest()) {
                todo.push_back(&child);
            }
        }
    }
    return trees;
}

//-----------------------------------------------------------------------------

TreeStats::TreeStats(const nodes::Node &tree)
    : size(0),
      num_less_checks(0),
      num_in_checks(0),
      num_inverted_checks(0),
      num_tuned_checks(0),
      max_set_size(0),
      expected_path_length(0.0),
      average_path_length(0.0),
      num_params(0)
{
    size_t sum_path = 0.0;
    expected_path_length = traverse(tree, 0, sum_path);
    average_path_length = double(sum_path) / double(size);
}

double
TreeStats::traverse(const nodes::Node &node, size_t depth, size_t &sum_path) {
    if (auto if_node = nodes::as<nodes::If>(node)) {
        double p_true = if_node->p_true();
        if (p_true != 0.5) {
            ++num_tuned_checks;
        }
        double true_path = traverse(if_node->true_expr(), depth + 1, sum_path);
        double false_path = traverse(if_node->false_expr(), depth + 1, sum_path);
        auto less = nodes::as<nodes::Less>(if_node->cond());
        auto in = nodes::as<nodes::In>(if_node->cond());
        auto inverted = nodes::as<nodes::Not>(if_node->cond());
        if (less) {
            auto symbol = nodes::as<nodes::Symbol>(less->lhs());
            assert(symbol);
            num_params = std::max(num_params, size_t(symbol->id() + 1));
            ++num_less_checks;
        } else if (in) {
            auto symbol = nodes::as<nodes::Symbol>(in->child());
            assert(symbol);
            num_params = std::max(num_params, size_t(symbol->id() + 1));
            ++num_in_checks;
            max_set_size = std::max(max_set_size, in->num_entries());
        } else {
            assert(inverted);
            auto ge = nodes::as<nodes::GreaterEqual>(inverted->child());
            assert(ge);
            auto symbol = nodes::as<nodes::Symbol>(ge->lhs());
            assert(symbol);
            num_params = std::max(num_params, size_t(symbol->id() + 1));
            ++num_inverted_checks;
        }
        return 1.0 + (p_true * true_path) + ((1.0 - p_true) * false_path);
    } else {
        ++size;
        sum_path += depth;
        return 0.0;
    }
}

ForestStats::ForestStats(const std::vector<const nodes::Node *> &trees)
    : num_trees(trees.size()),
      total_size(0),
      tree_sizes(),
      total_less_checks(0),
      total_in_checks(0),
      total_inverted_checks(0),
      total_tuned_checks(0),
      max_set_size(0),
      total_expected_path_length(0.0),
      total_average_path_length(0.0),
      num_params(0)
{
    std::map<size_t,size_t> size_map;
    for (const nodes::Node *tree: trees) {
        TreeStats stats(*tree);
        num_params = std::max(num_params, stats.num_params);
        total_size += stats.size;
        ++size_map[stats.size];
        total_less_checks += stats.num_less_checks;
        total_in_checks += stats.num_in_checks;
        total_inverted_checks += stats.num_inverted_checks;
        total_tuned_checks += stats.num_tuned_checks;
        max_set_size = std::max(max_set_size, stats.max_set_size);
        total_expected_path_length += stats.expected_path_length;
        total_average_path_length += stats.average_path_length;
    }
    for (auto const &size: size_map) {
        tree_sizes.push_back(TreeSize{size.first, size.second});
    }
}

//-----------------------------------------------------------------------------

bool contains_gbdt(const nodes::Node &node, size_t limit) {
    struct FindGBDT : NodeTraverser {
        size_t seen;
        size_t limit;
        explicit FindGBDT(size_t limit_in) : seen(0), limit(limit_in) {}
        bool found() const { return (seen >= limit); }
        bool open(const nodes::Node &) override { return !found(); }
        void close(const nodes::Node &node) override {
            if (node.is_tree() || node.is_forest()) {
                ++seen;
            }
        }
    } findGBDT(limit);
    node.traverse(findGBDT);
    return findGBDT.found(); 
}

//-----------------------------------------------------------------------------

Optimize::Result
Optimize::select_best(const ForestStats &stats,
                      const std::vector<const nodes::Node *> &trees)
{
    double path_len = stats.total_average_path_length;
    if ((stats.tree_sizes.back().size > 12) && (path_len > 2500.0)) {
        return apply_chain(VMForest::optimize_chain, stats, trees);
    }
    return Optimize::Result();
}

Optimize::Chain Optimize::best({select_best});
Optimize::Chain Optimize::none;

//-----------------------------------------------------------------------------

} // namespace vespalib::eval::gbdt
} // namespace vespalib::eval
} // namespace vespalib