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.

Utils.cs 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using Protobuf.Text;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection.Metadata.Ecma335;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Tensorflow.CodeGen
  9. {
  10. public static class Utils
  11. {
  12. public static string ConvertToUnderscore(string input)
  13. {
  14. if (string.IsNullOrEmpty(input))
  15. {
  16. return input;
  17. }
  18. StringBuilder result = new StringBuilder();
  19. int state = 1; // the previous char was not lowered.
  20. for (int i = 0; i < input.Length; i++)
  21. {
  22. char current = input[i];
  23. // 首字母不需要添加下划线
  24. if (char.IsUpper(current))
  25. {
  26. if(i > 0)
  27. {
  28. char pre = input[i - 1];
  29. if (char.IsDigit(pre))
  30. {
  31. result.Append(char.ToLower(current));
  32. continue;
  33. }
  34. }
  35. if (state == 0)
  36. {
  37. result.Append("_");
  38. state = 1;
  39. }
  40. result.Append(char.ToLower(current));
  41. }
  42. else
  43. {
  44. result.Append(char.ToLower(current));
  45. state = 0;
  46. }
  47. }
  48. return result.ToString();
  49. }
  50. public static OpList ReadAllOpDefs(string path)
  51. {
  52. var text = File.ReadAllText(path);
  53. var opDefs = OpList.Parser.ParseText(text);
  54. return opDefs;
  55. }
  56. // name, type string, default value
  57. public static List<(string, string, string)> GetAttrsDefaultValue(OpDef op, out Dictionary<string, string> dynamicDefaultValues)
  58. {
  59. dynamicDefaultValues = new();
  60. List<(string, string, string)> res = new();
  61. foreach (var attr in op.Attr)
  62. {
  63. if (attr.Type == "type")
  64. {
  65. bool found = op.InputArg.Any(x => x.TypeAttr == attr.Name);
  66. if (!found)
  67. {
  68. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Type)
  69. {
  70. string name = Enum.GetName(typeof(TF_DataType), attr.DefaultValue.Type.as_tf_dtype());
  71. string enumPath = typeof(TF_DataType).Name + "." + name;
  72. res.Add((attr.Name, "TF_DataType", enumPath));
  73. }
  74. else
  75. {
  76. res.Add((attr.Name, "TF_DataType", "NOVALUE"));
  77. }
  78. }
  79. }
  80. else if (attr.Type == "int")
  81. {
  82. if (op.InputArg.Any(x => x.NumberAttr == attr.Name))
  83. {
  84. continue;
  85. }
  86. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.I)
  87. {
  88. res.Add((attr.Name, "int", attr.DefaultValue.I.ToString()));
  89. }
  90. else
  91. {
  92. res.Add((attr.Name, "int", "0"));
  93. }
  94. }
  95. else if (attr.Type == "float")
  96. {
  97. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.F)
  98. {
  99. res.Add((attr.Name, "float", attr.DefaultValue.F.ToString() + "f"));
  100. }
  101. else
  102. {
  103. res.Add((attr.Name, "float", "NOVALUE"));
  104. }
  105. }
  106. else if (attr.Type == "string")
  107. {
  108. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.S)
  109. {
  110. res.Add((attr.Name, "string", $"\"{attr.DefaultValue.S.ToStringUtf8()}\""));
  111. }
  112. else
  113. {
  114. res.Add((attr.Name, "string", "NOVALUE"));
  115. }
  116. }
  117. else if (attr.Type == "bool")
  118. {
  119. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.B)
  120. {
  121. res.Add((attr.Name, "bool", attr.DefaultValue.B.ToString().ToLower()));
  122. }
  123. else
  124. {
  125. res.Add((attr.Name, "bool", "NOVALUE"));
  126. }
  127. }
  128. else if (attr.Type == "shape")
  129. {
  130. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Shape)
  131. {
  132. if (attr.DefaultValue.Shape.UnknownRank)
  133. {
  134. res.Add((attr.Name, "Shape", $"null"));
  135. }
  136. else
  137. {
  138. Shape shape = new Shape(attr.DefaultValue.Shape);
  139. string expression = $"new Shape({string.Join(", ", shape.dims)})";
  140. dynamicDefaultValues[attr.Name] = expression;
  141. res.Add((attr.Name, "Shape", $"null"));
  142. }
  143. }
  144. else
  145. {
  146. res.Add((attr.Name, "Shape", "NOVALUE"));
  147. }
  148. }
  149. else if (attr.Type == "list(type)")
  150. {
  151. if(op.InputArg.Any(x => x.TypeListAttr == attr.Name))
  152. {
  153. continue;
  154. }
  155. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Type)
  156. {
  157. List<TF_DataType> values = new();
  158. foreach (var value in attr.DefaultValue.List.Type)
  159. {
  160. values.Add(value.as_tf_dtype());
  161. }
  162. string expression = "new TF_DataType[]{" + $"{string.Join(", ", values)}" + "}";
  163. dynamicDefaultValues[attr.Name] = expression;
  164. res.Add((attr.Name, "TF_DataType[]", $"null"));
  165. }
  166. else
  167. {
  168. res.Add((attr.Name, "TF_DataType[]", "NOVALUE"));
  169. }
  170. }
  171. else if (attr.Type == "list(shape)")
  172. {
  173. res.Add((attr.Name, "Shape[]", "NOVALUE"));
  174. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List)
  175. {
  176. List<string> exps = new();
  177. foreach (var value in attr.DefaultValue.List.Shape)
  178. {
  179. exps.Add($"new Shape({string.Join(", ", value.Dim.Select(x => x.Size))})");
  180. }
  181. string expression = "new Shape[]{" + $"{string.Join(", ", exps)}" + "}";
  182. dynamicDefaultValues[attr.Name] = expression;
  183. res.Add((attr.Name, "string[]", $"null"));
  184. }
  185. else
  186. {
  187. res.Add((attr.Name, "string[]", "NOVALUE"));
  188. }
  189. }
  190. else if (attr.Type == "list(string)")
  191. {
  192. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List)
  193. {
  194. List<string> values = new();
  195. foreach (var value in attr.DefaultValue.List.S)
  196. {
  197. values.Add(value.ToStringUtf8());
  198. }
  199. string expression = "new string[]{" + $"{string.Join(", ", values)}" + "}";
  200. dynamicDefaultValues[attr.Name] = expression;
  201. res.Add((attr.Name, "string[]", $"null"));
  202. }
  203. else
  204. {
  205. res.Add((attr.Name, "string[]", "NOVALUE"));
  206. }
  207. }
  208. else if (attr.Type == "list(int)")
  209. {
  210. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List)
  211. {
  212. List<int> values = new();
  213. foreach (var value in attr.DefaultValue.List.I)
  214. {
  215. values.Add((int)value);
  216. }
  217. string expression = "new int[]{" + $"{string.Join(", ", values)}" + "}";
  218. dynamicDefaultValues[attr.Name] = expression;
  219. res.Add((attr.Name, "int[]", $"null"));
  220. }
  221. else
  222. {
  223. res.Add((attr.Name, "int[]", "NOVALUE"));
  224. }
  225. }
  226. else if (attr.Type == "list(float)")
  227. {
  228. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List)
  229. {
  230. List<float> values = new();
  231. foreach (var value in attr.DefaultValue.List.F)
  232. {
  233. values.Add(value);
  234. }
  235. string expression = "new float[]{" + $"{string.Join(", ", values)}" + "}";
  236. dynamicDefaultValues[attr.Name] = expression;
  237. res.Add((attr.Name, "float[]", $"null"));
  238. }
  239. else
  240. {
  241. res.Add((attr.Name, "float[]", "NOVALUE"));
  242. }
  243. }
  244. else if (attr.Type == "func")
  245. {
  246. res.Add((attr.Name, "object", "NOVALUE"));
  247. }
  248. else if (attr.Type == "list(func)")
  249. {
  250. res.Add((attr.Name, "object[]", "NOVALUE"));
  251. }
  252. else if (attr.Type == "tensor")
  253. {
  254. res.Add((attr.Name, "TensorProto", "NOVALUE"));
  255. }
  256. else
  257. {
  258. throw new NotImplementedException();
  259. }
  260. }
  261. return res;
  262. }
  263. }
  264. }