generate_bytecode.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. # mypy: allow-untyped-defs
  2. from typing import List
  3. from torch._C import _compile_graph_to_code_table, _generate_upgraders_graph
  4. def format_bytecode(table):
  5. # given a nested tuple, convert it to nested list
  6. def listify(content):
  7. if not isinstance(content, tuple):
  8. return content
  9. return [listify(i) for i in content]
  10. formatted_table = {}
  11. for entry in table:
  12. identifier = entry[0]
  13. content = entry[1]
  14. content = listify(content)
  15. formatted_table[identifier] = content
  16. return formatted_table
  17. def generate_upgraders_bytecode() -> List:
  18. yaml_content = []
  19. upgraders_graph_map = _generate_upgraders_graph()
  20. for upgrader_name, upgrader_graph in upgraders_graph_map.items():
  21. bytecode_table = _compile_graph_to_code_table(upgrader_name, upgrader_graph)
  22. entry = {upgrader_name: format_bytecode(bytecode_table)}
  23. yaml_content.append(entry)
  24. return yaml_content
  25. if __name__ == "__main__":
  26. raise RuntimeError("This file is not meant to be run directly")