Skip to content

Commit 6fe6057

Browse files
committed
Adjust unit test project.
1 parent a30df75 commit 6fe6057

File tree

66 files changed

+553
-7089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+553
-7089
lines changed

TensorFlow.NET.sln

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Text", "src\Tens
1717
EndProject
1818
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Recommenders", "src\TensorFlowNET.Recommenders\Tensorflow.Recommenders.csproj", "{F17AAECB-960A-4E18-A270-BAD776F0E55B}"
1919
EndProject
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tensorflow.Native.UnitTest", "test\TensorFlowNET.Native.UnitTest\Tensorflow.Native.UnitTest.csproj", "{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -107,8 +109,8 @@ Global
107109
{23C28035-2FCE-41F3-9A12-E73CE8A5AE32}.Release|x86.Build.0 = Release|Any CPU
108110
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
109111
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
110-
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.ActiveCfg = Debug|Any CPU
111-
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.Build.0 = Debug|Any CPU
112+
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.ActiveCfg = Debug|x64
113+
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x64.Build.0 = Debug|x64
112114
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x86.ActiveCfg = Debug|Any CPU
113115
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug|x86.Build.0 = Debug|Any CPU
114116
{03F06299-3F4B-4449-A709-3A647657BC0C}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
@@ -155,8 +157,8 @@ Global
155157
{49D71826-C03D-4FA7-9BAC-22C1327E65CF}.Release|x86.Build.0 = Release|Any CPU
156158
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
157159
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|Any CPU.Build.0 = Debug|Any CPU
158-
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x64.ActiveCfg = Debug|Any CPU
159-
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x64.Build.0 = Debug|Any CPU
160+
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x64.ActiveCfg = Debug|x64
161+
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x64.Build.0 = Debug|x64
160162
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x86.ActiveCfg = Debug|Any CPU
161163
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug|x86.Build.0 = Debug|Any CPU
162164
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
@@ -179,8 +181,8 @@ Global
179181
{1AB8108D-4FFE-4A16-88E7-328EAF686370}.Release|x86.Build.0 = Release|Any CPU
180182
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
181183
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|Any CPU.Build.0 = Debug|Any CPU
182-
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x64.ActiveCfg = Debug|Any CPU
183-
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x64.Build.0 = Debug|Any CPU
184+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x64.ActiveCfg = Debug|x64
185+
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x64.Build.0 = Debug|x64
184186
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x86.ActiveCfg = Debug|Any CPU
185187
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug|x86.Build.0 = Debug|Any CPU
186188
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
@@ -201,6 +203,30 @@ Global
201203
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x64.Build.0 = Release|Any CPU
202204
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x86.ActiveCfg = Release|Any CPU
203205
{F17AAECB-960A-4E18-A270-BAD776F0E55B}.Release|x86.Build.0 = Release|Any CPU
206+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
207+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
208+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|x64.ActiveCfg = Debug|x64
209+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|x64.Build.0 = Debug|x64
210+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|x86.ActiveCfg = Debug|Any CPU
211+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug|x86.Build.0 = Debug|Any CPU
212+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
213+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
214+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug-Minimal|x64.ActiveCfg = Debug|Any CPU
215+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug-Minimal|x64.Build.0 = Debug|Any CPU
216+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug-Minimal|x86.ActiveCfg = Debug|Any CPU
217+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Debug-Minimal|x86.Build.0 = Debug|Any CPU
218+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Publish|Any CPU.ActiveCfg = Debug|Any CPU
219+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Publish|Any CPU.Build.0 = Debug|Any CPU
220+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Publish|x64.ActiveCfg = Debug|Any CPU
221+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Publish|x64.Build.0 = Debug|Any CPU
222+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Publish|x86.ActiveCfg = Debug|Any CPU
223+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Publish|x86.Build.0 = Debug|Any CPU
224+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
225+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|Any CPU.Build.0 = Release|Any CPU
226+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x64.ActiveCfg = Release|Any CPU
227+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x64.Build.0 = Release|Any CPU
228+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x86.ActiveCfg = Release|Any CPU
229+
{84CA35F8-99FC-408E-8DF3-5AA175E5EFD3}.Release|x86.Build.0 = Release|Any CPU
204230
EndGlobalSection
205231
GlobalSection(SolutionProperties) = preSolution
206232
HideSolutionNode = FALSE

src/TensorFlowNET.Console/Tensorflow.Console.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<AssemblyName>Tensorflow</AssemblyName>
8+
<Platforms>AnyCPU;x64</Platforms>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/TensorFlowNET.Core/APIs/tf.graph.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,10 @@ public void reset_default_graph()
2828
=> ops.reset_default_graph();
2929

3030
public Graph get_default_graph()
31-
{
32-
return ops.get_default_graph();
33-
}
31+
=> ops.get_default_graph();
3432

35-
/// <summary>
36-
/// Equivalent to <see cref="get_default_graph"/> but does not create a new graph if it there is none.
37-
/// </summary>
3833
public Graph peak_default_graph()
39-
{
40-
return ops.default_graph_stack.peak_controller();
41-
}
34+
=> ops.peak_default_graph();
4235

4336
/// <summary>
4437
/// Creates a new graph.

src/TensorFlowNET.Core/Contexts/Context.AutoMode.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,7 @@ public T RunInAutoMode<T>(Func<T> graphAction, Func<T> eagerAction, params Tenso
3737
if (shouldRunInEager)
3838
return eagerAction();
3939
else
40-
{
41-
if (executing_eagerly())
42-
{
43-
graph_mode();
44-
var result = graphAction();
45-
restore_mode();
46-
return result;
47-
}
48-
else
49-
{
50-
return graphAction();
51-
}
52-
}
40+
return graphAction();
5341
}
5442

5543
// [DebuggerStepThrough]

src/TensorFlowNET.Core/Contexts/Context.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ public void end_step()
8080
/// Checks whether the current thread has eager execution enabled.
8181
/// </summary>
8282
/// <returns></returns>
83-
[DebuggerStepThrough]
83+
// [DebuggerStepThrough]
8484
public bool executing_eagerly()
8585
{
86+
if(context_switches.Count() == 0)
87+
tf.enable_eager_execution();
88+
8689
return context_switches.Current().EagerMode;
8790
}
8891

@@ -103,11 +106,16 @@ public void eager_mode(bool isFunc = false)
103106
public void restore_mode()
104107
{
105108
context_switches.Pop();
109+
tf.get_default_graph();
106110
}
107111

108112
public void reset_context()
109113
{
110-
c_api.TFE_ContextClearCaches(_handle);
114+
ops.reset_uid();
115+
ops.reset_default_graph();
116+
context_switches.Clear();
117+
if (_handle != null)
118+
c_api.TFE_ContextClearCaches(_handle);
111119
}
112120

113121
public void Dispose()

src/TensorFlowNET.Core/Contexts/ContextSwitchStack.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ public void Push(bool isEager, bool isFunc)
4040
});
4141
}
4242

43+
public void Clear()
44+
{
45+
stack.Clear();
46+
}
47+
4348
public void Pop()
4449
{
4550
stack.Pop();
4651
}
4752

53+
public int Count()
54+
{
55+
return stack.Count;
56+
}
57+
4858
public ContextSwitch Current()
4959
{
5060
return stack.Peek();

src/TensorFlowNET.Core/Data/MapDataset.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ public MapDataset(IDatasetV2 input_dataset,
1515
bool preserve_cardinality = false,
1616
bool use_legacy_function = false) : base(input_dataset)
1717
{
18-
using var func = new ConcreteFunction($"autograph_{map_func.Method.Name}");
18+
using var func = new ConcreteFunction($"{map_func.Method.Name}_{Guid.NewGuid()}");
19+
func.Enter();
1920
var input = tf.placeholder(input_dataset.element_spec[0].dtype);
2021
var output = map_func(input);
2122
func.ToGraph(input, output);
22-
23+
func.Exit();
24+
2325
structure = func.OutputStructure;
2426

2527
variant_tensor = ops.map_dataset(input_dataset.variant_tensor,

src/TensorFlowNET.Core/Functions/ConcreteFunction.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public string Name
3434
public ConcreteFunction(string name)
3535
{
3636
func_graph = new FuncGraph(name);
37-
func_graph.as_default();
3837
}
3938

4039
public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null)
@@ -46,7 +45,7 @@ public ConcreteFunction(FuncGraph graph, Dictionary<string, string> attrs = null
4645

4746
public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
4847
{
49-
string func_name = $"autograph_{Guid.NewGuid()}_{func.Method.Name}";
48+
string func_name = $"{func.Method.Name}_{Guid.NewGuid()}";
5049

5150
// IntPtr func_handle;
5251
using var graph = new FuncGraph(func_name);
@@ -59,11 +58,12 @@ public ConcreteFunction(Func<Tensor, Tensor> func, TF_DataType dtype)
5958
new[] { input },
6059
new[] { output },
6160
null);
61+
graph.Exit();
6262
}
6363

6464
public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
6565
{
66-
string func_name = $"autograph_{Guid.NewGuid()}_{func.Method.Name}";
66+
string func_name = $"{func.Method.Name}_{Guid.NewGuid()}";
6767

6868
// IntPtr func_handle;
6969
using var graph = new FuncGraph(func_name);
@@ -79,12 +79,13 @@ public ConcreteFunction(Func<Tensor, IDatasetV2> func, TF_DataType dtype)
7979
new[] { input },
8080
new[] { output.variant_tensor },
8181
null);
82+
graph.Exit();
8283
}
8384

8485
public ConcreteFunction(Func<Tensor, (Tensor, Tensor), (Tensor, Tensor)> func,
8586
TF_DataType[] dtypes, TensorShape[] shapes)
8687
{
87-
string func_name = $"autograph_{Guid.NewGuid()}_{func.Method.Name}";
88+
string func_name = $"{func.Method.Name}_{Guid.NewGuid()}";
8889

8990
// IntPtr func_handle;
9091
using var graph = new FuncGraph(func_name);
@@ -103,6 +104,7 @@ public ConcreteFunction(Func<Tensor, (Tensor, Tensor), (Tensor, Tensor)> func,
103104
new[] { input1, input2, input3 },
104105
new[] { outputs.Item1, outputs.Item2 },
105106
null);
107+
graph.Exit();
106108
}
107109

108110
public void ToGraph(Tensors inputs, Tensors outputs)
@@ -112,10 +114,19 @@ public void ToGraph(Tensors inputs, Tensors outputs)
112114
inputs,
113115
outputs,
114116
null);
115-
116117
OutputStructure = outputs.Select(x => x.ToTensorSpec()).ToArray();
117118
}
118119

120+
public void Enter()
121+
{
122+
func_graph.as_default();
123+
}
124+
125+
public void Exit()
126+
{
127+
func_graph.Exit();
128+
}
129+
119130
public Tensors Invoke(Tensors inputs)
120131
{
121132
var forward_backward = SelectForwardAndBackwardFunctions(inputs, 1, tf.Context.executing_eagerly());

src/TensorFlowNET.Core/Functions/EagerDefinedFunction.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public EagerDefinedFunction(string name, FuncGraph graph,
2626
var output_names = new string[0];
2727

2828
_func_graph = new FuncGraph(graph, name, attrs);
29-
_func_graph.as_default();
3029
_func_graph.ToGraph(operations, inputs, outputs, output_names);
3130
}
3231

src/TensorFlowNET.Core/Functions/TapeGradientFunctions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void Record(Tensors flat_outputs, Tensors inference_args)
8484
}
8585

8686
var gradients_wrt_outputs = new List<Tensor>();
87-
var backwards_graph = new FuncGraph($"{_BACKWARD_PREFIX}_{ops.uid()}");
87+
var backwards_graph = new FuncGraph($"{_BACKWARD_PREFIX}_{_func_graph.FuncName}_{ops.uid()}");
8888
backwards_graph.as_default();
8989
foreach (var output in trainable_outputs)
9090
gradients_wrt_outputs.Add(tf.placeholder(output.dtype, output.shape));
@@ -101,6 +101,7 @@ public void Record(Tensors flat_outputs, Tensors inference_args)
101101
if (!_func_graph.Outputs.Contains(capture))
102102
_func_graph.Outputs.Add(capture);
103103
}
104+
backwards_graph.Exit();
104105

105106
var forward_function_name = $"{_FORWARD_PREFIX}_{ops.uid()}";
106107
var backward_function_attr = new Dictionary<string, string>();

0 commit comments

Comments
 (0)