Naturalness of Attention: Revisiting Attention in Code Language Models

Mootez Saad,Tushar Sharma
2023-11-23
Abstract:Language models for code such as CodeBERT offer the capability to learn advanced source code representation, but their opacity poses barriers to understanding of captured properties. Recent attention analysis studies provide initial interpretability insights by focusing solely on attention weights rather than considering the wider context modeling of Transformers. This study aims to shed some light on the previously ignored factors of the attention mechanism beyond the attention weights. We conduct an initial empirical study analyzing both attention distributions and transformed representations in CodeBERT. Across two programming languages, Java and Python, we find that the scaled transformation norms of the input better capture syntactic structure compared to attention weights alone. Our analysis reveals characterization of how CodeBERT embeds syntactic code properties. The findings demonstrate the importance of incorporating factors beyond just attention weights for rigorously understanding neural code models. This lays the groundwork for developing more interpretable models and effective uses of attention mechanisms in program analysis.
Software Engineering,Machine Learning
What problem does this paper attempt to address?
The problem that this paper attempts to solve is: Current research on the attention mechanism in code language models (such as CodeBERT) mainly focuses on attention weights, while ignoring the influence of input transformation. This research method may not be able to fully understand how the model captures the properties of code. Therefore, this paper aims to re - examine the attention mechanism in code language models. By analyzing the attention weights and the norms of input transformation (i.e., \(\parallel\alpha f(x)\parallel\)), we can more comprehensively understand how these models embed the syntactic structure of source code. Specifically, the main objectives of the paper are: 1. **Inter - layer trend analysis**: Study the overall trend differences between the attention weights \(\alpha\) and the norms of input transformation \(\parallel\alpha f(x)\parallel\) among the layers of CodeBERT. 2. **Syntactic alignment analysis**: Compare the performance of \(\parallel\alpha f(x)\parallel\) and attention weights in terms of the alignment degree of the syntactic structure of source code. Through these analyses, the author hopes to reveal deeper characteristics of the attention mechanism in code representation and provide a basis for developing more interpretable models.