Views
No views yet
1import torch
2import transformers
3from transformers import AutoTokenizer
4
5tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neox-20b")
6tokenizer.pad_token = tokenizer.eos_token
7
8device = torch.device("cuda")
9model_name = "Nethermind/Mpt-Instruct-DotNet-S"
10config = transformers.AutoConfig.from_pretrained(model_name, trust_remote_code=True)
11config.init_device = device
12config.max_seq_len = 1024
13config.attn_config['attn_impl'] = 'torch'
14config.use_cache = False
15
16model = transformers.AutoModelForCausalLM.from_pretrained(
17 model_name,
18 config=config,
19 torch_dtype=torch.bfloat16,
20 trust_remote_code=True,
21 ignore_mismatched_sizes=True,
22 # load_in_8bit=True # when low on GPU memory
23)
24model.eval()
25
26INSTRUCTION_KEY = "### Instruction:"
27RESPONSE_KEY = "### Response:"
28PROMPT_FOR_GENERATION_FORMAT = """{system}
29{instruction_key}
30{instruction}
31{response_key}
32""".format(
33 system="{system}",
34 instruction_key=INSTRUCTION_KEY,
35 instruction="{instruction}",
36 response_key=RESPONSE_KEY
37)
38
39def give_answer(instruction="Create a loop over [0, 6, 7 , 77] that prints its contentrs", system="You are an experienced .Net C# developer. Below is an instruction that describes a task. Write a response that completes the request providing detailed explanations with code examples.", ):
40 question = PROMPT_FOR_GENERATION_FORMAT.format(system=system, instruction=instruction)
41 input_tokens = tokenizer.encode(question ,return_tensors='pt')
42 model.generate(input_tokens.to(device), max_new_tokens=min(512, 1024 - input_tokens.shape[1]), do_sample=False, top_k=1, top_p=0.95)
43 outputs = output_loop(tokenized_question)
44 answer = tokenizer.batch_decode(outputs, skip_special_tokens=True)
45 print(answer[0])
46IEthRpcModule : Short Q->Code, Explain Code X -> Step-By-Step (7k)