dotnet/TorchSharp

Attaching unowned tensor to a DisposeScope makes Statistics.DetachedFromScopeCount go negative.

mvphelps opened this issue · 0 comments

Repro:

[Fact]
    public void DetachedFromScopeCountNegative()
    {
        var stats = DisposeScopeManager.Statistics;
        stats.Reset();
        var t = torch.tensor(1);
        Assert.Equal(0, stats.DetachedFromScopeCount);
        var scope = torch.NewDisposeScope();
        scope.Attach(t);
        Assert.Equal(0, stats.DetachedFromScopeCount);
    }

Removing the branch in DisposeScope.Attach(IEnumerable<IDisposable> disposables) that subtracts fixes the issue and all tests pass, minus any tests checking counts (waiting on PR #1386 prior to fixing.