Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Buggy-to-Fixed-Code-ViT1D
|
| 2 |
+
|
| 3 |
+
A 1D Vision Transformer that maps buggy code embeddings to fixed code embeddings.
|
| 4 |
+
Datasets that the model can use can be found on: https://huggingface.co/datasets/ASSERT-KTH/RunBugRun-Final
|
| 5 |
+
More details about the models training at: https://github.com/ASSERT-KTH/code-embedding-difference
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
## Usage
|
| 9 |
+
```python
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
from huggingface_hub import hf_hub_download
|
| 13 |
+
import pickle
|
| 14 |
+
|
| 15 |
+
# Define model architecture
|
| 16 |
+
class ViT1D(nn.Module):
|
| 17 |
+
def __init__(self, input_size=1024, patch_size=16, emb_dim=256, depth=4, heads=8, mlp_ratio=4):
|
| 18 |
+
super().__init__()
|
| 19 |
+
assert input_size % patch_size == 0
|
| 20 |
+
self.num_patches = input_size // patch_size
|
| 21 |
+
self.patch_size = patch_size
|
| 22 |
+
self.patch_embed = nn.Linear(patch_size, emb_dim)
|
| 23 |
+
self.pos_embed = nn.Parameter(torch.randn(1, self.num_patches, emb_dim))
|
| 24 |
+
encoder_layer = nn.TransformerEncoderLayer(
|
| 25 |
+
d_model=emb_dim, nhead=heads,
|
| 26 |
+
dim_feedforward=emb_dim * mlp_ratio, batch_first=True
|
| 27 |
+
)
|
| 28 |
+
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=depth)
|
| 29 |
+
self.output_layer = nn.Linear(emb_dim * self.num_patches, input_size)
|
| 30 |
+
|
| 31 |
+
def forward(self, x):
|
| 32 |
+
bsz = x.size(0)
|
| 33 |
+
x = x.view(bsz, self.num_patches, self.patch_size)
|
| 34 |
+
x = self.patch_embed(x) + self.pos_embed
|
| 35 |
+
x = self.transformer(x)
|
| 36 |
+
x = x.flatten(1)
|
| 37 |
+
return self.output_layer(x)
|
| 38 |
+
|
| 39 |
+
# Load model
|
| 40 |
+
model = ViT1D(input_size=1024, patch_size=16, emb_dim=256, depth=4, heads=8, mlp_ratio=4)
|
| 41 |
+
model_path = hf_hub_download(
|
| 42 |
+
repo_id="ASSERT-KTH/Buggy-to-Fixed-Code-ViT1D",
|
| 43 |
+
filename="pytorch_model.pth"
|
| 44 |
+
)
|
| 45 |
+
model.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 46 |
+
model.eval()
|
| 47 |
+
|
| 48 |
+
# Load and predict
|
| 49 |
+
file_path = hf_hub_download(
|
| 50 |
+
repo_id="ASSERT-KTH/RunBugRun-Final",
|
| 51 |
+
filename="Embeddings_RBR/buggy_fixed_embeddings/buggy_fixed_embeddings_chunk_0000.pkl",
|
| 52 |
+
repo_type="dataset"
|
| 53 |
+
)
|
| 54 |
+
with open(file_path, 'rb') as f:
|
| 55 |
+
data = pickle.load(f)
|
| 56 |
+
buggy_embeddings = data['buggy_embeddings']
|
| 57 |
+
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
buggy_tensor = torch.tensor(buggy_embeddings[0:1], dtype=torch.float32)
|
| 60 |
+
predicted_fixed = model(buggy_tensor).numpy()
|
| 61 |
+
|
| 62 |
+
print("Predicted fixed embedding:")
|
| 63 |
+
print(predicted_fixed[0])
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
## Model Details
|
| 67 |
+
|
| 68 |
+
- **Architecture:** 1D Vision Transformer
|
| 69 |
+
- **Input:** Buggy code embeddings
|
| 70 |
+
- **Output:** Fixed code embeddings
|