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.

OpClassifier.cs 1.5 kB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Text.RegularExpressions;
  7. namespace Tensorflow.CodeGen
  8. {
  9. public class OpClassifier
  10. {
  11. private static readonly string _filenamePattern = @"^gen_[a-z]*_ops.py$";
  12. private static readonly string _pythonFunctionPattern = @"def\s+(\w+)\((?:\s*\w+\s*(?:=\s*[\S]*)*,\s*)*\s*\w+\s*=None\s*\):";
  13. private Dictionary<string, HashSet<string>> _opSet = new();
  14. public Dictionary<string, HashSet<string>> OpSet => _opSet;
  15. public OpClassifier(string pythonFileFolder)
  16. {
  17. DirectoryInfo directory = new DirectoryInfo(pythonFileFolder);
  18. foreach (FileInfo file in directory.GetFiles())
  19. {
  20. if (Regex.IsMatch(file.Name, _filenamePattern))
  21. {
  22. string filenamePrefix = file.Name.Split('.')[0];
  23. string content = File.ReadAllText(file.FullName);
  24. var matches = Regex.Matches(content, _pythonFunctionPattern);
  25. foreach(Match match in matches)
  26. {
  27. var funcName = match.Groups[1].Value;
  28. if (!funcName.EndsWith("_eager_fallback"))
  29. {
  30. _opSet.SetDefault(filenamePrefix, new HashSet<string>()).Add(funcName);
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }