diff --git a/ultralytics/data/augment.py b/ultralytics/data/augment.py
index d092e3c3703..bd821de28de 100644
--- a/ultralytics/data/augment.py
+++ b/ultralytics/data/augment.py
@@ -1591,7 +1591,7 @@ def __call__(self, labels=None, image=None):
             labels["ratio_pad"] = (labels["ratio_pad"], (left, top))  # for evaluation

         if len(labels):
-            labels = self._update_labels(labels, ratio, dw, dh)
+            labels = self._update_labels(labels, ratio, left, top)
             labels["img"] = img
             labels["resized_shape"] = new_shape
             return labels

Significant mAP Drop When Using Bottom-Right Padding Instead of Center Padding in YOLOv8 Training
### Search before asking

- [X] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and [discussions](https://github.com/ultralytics/ultralytics/discussions) and found no similar questions.

### Question

Hi, I'm training a YOLOv8 model on the same dataset, but noticed a significant difference in mAP when changing the padding strategy.

    When using center padding (default), the mAP@50 after the first epoch is around 0.88.
    When using bottom-right padding instead, the mAP@50 drops to 0.0001.

I ensured that:

    The same data augmentation and other settings were used in both cases.

However, the bottom-right padding leads to poor performance. What could be causing such a drastic performance drop? Is it related to padding affecting feature distribution, anchor design, or augmentation strategies? Any suggestions for improving performance in this case would be appreciated!

Thanks!
I changed "center=True" to "center=False",augment.py line1500
![2024-11-23 16-09-01 的屏幕截图](https://github.com/user-attachments/assets/b2c6c27d-2518-45cf-819b-40e6e6de87ce)
![2024-11-23 16-09-40 的屏幕截图](https://github.com/user-attachments/assets/fa10d8b8-2a61-4212-b709-2c213133bac4)
![2024-11-23 16-09-29 的屏幕截图](https://github.com/user-attachments/assets/2b2c9fd2-f53d-47ca-977f-7db6cd00f1c8)

### Additional

_No response_

👋 Hello @Gebbap, thank you for bringing your findings to the Ultralytics community's attention 🚀!

We recommend checking out our [Docs](https://docs.ultralytics.com), where you can find comprehensive information on [Python](https://docs.ultralytics.com/usage/python/) and [CLI](https://docs.ultralytics.com/usage/cli/) usage, which may offer some insights into your padding strategy question.

Given this is a deep technical question related to padding strategies, we would appreciate if you could provide a [minimum reproducible example](https://docs.ultralytics.com/help/minimum_reproducible_example/). This will greatly assist our team in diagnosing and addressing the issue more effectively.

Meanwhile, please ensure that you're running the most up-to-date version of the `ultralytics` package. You can upgrade using the following command:

```bash
pip install -U ultralytics
```

This ensures that any recent fixes or improvements are integrated into your environment.

For additional support and insights from both engineers and experienced community members, feel free to join the conversation on our [Discord](https://ultralytics.com/discord) 🎧, or start a discussion on [Discourse](https://community.ultralytics.com) or our [Subreddit](https://reddit.com/r/ultralytics).

An Ultralytics engineer will soon review your question to provide further assistance. Thank you for your patience and support 👍!

## Environments

YOLO can be executed in various verified environments, which come pre-installed with all dependencies, including [CUDA](https://developer.nvidia.com/cuda)/[CUDNN](https://developer.nvidia.com/cudnn), [Python](https://www.python.org/), and [PyTorch](https://pytorch.org/):

- **Notebooks** with free GPU:
- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial/)
- **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial/)
- **Docker Image**. See [Docker Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial/)

## Status

A green badge indicates all [Ultralytics CI](https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml?query=event%3Aschedule) tests are passing. CI tests ensure the correct operation of all YOLO [Modes](https://docs.ultralytics.com/modes/) and [Tasks](https://docs.ultralytics.com/tasks/) on macOS, Windows, and Ubuntu every 24 hours and upon each commit.
Did you check if the training labels are correct in the plots?
yes,it is correct.two results used the same dateset and labels
I checked the labels after setting `center=False` and they're wrong. So this modification is incorrect and breaks the training labels which is why you're getting low scores.

![train_batch0.jpg](https://github.com/user-attachments/assets/7ec598d9-8140-4dd7-bd7f-fa2764061ce9)

Thank you! Simply modifying the value of "center" only changes the position of the plot but doesn’t adjust the labels correctly. How can I change the padding method? Should I modify the source code, or is there an official way to achieve this?


diff --git a/ultralytics/utils/callbacks/wb.py b/ultralytics/utils/callbacks/wb.py
index b82b8d85ec3..22bbc347566 100644
--- a/ultralytics/utils/callbacks/wb.py
+++ b/ultralytics/utils/callbacks/wb.py
@@ -138,7 +138,7 @@ def on_train_end(trainer):
         art.add_file(trainer.best)
         wb.run.log_artifact(art, aliases=["best"])
     # Check if we actually have plots to save
-    if trainer.args.plots:
+    if trainer.args.plots and hasattr(trainer.validator.metrics, "curves_results"):
         for curve_name, curve_values in zip(trainer.validator.metrics.curves, trainer.validator.metrics.curves_results):
             x, y, x_title, y_title = curve_values
             _plot_curve(

wandb callback reporting fails if no positive examples in validator
### Search before asking

- [X] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report.

### Ultralytics YOLO Component

Predict

### Bug

When using the `wandb` callback, the following error occurs if there are no positive examples:
```
Traceback (most recent call last):
  File "pipeline.py", line 97, in <module>
    main()
  File "pipeline.py", line 84, in main
    output_path = train_stage(stage_config)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "train_new.py", line 57, in train_stage
    results = model.train(**train_args)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/ultralytics/engine/model.py", line 802, in train
    self.trainer.train()
  File "site-packages/ultralytics/engine/trainer.py", line 207, in train
    self._do_train(world_size)
  File "site-packages/ultralytics/engine/trainer.py", line 477, in _do_train
    self.run_callbacks("on_train_end")
  File "site-packages/ultralytics/engine/trainer.py", line 168, in run_callbacks
    callback(self)
  File "site-packages/ultralytics/utils/callbacks/wb.py", line 141, in on_train_end
    if trainer.args.plots and trainer.validator.metrics.curves and trainer.validator.metrics.curves_results:
                                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/ultralytics/utils/__init__.py", line 221, in __getattr__
    raise AttributeError(f"'{name}' object has no attribute '{attr}'. See valid attributes below.\n{self.__doc__}")
AttributeError: 'DetMetrics' object has no attribute 'curves_results'. See valid attributes below.
    Utility class for computing detection metrics such as precision, recall, and mean average precision (mAP) of an
    object detection model.
    Args:
        save_dir (Path): A path to the directory where the output plots will be saved. Defaults to current directory.
        plot (bool): A flag that indicates whether to plot precision-recall curves for each class. Defaults to False.
        on_plot (func): An optional callback to pass plots path and data when they are rendered. Defaults to None.
        names (dict of str): A dict of strings that represents the names of the classes. Defaults to an empty tuple.
    Attributes:
        save_dir (Path): A path to the directory where the output plots will be saved.
        plot (bool): A flag that indicates whether to plot the precision-recall curves for each class.
        on_plot (func): An optional callback to pass plots path and data when they are rendered.
        names (dict of str): A dict of strings that represents the names of the classes.
        box (Metric): An instance of the Metric class for storing the results of the detection metrics.
        speed (dict): A dictionary for storing the execution time of different parts of the detection process.
    Methods:
        process(tp, conf, pred_cls, target_cls): Updates the metric results with the latest batch of predictions.
        keys: Returns a list of keys for accessing the computed detection metrics.
        mean_results: Returns a list of mean values for the computed detection metrics.
        class_result(i): Returns a list of values for the computed detection metrics for a specific class.
        maps: Returns a dictionary of mean average precision (mAP) values for different IoU thresholds.
        fitness: Computes the fitness score based on the computed detection metrics.
        ap_class_index: Returns a list of class indices sorted by their average precision (AP) values.
        results_dict: Returns a dictionary that maps detection metric keys to their computed values.
        curves: TODO
        curves_results: TODO
```

### Environment

```
Ultralytics 8.3.30 🚀 Python-3.11.5 torch-2.1.2+cu121 CUDA:0 (NVIDIA GeForce RTX 4090, 24111MiB)
Setup complete ✅ (32 CPUs, 251.5 GB RAM, 6725.0/7096.0 GB disk)

OS                  Linux-6.5.0-45-generic-x86_64-with-glibc2.35
Environment         Linux
Python              3.11.5
Install             pip
RAM                 251.52 GB
Disk                6725.0/7096.0 GB
CPU                 AMD Ryzen Threadripper PRO 5955WX 16-Cores
CPU count           32
GPU                 NVIDIA GeForce RTX 4090, 24111MiB
GPU count           3
CUDA                12.1

numpy               ✅ 1.24.3>=1.23.0
matplotlib          ✅ 3.7.2>=3.3.0
opencv-python       ✅ 4.8.1.78>=4.6.0
pillow              ✅ 10.1.0>=7.1.2
pyyaml              ✅ 6.0>=5.3.1
requests            ✅ 2.31.0>=2.23.0
scipy               ✅ 1.11.1>=1.4.1
torch               ✅ 2.1.2>=1.8.0
torchvision         ✅ 0.16.2>=0.9.0
tqdm                ✅ 4.65.0>=4.64.0
psutil              ✅ 5.9.0
py-cpuinfo          ✅ 8.0.0
pandas              ✅ 2.0.3>=1.1.4
seaborn             ✅ 0.12.2>=0.11.0
ultralytics-thop    ✅ 2.0.0>=2.0.0
numpy               ✅ 1.24.3<2.0.0; sys_platform == "darwin"
torch               ✅ 2.1.2!=2.4.0,>=1.8.0; sys_platform == "win32"
```

### Minimal Reproducible Example

```
import wandb
from ultralytics import YOLO
from wandb.integration.ultralytics import add_wandb_callback

def train_yolo():
    # Initialize wandb
    wandb.init(
        project="yolo-example",
        name="training-run",
        job_type="training"
    )

    # Initialize YOLO model
    model = YOLO('yolov8n.yaml')  # or 'yolov8n.pt' for pretrained

    # Add wandb callback to log metrics
    add_wandb_callback(model)

    # Train the model
    results = model.train(
        data='coco128_negative.yaml',  # path to data config file with negatives
        epochs=3,
        batch=16,
        imgsz=640
    )

    # Close wandb run
    wandb.finish()

if __name__ == "__main__":
    train_yolo()
```

### Additional

_No response_

### Are you willing to submit a PR?

- [X] Yes I'd like to help by submitting a PR!


diff --git a/ultralytics/engine/results.py b/ultralytics/engine/results.py
index 029e4471e04..8de0a2e6a1c 100644
--- a/ultralytics/engine/results.py
+++ b/ultralytics/engine/results.py
@@ -750,7 +750,7 @@ def save_crop(self, save_dir, file_name=Path("im.jpg")):
             save_one_box(
                 d.xyxy,
                 self.orig_img.copy(),
-                file=Path(save_dir) / self.names[int(d.cls)] / f"{Path(file_name)}.jpg",
+                file=Path(save_dir) / self.names[int(d.cls)] / Path(file_name).with_suffix(".jpg"),
                 BGR=True,
             )

Save_crop method from Results with default params results in double file extension
### Search before asking

- [X] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report.

### Ultralytics YOLO Component

Predict

### Bug

Save_crop method in the Results object already adds file extension to the saved image (.jpg) and default value is set as 'im.jpg' so when using default behaviour we get a file named **"im.jpg.jpg"**
With this few examples
`results[0].save_crop(save_dir='../out')`
`results[0].save_crop(save_dir='../out', file_name='img.png')`
`results[0].save_crop(save_dir='../out', file_name='img')`
Here are the outputs in a File Explorer, only one without double extension is when you add file_name without any extension
![image](https://github.com/user-attachments/assets/873bb127-ca4e-439d-8f74-8aece55fd3fa)

### Environment

Setup complete ✅ (8 CPUs, 15.9 GB RAM, 477.1/931.5 GB disk)

OS                  Windows-10-10.0.17763-SP0
Environment         Windows
Python              3.10.15
Install             pip
RAM                 15.94 GB
Disk                477.1/931.5 GB
CPU                 Intel Core(TM) i7-9700 3.00GHz
CPU count           8
GPU                 NVIDIA GeForce GTX 1660, 6144MiB
GPU count           1
CUDA                11.8

numpy               ✅ 1.26.3>=1.23.0
matplotlib          ✅ 3.9.2>=3.3.0
opencv-python       ✅ 4.10.0.84>=4.6.0
pillow              ✅ 10.2.0>=7.1.2
pyyaml              ✅ 6.0.2>=5.3.1
requests            ✅ 2.32.3>=2.23.0
scipy               ✅ 1.14.1>=1.4.1
torch               ✅ 2.5.1+cu118>=1.8.0
torchvision         ✅ 0.20.1+cu118>=0.9.0
tqdm                ✅ 4.67.0>=4.64.0
psutil              ✅ 6.1.0
py-cpuinfo          ✅ 9.0.0
pandas              ✅ 2.2.3>=1.1.4
seaborn             ✅ 0.13.2>=0.11.0
ultralytics-thop    ✅ 2.0.11>=2.0.0
numpy               ✅ 1.26.3<2.0.0; sys_platform == "darwin"
torch               ✅ 2.5.1+cu118!=2.4.0,>=1.8.0; sys_platform == "win32"

### Minimal Reproducible Example

```
model = YOLO('../model/yolov8s.pt')
results = model.predict(frame, conf=0.5)
results[0].save_crop(save_dir='../out')
```

### Additional

_No response_

### Are you willing to submit a PR?

- [X] Yes I'd like to help by submitting a PR!

👋 Hello @M3nxudo, thank you for bringing this to our attention! We're excited to assist you 🚀 and appreciate your proactive approach to contribute with a PR.

For anyone facing similar issues, we highly recommend checking out our [Docs](https://docs.ultralytics.com) for guidance on both [Python](https://docs.ultralytics.com/usage/python/) and [CLI](https://docs.ultralytics.com/usage/cli/) usage.

Since this seems to be a 🐛 Bug Report, please ensure that the provided [minimal reproducible example](https://docs.ultralytics.com/help/minimum_reproducible_example/) accurately reflects the issue you're facing. This will help us diagnose the problem more efficiently.

For real-time help or to engage with our vibrant community, you can join us on [Discord](https://ultralytics.com/discord) 🎧. For more in-depth discussions, consider visiting our [Discourse](https://community.ultralytics.com) or share insights with members on our [Subreddit](https://reddit.com/r/ultralytics).

## Upgrade

Make sure you're using the latest version of the `ultralytics` package, along with all its [requirements](https://github.com/ultralytics/ultralytics/blob/main/pyproject.toml). Verify this in a [**Python>=3.8**](https://www.python.org/) environment with [**PyTorch>=1.8**](https://pytorch.org/get-started/locally/):

```bash
pip install -U ultralytics
```

## Environments

You can run YOLO in various verified environments that include all necessary dependencies, such as [CUDA](https://developer.nvidia.com/cuda), [Python](https://www.python.org/), and [PyTorch](https://pytorch.org/):

- **Notebooks** with free GPU:
- **Google Cloud** Deep Learning VM: Check the [GCP Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial/)
- **Amazon** Deep Learning AMI: See the [AWS Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial/)
- **Docker Image**: Refer to the [Docker Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial/)

## Status

Our [Ultralytics CI](https://github.com/ultralytics/ultralytics/actions/workflows/ci.yaml?query=event%3Aschedule) tests run every 24 hours and on all commits to ensure smooth operation across different environments and verify correct functionality for all YOLO [Modes](https://docs.ultralytics.com/modes/) and [Tasks](https://docs.ultralytics.com/tasks/).

This is an automated response and an Ultralytics engineer will review your issue shortly. Thank you for your understanding and collaboration! 🌟
Easiest solution would be to change the default value of the file_name param from  line 721 of results.py:
https://github.com/ultralytics/ultralytics/blob/main/ultralytics/engine/results.py
`def save_crop(self, save_dir, file_name=Path("im.jpg")):`
to
 **file_name=Path("im")**
since on line 753 we already add the file extension:
`file=Path(save_dir) / self.names[int(d.cls)] / f"{Path(file_name)}.jpg",`


diff --git a/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py b/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py
index 6ddd9ac23009..c7474d56c708 100644
--- a/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py
+++ b/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py
@@ -99,10 +99,19 @@ def __init__(
         self._step_index = None
         self._begin_index = None

+        self._shift = shift
+
         self.sigmas = sigmas.to("cpu")  # to avoid too much CPU/GPU communication
         self.sigma_min = self.sigmas[-1].item()
         self.sigma_max = self.sigmas[0].item()

+    @property
+    def shift(self):
+        """
+        The value used for shifting.
+        """
+        return self._shift
+
     @property
     def step_index(self):
         """
@@ -128,6 +137,9 @@ def set_begin_index(self, begin_index: int = 0):
         """
         self._begin_index = begin_index

+    def set_shift(self, shift: float):
+        self._shift = shift
+
     def scale_noise(
         self,
         sample: torch.FloatTensor,
@@ -236,7 +248,7 @@ def set_timesteps(
         if self.config.use_dynamic_shifting:
             sigmas = self.time_shift(mu, 1.0, sigmas)
         else:
-            sigmas = self.config.shift * sigmas / (1 + (self.config.shift - 1) * sigmas)
+            sigmas = self.shift * sigmas / (1 + (self.shift - 1) * sigmas)

         if self.config.shift_terminal:
             sigmas = self.stretch_shift_to_terminal(sigmas)

Allow configuring `shift=` for SD3 dynamically
**Is your feature request related to a problem? Please describe.**
Allow passing `shift=` per inference call (like timesteps) on the pipeline, for flow matching scheduler, or allow `set_shift()` etc. on the scheduler. This seems to be the key to getting good results with SD3 https://x.com/bfitzgerald242/status/1801018438120341911

Hi, you can do it like this:

```python
from diffusers import  FlowMatchEulerDiscreteScheduler

pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(pipe.scheduler.config, shift=3.0)
```
yep! but the same format is applicable for timesteps and was wondering if we can get around without re-instating the scheduler again and again?
Not for the moment, but I can see the potential in adding it as an argument if people change it a lot for each inference.

In my experience it didn't fix the anatomy problems and sometimes it made the quality worse but I tested it with the T5, still need to test it without it and do some more generations.

Can you share some examples where changing the shift helped with the generation? That would help a lot.
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) are likely to be ignored.
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) are likely to be ignored.
Useful to have, changing `shift` is popular with HunyuanVideo and SD3/Flux.
does it really help though? examples requested never were shared.
Documented benefit for HunyuanVideo.

https://github.com/huggingface/diffusers/blob/1524781b88ac1a082e755a030ba9d73cd6948e84/docs/source/en/api/pipelines/hunyuan_video.md?plain=1#L32

I'll run some tests for SD3/Flux to confirm.
but SD3 already has resolution-dependent shift using the `mu` calculations, right?
Actually this won't do anything for Flux because of dynamic shifting. We recently added support for dynamic shifting in SD3, it's not used by default though. Either way, it's a simple change that has at least some benefit to HunyuanVideo and it won't do any harm to add a function to change `shift` without creating the scheduler each time for those that want it.
well not to be argumentative but the schedulers are stateful, yes?  doesn't this mean recreating it has to be done anyway?


diff --git a/src/diffusers/models/unets/unet_2d.py b/src/diffusers/models/unets/unet_2d.py
index 5972505f2897..d05af686dede 100644
--- a/src/diffusers/models/unets/unet_2d.py
+++ b/src/diffusers/models/unets/unet_2d.py
@@ -97,6 +97,7 @@ def __init__(
         out_channels: int = 3,
         center_input_sample: bool = False,
         time_embedding_type: str = "positional",
+        time_embedding_dim: Optional[int] = None,
         freq_shift: int = 0,
         flip_sin_to_cos: bool = True,
         down_block_types: Tuple[str, ...] = ("DownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D", "AttnDownBlock2D"),
@@ -122,7 +123,7 @@ def __init__(
         super().__init__()

         self.sample_size = sample_size
-        time_embed_dim = block_out_channels[0] * 4
+        time_embed_dim = time_embedding_dim or block_out_channels[0] * 4

         # Check inputs
         if len(down_block_types) != len(up_block_types):

Make `time_embed_dim` of `UNet2DModel` changeable
**Is your feature request related to a problem? Please describe.**
I want to change the `time_embed_dim` of `UNet2DModel`, but it is hard coded as `time_embed_dim = block_out_channels[0] * 4` in the `__init__` function.

**Describe the solution you'd like.**
Make `time_embedding_dim` a parameter of the `__init__` function, with the default value of `None`. Use `time_embed_dim = time_embedding_dim or block_out_channels[0] * 4` in the function body.

**Describe alternatives you've considered.**
N/A.

**Additional context.**
The same thing in `UNet2DConditionModel` can be changed via the parameter `time_embedding_dim` of its `__init__` function.


diff --git a/src/diffusers/schedulers/scheduling_repaint.py b/src/diffusers/schedulers/scheduling_repaint.py
index 97665bb5277b..ae953cfb966b 100644
--- a/src/diffusers/schedulers/scheduling_repaint.py
+++ b/src/diffusers/schedulers/scheduling_repaint.py
@@ -319,7 +319,7 @@ def step(
         prev_unknown_part = alpha_prod_t_prev**0.5 * pred_original_sample + pred_sample_direction + variance

         # 8. Algorithm 1 Line 5 https://arxiv.org/pdf/2201.09865.pdf
-        prev_known_part = (alpha_prod_t_prev**0.5) * original_image + ((1 - alpha_prod_t_prev) ** 0.5) * noise
+        prev_known_part = (alpha_prod_t_prev**0.5) * original_image + (1 - alpha_prod_t_prev) * noise

         # 9. Algorithm 1 Line 8 https://arxiv.org/pdf/2201.09865.pdf
         pred_prev_sample = mask * prev_known_part + (1.0 - mask) * prev_unknown_part

Potential bug in repaint?
https://github.com/huggingface/diffusers/blob/dac623b59f52c58383a39207d5147aa34e0047cd/src/diffusers/schedulers/scheduling_repaint.py#L322

According to line5 of algorithm 1 in the paper, the second part in line 322 should remove the `**0.5`?
thanks!

I also think that should be removed as mentioned in algorithm 1 Line 5 from the [paper](https://arxiv.org/pdf/2201.09865)
```math
x_{t-1}^{known} \ =\ \sqrt{\overline{\alpha }_{t}} x_0 \ +( 1-\ \overline{\alpha }_{t}) \epsilon
```
Corrected
```python
prev_known_part = (alpha_prod_t_prev**0.5) * original_image + ((1 - alpha_prod_t_prev)) * noise
```
I don't think Fixing this might cause any issue in RePaintScheduler
@sayakpaul WDYT?

Cc: @yiyixuxu
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md) are likely to be ignored.


diff --git a/src/diffusers/loaders/lora_pipeline.py b/src/diffusers/loaders/lora_pipeline.py
index eb9b42c5fbb7..1445394b8784 100644
--- a/src/diffusers/loaders/lora_pipeline.py
+++ b/src/diffusers/loaders/lora_pipeline.py
@@ -2313,7 +2313,7 @@ def _maybe_expand_transformer_param_shape_or_error_(
         for name, module in transformer.named_modules():
             if isinstance(module, torch.nn.Linear):
                 module_weight = module.weight.data
-                module_bias = module.bias.data if hasattr(module, "bias") else None
+                module_bias = module.bias.data if module.bias is not None else None
                 bias = module_bias is not None

                 lora_A_weight_name = f"{name}.lora_A.weight"

Can't load multiple loras when using Flux Control LoRA
### Describe the bug

I was trying out the FluxControlPipeline with the Control LoRA introduced in #9999 , but had issues loading in multiple loras.

For example, if I load the depth lora first and then the 8-step lora, it errors on the 8-step lora, and if I load the 8-step lora first and then the depth lora, it errors when loading the depth lora.

### Reproduction

```
from diffusers import FluxControlPipeline
from huggingface_hub import hf_hub_download
import torch

control_pipe = FluxControlPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to("cuda")
control_pipe.load_lora_weights("black-forest-labs/FLUX.1-Depth-dev-lora")
control_pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"))

```

### Logs

```shell
AttributeError                            Traceback (most recent call last)
Cell In[6], line 8
      5 control_pipe = FluxControlPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to("cuda")
      7 control_pipe.load_lora_weights("black-forest-labs/FLUX.1-Depth-dev-lora")
----> 8 control_pipe.load_lora_weights(
      9         hf_hub_download(
     10             "ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"
     11         ),
     12         adapter_name="HyperFlux",
     13     )

File ~/.venv/lib/python3.10/site-packages/diffusers/loaders/lora_pipeline.py:1856, in FluxLoraLoaderMixin.load_lora_weights(self, pretrained_model_name_or_path_or_dict, adapter_name, **kwargs)
   1849 transformer_norm_state_dict = {
   1850     k: state_dict.pop(k)
   1851     for k in list(state_dict.keys())
   1852     if "transformer." in k and any(norm_key in k for norm_key in self._control_lora_supported_norm_keys)
   1853 }
   1855 transformer = getattr(self, self.transformer_name) if not hasattr(self, "transformer") else self.transformer
-> 1856 has_param_with_expanded_shape = self._maybe_expand_transformer_param_shape_or_error_(
   1857     transformer, transformer_lora_state_dict, transformer_norm_state_dict
   1858 )
   1860 if has_param_with_expanded_shape:
   1861     logger.info(
   1862         "The LoRA weights contain parameters that have different shapes that expected by the transformer. "
   1863         "As a result, the state_dict of the transformer has been expanded to match the LoRA parameter shapes. "
   1864         "To get a comprehensive list of parameter names that were modified, enable debug logging."
   1865     )

File ~/.venv/lib/python3.10/site-packages/diffusers/loaders/lora_pipeline.py:2316, in FluxLoraLoaderMixin._maybe_expand_transformer_param_shape_or_error_(cls, transformer, lora_state_dict, norm_state_dict, prefix)
   2314 if isinstance(module, torch.nn.Linear):
   2315     module_weight = module.weight.data
-> 2316     module_bias = module.bias.data if hasattr(module, "bias") else None
   2317     bias = module_bias is not None
   2319     lora_A_weight_name = f"{name}.lora_A.weight"

AttributeError: 'NoneType' object has no attribute 'data'
```

### System Info

- 🤗 Diffusers version: 0.32.0.dev0
- Platform: Linux-5.15.0-124-generic-x86_64-with-glibc2.35
- Running on Google Colab?: No
- Python version: 3.10.12
- PyTorch version (GPU?): 2.5.1+cu124 (True)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Huggingface_hub version: 0.26.5
- Transformers version: 4.47.0
- Accelerate version: 1.2.0
- PEFT version: 0.14.0
- Bitsandbytes version: not installed
- Safetensors version: 0.4.5
- xFormers version: not installed
- Accelerator: NVIDIA H100 80GB HBM3, 81559 MiB
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: No

### Who can help?

@a-r-r-o-w @sayakpaul

Oh, we should have anticipated this use case. I think the correct check should be `module_bias = module.bias.data if module.bias is not None else None` instead.

Even with the above fix, I don't think the weights would load as expected because the depth control lora would expand the input features of `x_embedder` to 128, but Hyper-SD LoRA will have input features of 64. Will try and respond back shortly

cc @yiyixuxu as well
It does indeed error out with the corrected if-statement as well due to the explanation above.

<details>
<summary> trace </summary>

```bash
Traceback (most recent call last):
  File "/home/aryan/work/diffusers/dump4.py", line 9, in <module>
    pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"))
  File "/home/aryan/work/diffusers/src/diffusers/loaders/lora_pipeline.py", line 1868, in load_lora_weights
    self.load_lora_into_transformer(
  File "/home/aryan/work/diffusers/src/diffusers/loaders/lora_pipeline.py", line 1932, in load_lora_into_transformer
    transformer.load_lora_adapter(
  File "/home/aryan/work/diffusers/src/diffusers/loaders/peft.py", line 320, in load_lora_adapter
    incompatible_keys = set_peft_model_state_dict(self, state_dict, adapter_name, **peft_kwargs)
  File "/raid/aryan/nightly-venv/lib/python3.10/site-packages/peft/utils/save_and_load.py", line 445, in set_peft_model_state_dict
    load_result = model.load_state_dict(peft_model_state_dict, strict=False, assign=True)
  File "/raid/aryan/nightly-venv/lib/python3.10/site-packages/torch/nn/modules/module.py", line 2584, in load_state_dict
    raise RuntimeError(
RuntimeError: Error(s) in loading state_dict for FluxTransformer2DModel:
        size mismatch for x_embedder.lora_A.default_1.weight: copying a param with shape torch.Size([64, 64]) from checkpoint, the shape in current model is torch.Size([64, 128]).
```
</details>

I do believe that this should work as expected allowing for depth-control-lora to work with N-step hyper-sd-loras. This is a unique case that has probably never been investigated before. Not completely sure on how we would handle this either :/

My initial thoughts are to expand the lora shapes as well, and set the weights of the linear layer corresponding to the depth control input to 0. This should effectively remove the control latent from interfering with the effect of hyper-sd and it will operate only on the denoising latent. Will experiment and let the results speak for whether this would be something we should try to prioritize support for (as there are 10000+ available Flux loras that might be compatible), and will let YiYi and Sayak comment on how best to handle this situation if it works as expected

Are you facing any errors when trying to run inference with LoRAs, but without control LoRAs? Either way, I think above mentioned condition needs to be updated.
I just tried using the normal `FluxPipeline`, which also encounters the same issue.

Repro script:

```
from diffusers import FluxPipeline
import torch
from huggingface_hub import hf_hub_download

pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16
)
pipe.to("cuda")

pipe.load_lora_weights(
    hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8steps-lora.safetensors"),
)
pipe.load_lora_weights(
    "strangerzonehf/Flux-Midjourney-Mix2-LoRA",
)
```


diff --git a/src/diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py b/src/diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py
index c6748ad418fe..6c36ec173539 100644
--- a/src/diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py
+++ b/src/diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py
@@ -446,13 +446,14 @@ def prepare_extra_step_kwargs(self, generator, eta):
             extra_step_kwargs["generator"] = generator
         return extra_step_kwargs

-    # Copied from diffusers.pipelines.stable_diffusion_k_diffusion.pipeline_stable_diffusion_k_diffusion.StableDiffusionKDiffusionPipeline.check_inputs
     def check_inputs(
         self,
         prompt,
         height,
         width,
         callback_steps,
+        gligen_images,
+        gligen_phrases,
         negative_prompt=None,
         prompt_embeds=None,
         negative_prompt_embeds=None,
@@ -499,6 +500,13 @@ def check_inputs(
                     f" {negative_prompt_embeds.shape}."
                 )

+        if gligen_images is not None and gligen_phrases is not None:
+            if len(gligen_images) != len(gligen_phrases):
+                raise ValueError(
+                    "`gligen_images` and `gligen_phrases` must have the same length when both are provided, but"
+                    f" got: `gligen_images` with length {len(gligen_images)} != `gligen_phrases` with length {len(gligen_phrases)}."
+                )
+
     # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_latents
     def prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):
         shape = (
@@ -814,6 +822,8 @@ def __call__(
             height,
             width,
             callback_steps,
+            gligen_images,
+            gligen_phrases,
             negative_prompt,
             prompt_embeds,
             negative_prompt_embeds,

Raise an error when `len(gligen_images )` is not equal to `len(gligen_phrases)` in `StableDiffusionGLIGENTextImagePipeline`
To whom it may concern,

I found that when using `StableDiffusionGLIGENTextImagePipeline`, there is no error raised when `len(gligen_images )` is not equal to `len(gligen_phrases)`. And when I dig into the source code, it seems that these two features are zipped together in a for loop during the preprocessing. I guess this will cause the longer one to be clipped unintentionally. (If my understanding is wrong, feel free to correct me.) Is there any possibility to raise an error or at least warning? Thanks in advance.

Source Code: https://github.com/huggingface/diffusers/blob/v0.31.0/src/diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py#L689

Hi @abcdefg133hi. Thanks for finding this. Your understanding is correct, the longer of `gligen_phrases` and `gligen_images` will be clipped:

```python
for phrase, image in zip(["text", "text1", "text2"], ["image", "image1"]):
    print(phrase, image)
text image
text1 image1
```

We should add this to `check_inputs` and raise an error when `len(gligen_images) != len(gligen_phrases)`. Note that `Copied from` will need to be removed.
https://github.com/huggingface/diffusers/blob/c9e4fab42ca481fe8e0d2456b54ec900fb57730d/src/diffusers/pipelines/stable_diffusion_gligen/pipeline_stable_diffusion_gligen_text_image.py#L450

Would you like to submit a PR?


diff --git a/src/diffusers/image_processor.py b/src/diffusers/image_processor.py
index 00d8588d5a2a..d6913f045ad2 100644
--- a/src/diffusers/image_processor.py
+++ b/src/diffusers/image_processor.py
@@ -236,7 +236,7 @@ def denormalize(images: Union[np.ndarray, torch.Tensor]) -> Union[np.ndarray, to
             `np.ndarray` or `torch.Tensor`:
                 The denormalized image array.
         """
-        return (images / 2 + 0.5).clamp(0, 1)
+        return (images * 0.5 + 0.5).clamp(0, 1)

     @staticmethod
     def convert_to_rgb(image: PIL.Image.Image) -> PIL.Image.Image:
@@ -537,6 +537,26 @@ def binarize(self, image: PIL.Image.Image) -> PIL.Image.Image:

         return image

+    def _denormalize_conditionally(
+        self, images: torch.Tensor, do_denormalize: Optional[List[bool]] = None
+    ) -> torch.Tensor:
+        r"""
+        Denormalize a batch of images based on a condition list.
+
+        Args:
+            images (`torch.Tensor`):
+                The input image tensor.
+            do_denormalize (`Optional[List[bool]`, *optional*, defaults to `None`):
+                A list of booleans indicating whether to denormalize each image in the batch. If `None`, will use the
+                value of `do_normalize` in the `VaeImageProcessor` config.
+        """
+        if do_denormalize is None:
+            return self.denormalize(images) if self.config.do_normalize else images
+
+        return torch.stack(
+            [self.denormalize(images[i]) if do_denormalize[i] else images[i] for i in range(images.shape[0])]
+        )
+
     def get_default_height_width(
         self,
         image: Union[PIL.Image.Image, np.ndarray, torch.Tensor],
@@ -752,12 +772,7 @@ def postprocess(
         if output_type == "latent":
             return image

-        if do_denormalize is None:
-            do_denormalize = [self.config.do_normalize] * image.shape[0]
-
-        image = torch.stack(
-            [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])]
-        )
+        image = self._denormalize_conditionally(image, do_denormalize)

         if output_type == "pt":
             return image
@@ -966,12 +981,7 @@ def postprocess(
             deprecate("Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False)
             output_type = "np"

-        if do_denormalize is None:
-            do_denormalize = [self.config.do_normalize] * image.shape[0]
-
-        image = torch.stack(
-            [self.denormalize(image[i]) if do_denormalize[i] else image[i] for i in range(image.shape[0])]
-        )
+        image = self._denormalize_conditionally(image, do_denormalize)

         image = self.pt_to_numpy(image)

Post processing performance can be improved
## Problem

Images generated in batches pay a performance penalty in the post-processing step of the diffusion pipeline.

A lot of calls to image_processor.denormalize are made instead of batching the computation.

### Suggested Improvements

#### Using multiplication instead of division

This is a freebie, numerically the same but much cheaper on all compute platforms

```python
def denormalize(images: Union[np.ndarray, torch.Tensor]) -> Union[np.ndarray, torch.Tensor]:
    return (images * 0.5 + 0.5).clamp(0, 1)
```

#### Adding Fast path in an all-or-nothing denormalization scenario

Instead of calling `torch.stack` on a batch where all (or none) of the images require denormalization, apply the operation on the full tensor directly

```python
def post_process(...):
    # ...
    if do_denormalize is None:
        return self.denormalize(images) if self.config.do_normalize else images
    # ...
```

#### Denormalize first, stack later

Invoking the `denormalize` call multiple times incurs a lot of overhead on the compute pipeline, more so than "wasting" compute of denormalizing everything and calling `torch.stack` on the results.

```python
def post_process(...):
    # ...
    denormalized = self.denormalize(image)
    image = torch.stack([
        denormalized[i] if do_denormalize[i] else image[i] for i in range(image.shape[0])
    ])
    # ...
```

## Benchmarks

https://colab.research.google.com/drive/1H1SKUlyEZduUeU50V8SVEZgmFrdl6Ego?usp=sharing

These were ran in a Colab T4 instance on CUDA. The batch is of the shape `[1024,3,512,512]` and dtype `fp16`. Further combinations can be made if requested.

### Baseline

### Suggested Improvements


diff --git a/src/diffusers/models/embeddings.py b/src/diffusers/models/embeddings.py
index 91451fa9aac2..8f8f1073da74 100644
--- a/src/diffusers/models/embeddings.py
+++ b/src/diffusers/models/embeddings.py
@@ -959,7 +959,12 @@ def forward(self, ids: torch.Tensor) -> torch.Tensor:
         freqs_dtype = torch.float32 if is_mps else torch.float64
         for i in range(n_axes):
             cos, sin = get_1d_rotary_pos_embed(
-                self.axes_dim[i], pos[:, i], repeat_interleave_real=True, use_real=True, freqs_dtype=freqs_dtype
+                self.axes_dim[i],
+                pos[:, i],
+                theta=self.theta,
+                repeat_interleave_real=True,
+                use_real=True,
+                freqs_dtype=freqs_dtype,
             )
             cos_out.append(cos)
             sin_out.append(sin)

Some bugs in FLUX pipeline
### Describe the bug

1. missing self.theta in get_1d_rotary_pos_embed:
https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/embeddings.py#L961-L963
2. if prompt_embeds is None, pooled_prompt_embeds will never be computed:
https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/flux/pipeline_flux.py#L348-L363

### Reproduction

.

### Logs

_No response_

### System Info

.

### Who can help?

@yiyixuxu @sayakpaul @DN6 @asomoza

`pooled_prompt_embeds` has to be passed when `prompt_embeds` is used so that's ok
https://github.com/huggingface/diffusers/blob/8421c1461bf4ab7801070d04d6ec1e6b28ee5b59/src/diffusers/pipelines/flux/pipeline_flux.py#L422-L425

Would you like to open a PR that passes `self.theta` to `get_1d_rotary_pos_embed`?


diff --git a/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py b/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py
index a77231cdc02d..aee1ad8c75f5 100644
--- a/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py
+++ b/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py
@@ -907,11 +907,7 @@ def __call__(
                     continue

                 # expand the latents if we are doing classifier free guidance
-                latent_model_input = (
-                    torch.cat([latents] * 2)
-                    if self.do_classifier_free_guidance and skip_guidance_layers is None
-                    else latents
-                )
+                latent_model_input = torch.cat([latents] * 2) if self.do_classifier_free_guidance else latents
                 # broadcast to batch dimension in a way that's compatible with ONNX/Core ML
                 timestep = t.expand(latent_model_input.shape[0])

@@ -935,6 +931,8 @@ def __call__(
                         else False
                     )
                     if skip_guidance_layers is not None and should_skip_layers:
+                        timestep = t.expand(latents.shape[0])
+                        latent_model_input = latents
                         noise_pred_skip_layers = self.transformer(
                             hidden_states=latent_model_input,
                             timestep=timestep,

RuntimeError with LyCORIS, Batch Inference and skip_guidance_layers
### Describe the bug

A `RuntimeError` occurs when using the following combination:

* SD3
* Batch inference (`num_images_per_prompt > 1`)
* LyCORIS
* `skip_guidance_layers` is set

The error message is: `"RuntimeError: The size of tensor a (2) must match the size of tensor b (4) at non-singleton dimension 0"`

It seems that batch inference (`num_images_per_prompt > 1`) does not work in conjunction with `skip_guidance_layers`.

### Reproduction

This code snippet produces the error:

```python
self.pipe = StableDiffusion3Pipeline.from_pretrained(
    "stabilityai/stable-diffusion-3-medium-diffusers",
    torch_dtype=torch.bfloat16
    )

self.pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
    self.pipe.scheduler.config,
    timestep_spacing="trailing",
    shift=3.0
    )
self.pipe.to("cuda")

lora_scale = 1.0
wrapper, _ = create_lycoris_from_weights(lora_scale, my_lora, self.pipe.transformer)
wrapper.merge_to()

image = self.pipe(
    prompt=request.prompt,
    num_inference_steps=request.num_inference_steps,
    num_images_per_prompt=2,  # Batch inference
    output_type="pil",
    generator=torch.Generator(device="cuda").manual_seed(42),
    guidance_scale=request.guidance_scale,
    width=request.width,
    height=request.height,
    skip_guidance_layers=[7, 8, 9],  # Doesn't seem to work with batching
).images[0]
```

Commenting out `skip_guidance_layers` resolves the error.

**Expected behavior**

Batch inference should work correctly even when `skip_guidance_layers` is used with LyCORIS.

### Logs

_No response_

### System Info

**Environment**

*  CUDA Version: 12.4
*  Python version: 3.12.1 (main, Jan 11 2024, 10:22:40) [GCC 10.2.1 20210110]
*  Diffusers version: https://github.com/huggingface/diffusers.git@99c0483b67427de467f11aa35d54678fd36a7ea2
*  The specific LyCORIS model and inference method used from Bghira: https://huggingface.co/bghira/sd35m-photo-mixedres-cL-sS3-noOverride?not-for-all-audiences=true

### Who can help?

@sayakpaul

This is not a fully reproducible snippet. Please provide one.

Cc: @asomoza and @yiyixuxu for skip layer guidance.
Reproducible with:

```bash
pip install lycoris_lora
```

```python
from diffusers import StableDiffusion3Pipeline, FlowMatchEulerDiscreteScheduler
import torch
from huggingface_hub import hf_hub_download

from lycoris import create_lycoris_from_weights

pipe = StableDiffusion3Pipeline.from_pretrained(
    "stabilityai/stable-diffusion-3.5-medium", torch_dtype=torch.bfloat16
)
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
    pipe.scheduler.config, timestep_spacing="trailing", shift=3.0
)
pipe.to("cuda")

weights_path = hf_hub_download(
    "bghira/sd35m-photo-mixedres-cL-sS3-noOverride",
    filename="pytorch_lora_weights.safetensors",
)

lora_scale = 1.0
wrapper, _ = create_lycoris_from_weights(lora_scale, weights_path, pipe.transformer)
wrapper.merge_to()

prompt = "A photo-realistic image of a cat"
num_inference_steps = 28
guidance_scale = 3.5
width = 1024
height = 1024

image = pipe(
    prompt=prompt,
    num_inference_steps=num_inference_steps,
    num_images_per_prompt=2,  # Batch inference
    output_type="pil",
    generator=torch.Generator(device="cuda").manual_seed(42),
    guidance_scale=guidance_scale,
    width=width,
    height=height,
    skip_guidance_layers=[7, 8, 9],  # Doesn't seem to work with batching
).images[0]
```

```python
Traceback (most recent call last):
  File "/workspace/test.py", line 30, in <module>
    image = pipe(
            ^^^^^
  File "/usr/local/lib/python3.11/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/diffusers/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py", line 918, in __call__
    noise_pred = self.transformer(
                 ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/diffusers/src/diffusers/models/transformers/transformer_sd3.py", line 393, in forward
    temb = self.time_text_embed(timestep, pooled_projections)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/diffusers/src/diffusers/models/embeddings.py", line 1208, in forward
    conditioning = timesteps_emb + pooled_projections
                   ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
RuntimeError: The size of tensor a (2) must match the size of tensor b (4) at non-singleton dimension 0
```
So, SLG seems to be the culprit here?
> So, SLG seems to be the culprit here?

Indeed, problem seems to appear since I started using SLG
After some testing lycoris is unrelated, minimally reproducible with

```python
from diffusers import StableDiffusion3Pipeline, FlowMatchEulerDiscreteScheduler
import torch

pipe = StableDiffusion3Pipeline.from_pretrained(
    "stabilityai/stable-diffusion-3.5-medium", torch_dtype=torch.bfloat16
)
pipe.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
    pipe.scheduler.config, timestep_spacing="trailing", shift=3.0
)
pipe.to("cuda")

prompt = "A photo-realistic image of a cat"
num_inference_steps = 28
guidance_scale = 3.5
width = 1024
height = 1024

image = pipe(
    prompt=prompt,
    num_inference_steps=num_inference_steps,
    num_images_per_prompt=2,
    output_type="pil",
    generator=torch.Generator("cuda").manual_seed(42),
    guidance_scale=guidance_scale,
    width=width,
    height=height,
    skip_guidance_layers=[7, 8, 9],
).images[0]
image.save("minimal.png")
```

```
latent_model_input: torch.Size([2, 16, 128, 128])
timestep: torch.Size([2])
prompt_embeds: torch.Size([4, 333, 4096])
pooled_prompt_embeds: torch.Size([4, 2048])
```

Batch discrepancy, from
https://github.com/huggingface/diffusers/blob/827b6c25f9b78a297345f356a7d152fd6faf27d8/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py#L912

Same discrepancy with num_images_per_prompt=1, I think in this case PyTorch broadcasting takes care of it
```
latent_model_input: torch.Size([1, 16, 128, 128])
timestep: torch.Size([1])
prompt_embeds: torch.Size([2, 333, 4096])
pooled_prompt_embeds: torch.Size([2, 2048])
```

Ends up with similar issue in the next section. I'll do more testing and put together a PR.
https://github.com/huggingface/diffusers/blob/827b6c25f9b78a297345f356a7d152fd6faf27d8/src/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py#L938-L946


diff --git a/src/diffusers/models/upsampling.py b/src/diffusers/models/upsampling.py
index cf07e45b0c5c..af04ae4b93cf 100644
--- a/src/diffusers/models/upsampling.py
+++ b/src/diffusers/models/upsampling.py
@@ -165,6 +165,14 @@ def forward(self, hidden_states: torch.Tensor, output_size: Optional[int] = None
         # if `output_size` is passed we force the interpolation output
         # size and do not make use of `scale_factor=2`
         if self.interpolate:
+            # upsample_nearest_nhwc also fails when the number of output elements is large
+            # https://github.com/pytorch/pytorch/issues/141831
+            scale_factor = (
+                2 if output_size is None else max([f / s for f, s in zip(output_size, hidden_states.shape[-2:])])
+            )
+            if hidden_states.numel() * scale_factor > pow(2, 31):
+                hidden_states = hidden_states.contiguous()
+
             if output_size is None:
                 hidden_states = F.interpolate(hidden_states, scale_factor=2.0, mode="nearest")
             else:

[BUG - STABLE DIFFUSION 3] Grey images generated
### Describe the bug

I'm running the SD3 model [stabilityai/stable-diffusion-3-medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium) with the following settings:
- Height: 1024
- Width: 1024
- Inference steps: 50
- Guidance scale: 7
- Prompts length: 32 (using the same input prompt: "A men jumps from a high building")

The model generates 32 images, but half of them are grey.

### Reproduction

Reproduction code:

```
import os

os.environ['CUDA_VISIBLE_DEVICES'] = '7'

import torch
from diffusers import StableDiffusion3Pipeline

base_dir = os.path.join(os.path.dirname(__file__), '..', '..')

def run():
    input_prompts = ["A men jumps from a high building"] * 32
    img_save_dir = f'{base_dir}/data/test_generated_img'
    os.makedirs(f'{img_save_dir}', exist_ok=True)

    pipe = StableDiffusion3Pipeline.from_pretrained("stabilityai/stable-diffusion-3-medium-diffusers", torch_dtype=torch.float16, cache_dir="/data0/tien/cache")
    pipe.to("cuda")

    torch.set_grad_enabled(False)
    images = pipe(
        prompt=input_prompts,
        negative_prompt="",
        height=1024,
        width=1024,
        num_inference_steps=50,
        guidance_scale=7.0
    ).images

    for j in range(len(input_prompts)):
        images[j].save(os.path.join(f'{img_save_dir}', f'{j}.jpg'))

    torch.cuda.empty_cache()

run()
```

### Logs

_No response_

### System Info
- Device: H100
- Driver Version: 550.127.05
- CUDA Version: 12.4
- Torch: 2.5.1+cu124
- OS: Ubuntu 22.04.3 LTS
- Python: 3.10.15
- diffusers: 0.31.0

### Who can help?

_No response_

Does this happen when you switch to torch.bfloat16? Also, was this working ever as expected and suddenly stopped working as expected?
@sayakpaul Hi, the error persists when using BF16.
Out of the 32 generated images, the first 16 are fine, but the last 16 are all in grayscale.
Weird.

Does it happen on other prompts?
Yes, it appears that this error occurs with any prompt.
@asomoza have you heard about this before?
no, but SD 3 had some other problems so probably no one else did some extensive tests or generations with it.

@hoangvictor is there a reason you're using [stable-diffusion-3-medium](https://huggingface.co/stabilityai/stable-diffusion-3-medium) instead of [stable-diffusion-3.5-medium](https://huggingface.co/stabilityai/stable-diffusion-3.5-medium)?
@asomoza SD3.5 has been out since October, but when I started my project, only SD3 was available. I’ve also tried SD3.5, and the issue persists. It appears the problem lies with the VAE during the image decoding process.
I did a test with an A100 80GB, I could only do 24 images or I get OOM. I could replicate your error, this is a rare test for me because I would never throw 32 times the same prompt at the model instead of doing it in a loop because I like to see the results and most people just use a batch of 4.

Anyway, after the 16th image, the rest are all grey images so I can confirm this issue cc: @sayakpaul

Hmm, @DN6 could this be an issue with prompt encoding?

What happens when we generate 24 images for a single prompt by specifying `num_images_per_prompt`? Also does it happen with other pipelines too?

Very weird that it happens after we request for a certain number of images no?
It also happens with `num_images_per_prompt` when it's more than 16.

Tested it with SD 1.5 and this issue didn't happen.

It happens with any number of images when it's more than 16.
This is a PyTorch bug / limitation in the upsample operation. I found a workaround that I'll push tomorrow. Meanwhile, you can retrieve the latents instead of the images using `output_type="latent"` in your pipeline invocation, and then decode them manually like this:

```python
with torch.inference_mode():
    latents = (latents / pipe.vae.config.scaling_factor) + pipe.vae.config.shift_factor
    images_1 = pipe.vae.decode(latents[:16], return_dict=False)[0]
    images_2 = pipe.vae.decode(latents[16:], return_dict=False)[0]
    images = torch.cat([images_1, images_2])
    images = pipe.image_processor.postprocess(images)
```


diff --git a/src/diffusers/loaders/single_file_utils.py b/src/diffusers/loaders/single_file_utils.py
index d1bad8b5a7cd..9a460cb5d1ef 100644
--- a/src/diffusers/loaders/single_file_utils.py
+++ b/src/diffusers/loaders/single_file_utils.py
@@ -62,7 +62,14 @@
     "xl_base": "conditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias",
     "xl_refiner": "conditioner.embedders.0.model.transformer.resblocks.9.mlp.c_proj.bias",
     "upscale": "model.diffusion_model.input_blocks.10.0.skip_connection.bias",
-    "controlnet": "control_model.time_embed.0.weight",
+    "controlnet": [
+        "control_model.time_embed.0.weight",
+        "controlnet_cond_embedding.conv_in.weight",
+    ],
+    # TODO: find non-Diffusers keys for controlnet_xl
+    "controlnet_xl": "add_embedding.linear_1.weight",
+    "controlnet_xl_large": "down_blocks.1.attentions.0.transformer_blocks.0.attn1.to_k.weight",
+    "controlnet_xl_mid": "down_blocks.1.attentions.0.norm.weight",
     "playground-v2-5": "edm_mean",
     "inpainting": "model.diffusion_model.input_blocks.0.0.weight",
     "clip": "cond_stage_model.transformer.text_model.embeddings.position_embedding.weight",
@@ -96,6 +103,9 @@
     "inpainting": {"pretrained_model_name_or_path": "stable-diffusion-v1-5/stable-diffusion-inpainting"},
     "inpainting_v2": {"pretrained_model_name_or_path": "stabilityai/stable-diffusion-2-inpainting"},
     "controlnet": {"pretrained_model_name_or_path": "lllyasviel/control_v11p_sd15_canny"},
+    "controlnet_xl_large": {"pretrained_model_name_or_path": "diffusers/controlnet-canny-sdxl-1.0"},
+    "controlnet_xl_mid": {"pretrained_model_name_or_path": "diffusers/controlnet-canny-sdxl-1.0-mid"},
+    "controlnet_xl_small": {"pretrained_model_name_or_path": "diffusers/controlnet-canny-sdxl-1.0-small"},
     "v2": {"pretrained_model_name_or_path": "stabilityai/stable-diffusion-2-1"},
     "v1": {"pretrained_model_name_or_path": "stable-diffusion-v1-5/stable-diffusion-v1-5"},
     "stable_cascade_stage_b": {"pretrained_model_name_or_path": "stabilityai/stable-cascade", "subfolder": "decoder"},
@@ -481,8 +491,16 @@ def infer_diffusers_model_type(checkpoint):
     elif CHECKPOINT_KEY_NAMES["upscale"] in checkpoint:
         model_type = "upscale"

-    elif CHECKPOINT_KEY_NAMES["controlnet"] in checkpoint:
-        model_type = "controlnet"
+    elif any(key in checkpoint for key in CHECKPOINT_KEY_NAMES["controlnet"]):
+        if CHECKPOINT_KEY_NAMES["controlnet_xl"] in checkpoint:
+            if CHECKPOINT_KEY_NAMES["controlnet_xl_large"] in checkpoint:
+                model_type = "controlnet_xl_large"
+            elif CHECKPOINT_KEY_NAMES["controlnet_xl_mid"] in checkpoint:
+                model_type = "controlnet_xl_mid"
+            else:
+                model_type = "controlnet_xl_small"
+        else:
+            model_type = "controlnet"

     elif (
         CHECKPOINT_KEY_NAMES["stable_cascade_stage_c"] in checkpoint
@@ -1072,6 +1090,9 @@ def convert_controlnet_checkpoint(
     config,
     **kwargs,
 ):
+    # Return checkpoint if it's already been converted
+    if "time_embedding.linear_1.weight" in checkpoint:
+        return checkpoint
     # Some controlnet ckpt files are distributed independently from the rest of the
     # model components i.e. https://huggingface.co/thibaud/controlnet-sd21/
     if "time_embed.0.weight" in checkpoint:

ControlNet broken from_single_file
### Describe the bug

controlnet loader from_single_file was originally added via #4084
and method `ControlNet.from_single_file()` works for non-converted controlnets.

but for controlnets in safetensors format that contain already converted state_dict, it errors out.

its not reasonable to expect from user to know what is the internal dict structure of the controlnet safetensors file
before he can use it.

even worse, some of the newer controlnets are distributed as single-file-only and are already in diffusers format
which makes them impossible to load in difufsers.
for example: <https://huggingface.co/Laxhar/noob_openpose/tree/main>

this issue was already mentioned several times, each time closed as "works as designed"
when in reality its just a failure that should be addressed as an issue.
see #8474 #9208 #8614 as examples of previous issues

### Reproduction

scenario-1: works with non-converted controlnet
```python
import torch
from diffusers import ControlNetModel
from huggingface_hub import hf_hub_download
local_path = hf_hub_download(repo_id='Aptronym/SDNext', filename='ControlNet11/controlnet11Models_canny.safetensors')
cn = ControlNetModel.from_single_file(local_path, torch_dtype=torch.float16)
print(cn.__class__)
```

scenario-1: fails for majority of controlnets available on huggingface
```python
import torch
from diffusers import ControlNetModel
from huggingface_hub import hf_hub_download
local_path = hf_hub_download(repo_id='lllyasviel/sd_control_collection', filename='diffusers_xl_canny_small.safetensors')
cn = ControlNetModel.from_single_file(local_path, torch_dtype=torch.float16)
print(cn.__class__)
```
initial failure is nonsense
> OSError: stable-diffusion-v1-5/stable-diffusion-v1-5 does not appear to have a file named config.json.

whats making this worse is that SD15 and SDXL share the same `ControlNet` class which causes some
confusion on the base repo where to lookup config.
e.g,, here we're loading SDXL controlnet and error referrs to SD15 repo.

anyhow, trying to force correct config:
```py
cn = ControlNetModel.from_single_file(local_path, torch_dtype=torch.float16, config='diffusers/controlnet-canny-sdxl-1.0-small')
```

results in even worse nonsense failure during loading of state_dict:
> TypeError: is_floating_point(): argument 'input' (position 1) must be Tensor, not NoneType

### System Info

diffusers=0.32.0.dev0
python==3.12.3
torch==2.5.1+cu124

### Who can help?

@yiyixuxu @sayakpaul @DN6 @asomoza

> even worse, some of the newer controlnets are distributed as single-file-only and are already in diffusers format
which makes them impossible to load in difufsers.
for example: https://huggingface.co/Laxhar/noob_openpose/tree/main

Isn't this an actual error as it is partially in the diffusers format by not including the `config.json`?
@sayakpaul cannot blame users for not knowing internals of `diffusers`.
99% of users will attempt to download that safetensors file and load it - and it will fail.
sure, author of that specific repo should add `config.json` (and yes, request to author is already created),
but are we going to re-educate every user "don't download controlnet safetensors"?
(and there is no way to differentiate from user perspective which ones can and which cannot be downloaded)
In that case what looks like a good solution to you? I will let @DN6 to of course comment here but IMO the following could be good candidates:

* Detect if this is a `diffusers` checkpoint and if the supplied path doesn't have a `config.json`. If detected, yield a nice error message.
* Detect if this is a `diffusers` checkpoint and try to fetch a `config.json` but I am not sure how well this would play out as it involves quite a bit of guess work from what I can see.
> Detect if this is a diffusers checkpoint and try to fetch a config.json but I am not sure how well this would play out as it involves quite a bit of guess work from what I can see.

which guesswork? is it sd15 or sdxl should be easy to infer from state_dict itself.
that's how it works when loading main dffusion model using from_single_file. if i were a user, i would have same expectation here.
Hmm could be. I thought too fast. So, fetch the config, load the state dict -- looks like the workflow here.

```py
with init_empty_weights():
    config = ControlNetModel.load_config("...")
    controlnet_model = ControlNetModel.from_config(config)
    controlnet_model.load_state_dict(diffusers_state_dict)
```
> Hmm could be. I thought too fast. So, fetch the config, load the state dict -- looks like the workflow here.

btw, as a starting point, we can pass `config=...` arg to `from_single_file` and add autodetect later.
but need to make `from_single_file` actually work :)

In the first case the model config is being detected as `v1` because the checked keys for `controlnet` are for non-Diffusers type checkpoints.

https://github.com/huggingface/diffusers/blob/f6f7afa1d7c6f45f8568c5603b1e6300d4583f04/src/diffusers/loaders/single_file_utils.py#L457

https://github.com/huggingface/diffusers/blob/f6f7afa1d7c6f45f8568c5603b1e6300d4583f04/src/diffusers/loaders/single_file_utils.py#L65

`infer_diffusers_model_type` can also be extended to detect e.g. `v1` vs `xl` ControlNet.

In the second case `convert_controlnet_checkpoint` expects non-Diffusers type, the state dict it returns appears to only contain `controlnet_mid_block.weight` and `controlnet_mid_block.bias` as `None` which in turn causes the error when the state dict is loaded to the model.

`convert_controlnet_checkpoint` can return the provided checkpoint when Diffusers keys are detected.

https://github.com/huggingface/diffusers/blob/f6f7afa1d7c6f45f8568c5603b1e6300d4583f04/src/diffusers/loaders/single_file_utils.py#L1169-L1170

https://github.com/huggingface/diffusers/blob/f6f7afa1d7c6f45f8568c5603b1e6300d4583f04/src/diffusers/loaders/single_file_utils.py#L1070

https://github.com/huggingface/diffusers/blob/f6f7afa1d7c6f45f8568c5603b1e6300d4583f04/src/diffusers/models/model_loading_utils.py#L170


diff --git a/src/diffusers/utils/dynamic_modules_utils.py b/src/diffusers/utils/dynamic_modules_utils.py
index f0cf953924ad..50d9bbaac57c 100644
--- a/src/diffusers/utils/dynamic_modules_utils.py
+++ b/src/diffusers/utils/dynamic_modules_utils.py
@@ -325,7 +325,7 @@ def get_cached_module_file(
         # We always copy local files (we could hash the file to see if there was a change, and give them the name of
         # that hash, to only copy when there is a modification but it seems overkill for now).
         # The only reason we do the copy is to avoid putting too many folders in sys.path.
-        shutil.copy(resolved_module_file, submodule_path / module_file)
+        shutil.copyfile(resolved_module_file, submodule_path / module_file)
         for module_needed in modules_needed:
             if len(module_needed.split(".")) == 2:
                 module_needed = "/".join(module_needed.split("."))
@@ -333,7 +333,7 @@ def get_cached_module_file(
                 if not os.path.exists(submodule_path / module_folder):
                     os.makedirs(submodule_path / module_folder)
             module_needed = f"{module_needed}.py"
-            shutil.copy(os.path.join(pretrained_model_name_or_path, module_needed), submodule_path / module_needed)
+            shutil.copyfile(os.path.join(pretrained_model_name_or_path, module_needed), submodule_path / module_needed)
     else:
         # Get the commit hash
         # TODO: we will get this info in the etag soon, so retrieve it from there and not here.
@@ -350,7 +350,7 @@ def get_cached_module_file(
                 module_folder = module_file.split("/")[0]
                 if not os.path.exists(submodule_path / module_folder):
                     os.makedirs(submodule_path / module_folder)
-            shutil.copy(resolved_module_file, submodule_path / module_file)
+            shutil.copyfile(resolved_module_file, submodule_path / module_file)

         # Make sure we also have every file with relative
         for module_needed in modules_needed:

Replace shutil.copy with shutil.copyfile
shutil.copy copies permission bits which fails when the user who's running the script is trying to use a common cache that was generated by another user, even though the first user has read & write permissions over the cache (through Group permission for example). A real case scenario: submitting jobs on a GPU cluster accessed by multiple users with common cache directory to reduce disk and network usage.

https://github.com/huggingface/diffusers/blob/0d1d267b12e47b40b0e8f265339c76e0f45f8c49/src/diffusers/utils/dynamic_modules_utils.py#L328

Suggested solution: replace shutil.copy by shutil.copyfile which doesn't copy ownership.

Maybe related to https://github.com/huggingface/huggingface_hub/pull/1220, https://github.com/huggingface/diffusers/issues/1517, https://github.com/huggingface/huggingface_hub/issues/1141.
For info I'm using v0.23.0.
@Wauplin WDYT?
Agree with using `shutil.copyfile` yes! I didn't thought about permission issues back then. copyfile will only copy the file content but that is what we want here :)

@almarouk would you be willing to open a PR to help us?


diff --git a/impacket/examples/secretsdump.py b/impacket/examples/secretsdump.py
index 43b776218..537c45dab 100644
--- a/impacket/examples/secretsdump.py
+++ b/impacket/examples/secretsdump.py
@@ -1432,7 +1432,7 @@ def dump(self):
             userName = V[userAccount['NameOffset']:userAccount['NameOffset']+userAccount['NameLength']].decode('utf-16le')

             if userAccount['NTHashLength'] == 0:
-                logging.error('SAM hashes extraction for user %s failed. The account doesn\'t have hash information.' % userName)
+                logging.debug('The account %s doesn\'t have hash information.' % userName)
                 continue

             encNTHash = b''

SAM Dump for accounts without secrets
I realised that some defaults Windows accounts, like for example WDAGUtilityAccount, throw the following error:

![image](https://github.com/user-attachments/assets/cb6a1d68-d50a-4a0b-8689-41bf878a9ca2)

However there is no error here. WDAGUtilisatyAccount does not have a NT hash in the SAM database because this is a virtual account used to contain applications in a sandbox (for example browsers) and these featuers are not used on windows servers. Considering I never saw secretsdump failing in dumping SAM database, I believe it is possible to switch the following liens from impacket/impacket/examples/secretsdump.py:

```python
if userAccount['NTHashLength'] == 0:
    logging.error('SAM hashes extraction for user %s failed. The account doesn\'t have hash information.' % userName)
    continue
```

to

```python
if userAccount['NTHashLength'] == 0:
    logging.debug('SAM hashes extraction for user %s failed. The account doesn\'t have hash information.' % userName)
    continue
```

That way most of tools using impacket secretsdump won't have a messed up output.

Let me know what you think about this :)

Hi @Dfte,
Which configuration are you running on? I tried here with a Windows Server 2019 in azure and the account `WDAGUtilityAccount` has a hash that is printed when running secretsdump
Also rechecked that the account was disabled (according to #802), and it was
![image](https://github.com/user-attachments/assets/b96bf51c-df29-4881-b031-8927799dd7c6)
I'm running a Windows Server 2019 v1809
I have tested this against a domain controller. Now it's ovbious that there are no hashes for such accounts in the SAM database which is disabled anyway for domain controllers. But is there really a reason to display an error for such use cases considering there is no way for a local account not to have at least a default LM/NT hash ?
Hey,

mm I don't think so.
I wanted to better understand / replay that scenario to check if there's some property in those objects that could help us realize it's not an issue not having gathered the hash.
But yes, I agree with you that we shouldn't be showing this case as an error

Will be playing with it a bit more and can create a PR to change it


diff --git a/impacket/dhcp.py b/impacket/dhcp.py
index aeabb4013..dde0c3909 100644
--- a/impacket/dhcp.py
+++ b/impacket/dhcp.py
@@ -178,7 +178,7 @@ def unpackOptions(self, options):
             # size = self.calcUnpackSize(format, options[i+1:])
             size = options[i+1]
             # print i, name, format, size
-            value = self.unpack(format, options[i+2:i+2+size])
+            value = self.unpack(format, bytes(options[i+2:i+2+size]))
             answer.append((name, value))
             i += 2+size

dhcp.py: decode error  "object has no attribute 'encode'"
### Configuration

impacket version:  HEAD
Python version:  3.11
Target OS:  Linux

### Debug Output With Command String

````
		dhcp = DhcpPacket(buffer)
		print(dhcp)
``````

```
Traceback (most recent call last):
  File "/home/mdt/Source/emdete/honeypot/snooper/main.py", line 14, in learn_dhcp
    dhcp = DHCP(buffer)
           ^^^^^^^^^^^^
  File "/home/mdt/Source/oss/python/impacket/impacket/dhcp.py", line 148, in __init__
    structure.Structure.__init__(self, data, alignment)
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 87, in __init__
    self.fromString(data)
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 152, in fromString
    self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0])
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 307, in unpack
    return eval(dataClassOrCode, {}, fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/home/mdt/Source/oss/python/impacket/impacket/dhcp.py", line 179, in unpackOptions
    value = self.unpack(format, options[i+2:i+2+size])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 382, in unpack
    return dataClassOrCode(data)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/six.py", line 644, in b
    return s.encode("latin-1")
           ^^^^^^^^
AttributeError: ("'bytearray' object has no attribute 'encode'", "When unpacking field 'options | _ | b''[:0]'")
```

### PCAP

```
 b'\x01\x01\x06\x00\xa3Y\xdf\x06\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf84A\xdb2.\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00c\x82Sc5\x01\x032\x04\xc0\xa8\x00\x8c6\x04\xc0\xa8\x00\x019\x02\x02@7\x07\x01\x03\x06\x0c\x0f\x1c*<\x0cudhcp 1.36.1=\x07\x01\xf84A\xdb2.\xff\x00\x00\x00\x00\x00\x00\x00\x00'
```

### Additional context

it seems the line 381 in `impacket/structure.py` should read:

```
if (isinstance(data, bytes) or isinstance(data, bytearray)) and dataClassOrCode is b:
```

because the buffer is not bytes but bytearray. fixing this leads to the next error:

```
Traceback (most recent call last):
  File "/home/mdt/Source/emdete/honeypot/snooper/main.py", line 14, in learn_dhcp
    dhcp = DhcpPacket(buffer)
           ^^^^^^^^^^^^^^^^^^
  File "/home/mdt/Source/oss/python/impacket/impacket/dhcp.py", line 148, in __init__
    structure.Structure.__init__(self, data, alignment)
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 87, in __init__
    self.fromString(data)
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 152, in fromString
    self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0])
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 307, in unpack
    return eval(dataClassOrCode, {}, fields)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/home/mdt/Source/oss/python/impacket/impacket/dhcp.py", line 179, in unpackOptions
    value = self.unpack(format, options[i+2:i+2+size])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mdt/Source/oss/python/impacket/impacket/structure.py", line 386, in unpack
    return unpack(format, data)[0]
           ^^^^^^^^^^^^^^^^^^^^
struct.error: ('unpack requires a buffer of 4 bytes', "When unpacking field 'options | _ | b''[:0]'")

```

I also got this error, here is my code:
```
import struct
from impacket import ImpactDecoder
from impacket import dhcp
import socket

dhcp_packet = (
    b'\x01'              # Message type: Boot Request (1 byte)
    b'\x01'              # Hardware type: Ethernet (1 byte)
    b'\x06'              # Hardware address length: 6 (1 byte)
    b'\x00'              # Hops: 0 (1 byte)
    b'\x39\x03\xF3\x26'  # Transaction ID: Random (4 bytes)
    b'\x00\x00'          # Seconds elapsed: 0 (2 bytes)
    b'\x80\x00'          # Flags: 0x8000 (Broadcast) (2 bytes)
    b'\x00\x00\x00\x00'  # Client IP address: 0.0.0.0 (4 bytes)
    b'\x00\x00\x00\x00'  # Your (client) IP address: 0.0.0.0 (4 bytes)
    b'\x00\x00\x00\x00'  # Next server IP address: 0.0.0.0 (4 bytes)
    b'\x00\x00\x00\x00'  # Relay agent IP address: 0.0.0.0 (4 bytes)
    b'\x00\x26\x9e\x04\x0a\x5b'  # Client MAC address (6 bytes)
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'  # Client hardware address padding (10 bytes)
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'  # Server name padding (64 bytes)
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'   # Boot filename padding (128 bytes)
    b'\x63\x82\x53\x63'  # Magic cookie: DHCP (4 bytes)
    # DHCP Options:
    b'\x35\x01\x01'
    b'\x32\x04\xc0\xa8\x01\x64'
    b'\x33\x04\x01\xa8\x01\x64'      # Option: (53) DHCP Message Type (Discover) (3 bytes)
    # b'\x3d\x07\x01\x00\x26\x9e\x04\x0a\x5b'  # Option: (61) Client identifier (7 bytes)
    b'\x37\x01\x03\x01\x06'  # Option: (55) Parameter Request List (5 bytes)
    # b'\xff'              # End Option (1 byte)
)

dhcp_decoder = ImpactDecoder.BootpDecoder()
dhcp_packet_decoded = dhcp_decoder.decode(dhcp_packet)
print(dhcp_packet_decoded)
```
Could anyone take a look at it?
As @emdete mentioned, this error can be solved by modifying the following condition to also include `bytearray` objects https://github.com/fortra/impacket/blob/3ce41be452dfe578f7edea16bc816e4f7fabe04d/impacket/structure.py#L384

The fix _may_ cause some unwanted splash damage so further testing is required.

Another option would be to fix the method responsible for calling structure.unpack with bytearray as an argument (instead of bytes). It was recently introduced by another PR: https://github.com/fortra/impacket/commit/3f645107bb4db65fd8a328399031a257723c6bfb.

We could convert `options[...]` to `bytes` in the following line: https://github.com/fortra/impacket/blob/3ce41be452dfe578f7edea16bc816e4f7fabe04d/impacket/dhcp.py#L181

Both alternatives fix the problem


diff --git a/modin/pandas/dataframe.py b/modin/pandas/dataframe.py
index de96ea0ab26..2ce83913ebb 100644
--- a/modin/pandas/dataframe.py
+++ b/modin/pandas/dataframe.py
@@ -2074,12 +2074,12 @@ def squeeze(
         Squeeze 1 dimensional axis objects into scalars.
         """
         axis = self._get_axis_number(axis) if axis is not None else None
-        if axis is None and (len(self.columns) == 1 or len(self.index) == 1):
+        if axis is None and (len(self.columns) == 1 or len(self) == 1):
             return Series(query_compiler=self._query_compiler).squeeze()
         if axis == 1 and len(self.columns) == 1:
             self._query_compiler._shape_hint = "column"
             return Series(query_compiler=self._query_compiler)
-        if axis == 0 and len(self.index) == 1:
+        if axis == 0 and len(self) == 1:
             qc = self.T._query_compiler
             qc._shape_hint = "column"
             return Series(query_compiler=qc)

Avoid unnecessary length checks in `df.squeeze`
It is possible that when `axis=1` in squeeze we still check `len(self.index)`, which is never necessary when `axis=1`. Link to code here: https://github.com/modin-project/modin/blob/eac3c77baf456c7bd7e1e5fde81790a4ed3ebb27/modin/pandas/dataframe.py#L2074-L2084

This is an easy fix, also see https://github.com/snowflakedb/snowpark-python/pull/1767


diff --git a/python/ccxt/base/exchange.py b/python/ccxt/base/exchange.py
index 9b79354f89c5..66f8170154a4 100644
--- a/python/ccxt/base/exchange.py
+++ b/python/ccxt/base/exchange.py
@@ -382,6 +382,7 @@ def __init__(self, config={}):
         self.transactions = dict() if self.transactions is None else self.transactions
         self.ohlcvs = dict() if self.ohlcvs is None else self.ohlcvs
         self.liquidations = dict() if self.liquidations is None else self.liquidations
+        self.myLiquidations = dict() if self.myLiquidations is None else self.myLiquidations
         self.currencies = dict() if self.currencies is None else self.currencies
         self.options = self.get_default_options() if self.options is None else self.options  # Python does not allow to define properties in run-time with setattr
         self.decimal_to_precision = decimal_to_precision

binance myLiquidations uninitialized before accessed
### Operating System

Ubuntu

### Programming Languages

Python

### CCXT Version

4.4.27

### Description

Got following error while watching binance websocket.

### Code

```
2024-11-26 05:56:31,267 - 875 - exchanges.binance - ERROR - 'NoneType' object does not support item assignment
Traceback (most recent call last):
  File "/home/sytd/test/src/exchanges/future_exchange_base.py", line 118, in watch_balances
    event = await self._exchange.watch_balance()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/pro/binance.py", line 2503, in watch_balance
    return await self.watch(url, messageHash, message, type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/async_support/base/ws/fast_client.py", line 27, in handler
    self.handle_message(message)
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 34, in handle_message
    self.handle_text_or_binary_message(message.data)
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/async_support/base/ws/aiohttp_client.py", line 29, in handle_text_or_binary_message
    self.on_message_callback(self, decoded)
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/pro/binance.py", line 3966, in handle_message
    method(client, message)
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/pro/binance.py", line 3396, in handle_order_update
    self.handle_my_liquidation(client, message)
  File "/home/sytd/test/.venv/lib/python3.11/site-packages/ccxt/pro/binance.py", line 518, in handle_my_liquidation
    self.myLiquidations[symbol] = myLiquidations
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: 'NoneType' object does not support item assignment
```

Hello @sytranvn,

Thanks for reporting it, we will fix it asap
@sytranvn Btw, what's the best way of reproducing the issue?
I was just listening for balance events only. Maybe place a future position and wait for it to be liquidated.


diff --git a/qiskit/circuit/parameterexpression.py b/qiskit/circuit/parameterexpression.py
index fe786762c09..feaa0b772c7 100644
--- a/qiskit/circuit/parameterexpression.py
+++ b/qiskit/circuit/parameterexpression.py
@@ -340,7 +340,7 @@ def _apply_operation(
         either a constant or a second ParameterExpression.

         Args:
-            operation: One of operator.{add,sub,mul,truediv}.
+            operation: An operator, such as add, sub, mul, and truediv.
             other: The second argument to be used with self in operation.
             reflected: Optional - The default ordering is "self operator other".
                        If reflected is True, this is switched to "other operator self".

Doc string of `operation` in ParameterExpression._apply_operation
It says
```
operation: One of operator.{add,sub,mul,truediv}.
```
But the function is already called also with other operations, for example `pow` in `ParameterExpression.__pow__`.

Good point, and while this is not user facing it would be nice to have the dev-facing docs correct. Would you like to open a small PR to fix it?


diff --git a/aiohttp/payload.py b/aiohttp/payload.py
index 27636977774..151f9dd497b 100644
--- a/aiohttp/payload.py
+++ b/aiohttp/payload.py
@@ -101,6 +101,7 @@ def __init__(self) -> None:
         self._first: List[_PayloadRegistryItem] = []
         self._normal: List[_PayloadRegistryItem] = []
         self._last: List[_PayloadRegistryItem] = []
+        self._normal_lookup: Dict[Any, PayloadType] = {}

     def get(
         self,
@@ -109,12 +110,20 @@ def get(
         _CHAIN: "Type[chain[_PayloadRegistryItem]]" = chain,
         **kwargs: Any,
     ) -> "Payload":
+        if self._first:
+            for factory, type_ in self._first:
+                if isinstance(data, type_):
+                    return factory(data, *args, **kwargs)
+        # Try the fast lookup first
+        if lookup_factory := self._normal_lookup.get(type(data)):
+            return lookup_factory(data, *args, **kwargs)
+        # Bail early if its already a Payload
         if isinstance(data, Payload):
             return data
-        for factory, type in _CHAIN(self._first, self._normal, self._last):
-            if isinstance(data, type):
+        # Fallback to the slower linear search
+        for factory, type_ in _CHAIN(self._normal, self._last):
+            if isinstance(data, type_):
                 return factory(data, *args, **kwargs)
-
         raise LookupError()

     def register(
@@ -124,6 +133,11 @@ def register(
             self._first.append((factory, type))
         elif order is Order.normal:
             self._normal.append((factory, type))
+            if isinstance(type, Iterable):
+                for t in type:
+                    self._normal_lookup[t] = factory
+            else:
+                self._normal_lookup[type] = factory
         elif order is Order.try_last:
             self._last.append((factory, type))
         else:
@@ -159,7 +173,8 @@ def __init__(
             self._headers[hdrs.CONTENT_TYPE] = content_type
         else:
             self._headers[hdrs.CONTENT_TYPE] = self._default_content_type
-        self._headers.update(headers or {})
+        if headers:
+            self._headers.update(headers)

     @property
     def size(self) -> Optional[int]:
@@ -228,9 +243,6 @@ class BytesPayload(Payload):
     def __init__(
         self, value: Union[bytes, bytearray, memoryview], *args: Any, **kwargs: Any
     ) -> None:
-        if not isinstance(value, (bytes, bytearray, memoryview)):
-            raise TypeError(f"value argument must be byte-ish, not {type(value)!r}")
-
         if "content_type" not in kwargs:
             kwargs["content_type"] = "application/octet-stream"

@@ -238,8 +250,10 @@ def __init__(

         if isinstance(value, memoryview):
             self._size = value.nbytes
-        else:
+        elif isinstance(value, (bytes, bytearray)):
             self._size = len(value)
+        else:
+            raise TypeError(f"value argument must be byte-ish, not {type(value)!r}")

         if self._size > TOO_LARGE_BYTES_BODY:
             kwargs = {"source": self}

Payload registry has to do a linear search to find payloads
https://github.com/aio-libs/aiohttp/blob/21f5f92a755dc5ac7225b5e76f561553cf86565e/aiohttp/payload.py#L97

There is a note that its inefficent.

We might be able to clean it up by giving each payload a key as part of the base class and then doing a dict lookup instead

> We might be able to clean it up by giving each payload a key as part of the base class and then doing a dict lookup instead

Doesn't really work, because it works with isinstance checks, which could be a subclass or an abc. I thought about this previously and didn't think of any improvements. Not sure what zope.interface is though, might be worth looking into.
Maybe if we have a property that is a key thats the same for the subclassed object.. not sure if that will work.  Needs some more thought.

Not sure what you mean, the subclasses are e.g. subclass of bytes or (more realistically) a subclass of io.BufferedIO. They are not our classes, just arbitrary types that might be used to store/stream data.
I need to dig at it a bit more to come up with a solution. This one will take a bit of thought so I opened the issue so I don't forget to come back to it.
Looking at this
We can likely store the payload type() and then do a dict lookup on the type() of the incoming and we get no matches because it's subclasses, fallback to the linear search
We don't have a benchmark for simple post requests which makes this a bit more difficult to quantify what the performance drag of the linear search is.  We should add a benchmark first


diff --git a/aiohttp/payload.py b/aiohttp/payload.py
index ea50b6a38cb..9979ed269b6 100644
--- a/aiohttp/payload.py
+++ b/aiohttp/payload.py
@@ -101,6 +101,7 @@ def __init__(self) -> None:
         self._first: List[_PayloadRegistryItem] = []
         self._normal: List[_PayloadRegistryItem] = []
         self._last: List[_PayloadRegistryItem] = []
+        self._normal_lookup: Dict[Any, PayloadType] = {}

     def get(
         self,
@@ -109,12 +110,20 @@ def get(
         _CHAIN: "Type[chain[_PayloadRegistryItem]]" = chain,
         **kwargs: Any,
     ) -> "Payload":
+        if self._first:
+            for factory, type_ in self._first:
+                if isinstance(data, type_):
+                    return factory(data, *args, **kwargs)
+        # Try the fast lookup first
+        if lookup_factory := self._normal_lookup.get(type(data)):
+            return lookup_factory(data, *args, **kwargs)
+        # Bail early if its already a Payload
         if isinstance(data, Payload):
             return data
-        for factory, type in _CHAIN(self._first, self._normal, self._last):
-            if isinstance(data, type):
+        # Fallback to the slower linear search
+        for factory, type_ in _CHAIN(self._normal, self._last):
+            if isinstance(data, type_):
                 return factory(data, *args, **kwargs)
-
         raise LookupError()

     def register(
@@ -124,6 +133,11 @@ def register(
             self._first.append((factory, type))
         elif order is Order.normal:
             self._normal.append((factory, type))
+            if isinstance(type, Iterable):
+                for t in type:
+                    self._normal_lookup[t] = factory
+            else:
+                self._normal_lookup[type] = factory
         elif order is Order.try_last:
             self._last.append((factory, type))
         else:
@@ -159,7 +173,8 @@ def __init__(
             self._headers[hdrs.CONTENT_TYPE] = content_type
         else:
             self._headers[hdrs.CONTENT_TYPE] = self._default_content_type
-        self._headers.update(headers or {})
+        if headers:
+            self._headers.update(headers)

     @property
     def size(self) -> Optional[int]:
@@ -228,9 +243,6 @@ class BytesPayload(Payload):
     def __init__(
         self, value: Union[bytes, bytearray, memoryview], *args: Any, **kwargs: Any
     ) -> None:
-        if not isinstance(value, (bytes, bytearray, memoryview)):
-            raise TypeError(f"value argument must be byte-ish, not {type(value)!r}")
-
         if "content_type" not in kwargs:
             kwargs["content_type"] = "application/octet-stream"

@@ -238,8 +250,10 @@ def __init__(

         if isinstance(value, memoryview):
             self._size = value.nbytes
-        else:
+        elif isinstance(value, (bytes, bytearray)):
             self._size = len(value)
+        else:
+            raise TypeError(f"value argument must be byte-ish, not {type(value)!r}")

         if self._size > TOO_LARGE_BYTES_BODY:
             warnings.warn(