Views
No views yet
1import time
2import sys, os
3import dataclasses
4sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5
6from exllamav2 import(
7 ExLlamaV2,
8 ExLlamaV2Config,
9 ExLlamaV2Cache,
10 ExLlamaV2Tokenizer,
11)
12
13from exllamav2.generator import (
14 ExLlamaV2BaseGenerator,
15 ExLlamaV2Sampler
16)
17
18@dataclasses.dataclass
19class ModelClass:
20 generator: ExLlamaV2BaseGenerator
21 tokenizer: ExLlamaV2Tokenizer
22
23DEBUG = os.environ.get("DEBUG") and True or False
24
25# Initialize model and cache
26def load_model(model_directory, max_seq_len=8192):
27 """
28 Loads a model from a directory and return the generator and tokenizer
29 """
30 config = ExLlamaV2Config()
31 config.model_dir = model_directory
32 config.max_seq_len = max_seq_len
33 config.prepare()
34
35 model = ExLlamaV2(config)
36 print("Loading model: " + model_directory)
37
38 cache = ExLlamaV2Cache(model, lazy = True, max_seq_len=max_seq_len)
39 model.load_autosplit(cache)
40
41 tokenizer = ExLlamaV2Tokenizer(config)
42 generator = ExLlamaV2BaseGenerator(model, cache, tokenizer)
43 model = ModelClass(generator=generator, tokenizer=tokenizer)
44 generator.warmup()
45 return model
46
47def generate_text(prompt, model, settings, max_new_tokens):
48 time_begin = time.time()
49 response = model.generator.generate_simple(prompt, settings, max_new_tokens)
50 response = response[len(prompt):]
51 time_end = time.time()
52 time_total = time_end - time_begin
53 tokens = model.tokenizer.encode(response)
54 count = tokens.shape[-1]
55 print(f"Response generated in {time_total:.2f} seconds, {count} tokens, {count / time_total:.2f} tokens/second, character len: {len(response)}")
56 return response
57
58model_actor = load_model("/models/HelixNet-actor-6.0bpw-h6-exl2")
59model_critic = load_model("/models/HelixNet-critic-6.0bpw-h6-exl2")
60model_regenerator = load_model("/models/HelixNet-regenerator-6.0bpw-h6-exl2")
61
62settings = ExLlamaV2Sampler.Settings()
63settings.temperature = 0.75
64settings.top_k = 50
65settings.top_p = 1.0
66max_new_tokens = 2000
67
68system_prompt = "You are HelixNet. Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation."
69
70while True:
71 user_input = input("You: ")
72
73 prompt_actor = f"SYSTEM: {system_prompt}\nUSER: {user_input}\nASSISTANT: "
74 if DEBUG: print(f"{prompt_actor}\n\n")
75 print("ACTOR:")
76 response_actor = generate_text(prompt_actor, model_actor, settings, max_new_tokens)
77 if DEBUG: print(f"{response_actor}\n\n")
78 print("="*132)
79
80 prompt_critic = f"SYSTEM: {system_prompt}\nUSER: {user_input}\nRESPONSE: {response_actor}\nCRITIQUE: "
81 if DEBUG: print(f"{prompt_critic}\n\n")
82 print("CRITIQUE:")
83 response_critic = generate_text(prompt_critic, model_critic, settings, max_new_tokens)
84 if DEBUG: print(f"{response_critic}\n\n")
85 print("="*132)
86
87 prompt_regenerator = f"SYSTEM: {system_prompt}\nUSER: {user_input}\nRESPONSE: {response_actor}\nCRITIQUE: {response_critic}\nREGENERATOR: "
88 if DEBUG: print(f"{prompt_regenerator}\n\n")
89 print("REGENERATION:")
90 response_regenerator = generate_text(prompt_regenerator, model_regenerator, settings, max_new_tokens)
91 print("="*132)
92 conversation = f"SYSTEM: {system_prompt}\nUSER: {user_input}\nASSISTANT: {response_regenerator}"
93 print(conversation)
actor, a critic, and a regenerator. The actor LLM produces an initial response to a given system-context and a question. The critic then takes in as input, a tuple of (system-context, question, response) and provides a critique based on the provided answer to the given system-context and the question. Its job is not to criticize, but to provide an intelligent critique so that the answer can be modified/regenerated to address the question better. Finally, the regenerator takes in a tuple of (system-context, question, response, critique) and regenerates the answer.| Task | Metric | Value |
| arc_challenge | acc_norm | 62.28 |
| hellaswag | acc_norm | 83.22 |
| mmlu | acc_norm | 63.10 |
| truthfulqa_mc | mc2 | 50.10 |
| Total Average | - | 0.64675 |
critic and the regenerator was tested not only on the accopanying actor model, but 13B and 70B SynthIA models as well. They seem to be readily transferrable, as the function that it has learnt is to provide an intelligent critique and then a regeneration of the original response. Please feel free to try out other models as the actor. However, the architecture works best with all three as presented here in HelixNet.


SYSTEM: Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation.
USER: What is the relationship between Earth's atmosphere, magnetic field and gravity?
ASSISTANT:critic and the regenerator.1import torch, json
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_path_actor = "/home/ubuntu/llm/HelixNet/actor"
5model_path_critic = "/home/ubuntu/llm/HelixNet/critic"
6model_path_regenerator = "/home/ubuntu/llm/HelixNet/regenerator"
7
8def load_model(model_path):
9 model = AutoModelForCausalLM.from_pretrained(
10 model_path,
11 torch_dtype=torch.float16,
12 device_map="cuda",
13 load_in_4bit=False,
14 trust_remote_code=True,
15 )
16 return model
17
18def load_tokenizer(model_path):
19 tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
20 return tokenizer
21
22model_actor = load_model(model_path_actor)
23model_critic = load_model(model_path_critic)
24model_regenerator = load_model(model_path_regenerator)
25
26tokenizer_actor = load_tokenizer(model_path_actor)
27tokenizer_critic = load_tokenizer(model_path_critic)
28tokenizer_regenerator = load_tokenizer(model_path_regenerator)
29
30def generate_text(instruction, model, tokenizer):
31 tokens = tokenizer.encode(instruction)
32 tokens = torch.LongTensor(tokens).unsqueeze(0)
33 tokens = tokens.to("cuda")
34
35 instance = {
36 "input_ids": tokens,
37 "top_p": 1.0,
38 "temperature": 0.75,
39 "generate_len": 1024,
40 "top_k": 50,
41 }
42
43 length = len(tokens[0])
44 with torch.no_grad():
45 rest = model.generate(
46 input_ids=tokens,
47 max_length=length + instance["generate_len"],
48 use_cache=True,
49 do_sample=True,
50 top_p=instance["top_p"],
51 temperature=instance["temperature"],
52 top_k=instance["top_k"],
53 num_return_sequences=1,
54 )
55 output = rest[0][length:]
56 string = tokenizer.decode(output, skip_special_tokens=True)
57 return f"{string}"
58
59system_prompt = "You are HelixNet. Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation."
60
61
62while True:
63 user_input = input("You: ")
64
65 prompt_actor = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nASSISTANT: "
66 actor_response = generate_text(prompt_actor, model_actor, tokenizer_actor)
67 print(f"ACTOR: {actor_response}\n\n")
68
69 prompt_critic = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nRESPONSE: {actor_response} \nCRITIQUE:"
70 critic_response = generate_text(prompt_critic, model_critic, tokenizer_critic)
71 print(f"CRITIQUE: {critic_response}\n\n")
72
73 prompt_regenerator = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nRESPONSE: {actor_response} \nCRITIQUE: {critic_response} \nREGENERATOR:"
74 regenerator_response = generate_text(prompt_regenerator, model_regenerator, tokenizer_regenerator)
75 print(f"REGENERATION: {regenerator_response}")
761import torch, json
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_path_actor = "/home/ubuntu/llm/HelixNet/actor"
5model_path_critic = "/home/ubuntu/llm/HelixNet/critic"
6model_path_regenerator = "/home/ubuntu/llm/HelixNet/regenerator"
7
8def load_model(model_path):
9 model = AutoModelForCausalLM.from_pretrained(
10 model_path,
11 torch_dtype=torch.float16,
12 device_map="cuda",
13 load_in_4bit=False,
14 trust_remote_code=True,
15 )
16 return model
17
18def load_tokenizer(model_path):
19 tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
20 return tokenizer
21
22model_actor = load_model(model_path_actor)
23model_critic = load_model(model_path_critic)
24model_regenerator = load_model(model_path_regenerator)
25
26tokenizer_actor = load_tokenizer(model_path_actor)
27tokenizer_critic = load_tokenizer(model_path_critic)
28tokenizer_regenerator = load_tokenizer(model_path_regenerator)
29
30def generate_text(instruction, model, tokenizer):
31 tokens = tokenizer.encode(instruction)
32 tokens = torch.LongTensor(tokens).unsqueeze(0)
33 tokens = tokens.to("cuda")
34
35 instance = {
36 "input_ids": tokens,
37 "top_p": 1.0,
38 "temperature": 0.75,
39 "generate_len": 1024,
40 "top_k": 50,
41 }
42
43 length = len(tokens[0])
44 with torch.no_grad():
45 rest = model.generate(
46 input_ids=tokens,
47 max_length=length + instance["generate_len"],
48 use_cache=True,
49 do_sample=True,
50 top_p=instance["top_p"],
51 temperature=instance["temperature"],
52 top_k=instance["top_k"],
53 num_return_sequences=1,
54 )
55 output = rest[0][length:]
56 string = tokenizer.decode(output, skip_special_tokens=True)
57 return f"{string}"
58
59system_prompt = "You are HelixNet. Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation."
60
61conversation = f"SYSTEM:{system_prompt}"
62
63while True:
64 user_input = input("You: ")
65
66 prompt_actor = f"{conversation} \nUSER: {user_input} \nASSISTANT: "
67 actor_response = generate_text(prompt_actor, model_actor, tokenizer_actor)
68 print("Generated ACTOR RESPONSE")
69
70 prompt_critic = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nRESPONSE: {actor_response} \nCRITIQUE:"
71 critic_response = generate_text(prompt_critic, model_critic, tokenizer_critic)
72 print("Generated CRITIQUE")
73
74 prompt_regenerator = f"SYSTEM: {system_prompt} \nUSER: {user_input} \nRESPONSE: {actor_response} \nCRITIQUE: {critic_response} \nREGENERATOR:"
75 regenerator_response = generate_text(prompt_regenerator, model_regenerator, tokenizer_regenerator)
76 print("Generated REGENERATION")
77
78 conversation = f"{conversation} \nUSER: {user_input} \nASSISTANT: {regenerator_response}"
79 print(conversation)