Weaves, Wires, and Morphisms: Formalizing and Implementing the Algebra of Deep Learning
Organizations: Laboratory for Information and Decision Systems, Massachusetts Institute of Technology
Abstract
Despite deep learning models running well-defined mathematical functions, we lack a formal mathematical framework for describing model architectures. Ad-hoc notation, diagrams, and pseudocode poorly handle nonlinear broadcasting and the relationship between individual components and composed models. This paper introduces a categorical framework for deep learning models that formalizes broadcasting through the novel axis-stride and array-broadcasted categories. This allows the mathematical function underlying architectures to be precisely expressed and manipulated in a compositional manner. These mathematical definitions are translated into human manageable diagrams and machine manageable data structures. We provide a mirrored implementation in Python (pyncd) and TypeScript (tsncd) to show the universal aspect of our framework, along with features including algebraic construction, graph conversion, PyTorch compilation and diagram rendering. This lays the foundation for a systematic, formal approach to deep learning model design and analysis.
Figures & tables
| Term Type | Associated Data | Constructed Property | Non-constructed Property | Interpretation |
| Product | Wrapping | Method \displaystyle\pi_{\ell}=m_{c,\ell}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G} | (\Pi_{j\in J}g_{j})\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G}=\Pi_{j\in J}(g_{j}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G}) | |
| Constructed Term | via | Wrapping | Method \displaystyle\pi_{\ell}=\hat{T}_{c}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}m_{c,\ell}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G} | g_{c,f}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G}=g_{c,f}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}\hat{T}_{c}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}\pi_{c} |
| Root Term | Where is non-core metadata. | Wrapping | Data Extraction \displaystyle\pi_{\ell}=p_{r,\ell}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G} | \displaystyle g_{r}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G}=\gamma_{r} \displaystyle\forall\ell\in r.g_{r}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}p_{r,\ell}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}V_{G}=\gamma_{r}\mathbin{\raise 2.58334pt\hbox{\vtop{\halign{#\cr\hfil\scriptscriptstyle\mathrm{o}\scriptscriptstyle\mathrm{9}\hfil\crcr}}}}\pi_{\ell} |
| ⬇ 1 qk_matmul = ops . Einops 2 . template ( 3 ’q h d, x h d -> h q d’ ) | ⬇ 1 softmax = ops . SoftMax . template () As we did not provide the axes, an unnamed axis is generated. Autoalignment will then rename it. |
| ⬇ 1 sv_matmul = ops . Einops 2 . template ( 3 ’h q x, x h d -> q h d’ ) | ⬇ 1 mask = ops . 2 WeightedTriangularLower 3 . template () |
Appendix figures & tables4 assets
Supplementary material from the paper’s appendix.