-
Notifications
You must be signed in to change notification settings - Fork 15
/
inference.py
191 lines (162 loc) · 6.3 KB
/
inference.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#from utils import *
from hydra_moe.inference import get_inference_model, get_base_inference_model
from hydra_moe.router import get_weights, mult_weights_by_alpha
from hydra_moe.args import *
from hydra_moe.utils import *
from collections import defaultdict
import copy
import json
import os
from os.path import exists, join, isdir
from dataclasses import dataclass, field
import sys
from typing import Optional, Dict, Sequence
import numpy as np
from tqdm import tqdm
import logging
import bitsandbytes as bnb
import pandas as pd
import yaml
import torch
import transformers
from torch.nn.utils.rnn import pad_sequence
import argparse
from transformers import (
set_seed,
Seq2SeqTrainer,
)
torch.backends.cuda.matmul.allow_tf32 = True
logger = logging.getLogger(__name__)
IGNORE_INDEX = -100
model = None
tokenizer = None
centroids = {}
kmeans_centroids = {}
generation_args = None
def initialize_model():
global model, tokenizer, base_model, base_tokenizer, generation_args
hfparser = transformers.HfArgumentParser((
ModelArguments, DataArguments, TrainingArguments, GenerationArguments
))
model_args, data_args, training_args, generation_args, extra_args = \
hfparser.parse_args_into_dataclasses(return_remaining_strings=True)
training_args.generation_config = transformers.GenerationConfig(**vars(generation_args))
args = argparse.Namespace(
**vars(model_args), **vars(data_args), **vars(training_args)
)
print(args)
cluster_nums = range(32)
# checkpoint_dirs = [
# {
# "adapter_dir": f"HydraLM/Nous-Hermes-llama-2-7b_7b_cluster{str(cluster).zfill(3) if cluster >= 10 else str(cluster).zfill(2)}_partitioned_v3_standardized_{str(cluster).zfill(3) if cluster >= 10 else str(cluster).zfill(2)}",
# "adapter_name": f"{str(cluster)}"
# }
# for cluster in cluster_nums
# ]
checkpoint_dirs = [
{
"adapter_dir": f"HydraLM/expert-{str(cluster)}",
"adapter_name": f"{str(cluster)}"
}
for cluster in cluster_nums
]
#Load PEFT adapters to model
print(checkpoint_dirs)
print(args)
model, tokenizer = get_inference_model(args, checkpoint_dirs)
base_model, base_tokenizer = get_base_inference_model(args, checkpoint_dirs)
model.config.use_cache = False
base_model.config.use_cache = False
print('loaded model')
set_seed(args.seed)
# Verifying the datatypes and parameter counts before training.
dtypes = {}
for _, p in model.named_parameters():
dtype = p.dtype
if dtype not in dtypes: dtypes[dtype] = 0
dtypes[dtype] += p.numel()
total = 0
for k, v in dtypes.items(): total+= v
for k, v in dtypes.items():
print(k, v, v/total)
logger.info("*** Predict ***")
# load_kmeans()
# load_centroid()
load_gating32()
def generate_prompt(instruction, input=None):
prompt = f"### User:\n{instruction.strip()}\n\n"
return prompt + "### Assistant:\n"
def generate_output(instruction, model, alphas, tokenizer, generation_args, count = 320):
prompt = generate_prompt(instruction)
inputs = tokenizer(prompt, return_tensors="pt").to('cuda')
print(f'Updating alphas to {alphas}')
model.update_alphas(alphas)
with torch.no_grad():
generation_output = model.generate(
input_ids=inputs["input_ids"],
max_length=count,
max_new_tokens = count,
do_sample=generation_args.do_sample,
num_beams=generation_args.num_beams,
temperature=generation_args.temperature,
top_k=generation_args.top_k,
top_p=generation_args.top_p,
repetition_penalty=generation_args.repetition_penalty,
length_penalty=generation_args.length_penalty,
no_repeat_ngram_size=generation_args.no_repeat_ngram_size,
num_return_sequences=1,
)
output = tokenizer.decode(generation_output[0], skip_special_tokens=False)
return output
def generate_base_output(instruction, model, alphas, tokenizer, generation_args, count = 320):
prompt = generate_prompt(instruction)
inputs = tokenizer(prompt, return_tensors="pt").to('cuda')
with torch.no_grad():
generation_output = model.generate(
input_ids=inputs["input_ids"],
max_length=count,
max_new_tokens = count,
do_sample=generation_args.do_sample,
num_beams=generation_args.num_beams,
temperature=generation_args.temperature,
top_k=generation_args.top_k,
top_p=generation_args.top_p,
repetition_penalty=generation_args.repetition_penalty,
length_penalty=generation_args.length_penalty,
no_repeat_ngram_size=generation_args.no_repeat_ngram_size,
num_return_sequences=1,
)
output = tokenizer.decode(generation_output[0], skip_special_tokens=False)
return output
def inference():
while True:
# Get user input
instruction = input("Enter your instruction: ")
methodIn = input("Enter your mode: transformer, centroid, combined ")
alphaIn = 2
expertsK = 3
#methods: combined, transformer, multi, kmeans, centroid
try:
weights = get_weights(instruction, methodIn)
except:
print("Switching to transformer")
weights = get_weights(instruction, "transformer")
# weights = get_weights(instruction, "transformer")
alphas = mult_weights_by_alpha(weights, int(alphaIn), int(expertsK) )
# alphas = mult_weights_by_alpha(weights, training_args.lora_alpha)
print("Predicted Weights:")
[print(k, v) for k, v in weights.items()]
output = generate_output(instruction, model, alphas, tokenizer, generation_args)
print("MoE Model:")
print(output)
output_base = generate_base_output(instruction, base_model, alphas, base_tokenizer, generation_args)
print("Base Model:")
print(output_base)
continue_prompt = input("Do you want to continue? (yes/no): ").strip()
if continue_prompt.lower() != "yes":
break
if __name__ == "__main__":
initialize_model()
inference()