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.6 kB

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