|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from primus.core.launcher.parser import PrimusParser |
| 5 | +from primus.core.projection.module_profilers.language_model import ( |
| 6 | + build_profiler, |
| 7 | + get_language_model_profiler_spec, |
| 8 | +) |
| 9 | +from primus.core.projection.training_config import ( |
| 10 | + convert_primus_config_to_projection_config, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def print_profiler_hierarchy(profiler, batch_size, seq_len, rank=None, name="root", depth=0, visited=None): |
| 15 | + """ |
| 16 | + Recursively print the profiler hierarchy with num_params and activation_memory for each component. |
| 17 | +
|
| 18 | + Args: |
| 19 | + profiler: The profiler instance to print |
| 20 | + batch_size: Batch size for activation memory calculation |
| 21 | + seq_len: Sequence length for activation memory calculation |
| 22 | + rank: Rank for parameter calculation (if None, calculates total parameters) |
| 23 | + name: Name of the current profiler component |
| 24 | + depth: Current depth in the hierarchy (for indentation) |
| 25 | + visited: Set of visited profiler IDs to avoid infinite recursion |
| 26 | + """ |
| 27 | + if visited is None: |
| 28 | + visited = set() |
| 29 | + |
| 30 | + # Avoid infinite recursion if profilers reference each other |
| 31 | + profiler_id = id(profiler) |
| 32 | + if profiler_id in visited: |
| 33 | + return |
| 34 | + visited.add(profiler_id) |
| 35 | + |
| 36 | + indent = " " * depth |
| 37 | + |
| 38 | + # Calculate metrics for this profiler |
| 39 | + try: |
| 40 | + if depth == 0: |
| 41 | + # Only output the total number of parameters for the entire model for depth 0. |
| 42 | + num_params = profiler.estimated_num_params(rank=None) |
| 43 | + print(f"{indent} Total Number of Parameters: {num_params / 1e9:.6f} Billion ({num_params:,})") |
| 44 | + else: |
| 45 | + num_params = profiler.estimated_num_params(rank=rank) |
| 46 | + activation_mem = profiler.estimated_activation_memory(batch_size, seq_len) |
| 47 | + print(f"{indent}[{name}]") |
| 48 | + print(f"{indent} Params: {num_params / 1e9:.6f} Billion ({num_params:,})") |
| 49 | + print(f"{indent} Activation Memory: {activation_mem / 1024 / 1024 / 1024:.4f} GB") |
| 50 | + |
| 51 | + # Recursively process sub_profilers if they exist |
| 52 | + if hasattr(profiler, "sub_profilers") and profiler.sub_profilers: |
| 53 | + for sub_name, sub_profiler in profiler.sub_profilers.items(): |
| 54 | + if sub_profiler is not None: |
| 55 | + print() # Add spacing between components |
| 56 | + print_profiler_hierarchy( |
| 57 | + sub_profiler, batch_size, seq_len, rank, sub_name, depth + 1, visited |
| 58 | + ) |
| 59 | + except Exception as e: |
| 60 | + print(f"{indent}[{name}] - Error calculating metrics: {e}") |
| 61 | + |
| 62 | + |
| 63 | +def launch_projection_from_cli(args, overrides): |
| 64 | + """ |
| 65 | + Entry point for the 'projection' subcommand. |
| 66 | +
|
| 67 | + """ |
| 68 | + cfg_path = Path(args.config) |
| 69 | + if not cfg_path.exists(): |
| 70 | + raise FileNotFoundError(f"[Primus:Projection] Config file '{cfg_path}' not found.") |
| 71 | + |
| 72 | + config_parser = PrimusParser() |
| 73 | + primus_config = config_parser.parse(args) |
| 74 | + training_config = convert_primus_config_to_projection_config(primus_config) |
| 75 | + print(training_config) |
| 76 | + |
| 77 | + model_profiler_spec = get_language_model_profiler_spec(training_config) |
| 78 | + model_profiler = build_profiler(model_profiler_spec) |
| 79 | + |
| 80 | + seq_len = training_config.runtime_config.sequence_length |
| 81 | + batch_size = training_config.runtime_config.micro_batch_size |
| 82 | + rank = int(os.getenv("RANK", "0")) |
| 83 | + |
| 84 | + # Print recursive profiler hierarchy with detailed breakdown |
| 85 | + print("\n" + "=" * 100) |
| 86 | + print(f"[Primus:Projection] Component-wise Profiling Results (Rank {rank}):") |
| 87 | + print("=" * 100) |
| 88 | + print() |
| 89 | + |
| 90 | + # Print the complete hierarchy recursively |
| 91 | + print_profiler_hierarchy( |
| 92 | + model_profiler, batch_size, seq_len, rank=rank, name="LanguageModelProfiler", depth=0 |
| 93 | + ) |
| 94 | + |
| 95 | + # Get overall totals from the model profiler for this rank |
| 96 | + num_params = model_profiler.estimated_num_params(rank=rank) |
| 97 | + activation_memory = model_profiler.estimated_activation_memory(batch_size, seq_len) |
| 98 | + num_bytes_per_param = model_profiler.get_num_bytes_per_param() |
| 99 | + print() |
| 100 | + print("=" * 100) |
| 101 | + print(f"[Primus:Projection] Memory Projection Summary on Rank {rank}:") |
| 102 | + print(f" Params: {num_params / 1e9:.6f} Billion ({num_params:,})") |
| 103 | + print(f" Param+Optimizer Memory: {num_params * num_bytes_per_param / 1024 / 1024 / 1024:.4f} GB") |
| 104 | + print( |
| 105 | + f" Activation Memory (per batch size {batch_size}, seq len {seq_len}): " |
| 106 | + f"{activation_memory / 1024 / 1024 / 1024:.4f} GB" |
| 107 | + ) |
| 108 | + print( |
| 109 | + f" Projected Total Memory: " |
| 110 | + f"{(num_params * num_bytes_per_param + activation_memory) / 1024 / 1024 / 1024:.4f} GB" |
| 111 | + ) |
| 112 | + print("=" * 100) |
0 commit comments