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

StackAlongDimension does not return correct result #108615

Open
JohnMasen opened this issue Oct 7, 2024 · 1 comment · May be fixed by #108620
Open

StackAlongDimension does not return correct result #108615

JohnMasen opened this issue Oct 7, 2024 · 1 comment · May be fixed by #108620
Labels
area-System.Numerics.Tensors in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@JohnMasen
Copy link

Description

StackAlongDimension always use the first parameter to calculate. please see the [Other information] for detailed analyze.

Reproduction Steps

use the code as below

using System.Numerics.Tensors;

#pragma warning disable SYSLIB5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
int[] f1 = [1, 2, 3, 4];
int[] f2 = [10, 20, 30, 40];

var v1=Tensor.Create(f1, [2,2]);
var v2 = Tensor.Create(f2, [2,2]);
var v3 = StackAlongDimension1(1,[v1,v2]);

Console.WriteLine(v3.ToString(9, 9, 9));

Expected behavior

output should be

[2x2x2], type = System.Int32, isPinned = False
{
{1,2}
{10,20}
{3,4}
{30,40}
}

Actual behavior

output is

[2x2x2], type = System.Int32, isPinned = False
{
{1,2}
{1,2}
{3,4}
{3,4}
}

Regression?

No response

Known Workarounds

No response

Configuration

VS 2022, .NET 8
Windows 11 [Version 10.0.26100.1742]
x64

Other information

by dive into the source code, I found the code is always using the first tensor for stacking.

public static Tensor<T> StackAlongDimension<T>(int dimension, params ReadOnlySpan<Tensor<T>> tensors)
        {
            if (tensors.Length < 2)
                ThrowHelper.ThrowArgument_StackTooFewTensors();

            for (int i = 1; i < tensors.Length; i++)
            {
                if (!TensorHelpers.AreLengthsTheSame<T>(tensors[0], tensors[i]))
                    ThrowHelper.ThrowArgument_StackShapesNotSame();
            }

            if (dimension < 0)
                dimension = tensors[0].Rank - dimension;

            Tensor<T>[] outputs = new Tensor<T>[tensors.Length];
            for (int i = 0; i < tensors.Length; i++)
            {
                outputs[i] = Tensor.Unsqueeze(tensors[0], dimension);    **<--it always use the first tensor**
            }
            return Tensor.ConcatenateOnDimension<T>(dimension, outputs);
        }

I created a PoC version, this version works as exptected.

Tensor<T> StackAlongDimension1<T>(int dimension,Tensor<T>[] tensors)
{

    if (dimension < 0)
    {
        dimension = tensors[0].Rank - dimension;
    }

    Tensor<T>[] array = new Tensor<T>[tensors.Length];
    for (int i = 0; i < tensors.Length; i++)
    {
        array[i] = tensors[i].Unsqueeze(dimension);   **<--use the correct tensor** 
    }
    return Tensor.ConcatenateOnDimension<T>(dimension, array);
}
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Oct 7, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 7, 2024
@vcsjones vcsjones added area-System.Numerics.Tensors and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Oct 7, 2024
Copy link
Contributor

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

@kasperk81 kasperk81 linked a pull request Oct 7, 2024 that will close this issue
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Oct 7, 2024
@jeffhandley jeffhandley added this to the 10.0.0 milestone Oct 8, 2024
@jeffhandley jeffhandley removed the untriaged New issue has not been triaged by the area owner label Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Numerics.Tensors in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants