Installation
Basic Installation
Install from PyPI:
pip install swe-pruner
Flash Attention Setup
SwePruner requires flash-attn which needs to be installed separately based on your system configuration. You can install it using one of the following methods:
- Pre-built wheel (recommended): Download the appropriate wheel file for your system from the flash-attention releases and install it:
pip install flash_attn-<version>-<platform>.whl
- From source: If no pre-built wheel is available, you can build from source:
pip install flash-attn --no-build-isolation
Note: Flash attention requires CUDA and specific PyTorch versions. Make sure your environment is compatible.
Model Download
The pre-trained model files are not included in the PyPI package. You need to download them separately:
- From HuggingFace Hub (if available):
# Using huggingface-hub
huggingface-cli download ayanami-kitasan/code-pruner --local-dir ./model
- Manual download: Download the model files and place them in a directory (e.g.,
./model) with the following structure:
model/
├── config.json
├── model.safetensors
├── tokenizer.json
├── tokenizer_config.json
└── ... (other tokenizer files)
Usage
Command Line Interface
Start the FastAPI server using the CLI:
swe-pruner --model-path ./model --port 8000
Options:
--host/-h: Host to bind the server to (default:0.0.0.0)--port/-p: Port to run the server on (default:8000)--model-path/-m: Path to model directory (overridesSWEPRUNER_MODEL_PATHenvironment variable)
You can also set the model path using an environment variable:
export SWEPRUNER_MODEL_PATH=./model
swe-pruner
Python API
Basic Usage
from swe_pruner.prune_wrapper import SwePrunerForCodePruning, PruneRequest
# Load the model
model = SwePrunerForCodePruning.from_pretrained("./model")
# Create a prune request
request = PruneRequest(
query="Find functions that handle user authentication",
code="""
def login(username, password):
# Authentication logic
if verify_credentials(username, password):
return create_session(username)
return None
def logout(session_id):
# Logout logic
invalidate_session(session_id)
""",
threshold=0.5,
always_keep_first_frags=False,
chunk_overlap_tokens=50
)
# Prune the code
response = model.prune(request)
print(f"Relevance score: {response.score}")
print(f"Pruned code:\n{response.pruned_code}")
print(f"Token count: {response.origin_token_cnt} -> {response.left_token_cnt}")
API Response
The PruneResponse object contains:
score: Document-level relevance score (float)pruned_code: Pruned code string with filtered sections markedtoken_scores: List of [token, score] pairskept_frags: List of kept line numbersorigin_token_cnt: Original token countleft_token_cnt: Remaining token count after pruningmodel_input_token_cnt: Total tokens sent to the modelerror_msg: Error message if any (optional)
FastAPI Server
Once the server is running, you can interact with it via HTTP:
Health Check
curl http://localhost:8000/health
Prune Code
curl -X POST http://localhost:8000/prune \
-H "Content-Type: application/json" \
-d '{
"query": "Find authentication functions",
"code": "def login(): ...",
"threshold": 0.5
}'
Configuration
Model Parameters
The model supports various configuration options through SwePrunerConfig:
backbone_model_name_or_path: Backbone model identifierbottleneck: Bottleneck dimension (default: 256)dropout: Dropout rate (default: 0.4)num_fusion_layers: Number of fusion layers (default: 1)num_heads: Number of attention heads (default: 8)use_multi_layer_fusion: Whether to use multi-layer fusion (default: True)compression_head_type: Type of compression head ("ffn", "simple", or "crf")
Pruning Parameters
threshold: Score threshold for keeping tokens (default: 0.5)always_keep_first_frags: Always keep the first N fragments (default: False)chunk_overlap_tokens: Overlap tokens between chunks for long code (default: 50)
Requirements
- Python >= 3.12
- PyTorch >= 2.8.0
- Transformers >= 4.57.1
- CUDA (for GPU acceleration)
- Flash Attention 2
See pyproject.toml for the complete list of dependencies.
License
MIT License
Metadata
Release files for swe-pruner 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| swe_pruner-0.1.1.tar.gz | 54.7 kB | Without provenance | Details for swe_pruner-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| swe_pruner-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for swe_pruner-0.1.1-py3-none-any.whl |
Total release size: 72.9 kB
Release files / swe_pruner-0.1.1.tar.gz
| Download URL | swe_pruner-0.1.1.tar.gz |
|---|---|
| Size | 54.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9d365a09508c006e190dd1ca6fd3e2e586eadd538c28a8eaf9bc0c64cf47a0a2
|
|
BLAKE2b-256 checksum How to use checksums |
9ad19fae039831518d402973f221499eacd2632bcbd9a9ceb45e1ffd86d76813
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Release files / swe_pruner-0.1.1-py3-none-any.whl
| Download URL | swe_pruner-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6b445890b2f03f0a57ca77538237590b30ec84bf5411c22f7a5f0da1af053fb9
|
|
BLAKE2b-256 checksum How to use checksums |
2098c2c93085bf228d2203680ffe30d0c5a6f80ce77195983605ce4784dc1bca
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.8.22
|
Security for swe-pruner 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.