Skip to content

Commit 8cab730

Browse files
authoredNov 11, 2023
Merge pull request #1215 from novikov-alexander/alnovi/pythontests_common
refactor: python test common class moved to a separate common assembly
2 parents 59b08ca + 7968dc3 commit 8cab730

File tree

11 files changed

+97
-497
lines changed

11 files changed

+97
-497
lines changed
 

‎TensorFlow.NET.sln

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Benchmark", "too
3939
EndProject
4040
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Console", "tools\TensorFlowNET.Console\Tensorflow.Console.csproj", "{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0}"
4141
EndProject
42-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TensorFlow.Kernel.UnitTest", "test\TensorFlow.Kernel.UnitTest\TensorFlow.Kernel.UnitTest.csproj", "{654A027D-1364-4729-880B-144DFE1FF5BB}"
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlow.Kernel.UnitTest", "test\TensorFlow.Kernel.UnitTest\TensorFlow.Kernel.UnitTest.csproj", "{654A027D-1364-4729-880B-144DFE1FF5BB}"
43+
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tensorflow.UnitTest", "test\Tensorflow.UnitTest\Tensorflow.UnitTest.csproj", "{A73DF5A6-866E-4AED-9017-AA2EE86368C4}"
4345
EndProject
4446
Global
4547
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -342,6 +344,24 @@ Global
342344
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x64.Build.0 = Release|Any CPU
343345
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x86.ActiveCfg = Release|Any CPU
344346
{654A027D-1364-4729-880B-144DFE1FF5BB}.Release|x86.Build.0 = Release|Any CPU
347+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
348+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
349+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x64.ActiveCfg = Debug|Any CPU
350+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x64.Build.0 = Debug|Any CPU
351+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x86.ActiveCfg = Debug|Any CPU
352+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Debug|x86.Build.0 = Debug|Any CPU
353+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|Any CPU.ActiveCfg = Debug|Any CPU
354+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|Any CPU.Build.0 = Debug|Any CPU
355+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x64.ActiveCfg = Debug|Any CPU
356+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x64.Build.0 = Debug|Any CPU
357+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x86.ActiveCfg = Debug|Any CPU
358+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.GPU|x86.Build.0 = Debug|Any CPU
359+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
360+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|Any CPU.Build.0 = Release|Any CPU
361+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x64.ActiveCfg = Release|Any CPU
362+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x64.Build.0 = Release|Any CPU
363+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x86.ActiveCfg = Release|Any CPU
364+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4}.Release|x86.Build.0 = Release|Any CPU
345365
EndGlobalSection
346366
GlobalSection(SolutionProperties) = preSolution
347367
HideSolutionNode = FALSE
@@ -363,6 +383,7 @@ Global
363383
{C23563DB-FE21-48E7-A411-87A109E4A899} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
364384
{1DC32255-BA1F-4D6D-A9C9-5BD5ED71CAA0} = {E1A5D2B7-10AF-4876-85C0-7714EF274214}
365385
{654A027D-1364-4729-880B-144DFE1FF5BB} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD}
386+
{A73DF5A6-866E-4AED-9017-AA2EE86368C4} = {1B0918B9-65AD-4F34-A287-AF4597B27DBD}
366387
EndGlobalSection
367388
GlobalSection(ExtensibilityGlobals) = postSolution
368389
SolutionGuid = {2DEAD3CC-486B-4918-A607-50B0DE7B114A}

‎test/TensorFlow.Kernel.UnitTest/TensorFlow.Kernel.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
1414
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
1515
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
1616
<PackageReference Include="coverlet.collector" Version="3.2.0" />

‎test/TensorFlowNET.Graph.UnitTest/GradientTest/GradientTest.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -625,25 +625,6 @@ public void testPartialDerivatives()
625625
}
626626
}
627627

628-
// TODO: remove when np.testing.assert_allclose(a, b) is implemented
629-
private class CollectionComparer : System.Collections.IComparer
630-
{
631-
private readonly double _epsilon = 1e-07;
632-
633-
public int Compare(object x, object y)
634-
{
635-
var a = (double)x;
636-
var b = (double)y;
637-
638-
double delta = Math.Abs(a - b);
639-
if (delta < _epsilon)
640-
{
641-
return 0;
642-
}
643-
return a.CompareTo(b);
644-
}
645-
}
646-
647628
private struct Case
648629
{
649630
public Tensor[] grad1;
@@ -748,8 +729,7 @@ Tensor[] gradients(Tensor[] ys, Tensor[] xs, Tensor[] stop_gradients = null)
748729
var npgrad2 = result[1];
749730
foreach (var (a, b) in npgrad1.Zip(npgrad2))
750731
{
751-
// TODO: np.testing.assert_allclose(a, b);
752-
CollectionAssert.AreEqual(a.ToArray(), b.ToArray(), new CollectionComparer());
732+
self.assertAllClose(a, b);
753733
}
754734
}
755735
}

‎test/TensorFlowNET.Graph.UnitTest/PythonTest.cs

Lines changed: 0 additions & 448 deletions
This file was deleted.

‎test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</PropertyGroup>
2525

2626
<ItemGroup>
27-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
27+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
2828
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
2929
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
3030
<PackageReference Include="coverlet.collector" Version="3.2.0">
@@ -36,6 +36,7 @@
3636
<ItemGroup>
3737
<ProjectReference Include="..\..\src\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
3838
<ProjectReference Include="..\..\tools\Tensorflow.UnitTest.RedistHolder\Tensorflow.UnitTest.RedistHolder.csproj" />
39+
<ProjectReference Include="..\Tensorflow.UnitTest\Tensorflow.UnitTest.csproj" />
3940
<ProjectReference Include="..\TensorFlowNET.Keras.UnitTest\Tensorflow.Keras.UnitTest.csproj" />
4041
</ItemGroup>
4142

‎test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
1717
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
1818
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
1919
<PackageReference Include="coverlet.collector" Version="3.2.0">
@@ -25,6 +25,7 @@
2525
<ItemGroup>
2626
<ProjectReference Include="..\..\src\TensorFlowNET.Keras\Tensorflow.Keras.csproj" />
2727
<ProjectReference Include="..\..\tools\Tensorflow.UnitTest.RedistHolder\Tensorflow.UnitTest.RedistHolder.csproj" />
28+
<ProjectReference Include="..\TensorFlow.Kernel.UnitTest\TensorFlow.Kernel.UnitTest.csproj" />
2829
</ItemGroup>
2930

3031
<ItemGroup>

‎test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
<ItemGroup>
4646
<PackageReference Include="FluentAssertions" Version="5.10.3" />
47-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
47+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
4848
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
4949
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
5050
<PackageReference Include="coverlet.collector" Version="3.2.0">

‎test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<ProjectReference Include="..\..\src\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
5252
<ProjectReference Include="..\..\src\TensorFlowNET.Text\Tensorflow.Text.csproj" />
5353
<ProjectReference Include="..\..\tools\Tensorflow.UnitTest.RedistHolder\Tensorflow.UnitTest.RedistHolder.csproj" />
54+
<ProjectReference Include="..\Tensorflow.UnitTest\Tensorflow.UnitTest.csproj" />
5455
</ItemGroup>
5556

5657
<ItemGroup>

‎test/TensorFlowNET.UnitTest/PythonTest.cs renamed to ‎test/Tensorflow.UnitTest/PythonTest.cs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Newtonsoft.Json.Linq;
33
using Tensorflow.NumPy;
4-
using System;
54
using System.Collections;
6-
using System.Linq;
75
using Tensorflow;
86
using static Tensorflow.Binding;
9-
using System.Collections.Generic;
107

118
namespace TensorFlowNET.UnitTest
129
{
@@ -89,9 +86,9 @@ public void assertEqual(object given, object expected)
8986
Assert.AreEqual(JObject.FromObject(expected).ToString(), JObject.FromObject(given).ToString());
9087
return;
9188
}
92-
if (given is ICollection && expected is ICollection)
89+
if (given is ICollection collectionGiven && expected is ICollection collectionExpected)
9390
{
94-
assertItemsEqual(given as ICollection, expected as ICollection);
91+
assertItemsEqual(collectionGiven, collectionExpected);
9592
return;
9693
}
9794
if (given is float && expected is float)
@@ -136,13 +133,23 @@ public void assertTrue(bool cond)
136133

137134
public void assertAllClose(NDArray array1, NDArray array2, double eps = 1e-5)
138135
{
139-
Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
136+
CollectionAssert.AreEqual(array1.ToArray(), array2.ToArray(), new CollectionComparer(eps));
137+
138+
//TODO: Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
140139
}
141140

142141
public void assertAllClose(double value, NDArray array2, double eps = 1e-5)
143142
{
143+
if (array2.shape.IsScalar)
144+
{
145+
double value2 = array2;
146+
Assert.AreEqual(value, value2, eps);
147+
return;
148+
}
144149
var array1 = np.ones_like(array2) * value;
145-
Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
150+
CollectionAssert.AreEqual(array1.ToArray(), array2.ToArray(), new CollectionComparer(eps));
151+
152+
//TODO: Assert.IsTrue(np.allclose(array1, array2, rtol: eps));
146153
}
147154

148155
private class CollectionComparer : IComparer
@@ -153,8 +160,21 @@ public CollectionComparer(double eps = 1e-06)
153160
{
154161
_epsilon = eps;
155162
}
156-
public int Compare(object x, object y)
163+
public int Compare(object? x, object? y)
157164
{
165+
if (x == null && y == null)
166+
{
167+
return 0;
168+
}
169+
else if (x == null)
170+
{
171+
return -1;
172+
}
173+
else if (y == null)
174+
{
175+
return 1;
176+
}
177+
158178
var a = (double)x;
159179
var b = (double)y;
160180

@@ -188,9 +208,9 @@ public void assertProtoEquals(object toProto, object o)
188208

189209
#region tensor evaluation and test session
190210

191-
private Session _cached_session = null;
192-
private Graph _cached_graph = null;
193-
private object _cached_config = null;
211+
private Session? _cached_session = null;
212+
private Graph? _cached_graph = null;
213+
private object? _cached_config = null;
194214
private bool _cached_force_gpu = false;
195215

196216
private void _ClearCachedSession()
@@ -209,7 +229,7 @@ private void _ClearCachedSession()
209229
// return nest.map_structure(self._eval_tensor, tensors);
210230
//}
211231

212-
protected object _eval_tensor(object tensor)
232+
protected object? _eval_tensor(object tensor)
213233
{
214234
if (tensor == null)
215235
return None;
@@ -240,7 +260,7 @@ protected object _eval_tensor(object tensor)
240260
/// </summary>
241261
public T evaluate<T>(Tensor tensor)
242262
{
243-
object result = null;
263+
object? result = null;
244264
// if context.executing_eagerly():
245265
// return self._eval_helper(tensors)
246266
// else:
@@ -276,8 +296,8 @@ public T evaluate<T>(Tensor tensor)
276296

277297

278298
///Returns a TensorFlow Session for use in executing tests.
279-
public Session cached_session(
280-
Graph graph = null, object config = null, bool use_gpu = false, bool force_gpu = false)
299+
public Session? cached_session(
300+
Graph? graph = null, object? config = null, bool use_gpu = false, bool force_gpu = false)
281301
{
282302
// This method behaves differently than self.session(): for performance reasons
283303
// `cached_session` will by default reuse the same session within the same
@@ -328,7 +348,7 @@ public Session cached_session(
328348
}
329349

330350
//Returns a TensorFlow Session for use in executing tests.
331-
public Session session(Graph graph = null, object config = null, bool use_gpu = false, bool force_gpu = false)
351+
public Session session(Graph? graph = null, object? config = null, bool use_gpu = false, bool force_gpu = false)
332352
{
333353
//Note that this will set this session and the graph as global defaults.
334354

@@ -362,7 +382,7 @@ public Session session(Graph graph = null, object config = null, bool use_gpu =
362382
// A Session object that should be used as a context manager to surround
363383
// the graph building and execution code in a test case.
364384

365-
Session s = null;
385+
Session? s = null;
366386
//if (context.executing_eagerly())
367387
// yield None
368388
//else
@@ -372,7 +392,7 @@ public Session session(Graph graph = null, object config = null, bool use_gpu =
372392
return s.as_default();
373393
}
374394

375-
private Session _constrain_devices_and_set_default(Session sess, bool use_gpu, bool force_gpu)
395+
private Session? _constrain_devices_and_set_default(Session sess, bool use_gpu, bool force_gpu)
376396
{
377397
// Set the session and its graph to global default and constrain devices."""
378398
if (tf.executing_eagerly())
@@ -407,7 +427,7 @@ private Session _constrain_devices_and_set_default(Session sess, bool use_gpu, b
407427
}
408428

409429
// See session() for details.
410-
private Session _create_session(Graph graph, object cfg, bool forceGpu)
430+
private Session _create_session(Graph? graph, object? cfg, bool forceGpu)
411431
{
412432
var prepare_config = new Func<object, object>((config) =>
413433
{
@@ -451,8 +471,8 @@ private Session _create_session(Graph graph, object cfg, bool forceGpu)
451471
}
452472

453473
private Session _get_cached_session(
454-
Graph graph = null,
455-
object config = null,
474+
Graph? graph = null,
475+
object? config = null,
456476
bool force_gpu = false,
457477
bool crash_if_inconsistent_args = true)
458478
{
@@ -488,7 +508,7 @@ different than the one that was used to create the
488508
session. Maybe create a new session with
489509
self.session()");
490510
}
491-
return _cached_session;
511+
return self._cached_session;
492512
}
493513
}
494514

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
<IsTestProject>true</IsTestProject>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
14+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
15+
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
16+
<PackageReference Include="coverlet.collector" Version="3.2.0" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\src\TensorFlowNET.Core\Tensorflow.Binding.csproj" />
21+
<ProjectReference Include="..\..\tools\Tensorflow.UnitTest.RedistHolder\Tensorflow.UnitTest.RedistHolder.csproj" />
22+
</ItemGroup>
23+
24+
</Project>

‎test/TensorflowNET.Hub.Unittest/Tensorflow.Hub.Unittest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
1313
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
1414
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
1515
<PackageReference Include="coverlet.collector" Version="3.1.2" />

0 commit comments

Comments
 (0)
Please sign in to comment.