Skip to content

Commit

Permalink
Merge pull request dotnet#1420 from yueyinqiu/n
Browse files Browse the repository at this point in the history
switch default newlines to `null` (`torch.newLine`) from `\n`
  • Loading branch information
NiklasGustafsson authored Dec 5, 2024
2 parents b31789d + 07b32ca commit 8cdfdbd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
6 changes: 3 additions & 3 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ This is a big change in implementation, but not as big in API surface area. Many

__Breaking Changes__:

The names of several arguments have been changed to align better with Pytorch naming. This may break code that passes such arguments by name, but will be caught at compile time.

The argument defaults for `torch.diagonal()` and `Tensor.diagonal()` arguments have been corrected.
The names of several arguments have been changed to align better with Pytorch naming. This may break code that passes such arguments by name, but will be caught at compile time.<br/>
The argument defaults for `torch.diagonal()` and `Tensor.diagonal()` arguments have been corrected.<br/>
The default `newLine` for `str`, `jlstr`, `npstr`, `cstr` and `print` have been corrected.<br/>

__Issues fixed__:

Expand Down
16 changes: 7 additions & 9 deletions src/TorchSharp/Tensor/Tensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6519,18 +6519,16 @@ public string ToString(TensorStringStyle style,
CultureInfo? cultureInfo = null,
string? newLine = null)
{
var w = width.HasValue ? width.Value : torch.lineWidth;
var nl = newLine is null ? torch.newLine : newLine;
var fmt = fltFormat is null ? torch.floatFormat : fltFormat;
var w = width ?? torch.lineWidth;
var nl = newLine ?? torch.newLine;
var fmt = fltFormat ?? torch.floatFormat;

if (String.IsNullOrEmpty(newLine))
newLine = Environment.NewLine;

if (device_type == DeviceType.META)
return ToMetadataString();
if (style is TensorStringStyle.Default)
style = torch.TensorStringStyle;
if (device_type is DeviceType.META)
style = TensorStringStyle.Metadata;

return style switch {
TensorStringStyle.Default => ToString(torch.TensorStringStyle, fltFormat, width, cultureInfo, nl),
TensorStringStyle.Metadata => ToMetadataString(),
TensorStringStyle.Julia => ToJuliaString(fmt, w, cultureInfo, nl),
TensorStringStyle.Numpy => ToNumpyString(this, ndim, true, fmt, cultureInfo, nl),
Expand Down
10 changes: 5 additions & 5 deletions src/TorchSharp/Tensor/TensorExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static Modules.Parameter AsParameter(this Tensor tensor)
///
/// Primarily intended for use in interactive notebooks.
/// </remarks>
public static string str(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = "\n", CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
public static string str(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = null, CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
{
return tensor.ToString(style, fltFormat, width, cultureInfo, newLine);
}
Expand All @@ -172,7 +172,7 @@ public static string str(this Tensor tensor, string? fltFormat = null, int? widt
///
/// Primarily intended for use in interactive notebooks.
/// </remarks>
public static string jlstr(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = "\n", CultureInfo? cultureInfo = null)
public static string jlstr(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = null, CultureInfo? cultureInfo = null)
{
return tensor.ToString(TensorStringStyle.Julia, fltFormat, width, cultureInfo, newLine);
}
Expand Down Expand Up @@ -214,7 +214,7 @@ public static string metastr(this Tensor tensor)
///
/// Primarily intended for use in interactive notebooks.
/// </remarks>
public static string npstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = "\n", CultureInfo? cultureInfo = null)
public static string npstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = null, CultureInfo? cultureInfo = null)
{
return tensor.ToString(TensorStringStyle.Numpy, fltFormat, width, cultureInfo, newLine);
}
Expand All @@ -238,7 +238,7 @@ public static string npstr(this Tensor tensor, string? fltFormat = "g5", int? wi
///
/// Primarily intended for use in interactive notebooks.
/// </remarks>
public static string cstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = "\n", CultureInfo? cultureInfo = null)
public static string cstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = null, CultureInfo? cultureInfo = null)
{
return tensor.ToString(TensorStringStyle.CSharp, fltFormat, width, cultureInfo, newLine);
}
Expand All @@ -259,7 +259,7 @@ public static string cstr(this Tensor tensor, string? fltFormat = "g5", int? wid
/// The style to use -- either 'default,' 'metadata,' 'julia,' or 'numpy'
/// </param>
/// <returns></returns>
public static Tensor print(this Tensor t, string? fltFormat = "g5", int? width = 100, string? newLine = "\n", CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
public static Tensor print(this Tensor t, string? fltFormat = "g5", int? width = 100, string? newLine = null, CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
{
Console.WriteLine(t.ToString(style, fltFormat, width, cultureInfo, newLine));
return t;
Expand Down
13 changes: 7 additions & 6 deletions test/TorchSharpTest/TestTorchTensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void Test1DToJuliaString()
{
Tensor t = torch.zeros(4);
var str = t.jlstr(cultureInfo: CultureInfo.InvariantCulture);
Assert.Equal($"[4], type = Float32, device = cpu\n 0 0 0 0\n", str);
Assert.Equal($"[4], type = Float32, device = cpu{Environment.NewLine} 0 0 0 0{Environment.NewLine}", str);
}
{
Tensor t = torch.zeros(4, torch.complex64);
Expand Down Expand Up @@ -151,12 +151,12 @@ public void Test2DToJuliaString()
{
Tensor t = torch.tensor(new float[] { 0.0f, 3.141f, 6.2834f, 3.14152f, 6.28e-06f, -13.141529f, 0.01f, 4713.14f }, 2, 4);
var str = t.str(cultureInfo: CultureInfo.InvariantCulture);
Assert.Equal($"[2x4], type = Float32, device = cpu\n 0 3.141 6.2834 3.1415\n 6.28e-06 -13.142 0.01 4713.1\n", str);
Assert.Equal($"[2x4], type = Float32, device = cpu{Environment.NewLine} 0 3.141 6.2834 3.1415{Environment.NewLine} 6.28e-06 -13.142 0.01 4713.1{Environment.NewLine}", str);
}
if (torch.cuda.is_available()) {
Tensor t = torch.tensor(new float[] { 0.0f, 3.141f, 6.2834f, 3.14152f, 6.28e-06f, -13.141529f, 0.01f, 4713.14f }, 2, 4, device: torch.CUDA);
var str = t.str(cultureInfo: CultureInfo.InvariantCulture);
Assert.Equal($"[2x4], type = Float32, device = cuda:0\n 0 3.141 6.2834 3.1415\n 6.28e-06 -13.142 0.01 4713.1\n", str);
Assert.Equal($"[2x4], type = Float32, device = cuda:0{Environment.NewLine} 0 3.141 6.2834 3.1415{Environment.NewLine} 6.28e-06 -13.142 0.01 4713.1{Environment.NewLine}", str);
}
{
Tensor t = torch.tensor(new float[] { 0.0f, 3.141f, 6.2834f, 3.14152f, 6.28e-06f, -13.141529f, 0.01f, 4713.14f }, 2, 4, device: torch.META);
Expand Down Expand Up @@ -185,9 +185,10 @@ public void Test3DToJuliaString()
0.01f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
}, 2, 2, 4);
var str = t.jlstr("0.0000000", cultureInfo: CultureInfo.InvariantCulture);
Assert.Equal($"[2x2x4], type = Float32, device = cpu\n[0,..,..] =\n 0.0000000 3.1410000 6.2834000 3.1415200\n" +
Assert.Equal(($"[2x2x4], type = Float32, device = cpu\n[0,..,..] =\n 0.0000000 3.1410000 6.2834000 3.1415200\n" +
$" 0.0000063 -13.1415300 0.0100000 4713.1400000\n\n[1,..,..] =\n 0.0100000 0.0000000 0.0000000 0.0000000\n" +
$" 0.0000000 0.0000000 0.0000000 0.0000000\n", str);
$" 0.0000000 0.0000000 0.0000000 0.0000000\n").Replace("\n", Environment.NewLine),
str);
}
}

Expand Down Expand Up @@ -287,7 +288,7 @@ public void TestToString1()
public void TestTensorDefaultPrint()
{
Tensor t = torch.zeros(2, 2);
string expectedOutput = t.ToString(TensorStringStyle.Default, "g5", 100, null, "\n") + Environment.NewLine;
string expectedOutput = t.ToString(TensorStringStyle.Default) + Environment.NewLine;
var originalOut = Console.Out;
using (var sw = new StringWriter())
{
Expand Down

0 comments on commit 8cdfdbd

Please sign in to comment.