Repository: huggingface/diffusers

Issue:
Rename variables from single letter to better naming
Pretty much all code in https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py still corresponds to the copy-pasted version from the beginning.

While weight names shouldn't be renamed, we should rename all single letter variables to more explicit variable naming. E.g. `h` -> `hidden_states`. 

Single letter variables are hard to deal with as search and replace doesn't work on them and are hard to read


Discussion:
Also maybe @anton-l if you'd like
That would be great @antival3nt :heart_eyes: 
@patrickvonplaten , please assign to me.
Feel free to take it @rashmimarganiatgithub - all yours! Generally as soon as someone opens a PR for an issue, we try to work together on this PR 
Hi @patrickvonplaten 
I'd be happy to work on any other variable names that need to be renamed.
Hey @daspartho ! Awesome, there are some single letter variables in `attention.py` file, feel free to work on those if you want. I'm listing the places that needs change below.

https://github.com/huggingface/diffusers/blob/b2b3b1a8ab83b020ecaf32f45de3ef23644331cf/src/diffusers/models/attention.py#L140
https://github.com/huggingface/diffusers/blob/b2b3b1a8ab83b020ecaf32f45de3ef23644331cf/src/diffusers/models/attention.py#L195
https://github.com/huggingface/diffusers/blob/b2b3b1a8ab83b020ecaf32f45de3ef23644331cf/src/diffusers/models/attention.py#L313
https://github.com/huggingface/diffusers/blob/b2b3b1a8ab83b020ecaf32f45de3ef23644331cf/src/diffusers/models/attention.py#L331

Hi @patil-suraj 
Thank you for pointing out the necessary changes; I'll work on it.
I'm just not sure what the exact variable name for the single-letter variables should be. Can you please help me?
Thanks :)
@daspartho ,**q, k, v** can be replaced with **query,key,value**. **b, c, h, w** can be replaced with **batch,channel,height,weight** . 
Thank you for clarifying, @rashmimarganiatgithub. I'll start a PR soon.
I've opened a PR for the issue :) #449 
Hi @patil-suraj 
I'd be happy to work on any additional files that require changes.
Hi @patil-suraj , @patrickvonplaten 
I would like to help with other files for better variable name changes.
Hey @i-am-epic @daspartho , there are still some places with single letter variables in `resenet.py` , feel free to work on those! I'm listing them down here.

https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L37
https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L54
https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L177
https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L239
https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/resnet.py#L329
Hi @patil-suraj,
Thank you for pointing out the changes; I'll work on it.
I'm not sure my naming analogy is perfect.
correct me with better naming convention,
x as parameter in forward function will be input_tensor and it returns output_tensor (and based on upsample, downsample or padding x can be upsample_input,downsample_input, padded_input).
Opened a PR for the issue :) #676 
I hope @i-am-epic doesn't mind me jumping in.
@daspartho no issues it would had been my first contribution.
Hi! @i-am-epic, 
You could work on this one. Happy first contribution :)
https://github.com/huggingface/diffusers/blob/f10576ad5c9dbd17c59e0af12a26583e3a540e20/src/diffusers/models/resnet.py#L329
> Hi! @i-am-epic, You could work on this one. Happy first contribution :)
> 
> https://github.com/huggingface/diffusers/blob/f10576ad5c9dbd17c59e0af12a26583e3a540e20/src/diffusers/models/resnet.py#L329
@daspartho can u help me what's wrong in my pull request #677
thanks :)
Opened new  PR #677 
Please do verify, thanks.
Is this still open? I see there is a merged PR above. I can jump on it if it is still open

Resolving patch:
diff --git a/src/diffusers/models/resnet.py b/src/diffusers/models/resnet.py
--- a/src/diffusers/models/resnet.py
+++ b/src/diffusers/models/resnet.py
@@ -95,9 +95,9 @@ def __init__(self, channels, use_conv=False, out_channels=None, padding=1, name=
             assert self.channels == self.out_channels
             self.conv = nn.AvgPool1d(kernel_size=stride, stride=stride)
 
-    def forward(self, x):
-        assert x.shape[1] == self.channels
-        return self.conv(x)
+    def forward(self, inputs):
+        assert inputs.shape[1] == self.channels
+        return self.conv(inputs)
 
 
 class Upsample2D(nn.Module):
@@ -431,13 +431,13 @@ def __init__(self, pad_mode="reflect"):
         self.pad = kernel_1d.shape[1] // 2 - 1
         self.register_buffer("kernel", kernel_1d.T @ kernel_1d, persistent=False)
 
-    def forward(self, x):
-        x = F.pad(x, (self.pad,) * 4, self.pad_mode)
-        weight = x.new_zeros([x.shape[1], x.shape[1], self.kernel.shape[0], self.kernel.shape[1]])
-        indices = torch.arange(x.shape[1], device=x.device)
-        kernel = self.kernel.to(weight)[None, :].expand(x.shape[1], -1, -1)
+    def forward(self, inputs):
+        inputs = F.pad(inputs, (self.pad,) * 4, self.pad_mode)
+        weight = inputs.new_zeros([inputs.shape[1], inputs.shape[1], self.kernel.shape[0], self.kernel.shape[1]])
+        indices = torch.arange(inputs.shape[1], device=inputs.device)
+        kernel = self.kernel.to(weight)[None, :].expand(inputs.shape[1], -1, -1)
         weight[indices, indices] = kernel
-        return F.conv2d(x, weight, stride=2)
+        return F.conv2d(inputs, weight, stride=2)
 
 
 class KUpsample2D(nn.Module):
@@ -448,13 +448,13 @@ def __init__(self, pad_mode="reflect"):
         self.pad = kernel_1d.shape[1] // 2 - 1
         self.register_buffer("kernel", kernel_1d.T @ kernel_1d, persistent=False)
 
-    def forward(self, x):
-        x = F.pad(x, ((self.pad + 1) // 2,) * 4, self.pad_mode)
-        weight = x.new_zeros([x.shape[1], x.shape[1], self.kernel.shape[0], self.kernel.shape[1]])
-        indices = torch.arange(x.shape[1], device=x.device)
-        kernel = self.kernel.to(weight)[None, :].expand(x.shape[1], -1, -1)
+    def forward(self, inputs):
+        inputs = F.pad(inputs, ((self.pad + 1) // 2,) * 4, self.pad_mode)
+        weight = inputs.new_zeros([inputs.shape[1], inputs.shape[1], self.kernel.shape[0], self.kernel.shape[1]])
+        indices = torch.arange(inputs.shape[1], device=inputs.device)
+        kernel = self.kernel.to(weight)[None, :].expand(inputs.shape[1], -1, -1)
         weight[indices, indices] = kernel
-        return F.conv_transpose2d(x, weight, stride=2, padding=self.pad * 2 + 1)
+        return F.conv_transpose2d(inputs, weight, stride=2, padding=self.pad * 2 + 1)
 
 
 class ResnetBlock2D(nn.Module):
@@ -664,13 +664,13 @@ def __init__(self, inp_channels, out_channels, kernel_size, n_groups=8):
         self.group_norm = nn.GroupNorm(n_groups, out_channels)
         self.mish = nn.Mish()
 
-    def forward(self, x):
-        x = self.conv1d(x)
-        x = rearrange_dims(x)
-        x = self.group_norm(x)
-        x = rearrange_dims(x)
-        x = self.mish(x)
-        return x
+    def forward(self, inputs):
+        intermediate_repr = self.conv1d(inputs)
+        intermediate_repr = rearrange_dims(intermediate_repr)
+        intermediate_repr = self.group_norm(intermediate_repr)
+        intermediate_repr = rearrange_dims(intermediate_repr)
+        output = self.mish(intermediate_repr)
+        return output
 
 
 # unet_rl.py
@@ -687,10 +687,10 @@ def __init__(self, inp_channels, out_channels, embed_dim, kernel_size=5):
             nn.Conv1d(inp_channels, out_channels, 1) if inp_channels != out_channels else nn.Identity()
         )
 
-    def forward(self, x, t):
+    def forward(self, inputs, t):
         """
         Args:
-            x : [ batch_size x inp_channels x horizon ]
+            inputs : [ batch_size x inp_channels x horizon ]
             t : [ batch_size x embed_dim ]
 
         returns:
@@ -698,9 +698,9 @@ def forward(self, x, t):
         """
         t = self.time_emb_act(t)
         t = self.time_emb(t)
-        out = self.conv_in(x) + rearrange_dims(t)
+        out = self.conv_in(inputs) + rearrange_dims(t)
         out = self.conv_out(out)
-        return out + self.residual_conv(x)
+        return out + self.residual_conv(inputs)
 
 
 def upsample_2d(hidden_states, kernel=None, factor=2, gain=1):


Test patch: