Take this small speed-up optimizations for main code
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 155
Description
### Feature Idea
**Slightly faster code for 2 functions.**
In samplers.py:
### Building in correct order + list comprehension
def ddim_scheduler(model_sampling, steps):
s = model_sampling
small_flag = math.isclose(float(s.sigmas[1]), 0, abs_tol=0.00001)
if small_flag:
steps += 1
sigma_len = len(s.sigmas)
ss = max(sigma_len // steps, 1)
# 1. Mathematically calculate the last valid index from the original forward loop.
# This finds the largest index that would have been picked.
num_strides = (sigma_len - 2) // ss
start_index = 1 + num_strides * ss
# 2. Use a single list comprehension with a reversed range.
# We start at our calculated start_index, go down towards 0 (exclusive),
# and step by the negative stride '-ss'.
sigs = [float(s.sigmas[i]) for i in range(start_index, 0, -ss)]
if small_flag:
return torch.FloatTensor(sigs)
else:
return torch.FloatTensor(sigs+[0.0])
###
In sample.py:
### Minimal optimization, not hoping that something will detect recalculation of the same value inside the loop:
def prepare_noise(latent_image, seed, noise_inds=None):
"""
creates random noise given a latent image and a seed.
optional arg skip can be used to skip and discard x number of noise generations for a given seed
"""
generator = torch.manual_seed(seed)
if noise_inds is None:
return torch.randn(latent_image.size(), dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu")
unique_inds, inverse = np.unique(noise_inds, return_inverse=True)
noises = []
aux_lst = [1] + list(latent_image.size())[1:]
for i in range(unique_inds[-1]+1):
noise = torch.randn(aux_lst, dtype=latent_image.dtype, layout=latent_image.layout, generator=generator, device="cpu")
if i in unique_inds:
noises.append(noise)
return torch.cat([noises[i] for i in inverse], axis=0)
###
### Existing Solutions
_No response_
### Other
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.