Repository: huggingface/diffusers

Issue:
Typo in noise level formula in Karras scheduler
### Describe the bug

Comparing the formula for noise levels in `KarrasVeScheduler` 

https://github.com/huggingface/diffusers/blob/a45dca077c636cd60199bab888da4ecfcc206d18/src/diffusers/schedulers/scheduling_karras_ve.py#L107-L113

with [Karras, Tero, et al.](https://arxiv.org/abs/2206.00364) (VE column of Table 1), it seems to me that there is `**2` missing, and the correct formula should be

```python
self.schedule = [
            (
                self.config.sigma_max**2
                * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
            )
            for i in self.timesteps
        ]
```

This way the noise indeed ranges from `sigma_min` to `sigma_max`, as the docstring says.

Please let me know if I'm misunderstanding something here

This actually affects the generated images: they become less grainy and oversaturated after this is fixed

### Reproduction

_No response_

### Logs

_No response_

### System Info

- `diffusers` version: 0.4.0.dev0
- Platform: Linux-5.10.133+-x86_64-with-Ubuntu-18.04-bionic
- Python version: 3.7.14
- PyTorch version (GPU?): 1.12.1+cu113 (True)
- Huggingface_hub version: 0.9.1
- Transformers version: 4.22.1
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No


Discussion:
Hi @sgrigory - great catch! This scheduler isn't used in any big spaces / demos yet, so do you want to make a PR to change this? If not, I can easily do so (there may need to be a couple updated tests too) 🤗 

Resolving patch:
diff --git a/src/diffusers/schedulers/scheduling_karras_ve.py b/src/diffusers/schedulers/scheduling_karras_ve.py
--- a/src/diffusers/schedulers/scheduling_karras_ve.py
+++ b/src/diffusers/schedulers/scheduling_karras_ve.py
@@ -110,7 +110,7 @@ def set_timesteps(self, num_inference_steps: int):
         self.timesteps = np.arange(0, self.num_inference_steps)[::-1].copy()
         self.schedule = [
             (
-                self.config.sigma_max
+                self.config.sigma_max**2
                 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
             )
             for i in self.timesteps
diff --git a/src/diffusers/schedulers/scheduling_karras_ve_flax.py b/src/diffusers/schedulers/scheduling_karras_ve_flax.py
--- a/src/diffusers/schedulers/scheduling_karras_ve_flax.py
+++ b/src/diffusers/schedulers/scheduling_karras_ve_flax.py
@@ -113,7 +113,7 @@ def set_timesteps(self, state: KarrasVeSchedulerState, num_inference_steps: int)
         timesteps = jnp.arange(0, num_inference_steps)[::-1].copy()
         schedule = [
             (
-                self.config.sigma_max
+                self.config.sigma_max**2
                 * (self.config.sigma_min**2 / self.config.sigma_max**2) ** (i / (num_inference_steps - 1))
             )
             for i in timesteps


Test patch:
diff --git a/tests/test_pipelines.py b/tests/test_pipelines.py
--- a/tests/test_pipelines.py
+++ b/tests/test_pipelines.py
@@ -1104,7 +1104,7 @@ def test_karras_ve_pipeline(self):
 
         image_slice = image[0, -3:, -3:, -1]
         assert image.shape == (1, 256, 256, 3)
-        expected_slice = np.array([0.26815, 0.1581, 0.2658, 0.23248, 0.1550, 0.2539, 0.1131, 0.1024, 0.0837])
+        expected_slice = np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586])
         assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2
 
     @slow