-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
remove call to F.pad
, improved calculation of memory_count
#10620
base: main
Are you sure you want to change the base?
remove call to F.pad
, improved calculation of memory_count
#10620
Conversation
F.pad
and better memory countF.pad
, improved calculation of memory
F.pad
, improved calculation of memoryF.pad
, improved calculation of memory_count
F.pad
, improved calculation of memory_count
F.pad
, improved calculation of memory_count
Hi @bm-synth. Thanks for your contribution. Can you share some figures on the memory and performance improvements? |
Hi @hlky. Running the following import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from diffusers.models.autoencoders.autoencoder_kl_cogvideox import CogVideoXCausalConv3d
torch.manual_seed(42)
def train(model: nn.Module, video_input: torch.Tensor):
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
model.train()
start_train = time.time()
for iteration in range(100): # Simulate 100 training iterations
optimizer.zero_grad()
output = model(video_input)[0]
loss = F.mse_loss(output, output+iteration) # sum iteration to fake different grads per iteration
loss.backward()
optimizer.step()
torch.cuda.synchronize()
train_time = time.time() - start_train
print("train_time", train_time, "secs")
return output.to("cpu")
def eval(model: nn.Module, video_input: torch.Tensor):
model.eval()
start_train = time.time()
with torch.no_grad():
for _ in range(300): # Simulate 300 inference iterations
model(video_input)
torch.cuda.synchronize()
eval_time = time.time() - start_train
print("eval_time", eval_time, "secs") calling with that input shape $ PYTHONPATH=./diffusers_main/src/ python test_autoencoder.py
input size: 0.498046875 GBs
eval_time 33.06385564804077 secs
train_time 34.33984375 secs
Max memory 22.18018913269043 GBs calling this PR branch gives: $ PYTHONPATH=./diffusers_PR/src/ python test_autoencoder.py
input size: 0.498046875 GBs
eval_time 31.588099241256714 secs
train_time 34.1251916885376 secs
Max memory 22.17398452758789 GBs on the shape $ PYTHONPATH=./diffusers_main/src/ python test_autoencoder.py
input size: 0.43773651123046875 GBs
eval_time 17.759469032287598 secs
train_time 96.50320744514465 secs
Max memory 16.353439331054688 GBs and this PR: $ PYTHONPATH=./diffusers_PR/src/ python test_autoencoder.py
input size: 0.43773651123046875 GBs
eval_time 16.8880774974823 secs
train_time 96.04004764556885 secs
Max memory 16.34803009033203 GBs I'll try to test more dimensions. |
F.pad
, improved calculation of memory_count
F.pad
, improved calculation of memory_count
@bm-synth Great, thanks. Would it also be possible to verify numerical accuracy between the two versions? For a change like this we would expect between 0 to 1e-6 difference. |
@hlky I updated the code above to fix a seed ( if __name__=='__main__':
output_main: torch.Tensor = torch.load("output_main.pt")
output_PR: torch.Tensor = torch.load("output_PR.pt")
print("mean:", output_main.mean().item(), "vs", output_PR.mean().item())
print("std:", output_main.std().item(), "vs", output_PR.std().item())
print("max abs diff:", (output_PR-output_main).diff().abs().max().item())
assert torch.allclose(output_main, output_PR) output:
|
F.pad
when running with non-replicate
pad mode, and instead let padding be done byConv3d
for a more efficient execution;memory_count
doesn't extend dimensions to allowtorch.compile
to do a better optimisation (?) by @ic-synthcc: @jamesbriggs-synth