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.

Context.ExecuteOp.cs 3.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using Tensorflow.Eager;
  17. using static Tensorflow.Binding;
  18. using Google.Protobuf;
  19. using System.Collections.Generic;
  20. namespace Tensorflow.Contexts
  21. {
  22. /// <summary>
  23. /// Environment in which eager operations execute.
  24. /// </summary>
  25. public sealed partial class Context
  26. {
  27. // [DebuggerStepThrough]
  28. public Tensors ExecuteOp(string OpType, string Name, ExecuteOpArgs args)
  29. {
  30. Func<Tensors> graphAction = () =>
  31. {
  32. var keywords = new Dictionary<string, object>();
  33. if(args.OpInputArgs != null)
  34. {
  35. foreach (var (i, input) in enumerate(args.OpInputArgs))
  36. keywords[$"input_{i}"] = input;
  37. }
  38. if(args.OpAttrs != null)
  39. {
  40. foreach (var attr in args.OpAttrs)
  41. keywords[attr.Key] = attr.Value;
  42. }
  43. return tf.OpDefLib._apply_op_helper(OpType, Name, keywords).outputs;
  44. };
  45. Func<Tensors> eagerAction = () =>
  46. {
  47. return tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(OpType, Name, args.OpInputArgs)
  48. {
  49. attrs = args.OpAttrs
  50. });
  51. };
  52. if (tf.Context.has_graph_arg(args.OpInputArgs))
  53. {
  54. if (executing_eagerly())
  55. {
  56. graph_mode();
  57. var result = graphAction();
  58. restore_mode();
  59. return result;
  60. }
  61. else
  62. {
  63. var result = graphAction();
  64. if (tf.Runner.MustRecordGradient())
  65. {
  66. var op = result[0].op;
  67. Dictionary<string, object> attrs;
  68. if (args.GetGradientAttrs == null)
  69. {
  70. attrs = new Dictionary<string, object>();
  71. attrs["T"] = op.get_attr<TF_DataType>("T");
  72. }
  73. else
  74. {
  75. attrs = ConvertToDict(args.GetGradientAttrs(op));
  76. }
  77. var args1 = new object[attrs.Count() * 2];
  78. int i = 0;
  79. foreach (var arg in attrs)
  80. {
  81. args1[i] = arg.Key;
  82. args1[i + 1] = arg.Value;
  83. i += 2;
  84. }
  85. tf.Runner.RecordGradient(OpType, op.inputs, args1, op.outputs);
  86. }
  87. return result;
  88. }
  89. }
  90. else
  91. {
  92. return eagerAction();
  93. }
  94. }
  95. }
  96. }