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

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