This repository evaluates the robustness of Large Language Models (LLMs) under various adversarial attacks, focusing on code generation tasks. We test both original and compressed LLMs across different datasets to provide comprehensive insights into model vulnerabilities.
Our framework provides:
We currently support the following models:
โจ๏ธ Character Attack (char_attack)
๐ Synonym Attack (synonym_attack)
๐ Translation Attack (translate_attack)
๐ Noise Attack (NEW!)
We will support both original LLMs and their compressed versions.
๐๏ธ Structural Attacks
๐ Semantic Preserving Transformations
We recommend using UV as the package installer for better dependency management and faster installation.
First, install UV using one of the following methods:
# For Linux/macOS with curl
curl -LsSf https://astral.sh/uv/install.sh | sh
# For Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Using pip (recommended)
pip install uv
If you only need basic functionality only with bitsandbytes quantization support:
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install the package with basic dependencies
uv pip install -e .
If you want to use all quantization features:
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install PyTorch first
uv pip install torch>=2.5.1
# Install with quantization dependencies
uv pip install --no-build-isolation -e .[quant]
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install PyTorch first to avoid build issues
uv pip install torch>=2.5.1
# Install all dependencies including quantization support
uv pip install --no-build-isolation -e .[all]
If you prefer using traditional pip:
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install PyTorch first
pip install torch>=2.5.1
# Install all dependencies
pip install --no-build-isolation -e .[all]
Note: The --no-build-isolation flag is needed for proper installation of quantization dependencies like autoawq. UV is recommended over pip for its improved dependency resolution and installation speed.
After installation, you can use the main functionality through the command-line interface. The framework provides three command-line interfaces:
adversarial-codegen attack [OPTIONS]
adversarial-codegen-test attack [OPTIONS]
adversarial-codegen-noise evaluate_single_model [OPTIONS]
adversarial-codegen and adversarial-codegen-test)adversarial-codegen-noise)# Attack original LLMs, full evaluation
adversarial-codegen attack \
--model_path /path/to/model \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Quick test
adversarial-codegen-test attack \
--model_path /path/to/model \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Attack LLMs with a specific adversarial attack method (synonym) and generation method (temperature sampling).
adversarial-codegen attack \
--model_path /path/to/model \
--dataset mbpp \
--attack_method synonym \
--replacement_prob 0.2 \
--max_synonyms 5 \
--temperature 0.8 \
--top_p 0.9 \
--num_beams 5 \
--seed 42 \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results \
--visualization True
# Attack LLMs with static quant (4-bit quant achieved by bnb)
adversarial-codegen attack \
--model_path /path/to/model \
--quantized_type static \
--quant_method bnb \
--quant_bits 4 \
--quant_type nf4 \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Attack LLMs with 8-bit quant
adversarial-codegen attack \
--model_path /path/to/model \
--quantized_type dynamic \
--quant_bits 8 \
--quantize_embeddings True \ # Generally don't quantize embedding layer
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Basic noise attack with default parameters
adversarial-codegen-noise evaluate_single_model \
--model_path /path/to/model \
--output_path results_noise.jsonl
# Noise attack with custom parameters
adversarial-codegen-noise evaluate_single_model \
--model_path /path/to/model \
--output_path results_noise.jsonl \
--noise_type gaussian \
--noise_level 1e-4 \
--quantization 4bit \
--max_length 1000
# Noise attack with 8-bit quantization
adversarial-codegen-noise evaluate_single_model \
--model_path /path/to/model \
--output_path results_8bit_noise.jsonl \
--quantization 8bit \
--noise_type uniform \
--noise_level 5e-4
The tool generates different types of outputs depending on the attack method used:
We welcome contributions! Please feel free to submit a Pull Request. For questions or suggestions, please contact:
This project builds upon and is inspired by several excellent works in the field:
Special thanks to all these projects that made our work possible.
This project is licensed under the MIT License - see the LICENSE file for details.
Python
93.5%
Shell
6.5%
This repository evaluates the robustness of Large Language Models (LLMs) under various adversarial attacks, focusing on code generation tasks. We test both original and compressed LLMs across different datasets to provide comprehensive insights into model vulnerabilities.
Our framework provides:
We currently support the following models:
โจ๏ธ Character Attack (char_attack)
๐ Synonym Attack (synonym_attack)
๐ Translation Attack (translate_attack)
๐ Noise Attack (NEW!)
We will support both original LLMs and their compressed versions.
๐๏ธ Structural Attacks
๐ Semantic Preserving Transformations
We recommend using UV as the package installer for better dependency management and faster installation.
First, install UV using one of the following methods:
# For Linux/macOS with curl
curl -LsSf https://astral.sh/uv/install.sh | sh
# For Windows (PowerShell)
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
# Using pip (recommended)
pip install uv
If you only need basic functionality only with bitsandbytes quantization support:
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install the package with basic dependencies
uv pip install -e .
If you want to use all quantization features:
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install PyTorch first
uv pip install torch>=2.5.1
# Install with quantization dependencies
uv pip install --no-build-isolation -e .[quant]
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install PyTorch first to avoid build issues
uv pip install torch>=2.5.1
# Install all dependencies including quantization support
uv pip install --no-build-isolation -e .[all]
If you prefer using traditional pip:
# Clone the repository
git clone https://github.com/yourusername/adversarial-codegen
cd adversarial-codegen
# Install PyTorch first
pip install torch>=2.5.1
# Install all dependencies
pip install --no-build-isolation -e .[all]
Note: The --no-build-isolation flag is needed for proper installation of quantization dependencies like autoawq. UV is recommended over pip for its improved dependency resolution and installation speed.
After installation, you can use the main functionality through the command-line interface. The framework provides three command-line interfaces:
adversarial-codegen attack [OPTIONS]
adversarial-codegen-test attack [OPTIONS]
adversarial-codegen-noise evaluate_single_model [OPTIONS]
adversarial-codegen and adversarial-codegen-test)adversarial-codegen-noise)# Attack original LLMs, full evaluation
adversarial-codegen attack \
--model_path /path/to/model \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Quick test
adversarial-codegen-test attack \
--model_path /path/to/model \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Attack LLMs with a specific adversarial attack method (synonym) and generation method (temperature sampling).
adversarial-codegen attack \
--model_path /path/to/model \
--dataset mbpp \
--attack_method synonym \
--replacement_prob 0.2 \
--max_synonyms 5 \
--temperature 0.8 \
--top_p 0.9 \
--num_beams 5 \
--seed 42 \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results \
--visualization True
# Attack LLMs with static quant (4-bit quant achieved by bnb)
adversarial-codegen attack \
--model_path /path/to/model \
--quantized_type static \
--quant_method bnb \
--quant_bits 4 \
--quant_type nf4 \
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Attack LLMs with 8-bit quant
adversarial-codegen attack \
--model_path /path/to/model \
--quantized_type dynamic \
--quant_bits 8 \
--quantize_embeddings True \ # Generally don't quantize embedding layer
--save_prompts /path/to/save/prompts \
--save_results /path/to/save/results
# Basic noise attack with default parameters
adversarial-codegen-noise evaluate_single_model \
--model_path /path/to/model \
--output_path results_noise.jsonl
# Noise attack with custom parameters
adversarial-codegen-noise evaluate_single_model \
--model_path /path/to/model \
--output_path results_noise.jsonl \
--noise_type gaussian \
--noise_level 1e-4 \
--quantization 4bit \
--max_length 1000
# Noise attack with 8-bit quantization
adversarial-codegen-noise evaluate_single_model \
--model_path /path/to/model \
--output_path results_8bit_noise.jsonl \
--quantization 8bit \
--noise_type uniform \
--noise_level 5e-4
The tool generates different types of outputs depending on the attack method used:
We welcome contributions! Please feel free to submit a Pull Request. For questions or suggestions, please contact:
This project builds upon and is inspired by several excellent works in the field:
Special thanks to all these projects that made our work possible.
This project is licensed under the MIT License - see the LICENSE file for details.
Python
93.5%
Shell
6.5%