import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20time%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20time%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%23%20Verify%20core%20linear%20algebraic%20operations%20with%20explicit%20einsum%20subscripts%0A%20%20%20%20rng%20%3D%20np.random.default_rng(42)%0A%0A%20%20%20%20%23%201D%20vectors%0A%20%20%20%20u%20%3D%20np.array(%5B1.0%2C%202.0%2C%203.0%2C%204.0%5D)%0A%20%20%20%20v%20%3D%20np.array(%5B5.0%2C%206.0%2C%207.0%2C%208.0%5D)%0A%0A%20%20%20%20%23%202D%20matrices%0A%20%20%20%20A%20%3D%20np.array(%5B%5B1.0%2C%202.0%5D%2C%20%5B3.0%2C%204.0%5D%2C%20%5B5.0%2C%206.0%5D%5D)%20%20%23%20Shape%20(3%2C%202)%0A%20%20%20%20B%20%3D%20np.array(%5B%5B7.0%2C%208.0%2C%209.0%5D%2C%20%5B10.0%2C%2011.0%2C%2012.0%5D%5D)%20%20%23%20Shape%20(2%2C%203)%0A%20%20%20%20S%20%3D%20np.array(%5B%5B1.0%2C%202.0%2C%203.0%5D%2C%20%5B4.0%2C%205.0%2C%206.0%5D%2C%20%5B7.0%2C%208.0%2C%209.0%5D%5D)%20%20%23%20Shape%20(3%2C%203)%0A%0A%20%20%20%20%23%20Contractions%0A%20%20%20%20dot_np%20%3D%20float(np.dot(u%2C%20v))%0A%20%20%20%20dot_ein%20%3D%20float(np.einsum(%22i%2Ci-%3E%22%2C%20u%2C%20v))%0A%0A%20%20%20%20outer_np%20%3D%20np.outer(u%2C%20v)%0A%20%20%20%20outer_ein%20%3D%20np.einsum(%22i%2Cj-%3Eij%22%2C%20u%2C%20v)%0A%0A%20%20%20%20matmul_np%20%3D%20A%20%40%20B%0A%20%20%20%20matmul_ein%20%3D%20np.einsum(%22ij%2Cjk-%3Eik%22%2C%20A%2C%20B)%0A%0A%20%20%20%20trace_np%20%3D%20float(np.trace(S))%0A%20%20%20%20trace_ein%20%3D%20float(np.einsum(%22ii-%3E%22%2C%20S))%0A%0A%20%20%20%20diag_np%20%3D%20np.diag(S)%0A%20%20%20%20diag_ein%20%3D%20np.einsum(%22ii-%3Ei%22%2C%20S)%0A%0A%20%20%20%20hadamard_np%20%3D%20S%20*%20S%0A%20%20%20%20hadamard_ein%20%3D%20np.einsum(%22ij%2Cij-%3Eij%22%2C%20S%2C%20S)%0A%0A%20%20%20%20trans_np%20%3D%20A.T%0A%20%20%20%20trans_ein%20%3D%20np.einsum(%22ij-%3Eji%22%2C%20A)%0A%0A%20%20%20%20%23%20Verification%20assertions%0A%20%20%20%20assert%20np.isclose(dot_np%2C%20dot_ein)%0A%20%20%20%20assert%20np.allclose(outer_np%2C%20outer_ein)%0A%20%20%20%20assert%20np.allclose(matmul_np%2C%20matmul_ein)%0A%20%20%20%20assert%20np.isclose(trace_np%2C%20trace_ein)%0A%20%20%20%20assert%20np.allclose(diag_np%2C%20diag_ein)%0A%20%20%20%20assert%20np.allclose(hadamard_np%2C%20hadamard_ein)%0A%20%20%20%20assert%20np.allclose(trans_np%2C%20trans_ein)%0A%20%20%20%20return%20(rng%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(go%2C%20mo)%3A%0A%20%20%20%20%23%20Interactive%20visualization%20of%20einsum%20indices%20across%20tensor%20shapes%0A%20%20%20%20_ops%20%3D%20%5B%0A%20%20%20%20%20%20%20%20%22Vector%20Dot%22%2C%20%22Matrix%20Trace%22%2C%20%22Matrix%20Diag%22%2C%20%22Transpose%22%2C%0A%20%20%20%20%20%20%20%20%22Matrix-Vector%22%2C%20%22Matmul%22%2C%20%22Bilinear%22%2C%20%22Batched%20Matmul%22%2C%0A%20%20%20%20%20%20%20%20%22Attention%20Scores%22%2C%20%22Attention%20Context%22%0A%20%20%20%20%5D%0A%20%20%20%20_input_ranks%20%3D%20%5B2%2C%201%2C%201%2C%201%2C%202%2C%202%2C%203%2C%202%2C%202%2C%202%5D%0A%20%20%20%20_output_ranks%20%3D%20%5B0%2C%200%2C%201%2C%202%2C%201%2C%202%2C%200%2C%203%2C%204%2C%204%5D%0A%20%20%20%20_summed_dims%20%3D%20%5B1%2C%201%2C%200%2C%200%2C%201%2C%201%2C%202%2C%201%2C%201%2C%201%5D%0A%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Output%20Tensor%20Rank%20(Free%20Indices)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_ops%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_output_ranks%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%233b82f6%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Bar(%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Contracted%20Axes%20(Dummy%20Indices)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D_ops%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_summed_dims%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker_color%3D%22%23f59e0b%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Tensor%20Rank%20%26%20Contraction%20Complexity%20by%20Einsum%20Operation%22%2C%0A%20%20%20%20%20%20%20%20barmode%3D%22group%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Algebraic%20Operation%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Number%20of%20Indices%20%2F%20Axes%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D420%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D40%2C%20t%3D60%2C%20b%3D80)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.02%2C%20y%3D0.98%2C%20bgcolor%3D%22rgba(255%2C255%2C255%2C0.8)%22)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Structural%20Analysis%20of%20Einsum%20Operations%0A%0A%20%20%20%20Notice%20the%20clean%20symmetry%3A%0A%20%20%20%20-%20Dot%20products%2C%20traces%2C%20and%20bilinear%20forms%20reduce%20all%20dimensions%20to%20a%20scalar%20(Output%20Rank%20%3D%200).%0A%20%20%20%20-%20Attention%20layers%20contract%20along%20a%20single%20inner%20dimension%20(%24d%24%20or%20%24j%24)%20while%20preserving%20batch%2C%20head%2C%20and%20sequence%20indices%20(%24b%2C%20h%2C%20i%24).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20rng)%3A%0A%20%20%20%20%23%20Demonstrate%20Multi-Head%20Attention%20via%20einsum%0A%20%20%20%20batch_size%2C%20num_heads%2C%20seq_len%2C%20head_dim%20%3D%202%2C%204%2C%208%2C%2016%0A%0A%20%20%20%20Q_att%20%3D%20rng.standard_normal((batch_size%2C%20num_heads%2C%20seq_len%2C%20head_dim))%0A%20%20%20%20K_att%20%3D%20rng.standard_normal((batch_size%2C%20num_heads%2C%20seq_len%2C%20head_dim))%0A%20%20%20%20V_att%20%3D%20rng.standard_normal((batch_size%2C%20num_heads%2C%20seq_len%2C%20head_dim))%0A%0A%20%20%20%20%23%20Attention%20scores%20via%20einsum%0A%20%20%20%20scores_einsum%20%3D%20np.einsum(%22bhid%2Cbhjd-%3Ebhij%22%2C%20Q_att%2C%20K_att)%20%2F%20np.sqrt(head_dim)%0A%0A%20%20%20%20%23%20Conventional%20loop%20%2F%20reshape%20equivalent%0A%20%20%20%20scores_matmul%20%3D%20np.matmul(Q_att%2C%20K_att.swapaxes(-1%2C%20-2))%20%2F%20np.sqrt(head_dim)%0A%20%20%20%20assert%20np.allclose(scores_einsum%2C%20scores_matmul)%0A%0A%20%20%20%20%23%20Softmax%20along%20key%20sequence%20dimension%20(axis%3D-1)%0A%20%20%20%20exp_scores%20%3D%20np.exp(scores_einsum%20-%20np.max(scores_einsum%2C%20axis%3D-1%2C%20keepdims%3DTrue))%0A%20%20%20%20P_att%20%3D%20exp_scores%20%2F%20np.sum(exp_scores%2C%20axis%3D-1%2C%20keepdims%3DTrue)%0A%0A%20%20%20%20%23%20Context%20representation%20via%20einsum%0A%20%20%20%20context_einsum%20%3D%20np.einsum(%22bhij%2Cbhjd-%3Ebhid%22%2C%20P_att%2C%20V_att)%0A%20%20%20%20context_matmul%20%3D%20np.matmul(P_att%2C%20V_att)%0A%20%20%20%20assert%20np.allclose(context_einsum%2C%20context_matmul)%0A%20%20%20%20return%20(P_att%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(P_att%2C%20go%2C%20make_subplots%2C%20mo)%3A%0A%20%20%20%20%23%20Visualize%20attention%20weight%20matrices%20for%204%20heads%20of%20Batch%200%0A%20%20%20%20_fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D4%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5Bf%22Head%20%7Bh%2B1%7D%22%20for%20h%20in%20range(4)%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.06%2C%0A%20%20%20%20)%0A%0A%20%20%20%20for%20_h%20in%20range(4)%3A%0A%20%20%20%20%20%20%20%20_attn_map%20%3D%20P_att%5B0%2C%20_h%2C%20%3A%2C%20%3A%5D%0A%20%20%20%20%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Heatmap(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20z%3D_attn_map%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22Viridis%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zmin%3D0.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20zmax%3Dfloat(_attn_map.max())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20showscale%3D(_h%20%3D%3D%203)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20colorbar%3Ddict(title%3D%22Weight%22%2C%20len%3D0.8%2C%20x%3D1.02)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Query%20%25%7By%7D%20-%3E%20Key%20%25%7Bx%7D%3Cbr%3EWeight%3A%20%25%7Bz%3A.3f%7D%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20col%3D_h%20%2B%201%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20_fig.update_xaxes(title_text%3D%22Key%20Pos%22%2C%20row%3D1%2C%20col%3D_h%20%2B%201)%0A%0A%20%20%20%20_fig.update_yaxes(title_text%3D%22Query%20Pos%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20_fig.update_yaxes(autorange%3D%22reversed%22)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Multi-Head%20Attention%20Weights%20Computed%20via%20np.einsum('bhid%2Cbhjd-%3Ebhij'%2C%20Q%2C%20K)%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D360%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D60%2C%20t%3D60%2C%20b%3D60)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Attention%20Heatmap%3A%20Batch%200%20across%204%20Attention%20Heads%0A%0A%20%20%20%20Notice%20that%20%60bhid%2Cbhjd-%3Ebhij%60%20naturally%20computes%20independent%20attention%20interaction%20matrices%20for%20each%20head%20without%20requiring%20explicit%20%60for%60%20loops%20or%20reshape%20gymnastics.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20rng)%3A%0A%20%20%20%20%23%20Benchmark%20chain%20contraction%20path%0A%20%20%20%20dim_small%20%3D%2010%0A%20%20%20%20dim_large%20%3D%201000%0A%0A%20%20%20%20A_chain%20%3D%20rng.standard_normal((dim_small%2C%20dim_large))%0A%20%20%20%20B_chain%20%3D%20rng.standard_normal((dim_large%2C%20dim_small))%0A%20%20%20%20C_chain%20%3D%20rng.standard_normal((dim_small%2C%20dim_large))%0A%0A%20%20%20%20%23%20Inspect%20einsum%20path%0A%20%20%20%20path_info%20%3D%20np.einsum_path(%22ij%2Cjk%2Ckl-%3Eil%22%2C%20A_chain%2C%20B_chain%2C%20C_chain%2C%20optimize%3D%22optimal%22)%0A%0A%20%20%20%20%23%20The%20string%20summary%20of%20path%20optimization%0A%20%20%20%20path_summary%20%3D%20str(path_info%5B1%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20rng%2C%20time)%3A%0A%20%20%20%20%23%20Micro-benchmark%3A%20np.einsum%20vs%20np.einsum(optimize%3DTrue)%20vs%20np.matmul%20(%40)%0A%20%20%20%20matrix_sizes%20%3D%20%5B64%2C%20128%2C%20256%2C%20512%5D%0A%20%20%20%20times_matmul%20%3D%20%5B%5D%0A%20%20%20%20times_einsum_naive%20%3D%20%5B%5D%0A%20%20%20%20times_einsum_opt%20%3D%20%5B%5D%0A%0A%20%20%20%20for%20_n%20in%20matrix_sizes%3A%0A%20%20%20%20%20%20%20%20_M1%20%3D%20rng.standard_normal((_n%2C%20_n))%0A%20%20%20%20%20%20%20%20_M2%20%3D%20rng.standard_normal((_n%2C%20_n))%0A%0A%20%20%20%20%20%20%20%20%23%201.%20BLAS%20%40%0A%20%20%20%20%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(5)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_%20%3D%20_M1%20%40%20_M2%0A%20%20%20%20%20%20%20%20_t_matmul%20%3D%20(time.perf_counter()%20-%20_t0)%20%2F%205.0%0A%20%20%20%20%20%20%20%20times_matmul.append(_t_matmul%20*%201000.0)%0A%0A%20%20%20%20%20%20%20%20%23%202.%20Einsum%20naive%0A%20%20%20%20%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(5)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_%20%3D%20np.einsum(%22ij%2Cjk-%3Eik%22%2C%20_M1%2C%20_M2%2C%20optimize%3DFalse)%0A%20%20%20%20%20%20%20%20_t_ein_naive%20%3D%20(time.perf_counter()%20-%20_t0)%20%2F%205.0%0A%20%20%20%20%20%20%20%20times_einsum_naive.append(_t_ein_naive%20*%201000.0)%0A%0A%20%20%20%20%20%20%20%20%23%203.%20Einsum%20optimized%0A%20%20%20%20%20%20%20%20_t0%20%3D%20time.perf_counter()%0A%20%20%20%20%20%20%20%20for%20_%20in%20range(5)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20_%20%3D%20np.einsum(%22ij%2Cjk-%3Eik%22%2C%20_M1%2C%20_M2%2C%20optimize%3DTrue)%0A%20%20%20%20%20%20%20%20_t_ein_opt%20%3D%20(time.perf_counter()%20-%20_t0)%20%2F%205.0%0A%20%20%20%20%20%20%20%20times_einsum_opt.append(_t_ein_opt%20*%201000.0)%0A%20%20%20%20return%20matrix_sizes%2C%20times_einsum_naive%2C%20times_einsum_opt%2C%20times_matmul%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20go%2C%0A%20%20%20%20matrix_sizes%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20times_einsum_naive%2C%0A%20%20%20%20times_einsum_opt%2C%0A%20%20%20%20times_matmul%2C%0A)%3A%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dmatrix_sizes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dtimes_matmul%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22BLAS%20np.matmul%20(%40)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2310b981%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Size%20%25%7Bx%7Dx%25%7Bx%7D%3Cbr%3ELatency%3A%20%25%7By%3A.3f%7D%20ms%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dmatrix_sizes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dtimes_einsum_opt%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22np.einsum(optimize%3DTrue)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%233b82f6%22%2C%20width%3D2.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Size%20%25%7Bx%7Dx%25%7Bx%7D%3Cbr%3ELatency%3A%20%25%7By%3A.3f%7D%20ms%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dmatrix_sizes%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dtimes_einsum_naive%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22np.einsum(optimize%3DFalse)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ef4444%22%2C%20width%3D2.5%2C%20dash%3D%22dot%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20hovertemplate%3D%22Size%20%25%7Bx%7Dx%25%7Bx%7D%3Cbr%3ELatency%3A%20%25%7By%3A.3f%7D%20ms%3Cextra%3E%3C%2Fextra%3E%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Execution%20Latency%3A%20Matrix%20Multiplication%20(NxN)%20across%20Runtimes%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22Matrix%20Dimension%20N%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22Execution%20Time%20(ms)%22%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D420%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D60%2C%20r%3D40%2C%20t%3D60%2C%20b%3D60)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.02%2C%20y%3D0.98%2C%20bgcolor%3D%22rgba(255%2C255%2C255%2C0.8)%22)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_md%20%3D%20mo.md(r%22%22%22%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20Micro-Benchmark%3A%20Latency%20vs%20Native%20BLAS%0A%0A%20%20%20%20Modern%20%60np.einsum%60%20with%20%60optimize%3DTrue%60%20delegates%20standard%20matrix%20multiplications%20directly%20to%20hardware-tuned%20BLAS%20libraries%20(%60dgemm%60%20%2F%20OpenBLAS%20%2F%20MKL)%2C%20matching%20native%20%60%40%60%20performance%20while%20offering%20orders-of-magnitude%20greater%20expressiveness%20for%20arbitrary%20rank%20tensors!%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
49a5bbc11c867c0919ed1ec387307a6e