|
| 1 | +from __future__ import ( |
| 2 | + absolute_import, |
| 3 | + annotations, |
| 4 | + division, |
| 5 | + print_function, |
| 6 | + unicode_literals, |
| 7 | +) |
| 8 | + |
| 9 | +import gzip |
| 10 | +import json |
| 11 | + |
| 12 | +from .execution_trace import ExecutionTrace |
| 13 | + |
| 14 | + |
| 15 | +class TraceValidator: |
| 16 | + |
| 17 | + def __init__(self, execution_trace: ExecutionTrace): |
| 18 | + self.et = execution_trace |
| 19 | + |
| 20 | + def _ops(self): |
| 21 | + return (n for n in self.et.nodes.values() if n.is_op()) |
| 22 | + |
| 23 | + def _validate_ops(self) -> bool: |
| 24 | + """Make sure the pytorch operators are valid""" |
| 25 | + ops = self._ops() |
| 26 | + for op in ops: |
| 27 | + if op.name == "": |
| 28 | + print(f"op should have valid name, node id = {op.id}") |
| 29 | + |
| 30 | + # if len(list(op.get_outputs())) + len(list(op.get_inputs())) == 0: |
| 31 | + # print(f"op should have outputs or inputs, node = {op.name}") |
| 32 | + # FIXME see "autograd::engine::evaluate_function: DivBackward1" |
| 33 | + # currently let's skip this |
| 34 | + # return False |
| 35 | + return True |
| 36 | + |
| 37 | + def _validate_tree(self) -> bool: |
| 38 | + """TBD validate that the generated datastructure is a tree |
| 39 | + with parent/child relationship. We can use pydot or networkx libs for this |
| 40 | + """ |
| 41 | + return True |
| 42 | + |
| 43 | + def _validate_param_comms(self) -> bool: |
| 44 | + """Check if param comms has correct attributes""" |
| 45 | + # This should use the comms parser, for now something simple will be fine |
| 46 | + # https://github.com/facebookresearch/param/blob/main/train/comms/pt/commsTraceParser.py#L256 |
| 47 | + |
| 48 | + if self.et.schema_pytorch() < (1, 0, 2): |
| 49 | + return True |
| 50 | + |
| 51 | + def check_comms_node(n) -> bool: |
| 52 | + """TODO use comms parser""" |
| 53 | + has_pg_id = False |
| 54 | + # Slightly hacky but find a argument with tuple type |
| 55 | + for arg in n.get_inputs(): |
| 56 | + if arg[0] == "Tuple[String,String]": |
| 57 | + print(f" {n.name}, process group args = {arg}") |
| 58 | + has_pg_id = True |
| 59 | + return has_pg_id |
| 60 | + |
| 61 | + return all( |
| 62 | + check_comms_node(n) |
| 63 | + for n in self.et.nodes.values() |
| 64 | + if n.is_op() and n.name == "record_param_comms" |
| 65 | + ) |
| 66 | + |
| 67 | + def _validate_triton(self) -> bool: |
| 68 | + """Make sure triton kernels have correct values |
| 69 | + TODO update for checking if kernel files are captured. |
| 70 | + """ |
| 71 | + return True |
| 72 | + |
| 73 | + def validate(self) -> bool: |
| 74 | + return all( |
| 75 | + [ |
| 76 | + self._validate_ops(), |
| 77 | + self._validate_tree(), |
| 78 | + self._validate_param_comms(), |
| 79 | + self._validate_triton(), |
| 80 | + ] |
| 81 | + ) |
| 82 | + |
| 83 | + def num_ops(self) -> int: |
| 84 | + return len(list(self._ops())) |
| 85 | + |
| 86 | + def num_comm_ops(self) -> int: |
| 87 | + return sum(1 for op in self._ops() if op.name == "record_param_comms") |
| 88 | + |
| 89 | + def num_triton_ops(self) -> int: |
| 90 | + return sum(1 for op in self._ops() if "triton" in op.name) |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + import sys |
| 95 | + |
| 96 | + execution_json = sys.argv[1] |
| 97 | + |
| 98 | + with ( |
| 99 | + gzip.open(execution_json, "rb") |
| 100 | + if execution_json.endswith("gz") |
| 101 | + else open(execution_json, "r") |
| 102 | + ) as execution_data: |
| 103 | + execution_trace: ExecutionTrace = ExecutionTrace(json.load(execution_data)) |
| 104 | + t = TraceValidator(execution_trace) |
| 105 | + print( |
| 106 | + f"num ops = {t.num_ops()}, num comms = {t.num_comm_ops()}, " |
| 107 | + f"num triton ops = {t.num_triton_ops()}" |
| 108 | + ) |
| 109 | + print("Trace validation result = ", t.validate()) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + main() |
0 commit comments