Source-linked AI summary
The Magic of IF: Investigating Causal Reasoning Abilities in Large Language Models of Code
Xiao Liu, Da Yin, Chen Zhang, Yansong Feng, Dongyan Zhao
TL;DR
Complex causal reasoning remains challenging for LLMs, and this paper tests whether code prompts can help by representing causal relations with conditional structures. Code-LLMs with these prompts perform significantly better than text-only models, while prompt analyses find conditional structure crucial and robustness to format perturbations.
Problem
Although LLMs handle many language tasks, complex causal reasoning such as abductive and counterfactual reasoning remains challenging.
Source: §1 · p. 1Method
Results
Takeaways & Limitations
The study finds that reasonable causal structure in code helps generate plausible outputs, while Code-LLMs are robust to format perturbations.
Source: §1 · p. 2, §5 · p. 5CODEX is no longer publicly available, and the tested open-source CodeGen and StarCoder alternatives did not match its performance in the case study.
Source: Limitations · p. 6
Abstract
from arXiv · showhide
Causal reasoning, the ability to identify cause-and-effect relationship, is crucial in human thinking. Although large language models (LLMs) succeed in many NLP tasks, it is still challenging for them to conduct complex causal reasoning like abductive reasoning and counterfactual reasoning. Given the fact that programming code may express causal relations more often and explicitly with conditional statements like ``if``, we want to explore whether Code-LLMs acquire better causal reasoning abilities. Our experiments show that compared to text-only LLMs, Code-LLMs with code prompts are significantly better in causal reasoning. We further intervene on the prompts from different aspects, and discover that the programming structure is crucial in code prompt design, while Code-LLMs are robust towards format perturbations.
1 Introduction
The paper finds that Code-LLMs with code prompts perform better than text-only LLMs on causal reasoning, and that conditional code structures make causal branches explicit. It addresses the difficulty of handling complex causal reasoning when event relations and alternatives are rarely explicit in text.
1 Introduction
- Complex causal reasoning involving multiple events and alternative branches remains challenging for LLMs, despite their ability to handle single cause-and-effect relations.The paper focuses on abductive reasoning and counterfactual reasoning.Source: §1 · p. 1
- Text often presents events chronologically, making causal relations hard to extract, while branches are rarely expressed outside specialized storytelling.Conditional statements, including if, elif, and switch, explicitly represent causal relations and branching in code.Source: §1 · p. 2
- Code-LLMs with code prompts perform much better than text-only LLMs and previous methods, while conditional structure is crucial and format perturbations are tolerated.The analysis identifies conditional statements as the key programming structure.Source: §1 · p. 2
- The authors design code prompts that use conditional statements to represent causal structures and evaluate them on abductive and counterfactual reasoning.They investigate whether Code-LLMs' code pretraining and structural prediction abilities support natural-language causal reasoning.Source: §1 · p. 2
2 Modeling Causal Structure with Code
The paper represents causal reasoning tasks as code prompts, using conditional structures for event relations and placing the target output at the end. It uses different prompt structures for abductive and counterfactual reasoning, while noting that the code representation only approximates the underlying causal relations.
2 Modeling Causal Structure with Code
- The prompts encode causal structure with conditionals and place the target output last, using functions for events because models generate only at the prompt end.The main function expresses causal structure, and event functions follow it.Source: §2 · pp. 2–3
- For abductive reasoning, the prompt executes the premise and then the ending if the hypothesis condition is met, leaving the hypothesis function for generation.The hypothesis is placed last in the prompt.Source: §2 · p. 3
- For counterfactual reasoning, an if-elif structure executes the premise, then selects the original ending or the counterfactual ending according to the context.The target is the counterfactual ending, which should minimally modify the original and cohere with the counterfactual context.Source: §2 · p. 3
- The conditional-code representation approximates, but does not exactly capture, the underlying causal relationships.The paper explicitly qualifies the representation as not entirely accurate.Source: §2 · p. 3
3 Evaluation
The evaluation finds that CODEX significantly outperforms prior unsupervised methods and text-only DAVINCI models on abductive and counterfactual reasoning. Prompt exchanges show that code prompts help CODEX, but their effects differ across the two tasks for DAVINCI002.
Experimental Setup
- The evaluation uses 3,561 ART test instances for abduction and 1,871 TimeTravel test instances for counterfactual reasoning, comparing CODEX with DAVINCI002 and DAVINCI003.CODEX is code-davinci-002; the DAVINCI models are instruction-tuned versions originating from CODEX.Source: §3 · p. 3
Evaluation Metrics
- Automatic evaluation uses BLEU4, ROUGEL, CIDEr, and BERTScore for abduction, and BLEU4, ROUGEL, and BERTScore for counterfactual reasoning.Human evaluation compares CODEX and DAVINCI002 on 100 test examples using task-specific coherence criteria and original-ending preservation for counterfactuals.Source: §3 · p. 4
Automatic Evaluation
- CODEX significantly outperforms previous methods and DAVINCI on both abductive and counterfactual reasoning tasks.The authors also report that DAVINCI003 underperforms DAVINCI002 on most metrics, probably because its longer, more discursive outputs do not fit the tasks.Source: §3 · p. 4
- The results are shown in Table 2: CODEX outperforms DAVINCI002 in all aspects, better considers context during generation, and preserves original content in counterfactual reasoning.Source: §3 · p. 4
Contributions of the Model and the Prompt
- With exchanged prompts, code prompts benefit DAVINCI002 on abduction but hurt its counterfactual reasoning, while CODEX performs better with its code prompt.The authors suggest the more complicated counterfactual causal structure may make its code prompt harder for text models to understand.Source: §3 · p. 4, Table 3 · p. 4
4 What are Crucial in Code Prompts?
The prompt interventions vary prior information, conditional structure, code format, and programming language. CODEX is more affected by changes to the causal structure than by format or language changes.
Intervention Design
- The interventions vary prior information, programming structure, code format, and language to test sensitivity to prompt content and form.Information changes remove task instructions or replace informative function names; structure changes convert conditionals to sequences or disrupt function positions.Source: §4 · pp. 4–5
- Class moves the programming structure into an __init__ method, while Print and Return place event text in print statements or function return values.The original Python prompts are also converted into Java and C.Source: §4 · p. 5
Intervention Results
- Changes to programming structure have a larger negative impact than removing prior information; disrupting conditional function positions causes a major drop versus function-name perturbation.The authors attribute the drop to disruption of the conditional structure that represents event relations.Source: §4 · p. 5, Table 4 · p. 5
- CODEX is robust to format and language changes, with Class and Java settings performing even better than the original prompt.The tested formats include Class, Print, and Return; the language variants are Java and C.Source: §4 · p. 5
5 Conclusion
The paper finds that Code-LLMs with conditional code prompts perform well on abductive and counterfactual reasoning, significantly outperforming text-only LLMs. Its prompt interventions indicate that representing causal structure is crucial, while format perturbations are tolerated.
Conclusion
- With conditional-statement code prompts, Code-LLMs achieve strong abductive and counterfactual reasoning performance and significantly outperform text-only LLMs.The conclusion characterizes the prompts as representing causal structure.Source: §5 · p. 5
- The study finds that a reasonable causal structure in code can help Code-LLMs generate plausible outputs.The conclusion identifies conditional statements as the code-prompt structure used in the investigation.Source: §5 · p. 5
- Code-LLMs are robust to format perturbations in the prompt interventions.The conclusion presents this as a finding about format, alongside the importance of causal structure.Source: §5 · p. 5
Limitations
The experiments cover English programming languages, and prompts were manually designed without prompt search. Comparisons with newer models used only 100 instances, while the tested open-source Code-LLMs were much smaller than CODEX.
Language
- English-only experiments leave international and non-English programming languages outside the evaluated scope.The authors cite Scratch and Qalb as examples of programming languages they hope Code-LLMs will support.Source: Limitations · p. 5
Prompt Engineering
- Manual prompt design leaves open whether prompt-search methods could produce better prompts.The authors note that CODEX was fairly robust to format perturbations in their interventions.Source: Limitations · p. 5
Model
- Newer-model comparisons used text prompts on a subset of 100 instances, and CODEX's automatic-evaluation advantage partly reflects newer models' more detailed outputs.The reported comparison includes CHATGPT, GPT-4, and BARD; the authors note that their detailed outputs differ from the references.Source: Limitations · pp. 5–6, Table 5 · p. 6
- CODEGEN and STARCODER's case-study performance was not comparable to CODEX's, with their smaller model sizes offered as a possible explanation.The alternatives were more than ten times smaller than CODEX; CODEX generation results are provided in the authors' GitHub repository.Source: Limitations · p. 6
Ethics Statement
The authors report that their off-the-shelf LLM results may inherit model biases and should not be used without human supervision. They also describe experiment costs and annotator recruitment and compensation.
Ethics Statement
- Off-the-shelf LLM results may inherit underlying biases and should not be used individually without human supervision.The experiments used less than $100, and the authors report API pricing at the time of the study.Source: Ethics Statement · p. 6
- Annotators for human evaluation were recruited from the authors' friends and colleagues and paid more than $10 per hour.Source: Ethics Statement · p. 6
- The Codex API was free during the experiments, while the Davinci APIs cost $0.02 per thousand tokens.Source: Ethics Statement · p. 6
A.1 Related Work
Prior causal-reasoning work uses statistical methods, semantic cues, causal inference, fine-tuning, or knowledge graphs. Related work on Code-LLMs covers numerical reasoning and structural prediction, while this paper examines causal reasoning acquired through code pre-training.
Causal Reasoning
- Causal-reasoning studies have used statistical methods, explicit semantic cues, causal inference, causal-event fine-tuning, and causal knowledge graphs.Examples include Chang and Choi (2005), Gordon et al. (2011), Rink et al. (2010), Li and Mao (2019), Liu et al. (2021), Zhang et al. (2022), Li et al. (2019), Du et al. (2021), and Wang et al. (2022a).Source: §A.1 · p. 9
Applying Code-LLMs to Natural Language Tasks
- Code-LLM applications to natural-language tasks have focused on numerical reasoning and structural prediction, with text-to-SQL also studied.Numerical reasoning work generates programs for an external interpreter, while structural prediction work generates code-form structures for conversion to task outputs.Source: §A.1 · p. 9
- Unlike prior work that augments models or uses code for other task types, this paper asks Code-LLMs to generate natural-language events from causal structures.The authors frame their focus as causal reasoning abilities acquired during Code-LLM pre-training.Source: §A.1 · p. 9
A.2 Prompts
The appendix describes figures for probing DAVINCI prompts and abductive-reasoning prompt interventions. It also notes that prompt language conversion was automated with CODEX.
A.1 Prompts
- Figure A.1 demonstrates the prompts used to probe DAVINCI.Source: §A.2 · p. 9
- CODEX automatically converted the language with the instruction “# python to java/c.”Source: §A.2 · p. 9
- Figure A.2 shows interventions on code prompts for abductive reasoning.Source: §A.2 · p. 9
A.3 Models for Comparison
The appendix describes comparison baselines, additional counterfactual evaluations and prompt interventions, and qualitative examples of model generations.
A.3 Models for Comparison
- The comparison includes constraint-based DELOREAN and COLD, editing-based CGMH and EDUCAT, and DIFFUSION, a controllable language model based on continuous diffusions.CGMH and EDUCAT target counterfactual reasoning; DIFFUSION performs control tasks including abductive reasoning.Source: §A.3 · p. 9
A.4 Additional Results
- CODEX outperforms previous methods in the counterfactual first-sentence setting.Only the first sentence of the original ending is used, and models generate a one-sentence counterfactual ending.Source: §A.4 · p. 9, Table A.1 · p. 9
- Counterfactual prompt interventions show that programming-structure changes affect CODEX more than information changes, while format and language changes have less effect.The passage reports robustness to format and language changes.Source: §A.4 · p. 9, Table A.2 · p. 10
- In the one-shot setting, both DAVINCI002 and CODEX improve over zero-shot, while CODEX continues to outperform DAVINCI002.The models receive the same single demonstration example.Source: §A.4 · pp. 9–10, Table A.3 · p. 11
A.5 Case Study
- Qualitative examples show CODEX generations are more context-coherent than DAVINCI’s, while GPT-4 and CHATGPT add detail and open-source Code-LLMs often produce inconsistent outputs.CODEX better follows the task instruction and preserves the original ending in counterfactual reasoning.Source: §A.5 · p. 10, Table A.4 · p. 11