clang-tools 22.0.0git
|
Files | |
CompletionModelCodegen.py |
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.
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.
An input (code completion candidate) is characterized as a set of features, such as the type of symbol or the number of existing references.
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:
A leaf node contains the value score. To compute an overall quality score, we traverse each tree in this way and add up the scores.
The input model is represented in json format.
The file features.json defines the features available to the model. It is a json list of features. The features can be of following two kinds.
The field enum specifies the fully qualified name of the enum. The maximum cardinality of the enum can be 32.
The field header specifies the header containing the declaration of the enum. This header is included by the inference runtime.
The file forest.json defines the decision forest. It is a json list of DecisionTree.
DecisionTree is one of IfGreaterNode, IfMemberNode, LeafNode.
The implementation of inference runtime is split across:
The code generator CompletionModelCodegen.py takes input the ${model} dir and generates the inference library:
Invocation
CompletionModel.cmake provides gen_decision_forest method . Client intending to use the CompletionModel for inference can use this to trigger the code generator and generate the inference library. It can then use the generated API by including and depending on this library.
The code generator defines the Example class inside relevant namespaces as specified in option ${cpp_class}.
Members of this generated class comprises of all the features mentioned in features.json. Thus this class can represent a code completion candidate that needs to be scored.
The API also provides float Evaluate(const MyClass&) which can be used to score the completion candidate.
Inorder to use the inference runtime, one can use gen_decision_forest function described in CompletionModel.cmake which invokes CodeCompletionCodegen.py with the appropriate arguments.
For example, the following invocation reads the model present in path/to/model and creates ${CMAKE_CURRENT_BINARY_DIR}/myfilename.h and ${CMAKE_CURRENT_BINARY_DIR}/myfilename.cpp describing a class named MyClass in namespace fully::qualified.