manbeast3b commited on
Commit
77985ff
·
verified ·
1 Parent(s): cb993ab

Update src/pipeline.py

Browse files
Files changed (1) hide show
  1. src/pipeline.py +33 -0
src/pipeline.py CHANGED
@@ -14,6 +14,7 @@ import time
14
  from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
  from torchao.quantization import quantize_, int8_weight_only
16
  #from torchao.quantization import autoquant
 
17
  Pipeline = None
18
 
19
  ckpt_id = "black-forest-labs/FLUX.1-schnell"
@@ -28,13 +29,45 @@ def empty_cache():
28
  def load_pipeline() -> Pipeline:
29
  empty_cache()
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  dtype, device = torch.bfloat16, "cuda"
32
 
33
  empty_cache()
34
  pipeline = DiffusionPipeline.from_pretrained(
 
35
  ckpt_id,
36
  torch_dtype=dtype,
37
  )
 
 
 
 
 
 
 
 
38
  pipeline.enable_sequential_cpu_offload()
39
  for _ in range(2):
40
  empty_cache()
 
14
  from diffusers import FluxTransformer2DModel, DiffusionPipeline
15
  from torchao.quantization import quantize_, int8_weight_only
16
  #from torchao.quantization import autoquant
17
+ from model import E, D
18
  Pipeline = None
19
 
20
  ckpt_id = "black-forest-labs/FLUX.1-schnell"
 
29
  def load_pipeline() -> Pipeline:
30
  empty_cache()
31
 
32
+ vae = AutoencoderTiny.from_pretrained("madebyollin/taef1")
33
+ vae.encoder = E(16)
34
+ vae.decoder = D(16)
35
+
36
+ e_p = "encoder.pth"
37
+ d_p = "decoder.pth"
38
+
39
+ if e_p:
40
+ e_sd = torch.load(e_p, map_location="cpu", weights_only=True)
41
+ f_sd = {k.strip("encoder."): v for k, v in e_sd.items() if k.strip("encoder.") in vae.encoder.state_dict() and v.size() == vae.encoder.state_dict()[k.strip("encoder.")].size()}
42
+ print(f" num of keys in filtered: {len(f_sd)} and in decoder: {len(vae.encoder.state_dict())}")
43
+ vae.encoder.load_state_dict(f_sd, strict=False)
44
+
45
+ if d_p:
46
+ d_sd = torch.load(d_p, map_location="cpu", weights_only=True)
47
+ f_sd = {k.strip("decoder."): v for k, v in d_sd.items() if k.strip("decoder.") in vae.decoder.state_dict() and v.size() == vae.decoder.state_dict()[k.strip("decoder.")].size()}
48
+ print(f" num of keys in filtered: {len(f_sd)} and in decoder: {len(vae.decoder.state_dict())}")
49
+ vae.decoder.load_state_dict(f_sd, strict=False)
50
+
51
+ vae.decoder.requires_grad_(False)
52
+ vae.encoder.requires_grad_(False)
53
+ vae.to(dtype=DTYPE)
54
+
55
  dtype, device = torch.bfloat16, "cuda"
56
 
57
  empty_cache()
58
  pipeline = DiffusionPipeline.from_pretrained(
59
+ vae = vae,
60
  ckpt_id,
61
  torch_dtype=dtype,
62
  )
63
+
64
+ pipe.text_encoder.to(memory_format=torch.channels_last)
65
+ pipe.text_encoder_2.to(memory_format=torch.channels_last)
66
+ pipe.transformer.to(memory_format=torch.channels_last)
67
+ pipeline.vae.to(memory_format=torch.channels_last)
68
+ pipeline.vae = torch.compile(pipe.vae, mode="max-autotune")
69
+ pipeline._exclude_from_cpu_offload = ["vae"]
70
+
71
  pipeline.enable_sequential_cpu_offload()
72
  for _ in range(2):
73
  empty_cache()