Views
No views yet
sigmoid(projector(last_hidden_state[:, -1]))projector.pth.issuepatchreview<issue>{issue}</issue><patch>{patch}</patch><review>{review}<review>1from pathlib import Path
2import json
3
4import torch
5import torch.nn as nn
6from transformers import AutoModelForCausalLM, AutoTokenizer
7
8
9MODEL_DIR = "codefuse-ai/SWE-CARE-RM"
10MAX_SEQ_LEN = 51200
11MIN_REVIEW_LEN = 4096
12TRUST_REMOTE_CODE = True
13
14with open(f"{MODEL_DIR}/data_sample.jsonl", "r") as fr:
15 for line in fr:
16 json_data = json.loads(line)
17 break
18
19SAMPLE = {
20 "issue": json_data['problem_statement'],
21 "patch": json_data['patch_to_review'],
22 "review": json_data['pos_review'][0]
23}
24
25class Projector(nn.Module):
26 def __init__(self, arch, input_size, hidden_size, use_bf16):
27 super().__init__()
28 depth = int(arch[len("mlp"): arch.index("x_relu")])
29 layers = [nn.Linear(input_size, hidden_size).bfloat16() if use_bf16 else
30nn.Linear(input_size, hidden_size)]
31 for _ in range(1, depth):
32 layers.append(nn.ReLU())
33 layers.append(nn.Linear(hidden_size, 1).bfloat16() if use_bf16 else
34nn.Linear(hidden_size, 1))
35 self.model = nn.Sequential(*layers)
36
37 def forward(self, x):
38 return self.model(x)
39
40
41def resolve_dtype(dtype_name):
42 if dtype_name in {"bf16", "bfloat16"}:
43 return torch.bfloat16
44 if dtype_name in {"fp16", "float16"}:
45 return torch.float16
46 return torch.float32
47
48
49def infer_proj_arch(projector_state_dict):
50 linear_weight_keys = [k for k in projector_state_dict if k.startswith("model.")
51and k.endswith(".weight")]
52 return f"mlp{len(linear_weight_keys)}x_relu"
53
54
55def process_one(issue_ids, issue_masks, patch_ids, patch_masks, review_ids,
56review_masks, max_len, min_review_len):
57 review_keep = min(min_review_len, len(review_ids))
58 remain_for_patch = max(max_len - len(issue_ids) - review_keep, 0)
59 patch_keep = min(len(patch_ids), remain_for_patch)
60
61 ids_all = issue_ids + patch_ids[:patch_keep] + review_ids[-review_keep:]
62 masks_all = issue_masks + patch_masks[:patch_keep] + review_masks[-review_keep:]
63
64 if len(ids_all) < max_len:
65 pad_len = max_len - len(ids_all)
66 ids_all = [0] * pad_len + ids_all
67 masks_all = [0] * pad_len + masks_all
68
69 return ids_all[:max_len], masks_all[:max_len]
70
71
72reward_config = {}
73reward_config_path = Path(MODEL_DIR) / "reward_config.json"
74if reward_config_path.exists():
75 reward_config = json.load(open(reward_config_path, "r", encoding="utf-8"))
76
77projector_path = Path(MODEL_DIR) / "projector.pth"
78projector_state_dict = torch.load(projector_path, map_location="cpu")
79proj_arch = reward_config.get("proj_arch") or infer_proj_arch(projector_state_dict)
80torch_dtype = resolve_dtype(reward_config.get("torch_dtype") or "bfloat16")
81attn_implementation = reward_config.get("attn_implementation")
82
83tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR,
84trust_remote_code=TRUST_REMOTE_CODE, padding_side="left")
85
86model_kwargs = {"trust_remote_code": TRUST_REMOTE_CODE, "torch_dtype": torch_dtype}
87if attn_implementation:
88 model_kwargs["attn_implementation"] = attn_implementation
89decoder = AutoModelForCausalLM.from_pretrained(MODEL_DIR, **model_kwargs)
90
91projector = Projector(proj_arch, decoder.config.hidden_size,
92decoder.config.hidden_size, torch_dtype == torch.bfloat16)
93projector.load_state_dict(projector_state_dict)
94
95device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
96decoder.to(device).eval()
97projector.to(device).eval()
98
99issue_inputs = tokenizer(f"<issue>{SAMPLE['issue']}</issue>", padding=False,
100truncation="longest_first")
101patch_inputs = tokenizer(f"<patch>{SAMPLE['patch']}</patch>", padding=False,
102truncation="longest_first")
103review_inputs = tokenizer(SAMPLE["review"], padding=False, truncation="longest_first")
104
105input_ids, attention_mask = process_one(
106 issue_inputs["input_ids"],
107 issue_inputs["attention_mask"],
108 patch_inputs["input_ids"],
109 patch_inputs["attention_mask"],
110 review_inputs["input_ids"],
111 review_inputs["attention_mask"],
112 max_len=MAX_SEQ_LEN,
113 min_review_len=MIN_REVIEW_LEN,
114)
115
116inputs = {
117 "input_ids": torch.tensor([input_ids], dtype=torch.long, device=device),
118 "attention_mask": torch.tensor([attention_mask], dtype=torch.long, device=device),
119}
120
121with torch.no_grad():
122 hidden_state = decoder(**inputs, output_hidden_states=True).hidden_states[-1]
123 reward = torch.sigmoid(projector(hidden_state).squeeze(-1)[:, -1]).item()
124
125print(reward)@misc{guo2025codefusecrbenchcomprehensivenessawarebenchmarkendtoend,
title={CodeFuse-CR-Bench: A Comprehensiveness-aware Benchmark for End-to-End Code Review Evaluation in Python Projects},
author={Hanyang Guo and Xunjin Zheng and Zihan Liao and Hang Yu and Peng DI and Ziyin Zhang and Hong-Ning Dai},
year={2025},
eprint={2509.14856},
archivePrefix={arXiv},
primaryClass={cs.SE},
url={https://arxiv.org/abs/2509.14856},
}