|
12 | 12 | """ |
13 | 13 |
|
14 | 14 | # Matrix multiplication system template |
15 | | -MATMUL_SYSTEM_TEMPLATE = """You are an expert algorithm engineer specialized in numerical computing and matrix operations. |
16 | | -Your task is to optimize matrix multiplication algorithms for better performance while maintaining correctness. |
17 | | -Apply techniques like loop reordering, blocking, recursion, and mathematical insights to reduce the number of operations. |
18 | | -Focus on making improvements for smaller matrix sizes (2x2 to 5x5) where algorithmic innovations like Strassen's algorithm can make a difference. |
| 15 | +MATMUL_SYSTEM_TEMPLATE = """You are an expert algorithm engineer specialized in numerical computing and matrix operations with a deep expertise in matrix multiplication optimizations. |
| 16 | +
|
| 17 | +Your task is to optimize matrix multiplication algorithms for better performance while maintaining correctness. You're familiar with advanced techniques including: |
| 18 | +
|
| 19 | +1. Strassen's algorithm, which reduces 7 multiplications instead of 8 for 2x2 matrices |
| 20 | +2. Winograd's variant, which minimizes additions in Strassen's algorithm |
| 21 | +3. The Coppersmith-Winograd algorithm and its theoretical improvements |
| 22 | +4. Memory access pattern optimizations (loop reordering, cache-oblivious algorithms) |
| 23 | +5. Low-level optimizations (loop unrolling, SIMD-friendly code, elimination of unnecessary operations) |
| 24 | +6. Special case optimizations for specific matrix dimensions |
| 25 | +7. Advanced mathematical decompositions like tensor methods |
| 26 | +
|
| 27 | +Focus particularly on optimizing small matrix sizes (2x2 to 5x5) where algorithmic innovations can make a significant difference versus hardware-level optimizations. Apply insights from linear algebra to reduce the total number of operations required. |
| 28 | +
|
| 29 | +The goal is to achieve the maximum possible speedup while maintaining 100% correctness of the output compared to the standard implementation. |
19 | 30 | """ |
20 | 31 |
|
21 | 32 | # User message template for diff-based evolution |
|
77 | 88 |
|
78 | 89 | # Task |
79 | 90 | Optimize the matrix multiplication algorithm for better performance while maintaining correctness. |
80 | | -Focus on smaller matrix sizes (2x2 to 5x5) where algorithmic innovations can make a significant difference. |
| 91 | +Your goal is to achieve the maximum possible speedup for matrix sizes from 2x2 to 5x5. |
| 92 | +
|
| 93 | +The evaluation metrics show how much your implementation is faster than the naive algorithm. Higher values are better. The optimization techniques you should consider include: |
81 | 94 |
|
82 | | -Consider these optimization strategies: |
83 | | -1. Loop reordering for better cache locality |
84 | | -2. Loop unrolling to reduce loop overhead |
85 | | -3. Blocking/tiling for better memory access patterns |
86 | | -4. Algorithmic improvements like Strassen's algorithm for recursive decomposition |
87 | | -5. Special case handling for specific matrix sizes |
88 | | -6. Vectorization hints and SIMD-friendly operations |
| 95 | +## Algorithm-level optimizations (highest impact): |
| 96 | +1. Implement Strassen's algorithm for 2x2, 4x4 matrices (reduces operations from O(n³) to O(n²·⁸¹)) |
| 97 | +2. Create specialized functions for specific matrix sizes (2x2, 3x3, 4x4, 5x5) |
| 98 | +3. Recursive decomposition with custom base cases |
| 99 | +4. Winograd's variant that minimizes the number of additions |
| 100 | +5. Tensor-based decompositions for further reducing scalar multiplications |
| 101 | +
|
| 102 | +## Implementation-level optimizations: |
| 103 | +1. Loop reordering for better cache locality (k-i-j instead of i-j-k) |
| 104 | +2. Loop unrolling to reduce loop overhead and enable compiler optimizations |
| 105 | +3. Memory access pattern improvements (array layout, temporary storage) |
| 106 | +4. Complete elimination of unnecessary operations and checks |
| 107 | +5. Smart bounds checking and early termination for special cases |
89 | 108 |
|
90 | 109 | You MUST use the exact SEARCH/REPLACE diff format shown below to indicate changes: |
91 | 110 |
|
|
95 | 114 | # New replacement code |
96 | 115 | >>>>>>> REPLACE |
97 | 116 |
|
98 | | -Example of valid diff format: |
| 117 | +Examples of good changes include: |
| 118 | +
|
| 119 | +1. Implementing Strassen for 2x2 matrices: |
99 | 120 | <<<<<<< SEARCH |
100 | | -for i in range(m): |
101 | | - for j in range(p): |
102 | | - for k in range(n): |
103 | | - C[i, j] += A[i, k] * B[k, j] |
104 | | -======= |
105 | | -# Reorder loops for better memory access pattern |
106 | | -for i in range(m): |
107 | | - for k in range(n): |
| 121 | +def matrix_multiply(A: np.ndarray, B: np.ndarray) -> np.ndarray: |
| 122 | + m, n = A.shape |
| 123 | + n2, p = B.shape |
| 124 | + |
| 125 | + if n != n2: |
| 126 | + raise ValueError(f"Incompatible matrix shapes: {{A.shape}} and {{B.shape}}") |
| 127 | + |
| 128 | + # Initialize result matrix with zeros |
| 129 | + C = np.zeros((m, p), dtype=A.dtype) |
| 130 | + |
| 131 | + # Naive triple-loop implementation |
| 132 | + for i in range(m): |
108 | 133 | for j in range(p): |
109 | | - C[i, j] += A[i, k] * B[k, j] |
| 134 | + for k in range(n): |
| 135 | + C[i, j] += A[i, k] * B[k, j] |
| 136 | + |
| 137 | + return C |
| 138 | +======= |
| 139 | +def matrix_multiply(A: np.ndarray, B: np.ndarray) -> np.ndarray: |
| 140 | + m, n = A.shape |
| 141 | + n2, p = B.shape |
| 142 | + |
| 143 | + if n != n2: |
| 144 | + raise ValueError(f"Incompatible matrix shapes: {{A.shape}} and {{B.shape}}") |
| 145 | + |
| 146 | + # Special case for 2x2 matrices using Strassen's algorithm |
| 147 | + if m == 2 and n == 2 and p == 2: |
| 148 | + return strassen_2x2(A, B) |
| 149 | + |
| 150 | + # Initialize result matrix with zeros |
| 151 | + C = np.zeros((m, p), dtype=A.dtype) |
| 152 | + |
| 153 | + # Optimized loop ordering for better cache locality |
| 154 | + for i in range(m): |
| 155 | + for k in range(n): |
| 156 | + A_ik = A[i, k] |
| 157 | + for j in range(p): |
| 158 | + C[i, j] += A_ik * B[k, j] |
| 159 | + |
| 160 | + return C |
| 161 | +
|
| 162 | +def strassen_2x2(A: np.ndarray, B: np.ndarray) -> np.ndarray: |
| 163 | + # Strassen's algorithm for 2x2 matrices |
| 164 | + # This reduces multiplications from 8 to 7 |
| 165 | + |
| 166 | + # Extract elements |
| 167 | + a11, a12 = A[0, 0], A[0, 1] |
| 168 | + a21, a22 = A[1, 0], A[1, 1] |
| 169 | + b11, b12 = B[0, 0], B[0, 1] |
| 170 | + b21, b22 = B[1, 0], B[1, 1] |
| 171 | + |
| 172 | + # Compute the 7 products needed in Strassen's algorithm |
| 173 | + m1 = (a11 + a22) * (b11 + b22) |
| 174 | + m2 = (a21 + a22) * b11 |
| 175 | + m3 = a11 * (b12 - b22) |
| 176 | + m4 = a22 * (b21 - b11) |
| 177 | + m5 = (a11 + a12) * b22 |
| 178 | + m6 = (a21 - a11) * (b11 + b12) |
| 179 | + m7 = (a12 - a22) * (b21 + b22) |
| 180 | + |
| 181 | + # Compute the result matrix elements |
| 182 | + c11 = m1 + m4 - m5 + m7 |
| 183 | + c12 = m3 + m5 |
| 184 | + c21 = m2 + m4 |
| 185 | + c22 = m1 - m2 + m3 + m6 |
| 186 | + |
| 187 | + # Construct the result matrix |
| 188 | + C = np.zeros((2, 2), dtype=A.dtype) |
| 189 | + C[0, 0], C[0, 1] = c11, c12 |
| 190 | + C[1, 0], C[1, 1] = c21, c22 |
| 191 | + |
| 192 | + return C |
110 | 193 | >>>>>>> REPLACE |
111 | 194 |
|
112 | | -You can suggest multiple changes. Each SEARCH section must exactly match code in the current program. |
113 | | -Explain the reasoning behind your optimizations. |
| 195 | +Explain your reasoning and clearly state which specific optimizations you're implementing. Be creative but thorough in your approach to achieve the maximum possible speedup. |
114 | 196 | """ |
115 | 197 |
|
116 | 198 | # User message template for full rewrite |
|
0 commit comments