File size: 1,770 Bytes
2a21e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from clearml import Model
import torch
import os
# Import needed classes for local loading and LoRA construction
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from peft import LoraConfig, get_peft_model

# 1. Download the LoRA checkpoint artifact from ClearML
CLEARML_MODEL_ID = "34e25deb24c64b74b29c8519ed15fe3e"
model_obj = Model(model_id=CLEARML_MODEL_ID)
checkpoint_path = model_obj.get_local_copy()
adapter_dir = os.path.dirname(checkpoint_path)
print(f"LoRA checkpoint downloaded to: {checkpoint_path}")

# 2. Load the base pretrained CodeT5 model and tokenizer from local config.json directory
BASE_MODEL_PATH = "microsoft/codereviewer"
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_PATH)
base_model = AutoModelForSeq2SeqLM.from_pretrained(BASE_MODEL_PATH)

# Print all base model parameters and their shapes
print("\nBase model parameters:")
for name, param in base_model.named_parameters():
    print(f"{name}: {tuple(param.shape)}")

# 3. Reconstruct and attach LoRA adapters
lora_config = LoraConfig(
    r=64,
    lora_alpha=128,
    target_modules=["q", "k", "v", "o", "wi", "wo"],
    lora_dropout=0.05,
    bias="none",
    task_type="SEQ_2_SEQ_LM"
)
model = get_peft_model(base_model, lora_config)

# 4. Load LoRA adapter weights from ClearML checkpoint
adapter_state = torch.load(checkpoint_path, map_location="cpu")
model.load_state_dict(adapter_state, strict=False)

# 5. Move to CPU and set evaluation mode
model.to("cpu").eval()

print("Model with LoRA adapters loaded and ready for inference.")

# Print out all LoRA adapter parameter names and shapes as before
print("\nFinetuned (LoRA adapter) parameters:")
for name, param in model.named_parameters():
    if "lora_" in name:
        print(f"{name}: {tuple(param.shape)}")