Skip to content

torch.deleters performance issue on .NET framework cpu target #1435

Open

Description

@hglee

I discovered this performance issue while using the MNIST dataset.

This only happens with the combination of .NET framework and cpu target. It does not happen with .NET target or cuda.

This is a minimal reproducible source.

            // match to MNIST dataset size
            var size = 70000;

            var tensors = new List<torch.Tensor>(size);

            var dev = new torch.Device("cpu");

            for (int i = 0; i < size; ++i)
            {
                tensors.Add(torch.tensor(new[] { 1.0f }, device: dev));
            }

            Console.WriteLine(tensors.Count);

            foreach (var tensor in tensors)
            {
                tensor.Dispose();
            }

            tensors.Clear();

            Console.WriteLine(tensors.Count);

The profiler indicates ConcurrentDictionary.TryAdd() and ConcurrentDictionary.TryRemove(), but it looks like MulticastDelegate.Equals() is the problem.

Image

Image

For .NET framework and cuda combination, it seems like removed directly on _tensor_generic.

Image

Activity

  1. hglee commented on Jan 13, 2025

    @hglee
    Author

    Maybe related to dotnet/coreclr#11019

    And it seems not ported to .NET framework runtime.

  2. ghost added
    bugSomething isn't working
    on Jan 27, 2025
  3. hglee commented on Jan 27, 2025

    @hglee
    Author

    Hey @hglee , thanks for bringing up the issue.

    Would it be possible to share which profiler you're using for the benchmarking?

    Hi, it's Jetbrains dotTrace (https://www.jetbrains.com/profiler/).

    The upper was measured in timeline mode and the bottom CUDA was measured in tracing mode.

  4. alinpahontu2912 commented on Feb 16, 2026

    @alinpahontu2912
    Member

    Hi @hglee,

    Thank you for reporting the performance issue with torch.deleters on the .NET Framework CPU target. We appreciate your detailed description and the reproducible example you provided. It seems like a complex issue, especially since it doesn't occur with the .NET target or CUDA.

    I noticed that this issue has been open for quite some time, and TorchSharp has evolved significantly since then. Could you let us know if this issue is still relevant or if it has been resolved in more recent versions? If it persists, any updated information or benchmarks would be incredibly helpful for us to investigate further.

    Thanks again for your contribution to the community!

  5. alinpahontu2912 commented on Feb 17, 2026

    @alinpahontu2912
    Member

    Hey @hglee we would also encourage you to switch from .net fx to .net core to benefit from the latest improvements

  6. hglee commented on Feb 17, 2026

    @hglee
    Author

    Sorry for late. I tried it again with 0.106.0, but issue still there.

    But I agreed to close this issue as not planned, if you have no plan to solve this issue by techincal problem.

    FYI, this is a performance in v0.106.0

    Image

    And comparison between v0.105.2 and v0.106.0, nearly same within error range.

    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions