This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Use Decision Forest to score code completions.
ClosedPublic

Authored by usaxena95 on Sep 25 2020, 1:07 AM.

Details

Summary

By default clangd will score a code completion item using heuristics model.

Scoring can be done by Decision Forest model by passing --ranking_model=decision_forest to
clangd.

Features omitted from the model:

  • NameMatch is excluded because the final score must be multiplicative in NameMatch to allow rescoring by the editor.
  • NeedsFixIts is excluded because the generating dataset that needs 'fixits' is non-trivial.

There are multiple ways (heuristics) to combine the above two features with the prediction of the DF:

  • NeedsFixIts is used as is with a penalty of 0.5.

Various alternatives of combining NameMatch N and Decision forest Prediction P

  • N * scale(P, 0, 1): Linearly scale the output of model to range [0, 1]
  • N * a^P:
    • More natural: Prediction of each Decision Tree can be considered as a multiplicative boost (like NameMatch)
    • Ordering is independent of the absolute value of P. Order of two items is proportional to a^{difference in model prediction score}. Higher a gives higher weightage to model output as compared to NameMatch score.

Baseline MRR = 0.619
MRR for various combinations:
N * P = 0.6346, advantage%=2.5768
N * 1.1^P = 0.6600, advantage%=6.6853
N * 1.2^P = 0.6669, advantage%=7.8005
N * 1.3^P = 0.6668, advantage%=7.7795
N * 1.4^P = 0.6659, advantage%=7.6270
N * 1.5^P = 0.6646, advantage%=7.4200
N * 1.6^P = 0.6636, advantage%=7.2671
N * 1.7^P = 0.6629, advantage%=7.1450
N * 2^P = 0.6612, advantage%=6.8673
N * 2.5^P = 0.6598, advantage%=6.6491
N * 3^P = 0.6590, advantage%=6.5242
N * scaled[0, 1] = 0.6465, advantage%=4.5054

Diff Detail

Event Timeline

usaxena95 created this revision.Sep 25 2020, 1:07 AM
usaxena95 requested review of this revision.Sep 25 2020, 1:07 AM
usaxena95 updated this revision to Diff 294281.Sep 25 2020, 4:56 AM

Moved DF evaluation to Quality.cpp

usaxena95 edited the summary of this revision. (Show Details)Sep 25 2020, 6:38 AM
usaxena95 added reviewers: sammccall, adamcz.
adamcz accepted this revision.Sep 25 2020, 6:59 AM

Could you add a test that sets this flag? Perhaps we can run CodeCompletionTests.cpp twice, once with this flag, once without? Just to exercise these code paths, I think most expectations there are unordered, so it should work?

clang-tools-extra/clangd/CodeComplete.cpp
1636

Ideally we'd rename the evaluate() here, since SymbolQualitySignals is used for both heuristic and DecisionForest version, but evaluate is heuristic-specific. I think in pefect world this would be out of SymbolQualitySignals class (which would become just storage), but at least it should be renamed to evaluateUsingHeuristic().

1656

Could we make the weight of Relevance.NameMatch configurable, maybe through CodeCompletionOptions or such? I'm worried it may dominate the score too much and being able to configure this would allow us to run experiments easily.

This revision is now accepted and ready to land.Sep 25 2020, 6:59 AM
usaxena95 updated this revision to Diff 294684.Sep 28 2020, 6:14 AM
usaxena95 marked 2 inline comments as done.

Addressed comments.

If we just consider ranking, there are not many code paths. Most of the code paths are for gathering and creating code completion items.
In the end all the code paths end up in the addCandidate method to get scored either by heuristics or by decision forest.
So I have added 2 tests for checking sanity and exercising the code path.

clang-tools-extra/clangd/CodeComplete.cpp
1636

Right. I plan to do this but the changes for a rename would include unrelated files. So I planed to do this another patch keeping this one tractable and clean.
Let me know if you want me to do it in this one itself.

1656

Since this is multiplicative, I don't think it is possible to weigh NameMatch.
Although we can weigh the contribution of DF prediction.
Did some maths and realised that the base in pow(base, Evaluate(E)) can effectively represent the weightage given to DF Model.

adamcz accepted this revision.Sep 28 2020, 6:45 AM
adamcz added inline comments.
clang-tools-extra/clangd/CodeComplete.cpp
1636

Separate change sounds good.

clang-tools-extra/clangd/tool/ClangdMain.cpp
185

typo: Decsion

usaxena95 updated this revision to Diff 294711.Sep 28 2020, 8:03 AM
usaxena95 marked an inline comment as done.

Added fixme for renaming evaluate() and fixed typo.

usaxena95 edited the summary of this revision. (Show Details)Sep 28 2020, 9:17 AM
usaxena95 edited the summary of this revision. (Show Details)

Just noticed: typo in commit description - compeltions

usaxena95 retitled this revision from [clangd] Use Decision Forest to score code compeltions. to [clangd] Use Decision Forest to score code completions..Sep 28 2020, 9:34 AM
usaxena95 edited the summary of this revision. (Show Details)
This revision was landed with ongoing or failed builds.Sep 28 2020, 9:59 AM
This revision was automatically updated to reflect the committed changes.