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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Twill's Main Search Procedure (Algorithm 1 from the paper).
|
| 3 |
+
|
| 4 |
+
Combines Phase 1 (ZLP modulo scheduling) and Phase 2 (SMT joint SWP+WS)
|
| 5 |
+
in an iterative search over initiation intervals and schedule lengths.
|
| 6 |
+
|
| 7 |
+
Algorithm 1: Twill(G)
|
| 8 |
+
I ← 0
|
| 9 |
+
while true:
|
| 10 |
+
I ← I + 1
|
| 11 |
+
M ← Optimal-Modulo-Schedule(G, I)
|
| 12 |
+
if M = failure: continue
|
| 13 |
+
L ← Len(M)
|
| 14 |
+
while ⌈L/I⌉ = ⌈Len(M)/I⌉:
|
| 15 |
+
(M*, A*) ← SWP-and-WS(G, M, I, L)
|
| 16 |
+
if (M*, A*) = failure: L ← L+1; continue
|
| 17 |
+
return (M*, I, A*)
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import time
|
| 21 |
+
import math
|
| 22 |
+
from typing import Optional, Tuple
|
| 23 |
+
from twill.graph import DependenceGraph
|
| 24 |
+
from twill.cost_normalization import normalize_costs
|
| 25 |
+
from twill.modulo_scheduler import optimal_modulo_schedule, ModuloScheduleResult, validate_schedule
|
| 26 |
+
from twill.smt_joint import swp_and_ws, JointSWPWSResult
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class TwillResult:
|
| 30 |
+
"""Complete result from the Twill solver.
|
| 31 |
+
|
| 32 |
+
Attributes:
|
| 33 |
+
joint_result: The JointSWPWSResult containing schedule and warp assignment
|
| 34 |
+
initial_modulo_schedule: The Phase 1 modulo schedule that seeded the search
|
| 35 |
+
normalized_costs: The cost normalization result (if used)
|
| 36 |
+
solve_time_seconds: Total wall-clock time for the solver
|
| 37 |
+
iterations_tried: Number of I values tried before finding a solution
|
| 38 |
+
"""
|
| 39 |
+
def __init__(
|
| 40 |
+
self,
|
| 41 |
+
joint_result: JointSWPWSResult,
|
| 42 |
+
initial_schedule: ModuloScheduleResult,
|
| 43 |
+
solve_time: float,
|
| 44 |
+
iterations_tried: int,
|
| 45 |
+
normalized_costs: Optional[dict] = None,
|
| 46 |
+
):
|
| 47 |
+
self.joint_result = joint_result
|
| 48 |
+
self.initial_modulo_schedule = initial_schedule
|
| 49 |
+
self.solve_time_seconds = solve_time
|
| 50 |
+
self.iterations_tried = iterations_tried
|
| 51 |
+
self.normalized_costs = normalized_costs
|
| 52 |
+
|
| 53 |
+
@property
|
| 54 |
+
def schedule(self):
|
| 55 |
+
return self.joint_result.schedule
|
| 56 |
+
|
| 57 |
+
@property
|
| 58 |
+
def I(self):
|
| 59 |
+
return self.joint_result.I
|
| 60 |
+
|
| 61 |
+
@property
|
| 62 |
+
def warp_assignment(self):
|
| 63 |
+
return self.joint_result.warp_assignment
|
| 64 |
+
|
| 65 |
+
def __repr__(self):
|
| 66 |
+
return (
|
| 67 |
+
f"TwillResult(\n"
|
| 68 |
+
f" solve_time={self.solve_time_seconds:.2f}s\n"
|
| 69 |
+
f" iterations_tried={self.iterations_tried}\n"
|
| 70 |
+
f" {self.joint_result}\n"
|
| 71 |
+
f")"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def twill_solve(
|
| 76 |
+
graph: DependenceGraph,
|
| 77 |
+
max_I: int = 20,
|
| 78 |
+
enable_cost_normalization: bool = True,
|
| 79 |
+
cost_norm_U: int = 300,
|
| 80 |
+
enable_memory_constraints: bool = True,
|
| 81 |
+
enable_warp_constraints: bool = True,
|
| 82 |
+
modulo_solver_timeout: int = 120,
|
| 83 |
+
smt_solver_timeout_ms: int = 120000,
|
| 84 |
+
verbose: bool = True,
|
| 85 |
+
) -> Optional[TwillResult]:
|
| 86 |
+
"""Run the full Twill search procedure.
|
| 87 |
+
|
| 88 |
+
This is the main entry point implementing Algorithm 1 from the paper.
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
graph: Loop dependence graph with machine description
|
| 92 |
+
max_I: Maximum initiation interval to search up to
|
| 93 |
+
enable_cost_normalization: Apply cost normalization before solving
|
| 94 |
+
cost_norm_U: Upper bound for cost normalization (Section 5.2)
|
| 95 |
+
enable_memory_constraints: Include memory capacity constraints (Section 4.2)
|
| 96 |
+
enable_warp_constraints: Include warp assignment constraints (Section 4.3)
|
| 97 |
+
modulo_solver_timeout: Timeout for Phase 1 ILP solver (seconds)
|
| 98 |
+
smt_solver_timeout_ms: Timeout for Phase 2 SMT solver (milliseconds)
|
| 99 |
+
verbose: Print progress information
|
| 100 |
+
|
| 101 |
+
Returns:
|
| 102 |
+
TwillResult if a valid schedule is found, None otherwise
|
| 103 |
+
"""
|
| 104 |
+
start_time = time.time()
|
| 105 |
+
|
| 106 |
+
if verbose:
|
| 107 |
+
print(f"=" * 60)
|
| 108 |
+
print(f"Twill Solver v0.1")
|
| 109 |
+
print(f"=" * 60)
|
| 110 |
+
print(f"Graph: {graph}")
|
| 111 |
+
print(f"Instructions: {[v.name for v in graph.V]}")
|
| 112 |
+
print(f"Edges: {graph.E}")
|
| 113 |
+
print(f"Machine: {graph.machine.name}")
|
| 114 |
+
print(f"Functional units: {graph.machine.functional_units}")
|
| 115 |
+
print(f"Capacities: {graph.machine.capacities}")
|
| 116 |
+
print()
|
| 117 |
+
|
| 118 |
+
# Step 0: Cost normalization (Section 5.2)
|
| 119 |
+
normalized_costs_dict = None
|
| 120 |
+
if enable_cost_normalization:
|
| 121 |
+
# Collect all unique cycle counts from instructions and edges
|
| 122 |
+
cost_items = {}
|
| 123 |
+
for v in graph.V:
|
| 124 |
+
cost_items[v.name] = v.cycles
|
| 125 |
+
|
| 126 |
+
if max(cost_items.values()) > cost_norm_U // len(cost_items):
|
| 127 |
+
if verbose:
|
| 128 |
+
print(f"Cost Normalization (U={cost_norm_U}):")
|
| 129 |
+
print(f" Original costs: {cost_items}")
|
| 130 |
+
|
| 131 |
+
normalized_costs_dict, F = normalize_costs(cost_items, U=cost_norm_U)
|
| 132 |
+
|
| 133 |
+
if verbose:
|
| 134 |
+
print(f" Normalized costs: {normalized_costs_dict}")
|
| 135 |
+
print(f" Distortion F: {F}")
|
| 136 |
+
print()
|
| 137 |
+
|
| 138 |
+
# Note: In a full implementation, we would rebuild the graph with
|
| 139 |
+
# normalized costs. For this implementation, costs are typically
|
| 140 |
+
# already small (from the input specification) so normalization
|
| 141 |
+
# is primarily for real GPU cycle counts (e.g., ~1000 cycles for WGMMA).
|
| 142 |
+
|
| 143 |
+
# Compute resource lower bound on I
|
| 144 |
+
min_I = graph.compute_min_initiation_interval()
|
| 145 |
+
if verbose:
|
| 146 |
+
print(f"Minimum I (resource bound): {min_I}")
|
| 147 |
+
print()
|
| 148 |
+
|
| 149 |
+
iterations_tried = 0
|
| 150 |
+
|
| 151 |
+
# Algorithm 1: Main search loop
|
| 152 |
+
for I in range(max(1, min_I), max_I + 1):
|
| 153 |
+
iterations_tried += 1
|
| 154 |
+
|
| 155 |
+
if verbose:
|
| 156 |
+
print(f"--- Trying I = {I} ---")
|
| 157 |
+
|
| 158 |
+
# Phase 1: Optimal Modulo Schedule
|
| 159 |
+
if verbose:
|
| 160 |
+
print(f" Phase 1: ILP Modulo Scheduling...")
|
| 161 |
+
|
| 162 |
+
M = optimal_modulo_schedule(
|
| 163 |
+
graph, I,
|
| 164 |
+
solver_time_limit=modulo_solver_timeout,
|
| 165 |
+
verbose=False,
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
if M is None:
|
| 169 |
+
if verbose:
|
| 170 |
+
print(f" Phase 1: INFEASIBLE for I={I}")
|
| 171 |
+
continue
|
| 172 |
+
|
| 173 |
+
if verbose:
|
| 174 |
+
print(f" Phase 1: Found M with L={M.length}, copies={M.num_copies}")
|
| 175 |
+
print(f" Schedule: {M.schedule}")
|
| 176 |
+
|
| 177 |
+
# Validate
|
| 178 |
+
valid, violations = validate_schedule(graph, M)
|
| 179 |
+
if not valid:
|
| 180 |
+
print(f" WARNING: Schedule validation failed!")
|
| 181 |
+
for v in violations:
|
| 182 |
+
print(f" {v}")
|
| 183 |
+
|
| 184 |
+
# Phase 2: Joint SWP + WS, searching over L
|
| 185 |
+
L = M.length
|
| 186 |
+
initial_num_copies = M.num_copies
|
| 187 |
+
|
| 188 |
+
while math.ceil(L / I) == initial_num_copies:
|
| 189 |
+
if verbose:
|
| 190 |
+
print(f" Phase 2: SMT Joint SWP+WS with L={L}...")
|
| 191 |
+
|
| 192 |
+
result = swp_and_ws(
|
| 193 |
+
graph=graph,
|
| 194 |
+
initial_schedule=M,
|
| 195 |
+
I=I,
|
| 196 |
+
L=L,
|
| 197 |
+
enable_memory_constraints=enable_memory_constraints,
|
| 198 |
+
enable_warp_constraints=enable_warp_constraints,
|
| 199 |
+
timeout_ms=smt_solver_timeout_ms,
|
| 200 |
+
verbose=verbose,
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
if result is not None:
|
| 204 |
+
solve_time = time.time() - start_time
|
| 205 |
+
if verbose:
|
| 206 |
+
print()
|
| 207 |
+
print(f"=" * 60)
|
| 208 |
+
print(f"SOLUTION FOUND in {solve_time:.2f}s")
|
| 209 |
+
print(f"=" * 60)
|
| 210 |
+
print(f" Initiation Interval I = {I}")
|
| 211 |
+
print(f" Schedule Length L = {L}")
|
| 212 |
+
print(f" Overlapping copies = {result.num_copies}")
|
| 213 |
+
print(f" Schedule M*: {result.schedule}")
|
| 214 |
+
print(f" {result.warp_assignment}")
|
| 215 |
+
|
| 216 |
+
return TwillResult(
|
| 217 |
+
joint_result=result,
|
| 218 |
+
initial_schedule=M,
|
| 219 |
+
solve_time=solve_time,
|
| 220 |
+
iterations_tried=iterations_tried,
|
| 221 |
+
normalized_costs=normalized_costs_dict,
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
if verbose:
|
| 225 |
+
print(f" Phase 2: UNSAT for L={L}, trying L={L+1}")
|
| 226 |
+
L += 1
|
| 227 |
+
|
| 228 |
+
if verbose:
|
| 229 |
+
print(f" Exhausted L search for I={I} (would change num_copies)")
|
| 230 |
+
|
| 231 |
+
if verbose:
|
| 232 |
+
print(f"\nNo solution found up to I={max_I}")
|
| 233 |
+
return None
|