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

[Don't merge] ex code for transfer layout pass #520

Open
wants to merge 2 commits into
base: develop
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
42 changes: 36 additions & 6 deletions ppdiffusers/deploy/sd15/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ def parse_arguments():
default=False,
help="Whether to tune the shape of tensorrt engine.",
)
parser.add_argument(
"--use_pir",
type=strtobool,
nargs='*',
default=['0', '0', '0', '0'],
)
parser.add_argument(
"--use_new_transpose",
type=strtobool,
nargs='*',
default=['0', '0', '0', '0'],
)


return parser.parse_args()

Expand All @@ -143,19 +156,21 @@ def create_paddle_inference_runtime(
disable_paddle_pass=[],
workspace=24 * 1024 * 1024 * 1024,
tune=False,
use_pir=False,
use_new_transpose=False,
):
config = paddle_infer.Config()
config.enable_memory_optim()
shape_file = f"{model_dir}/{model_name}/shape_range_info.pbtxt"
if tune:
config.collect_shape_range_info(shape_file)
config.switch_ir_optim(False)
else:
if use_pir:
config.enable_new_executor()
if in_pir_executor_mode():
config.enable_new_ir()
if in_cinn_mode():
config.enable_cinn()
config.enable_new_ir()
if use_new_transpose:
config.enable_transfer_layout()


if device_id != -1:
config.use_gpu()
Expand Down Expand Up @@ -232,6 +247,8 @@ def main(args):
disable_paddle_trt_ops=["range", "lookup_table_v2"],
disable_paddle_pass=paddle_delete_passes.get("text_encoder", []),
tune=False,
use_pir=args.use_pir[0],
use_new_transpose=args.use_new_transpose[0],
),
vae_encoder=create_paddle_inference_runtime(
model_dir=args.model_dir,
Expand All @@ -241,6 +258,8 @@ def main(args):
device_id=args.device_id,
disable_paddle_pass=paddle_delete_passes.get("vae_encoder", []),
tune=False,
use_pir=args.use_pir[1],
use_new_transpose=args.use_new_transpose[1],
),
vae_decoder=create_paddle_inference_runtime(
model_dir=args.model_dir,
Expand All @@ -250,6 +269,8 @@ def main(args):
device_id=args.device_id,
disable_paddle_pass=paddle_delete_passes.get("vae_decoder", []),
tune=False,
use_pir=args.use_pir[2],
use_new_transpose=args.use_new_transpose[2],
),
unet=create_paddle_inference_runtime(
model_dir=args.model_dir,
Expand All @@ -259,6 +280,8 @@ def main(args):
device_id=args.device_id,
disable_paddle_pass=no_need_passes,
tune=args.tune,
use_pir=args.use_pir[3],
use_new_transpose=args.use_new_transpose[3],
),
)
pipe = PaddleInferStableDiffusionMegaPipeline.from_pretrained(
Expand All @@ -279,11 +302,13 @@ def main(args):
prompt = "a photo of an astronaut riding a horse on mars"
time_costs = []
# warmup
generator = paddle.Generator().manual_seed(0)
pipe.text2img(
prompt,
num_inference_steps=20,
height=height,
width=width,
generator=generator,
# parse_prompt_type=parse_prompt_type,
)
print("==> Test text2img performance.")
Expand All @@ -296,6 +321,7 @@ def main(args):
num_inference_steps=args.inference_steps,
height=height,
width=width,
generator=generator,
# parse_prompt_type=parse_prompt_type,
).images
latency = time.time() - start
Expand All @@ -305,7 +331,11 @@ def main(args):
f"Mean latency: {np.mean(time_costs):2f} s, p50 latency: {np.percentile(time_costs, 50):2f} s, "
f"p90 latency: {np.percentile(time_costs, 90):2f} s, p95 latency: {np.percentile(time_costs, 95):2f} s."
)
images[0].save(f"{folder}/text2img.png")
images[0].save("{}/text2img_{}_{}.png".format(
folder,
'_'.join(map(str, args.use_pir)),
'_'.join(map(str, args.use_new_transpose))
))

if args.task_name in ["img2img", "all"]:
# img2img
Expand Down
9 changes: 9 additions & 0 deletions ppdiffusers/deploy/sd15/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# GLOG_v=2 python infer.py --model_dir static_model/stable-diffusion-v1-5/ --scheduler "ddim" --backend paddle --device gpu --task_name text2img --use_pir 0 0 0 1 --use_new_transpose 0 0 0 1 > log_transfer_layout_unet 2>&1
# GLOG_v=2 python infer.py --model_dir static_model/stable-diffusion-v1-5/ --scheduler "ddim" --backend paddle --device gpu --task_name text2img --use_pir 0 0 0 1 --use_new_transpose 0 0 0 0 > log_no_transfer_layout_unet 2>&1

# GLOG_v=2 python infer.py --model_dir static_model/stable-diffusion-v1-5/ --scheduler "ddim" --backend paddle --device gpu --task_name text2img --use_pir 0 0 1 0 --use_new_transpose 0 0 1 0 > log_transfer_layout_vae_decoder 2>&1
# GLOG_v=2 python infer.py --model_dir static_model/stable-diffusion-v1-5/ --scheduler "ddim" --backend paddle --device gpu --task_name text2img --use_pir 0 0 1 0 --use_new_transpose 0 0 0 0 > log_no_transfer_layout_vae_decoder 2>&1


CUDNN_LOGDEST_DBG=stdout python infer.py --model_dir static_model/stable-diffusion-v1-5/ --scheduler "ddim" --backend paddle --device gpu --task_name text2img --use_pir 0 0 1 0 --use_new_transpose 0 0 1 0 > log_transfer_layout_vae_decoder_with_cudnn_log 2>&1
CUDNN_LOGDEST_DBG=stdout python infer.py --model_dir static_model/stable-diffusion-v1-5/ --scheduler "ddim" --backend paddle --device gpu --task_name text2img --use_pir 0 0 1 0 --use_new_transpose 0 0 0 0 > log_no_transfer_layout_vae_decoder_with_cudnn_log 2>&1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

from typing import Callable, Dict, List, Optional, Union
import os

import paddle
import PIL
Expand Down Expand Up @@ -336,6 +337,7 @@ def __call__(
paddle.device.synchronize()

if not output_type == "latent":
print('[vae decode start]', flush=True)
image = self._decode_vae_latents(
latents / self.vae_scaling_factor, infer_op=infer_op_dict.get("vae_decoder", None)
)
Expand Down
2 changes: 1 addition & 1 deletion ppdiffusers/ppdiffusers/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"critical": logging.CRITICAL,
}

_default_log_level = logging.WARNING
_default_log_level = logging.DEBUG

_tqdm_active = True

Expand Down
1 change: 0 additions & 1 deletion ppdiffusers/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
paddlenlp>=2.7.2
safetensors>=0.3.1
ftfy
regex
Expand Down