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.

AutoGraphAttribute.cs 1.7 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*using MethodBoundaryAspect.Fody.Attributes;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using Tensorflow.Eager;
  7. using static Tensorflow.Binding;
  8. namespace Tensorflow.Graphs
  9. {
  10. public sealed class AutoGraphAspect : OnMethodBoundaryAspect
  11. {
  12. FuncGraph graph;
  13. IntPtr func_handle;
  14. public override void OnEntry(MethodExecutionArgs args)
  15. {
  16. tf.compat.v1.disable_eager_execution();
  17. // convert args to placeholder
  18. for (var i = 0; i < args.Arguments.Length; i++)
  19. {
  20. if (args.Arguments[i] is EagerTensor tensor)
  21. args.Arguments[i] = tf.placeholder(tensor.dtype, shape: tensor.TensorShape);
  22. }
  23. // make function as an Operation by autograph
  24. graph = new FuncGraph("autograph_add");
  25. graph.as_default();
  26. }
  27. public override void OnExit(MethodExecutionArgs args)
  28. {
  29. var output = (Tensor)args.Method.Invoke(args.Instance, args.Arguments);
  30. var opers = graph._nodes_by_name.Values.Select(x => x as Operation).ToArray();
  31. func_handle = graph.ToGraph(opers,
  32. new Operation[] { },
  33. new Operation[] { },
  34. null);
  35. c_api.TFE_ContextAddFunction(tf.Context.Handle, func_handle, tf.Status.Handle);
  36. var a1 = tf.constant(1);
  37. var b1 = tf.constant(2);
  38. var result = tf.Runner.TFE_Execute(tf.Context,
  39. tf.Context.DeviceName,
  40. "autograph_add",
  41. new[] { a1, b1 },
  42. null,
  43. 1);
  44. graph.Dispose();
  45. }
  46. }
  47. }*/