Skip to content

Commit 8cdfdbd

Browse files
Merge pull request #1420 from yueyinqiu/n
switch default newlines to `null` (`torch.newLine`) from `\n`
2 parents b31789d + 07b32ca commit 8cdfdbd

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

RELEASENOTES.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ This is a big change in implementation, but not as big in API surface area. Many
1313

1414
__Breaking Changes__:
1515

16-
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.
17-
18-
The argument defaults for `torch.diagonal()` and `Tensor.diagonal()` arguments have been corrected.
16+
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/>
17+
The argument defaults for `torch.diagonal()` and `Tensor.diagonal()` arguments have been corrected.<br/>
18+
The default `newLine` for `str`, `jlstr`, `npstr`, `cstr` and `print` have been corrected.<br/>
1919

2020
__Issues fixed__:
2121

src/TorchSharp/Tensor/Tensor.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -6519,18 +6519,16 @@ public string ToString(TensorStringStyle style,
65196519
CultureInfo? cultureInfo = null,
65206520
string? newLine = null)
65216521
{
6522-
var w = width.HasValue ? width.Value : torch.lineWidth;
6523-
var nl = newLine is null ? torch.newLine : newLine;
6524-
var fmt = fltFormat is null ? torch.floatFormat : fltFormat;
6522+
var w = width ?? torch.lineWidth;
6523+
var nl = newLine ?? torch.newLine;
6524+
var fmt = fltFormat ?? torch.floatFormat;
65256525

6526-
if (String.IsNullOrEmpty(newLine))
6527-
newLine = Environment.NewLine;
6528-
6529-
if (device_type == DeviceType.META)
6530-
return ToMetadataString();
6526+
if (style is TensorStringStyle.Default)
6527+
style = torch.TensorStringStyle;
6528+
if (device_type is DeviceType.META)
6529+
style = TensorStringStyle.Metadata;
65316530

65326531
return style switch {
6533-
TensorStringStyle.Default => ToString(torch.TensorStringStyle, fltFormat, width, cultureInfo, nl),
65346532
TensorStringStyle.Metadata => ToMetadataString(),
65356533
TensorStringStyle.Julia => ToJuliaString(fmt, w, cultureInfo, nl),
65366534
TensorStringStyle.Numpy => ToNumpyString(this, ndim, true, fmt, cultureInfo, nl),

src/TorchSharp/Tensor/TensorExtensionMethods.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static Modules.Parameter AsParameter(this Tensor tensor)
148148
///
149149
/// Primarily intended for use in interactive notebooks.
150150
/// </remarks>
151-
public static string str(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = "\n", CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
151+
public static string str(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = null, CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
152152
{
153153
return tensor.ToString(style, fltFormat, width, cultureInfo, newLine);
154154
}
@@ -172,7 +172,7 @@ public static string str(this Tensor tensor, string? fltFormat = null, int? widt
172172
///
173173
/// Primarily intended for use in interactive notebooks.
174174
/// </remarks>
175-
public static string jlstr(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = "\n", CultureInfo? cultureInfo = null)
175+
public static string jlstr(this Tensor tensor, string? fltFormat = null, int? width = null, string? newLine = null, CultureInfo? cultureInfo = null)
176176
{
177177
return tensor.ToString(TensorStringStyle.Julia, fltFormat, width, cultureInfo, newLine);
178178
}
@@ -214,7 +214,7 @@ public static string metastr(this Tensor tensor)
214214
///
215215
/// Primarily intended for use in interactive notebooks.
216216
/// </remarks>
217-
public static string npstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = "\n", CultureInfo? cultureInfo = null)
217+
public static string npstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = null, CultureInfo? cultureInfo = null)
218218
{
219219
return tensor.ToString(TensorStringStyle.Numpy, fltFormat, width, cultureInfo, newLine);
220220
}
@@ -238,7 +238,7 @@ public static string npstr(this Tensor tensor, string? fltFormat = "g5", int? wi
238238
///
239239
/// Primarily intended for use in interactive notebooks.
240240
/// </remarks>
241-
public static string cstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = "\n", CultureInfo? cultureInfo = null)
241+
public static string cstr(this Tensor tensor, string? fltFormat = "g5", int? width = 100, string? newLine = null, CultureInfo? cultureInfo = null)
242242
{
243243
return tensor.ToString(TensorStringStyle.CSharp, fltFormat, width, cultureInfo, newLine);
244244
}
@@ -259,7 +259,7 @@ public static string cstr(this Tensor tensor, string? fltFormat = "g5", int? wid
259259
/// The style to use -- either 'default,' 'metadata,' 'julia,' or 'numpy'
260260
/// </param>
261261
/// <returns></returns>
262-
public static Tensor print(this Tensor t, string? fltFormat = "g5", int? width = 100, string? newLine = "\n", CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
262+
public static Tensor print(this Tensor t, string? fltFormat = "g5", int? width = 100, string? newLine = null, CultureInfo? cultureInfo = null, TensorStringStyle style = TensorStringStyle.Default)
263263
{
264264
Console.WriteLine(t.ToString(style, fltFormat, width, cultureInfo, newLine));
265265
return t;

test/TorchSharpTest/TestTorchTensor.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void Test1DToJuliaString()
117117
{
118118
Tensor t = torch.zeros(4);
119119
var str = t.jlstr(cultureInfo: CultureInfo.InvariantCulture);
120-
Assert.Equal($"[4], type = Float32, device = cpu\n 0 0 0 0\n", str);
120+
Assert.Equal($"[4], type = Float32, device = cpu{Environment.NewLine} 0 0 0 0{Environment.NewLine}", str);
121121
}
122122
{
123123
Tensor t = torch.zeros(4, torch.complex64);
@@ -151,12 +151,12 @@ public void Test2DToJuliaString()
151151
{
152152
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);
153153
var str = t.str(cultureInfo: CultureInfo.InvariantCulture);
154-
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);
154+
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);
155155
}
156156
if (torch.cuda.is_available()) {
157157
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);
158158
var str = t.str(cultureInfo: CultureInfo.InvariantCulture);
159-
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);
159+
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);
160160
}
161161
{
162162
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);
@@ -185,9 +185,10 @@ public void Test3DToJuliaString()
185185
0.01f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
186186
}, 2, 2, 4);
187187
var str = t.jlstr("0.0000000", cultureInfo: CultureInfo.InvariantCulture);
188-
Assert.Equal($"[2x2x4], type = Float32, device = cpu\n[0,..,..] =\n 0.0000000 3.1410000 6.2834000 3.1415200\n" +
188+
Assert.Equal(($"[2x2x4], type = Float32, device = cpu\n[0,..,..] =\n 0.0000000 3.1410000 6.2834000 3.1415200\n" +
189189
$" 0.0000063 -13.1415300 0.0100000 4713.1400000\n\n[1,..,..] =\n 0.0100000 0.0000000 0.0000000 0.0000000\n" +
190-
$" 0.0000000 0.0000000 0.0000000 0.0000000\n", str);
190+
$" 0.0000000 0.0000000 0.0000000 0.0000000\n").Replace("\n", Environment.NewLine),
191+
str);
191192
}
192193
}
193194

@@ -287,7 +288,7 @@ public void TestToString1()
287288
public void TestTensorDefaultPrint()
288289
{
289290
Tensor t = torch.zeros(2, 2);
290-
string expectedOutput = t.ToString(TensorStringStyle.Default, "g5", 100, null, "\n") + Environment.NewLine;
291+
string expectedOutput = t.ToString(TensorStringStyle.Default) + Environment.NewLine;
291292
var originalOut = Console.Out;
292293
using (var sw = new StringWriter())
293294
{

0 commit comments

Comments
 (0)