huggingface / huggingface/diffusers
Fix skipped `test_model_xattn_padding` test by updating mask padding logic
- Dominant language
- Python
- Stars
- 34.5k
- Forks
- 7.3k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 91
Description
### **Title:**
Fix skipped `test_model_xattn_padding` test by updating mask padding logic
### **Description:**
Currently, there is a skipped test in `tests/models/unets/test_models_unet_2d_condition.py` regarding cross-attention mask padding (`test_model_xattn_padding`).
The test is currently skipped with the following reason:
> *"we currently pad mask by target_length tokens (what unclip needs), whereas stable-diffusion's cross-attn needs to instead pad by remaining_length."*
This is reflected by a dangling `TODO` in `src/diffusers/models/attention_processor.py` inside `prepare_attention_mask` (around line 740):
```python
# TODO: for pipelines such as stable-diffusion, padding cross-attn mask:
# we want to instead pad by (0, remaining_length), where remaining_length is:
# remaining_length: int = target_length - current_length
# TODO: re-enable tests/models/test_models_unet_2d_condition.py#test_model_xattn_padding
attention_mask = F.pad(attention_mask, (0, target_length), value=0.0)
```
### **Expected Behavior / Proposed Solution:**
1. Update `prepare_attention_mask` in `attention_processor.py` (and any other relevant `attention.py` files) to pad the mask by `remaining_length = target_length - current_length`.
2. Ensure the MPS `torch.zeros()` hack is also updated to pad by `remaining_length` rather than `target_length`.
3. Verify that these changes do not break existing `unclip` tests/pipelines.
4. Remove `@pytest.mark.skip` from `test_model_xattn_padding` in `tests/models/unets/test_models_unet_2d_condition.py` to re-enable it.
### **Relevant Files:**
- `src/diffusers/models/attention_processor.py`
- `tests/models/unets/test_models_unet_2d_condition.py`
@sayakpaul If this is indeed technical debt that needs fixing, I would be happy to put together a PR to update the logic and re-enable the test myself!
Contributor guide
Research direction
Read prepare_attention_mask in src/diffusers/models/attention_processor.py and the skipped test in tests/models/unets/test_models_unet_2d_condition.py first; trace the current_length, target_length, and MPS padding paths. Run test_model_xattn_padding and the relevant unclip tests or pipelines; done means the test is re-enabled and passes without regressions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100