Commit
·
026639f
1
Parent(s):
4cb8587
Upload files
Browse files- handler.py +104 -0
- requirements.txt +7 -0
handler.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
# import transformers
|
| 3 |
+
# from transformers import AutoTokenizer
|
| 4 |
+
# import torch
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# torch.backends.cuda.matmul.allow_tf32 = True
|
| 10 |
+
|
| 11 |
+
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
| 12 |
+
|
| 13 |
+
class EndpointHandler():
|
| 14 |
+
|
| 15 |
+
def __init__(self, path=""):
|
| 16 |
+
|
| 17 |
+
# Use the DPMSolverMultistepScheduler (DPM-Solver++) scheduler here instead
|
| 18 |
+
self.pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
| 19 |
+
self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(self.pipe.scheduler.config)
|
| 20 |
+
self.pipe = self.pipe.to("cuda")
|
| 21 |
+
|
| 22 |
+
# self.pipe.enable_attention_slicing()
|
| 23 |
+
self.pipe.enable_xformers_memory_efficient_attention()
|
| 24 |
+
|
| 25 |
+
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
+
# self.model.eval()
|
| 27 |
+
# self.model.to(device=device, dtype=self.torch_dtype)
|
| 28 |
+
|
| 29 |
+
# self.generate_kwargs = {
|
| 30 |
+
# 'max_new_tokens': 512,
|
| 31 |
+
# 'temperature': 0.0001,
|
| 32 |
+
# 'top_p': 1.0,
|
| 33 |
+
# 'top_k': 0,
|
| 34 |
+
# 'use_cache': True,
|
| 35 |
+
# 'do_sample': True,
|
| 36 |
+
# 'eos_token_id': self.tokenizer.eos_token_id,
|
| 37 |
+
# 'pad_token_id': self.tokenizer.pad_token_id,
|
| 38 |
+
# "repetition_penalty": 1.1
|
| 39 |
+
# }
|
| 40 |
+
|
| 41 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 42 |
+
"""
|
| 43 |
+
data args:
|
| 44 |
+
inputs (:obj: `str` | `PIL.Image` | `np.array`)
|
| 45 |
+
kwargs
|
| 46 |
+
Return:
|
| 47 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
# streamer = TextIteratorStreamer(
|
| 51 |
+
# self.tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True
|
| 52 |
+
# )
|
| 53 |
+
|
| 54 |
+
## Model Parameters
|
| 55 |
+
# self.generate_kwargs['max_new_tokens'] = data['max_new_tokens'] if 'max_new_tokens' in data else self.generate_kwargs['max_new_tokens']
|
| 56 |
+
# self.generate_kwargs['temperature'] = data['temperature'] if 'temperature' in data else self.generate_kwargs['temperature']
|
| 57 |
+
# self.generate_kwargs['top_p'] = data['top_p'] if 'top_p' in data else self.generate_kwargs['top_p']
|
| 58 |
+
# self.generate_kwargs['top_k'] = data['top_k'] if 'top_k' in data else self.generate_kwargs['top_k']
|
| 59 |
+
# self.generate_kwargs['do_sample'] = data['do_sample'] if 'do_sample' in data else self.generate_kwargs['do_sample']
|
| 60 |
+
# self.generate_kwargs['repetition_penalty'] = data['repetition_penalty'] if 'repetition_penalty' in data else self.generate_kwargs['repetition_penalty']
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
## Prepare the inputs
|
| 64 |
+
# inputs = data.pop("inputs",data)
|
| 65 |
+
# input_ids = self.tokenizer(inputs, return_tensors="pt").input_ids
|
| 66 |
+
# input_ids = input_ids.to(self.model.device)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# pip install accelerate
|
| 70 |
+
|
| 71 |
+
batch_size = data.pop("batch_size",data)
|
| 72 |
+
|
| 73 |
+
now = datetime.now()
|
| 74 |
+
|
| 75 |
+
with torch.inference_mode():
|
| 76 |
+
prompt = "a photo of an astronaut riding a horse on mars"
|
| 77 |
+
image = pipe([prompt]*batch_size, num_inference_steps=20)
|
| 78 |
+
|
| 79 |
+
# image.save("astronaut_rides_horse.png")
|
| 80 |
+
|
| 81 |
+
current = datetime.now()
|
| 82 |
+
|
| 83 |
+
# encoded_inp = self.tokenizer(inputs, return_tensors='pt', padding=True)
|
| 84 |
+
# for key, value in encoded_inp.items():
|
| 85 |
+
# encoded_inp[key] = value.to('cuda:0')
|
| 86 |
+
|
| 87 |
+
## Invoke the model
|
| 88 |
+
# with torch.no_grad():
|
| 89 |
+
# gen_tokens = self.model.generate(
|
| 90 |
+
# input_ids=encoded_inp['input_ids'],
|
| 91 |
+
# attention_mask=encoded_inp['attention_mask'],
|
| 92 |
+
# **generate_kwargs,
|
| 93 |
+
# )
|
| 94 |
+
|
| 95 |
+
# ## Decode using tokenizer
|
| 96 |
+
# decoded_gen = self.tokenizer.batch_decode(gen_tokens, skip_special_tokens=True)
|
| 97 |
+
|
| 98 |
+
# with torch.no_grad():
|
| 99 |
+
# output_ids = self.model.generate(input_ids, **self.generate_kwargs)
|
| 100 |
+
# # Slice the output_ids tensor to get only new tokens
|
| 101 |
+
# new_tokens = output_ids[0, len(input_ids[0]) :]
|
| 102 |
+
# output_text = self.tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 103 |
+
|
| 104 |
+
return [{"batch_size":batch_size, "time_elapsed": str(current-now)}]
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
diffusers
|
| 5 |
+
scipy
|
| 6 |
+
safetensors
|
| 7 |
+
xformers
|