Repository navigation
Expand file tree
/
Copy pathGraph.cs
More file actions
234 lines (198 loc) · 7.16 KB
/
Copy pathGraph.cs
File metadata and controls
234 lines (198 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using TF_DataType = Tensorflow.DataType;
namespace Tensorflow
{
/// <summary>
/// TensorFlow uses a dataflow graph to represent your computation in terms of the dependencies between individual operations.
/// This leads to a low-level programming model in which you first define the dataflow graph,
/// then create a TensorFlow session to run parts of the graph across a set of local and remote devices.
/// https://www.tensorflow.org/guide/graphs
/// </summary>
public partial class Graph : IDisposable
{
private IntPtr _handle;
private Dictionary<int, Operation> _nodes_by_id;
private Dictionary<string, Operation> _nodes_by_name;
private Dictionary<string, int> _names_in_use;
public int _version;
private int _next_id_counter;
private List<String> _unfetchable_ops = new List<string>();
private string _name_stack;
public string _graph_key;
public Status Status { get; }
public Graph()
{
_handle = c_api.TF_NewGraph();
Status = new Status();
}
public Graph(IntPtr graph)
{
_handle = graph;
Status = new Status();
_nodes_by_id = new Dictionary<int, Operation>();
_nodes_by_name = new Dictionary<string, Operation>();
_names_in_use = new Dictionary<string, int>();
}
public T as_graph_element<T>(T obj, bool allow_tensor = true, bool allow_operation = true)
{
return _as_graph_element_locked(obj, allow_tensor, allow_operation);
}
private Func<object> _as_graph_element(object obj)
{
return null;
}
private T _as_graph_element_locked<T>(T obj, bool allow_tensor = true, bool allow_operation = true)
{
string types_str = "";
if (allow_tensor && allow_operation)
{
types_str = "Tensor or Operation";
}
else if (allow_tensor)
{
types_str = "Tensor";
}
else if (allow_operation)
{
types_str = "Operation";
}
var temp_obj = _as_graph_element(obj);
if(obj is Tensor && allow_tensor)
{
if ((obj as Tensor).Graph.Equals(this))
{
return obj;
}
else
{
throw new Exception($"Tensor {obj} is not an element of this graph.");
}
}
throw new Exception($"Can not convert a {typeof(T).Name} into a {types_str}.");
}
public unsafe Operation create_op(string op_type, List<Tensor> inputs, TF_DataType[] dtypes,
TF_DataType[] input_types = null, string name = "",
Dictionary<string, AttrValue> attrs = null, OpDef op_def = null)
{
if (String.IsNullOrEmpty(name))
{
name = op_type;
}
name = name.EndsWith("/") ? ops._name_from_scope_name(name) : unique_name(name);
var node_def = ops._NodeDef(op_type, name, device: "", attrs: attrs);
var op = new Operation(node_def,
this,
inputs: inputs,
output_types: dtypes,
control_inputs: new object[] { },
input_types: input_types,
original_op: null,
op_def: op_def);
_create_op_helper(op, true);
return op;
}
private void _create_op_helper(Operation op, bool compute_device = true)
{
}
public void _add_op(Operation op)
{
_nodes_by_id[op._id] = op;
//_nodes_by_name[op.name] = op;
_version = Math.Max(_version, op._id);
}
public int _next_id()
{
return ++_next_id_counter;
}
public bool is_fetchable<T>(T tensor_or_op)
{
if (tensor_or_op is Tensor)
{
return !_unfetchable_ops.Contains((tensor_or_op as Tensor).name); ;
}
else if (tensor_or_op is Operation)
{
return !_unfetchable_ops.Contains((tensor_or_op as Operation).Name);
}
return false;
}
public string name_scope(string name)
{
string new_stack = "";
if (name.EndsWith("/"))
{
new_stack = ops._name_from_scope_name(name);
}
else
{
new_stack = unique_name(name);
}
_name_stack = new_stack;
return String.IsNullOrEmpty(new_stack) ? "" : new_stack + "/";
}
public string unique_name(string name)
{
if (!String.IsNullOrEmpty(_name_stack))
{
name = _name_stack + "/" + name;
}
var name_key = name.ToLower();
if (_names_in_use.ContainsKey(name_key))
{
_names_in_use[name_key]++;
}
else
{
_names_in_use[name_key] = 1;
return name;
}
return $"{name}_{_names_in_use[name_key]}";
}
public TF_Output[] ReturnOutputs(IntPtr results)
{
IntPtr return_output_handle = IntPtr.Zero;
int num_return_outputs = 0;
c_api.TF_ImportGraphDefResultsReturnOutputs(results, ref num_return_outputs, ref return_output_handle);
TF_Output[] return_outputs = new TF_Output[num_return_outputs];
for (int i = 0; i < num_return_outputs; i++)
{
var handle = return_output_handle + (Marshal.SizeOf<TF_Output>() * i);
return_outputs[i] = Marshal.PtrToStructure<TF_Output>(handle);
}
return return_outputs;
}
public unsafe Operation[] ReturnOperations(IntPtr results)
{
TF_Operation return_oper_handle = new TF_Operation();
int num_return_opers = 0;
c_api.TF_ImportGraphDefResultsReturnOperations(results, ref num_return_opers, ref return_oper_handle);
Operation[] return_opers = new Operation[num_return_opers];
for (int i = 0; i < num_return_opers; i++)
{
var handle = return_oper_handle.node + Marshal.SizeOf<TF_Operation>() * i;
return_opers[i] = new Operation(*(IntPtr*)handle);
}
return return_opers;
}
public Operation OperationByName(string operName)
{
return c_api.TF_GraphOperationByName(_handle, operName);
}
public Operation[] get_operations()
{
return _nodes_by_name.Values.Select(x => x).ToArray();
}
public void Dispose()
{
c_api.TF_DeleteGraph(_handle);
}
public static implicit operator IntPtr(Graph graph)
{
return graph._handle;
}
}
}