Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model = AutoModelForCausalLM.from_pretrained(
5 "seanmor5/phi-2-function-identification",
6 attn_implementation="flash_attention_2",
7 torch_dtype=torch.bfloat16,
8)
9model.to(torch.device("cuda"))
10tokenizer = AutoTokenizer.from_pretrained("seanmor5/phi-2-function-identification")
11
12def prompt(code):
13 return (
14 "Input: Given the following disassembled code, provide a descriptive"
15 + " function name for the code. Your function name should"
16 + " accurately describe the purpose of the code. It should"
17 + " be formatted in C style with lowercase and snakecase."
18 + f" Only output the name as valid JSON, e.g. {json.dumps({'name': 'function_name'})}"
19 + f"\nCode: {code}\nOutput:"
20 )
21
22def identify_function(code):
23 eos_tokens = tokenizer.convert_tokens_to_ids(['"}', "<|endoftext|>"])
24 inputs = tokenizer(prompt(func), return_tensors="pt")
25 inputs.to(torch.device("cuda"))
26
27 outputs = model.generate(**inputs, max_new_tokens=64, eos_token_id=eos_tokens)
28 text = tokenizer.batch_decode(outputs[:, inputs["input_ids"].shape[1] :])[0]
29 return text
30
31func = """
32void fcn.140030b80(ulong param_1, ulong param_2, ulong param_3) {
33 ulong uVar1; uVar1 = fcn.140030ae0(param_3);
34 fcn.14002efc0(param_1, param_2, uVar1); return;
35}
36"""
37
38print(identify_function(func))"} when generating.