|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "optimization_intent.hpp" |
| 4 | +#include "op_trace.hpp" |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +namespace infinicore::analyzer { |
| 10 | + |
| 11 | +/// IntentGenerator — the core "mutual awareness" logic. |
| 12 | +/// |
| 13 | +/// This is where task demand and resource supply are jointly |
| 14 | +/// analyzed to produce an OptimizationIntent. It implements |
| 15 | +/// the key insight: the same task phase has different optimization |
| 16 | +/// needs under different resource conditions, and the same resource |
| 17 | +/// state has different supply value under different task phases. |
| 18 | +class IntentGenerator { |
| 19 | +public: |
| 20 | + IntentGenerator() = default; |
| 21 | + |
| 22 | + /// Generate the global semantic intent from phase detection |
| 23 | + /// result and op trace window. |
| 24 | + GlobalSemanticIntent generateGlobal( |
| 25 | + PhaseType phase, |
| 26 | + const std::vector<OpTraceEntry> &window, |
| 27 | + const std::vector<DeviceLocalIntent> &device_intents) const { |
| 28 | + |
| 29 | + GlobalSemanticIntent intent; |
| 30 | + intent.current_phase = phase; |
| 31 | + intent.timestamp_ns = OpTraceEntry::now(); |
| 32 | + |
| 33 | + if (!window.empty()) { |
| 34 | + intent.op_window_start = 0; |
| 35 | + intent.op_window_end = static_cast<uint32_t>(window.size()); |
| 36 | + } |
| 37 | + |
| 38 | + // --- Compute intensity estimation --- |
| 39 | + intent.compute_intensity = estimateComputeIntensity(phase, window); |
| 40 | + |
| 41 | + // --- Determine primary bottleneck (mutual awareness) --- |
| 42 | + intent.primary_bottleneck = determineGlobalBottleneck(phase, device_intents); |
| 43 | + |
| 44 | + // --- Set optimization goal based on phase + bottleneck --- |
| 45 | + intent.goal = determineGoal(phase, intent.primary_bottleneck); |
| 46 | + |
| 47 | + // --- Generate strategy hints --- |
| 48 | + intent.strategy = generateStrategy(phase, intent.primary_bottleneck, device_intents); |
| 49 | + |
| 50 | + // --- Confidence --- |
| 51 | + intent.confidence = computeConfidence(phase, window); |
| 52 | + |
| 53 | + return intent; |
| 54 | + } |
| 55 | + |
| 56 | + /// Build the complete two-layer OptimizationIntent. |
| 57 | + OptimizationIntent generate( |
| 58 | + PhaseType phase, |
| 59 | + const std::vector<OpTraceEntry> &window, |
| 60 | + const std::vector<DeviceLocalIntent> &device_intents) const { |
| 61 | + |
| 62 | + OptimizationIntent result; |
| 63 | + result.global = generateGlobal(phase, window, device_intents); |
| 64 | + result.per_device = device_intents; |
| 65 | + return result; |
| 66 | + } |
| 67 | + |
| 68 | +private: |
| 69 | + /// Estimate compute intensity (higher = more compute-heavy). |
| 70 | + /// Uses a simple heuristic based on op type composition. |
| 71 | + float estimateComputeIntensity( |
| 72 | + PhaseType phase, |
| 73 | + const std::vector<OpTraceEntry> &window) const { |
| 74 | + |
| 75 | + if (window.empty()) return 0.0f; |
| 76 | + |
| 77 | + size_t heavy_compute_ops = 0; |
| 78 | + for (auto &e : window) { |
| 79 | + if (isGemmMlpOp(e.op_type) || isAttentionOp(e.op_type)) { |
| 80 | + heavy_compute_ops++; |
| 81 | + } |
| 82 | + } |
| 83 | + return static_cast<float>(heavy_compute_ops) / static_cast<float>(window.size()); |
| 84 | + } |
| 85 | + |
| 86 | + /// Determine global bottleneck by jointly considering phase and |
| 87 | + /// per-device resource state (the core mutual awareness logic). |
| 88 | + BottleneckType determineGlobalBottleneck( |
| 89 | + PhaseType phase, |
| 90 | + const std::vector<DeviceLocalIntent> &device_intents) const { |
| 91 | + |
| 92 | + bool any_memory_bound = false; |
| 93 | + bool any_compute_bound = false; |
| 94 | + bool any_bandwidth_bound = false; |
| 95 | + bool any_communication_bound = false; |
| 96 | + for (auto &d : device_intents) { |
| 97 | + any_memory_bound = any_memory_bound || d.local_bottleneck == BottleneckType::MEMORY_BOUND; |
| 98 | + any_compute_bound = any_compute_bound || d.local_bottleneck == BottleneckType::COMPUTE_BOUND; |
| 99 | + any_bandwidth_bound = any_bandwidth_bound || d.local_bottleneck == BottleneckType::BANDWIDTH_BOUND; |
| 100 | + any_communication_bound = any_communication_bound || d.local_bottleneck == BottleneckType::COMMUNICATION_BOUND; |
| 101 | + } |
| 102 | + |
| 103 | + // --- Mutual awareness logic --- |
| 104 | + // The same resource state has different "supply value" depending on phase: |
| 105 | + |
| 106 | + if (any_memory_bound) { |
| 107 | + return BottleneckType::MEMORY_BOUND; |
| 108 | + } |
| 109 | + |
| 110 | + if (phase == PhaseType::COMMUNICATION || any_communication_bound) { |
| 111 | + return BottleneckType::COMMUNICATION_BOUND; |
| 112 | + } |
| 113 | + |
| 114 | + switch (phase) { |
| 115 | + case PhaseType::ATTENTION_DENSE: |
| 116 | + case PhaseType::PREFILL: |
| 117 | + // Attention/prefill is dominated by memory movement and KV access, |
| 118 | + // so phase semantics should win unless memory/communication already |
| 119 | + // forced an earlier return above. |
| 120 | + if (any_bandwidth_bound) { |
| 121 | + return BottleneckType::BANDWIDTH_BOUND; |
| 122 | + } |
| 123 | + return BottleneckType::BANDWIDTH_BOUND; |
| 124 | + |
| 125 | + case PhaseType::GEMM_MLP_DENSE: |
| 126 | + if (any_compute_bound) { |
| 127 | + return BottleneckType::COMPUTE_BOUND; |
| 128 | + } |
| 129 | + if (any_bandwidth_bound) { |
| 130 | + return BottleneckType::BANDWIDTH_BOUND; |
| 131 | + } |
| 132 | + return BottleneckType::COMPUTE_BOUND; |
| 133 | + |
| 134 | + case PhaseType::DECODE: |
| 135 | + if (any_bandwidth_bound) { |
| 136 | + return BottleneckType::BANDWIDTH_BOUND; |
| 137 | + } |
| 138 | + if (any_compute_bound) { |
| 139 | + return BottleneckType::COMPUTE_BOUND; |
| 140 | + } |
| 141 | + return BottleneckType::BANDWIDTH_BOUND; |
| 142 | + |
| 143 | + case PhaseType::KV_CACHE: |
| 144 | + if (any_bandwidth_bound) { |
| 145 | + return BottleneckType::BANDWIDTH_BOUND; |
| 146 | + } |
| 147 | + return BottleneckType::MEMORY_BOUND; |
| 148 | + |
| 149 | + default: |
| 150 | + if (any_bandwidth_bound) { |
| 151 | + return BottleneckType::BANDWIDTH_BOUND; |
| 152 | + } |
| 153 | + if (any_compute_bound) { |
| 154 | + return BottleneckType::COMPUTE_BOUND; |
| 155 | + } |
| 156 | + return BottleneckType::BALANCED; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// Determine optimization goal based on phase and bottleneck. |
| 161 | + OptimizationGoal determineGoal( |
| 162 | + PhaseType phase, |
| 163 | + BottleneckType bottleneck) const { |
| 164 | + |
| 165 | + // Under memory pressure, prioritize memory safety |
| 166 | + if (bottleneck == BottleneckType::MEMORY_BOUND) { |
| 167 | + return OptimizationGoal::MEMORY_SAFE; |
| 168 | + } |
| 169 | + |
| 170 | + if (bottleneck == BottleneckType::COMMUNICATION_BOUND) { |
| 171 | + return OptimizationGoal::STABILITY_FIRST; |
| 172 | + } |
| 173 | + |
| 174 | + switch (phase) { |
| 175 | + case PhaseType::DECODE: |
| 176 | + // Decode latency is user-facing → latency first |
| 177 | + return OptimizationGoal::LATENCY_FIRST; |
| 178 | + |
| 179 | + case PhaseType::PREFILL: |
| 180 | + // Prefill processes a full prompt → throughput first |
| 181 | + return OptimizationGoal::THROUGHPUT_FIRST; |
| 182 | + |
| 183 | + case PhaseType::ATTENTION_DENSE: |
| 184 | + return OptimizationGoal::LATENCY_FIRST; |
| 185 | + |
| 186 | + case PhaseType::GEMM_MLP_DENSE: |
| 187 | + return OptimizationGoal::THROUGHPUT_FIRST; |
| 188 | + |
| 189 | + default: |
| 190 | + return OptimizationGoal::LATENCY_FIRST; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + /// Generate strategy hints from phase + bottleneck + resources. |
| 195 | + StrategyHint generateStrategy( |
| 196 | + PhaseType phase, |
| 197 | + BottleneckType bottleneck, |
| 198 | + const std::vector<DeviceLocalIntent> &device_intents) const { |
| 199 | + |
| 200 | + StrategyHint hint; |
| 201 | + |
| 202 | + // Fusion is beneficial for bandwidth-bound phases (reduce memory traffic) |
| 203 | + hint.prefer_fused_ops = (bottleneck == BottleneckType::BANDWIDTH_BOUND) |
| 204 | + || phase == PhaseType::DECODE; |
| 205 | + |
| 206 | + // In-place when memory is tight |
| 207 | + hint.prefer_in_place = (bottleneck == BottleneckType::MEMORY_BOUND); |
| 208 | + |
| 209 | + // Recomputation (activation checkpointing) when memory is critical |
| 210 | + bool extreme_memory = false; |
| 211 | + for (auto &d : device_intents) { |
| 212 | + if (d.memory_usage_ratio >= 0.95f) { |
| 213 | + extreme_memory = true; |
| 214 | + break; |
| 215 | + } |
| 216 | + } |
| 217 | + hint.prefer_recomputation = extreme_memory; |
| 218 | + |
| 219 | + // Async comm overlap for multi-device and communication phases |
| 220 | + hint.prefer_async_comm = (device_intents.size() > 1) |
| 221 | + && (phase == PhaseType::GEMM_MLP_DENSE |
| 222 | + || phase == PhaseType::COMMUNICATION); |
| 223 | + |
| 224 | + return hint; |
| 225 | + } |
| 226 | + |
| 227 | + /// Compute confidence based on how clear the phase signal is. |
| 228 | + float computeConfidence( |
| 229 | + PhaseType phase, |
| 230 | + const std::vector<OpTraceEntry> &window) const { |
| 231 | + |
| 232 | + if (window.empty() || phase == PhaseType::UNKNOWN) { |
| 233 | + return 0.0f; |
| 234 | + } |
| 235 | + |
| 236 | + // Count how many ops in the window match the detected phase |
| 237 | + size_t matching = 0; |
| 238 | + for (auto &e : window) { |
| 239 | + bool match = false; |
| 240 | + switch (phase) { |
| 241 | + case PhaseType::ATTENTION_DENSE: |
| 242 | + case PhaseType::PREFILL: |
| 243 | + match = isAttentionOp(e.op_type); |
| 244 | + break; |
| 245 | + case PhaseType::GEMM_MLP_DENSE: |
| 246 | + match = isGemmMlpOp(e.op_type) || isActivationOp(e.op_type); |
| 247 | + break; |
| 248 | + case PhaseType::KV_CACHE: |
| 249 | + match = isKvCacheOp(e.op_type); |
| 250 | + break; |
| 251 | + case PhaseType::DECODE: |
| 252 | + match = isAttentionOp(e.op_type) || isGemmMlpOp(e.op_type); |
| 253 | + break; |
| 254 | + default: |
| 255 | + break; |
| 256 | + } |
| 257 | + if (match) matching++; |
| 258 | + } |
| 259 | + |
| 260 | + return static_cast<float>(matching) / static_cast<float>(window.size()); |
| 261 | + } |
| 262 | +}; |
| 263 | + |
| 264 | +} // namespace infinicore::analyzer |
0 commit comments