Zero-Shot Code Representation Learning via Prompt Tuning

Nan Cui, Xiaodong Gu, Beijun ShenarXiv 2024 · DOI

Introduction

Modern software engineering increasingly relies on Pre-trained Language Models (PLMs) such as CodeBERT to facilitate code understanding and generation. The standard paradigm involves pre-training on large unlabeled corpora followed by fine-tuning on task-specific labeled data. However, this approach faces significant challenges in low-resource domains, such as smart contract development in Solidity, where labeled data is scarce. Furthermore, a fundamental "objective gap" exists between the pre-training phase (e.g., masked language modeling) and downstream fine-tuning (e.g., classification), which hinders the model's ability to fully leverage its pre-trained knowledge.

The standard pipeline involves pre-training on unlabeled data followed by fine-tuning with task-specific headers, creating a gap between training objectives.

Methodology: The Zecoler Framework

To address these limitations, the authors propose Zecoler (Zero-shot code representation learner). Zecoler shifts from traditional fine-tuning to Prompt Tuning, a technique that reformulates downstream tasks to match the model's original pre-training objectives.

Task Casting and Prompting

Zecoler converts classification and retrieval tasks into a Masked Language Modeling (MLM) format. For instance, in clone detection, the model is prompted to predict a token at a [MASK] position—such as "yes" or "no"—to indicate if two snippets are clones. This "task casting" allows the model to utilize its latent knowledge more effectively.

Zecoler's prompt tuning architecture utilizes soft prompts and a verbalizer to map MLM predictions to task labels.

The framework incorporates several key components:

  • Trainable Soft Prompts: Continuous vectors (pseudo-tokens) are inserted into the input sequence. These prompts are trained on high-resource source languages (e.g., Java) to guide the PLM's logic for specific tasks.
  • Bi-LSTM Encoder: A bidirectional LSTM encodes these prompt tokens to ensure stable training and avoid local minima.
  • Verbalizer: A mapping function translates predicted vocabulary tokens (e.g., "yes") into task-specific labels (e.g., "clone"), bridging the gap between the MLM output and the desired classification.

Experimental Setup

The effectiveness of Zecoler was evaluated across five tasks using diverse datasets:

  • SCCD: A new dataset of 10,000 labeled Solidity code pairs for clone detection.
  • SCS: A large-scale Solidity code-comment dataset for summarization.
  • CodeNet & CodeSearchNet: Standard benchmarks for multilingual clone detection and natural language code search.

Key Results and Insights

Zecoler demonstrates substantial improvements in zero-shot and cross-lingual scenarios. In Solidity code search, it achieved a 30% accuracy boost over standard fine-tuned CodeBERT. When transferring knowledge from Java to Solidity, Zecoler outperformed baselines by approximately 14.7% in classification tasks.

Ablation studies revealed that uniform prompt placement—distributing prompt tokens throughout the input—is superior to prefix or suffix prompting. This suggests that prompts must interact with the entire code structure to provide effective guidance. Interestingly, the study also noted a "Model Size Paradox," where larger models sometimes underperformed base models in classification, likely due to sensitivity to structural differences in input formats.

Conclusion

Zecoler provides a robust framework for zero-shot code representation learning by aligning downstream tasks with pre-training objectives through prompt tuning. While the approach requires more computational time to converge than standard fine-tuning, its ability to generalize to low-resource languages like Solidity without labeled data marks a significant advancement in the field. Future work will explore incorporating structural information, such as Abstract Syntax Trees (ASTs), into the prompting mechanism.

Summary updated

BibTeX
@misc{cui2024zero,
  author = {Nan Cui and Xiaodong Gu and Beijun Shen},
  title = {{Zero-Shot Code Representation Learning via Prompt Tuning}},
  year = {2024},
  eprint = {2404.08947},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2404.08947},
}
Figure