Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPU LayerNormalization Produces NaN for Inputs with Small Variances #21935

Open
manipopopo opened this issue Aug 30, 2024 · 1 comment
Open
Labels
stale issues that have not been addressed in a while; categorized by a bot

Comments

@manipopopo
Copy link

manipopopo commented Aug 30, 2024

Describe the issue

The CPU implementation of LayerNormalization occasionally produces nan when computing the square root of variance. This occurs when the input variance is extremely small, potentially leading to negative sqrt inputs due to numerical precision errors.

To reproduce

import io
import onnxruntime as ort
import torch
from torch import nn

x = torch.full((256,), 1234.0)

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer_norm = nn.LayerNorm(256)
    
    def forward(self, x):
        return self.layer_norm(x)

model = Model()
onnx_program = torch.onnx.dynamo_export(model, x)
b = io.BytesIO()
onnx_program.save(b)

sess = ort.InferenceSession(b.getvalue())
print(sess.run(None, {sess.get_inputs()[0].name: x.numpy()})[0])  # nan

Urgency

No response

Platform

Windows

OS Version

Win11

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.19.0

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

Copy link
Contributor

This issue has been automatically marked as stale due to inactivity and will be closed in 30 days if no further activity occurs. If further support is needed, please provide an update and/or more details.

@github-actions github-actions bot added the stale issues that have not been addressed in a while; categorized by a bot label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale issues that have not been addressed in a while; categorized by a bot
Projects
None yet
Development

No branches or pull requests

1 participant