Update src/pipeline.py
Browse files- src/pipeline.py +13 -8
src/pipeline.py
CHANGED
|
@@ -26,11 +26,15 @@ torch.backends.cudnn.benchmark = True
|
|
| 26 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 27 |
torch.cuda.set_per_process_memory_fraction(0.95)
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
class BasicQuantization:
|
| 36 |
def __init__(self, bits=1):
|
|
@@ -105,8 +109,11 @@ def load_pipeline() -> Pipeline:
|
|
| 105 |
|
| 106 |
############ Text Encoder ############
|
| 107 |
text_encoder = CLIPTextModel.from_pretrained(
|
| 108 |
-
ckpt_id, subfolder="text_encoder", torch_dtype=torch.bfloat16
|
| 109 |
)
|
|
|
|
|
|
|
|
|
|
| 110 |
############ Text Encoder 2 ############
|
| 111 |
text_encoder_2 = T5EncoderModel.from_pretrained(
|
| 112 |
"city96/t5-v1_1-xxl-encoder-bf16", torch_dtype=torch.bfloat16
|
|
@@ -149,8 +156,6 @@ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
|
|
| 149 |
max_sequence_length=256,
|
| 150 |
height=request.height,
|
| 151 |
width=request.width,
|
| 152 |
-
# callback_on_step_end=callback_dynamic_cfg,
|
| 153 |
-
# callback_on_step_end_tensor_inputs=['prompt_embeds'],
|
| 154 |
output_type="pt").images[0]
|
| 155 |
|
| 156 |
return torchvision.transforms.functional.to_pil_image(image.to(torch.float32).mul_(2).sub_(1))
|
|
|
|
| 26 |
torch.backends.cuda.matmul.allow_tf32 = True
|
| 27 |
torch.cuda.set_per_process_memory_fraction(0.95)
|
| 28 |
|
| 29 |
+
def dynamic_range_quantization(self, bit_width=8):
|
| 30 |
+
"""Dynamic range quantization with adaptive scaling"""
|
| 31 |
+
|
| 32 |
+
quantized_model = torch.quantization.quantize_dynamic(
|
| 33 |
+
self.model,
|
| 34 |
+
{nn.Linear, nn.Conv2d},
|
| 35 |
+
dtype=torch.qint8 if bit_width == 8 else torch.quint8 # Dynamic dtype based on bit_width
|
| 36 |
+
)
|
| 37 |
+
return quantized_model
|
| 38 |
|
| 39 |
class BasicQuantization:
|
| 40 |
def __init__(self, bits=1):
|
|
|
|
| 109 |
|
| 110 |
############ Text Encoder ############
|
| 111 |
text_encoder = CLIPTextModel.from_pretrained(
|
| 112 |
+
ckpt_id, subfolder="text_encoder", #torch_dtype=torch.bfloat16
|
| 113 |
)
|
| 114 |
+
text_encoder = dynamic_range_quantization(text_encodertext_encoder)
|
| 115 |
+
text_encoder.to(torch.bfloat16)
|
| 116 |
+
|
| 117 |
############ Text Encoder 2 ############
|
| 118 |
text_encoder_2 = T5EncoderModel.from_pretrained(
|
| 119 |
"city96/t5-v1_1-xxl-encoder-bf16", torch_dtype=torch.bfloat16
|
|
|
|
| 156 |
max_sequence_length=256,
|
| 157 |
height=request.height,
|
| 158 |
width=request.width,
|
|
|
|
|
|
|
| 159 |
output_type="pt").images[0]
|
| 160 |
|
| 161 |
return torchvision.transforms.functional.to_pil_image(image.to(torch.float32).mul_(2).sub_(1))
|