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

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