BucketOfFish commited on
Commit
dc6124b
1 Parent(s): 89d375e

Streaming inference script

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. streaming_inference.py +89 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
streaming_inference.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from threading import Thread
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
+ import torch
5
+
6
+ from .configuration_phi import PhiConfig
7
+ from .modeling_phi import PhiForCausalLM
8
+
9
+
10
+ # This works, but is not streaming
11
+ """
12
+ if __name__ == "__main__":
13
+ device = "cuda"
14
+
15
+ model_config = PhiConfig(**json.load(open("simplified_phi2/config.json")))
16
+ model = PhiForCausalLM(model_config).to(device)
17
+ phi_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", trust_remote_code=True)
18
+ model.load_state_dict(phi_model.state_dict())
19
+
20
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
21
+
22
+ text = "Write an essay on sea monkeys: "
23
+ tokens = tokenizer(text, return_tensors="pt", return_attention_mask=False).to(device)
24
+ outputs = model.generate(**tokens, max_length=200)
25
+ text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
26
+ print(text)
27
+ """
28
+
29
+
30
+ # This is streaming, but does not work because you can't set trust_remote_code=True
31
+ """
32
+ if __name__ == "__main__":
33
+ client = InferenceClient(model="microsoft/phi-2")
34
+ text = "How do you make cheese?"
35
+ for token in client.text_generation(text, max_new_tokens=500, stream=True):
36
+ print(token, end="")
37
+ """
38
+
39
+
40
+ # This is trying the TextIteratorStreamer class
41
+ if __name__ == "__main__":
42
+ # make and load tokenizer, use tokenizer to initialize token_streamer
43
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
44
+ token_streamer = TextIteratorStreamer(tokenizer)
45
+
46
+ # make model and run model.generate(streamer=TextIteratorStreamer) on a thread
47
+ device = "cuda"
48
+ model_config = PhiConfig(**json.load(open("simplified_phi2/config.json")))
49
+ model = PhiForCausalLM(model_config).to(device)
50
+ phi_model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", trust_remote_code=True)
51
+ model.load_state_dict(phi_model.state_dict())
52
+ thread = Thread(
53
+ target=model.generate,
54
+ kwargs=dict(
55
+ tokenizer( # returns a torch dictionary
56
+ "Here is an essay on sea monkeys: ",
57
+ return_tensors="pt",
58
+ return_attention_mask=False,
59
+ ).to(device),
60
+ streamer=token_streamer,
61
+ max_new_tokens=500,
62
+ eos_token_id=tokenizer.eos_token_id,
63
+ ),
64
+ )
65
+ thread.start()
66
+
67
+ # generate
68
+ my_output = ""
69
+ for new_token in token_streamer:
70
+ my_output += new_token
71
+ print(new_token, end="", flush=True)
72
+ print()
73
+
74
+ # check output
75
+ expected_output = """Here is an essay on sea monkeys:
76
+
77
+ Sea monkeys are a type of brine shrimp that are often sold as pets in kits. They are easy to care for and can be a fun addition to any aquarium. However, it is important to understand the proper care and feeding of sea monkeys to ensure their health and longevity.
78
+
79
+ Sea monkeys are sensitive to changes in temperature, pH, and salinity. It is important to keep their environment at a consistent temperature of around 75-80 degrees Fahrenheit. The pH level should be between 7.5 and 8.5, and the salinity should be around 1.023. These conditions can be achieved by using a saltwater mix and a thermometer.
80
+
81
+ Feeding sea monkeys is also important. They can be fed a variety of foods, including flakes, pellets, and freeze-dried bloodworms. It is important to feed them regularly, but not too much. Overfeeding can lead to health problems and a shorter lifespan.
82
+
83
+ In addition to proper care and feeding, it is important to understand the different types of sea monkeys. There are several different species, including the brine shrimp, the fairy shrimp, and the tadpole shrimp. Each species has its own unique characteristics and requirements.
84
+
85
+ Sea monkeys are also known for their ability to reproduce quickly. They can lay hundreds of eggs at a time, which can lead to a large population in a short amount of time. However, it is important to keep their environment clean and free of debris to prevent overcrowding and disease.
86
+
87
+ In conclusion, sea monkeys are a fascinating and easy-to-care-for pet. However, it is important to understand their unique requirements and to provide them with proper care and feeding. By doing so, you can ensure their health and longevity and enjoy watching them grow and thrive in their aquarium.<|endoftext|>"""
88
+ assert my_output == expected_output
89
+ print("Test passed")