Repository: huggingface/diffusers
Pull request: feat: add `act_fn` param to `OutValueFunctionBlock`
# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

(Hopefully) fixes #1287 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md)?
- [x] Did you read our [philosophy doc](https://github.com/huggingface/diffusers/blob/main/PHILOSOPHY.md) (important for complex PRs)?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/diffusers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Core library:

- Schedulers: @williamberman and @patrickvonplaten
- Pipelines:  @patrickvonplaten and @sayakpaul
- Training examples: @sayakpaul and @patrickvonplaten
- Docs: @stevenliu and @yiyixu
- JAX and MPS: @pcuenca
- Audio: @sanchit-gandhi
- General functionalities: @patrickvonplaten and @sayakpaul

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- transformers: [different repo](https://github.com/huggingface/transformers)
- safetensors: [different repo](https://github.com/huggingface/safetensors)

-->

Request for Review: @natolambert 


Problem statement:
Update UNet1D Tests to not use Mish
**Is your feature request related to a problem? Please describe.**
Currently the `UNet1DTests` heavily rely on mish because they were based on a re-implementation of a model. This is not needed generally, so it would be good to add another 1d model just for tests so we can delete a lot of the tests that look like this.

```
    @unittest.skipIf(torch_device == "mps", "mish op not supported in MPS")
    def test_determinism(self):
        super().test_determinism()
```

Hints from issue discussion:
Hi @natolambert, wondering if I could take up this issue. And do we essentially want _some_ 1D model that can run without requiring mish? Thanks!
Yeah @thehimalayanleo that's exactly right. It's preferable that it has an application, but not needed.

The RL tests can stay as is, it's just that a lot of the `UNet1D` tests were added in the RL PR, so there can be some quality of life improvements there 🤗.
Thank you, @natolambert. I will take a look over the weekend and submit a PR.
The activation function as well should be an optional param until Mish is supported on M1. I am unable to run training even with MPS fallback enabled 
@natolambert could you please explain how to fix this. This might seem stupid but doesn't simply changing the activation to `swish` in `prepare_init_args_and_inputs_for_common` solve the problem (and updating the `OutValueFunctionBlock` to not have `mish`)?

Ref: #3994

Commit message: Merge branch 'main' into saurav/unet1d-mish-fix

Patch to review:
diff --git a/src/diffusers/models/unet_1d_blocks.py b/src/diffusers/models/unet_1d_blocks.py
index 3c04bffeeacc..60237bd41edd 100644
--- a/src/diffusers/models/unet_1d_blocks.py
+++ b/src/diffusers/models/unet_1d_blocks.py
@@ -235,12 +235,12 @@ def forward(self, hidden_states, temb=None):
 
 
 class OutValueFunctionBlock(nn.Module):
-    def __init__(self, fc_dim, embed_dim):
+    def __init__(self, fc_dim, embed_dim, act_fn):
         super().__init__()
         self.final_block = nn.ModuleList(
             [
                 nn.Linear(fc_dim + embed_dim, fc_dim // 2),
-                nn.Mish(),
+                get_activation(act_fn),
                 nn.Linear(fc_dim // 2, 1),
             ]
         )
@@ -652,5 +652,5 @@ def get_out_block(*, out_block_type, num_groups_out, embed_dim, out_channels, ac
     if out_block_type == "OutConv1DBlock":
         return OutConv1DBlock(num_groups_out, out_channels, embed_dim, act_fn)
     elif out_block_type == "ValueFunction":
-        return OutValueFunctionBlock(fc_dim, embed_dim)
+        return OutValueFunctionBlock(fc_dim, embed_dim, act_fn)
     return None
diff --git a/tests/models/test_models_unet_1d.py b/tests/models/test_models_unet_1d.py
index 99a243e911c9..410ff2a8df87 100644
--- a/tests/models/test_models_unet_1d.py
+++ b/tests/models/test_models_unet_1d.py
@@ -89,7 +89,7 @@ def prepare_init_args_and_inputs_for_common(self):
             "mid_block_type": "MidResTemporalBlock1D",
             "down_block_types": ("DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D", "DownResnetBlock1D"),
             "up_block_types": ("UpResnetBlock1D", "UpResnetBlock1D", "UpResnetBlock1D"),
-            "act_fn": "mish",
+            "act_fn": "swish",
         }
         inputs_dict = self.dummy_input
         return init_dict, inputs_dict


Code review comments:
diff_hunk: @@ -235,12 +235,12 @@ def forward(self, hidden_states, temb=None):
 
 
 class OutValueFunctionBlock(nn.Module):
-    def __init__(self, fc_dim, embed_dim):
+    def __init__(self, fc_dim, embed_dim, act_fn):
path: src/diffusers/models/unet_1d_blocks.py
text: @user1:
```suggestion
    def __init__(self, fc_dim, embed_dim, act_fn="mish"):
```