manbeast3b commited on
Commit
4350fd2
·
verified ·
1 Parent(s): 9882584

Update src/pipeline.py

Browse files
Files changed (1) hide show
  1. src/pipeline.py +22 -9
src/pipeline.py CHANGED
@@ -9,7 +9,7 @@ from torch import Generator
9
  from diffusers import FluxPipeline
10
  from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
11
  import torch.nn as nn
12
- from model import Model, Decoder
13
  import torchvision
14
 
15
  Pipeline = None
@@ -28,15 +28,26 @@ def load_pipeline() -> Pipeline:
28
  # vae.to(dtype=DTYPE)
29
 
30
  vae = AutoencoderTiny.from_pretrained("madebyollin/taef1")
 
31
  vae.decoder = Decoder(16)
 
 
32
  decoder_path = "decoder.pth"
33
- decoder_state_dict = torch.load(decoder_path, weights_only=True) #map_location="cpu",
34
- filtered_state_dict = {k: v for k, v in decoder_state_dict.items() if k in vae.decoder.state_dict() and v.size() == vae.decoder.state_dict()[k].size()}
35
- print(decoder_state_dict.keys())
36
- print(filtered_state_dict.keys())
37
- print(f"num of keys in pth: {len(decoder_state_dict)} and decoder: {len(vae.decoder.state_dict())}")
38
- vae.decoder.load_state_dict(filtered_state_dict, strict=False)
 
 
 
 
 
 
 
39
  vae.decoder.requires_grad_(False)
 
40
  vae.to(dtype=DTYPE)
41
 
42
  pipeline = FluxPipeline.from_pretrained(MODEL_ID,vae=vae,
@@ -68,11 +79,13 @@ def infer(request: TextToImageRequest, pipeline: Pipeline) -> Image:
68
  generator = Generator("cuda").manual_seed(request.seed)
69
  image = None
70
  # with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False):
71
- image=pipeline(request.prompt,generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pt").images[0]
 
72
  # image = image / 255.
73
  # image = image.mul_(2).sub_(1)
74
  # image = ((image + 1) / 2) * 255
75
  # image = image.clamp(0, 255)
76
  # image = image.to(torch.float32)
77
  # return torchvision.transforms.functional.to_pil_image(image)
78
- return torchvision.transforms.functional.to_pil_image(image.to(torch.float32).mul_(2).sub_(1))
 
 
9
  from diffusers import FluxPipeline
10
  from torchao.quantization import quantize_, int8_weight_only, fpx_weight_only
11
  import torch.nn as nn
12
+ from model import Model, Decoder, Encoder
13
  import torchvision
14
 
15
  Pipeline = None
 
28
  # vae.to(dtype=DTYPE)
29
 
30
  vae = AutoencoderTiny.from_pretrained("madebyollin/taef1")
31
+ vae.encoder = Encoder(16)
32
  vae.decoder = Decoder(16)
33
+
34
+ encoder_path = "encoder.pth"
35
  decoder_path = "decoder.pth"
36
+
37
+ if encoder_path is not None:
38
+ encoder_state_dict = torch.load(encoder_path, map_location="cpu", weights_only=True)
39
+ filtered_state_dict = {k.strip('encoder.'): v for k, v in encoder_state_dict.items() if k.strip('encoder.') in vae.encoder.state_dict() and v.size() == vae.encoder.state_dict()[k.strip('encoder.')].size()}
40
+ print(f" num of keys in filtered: {len(filtered_state_dict)} and in decoder: {len(vae.encoder.state_dict())}")
41
+ vae.encoder.load_state_dict(filtered_state_dict, strict=False)
42
+
43
+ if decoder_path is not None:
44
+ decoder_state_dict = torch.load(decoder_path, map_location="cpu", weights_only=True)
45
+ filtered_state_dict = {k.strip('decoder.'): v for k, v in decoder_state_dict.items() if k.strip('decoder.') in vae.decoder.state_dict() and v.size() == vae.decoder.state_dict()[k.strip('decoder.')].size()}
46
+ print(f" num of keys in filtered: {len(filtered_state_dict)} and in decoder: {len(vae.decoder.state_dict())}")
47
+ vae.decoder.load_state_dict(filtered_state_dict, strict=False)
48
+
49
  vae.decoder.requires_grad_(False)
50
+ vae.encoder.requires_grad_(False)
51
  vae.to(dtype=DTYPE)
52
 
53
  pipeline = FluxPipeline.from_pretrained(MODEL_ID,vae=vae,
 
79
  generator = Generator("cuda").manual_seed(request.seed)
80
  image = None
81
  # with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=False, enable_mem_efficient=False):
82
+ # image=pipeline(request.prompt,generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pt").images[0]
83
+ image=pipeline(request.prompt,generator=generator, guidance_scale=0.0, num_inference_steps=4, max_sequence_length=256, height=request.height, width=request.width, output_type="pil").images[0]
84
  # image = image / 255.
85
  # image = image.mul_(2).sub_(1)
86
  # image = ((image + 1) / 2) * 255
87
  # image = image.clamp(0, 255)
88
  # image = image.to(torch.float32)
89
  # return torchvision.transforms.functional.to_pil_image(image)
90
+ # return torchvision.transforms.functional.to_pil_image(image.to(torch.float32).mul_(2).sub_(1))
91
+ return image