Skip to content

native_dropout does not scale the kept elements in training mode #25899

Backlog

Description

@AxelNoun

🐛 Describe the bug

In training mode the portable native_dropout.out kernel (kernels/portable/cpu/op_native_dropout.cpp) zeroes the dropped elements but returns the kept ones unchanged. ATen returns x * mask / (1 - p), so with ExecuTorch the output mean is 1 - p times the eager one. It is the only kernel for this op, and F.dropout(training=True) and nn.Dropout in train() mode lower to it. I found this by reading the kernel and confirmed it against eager.

import torch
from executorch.exir import to_edge
from executorch.runtime import Runtime

class M(torch.nn.Module):
    def forward(self, x):
        return torch.nn.functional.dropout(x, 0.5, training=True)

x = torch.rand(10000) + 1.0
prog = to_edge(torch.export.export(M(), (x,))).to_executorch()
(out,) = Runtime.get().load_program(prog.buffer).load_method("forward").execute([x])
kept = out != 0
print((out[kept] / x[kept]).unique())  # eager: tensor([2.])
print(out.mean() / x.mean())           # eager: about 1.0
tensor([1.])
tensor(0.4973)

The existing tests only cover train=False and p equal to 0 or 1, where no scaling happens. I have a fix with a test and will open a PR linked to this issue.

Versions

ExecuTorch built from main at 035df43, PyTorch 2.15.0+cpu, Python 3.12.3, Ubuntu 24.04 on WSL2, x86_64.

This issue was written with the help of Claude Code (Anthropic); I verified the reproduction.

Activity

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

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