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