Skip to content

Commit 2795ce3

Browse files
committed
encode decode
1 parent 0339bb5 commit 2795ce3

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

xtra_labs/llm_finetune/draft.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
# TEXT: overview of LLM lab
2020
# Load pretrained LLM (medium size model)
2121

22-
model_name = "facebook/opt-1.3b"
22+
model_name = "facebook/opt-125m"
23+
# model_name = "facebook/opt-1.3b"
2324
# had to load non TF version to run benchmarking code
2425
model = transformers.AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
2526
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
@@ -42,9 +43,9 @@ def generate(start_text, model, tokenizer, num_steps=20, temp=1.):
4243
num_start = len(x)
4344

4445
for i in range(num_steps):
45-
input_tensor = tf.reshape(tf.constant(x), [1, -1])
46+
input_tensor = torch.tensor(x).view(1, -1).to("cuda")
4647
logits = model(input_tensor).logits
47-
probs = tf.nn.softmax(logits/temp)[0, -1, :]
48+
probs = F.softmax(logits/temp, dim=-1)[0, -1, :].cpu().detach()
4849

4950
new_token = predict_next_token(probs, tokenizer)
5051
x.append(new_token)
@@ -149,11 +150,22 @@ def replace_linear_with_lora(module):
149150
model = model.to("cuda")
150151
for batch in ft_dataset:
151152
prompt = batch["text"]
152-
encoding = tokenizer(prompt)
153-
input_ids = torch.IntTensor(encoding["input_ids"]).to("cuda").unsqueeze(0)
154-
attention_mask = torch.Tensor(encoding["attention_mask"]).to("cuda").unsqueeze(0)
155-
outputs = model(input_ids, attention_mask)
156153

154+
# encode with tokenizer
155+
x = tokenizer.encode(prompt)
156+
x_tensor = torch.tensor(x).view(1, -1).to("cuda")
157+
input_tensor = x_tensor[:,:context_length]
158+
target_next_word = x_tensor[:,context_length]
159+
160+
# run through model
161+
logits = model(input_tensor).logits
162+
163+
probs = F.softmax(logits, dim=-1)[0, -1, :].cpu().detach()
164+
new_token = np.random.choice(len(probs), p=probs.numpy())
165+
print(tokenizer.decode(new_token), end='', flush=True)
166+
167+
# apply loss
168+
157169

158170
# evaluate finetuned model on benchmark
159171
category_accs_1300m_ft, avg_acc_1300m_ft = run_benchmark(model, tokenizer, dataset)

0 commit comments

Comments
 (0)