manbeast3b commited on
Commit
fbd72be
·
verified ·
1 Parent(s): 6005ab8

Update src/pipeline.py

Browse files
Files changed (1) hide show
  1. src/pipeline.py +48 -21
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
  import torch.nn.functional as F
@@ -16,36 +16,59 @@ import torch.nn as nn
16
  # from torchao.quantization import quantize_, float8_dynamic_activation_float8_weight #PerRow,
17
  import os
18
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:False,garbage_collection_threshold:0.01"
 
19
  Pipeline = None
20
 
21
- def w8_a16_forward(weight, input, scales, bias=None):
22
- casted_weights = weight.to(input.dtype)
23
- output = F.linear(input, casted_weights) * scales # overhead
24
- if bias is not None:
25
- output = output + bias
26
- return output
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  class W8A16LinearLayer(nn.Module):
29
  def __init__(self, in_features, out_features, bias=True, dtype=torch.float32):
30
  super().__init__()
31
- self.register_buffer(
32
- "int8_weights",
33
- torch.randint(-128, 127, (out_features, in_features), dtype=torch.int8))
34
- self.register_buffer("scales", torch.randn((out_features), dtype=dtype))
35
  if bias:
36
- self.register_buffer("bias", torch.randn((1, out_features), dtype=dtype))
37
-
 
38
  def quantize(self, weights):
39
  w_fp32 = weights.clone().to(torch.float32)
40
  scales = w_fp32.abs().max(dim=-1).values / 127
41
  scales = scales.to(weights.dtype)
42
- int8_weights = torch.round(weights/scales.unsqueeze(1)).to(torch.int8)
43
- self.int8_weights = int8_weights
44
- self.scales = scales
45
- self.bias = None
46
 
47
  def forward(self, input):
48
- return w8_a16_forward(self.int8_weights, input, self.scales, self.bias)
 
 
 
 
49
 
50
  def replace_linear_with_target_and_quantize(module, target_class, module_name_to_exclude):
51
  # with open("/root/.cache/huggingface/hub/output_layers.txt", "a") as f:
@@ -54,8 +77,9 @@ def replace_linear_with_target_and_quantize(module, target_class, module_name_to
54
  old_bias = child.bias
55
  old_weight = child.weight
56
  new_module = target_class(child.in_features, child.out_features, old_bias is not None, child.weight.dtype)
 
 
57
  setattr(module, name, new_module)
58
- getattr(module, name).quantize(old_weight)
59
  if old_bias is not None:
60
  getattr(module, name).bias = old_bias
61
 
@@ -84,10 +108,12 @@ def load_pipeline() -> Pipeline:
84
  "city96/t5-v1_1-xxl-encoder-bf16", torch_dtype=torch.bfloat16
85
  )
86
  vae=AutoencoderKL.from_pretrained(ckpt_id, subfolder="vae", torch_dtype=dtype)
 
87
  pipeline = DiffusionPipeline.from_pretrained(
88
  ckpt_id,
89
  vae=vae,
90
  text_encoder_2 = text_encoder_2,
 
91
  torch_dtype=dtype,
92
  )
93
  # quantize_(pipeline.transformer, float8_dynamic_activation_float8_weight())
@@ -98,8 +124,9 @@ def load_pipeline() -> Pipeline:
98
  pipeline.text_encoder.to(memory_format=torch.channels_last)
99
  pipeline.transformer.to(memory_format=torch.channels_last)
100
  replace_linear_with_target_and_quantize(pipeline.transformer, W8A16LinearLayer, [])
101
- pipeline.transformer.save_pretrained("/root/.cache/huggingface/hub/transformer-flux")
102
- exit()
 
103
 
104
  pipeline.vae.to(memory_format=torch.channels_last)
105
  pipeline.vae = torch.compile(pipeline.vae)
 
1
+ from diffusers import FluxPipeline, AutoencoderKL, FluxTransformer2DModel #AutoencoderTiny
2
  from diffusers.image_processor import VaeImageProcessor
3
  from diffusers.schedulers import FlowMatchEulerDiscreteScheduler
4
  import torch.nn.functional as F
 
16
  # from torchao.quantization import quantize_, float8_dynamic_activation_float8_weight #PerRow,
17
  import os
18
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:False,garbage_collection_threshold:0.01"
19
+ os.environ["HUGGINGFACE_HUB_TOKEN"] = ""
20
  Pipeline = None
21
 
22
+ # def w8_a16_forward(weight, input, scales, bias=None):
23
+ # casted_weights = weight.to(input.dtype)
24
+ # output = F.linear(input, casted_weights) * scales # overhead
25
+ # if bias is not None:
26
+ # output = output + bias
27
+ # return output
28
 
29
+ # class W8A16LinearLayer(nn.Module):
30
+ # def __init__(self, in_features, out_features, bias=True, dtype=torch.float32):
31
+ # super().__init__()
32
+ # self.register_buffer(
33
+ # "int8_weights",
34
+ # torch.randint(-128, 127, (out_features, in_features), dtype=torch.int8))
35
+ # self.register_buffer("scales", torch.randn((out_features), dtype=dtype))
36
+ # if bias:
37
+ # self.register_buffer("bias", torch.randn((1, out_features), dtype=dtype))
38
+
39
+ # def quantize(self, weights):
40
+ # w_fp32 = weights.clone().to(torch.float32)
41
+ # scales = w_fp32.abs().max(dim=-1).values / 127
42
+ # scales = scales.to(weights.dtype)
43
+ # int8_weights = torch.round(weights/scales.unsqueeze(1)).to(torch.int8)
44
+ # self.int8_weights = int8_weights
45
+ # self.scales = scales
46
+ # self.bias = None
47
+
48
+ # def forward(self, input):
49
+ # return w8_a16_forward(self.int8_weights, input, self.scales, self.bias)
50
+
51
  class W8A16LinearLayer(nn.Module):
52
  def __init__(self, in_features, out_features, bias=True, dtype=torch.float32):
53
  super().__init__()
54
+ self.weight = nn.Parameter(torch.randn(out_features, in_features, dtype=dtype))
 
 
 
55
  if bias:
56
+ self.bias = nn.Parameter(torch.randn(1, out_features, dtype=dtype))
57
+ self.scales = nn.Parameter(torch.randn(out_features, dtype=dtype))
58
+
59
  def quantize(self, weights):
60
  w_fp32 = weights.clone().to(torch.float32)
61
  scales = w_fp32.abs().max(dim=-1).values / 127
62
  scales = scales.to(weights.dtype)
63
+ self.weight.data = torch.round(weights/scales.unsqueeze(1)).to(torch.int8)
64
+ self.scales.data = scales
 
 
65
 
66
  def forward(self, input):
67
+ casted_weights = self.weight.to(input.dtype)
68
+ output = F.linear(input, casted_weights) * self.scales
69
+ if self.bias is not None:
70
+ output = output + self.bias
71
+ return output
72
 
73
  def replace_linear_with_target_and_quantize(module, target_class, module_name_to_exclude):
74
  # with open("/root/.cache/huggingface/hub/output_layers.txt", "a") as f:
 
77
  old_bias = child.bias
78
  old_weight = child.weight
79
  new_module = target_class(child.in_features, child.out_features, old_bias is not None, child.weight.dtype)
80
+ new_module.quantize(old_weight)
81
+ delattr(module, name)
82
  setattr(module, name, new_module)
 
83
  if old_bias is not None:
84
  getattr(module, name).bias = old_bias
85
 
 
108
  "city96/t5-v1_1-xxl-encoder-bf16", torch_dtype=torch.bfloat16
109
  )
110
  vae=AutoencoderKL.from_pretrained(ckpt_id, subfolder="vae", torch_dtype=dtype)
111
+ # transformer = FluxTransformer2DModel.from_pretrined("manbeast3b/transfomer-flux-schnell-int8") # torch_dtype=dtype
112
  pipeline = DiffusionPipeline.from_pretrained(
113
  ckpt_id,
114
  vae=vae,
115
  text_encoder_2 = text_encoder_2,
116
+ transformer=transformer,
117
  torch_dtype=dtype,
118
  )
119
  # quantize_(pipeline.transformer, float8_dynamic_activation_float8_weight())
 
124
  pipeline.text_encoder.to(memory_format=torch.channels_last)
125
  pipeline.transformer.to(memory_format=torch.channels_last)
126
  replace_linear_with_target_and_quantize(pipeline.transformer, W8A16LinearLayer, [])
127
+ # pipeline.transformer.save_pretrained("manbeast3b/transfomer-flux-schnell-int8-new", push_to_hub=True, token="")
128
+ # pipeline.transformer.save_pretrained("/root/.cache/huggingface/hub/transformer-flux")
129
+ # exit()
130
 
131
  pipeline.vae.to(memory_format=torch.channels_last)
132
  pipeline.vae = torch.compile(pipeline.vae)