Skip to content

TensorPrimitives.Exp<double> returns negative infinity instead of 0 for doubles between approximately -709.5 and -709.7 #107838

Closed

Description

@BarionLP

Description

TensorPrimitives.Exp returns negative infinity instead of 0 for doubles between approximately -709.5 and -709.7.
When using Math.Exp() it correctly returns 0.
when using float also everything is fine.
I'm using System.Numerics.Tensors version 9.0.0-rc.1.24431.7

Reproduction Steps

double value = -709.5; //with -709.4 and -709.8 everything works fine
double[] test = [value, value, value, value, value];
double[] destination = new double[test.Length];
TensorPrimitives.Exp<double>(test, destination);

Console.WriteLine(string.Join(' ', destination));

Expected behavior

the above code should output 0 or values close to 0

Actual behavior

Output on my machine: -Infinity -Infinity -Infinity -Infinity -Infinity

Regression?

i haven't tried in the preview versions

Known Workarounds

No response

Configuration

.NET 9 rc1
Windows 10
x64 (AMD)
Simd uses 256bits (4 doubles)

Other information

No response

Activity

  1. added
    untriaged
    on Sep 15, 2024
  2. dotnet-policy-service commented on Sep 15, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics
    See info in area-owners.md if you want to be subscribed.

  3. BarionLP commented on Sep 15, 2024

    @BarionLP
    ContributorAuthor

    Same on my Surface Pro X (ARM64, 128bit simd) and an old HP ProBook (Intel Core i7 x64, 256bit simd)

  4. added this to the 9.0.0 milestone on Sep 15, 2024
  5. removed
    untriaged
    on Sep 15, 2024
  6. stephentoub commented on Sep 16, 2024

    @stephentoub
    Member
  7. stephentoub commented on Sep 16, 2024

    @stephentoub
    Member

    Something about the vectorized implementation of Exp, e.g. this:

    using System.Numerics.Tensors;
    
    for (int length = 1; length <= 2; length++)
    {
        Console.WriteLine($"Length: {length}");
        double[] values = new double[length];
        double[] results = new double[values.Length];
    
        values.AsSpan().Fill(-709.7);
        TensorPrimitives.Exp<double>(values, results);
    
        Console.WriteLine(Math.Exp(values[0]));
        Console.WriteLine(results[0]);
    
        Console.WriteLine();
    }

    outputs this (vectorization will kick in for at least two doubles):

    Length: 1
    6.04235438695844E-309
    6.04235438695844E-309
    
    Length: 2
    6.04235438695844E-309
    -∞
    
  8. self-assigned this
    on Sep 16, 2024
  9. added
    in-pr
    on Sep 16, 2024
  10. BarionLP commented on Sep 16, 2024

    @BarionLP
    ContributorAuthor

    I would appreciate if you can explain what happened here.
    I love low level performance and would like to learn more about it.

  11. dotnet-policy-service commented on Sep 17, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics-tensors
    See info in area-owners.md if you want to be subscribed.

  12. stephentoub commented on Sep 17, 2024

    @stephentoub
    Member

    I would appreciate if you can explain what happened here.

    The code has a fallback code path for edge case values, which these are. The vectorized implementation of that fallback is producing different answers than the scalar implementation.

  13. BarionLP commented on Oct 17, 2024

    @BarionLP
    ContributorAuthor

    can confirm fix in .NET 9 RC 2

  14. locked and limited conversation to collaborators on Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Numerics.Tensorsin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions