You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

FuncGraph.cs 21 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. using Google.Protobuf;
  2. using System;
  3. using System.Buffers;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using Tensorflow.Eager;
  7. using Tensorflow.Exceptions;
  8. using Tensorflow.Framework;
  9. using Tensorflow.Framework.Models;
  10. using Tensorflow.Functions;
  11. using Tensorflow.Operations;
  12. using Tensorflow.Util;
  13. using static Tensorflow.Binding;
  14. namespace Tensorflow.Graphs;
  15. /// <summary>
  16. /// Graph representing a function body.
  17. /// </summary>
  18. public class FuncGraph : Graph, IDisposable
  19. {
  20. internal SafeFuncGraphHandle _func_graph_handle;
  21. internal HashSet<Tensor> _resource_tensor_inputs;
  22. internal HashSet<WeakReference<IVariableV1>> _watched_variables;
  23. internal IEnumerable<WeakReference<IVariableV1>> _weak_variables;
  24. internal object[] _structured_outputs;
  25. internal Dictionary<long, string> _output_names;
  26. public string FuncName => _graph_key;
  27. public Tensors Inputs { get; set; } = new Tensors();
  28. public Tensors Outputs { get; set; } = new Tensors();
  29. public Tensors FlatStructuredOutputs
  30. {
  31. get
  32. {
  33. List<Tensor> res = new();
  34. foreach(var obj in _structured_outputs)
  35. {
  36. if(obj is Tensor tensor)
  37. {
  38. res.Add(tensor);
  39. }
  40. else if(obj is IEnumerable<Tensor> tensors)
  41. {
  42. res.AddRange(tensors);
  43. }
  44. else
  45. {
  46. throw new TypeError("The structured outputs member should be tensor or tensors.");
  47. }
  48. }
  49. return res;
  50. }
  51. }
  52. public string Name { get; set; }
  53. public IEnumerable<IVariableV1> Variables
  54. {
  55. get
  56. {
  57. return _weak_variables.Select(v =>
  58. {
  59. if (v.TryGetTarget(out var target))
  60. {
  61. return target;
  62. }
  63. else
  64. {
  65. throw new AssertionError("Called a function referencing variables which have been deleted. " +
  66. "This likely means that function-local variables were created and " +
  67. "not referenced elsewhere in the program. This is generally a " +
  68. "mistake; consider storing variables in an object attribute on first call.");
  69. }
  70. });
  71. }
  72. internal set
  73. {
  74. _weak_variables = value.Select(x => new WeakReference<IVariableV1>(x));
  75. }
  76. }
  77. public IEnumerable<IVariableV1> TrainableVariables => Variables.Where(v => v.Trainable);
  78. public Dictionary<string, AttrValue> Attrs { get; set; }
  79. Dictionary<long, (Tensor, Tensor)> _captures
  80. = new Dictionary<long, (Tensor, Tensor)>();
  81. public Tensor[] external_captures
  82. => _captures.Select(x => x.Value.Item1).ToArray();
  83. public (Tensor, Tensor)[] captures
  84. => _captures.Values.Select(x => x).ToArray();
  85. public Tensor[] internal_captures
  86. => _captures.Select(x => x.Value.Item2).ToArray();
  87. public Tensor[] captured_inputs
  88. => external_captures;
  89. /// <summary>
  90. /// Construct a new FuncGraph.
  91. /// </summary>
  92. public FuncGraph(string name) : base()
  93. {
  94. outer_graph = ops.get_default_graph();
  95. while (outer_graph.building_function)
  96. outer_graph = outer_graph.OuterGraph;
  97. _graph_key = Name = name;
  98. building_function = true;
  99. _weak_variables = new List<WeakReference<IVariableV1>>();
  100. _resource_tensor_inputs = new HashSet<Tensor>();
  101. _watched_variables = new HashSet<WeakReference<IVariableV1>>();
  102. }
  103. public FuncGraph(SafeGraphHandle handle, string name, Dictionary<string, AttrValue> attrs) : base()
  104. {
  105. outer_graph = ops.get_default_graph();
  106. while (outer_graph.building_function)
  107. outer_graph = outer_graph.OuterGraph;
  108. _graph_key = Name = name;
  109. building_function = true;
  110. Attrs = attrs;
  111. // Will to test if FuncGraph has memory leak
  112. // c_api.TF_DeleteGraph(_handle);
  113. _handle = handle;
  114. _weak_variables = new List<WeakReference<IVariableV1>>();
  115. _resource_tensor_inputs = new HashSet<Tensor>();
  116. _watched_variables = new HashSet<WeakReference<IVariableV1>>();
  117. }
  118. public void replace_capture(Tensor tensor, Tensor placeholder)
  119. {
  120. _captures[tensor.Id] = (tensor, placeholder);
  121. }
  122. public unsafe void ToGraph(Operation[] opers,
  123. Tensor[] inputs, Tensor[] outputs,
  124. string[] output_names)
  125. {
  126. var status = new Status();
  127. if (output_names is null)
  128. {
  129. output_names = new string[0];
  130. };
  131. _func_graph_handle = c_api.TF_GraphToFunction(_handle,
  132. _graph_key,
  133. false,
  134. opers.Length,
  135. opers.Select(x => (IntPtr)x).ToArray(),
  136. inputs.Length,
  137. inputs.Select(x => new TF_Output(x.op, 0)).ToArray(),
  138. outputs.Length,
  139. outputs.Select(x => new TF_Output(x.op, 0)).ToArray(),
  140. output_names.Length != outputs.Length ? null : output_names,
  141. IntPtr.Zero,
  142. null,
  143. status);
  144. status.Check(true);
  145. SetAttrs();
  146. // c_api.TF_GraphCopyFunction(outer_graph, _func_graph_handle, IntPtr.Zero, status.Handle);
  147. // status.Check(true);
  148. c_api.TFE_ContextAddFunction(tf.Context, _func_graph_handle, status);
  149. status.Check(true);
  150. _graph_key = c_api.StringPiece(c_api.TF_FunctionName(_func_graph_handle));
  151. Inputs = inputs;
  152. // mark_as_return
  153. Outputs = outputs;// .Select(x => array_ops.identity(x)).ToArray();
  154. }
  155. public override Operation create_op(string op_type, Tensor[] inputs, TF_DataType[] dtypes, TF_DataType[] input_types = null, string name = null, Dictionary<string, AttrValue> attrs = null, OpDef op_def = null, bool compute_device = true)
  156. {
  157. foreach(var (i, inp) in enumerate(inputs))
  158. inputs[i] = capture(inp);
  159. return base.create_op(op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device);
  160. }
  161. const int _EAGER_CONST_THRESHOLD = 128;
  162. public Tensor capture(Tensor tensor, string name = null, Shape shape = null)
  163. {
  164. if(tensor is EagerTensor)
  165. {
  166. if (name == null)
  167. name = ops.uid().ToString();
  168. // Small EagerTensors are captured with Const ops
  169. if (dtypes.is_value_dtype(tensor.dtype)
  170. && (tensor.rank == 0 || tensor.size < _EAGER_CONST_THRESHOLD))
  171. return capture_eager_tensor(tensor, name);
  172. // Large EagerTensors and resources are captured with Placeholder ops
  173. return _capture_helper(tensor, name, shape: shape);
  174. }
  175. if(tensor.graph != this)
  176. {
  177. if (name == null)
  178. name = tensor.op.name;
  179. var inner_graph = tensor.graph;
  180. while(inner_graph != null && inner_graph is FuncGraph inner_func_graph)
  181. {
  182. if (inner_graph == this)
  183. throw new InaccessibleTensorError($"The tensor '{tensor.name}' cannot be accessed here: it is defined" +
  184. " in another function or code block. Use return values," +
  185. " explicit Python locals or TensorFlow collections to access" +
  186. $" it. Defined in: {tensor.graph.graph_key}; accessed from: {graph_key}.");
  187. inner_graph = inner_func_graph.outer_graph;
  188. }
  189. return _capture_helper(tensor, name);
  190. }
  191. return tensor;
  192. }
  193. public void watch_variable(IVariableV1 v)
  194. {
  195. if (_resource_tensor_inputs.Contains(v.Handle))
  196. {
  197. return;
  198. }
  199. _watched_variables.Add(new WeakReference<IVariableV1>(v));
  200. //this = this.outer_graph;
  201. }
  202. Tensor capture_eager_tensor(Tensor tensor, string name)
  203. {
  204. Tensor graph_const = null;
  205. if (!_captures.ContainsKey(tensor.Id))
  206. {
  207. graph_const = tf_with(ops.control_dependencies(null), ctl
  208. => constant_op.constant(tensor.numpy(), dtype: tensor.dtype, shape: tensor.shape, name: name));
  209. add_capture(tensor, graph_const);
  210. }
  211. else
  212. {
  213. graph_const = _captures[tensor.Id].Item2;
  214. }
  215. BackwardFunction _backward_function_wrapper = (output_grads, unneeded_gradients) =>
  216. {
  217. return output_grads;
  218. };
  219. tf.Runner.RecordGradient("captured_value",
  220. new[] { graph_const }, null,
  221. new[] { tensor },
  222. getBackwardFunction: _backward_function_wrapper
  223. /*getForwardFunction: forward_function*/);
  224. return graph_const;
  225. }
  226. Tensor _capture_helper(Tensor tensor, string name, Shape shape = null)
  227. {
  228. Tensor placeholder = null;
  229. if (!_captures.ContainsKey(tensor.Id))
  230. {
  231. placeholder = _create_substitute_placeholder(tensor,
  232. name: name,
  233. dtype: tensor.dtype,
  234. shape: shape);
  235. add_capture(tensor, placeholder);
  236. }
  237. else
  238. {
  239. placeholder = _captures[tensor.Id].Item2;
  240. }
  241. BackwardFunction _backward_function_wrapper = (output_grads, unneeded_gradients) =>
  242. {
  243. return output_grads;
  244. };
  245. tf.Runner.RecordGradient("captured_value",
  246. new[] { placeholder }, null,
  247. new[] { tensor },
  248. getBackwardFunction: _backward_function_wrapper
  249. /*getForwardFunction: forward_function*/);
  250. return placeholder;
  251. }
  252. void add_capture(Tensor tensor, Tensor placeholder)
  253. {
  254. _captures.Add(tensor.Id, (tensor, placeholder));
  255. Inputs.Add(placeholder);
  256. }
  257. Tensor pop_capture(Tensor tensor)
  258. {
  259. if(_captures.TryGetValue(tensor.Id, out var capture))
  260. {
  261. _captures.Remove(tensor.Id);
  262. return capture.Item2;
  263. }
  264. else
  265. {
  266. return null;
  267. }
  268. }
  269. Tensor _create_substitute_placeholder(Tensor value,
  270. string name = null,
  271. TF_DataType dtype = TF_DataType.DtInvalid,
  272. Shape shape = null)
  273. {
  274. if (shape is null)
  275. shape = value.shape;
  276. if (dtype == TF_DataType.DtInvalid)
  277. dtype = value.dtype;
  278. var placeholder = tf_with(ops.control_dependencies(null), ctl
  279. => array_ops.placeholder(dtype, shape: shape, name: name));
  280. // custom_gradient.copy_handle_data(value, placeholder)
  281. return placeholder;
  282. }
  283. void SetAttrs()
  284. {
  285. if (Attrs == null)
  286. return;
  287. foreach (var (_name, attr_value) in enumerate(Attrs))
  288. {
  289. var serialized = attr_value.ToByteArray();
  290. c_api.TF_FunctionSetAttrValueProto(_func_graph_handle, _name, serialized, serialized.Length, tf.Status);
  291. tf.Status.Check(true);
  292. }
  293. }
  294. public override Graph as_default()
  295. {
  296. tf.Context.graph_mode(isFunc: true);
  297. ops.set_default_graph(this);
  298. return this;
  299. }
  300. public override void Exit()
  301. {
  302. tf.Context.restore_mode();
  303. ops.pop_graph();
  304. }
  305. public void Dispose()
  306. {
  307. c_api.TFE_ContextRemoveFunction(tf.Context, _graph_key, tf.Status);
  308. }
  309. public static FuncGraph func_graph_from_func(string name, Func<object[], object[]> func,
  310. object[] args, Dictionary<string, object> kwargs, TensorSpec[] signature = null,
  311. FuncGraph func_graph = null, bool autograph = false, object autograph_options = null,
  312. bool add_control_dependencies = true, string[] arg_names = null,
  313. Tensor op_return_value = null, bool capture_by_value = false,
  314. bool acd_record_initial_resource_uses = false)
  315. {
  316. if(func_graph is null)
  317. {
  318. func_graph = new FuncGraph(name);
  319. }
  320. // TODO(Rinne): deal with control dependencies.
  321. func_graph.as_default();
  322. var current_scope = variable_scope.get_variable_scope();
  323. var default_use_resource = current_scope.use_resource;
  324. current_scope.use_resource = true;
  325. if(signature is not null)
  326. {
  327. args = signature;
  328. kwargs = new Dictionary<string, object>();
  329. }
  330. var func_args = _get_defun_inputs_from_args(args, arg_names);
  331. var func_kwargs = _get_defun_inputs_from_kwargs(kwargs);
  332. if(func_kwargs is not null && func_kwargs.Count > 0)
  333. {
  334. throw new NotImplementedException("The keyword args has not been supported in `func_graph_from_func`.");
  335. }
  336. foreach(var arg in nest.flatten<object>(new object[] { func_args, func_kwargs }))
  337. {
  338. if(arg is Tensor tensor && tensor.dtype == dtypes.resource)
  339. {
  340. func_graph._resource_tensor_inputs.Add(tensor);
  341. }
  342. else if (arg is ResourceVariable variable)
  343. {
  344. func_graph._resource_tensor_inputs.Add(variable.Handle);
  345. }
  346. }
  347. // skip the assignment of `func_graph.structured_input_signature`.
  348. var flat_func_args = nest.flatten(func_args as object);
  349. var flat_func_kwargs = nest.flatten(func_kwargs as object);
  350. func_graph.Inputs = new Tensors(flat_func_args.concat(flat_func_kwargs)
  351. .Where(x => x is Tensor).Select(x => (Tensor)x));
  352. //var func_args_before = nest.pack_sequence_as(func_args, flat_func_args, true);
  353. //var func_kwargs_before = nest.pack_sequence_as(func_kwargs, flat_func_kwargs, true);
  354. Tensor convert(object x)
  355. {
  356. if (x is null) return null;
  357. Tensor res = null;
  358. if(op_return_value is not null && x is Operation)
  359. {
  360. tf_with(ops.control_dependencies(new object[] { x }), _ =>
  361. {
  362. res = array_ops.identity(op_return_value);
  363. });
  364. }
  365. else if(x is not TensorArray)
  366. {
  367. Debug.Assert(x is Tensor);
  368. res = ops.convert_to_tensor_or_composite(x as Tensor);
  369. }
  370. else
  371. {
  372. throw new NotImplementedException($"The `TensorArray` is not supported here currently.");
  373. }
  374. if (add_control_dependencies)
  375. {
  376. // TODO(Rinne): `x = deps_ctx.mark_as_return(x)`.
  377. }
  378. return res;
  379. }
  380. if (autograph)
  381. {
  382. throw new NotImplementedException("The autograph of `func_graph_from_func` has not been supported.");
  383. }
  384. var func_outputs = func(func_args);
  385. func_outputs = variable_utils.convert_variables_to_tensors(func_outputs);
  386. func_outputs = func_outputs.Select(x => convert(x)).ToArray();
  387. // TODO(Rinne): `check_func_mutation`.
  388. current_scope.use_resource = default_use_resource;
  389. var graph_variables = func_graph._watched_variables.ToList();
  390. HashSet<IVariableV1> arg_variables = new HashSet<IVariableV1>();
  391. List<Tensor> inputs = new();
  392. foreach(var arg in composite_tensor_utils.flatten_with_variables(func_args))
  393. {
  394. if(arg is BaseResourceVariable variable)
  395. {
  396. var resource_placeholder = func_graph.pop_capture(variable.Handle);
  397. if(resource_placeholder is null)
  398. {
  399. continue;
  400. }
  401. Debug.Assert(variable is IVariableV1);
  402. arg_variables.Add(variable as IVariableV1);
  403. inputs.Add(resource_placeholder);
  404. }
  405. else if(arg is Tensor tensor)
  406. {
  407. inputs.Add(tensor);
  408. }
  409. }
  410. var variables = graph_variables.Select(v =>
  411. {
  412. if (v.TryGetTarget(out var target))
  413. {
  414. return target;
  415. }
  416. else
  417. {
  418. return null;
  419. }
  420. }).Where(v => v is not null && !arg_variables.Contains(v));
  421. func_graph.Inputs = inputs.Concat(func_graph.internal_captures).ToArray();
  422. func_graph._structured_outputs = func_outputs;
  423. func_graph.Outputs.AddRange(func_graph.FlatStructuredOutputs.Where(x => x is not null)
  424. .Select(x => func_graph.capture(x)));
  425. func_graph.Variables = variables;
  426. func_graph.Exit();
  427. if (add_control_dependencies)
  428. {
  429. // TODO(Rinne): implement it.
  430. }
  431. return func_graph;
  432. }
  433. private static object[] _get_defun_inputs_from_args(object[] args, string[] names)
  434. {
  435. return _get_defun_inputs(args, names, args) as object[];
  436. }
  437. private static Dictionary<string, object> _get_defun_inputs_from_kwargs(Dictionary<string, object> kwargs)
  438. {
  439. // TODO(Rinne): implement it.
  440. Debug.Assert(kwargs is null || kwargs.Count == 0);
  441. return kwargs;
  442. //string[] names;
  443. //object[] args;
  444. //if(kwargs is not null && kwargs.Count > 0)
  445. //{
  446. // var sorted_kwargs = kwargs.OrderBy(x => x.Key);
  447. // names = sorted_kwargs.Select(x => x.Key).ToArray();
  448. // args = sorted_kwargs.Select(x => x.Value).ToArray();
  449. //}
  450. //else
  451. //{
  452. // names = new string[0];
  453. // args = new object[0];
  454. //}
  455. //return _get_defun_inputs(args, names, kwargs) as Dictionary<string, object>;
  456. }
  457. private static object _get_defun_inputs(object[] args, string[] names, object structured_args)
  458. {
  459. List<object> function_inputs = new();
  460. if(names is null)
  461. {
  462. names = new string[args.Length];
  463. }
  464. foreach(var (arg_value, name) in zip(args, names))
  465. {
  466. foreach(var val in composite_tensor_utils.flatten_with_variables_or_variable_specs(arg_value))
  467. {
  468. function_inputs.Add(_get_defun_input(val, name));
  469. }
  470. }
  471. return nest.pack_sequence_as(structured_args, nest.flatten<object>(function_inputs), true);
  472. }
  473. private static object _get_defun_input(object arg, string name)
  474. {
  475. var func_graph = ops.get_default_graph() as FuncGraph;
  476. Debug.Assert(func_graph is not null);
  477. if (arg is Tensor tensor)
  478. {
  479. Tensor placeholder;
  480. try
  481. {
  482. placeholder = tf.placeholder(tensor.dtype, tensor.shape, name);
  483. }
  484. catch (ValueError)
  485. {
  486. // TODO(Rinne): Add warning here.
  487. placeholder = tf.placeholder(tensor.dtype, tensor.shape);
  488. }
  489. handle_data_util.copy_handle_data(tensor, placeholder);
  490. if (name is not null)
  491. {
  492. placeholder.op._set_attr("_user_specified_name", new AttrValue()
  493. {
  494. S = tf.compat.as_bytes(name)
  495. });
  496. }
  497. return placeholder;
  498. }
  499. else if (arg is TensorSpec spec)
  500. {
  501. string requested_name;
  502. if (!string.IsNullOrEmpty(spec.name))
  503. {
  504. requested_name = spec.name;
  505. }
  506. else
  507. {
  508. requested_name = name;
  509. }
  510. Tensor placeholder;
  511. try
  512. {
  513. placeholder = tf.placeholder(spec.dtype, spec.shape, requested_name);
  514. }
  515. catch (ValueError)
  516. {
  517. // TODO(Rinne): Add warning here.
  518. placeholder = tf.placeholder(spec.dtype, spec.shape);
  519. }
  520. if (name is not null)
  521. {
  522. placeholder.op._set_attr("_user_specified_name", new AttrValue()
  523. {
  524. S = tf.compat.as_bytes(requested_name)
  525. });
  526. }
  527. return placeholder;
  528. }
  529. else if (arg is BaseResourceVariable variable)
  530. {
  531. var placeholder = func_graph.capture(variable.Handle, name);
  532. placeholder.op._set_attr("_user_specified_name", new AttrValue()
  533. {
  534. S = tf.compat.as_bytes(name)
  535. });
  536. return arg;
  537. }
  538. // TODO(Rinne): deal with `VariableSpec`.
  539. else
  540. {
  541. return arg;
  542. }
  543. }
  544. }