Skip to content

Commit 536293d

Browse files
committed
Add log_device_placement #713
1 parent cca2111 commit 536293d

File tree

17 files changed

+363
-162
lines changed

17 files changed

+363
-162
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17-
namespace Tensorflow.Contrib.Learn.Preprocessing
17+
using Tensorflow.Contexts;
18+
using Tensorflow.Framework;
19+
20+
namespace Tensorflow
1821
{
19-
public class VocabularyProcessor
22+
public partial class tensorflow
2023
{
21-
private int _max_document_length;
22-
private int _min_frequency;
23-
24-
public VocabularyProcessor(int max_document_length,
25-
int min_frequency)
26-
{
27-
_max_document_length = max_document_length;
28-
_min_frequency = min_frequency;
29-
}
24+
/// <summary>
25+
/// Public API for tf.debugging namespace
26+
/// https://www.tensorflow.org/api_docs/python/tf/debugging
27+
/// More debugging instructions
28+
/// https://developer.ibm.com/technologies/artificial-intelligence/tutorials/debug-tensorflow/
29+
/// </summary>
30+
public ConfigImpl config => new ConfigImpl();
3031
}
3132
}

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

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,18 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using Tensorflow.Debugging;
18+
1719
namespace Tensorflow
1820
{
1921
public partial class tensorflow
2022
{
2123
/// <summary>
22-
/// Assert the condition `x == y` holds element-wise.
24+
/// Public API for tf.debugging namespace
25+
/// https://www.tensorflow.org/api_docs/python/tf/debugging
26+
/// More debugging instructions
27+
/// https://developer.ibm.com/technologies/artificial-intelligence/tutorials/debug-tensorflow/
2328
/// </summary>
24-
/// <typeparam name="T1"></typeparam>
25-
/// <typeparam name="T2"></typeparam>
26-
/// <param name="t1"></param>
27-
/// <param name="t2"></param>
28-
/// <param name="data"></param>
29-
/// <param name="message"></param>
30-
/// <param name="name"></param>
31-
/// <returns></returns>
32-
public Tensor assert_equal<T1, T2>(T1 t1,
33-
T2 t2,
34-
object[] data = null,
35-
string message = null,
36-
string name = null)
37-
=> check_ops.assert_equal(t1,
38-
t2,
39-
data: data,
40-
message: message,
41-
name: name);
42-
43-
public Tensor assert_greater_equal<T1, T2>(Tensor x,
44-
Tensor y,
45-
object[] data = null,
46-
string message = null,
47-
string name = null)
48-
=> check_ops.assert_greater_equal(x,
49-
y,
50-
data: data,
51-
message: message,
52-
name: name);
29+
public DebugImpl debugging => new DebugImpl();
5330
}
5431
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*****************************************************************************
2+
Copyright 2018 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 System.Diagnostics;
19+
using System.Linq;
20+
using Tensorflow.Eager;
21+
using static Tensorflow.Binding;
22+
using Google.Protobuf;
23+
24+
namespace Tensorflow.Contexts
25+
{
26+
/// <summary>
27+
/// Environment in which eager operations execute.
28+
/// </summary>
29+
public sealed partial class Context
30+
{
31+
// [DebuggerStepThrough]
32+
public T RunInAutoMode<T>(Func<T> graphAction, Func<T> eagerAction, params Tensor[] tensors)
33+
{
34+
var shouldRunInEager = executing_eagerly()
35+
&& tensors.Count(x => x.IsEagerTensor) == tensors.Length;
36+
37+
if (shouldRunInEager)
38+
return eagerAction();
39+
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+
}
53+
}
54+
55+
// [DebuggerStepThrough]
56+
public Tensors RunInAutoMode2(Func<Tensors> graphAction,
57+
Func<Tensors> eagerAction,
58+
Action<Operation> recordGradient,
59+
Tensors tensors)
60+
{
61+
var shouldRunInEager = executing_eagerly()
62+
&& tensors.Count(x => x.IsEagerTensor) == tensors.Length;
63+
64+
if (shouldRunInEager)
65+
return eagerAction();
66+
else
67+
{
68+
if (executing_eagerly())
69+
{
70+
graph_mode();
71+
var result = graphAction();
72+
restore_mode();
73+
return result;
74+
}
75+
else
76+
{
77+
var result = graphAction();
78+
if (tf.Runner.MustRecordGradient())
79+
recordGradient(result[0].op);
80+
return result;
81+
}
82+
}
83+
}
84+
}
85+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*****************************************************************************
2+
Copyright 2018 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 System.Diagnostics;
19+
20+
namespace Tensorflow.Contexts
21+
{
22+
/// <summary>
23+
/// Environment in which eager operations execute.
24+
/// </summary>
25+
public sealed partial class Context
26+
{
27+
ConfigProto _config;
28+
29+
ConfigProto config()
30+
{
31+
var config = new ConfigProto()
32+
{
33+
LogDevicePlacement = _log_device_placement,
34+
GpuOptions = _compute_gpu_options()
35+
};
36+
37+
return config;
38+
}
39+
40+
GPUOptions _compute_gpu_options()
41+
{
42+
return new GPUOptions()
43+
{
44+
45+
};
46+
}
47+
}
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*****************************************************************************
2+
Copyright 2018 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 System.Diagnostics;
19+
using System.Linq;
20+
using Tensorflow.Eager;
21+
using static Tensorflow.Binding;
22+
using Google.Protobuf;
23+
24+
namespace Tensorflow.Contexts
25+
{
26+
/// <summary>
27+
/// Environment in which eager operations execute.
28+
/// </summary>
29+
public sealed partial class Context
30+
{
31+
ContextDevicePlacementPolicy _device_policy;
32+
bool _log_device_placement;
33+
34+
public void log_device_placement(bool enable)
35+
{
36+
if (_handle != null)
37+
c_api.TFE_ContextSetLogDevicePlacement(_handle, enable, tf.Status.Handle);
38+
_log_device_placement = enable;
39+
// _thread_local_data.function_call_options = null;
40+
}
41+
}
42+
}

src/TensorFlowNET.Core/Contexts/Context.cs

Lines changed: 21 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ limitations under the License.
1919
using System.Linq;
2020
using Tensorflow.Eager;
2121
using static Tensorflow.Binding;
22+
using Google.Protobuf;
2223

2324
namespace Tensorflow.Contexts
2425
{
2526
/// <summary>
2627
/// Environment in which eager operations execute.
2728
/// </summary>
28-
public sealed class Context : IDisposable
29+
public sealed partial class Context : IDisposable
2930
{
3031
public const int GRAPH_MODE = 0;
3132
public const int EAGER_MODE = 1;
@@ -37,14 +38,14 @@ public sealed class Context : IDisposable
3738
ContextSwitchStack context_switches;
3839
public FunctionCallOptions FunctionCallOptions { get; }
3940

40-
public SafeContextHandle Handle { get; }
41+
SafeContextHandle _handle;
42+
public SafeContextHandle Handle => _handle;
4143

42-
public Context(ContextOptions opts, Status status)
44+
public Context()
4345
{
44-
Handle = c_api.TFE_NewContext(opts.Handle, status.Handle);
45-
status.Check(true);
46+
_device_policy = ContextDevicePlacementPolicy.DEVICE_PLACEMENT_SILENT;
4647
context_switches = new ContextSwitchStack(defaultExecutionMode == EAGER_MODE, false);
47-
initialized = true;
48+
initialized = false;
4849
FunctionCallOptions = new FunctionCallOptions();
4950
}
5051

@@ -55,14 +56,25 @@ public void ensure_initialized()
5556
{
5657
if (initialized)
5758
return;
59+
60+
_config = config();
61+
var config_str = _config.ToByteArray();
62+
63+
using var opts = new ContextOptions();
64+
using var status = new Status();
65+
c_api.TFE_ContextOptionsSetConfig(opts.Handle, config_str, (ulong)config_str.Length, status.Handle);
66+
status.Check(true);
67+
c_api.TFE_ContextOptionsSetDevicePlacementPolicy(opts.Handle, _device_policy);
68+
_handle = c_api.TFE_NewContext(opts.Handle, status.Handle);
69+
status.Check(true);
5870
initialized = true;
5971
}
6072

6173
public void start_step()
62-
=> c_api.TFE_ContextStartStep(Handle);
74+
=> c_api.TFE_ContextStartStep(_handle);
6375

6476
public void end_step()
65-
=> c_api.TFE_ContextEndStep(Handle);
77+
=> c_api.TFE_ContextEndStep(_handle);
6678

6779
/// <summary>
6880
/// Checks whether the current thread has eager execution enabled.
@@ -91,61 +103,7 @@ public void restore_mode()
91103
context_switches.Pop();
92104
}
93105

94-
// [DebuggerStepThrough]
95-
public T RunInAutoMode<T>(Func<T> graphAction, Func<T> eagerAction, params Tensor[] tensors)
96-
{
97-
var shouldRunInEager = executing_eagerly()
98-
&& tensors.Count(x => x.IsEagerTensor) == tensors.Length;
99-
100-
if (shouldRunInEager)
101-
return eagerAction();
102-
else
103-
{
104-
if (executing_eagerly())
105-
{
106-
graph_mode();
107-
var result = graphAction();
108-
restore_mode();
109-
return result;
110-
}
111-
else
112-
{
113-
return graphAction();
114-
}
115-
}
116-
}
117-
118-
// [DebuggerStepThrough]
119-
public Tensors RunInAutoMode2(Func<Tensors> graphAction,
120-
Func<Tensors> eagerAction,
121-
Action<Operation> recordGradient,
122-
Tensors tensors)
123-
{
124-
var shouldRunInEager = executing_eagerly()
125-
&& tensors.Count(x => x.IsEagerTensor) == tensors.Length;
126-
127-
if (shouldRunInEager)
128-
return eagerAction();
129-
else
130-
{
131-
if (executing_eagerly())
132-
{
133-
graph_mode();
134-
var result = graphAction();
135-
restore_mode();
136-
return result;
137-
}
138-
else
139-
{
140-
var result = graphAction();
141-
if (tf.Runner.MustRecordGradient())
142-
recordGradient(result[0].op);
143-
return result;
144-
}
145-
}
146-
}
147-
148106
public void Dispose()
149-
=> Handle.Dispose();
107+
=> _handle.Dispose();
150108
}
151109
}

0 commit comments

Comments
 (0)