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

Need to restrict the value of alpha to be positive for elu operation #383

Closed
lisa0314 opened this issue May 4, 2023 · 7 comments
Closed

Comments

@lisa0314
Copy link

lisa0314 commented May 4, 2023

According to the elu definition of wikipedia and the paper, the alpha should be positive.

This issue was raised by @huningxin in WebNN Chromium CL review. Thanks Ningxin!

@huningxin
Copy link
Contributor

Thanks for opening this issue, @lisa0314 !

The quick survey of the native ML APIs support:

/cc @wacky6 @fdwr

@fdwr
Copy link
Collaborator

fdwr commented May 5, 2023

Hmm, ML API's typically don't restrict floating point inputs (more often they reject invalid integer quantities like bad axes/sizes), and for the sake of broader compat, we probably shouldn't unnecessarily reject values that work in other libraries. e.g.:

import torch

x = torch.tensor([-3, 3], dtype = torch.float32)
s = torch.nn.ELU(alpha = -1) # ✅ works, and even float('nan') is allowed.
y = s(x)

print("value:", y)
print("shape:", y.shape)
print("dtype:", y.dtype)

# value: tensor([0.9502, 3.0000])
# shape: torch.Size([2])
# dtype: torch.float32

The plot has a kink, but otherwise looks non-degenerate:

UPDATE: Fixed graph after Ningxin's comment
image

If we rejected negative values, and someone was somehow using it for compat reasons, what would be the decomposition? I suppose you could fall back to elementwiseIf(greater(x, 0), x, scale * (exp(x) - 1)), now that elementwiseIf and greater are pending operators.

@huningxin
Copy link
Contributor

@fdwr

When alpha is -1, the calculation becomes -1 * ( exp(x) - 1), and the plot would look like

if someone did need a negative alpha scale coefficient for negative inputs for whatever unusual compat reason, what would be the decomposition?

I think the decomposition sample in current spec still works for negative alpha, e.g. -1:

return builder.add(
          builder.max(builder.constant(0), x),
          builder.mul(
            builder.constant(-1), // alpha = -1
            builder.sub(
              builder.exp(builder.min(builder.constant(0), x)),
              builder.constant(1))));

(BTW, there is a typo in the elu sample code, I'll fix it.)

@fdwr
Copy link
Collaborator

fdwr commented May 6, 2023

@huningxin: Doh, fixed graph. 👍 Yep, you're right, because the scale multiply occurs after the min, the existing decomposition works fine.

I think elu should just return a result equivalent to its decomposition elementwiseIf(greater(x, 0), x, scale * (exp(x) - 1)), whether the alpha scale is positive or negative. Similarly for NaN's, I'd just follow standard IEEE behavior and propagate it through. We don't have special checks for NaN's with the other floating-point activation/elementwise operators, and consider that if scale was actually a tensor instead of a single scalar, we wouldn't bother to scan every value inside it.

@huningxin
Copy link
Contributor

A summary of Chromium prototyping:

@fdwr
Copy link
Collaborator

fdwr commented Sep 25, 2024

IIRC, we agreed to close this one in TPAC2024?

@inexorabletash
Copy link
Member

Yes, discussion at https://www.w3.org/2024/09/23-webmachinelearning-minutes.html#e89f

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

No branches or pull requests

5 participants