Skip to content

fix: string find checks are unpythonic and duplicated - #28

Open
andrewwhitecdw wants to merge 1 commit into
NVIDIA:mainfrom
andrewwhitecdw:codequality/utils-string-find-checks-are-unpythonic-and
Open

fix: string find checks are unpythonic and duplicated#28
andrewwhitecdw wants to merge 1 commit into
NVIDIA:mainfrom
andrewwhitecdw:codequality/utils-string-find-checks-are-unpythonic-and

Conversation

@andrewwhitecdw

Copy link
Copy Markdown

This PR addresses the following issue in utils.py: string find checks are unpythonic and duplicated.

Changes

  • utils.py: string find checks are unpythonic and duplicated.

Details

--- a/utils.py
+++ b/utils.py
@@ -31,13 +31,13 @@ def plot_spectrogram_clipped(spectrogram, clip_max=2.0):
 
 def init_weights(m, mean=0.0, std=0.01):
     classname = m.__class__.__name__
-    if classname.find("Conv") != -1:
+    if "Conv" in classname:
         m.weight.data.normal_(mean, std)
 
 
 def apply_weight_norm(m):
     classname = m.__class__.__name__
-    if classname.find("Conv") != -1:
+    if "Conv" in classname:
         weight_norm(m)
 
 

Tests

  • tests/test_conv_substring_check.py
--- /dev/null
+++ b/tests/test_conv_substring_check.py
@@ -0,0 +1,20 @@
+import torch.nn as nn
+from utils import apply_weight_norm, init_weights
+
+
+def test_init_weights_and_weight_norm_apply_only_to_conv_modules():
+    conv = nn.Conv1d(1, 1, 3)
+    linear = nn.Linear(3, 3)
+
+    init_weights(conv, std=0.5)
+    init_weights(linear, std=0.5)
+
+    # Conv weights are re-initialized; linear weights stay at zero.
+    assert conv.weight.std().item() > 0
+    assert linear.weight.std().item() == 0
+
+    apply_weight_norm(conv)
+    apply_weight_norm(linear)
+
+    # Weight normalization is applied only to Conv modules.
+    assert hasattr(conv, "weight_g")
+    assert not hasattr(linear, "weight_g")

@andrewwhitecdw

Copy link
Copy Markdown
Author

Closing per adversarial audit: branch contains off-scope or not-a-bug changes.

@andrewwhitecdw
andrewwhitecdw deleted the codequality/utils-string-find-checks-are-unpythonic-and branch August 2, 2026 14:03
@andrewwhitecdw
andrewwhitecdw restored the codequality/utils-string-find-checks-are-unpythonic-and branch August 17, 2026 20:31
@andrewwhitecdw

Copy link
Copy Markdown
Author

script closed on accident

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant