Skip to content

Commit ba8b0f3

Browse files
committed
add disable_eager_execution, clean unit test.
1 parent 4bba3f8 commit ba8b0f3

22 files changed

Lines changed: 121 additions & 123 deletions

File tree

src/TensorFlowNET.Console/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void Main(string[] args)
2727
// 100K gradient 44M.
2828
mm.Execute(10, 10 * batchSize, cases.Gradient);
2929

30-
// 120M
30+
// 95M
3131
Console.WriteLine("Finished.");
3232
Console.ReadLine();
3333
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*****************************************************************************
2+
Copyright 2020 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using NumSharp;
18+
19+
namespace Tensorflow
20+
{
21+
public partial class tensorflow
22+
{
23+
public CompatApi compat { get; } = new CompatApi();
24+
25+
public class CompatApi
26+
{
27+
public CompatV1Api v1 { get; } = new CompatV1Api();
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*****************************************************************************
2+
Copyright 2020 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using System;
18+
using Tensorflow.Eager;
19+
using static Tensorflow.Binding;
20+
21+
namespace Tensorflow
22+
{
23+
public class CompatV1Api
24+
{
25+
public void disable_eager_execution()
26+
{
27+
tf.context.default_execution_mode = Context.GRAPH_MODE;
28+
}
29+
}
30+
}

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ private void _check_not_finalized()
259259

260260
public Operation create_op(string op_type, Tensor[] inputs, TF_DataType[] dtypes,
261261
TF_DataType[] input_types = null, string name = null,
262-
Dictionary<string, AttrValue> attrs = null, OpDef op_def = null)
262+
Dictionary<string, AttrValue> attrs = null, OpDef op_def = null,
263+
bool compute_device = true)
263264
{
264265
if (inputs == null)
265266
inputs = new Tensor[0];
@@ -270,7 +271,7 @@ public Operation create_op(string op_type, Tensor[] inputs, TF_DataType[] dtypes
270271
// If a names ends with a '/' it is a "name scope" and we use it as-is,
271272
// after removing the trailing '/'.
272273
name = name.EndsWith("/") ? ops.name_from_scope_name(name) : unique_name(name);
273-
var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs);
274+
var node_def = ops._NodeDef(op_type, name, attrs: attrs);
274275

275276
var input_ops = inputs.Select(x => x.op).ToArray();
276277
var control_inputs = _control_dependencies_for_inputs(input_ops);
@@ -284,7 +285,7 @@ public Operation create_op(string op_type, Tensor[] inputs, TF_DataType[] dtypes
284285
original_op: null,
285286
op_def: op_def);
286287

287-
_create_op_helper(op, true);
288+
_create_op_helper(op, compute_device);
288289

289290
/*Console.Write($"create_op: {op_type} '{node_def.Name}'");
290291
Console.Write($", inputs: {(inputs.Length == 0 ? "empty" : String.Join(", ", inputs.Select(x => x.name)))}");

src/TensorFlowNET.Core/Operations/Operation.Control.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public void _control_flow_post_processing()
4040

4141
public void _add_control_input(Operation op)
4242
{
43-
//c_api.TF_AddControlInput(_operDesc, op);
44-
c_api.AddControlInput(graph, _handle, op);
43+
c_api.TF_AddControlInput(OpDesc, op);
44+
//c_api.AddControlInput(graph, _handle, op);
4545
}
4646

4747
public void _add_control_inputs(Operation[] ops)

src/TensorFlowNET.Core/Operations/Operation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public partial class Operation : ITensorOrOperation
6464
public string Device => _handle == IntPtr.Zero ? null : c_api.StringPiece(c_api.TF_OperationDevice(_handle));
6565

6666
bool _is_stateful;
67-
67+
public OperationDescription OpDesc { get; set; }
6868

6969
public NodeDef node_def
7070
{
@@ -170,7 +170,7 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
170170
op_def = g.GetOpDef(node_def.Op);
171171

172172
var grouped_inputs = _reconstruct_sequence_inputs(op_def, inputs, node_def.Attr);
173-
_handle = ops._create_c_op(g, node_def, grouped_inputs, control_input_ops.ToArray());
173+
(_handle, OpDesc) = ops._create_c_op(g, node_def, grouped_inputs, control_input_ops.ToArray());
174174
_is_stateful = op_def.IsStateful;
175175

176176
// Initialize self._outputs.

src/TensorFlowNET.Core/Tensors/c_api.tensor.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int
187187
[DllImport(TensorFlowLibName)]
188188
public static extern unsafe ulong TF_StringEncode(byte* src, ulong src_len, sbyte* dst, ulong dst_len, SafeStatusHandle status);
189189

190-
[DllImport(TensorFlowLibName)]
191-
public static extern unsafe ulong TF_StringEncode(IntPtr src, ulong src_len, IntPtr dst, ulong dst_len, SafeStatusHandle status);
192-
193190
/// <summary>
194191
/// Decode a string encoded using TF_StringEncode.
195192
/// </summary>
@@ -199,9 +196,6 @@ public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int
199196
/// <param name="dst_len">size_t*</param>
200197
/// <param name="status">TF_Status*</param>
201198
/// <returns></returns>
202-
[DllImport(TensorFlowLibName)]
203-
public static extern ulong TF_StringDecode(IntPtr src, ulong src_len, IntPtr dst, ref ulong dst_len, SafeStatusHandle status);
204-
205199
[DllImport(TensorFlowLibName)]
206200
public static extern unsafe ulong TF_StringDecode(byte* src, ulong src_len, byte** dst, ref ulong dst_len, SafeStatusHandle status);
207201

src/TensorFlowNET.Core/ops.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static _ControlDependenciesController control_dependencies(object[] contr
155155
/// </param>
156156
/// <param name="control_inputs">A list of `Operation`s to set as control dependencies.</param>
157157
/// <returns>A wrapped TF_Operation*.</returns>
158-
public static IntPtr _create_c_op<T>(Graph graph, NodeDef node_def, T[] inputs, Operation[] control_inputs)
158+
public static (IntPtr, OperationDescription) _create_c_op<T>(Graph graph, NodeDef node_def, T[] inputs, Operation[] control_inputs)
159159
{
160160
lock (Locks.ProcessWide)
161161
{
@@ -198,7 +198,7 @@ public static IntPtr _create_c_op<T>(Graph graph, NodeDef node_def, T[] inputs,
198198

199199
status.Check(true);
200200

201-
return c_op;
201+
return (c_op, op_desc);
202202
}
203203
}
204204

@@ -207,7 +207,7 @@ public static OpDef _get_op_def(Graph graph, string type)
207207
return graph.GetOpDef(type);
208208
}
209209

210-
public static NodeDef _NodeDef(string op_type, string name, string device = "", Dictionary<string, AttrValue> attrs = null)
210+
public static NodeDef _NodeDef(string op_type, string name, Dictionary<string, AttrValue> attrs = null)
211211
{
212212
var node_def = new NodeDef();
213213
node_def.Op = op_type;

test/TensorFlowNET.UnitTest/Basics/QueueTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
using System.Linq;
55
using System.Text;
66
using Tensorflow;
7+
using Tensorflow.UnitTest;
78
using static Tensorflow.Binding;
89

910
namespace TensorFlowNET.UnitTest.Basics
1011
{
11-
[Ignore]
1212
[TestClass]
13-
public class QueueTest
13+
public class QueueTest : GraphModeTestBase
1414
{
1515
[TestMethod]
1616
public void PaddingFIFOQueue()

test/TensorFlowNET.UnitTest/Basics/VariableTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace TensorFlowNET.UnitTest.Basics
1010
[TestClass]
1111
public class VariableTest
1212
{
13-
[Ignore]
1413
[TestMethod]
1514
public void NewVariable()
1615
{
@@ -34,7 +33,6 @@ public void VarSum()
3433
Assert.AreEqual(4, (int)y.numpy());
3534
}
3635

37-
[Ignore]
3836
[TestMethod]
3937
public void Assign1()
4038
{

0 commit comments

Comments
 (0)