Skip to content

[BUG] PositionalEncoding.forward() dropout check is always truthy #654

@onkar717

Description

@onkar717

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
pyaptamer 0.1.0a1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions