use autocast
Browse files
app.py
CHANGED
@@ -1,10 +1,14 @@
|
|
1 |
-
import spaces
|
2 |
-
import gradio as gr
|
3 |
from threading import Thread
|
4 |
-
|
|
|
|
|
|
|
5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
6 |
import torch
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
# Define model options
|
10 |
MODEL_OPTIONS = {
|
@@ -39,23 +43,25 @@ def generate(
|
|
39 |
top_p = float(top_p)
|
40 |
|
41 |
inputs = current_tokenizer(prompt, return_tensors="pt").to(current_model.device)
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
|
57 |
-
|
58 |
-
|
59 |
|
60 |
# Write the prompt in blue
|
61 |
output = "<span style='color: blue;'>" + prompt + "</span>"
|
|
|
|
|
|
|
1 |
from threading import Thread
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from gradio.layouts import Accordion
|
5 |
+
import spaces
|
6 |
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
7 |
import torch
|
8 |
+
|
9 |
+
from open_lm.hf import *
|
10 |
+
from open_lm.precision import get_autocast
|
11 |
+
|
12 |
|
13 |
# Define model options
|
14 |
MODEL_OPTIONS = {
|
|
|
43 |
top_p = float(top_p)
|
44 |
|
45 |
inputs = current_tokenizer(prompt, return_tensors="pt").to(current_model.device)
|
46 |
+
autocast = get_autocast("amp_bf16")
|
47 |
+
|
48 |
+
with autocast():
|
49 |
+
generate_kwargs = dict(
|
50 |
+
**inputs,
|
51 |
+
max_new_tokens=max_new_tokens,
|
52 |
+
temperature=temperature,
|
53 |
+
top_p=top_p,
|
54 |
+
repetition_penalty=repetition_penalty,
|
55 |
+
do_sample=True,
|
56 |
+
pad_token_id=current_tokenizer.eos_token_id
|
57 |
+
)
|
58 |
|
59 |
+
streamer = TextIteratorStreamer(current_tokenizer, skip_prompt=True, skip_special_tokens=False)
|
60 |
+
streamer.stop_signal = current_tokenizer.decode(current_tokenizer.eos_token_id)
|
61 |
+
generate_kwargs["streamer"] = streamer
|
62 |
|
63 |
+
thread = Thread(target=current_model.generate, kwargs=generate_kwargs)
|
64 |
+
thread.start()
|
65 |
|
66 |
# Write the prompt in blue
|
67 |
output = "<span style='color: blue;'>" + prompt + "</span>"
|