manbeast3b commited on
Commit
2f5adf0
·
verified ·
1 Parent(s): 4f22740

Update src/pipeline.py

Browse files
Files changed (1) hide show
  1. src/pipeline.py +52 -3
src/pipeline.py CHANGED
@@ -1,4 +1,4 @@
1
- from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
2
  from diffusers.image_processor import VaeImageProcessor
3
  from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
4
 
@@ -12,11 +12,58 @@ from pipelines.models import TextToImageRequest
12
  from torch import Generator
13
  import time
14
  from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
- from torchao.quantization import quantize_, float8_dynamic_activation_float8_weight #PerRow,
16
  import os
17
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:False,garbage_collection_threshold:0.01"
18
  Pipeline = None
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ckpt_id = "black-forest-labs/FLUX.1-schnell"
21
  def empty_cache():
22
  start = time.time()
@@ -39,12 +86,14 @@ def load_pipeline() -> Pipeline:
39
  text_encoder_2 = text_encoder_2,
40
  torch_dtype=dtype,
41
  )
42
- quantize_(pipeline.transformer, float8_dynamic_activation_float8_weight())
 
43
  torch.backends.cudnn.benchmark = True
44
  torch.backends.cuda.matmul.allow_tf32 = True
45
  torch.cuda.set_per_process_memory_fraction(0.99)
46
  pipeline.text_encoder.to(memory_format=torch.channels_last)
47
  pipeline.transformer.to(memory_format=torch.channels_last)
 
48
 
49
 
50
  pipeline.vae.to(memory_format=torch.channels_last)
 
1
+ # from diffusers import FluxPipeline, AutoencoderKL, AutoencoderTiny
2
  from diffusers.image_processor import VaeImageProcessor
3
  from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
4
 
 
12
  from torch import Generator
13
  import time
14
  from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
+ # from torchao.quantization import quantize_, float8_dynamic_activation_float8_weight #PerRow,
16
  import os
17
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:False,garbage_collection_threshold:0.01"
18
  Pipeline = None
19
 
20
+ class W8A16LinearLayer(nn.Module):
21
+ def __init__(self, in_features, out_features, bias=True, dtype=torch.float32):
22
+ super().__init__()
23
+
24
+ self.register_buffer(
25
+ "int8_weights",
26
+ torch.randint(-128, 127, (out_features, in_features), dtype=torch.int8))
27
+
28
+ self.register_buffer("scales", torch.randn((out_features), dtype=dtype))
29
+
30
+ if bias:
31
+ self.register_buffer("bias", torch.randn((1, out_features), dtype=dtype))
32
+ else:
33
+
34
+ def quantize(self, weights):
35
+ w_fp32 = weights.clone().to(torch.float32)
36
+
37
+ scales = w_fp32.abs().max(dim=-1).values / 127
38
+ scales = scales.to(weights.dtype)
39
+
40
+ int8_weights = torch.round(weights/scales.unsqueeze(1)).to(torch.int8)
41
+
42
+ self.int8_weights = int8_weights
43
+ self.scales = scales self.bias = None
44
+
45
+ def forward(self, input):
46
+ return w8_a16_forward(self.int8_weights, input, self.scales, self.bias)
47
+
48
+ def replace_linear_with_target_and_quantize(module, target_class, module_name_to_exclude):
49
+ for name, child in module.named_children():
50
+ if isinstance(child, nn.Linear) and not \
51
+ any([x == name for x in module_name_to_exclude]):
52
+ old_bias = child.bias
53
+ old_weight = child.weight
54
+
55
+ new_module = target_class(child.in_features, child.out_features, old_bias is not None, child.weight.dtype)
56
+ setattr(module, name, new_module)
57
+
58
+ getattr(module, name).quantize(old_weight)
59
+
60
+ if old_bias is not None:
61
+ getattr(module, name).bias = old_bias
62
+ else:
63
+ # Recursively call the function for nested modules
64
+ replace_linear_with_target_and_quantize(child, target_class, module_name_to_exclude)
65
+
66
+
67
  ckpt_id = "black-forest-labs/FLUX.1-schnell"
68
  def empty_cache():
69
  start = time.time()
 
86
  text_encoder_2 = text_encoder_2,
87
  torch_dtype=dtype,
88
  )
89
+ # quantize_(pipeline.transformer, float8_dynamic_activation_float8_weight())
90
+
91
  torch.backends.cudnn.benchmark = True
92
  torch.backends.cuda.matmul.allow_tf32 = True
93
  torch.cuda.set_per_process_memory_fraction(0.99)
94
  pipeline.text_encoder.to(memory_format=torch.channels_last)
95
  pipeline.transformer.to(memory_format=torch.channels_last)
96
+ replace_linear_with_target_and_quantize(pipeline.transformer, W8A16LinearLayer, [])
97
 
98
 
99
  pipeline.vae.to(memory_format=torch.channels_last)