Diff Coverage

Diff: origin/master...HEAD, staged and unstaged changes

Source File Diff Coverage (%) Missing Lines
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/_backbone.py 100%  
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/_hook_manager.py 100%  
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/_ppb.py 100%  
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/demo.py 0.0% 16,21,24,26-27,30,37,41,44,47,49,52,54,56,58,60,62,64,67,70,72,75,78,81,84-86,88,90,92,96,100,103-104
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/evaluators/body.py 100%  
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/hooks/template.py 100%  
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/validators/ep_constraints.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/common/_cost_model_variables.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/common/cost_model_preprocess.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/common/framework_parsers/cost_model_parser_hyper.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/common/framework_parsers/cost_model_parser_mindformers.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/common/hardware.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/dimensions.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/parallelize.py 100%  
hyper_parallel/auto_parallel/sapp_nd/nd/run_nd.py 100%  
hyper_parallel/auto_parallel/sapp_nd/perf_estimation/comm_time.py 100%  
hyper_parallel/auto_parallel/sapp_nd/perf_estimation/estimate.py 100%  
hyper_parallel/auto_parallel/sapp_ppb/pp_config_builder/layer_loader.py 100%  
hyper_parallel/auto_parallel/sapp_ppb/pp_config_builder/yaml_parser.py 100%  
hyper_parallel/auto_parallel/sapp_ppb/pp_modeling/pp_balancer.py 66.7% 527
hyper_parallel/auto_parallel/sapp_ppb/sapp/sapp_pipeline.py 100%  
hyper_parallel/auto_parallel/sapp_ppb/simulator/plot_manager.py 100%  
hyper_parallel/auto_parallel/sapp_ppb/simulator/pp_simulator.py 100%  
hyper_parallel/core/pipeline_parallel/mpipe/__init__.py 100%  
hyper_parallel/core/pipeline_parallel/mpipe/executor.py 100%  
hyper_parallel/core/pipeline_parallel/mpipe/schedule.py 100%  
hyper_parallel/core/pipeline_parallel/scheduler.py 37.9% 1064-1066,1073,1092-1093,1096,1101-1102,1104-1105,1108-1111,1113-1117,1121-1122,1124-1126,1129-1137,1139-1140
hyper_parallel/auto_parallel/sapp_nd/memory_estimation/demo.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Walkthrough of the EvaluatorV2 memory estimation API, see README.md."""
from typing import Any

from hyper_parallel.auto_parallel.sapp_nd.nd.common.layer_type import LayerType
from hyper_parallel.auto_parallel.sapp_nd.memory_estimation.estimate_v2 import EvaluatorV2
from hyper_parallel.auto_parallel.sapp_nd.memory_estimation.hooks.template import Template
from hyper_parallel.auto_parallel.sapp_nd.memory_estimation.logger import logger


def my_attn_num_param(ccfg: Any, ctx: Any) -> float:
    """Attention parameter count that overrides the default formula."""
    del ctx
    return 10 * ccfg.h * ccfg.h


def custom(ccfg: Any) -> None:
    """Cost model variables that override the parsed configuration."""
    ccfg.bytes_compute = 1
    ccfg.s = 1024
    ccfg.n_attMM = 5
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
    ccfg.s = 1024
    ccfg.n_attMM = 5


def main() -> None:
    """Estimate, inspect and customize the memory of the bundled test cases."""
    # Instantiate evaluator with a model configuration,
    #  log_level=0 removes warning messages
    e = EvaluatorV2("./test_cases/mixtral/default.yaml", log_level=0)

    # Check all defined node type
    logger.output("%s", list(LayerType))

    # Estimate peak memory (in Megabytes)
    peak_mem = e.estimate_peak(verbose=True)
    # Check whether estimation fits in device's max memory
    e.mem_fit(peak_mem)

    # Estimate static memory of a specific pipeline stage (in Megabytes)
    logger.output("%s", e.static_mem_stage(1))
    # Estimate dynamic memory of a specific pipeline stage (in Megabytes)
    logger.output("%s", e.dynamic_mem_stage(1))
    # Estimate static memory of a specific layer and stage (in Megabytes)
    logger.output("%s", e.static_mem_layer(LayerType.FULL_REC_LAYER, 1))
    # Estimate dynamic memory of a specific layer and stage (in Megabytes)
    logger.output("%s", e.dynamic_mem_layer(LayerType.FULL_REC_LAYER, 1))
    # Retrieve the memory estimation logs of a specific stage (in Megabytes)
    logger.output("%s", e.logs_mem_stage(1))
    # Fetch memory insights from each pipeline stage
    logger.output("%s", e.estimate_peak_insight())
    # PPB Input
    logger.output("%s", e.estimate_layer_memory())

    # Inspect a specific stage (here is the first one)
    e.estimate_peak(spec_stage_id=0, verbose=True)

    # Plot
    e.estimate_peak(plot=True)

    e = EvaluatorV2("./test_cases/deepseek3/default.yaml", log_level=0)

    # Overwriting context function
    e.set_attn_eval_fun(num_p=my_attn_num_param)

    # Overwriting a training feature
    e.set_passes(swap_os=True)

    # Overwriting cost model variables
    e.set_ccfg(custom)

    # Overwriting strategy
    logger.output("%s", e.get_strategy())
    e.set_strategy(dp=8, tp=8, m=128)
    logger.output("%s", e.get_strategy())

    e.estimate_peak(verbose=True)
    # Inspect ccfg object (cost model variables)
    e.print_ccfg()
    # Inspect ctx object (evaluation variables and functions)
    e.print_ctx()

    # Load a hook class
    # ... when declaring an Evaluator
    e = EvaluatorV2(
        "./test_cases/deepseek3/default.yaml", log_level=0, hook_cls=Template()
    )
    # ... by using load_hook_cls()
    e.load_hook_cls(Template())


if __name__ == "__main__":
    main()
hyper_parallel/auto_parallel/sapp_ppb/pp_modeling/pp_balancer.py
523
524
525
526
527
528
529
530
                    solver, group_name, inter, self._get_body_layer_by_name(group_name).recompute_considered_,
                )
                for stage_id, rec_counts in enumerate(chunk_stage_rec):
                    for rec_type, count in rec_counts:
                        stage_partition[inter * pp + stage_id].extend(
                            (layer_id, rec_type) for layer_id in range(current_layer_id, current_layer_id + count)
                        )
                        current_layer_id += count
hyper_parallel/core/pipeline_parallel/scheduler.py
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
        if step_type == MetaStepType.DATA_LOAD:
            if stage_index <= 0:
                self._exec_data_load(micro_index, arg_mbs, kwarg_mbs)
        elif step_type == MetaStepType.DATA_SEND:
            self._exec_data_send(stage_index, micro_index, arg_mbs, kwarg_mbs)
        elif step_type == MetaStepType.DATA_RECV:
            self._exec_data_recv(stage_index, micro_index, arg_mbs, kwarg_mbs)

    def _exec_data_load(self, micro_index: int, arg_mbs: list, kwarg_mbs: list) -> None:
        """Load the next micro-batch onto stage 0's device and stage it as ``micro_index``'s input."""
        device = self.stages[0].device
1069
1070
1071
1072
1073
1074
1075
1076
1077
        """Load the next micro-batch onto stage 0's device and stage it as ``micro_index``'s input."""
        device = self.stages[0].device
        micro_batch = next(self.data_iterator)
        if isinstance(micro_batch, list):
            micro_batch = micro_batch[micro_index]
        micro_batch = {
            key: (value.to(device, non_blocking=True) if hasattr(value, "to") else value)
            for key, value in micro_batch.items()
        }
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
        self.last_local_tokens += n_valid

        micro_batch["targets"] = targets
        for key in self.kwargs_batch_dim:
            if key in kwarg_mbs[micro_index].keys():
                kwarg_mbs[micro_index][key] = torch.cat(
                    [kwarg_mbs[micro_index][key], micro_batch[key]], dim=0)
            else:
                kwarg_mbs[micro_index][key] = micro_batch[key]
        arg_mbs[micro_index] = [input_ids]

    def _exec_data_send(self, stage_index: int, micro_index: int, arg_mbs: list, kwarg_mbs: list) -> None:
        """Send ``micro_index``'s data keys to the stage that consumes them."""
        if getattr(self, "_data_dst", False):
            dst_stage_idx = self._data_dst[stage_index][micro_index]
        else:
            dst_stage_idx = self.stages[0].dst_stage
        dst = self.stages[0]._global_rank(dst_stage_idx)  # pylint: disable=protected-access
        # One meta round-trip per DATA_SEND instead of K; the matching
        # DATA_RECV unpacks the list in the same order.
        metas = []
        tensors = []
        for key in self._data_keys:
            tensor = (arg_mbs[micro_index][0] if key == "input_ids"
                      else kwarg_mbs[micro_index][key])
            metas.append([tuple(tensor.shape), tensor.dtype])
            tensors.append(tensor)
        dist.send_object_list(metas, dst)
        handles = [dist.isend(t, dst) for t in tensors]
        self._wait_p2p(handles)

    def _exec_data_recv(self, stage_index: int, micro_index: int, arg_mbs: list, kwarg_mbs: list) -> None:
        """Receive ``micro_index``'s data keys and stage them as its input."""
        if getattr(self, "_data_src", False):
            src_stage_idx = self._data_src[stage_index][micro_index]
        else:
            src_stage_idx = self.stages[0].src_stage
        src = self.stages[0]._global_rank(src_stage_idx)  # pylint: disable=protected-access
        device = self.stages[0].device
        # One recv_object_list unpacks every key's (shape, dtype) at
        # once, matching the packed DATA_SEND above.
        metas: list = [None] * len(self._data_keys)
        dist.recv_object_list(metas, src)
        handles = []
        for key, meta in zip(self._data_keys, metas):
            shape, dtype = meta
            buffer = torch.empty(shape, dtype=dtype, device=device)
            handles.append(dist.irecv(buffer, src))
            if key == "input_ids":
                arg_mbs[micro_index] = [buffer]
            else:
                kwarg_mbs[micro_index][key] = buffer
        self._wait_p2p(handles)

    def _exec_pipeline_swap_step(self, cur_step, arg_mbs, kwarg_mbs):
        """Execute a pipeline activation-swap control step."""
        if self._swap_session is None: