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.

c_test_util.cs 6.5 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System.Diagnostics.CodeAnalysis;
  2. using Tensorflow;
  3. using Tensorflow.Util;
  4. using Buffer = Tensorflow.Buffer;
  5. namespace TensorFlowNET.UnitTest
  6. {
  7. /// <summary>
  8. /// Port from `tensorflow\c\c_test_util.cc`
  9. /// </summary>
  10. public static class c_test_util
  11. {
  12. public static Operation Add(Operation l, Operation r, Graph graph, Status s, string name = "add")
  13. {
  14. var desc = c_api.TF_NewOperation(graph, "AddN", name);
  15. var inputs = new TF_Output[]
  16. {
  17. new TF_Output(l, 0),
  18. new TF_Output(r, 0),
  19. };
  20. c_api.TF_AddInputList(desc, inputs, inputs.Length);
  21. var op = c_api.TF_FinishOperation(desc, s);
  22. s.Check();
  23. return op;
  24. }
  25. [SuppressMessage("ReSharper", "RedundantAssignment")]
  26. public static bool GetAttrValue(Operation oper, string attr_name, ref AttrValue attr_value, Status s)
  27. {
  28. using (var buffer = new Buffer())
  29. {
  30. c_api.TF_OperationGetAttrValueProto(oper, attr_name, buffer, s);
  31. attr_value = AttrValue.Parser.ParseFrom(buffer.MemoryBlock.Stream());
  32. }
  33. return s.Code == TF_Code.TF_OK;
  34. }
  35. public static GraphDef GetGraphDef(Graph graph)
  36. {
  37. using (var s = new Status())
  38. using (var buffer = new Buffer())
  39. {
  40. c_api.TF_GraphToGraphDef(graph, buffer, s);
  41. s.Check();
  42. return GraphDef.Parser.ParseFrom(buffer.MemoryBlock.Stream());
  43. }
  44. }
  45. public static bool IsAddN(NodeDef node_def, int n)
  46. {
  47. if (node_def.Op != "AddN" || node_def.Name != "add" ||
  48. node_def.Input.Count != n)
  49. {
  50. return false;
  51. }
  52. bool found_t = false;
  53. bool found_n = false;
  54. foreach (var attr in node_def.Attr)
  55. {
  56. if (attr.Key == "T")
  57. {
  58. if (attr.Value.Type == DataType.DtInt32)
  59. {
  60. found_t = true;
  61. }
  62. else
  63. {
  64. return false;
  65. }
  66. }
  67. else if (attr.Key == "N")
  68. {
  69. if (attr.Value.I == n)
  70. {
  71. found_n = true;
  72. }
  73. else
  74. {
  75. return false;
  76. }
  77. }
  78. }
  79. return found_t && found_n;
  80. }
  81. public static bool IsNeg(NodeDef node_def, string input)
  82. {
  83. return node_def.Op == "Neg" && node_def.Name == "neg" &&
  84. node_def.Input.Count == 1 && node_def.Input[0] == input;
  85. }
  86. public static bool IsPlaceholder(NodeDef node_def)
  87. {
  88. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  89. {
  90. return false;
  91. }
  92. bool found_dtype = false;
  93. bool found_shape = false;
  94. foreach (var attr in node_def.Attr)
  95. {
  96. if (attr.Key == "dtype")
  97. {
  98. if (attr.Value.Type == DataType.DtInt32)
  99. {
  100. found_dtype = true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. else if (attr.Key == "shape")
  108. {
  109. found_shape = true;
  110. }
  111. }
  112. return found_dtype && found_shape;
  113. }
  114. public static bool IsScalarConst(NodeDef node_def, int v)
  115. {
  116. if (node_def.Op != "Const" || node_def.Name != "scalar")
  117. {
  118. return false;
  119. }
  120. bool found_dtype = false;
  121. bool found_value = false;
  122. foreach (var attr in node_def.Attr) {
  123. if (attr.Key == "dtype")
  124. {
  125. if (attr.Value.Type == DataType.DtInt32)
  126. {
  127. found_dtype = true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. else if (attr.Key == "value")
  135. {
  136. if (attr.Value.Tensor != null &&
  137. attr.Value.Tensor.IntVal.Count == 1 &&
  138. attr.Value.Tensor.IntVal[0] == v)
  139. {
  140. found_value = true;
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. }
  147. }
  148. return found_dtype && found_value;
  149. }
  150. public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
  151. {
  152. OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
  153. var neg_input = new TF_Output(n, 0);
  154. c_api.TF_AddInput(desc, neg_input);
  155. var op = c_api.TF_FinishOperation(desc, s);
  156. s.Check();
  157. return op;
  158. }
  159. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  160. {
  161. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  162. c_api.TF_SetAttrType(desc, "dtype", dtype);
  163. if (dims != null)
  164. {
  165. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  166. }
  167. var op = c_api.TF_FinishOperation(desc, s);
  168. s.Check();
  169. return op;
  170. }
  171. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  172. {
  173. var desc = c_api.TF_NewOperation(graph, "Const", name);
  174. c_api.TF_SetAttrTensor(desc, "value", t, s);
  175. s.Check();
  176. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  177. var op = c_api.TF_FinishOperation(desc, s);
  178. s.Check();
  179. return op;
  180. }
  181. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  182. {
  183. return Const(new Tensor(v), graph, s, name);
  184. }
  185. }
  186. }