Describe the bug
regression PR: #11098 .
The attn_processor will turn the hidden_states into channel last (NHWC) layout, which will have great benefit on CPU when running matmul.
But the op: .contiguous() will turn the tensor into NCHW layout as the tensor orginal layout, it will make the matmul slower than the NHWC layout (converted by attn processor). Besides, the op .contiguous() also costs too much time on CPU if the tensor layout is bad (just like in this case).
Reproduction
numactl -C 0-31 --membind 0 python test.py
fromdiffusersimportAnimateDiffPipeline, MotionAdapter, EulerDiscreteSchedulerfromsafetensors.torchimportload_filefromhuggingface_hubimporthf_hub_downloadfromtransformersimportset_seedimporttorchimporttimeSEED=42device="cpu"model_dtype=torch.float16WARM_UP=4RUN=4set_seed(SEED)
print("\nLoading AnimateDiff-Lightning model...")
model_id="ByteDance/AnimateDiff-Lightning"step=4ckpt=f"animatediff_lightning_{step}step_diffusers.safetensors"base="emilianJR/epiCRealism"adapter=MotionAdapter().to(device, model_dtype)
adapter.load_state_dict(load_file(hf_hub_download(model_id, ckpt), device=device))
pipe=AnimateDiffPipeline.from_pretrained(base, motion_adapter=adapter, torch_dtype=model_dtype).to(device)
pipe.scheduler=EulerDiscreteScheduler.from_config(
pipe.scheduler.config, timestep_spacing="trailing", beta_schedule="linear"
)
defrun_inference():
set_seed(SEED)
withtorch.no_grad():
output=pipe(
prompt="An astronaut riding a green horse",
guidance_scale=1.0,
num_inference_steps=4,
).frames[0]
returnoutput# Warm upprint(f"\nWarming up ({WARM_UP} iterations)...")
foriinrange(WARM_UP):
run_inference()
print(f" Warm-up {i+1}/{WARM_UP} done")
# Benchmarkprint(f"\nRunning benchmark ({RUN} iterations)...")
elapsed_times= []
foriinrange(RUN):
start=time.perf_counter()
output=run_inference()
end=time.perf_counter()
elapsed= (end-start) *1000# mselapsed_times.append(elapsed)
print(f" Run {i+1}/{RUN}: {elapsed:.2f} ms")
# Statisticsavg_time=sum(elapsed_times) /len(elapsed_times)
min_time=min(elapsed_times)
max_time=max(elapsed_times)
print(f"\n{'='*50}")
print(f"Results ({RUN} runs):")
print(f" Average: {avg_time:.2f} ms")
print(f" Min: {min_time:.2f} ms")
print(f" Max: {max_time:.2f} ms")
print(f"{'='*50}")The pipeline latency has 50% performance regression after the regression PR.
Since the PR is targeted to fix the DDP issue, I think we can check if DDP before using .contiguous(). WDYT? @sayakpaul
Hi @jinc7461 . Could you please provide the script to reproduce the error, and give me some advice to check before using .contiguous() ? Thanks!
cc @sywangyi
Logs
System Info
torch 2.11.0.dev20260113+cpu
platform: Intel Xeon 6
Who can help?
No response
Describe the bug
regression PR: #11098 .
The attn_processor will turn the hidden_states into channel last (NHWC) layout, which will have great benefit on CPU when running matmul.
But the op:
.contiguous()will turn the tensor into NCHW layout as the tensor orginal layout, it will make the matmul slower than the NHWC layout (converted by attn processor). Besides, the op.contiguous()also costs too much time on CPU if the tensor layout is bad (just like in this case).Reproduction
numactl -C 0-31 --membind 0 python test.py
The pipeline latency has 50% performance regression after the regression PR.
Since the PR is targeted to fix the DDP issue, I think we can check
if DDPbefore using.contiguous(). WDYT? @sayakpaulHi @jinc7461 . Could you please provide the script to reproduce the error, and give me some advice to check before using
.contiguous()? Thanks!cc @sywangyi
Logs
System Info
torch 2.11.0.dev20260113+cpu
platform: Intel Xeon 6
Who can help?
No response