Skip to content

Repository files navigation

CodeBridge

CodeBridge is a project dedicated to optimizing large language models (LLMs) for low-resource programming languages (LRPLs) like Cangjie. It utilizes CodeBridge, a three-stage transfer learning approach, to enhance code completion accuracy by leveraging knowledge from high-resource programming languages (HRPLs) such as Java and Rust. Additionally, Retrieval-Augmented Generation (RAG) is incorporated during inference to improve performance.

Project Structure

  • .env: Environment variables for API keys.
  • dataset: Contains datasets for training and evaluation.
  • LLaMA-Factory: Framework used for fine-tuning LLMs.
  • src: Core source code directory, containing:
    • metric/: Scripts for evaluating model performance.
    • rag/: Implementations of Retrieval-Augmented Generation (RAG).
    • tree_sitter_cj/: Cangjie code parsing utilities.
    • data_cleaning.py: Scripts for preprocessing datasets.
    • inference.py: Hugging Face transformers-based inference scripts.
  • inference.ipynb: Jupyter notebook for inference using vLLM.
  • train.sh: Shell script for training the model.
  • llm.py: Interface for interacting with the model.
  • requirements.txt: Dependencies for the project.

CodeBridge: Three-Stage Fine-Tuning Process

CangjieLLM adopts CodeBridge, a novel three-stage training strategy that improves code completion for low-resource programming languages (LRPLs) through transfer learning from high-resource languages (HRPLs).

Overview

Training Strategy

  1. Teaching Phase:

    • Dataset: Cangjie corpus (~8M tokens)
    • Epochs: 4
    • Learning Rate: 2e-5
    • Goal: Rapidly expose the model to Cangjie's syntax and semantics.
  2. Practice Phase:

    • Dataset: Java/Rust corpus (~24M tokens)
    • Epochs: 1
    • Learning Rate: 7e-6
    • Goal: Enhance structural and semantic understanding by leveraging high-resource programming languages.
  3. Correction Phase:

    • Dataset: Cangjie corpus (same as step 1)
    • Epochs: 4
    • Learning Rate: 5e-6
    • Goal: Fine-tune the model to rectify transfer-induced biases.

Training Strategy

Dataset Preparation

  • Data Sources:
    • Cangjie dataset from Huawei repositories (Cangjie-SIG, Cangjie-TPC, HW-PLLab).
    • Java/Rust dataset from StarCoder preprocessed corpus.
  • Data Cleaning:
    • File filtering based on size, encoding, character composition, and comment removal.
    • Deduplication using a 90% similarity threshold.
  • Data Splitting:
    • 20 projects used for evaluation (held-out test set).
    • Remaining data used for training.

Inference with RAG and Prefix Matching

For inference, Retrieval-Augmented Generation (RAG) is combined with a prefix-matching strategy to improve code completion accuracy.

Prefix-Matching Decoding Strategy

  • If the input ends with a space → Extract the preceding non-space segment as prefix.
  • If the input ends with a symbol → Use context-based matching to determine the appropriate completion.

This method ensures that the generated output aligns more accurately with user expectations.

Experimental Setup

  • Hardware: 4x A100 GPUs (80GB)
  • Batch Size: 4
  • Training Time:
    • Teaching Phase: 36 hours
    • Practice Phase: 10 hours
    • Correction Phase: 36 hours

Metrics

The metric module evaluates performance using:

  • Exact Match Rate (EM): Measures the percentage of perfect matches.
  • Edit Similarity (ES): Computes edit distance similarity.
  • Line Accuracy: Percentage of correctly generated lines within a block.

Results

1. Effectiveness of CodeBridge (RQ1)

SettingLine-Level Exact Match RateLine-Level Edit SimilarityFunction-Level Line Accuracy
Baseline (Untrained Model)35.49%0.669925.15%
Teaching Only44.07%0.739731.94%
No Transfer Learning (High LR)49.44%0.764530.51%
No Transfer Learning (Low LR)46.53%0.756830.70%
Transfer Learning First47.43%0.756331.91%
Full Three-Step Strategy52.35%0.769233.27%

2. Impact of Training Configurations (RQ2)

This section explores how different training settings, such as transfer data volume, final-stage learning rate, and number of epochs, influence both line-level and function-level performance.

Final Stage LRTransfer Data VolumeEpochsLine-Level Exact Match RateLine-Level Edit SimilarityFunction-Level Line Accuracy
Cosine LR-845.64%0.743231.78%
5e-61:3 (24M tokens)4+1+452.35%0.769233.27%
5e-61:1 (8M tokens)4+1+440.49%0.716532.00%
5e-61:5 (40M tokens)4+1+448.32%0.765932.10%
1e-51:3 (24M tokens)4+1+447.65%0.766032.70%
3e-61:3 (24M tokens)4+1+448.77%0.761632.57%
5e-61:3 (24M tokens)2+1+248.55%0.749732.16%
5e-61:3 (24M tokens)3+1+346.98%0.753632.87%

3. Generalizability of CodeBridge (RQ3)

To assess CodeBridge's generalizability, we evaluate its effectiveness across different LLM architectures and model sizes, analyzing line-level and function-level performance.

ModelTraining StepLine-Level Exact Match RateLine-Level Edit SimilarityFunction-Level Line Accuracy
CodeLlama-13B-InstructOrigin32.21%0.669527.24%
Step 156.60%0.808233.68%
Step 250.56%0.790032.10%
Step 357.94%0.813534.13%
No Transfer56.82%0.814233.90%
Qwen2.5-14B-InstructOrigin29.69%0.634220.60%
Step 146.98%0.747325.75%
Step 232.89%0.703421.89%
Step 350.56%0.771727.12%
No Transfer48.10%0.744626.64%
DeepSeek-Coder-1.3B-InstructOrigin27.46%0.551820.80%
Step 143.53%0.701726.39%
Step 239.96%0.715022.83%
Step 344.20%0.717927.28%
No Transfer43.97%0.705926.69%
DeepSeek-Coder-6.7B-InstructOrigin32.37%0.614723.58%
Step 151.79%0.768330.85%
Step 239.53%0.654624.60%
Step 354.02%0.780731.45%
No Transfer52.90%0.776430.94%

Setup

  1. Clone the repository and install dependencies:
    pip install -r requirements.txt
    cd LLaMA-Factory
    pip install -e ".[torch,metrics]"
  2. Prepare the dataset and store dataset metadata in dataset_info.json.
  3. Modify train.sh to specify the training configuration.
  4. Run training:
    bash train.sh
  5. For inference, use inference.ipynb.

Contribution

We welcome contributions! Feel free to open issues or submit pull requests.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages