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 7.2 kB

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