Describe the bug
In PositionalEncoding.forward(), the conditional if self.dropout is intended to skip dropout when p=0. However, self.dropout is an nn.Dropout module instance, which is always truthy regardless of the dropout probability even nn.Dropout(p=0) evaluates as True.
This means dropout is always applied during the forward pass, even when the user explicitly sets dropout=0 to disable it.
To Reproduce
import torch
from pyaptamer.aptatrans.layers._encoder import PositionalEncoding
# Create positional encoding with dropout=0 (should mean NO dropout)
pe = PositionalEncoding(d_model=32, dropout=0, max_len=10)
# The bug: self.dropout is nn.Dropout(p=0), which is truthy
print(bool(pe.dropout)) # True — always truthy!
# This means the `if self.dropout:` check always passes
# and dropout is always called, even when p=0
Expected behavior
When dropout=0, the dropout step should be effectively skipped. Since nn.Dropout(p=0) is a no-op anyway, the simplest fix is to always call self.dropout(out) unconditionally removing the buggy conditional entirely.
Additional context
nn.Dropout(p=0) is a no-op (returns input unchanged), so the bug has no observable effect on outputs currently.
- However, the buggy conditional is misleading and would cause real issues if someone changed the logic to rely on it (e.g., adding an
else branch).
- This is a well-known PyTorch anti-pattern module instances should never be used in boolean checks for "is this layer active?"
Versions
Details
Describe the bug
In
PositionalEncoding.forward(), the conditionalif self.dropoutis intended to skip dropout whenp=0. However,self.dropoutis annn.Dropoutmodule instance, which is always truthy regardless of the dropout probability evennn.Dropout(p=0)evaluates asTrue.This means dropout is always applied during the forward pass, even when the user explicitly sets
dropout=0to disable it.To Reproduce
Expected behavior
When
dropout=0, the dropout step should be effectively skipped. Sincenn.Dropout(p=0)is a no-op anyway, the simplest fix is to always callself.dropout(out)unconditionally removing the buggy conditional entirely.Additional context
nn.Dropout(p=0)is a no-op (returns input unchanged), so the bug has no observable effect on outputs currently.elsebranch).Versions
Details