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

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