Skip to content

Tensors in .NET #98323

Open
Open

Description

@ericstj

Goal: Provide a type for use as both exchange and interop that represents multi-dimensional data if a single primitive Type. Implement arithmetic and linear algebra operations so that the type can serve as a sufficient basis for data preparation and as an input and output to neural networks.

  • Explore memory layouts and how they play into interop
  • Explore high-level Tensor design
    • Abstract? Type heirarchy? Construction patterns.
    • Dense vs sparse
    • Slices
    • Dimension order
    • Expected outcome: design document draft in dotnet/designs
  • Explore Tensor arithmetic design
  • Explore Tensor construction design
  • Explore Tensor loading and saving
  • TODO: Further break down Tensor implementation into smaller chunks
  • Tensor API proposal
    • Expected outcome: API review
  • Tensor core types implementation
    • Expected outcome: PR
  • TODO: Further break down Tensor implementation into smaller chunks
  • Performance tests
  • Interop with OnnxRuntime
  • Interop with TorchSharp
  • Expose in ML.NET

Activity

  1. added
    Epic
    on Feb 12, 2024
  2. ghost added
    untriaged
    on Feb 12, 2024
  3. ghost added
    needs-area-label
    on Feb 12, 2024
  4. hez2010 commented on Feb 13, 2024

    @hez2010
    Contributor

    I hope we can take #89730 into consider together as it's critical for achieving high performance linear algebra operations on arbitrary ND tensors. Such feature has been a fundamental of libraries like the native implementation part of pytorch.

  5. removed
    needs-area-label
    on Feb 13, 2024
  6. Shuenhoy commented on Feb 14, 2024

    @Shuenhoy

    I found my original comments somehow away from my actual intentions. The key point is not ET, layz evaluation, or any specific techniques. It is the ability of eliminating intermediate arrays (known as deforestation, or fusion https://en.wikipedia.org/wiki/Deforestation_(computer_science)). Users can always use lowlevel primitives to manually archive this. But if the highlevel api can not handle this, its usages will be largely limited.


    original comments

    For the arithmetic and linear algebra operations, I suggest considering the "expression template"(ET) techniques commonly used in C++ world (eg. eigen3 and blaze), which is a kind of lazy evaluations that can reduce the allocation of temp object. This is helpful especially for a GC language.

    For a quick impression, v1 + v2 in ET will produce a VecAdd<Vec, Vec> instead of a Vec. Similarly, m * (v1 + v2) will produce a MatMul<Mat, VecAdd<Vec, Vec>>. The computation is encoded in the type, and will get evaluated when assigned to a concrete Vec type.

    Last month, as part of attemp at rebasing my research from C++ to .NET, I made a experiment with ET in .NET via generics: https://github.com/Shuenhoy/DotnetETExp . The (not sufficient but illustrative) results look promising and suggest ET may also be helpful in .NET world.

    However, there are serveral barries that prevent me further investigating:

    // ...
    member (*.) (left: 'scalar & #INumberBase<'scalar>, right: 'MatExp & #IMatExp<'matExp, 'scalar> ) = // ...
    member (*.) (left: 'matExp & #IMatExp<'matExp, 'scalar>, right:  'scalar & #INumberBase<'scalar> ) = // ...

    There is an workaround to use a wrapper struct like type MatExp<'inner, 'scalar when IMatExp<'inner> and INumberBase<'scalar>>. But then we have to manually do the wrap.

    Some other features like existential types (dotnet/csharplang#5556) may also be useful. But I cannot provide more information for now. In case someone is interested, I have uploaded my attempts with F# here https://github.com/Shuenhoy/Furzn/ .

    These feature requrests have been existed for a while and some may require changes in runtime and even metadata. I do not expect they can be all implemented any sooner. But I hope .NET foundation can take a closer look on them for a potentialy better presentation of Tensors and Linear algebra in .NET.

  7. tannergooding commented on Feb 14, 2024

    @tannergooding
    Member

    Graph optimizations like that are a separate/independent concern and are not something that should be part of the default type experience.

    None of the major tensor libraries force such handling. They all allow trivial direct usage and provide a separate way to do lazy evaluation over an expression tree in order to allow dynamic code generation for additional performance.

  8. 5 remaining items

  9. removed
    untriaged
    on Mar 21, 2024
  10. GeorgeS2019 commented on Apr 6, 2024

    @GeorgeS2019

    Interop with TorchSharp

    There are MANY hundreds of books written in python which TorchSharp can leverage

    When dealing with Tensors in .NET, only very advanced users understand what to do with them in .NET

    I urge more discussions using TorchSharp as context so we can see and then share where Tensors in .NET is attempting to acheive.

    What kinds of gaps and use cases that demand Tensors in .NET?

    The whole concept of why we need to do this and how the goals not possible with e.g. TorchSharp is not very clear.

    The .NET community is SO FAR behind compared to python.

    Introducing something so that .NET community has something to play with without helping the communtiy to see THE WHOLE is making us nervous

  11. modified the milestones: 9.0.0, 10.0.0 on Aug 12, 2024
  12. tannergooding commented on Aug 12, 2024

    @tannergooding
    Member

    Moving remaining work here to .NET 10. We made significant progress in .NET 9

  13. jeffhandley commented on Jul 25, 2025

    @jeffhandley
    Member

    The API surface will be stabilized in .NET 10 but the remaining items in the issue description will carry forward into .NET 11 follow-up.

  14. modified the milestones: 10.0.0, 11.0.0 on Jul 25, 2025
  15. GeorgeS2019 commented on Jul 26, 2025

    @GeorgeS2019

    TODO in .NET 11:

    • Further break down Tensor implementation into smaller chunks

    • Performance tests

    • Interop with OnnxRuntime

    • Interop with TorchSharp

    • Expose in ML.NET

  16. vpenades commented on Dec 10, 2025

    @vpenades

    if this is becoming a wishlist, my votes go to interop with onnxruntime, and more helper methods related to Image-To-Tensor and Tensor-To-Image methods

    I'm already doing some stuff on my own, as in, filling a tensor with an image as a source.

  17. modified the milestones: 11.0.0, Future on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Epicarea-System.Numerics.Tensors

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions