1 # Decision Forest Code Completion Model
4 A **decision forest** is a collection of many decision trees. A **decision tree** is a full binary tree that provides a quality prediction for an input (code completion item). Internal nodes represent a **binary decision** based on the input data, and leaf nodes represent a prediction.
6 In order to predict the relevance of a code completion item, we traverse each of the decision trees beginning with their roots until we reach a leaf.
8 An input (code completion candidate) is characterized as a set of **features**, such as the *type of symbol* or the *number of existing references*.
10 At every non-leaf node, we evaluate the condition to decide whether to go left or right. The condition compares one *feature** of the input against a constant. The condition can be of two types:
11 - **if_greater**: Checks whether a numerical feature is **>=** a **threshold**.
12 - **if_member**: Check whether the **enum** feature is contained in the **set** defined in the node.
14 A leaf node contains the value **score**.
15 To compute an overall **quality** score, we traverse each tree in this way and add up the scores.
18 The input model is represented in json format.
21 The file **features.json** defines the features available to the model.
22 It is a json list of features. The features can be of following two kinds.
27 "name": "a_numerical_feature",
34 "name": "an_enum_feature",
36 "enum": "fully::qualified::enum",
37 "header": "path/to/HeaderDeclaringEnum.h"
40 The field `enum` specifies the fully qualified name of the enum.
41 The maximum cardinality of the enum can be **32**.
43 The field `header` specifies the header containing the declaration of the enum.
44 This header is included by the inference runtime.
48 The file `forest.json` defines the decision forest. It is a json list of **DecisionTree**.
50 **DecisionTree** is one of **IfGreaterNode**, **IfMemberNode**, **LeafNode**.
54 "operation": "if_greater",
55 "feature": "a_numerical_feature",
56 "threshold": A real number,
57 "then": {A DecisionTree},
58 "else": {A DecisionTree}
64 "operation": "if_member",
65 "feature": "an_enum_feature",
66 "set": ["enum_value1", "enum_value2", ...],
67 "then": {A DecisionTree},
68 "else": {A DecisionTree}
75 "score": A real number
79 ## Code Generator for Inference
80 The implementation of inference runtime is split across:
83 The code generator `CompletionModelCodegen.py` takes input the `${model}` dir and generates the inference library:
84 - `${output_dir}/{filename}.h`
85 - `${output_dir}/{filename}.cpp`
89 python3 CompletionModelCodegen.py \
90 --model path/to/model/dir \
91 --output_dir path/to/output/dir \
92 --filename OutputFileName \
93 --cpp_class clang::clangd::YourExampleClass
96 `CompletionModel.cmake` provides `gen_decision_forest` method .
97 Client intending to use the CompletionModel for inference can use this to trigger the code generator and generate the inference library.
98 It can then use the generated API by including and depending on this library.
100 ### Generated API for inference
101 The code generator defines the Example `class` inside relevant namespaces as specified in option `${cpp_class}`.
103 Members of this generated class comprises of all the features mentioned in `features.json`.
104 Thus this class can represent a code completion candidate that needs to be scored.
106 The API also provides `float Evaluate(const MyClass&)` which can be used to score the completion candidate.
110 ### model/features.json
122 "name": "ACategorical",
124 "enum": "ns1::ns2::TestEnum",
125 "header": "model/CategoricalFeature.h"
129 ### model/forest.json
133 "operation": "if_greater",
134 "feature": "ANumber",
137 "operation": "if_greater",
141 "operation": "boost",
145 "operation": "boost",
150 "operation": "if_member",
151 "feature": "ACategorical",
157 "operation": "boost",
161 "operation": "boost",
167 "operation": "if_member",
168 "feature": "ACategorical",
174 "operation": "boost",
178 "operation": "boost",
184 ### DecisionForestRuntime.h
192 void setANumber(float V) { ... }
193 void setAFloat(float V) { ... }
194 void setACategorical(unsigned V) { ... }
200 float Evaluate(const Example&);
207 Inorder to use the inference runtime, one can use `gen_decision_forest` function
208 described in `CompletionModel.cmake` which invokes `CodeCompletionCodegen.py` with the appropriate arguments.
210 For example, the following invocation reads the model present in `path/to/model` and creates
211 `${CMAKE_CURRENT_BINARY_DIR}/myfilename.h` and `${CMAKE_CURRENT_BINARY_DIR}/myfilename.cpp`
212 describing a `class` named `MyClass` in namespace `fully::qualified`.
217 gen_decision_forest(path/to/model
219 ::fully::qualifed::MyClass)