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

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. } else
  81. {
  82. return false;
  83. }
  84. } else if (attr.Key == "N")
  85. {
  86. if (attr.Value.I == n)
  87. {
  88. found_n = true;
  89. } else
  90. {
  91. return false;
  92. }
  93. }
  94. }
  95. return found_t && found_n;
  96. }
  97. public static bool IsNeg(NodeDef node_def, string input)
  98. {
  99. return node_def.Op == "Neg" && node_def.Name == "neg" &&
  100. node_def.Input.Count == 1 && node_def.Input[0] == input;
  101. }
  102. public static bool IsPlaceholder(NodeDef node_def)
  103. {
  104. if (node_def.Op != "Placeholder" || node_def.Name != "feed")
  105. {
  106. return false;
  107. }
  108. bool found_dtype = false;
  109. bool found_shape = false;
  110. foreach (var attr in node_def.Attr)
  111. {
  112. if (attr.Key == "dtype")
  113. {
  114. if (attr.Value.Type == DataType.DtInt32)
  115. {
  116. found_dtype = true;
  117. } else
  118. {
  119. return false;
  120. }
  121. } else if (attr.Key == "shape")
  122. {
  123. found_shape = true;
  124. }
  125. }
  126. return found_dtype && found_shape;
  127. }
  128. public static bool IsScalarConst(NodeDef node_def, int v)
  129. {
  130. if (node_def.Op != "Const" || node_def.Name != "scalar")
  131. {
  132. return false;
  133. }
  134. bool found_dtype = false;
  135. bool found_value = false;
  136. foreach (var attr in node_def.Attr)
  137. {
  138. if (attr.Key == "dtype")
  139. {
  140. if (attr.Value.Type == DataType.DtInt32)
  141. {
  142. found_dtype = true;
  143. } else
  144. {
  145. return false;
  146. }
  147. } else if (attr.Key == "value")
  148. {
  149. if (attr.Value.Tensor != null &&
  150. attr.Value.Tensor.IntVal.Count == 1 &&
  151. attr.Value.Tensor.IntVal[0] == v)
  152. {
  153. found_value = true;
  154. } else
  155. {
  156. return false;
  157. }
  158. }
  159. }
  160. return found_dtype && found_value;
  161. }
  162. public static Operation Neg(Operation n, Graph graph, Status s, string name = "neg")
  163. {
  164. lock (Locks.ProcessWide)
  165. {
  166. OperationDescription desc = c_api.TF_NewOperation(graph, "Neg", name);
  167. var neg_input = new TF_Output(n, 0);
  168. c_api.TF_AddInput(desc, neg_input);
  169. var op = c_api.TF_FinishOperation(desc, s.Handle);
  170. s.Check();
  171. return op;
  172. }
  173. }
  174. public static Operation Placeholder(Graph graph, Status s, string name = "feed", TF_DataType dtype = TF_DataType.TF_INT32, long[] dims = null)
  175. {
  176. lock (Locks.ProcessWide)
  177. {
  178. var desc = c_api.TF_NewOperation(graph, "Placeholder", name);
  179. c_api.TF_SetAttrType(desc, "dtype", dtype);
  180. if (dims != null)
  181. {
  182. c_api.TF_SetAttrShape(desc, "shape", dims, dims.Length);
  183. }
  184. var op = c_api.TF_FinishOperation(desc, s.Handle);
  185. s.Check();
  186. return op;
  187. }
  188. }
  189. public static Operation Const(Tensor t, Graph graph, Status s, string name)
  190. {
  191. lock (Locks.ProcessWide)
  192. {
  193. var desc = c_api.TF_NewOperation(graph, "Const", name);
  194. c_api.TF_SetAttrTensor(desc, "value", t, s.Handle);
  195. s.Check();
  196. c_api.TF_SetAttrType(desc, "dtype", t.dtype);
  197. var op = c_api.TF_FinishOperation(desc, s.Handle);
  198. s.Check();
  199. return op;
  200. }
  201. }
  202. public static Operation ScalarConst(int v, Graph graph, Status s, string name = "scalar")
  203. {
  204. return Const(new Tensor(v), graph, s, name);
  205. }
  206. }
  207. }