Skip to content

Commit 2d2fcfc

Browse files
Merge branch 'main' into unit
2 parents 77bf378 + 6c93cc6 commit 2d2fcfc

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/TorchSharp/Tensor/Tensor.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6576,16 +6576,18 @@ private static string ToNumpyString(Tensor t, long mdim, bool isFCreate, string
65766576

65776577
var dim = t.dim();
65786578

6579-
if (t.size().Length == 0) return "";
65806579
var sb = new StringBuilder(isFCreate ? string.Join("", Enumerable.Repeat(' ', (int)(mdim - dim))) : "");
6580+
6581+
if (dim == 0) {
6582+
PrintValue(sb, t.dtype, t.ToScalar(), fltFormat, actualCulturInfo);
6583+
return sb.ToString(); ;
6584+
}
6585+
65816586
sb.Append('[');
65826587
var currentSize = t.size()[0];
65836588
if (currentSize == 0) {
65846589
// print nothing
65856590
}
6586-
else if (dim == 0) {
6587-
PrintValue(sb, t.dtype, t.ToScalar(), fltFormat, actualCulturInfo);
6588-
}
65896591
else if (dim == 1) {
65906592
if (currentSize <= torch.maxColumns) {
65916593
for (var i = 0; i < currentSize - 1; i++) {

test/TorchSharpTest/TestTorchTensorBugs.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,15 @@ public void Validate_1250()
16601660
Assert.Equal("[1], type = Float32, device = cpu, value = float [] {0f}", torch.zeros(1).cstr());
16611661
}
16621662

1663+
[Fact]
1664+
public void Validate_1250a()
1665+
{
1666+
var scalar = torch.zeros(Array.Empty<long>());
1667+
Assert.Equal("0", scalar.npstr());
1668+
Assert.Equal("[], type = Float32, device = cpu, value = 0", scalar.cstr());
1669+
Assert.Equal("[], type = Float32, device = cpu, value = 0", scalar.jlstr());
1670+
}
1671+
16631672
[Fact]
16641673
public void ValidateLoadWithDeflateStream()
16651674
{

0 commit comments

Comments
 (0)