Skip to main content
QUICK REVIEW

[Paper Review] Do Large Language Models Pay Similar Attention Like Human Programmers When Generating Code?

Bonan Kou, S. Chen|arXiv (Cornell University)|Jun 2, 2023
Software Engineering ResearchComputer Science90 references3 citations
TL;DR

This study investigates whether large language models (LLMs) attend to the same parts of natural language code descriptions as human programmers during code generation. Using a newly curated programmer attention dataset and 12 attention computation methods across five SOTA LLMs, it finds consistent misalignment between model and human attention, with perturbation-based methods (like SHAP) showing the strongest alignment and highest programmer preference, despite no correlation between model accuracy and attention alignment.

ABSTRACT

Large Language Models (LLMs) have recently been widely used for code generation. Due to the complexity and opacity of LLMs, little is known about how these models generate code. We made the first attempt to bridge this knowledge gap by investigating whether LLMs attend to the same parts of a task description as human programmers during code generation. An analysis of six LLMs, including GPT-4, on two popular code generation benchmarks revealed a consistent misalignment between LLMs' and programmers' attention. We manually analyzed 211 incorrect code snippets and found five attention patterns that can be used to explain many code generation errors. Finally, a user study showed that model attention computed by a perturbation-based method is often favored by human programmers. Our findings highlight the need for human-aligned LLMs for better interpretability and programmer trust.

Motivation & Objective

  • To understand whether LLMs attend to the same salient parts of natural language descriptions as human programmers during code generation.
  • To evaluate the extent of attention misalignment between LLMs and real programmers across multiple models and attention computation methods.
  • To investigate whether higher code generation accuracy in LLMs correlates with better alignment to human attention patterns.
  • To identify the most effective and human-preferred attention computation method for interpreting LLM-based code generation.
  • To provide a foundation for developing more interpretable, human-aligned LLMs in software engineering by introducing a new programmer attention dataset and empirical analysis framework.

Proposed method

  • Curated a novel programmer attention dataset by having two experienced programmers manually label salient words and phrases in 164 HumanEval Python programming tasks.
  • Implemented 12 attention computation methods across three categories: six self-attention-based, four gradient-based, and two perturbation-based (including SHAP).
  • Quantitatively compared LLM attention outputs against the human-annotated programmer attention using alignment metrics on the full dataset.
  • Conducted a user study with 22 real-world programmers to evaluate preference for different attention visualization methods.
  • Evaluated five state-of-the-art LLMs (InCoder, CoderGen, PolyCoder, CodeParrot, GPT-J-6B) on the HumanEval benchmark to ensure generalizability across models.
  • Used both quantitative metrics and qualitative feedback to assess the fidelity and usability of attention explanations.

Experimental results

Research questions

  • RQ1RQ1: To what extent is model attention aligned with human attention?
  • RQ2RQ2: Do LLMs with higher code generation accuracy have better alignment with real programmers?
  • RQ3RQ3: What is the impact of different attention calculation methods on attention alignment?
  • RQ4RQ4: Which attention calculation method is most preferred by real programmers?

Key findings

  • A consistent and significant misalignment exists between LLM attention and human programmer attention across all five models, regardless of the attention computation method used.
  • There is no statistically significant correlation between a model’s code generation accuracy and its alignment with human attention, indicating that accuracy does not imply human-like attention patterns.
  • Among the 12 attention computation methods, perturbation-based methods—particularly SHAP—achieved the highest alignment with human-annotated attention, outperforming self- and gradient-based methods.
  • In the user study, perturbation-based methods were most preferred by real programmers, who found them more intuitive and reliable for understanding model behavior.
  • The results suggest that current LLMs do not mimic human cognitive processes in code generation, raising concerns about interpretability and trust in production use.
  • The study provides the first publicly available programmer attention dataset for code generation, enabling future research into human-aligned model training and interpretability.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.