Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for using torch.profiler via setup argument #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions fp8/flux_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,15 +615,16 @@ def generate(
)

# prepare inputs
img, img_ids, vec, txt, txt_ids = map(
lambda x: x, # x.contiguous(),
self.prepare(
img=img,
prompt=prompt,
target_device=self.device_flux,
target_dtype=self.dtype,
),
)
with torch.profiler.record_function("prepare"):
img, img_ids, vec, txt, txt_ids = map(
lambda x: x, # x.contiguous(),
self.prepare(
img=img,
prompt=prompt,
target_device=self.device_flux,
target_dtype=self.dtype,
),
)

# dispatch to gpu if offloaded
if self.offload_flow:
Expand All @@ -634,16 +635,17 @@ def generate(
output_imgs = []

for i in range(batch_size):
denoised_img = self.denoise_single_item(
img[i],
img_ids[i],
txt[i],
txt_ids[i],
vec[i],
timesteps,
guidance,
compiling
)
with torch.profiler.record_function("denoise-single-item"):
denoised_img = self.denoise_single_item(
img[i],
img_ids[i],
txt[i],
txt_ids[i],
vec[i],
timesteps,
guidance,
compiling
)
output_imgs.append(denoised_img)
compiling = False

Expand All @@ -655,7 +657,8 @@ def generate(
torch.cuda.empty_cache()

# decode latents to pixel space
img = self.vae_decode(img, height, width)
with torch.profiler.record_function("vae-decode"):
img = self.vae_decode(img, height, width)

return self.as_img_tensor(img)

Expand Down
84 changes: 55 additions & 29 deletions predict.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import os
import time
from typing import Any, Tuple, Optional
Expand Down Expand Up @@ -136,8 +137,10 @@ def base_setup(
compile_fp8: bool = False,
compile_bf16: bool = False,
disable_fp8: bool = False,
enable_profiling: bool = False,
) -> None:
self.flow_model_name = flow_model_name
self.enable_profiling = enable_profiling
print(f"Booting model {self.flow_model_name}")

gpu_name = (
Expand Down Expand Up @@ -477,6 +480,7 @@ def postprocess(
output_format: str,
output_quality: int,
np_images: Optional[List[Image]] = None,
profile: Optional[Path] = None,
) -> List[Path]:
has_nsfw_content = [False] * len(images)

Expand Down Expand Up @@ -513,6 +517,8 @@ def postprocess(
)

print(f"Total safe images: {len(output_paths)} out of {len(images)}")
if profile:
output_paths.append(profile)
return output_paths

def run_safety_checker(self, images, np_images):
Expand Down Expand Up @@ -547,32 +553,48 @@ def shared_predict(
seed: int = None,
width: int = 1024,
height: int = 1024,
):
if go_fast and not self.disable_fp8:
return self.fp8_predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
image=image,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
) -> Tuple[List[Image.Image], Optional[List[np.ndarray]], Optional[Path]]:
if self.enable_profiling:
profiler = torch.profiler.profile(
activities=[
torch.profiler.ProfilerActivity.CPU,
torch.profiler.ProfilerActivity.CUDA,
]
)
if self.disable_fp8:
print("running bf16 model, fp8 disabled")
return self.base_predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
image=image,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
)
else:
profiler = contextlib.nullcontext()

with profiler:
if go_fast and not self.disable_fp8:
imgs, np_imgs = self.fp8_predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
image=image,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
)
else:
if self.disable_fp8:
print("running bf16 model, fp8 disabled")
imgs, np_imgs = self.base_predict(
prompt=prompt,
num_outputs=num_outputs,
num_inference_steps=num_inference_steps,
guidance=guidance,
image=image,
prompt_strength=prompt_strength,
seed=seed,
width=width,
height=height,
)
if isinstance(profiler, torch.profiler.profile):
profiler.export_chrome_trace("chrome-trace.json")
return imgs, np_imgs, Path("chrome-trace.json")
return imgs, np_imgs, None


class SchnellPredictor(Predictor):
Expand All @@ -598,7 +620,7 @@ def predict(
megapixels: str = SHARED_INPUTS.megapixels,
) -> List[Path]:
width, height = self.preprocess(aspect_ratio, megapixels)
imgs, np_imgs = self.shared_predict(
imgs, np_imgs, profile = self.shared_predict(
go_fast,
prompt,
num_outputs,
Expand All @@ -614,6 +636,7 @@ def predict(
output_format,
output_quality,
np_images=np_imgs,
profile=profile,
)


Expand Down Expand Up @@ -656,7 +679,7 @@ def predict(
print("img2img not supported with fp8 quantization; running with bf16")
go_fast = False
width, height = self.preprocess(aspect_ratio, megapixels)
imgs, np_imgs = self.shared_predict(
imgs, np_imgs, profile = self.shared_predict(
go_fast,
prompt,
num_outputs,
Expand All @@ -675,6 +698,7 @@ def predict(
output_format,
output_quality,
np_images=np_imgs,
profile=profile,
)


Expand Down Expand Up @@ -706,7 +730,7 @@ def predict(
self.handle_loras(go_fast, lora_weights, lora_scale)

width, height = self.preprocess(aspect_ratio, megapixels)
imgs, np_imgs = self.shared_predict(
imgs, np_imgs, profile = self.shared_predict(
go_fast,
prompt,
num_outputs,
Expand All @@ -722,6 +746,7 @@ def predict(
output_format,
output_quality,
np_images=np_imgs,
profile=profile,
)


Expand Down Expand Up @@ -770,7 +795,7 @@ def predict(
self.handle_loras(go_fast, lora_weights, lora_scale)

width, height = self.preprocess(aspect_ratio, megapixels)
imgs, np_imgs = self.shared_predict(
imgs, np_imgs, profile = self.shared_predict(
go_fast,
prompt,
num_outputs,
Expand All @@ -789,6 +814,7 @@ def predict(
output_format,
output_quality,
np_images=np_imgs,
profile=profile,
)


Expand Down
Loading