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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.Type)
  152. {
  153. List<TF_DataType> values = new();
  154. foreach (var value in attr.DefaultValue.List.Type)
  155. {
  156. values.Add(value.as_tf_dtype());
  157. }
  158. string expression = "new TF_DataType[]{" + $"{string.Join(", ", values)}" + "}";
  159. dynamicDefaultValues[attr.Name] = expression;
  160. res.Add((attr.Name, "TF_DataType[]", $"null"));
  161. }
  162. else
  163. {
  164. res.Add((attr.Name, "TF_DataType[]", "NOVALUE"));
  165. }
  166. }
  167. else if (attr.Type == "list(shape)")
  168. {
  169. res.Add((attr.Name, "Shape[]", "NOVALUE"));
  170. }
  171. else if (attr.Type == "list(string)")
  172. {
  173. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.S)
  174. {
  175. List<string> values = new();
  176. foreach (var value in attr.DefaultValue.List.S)
  177. {
  178. values.Add(value.ToStringUtf8());
  179. }
  180. string expression = "new string[]{" + $"{string.Join(", ", values)}" + "}";
  181. dynamicDefaultValues[attr.Name] = expression;
  182. res.Add((attr.Name, "string[]", $"null"));
  183. }
  184. else
  185. {
  186. res.Add((attr.Name, "string[]", "NOVALUE"));
  187. }
  188. }
  189. else if (attr.Type == "list(int)")
  190. {
  191. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List)
  192. {
  193. List<int> values = new();
  194. foreach (var value in attr.DefaultValue.List.I)
  195. {
  196. values.Add((int)value);
  197. }
  198. string expression = "new int[]{" + $"{string.Join(", ", values)}" + "}";
  199. dynamicDefaultValues[attr.Name] = expression;
  200. res.Add((attr.Name, "int[]", $"null"));
  201. }
  202. else
  203. {
  204. res.Add((attr.Name, "int[]", "NOVALUE"));
  205. }
  206. }
  207. else if (attr.Type == "list(float)")
  208. {
  209. if (attr.DefaultValue is not null && attr.DefaultValue.ValueCase == AttrValue.ValueOneofCase.List)
  210. {
  211. List<float> values = new();
  212. foreach (var value in attr.DefaultValue.List.F)
  213. {
  214. values.Add(value);
  215. }
  216. string expression = "new float[]{" + $"{string.Join(", ", values)}" + "}";
  217. dynamicDefaultValues[attr.Name] = expression;
  218. res.Add((attr.Name, "float[]", $"null"));
  219. }
  220. else
  221. {
  222. res.Add((attr.Name, "float[]", "NOVALUE"));
  223. }
  224. }
  225. else if (attr.Type == "func")
  226. {
  227. res.Add((attr.Name, "Func<Tensors, Tensors>", "NOVALUE"));
  228. }
  229. else if (attr.Type == "list(func)")
  230. {
  231. res.Add((attr.Name, "Func<Tensors, Tensors>[]", "NOVALUE"));
  232. }
  233. else if (attr.Type == "tensor")
  234. {
  235. res.Add((attr.Name, "TensorProto", "NOVALUE"));
  236. }
  237. else
  238. {
  239. throw new NotImplementedException();
  240. }
  241. }
  242. return res;
  243. }
  244. }
  245. }