Þ¤pathÙ>/home/runner/work/LINMA2472/LINMA2472/Lectures/transformers.jl¬cell_resultsÞtÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3Чrunning§runtimeÎñÅ4å¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí©)Nb·has_pluto_hook_features¤bodyÚß

Cross-Attention between

  • values and keys $E(CX + P)$ where $E$ is the encoder, and $X$ is the matrix of input tokens

  • query $Q$ depending on past output $Y$ and number of layers already applied

$$\text{MultiHead}(E(CX + P), E(CX + P), Q)$$

The embedding vectors $CX$ take then different projections for value, key, query and also for different heads!

$$\begin{multline} \text{head}_j = \text{Attention}(W_j^VV, W_j^KK, W_j^QQ)\\ \text{where } V = K = E(CX + P) \end{multline}$$

°persist_js_stateÂÙ$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bbЧrunning§runtimeΘ3¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bb¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/9T·has_pluto_hook_features¤bodyÙR

Residual connection

°persist_js_stateÂÙ$6622f9f0-cecc-476e-9d49-7d651f433b9fЧrunning§runtimeξ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6622f9f0-cecc-476e-9d49-7d651f433b9f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�- ·has_pluto_hook_features¤bodyÙb

Pre-transformers approaches

°persist_js_stateÂÙ$70f395b2-f8c2-44d5-b0af-702659dd7feeЧrunning§runtimeÍ69¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$70f395b2-f8c2-44d5-b0af-702659dd7fee¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassigneeÀ²last_run_timestampËAÚªí£»«`·has_pluto_hook_features¤body…¦prefix¥Int64¨objectid°c8c9dd3b74dbd892¤type¥Array¬prefix_short ¨elements’’’¡1ªtext/plain’’¡1ªtext/plain°persist_js_stateÂÙ$771d39a5-74dc-494e-929e-1164bb08b983Чrunning§runtime΄=äÚ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$771d39a5-74dc-494e-929e-1164bb08b983¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí«àû ·has_pluto_hook_features¤bodyÚ h
NameRef$n_\text{voc}$$n_\text{ctx}$Tokenizer
GPT-2[RWCL19]50k1024tiktoken
GPT-3[BMRS20]50k2048tiktoken
GPT-3.5100k4096tiktoken
GPT-4100k32ktiktoken
GPT-4o200k128ktiktoken
Gemini-1[TABA24]256k10MSentencePiece
Gemini-1.5[TGLB24]256k10MSentencePiece
Gemma[TMHD24]256k8192SentencePiece
Gemma-2[TRPS24]256k8192SentencePiece
Llama-2[TMSA23]32k4kSentencePiece
Llama-3128k8ktiktoken
Llama-3.1128k128ktiktoken
Llama-3.2128k128ktiktoken
MegaByte[YSFA23]2568192Bytes
°persist_js_stateÂÙ$85a10748-8d19-44a8-a1c5-0d13b093f1bfЧrunning§runtimeÎ2`¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$85a10748-8d19-44a8-a1c5-0d13b093f1bf¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí§r¨·has_pluto_hook_features¤bodyÙ2draw_transformer (generic function with 2 methods)°persist_js_stateÂÙ$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eЧrunning§runtimeÎm¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4f1d5112-dbac-4eb6-8518-0dc4193c3f8e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí¦U ·has_pluto_hook_features¤bodyÙ$bib (generic function with 1 method)°persist_js_stateÂÙ$93200f46-7c8f-4362-a445-43c57b50a2d2Чrunning§runtimeÍ>¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$93200f46-7c8f-4362-a445-43c57b50a2d2¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassigneeÀ²last_run_timestampËAÚªí«=þi·has_pluto_hook_features¤body…¦prefix¦String¨objectid°4d0349d1775070f8¤type¥Array¬prefix_short ¨elements˜’’¦"Name"ªtext/plain’’¬"Num params"ªtext/plain’’¥"Ref"ªtext/plain’’³"``n_\\text{voc}``"ªtext/plain’’³"``d_\\text{emb}``"ªtext/plain’’³"``n_\\text{ctx}``"ªtext/plain’’²"``d_\\text{ff}``"ªtext/plain’’«"Tokenizer"ªtext/plain°persist_js_stateÂÙ$2a8433e3-9a3b-487b-abf3-09278ea42389Чrunning§runtimeÎTظdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$2a8433e3-9a3b-487b-abf3-09278ea42389¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§ôí·has_pluto_hook_features¤bodyÚ

[IS15] S. Ioffe and C. Szegedy. Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift (Mar 2015), arXiv:1502.03167. Accessed on Nov 12, 2024.
[BKH16] J. L. Ba, J. R. Kiros and G. E. Hinton. Layer Normalization (Jul 2016), arXiv:1607.06450. Accessed on Nov 12, 2024.
[VSP+17] A. Vaswani, N. Shazeer, N. Parmar et al. Attention Is All You Need. In: Advances in Neural Information Processing Systems, Vol. 30 (Curran Associates, Inc., 2017). Accessed on Oct 11, 2024.

°persist_js_stateÂÙ$61dc1905-338f-4bfd-a158-2f6bacff769eЧrunning§runtimeÎr> Ǹdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$61dc1905-338f-4bfd-a158-2f6bacff769e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦üÞ¾·has_pluto_hook_features¤bodyÚ<

[GBC16] I. Goodfellow, Y. Bengio and A. Courville. Deep Learning (MIT Press, 2016). Accessed on Aug 28, 2024.
[VSP+17] A. Vaswani, N. Shazeer, N. Parmar et al. Attention Is All You Need. In: Advances in Neural Information Processing Systems, Vol. 30 (Curran Associates, Inc., 2017). Accessed on Oct 11, 2024.

°persist_js_stateÂÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221Чrunning§runtimeÎ;s¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§�·has_pluto_hook_features¤bodyÚ½

[PW17] O. Press and L. Wolf. Using the Output Embedding to Improve Language Models (Feb 2017), arXiv:1608.05859. Accessed on Nov 11, 2024.
[VSP+17] A. Vaswani, N. Shazeer, N. Parmar et al. Attention Is All You Need. In: Advances in Neural Information Processing Systems, Vol. 30 (Curran Associates, Inc., 2017). Accessed on Oct 11, 2024.

°persist_js_stateÂÙ$76e2f97b-1c06-40cd-b134-d5155aa5587dЧrunning§runtimeÎv¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$76e2f97b-1c06-40cd-b134-d5155aa5587d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§·å·has_pluto_hook_features¤bodyÚ~

[BDV00] Y. Bengio, R. Ducharme and P. Vincent. A Neural Probabilistic Language Model. In: Advances in Neural Information Processing Systems, Vol. 13 (MIT Press, 2000). Accessed on Oct 11, 2024.

°persist_js_stateÂÙ$f6f7376e-9984-4289-b8ff-9d47e5358791Чrunning§runtimeάãw¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f6f7376e-9984-4289-b8ff-9d47e5358791¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí¤L»Ã·has_pluto_hook_features¤body °persist_js_stateÂÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7Чrunning§runtimeÎ�j¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí£Äh�·has_pluto_hook_features¤bodyÙ1numerical_lookup (generic function with 1 method)°persist_js_stateÂÙ$f2cba2aa-c541-4692-a441-e65741750a15Чrunning§runtimeÎ�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f2cba2aa-c541-4692-a441-e65741750a15¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/Q·has_pluto_hook_features¤bodyÙR

Layer normalization

°persist_js_stateÂÙ$6fc13413-53de-4c75-9b9e-620e0b7f8a1fЧrunning§runtimeÎ�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6fc13413-53de-4c75-9b9e-620e0b7f8a1f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¤*�4·has_pluto_hook_features¤bodyÚ&
Is $W^O$ needed if $h = 1$ ?

No, if $h = 1$, we can merge $W^OW_1^V$ into a new $W_1^V$.

°persist_js_stateÂÙ$ccf2dc71-b883-497a-bc58-29ffaf9ea4adЧrunning§runtimeÎCݸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$ccf2dc71-b883-497a-bc58-29ffaf9ea4ad¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�$ÆÞ·has_pluto_hook_features¤bodyÙ~

Text to vectors : step 1 → tokenization

°persist_js_stateÂÙ$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2aЧrunning§runtimeÎÁŸ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§#…·has_pluto_hook_features¤bodyÚI

[MKB+10] T. Mikolov, M. Karafiát, L. Burget et al. Recurrent Neural Network Based Language Model. In: Proc. Interspeech 2010 (2010); pp. 1045–1048. Accessed on Nov 11, 2024.
[GBC16] I. Goodfellow, Y. Bengio and A. Courville. Deep Learning (MIT Press, 2016). Accessed on Aug 28, 2024.

°persist_js_stateÂÙ$86101f07-67c5-4df2-911c-4013c44d6c5bЧrunning§runtimeÎeg¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$86101f07-67c5-4df2-911c-4013c44d6c5b¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦Eÿã·has_pluto_hook_features¤bodyÚÉ

Attention head provides a differentiable numerical dictionary [BCB16]

$$\begin{align} \alpha & = \text{softmax}(\langle q, k_1 \rangle, \ldots, \langle q, k_{n_\text{ctx}}\rangle) & \text{Attention}(q, k, v) & = \sum_{i=1}^{n_\text{ctx}} \alpha_i v_i \end{align}$$

°persist_js_stateÂÙ$a3efd921-eb14-4901-9d6c-800cc812fe02Чrunning§runtimeÎÛì¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a3efd921-eb14-4901-9d6c-800cc812fe02¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/ �·has_pluto_hook_features¤bodyÙH

Self-Attention

°persist_js_stateÂÙ$6800afbf-8ac6-4308-b4cc-b37da57e42c1Чrunning§runtimeÎÆß¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6800afbf-8ac6-4308-b4cc-b37da57e42c1¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.Š·has_pluto_hook_features¤bodyÙ^

Attention is all you need

°persist_js_stateÂÙ$18c26901-85eb-45ac-89bf-b03bd255007aЧrunning§runtimeΪCe¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$18c26901-85eb-45ac-89bf-b03bd255007a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§‚œk·has_pluto_hook_features¤bodyÛ^š

Residual connection [HZRS15]

°persist_js_stateÂÙ$bcbb3db2-85b3-4cb0-9309-f5c032d14da5Чrunning§runtimeÎFš¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$bcbb3db2-85b3-4cb0-9309-f5c032d14da5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.P3·has_pluto_hook_features¤bodyÚP

What would a numerical dictionary look like ? Consider keys $k_i \in \mathbb{R}^{d_k}$ and values $v_i \in \mathbb{R}^{d_v}$. Given a query $q \in \mathbb{R}^{d_k}$,

°persist_js_stateÂÙ$4e10271c-49f8-4f1d-869c-5fa11275d7f6Чrunning§runtimeΥиdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4e10271c-49f8-4f1d-869c-5fa11275d7f6¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-èB·has_pluto_hook_features¤bodyÙz

Recurrent neural networks (RNN)

°persist_js_stateÂÙ$c1437dcc-22cb-424f-9b8e-326172f82d86Чrunning§runtimeΠ甸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c1437dcc-22cb-424f-9b8e-326172f82d86¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦Lß·has_pluto_hook_features¤bodyÚJ
°persist_js_stateÂÙ$c09ec483-9fcf-48e7-b3c0-2508289e3cf3Чrunning§runtimeÎ�V¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c09ec483-9fcf-48e7-b3c0-2508289e3cf3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�‹Y·has_pluto_hook_features¤bodyÙV

Autoregressive Models

°persist_js_stateÂÙ$1bbf2152-4fdf-4ed2-9bdf-95d699824d11Чrunning§runtimeÎ zƸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$1bbf2152-4fdf-4ed2-9bdf-95d699824d11¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�+)J·has_pluto_hook_features¤bodyÚ‹

Why not encode each letter ?

Why not encode each word ?

LanguageFrenchEnglishDutchGerman
$n$408,078350,000350,000200,000
°persist_js_stateÂÙ$728c16b7-50cd-43fe-a0d7-61d37952a6b3Чrunning§runtimeÍ1|¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$728c16b7-50cd-43fe-a0d7-61d37952a6b3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassigneeÀ²last_run_timestampËAÚªí©«ib·has_pluto_hook_features¤body…¦prefix¾Dict{Tuple{Char, Char}, Int64}¨objectid°405bf575b80f9c3c¤type¤Dict¬prefix_short¤Dict¨elements–’’ƒ¨objectid°f57ec60eb2873fc1¤type¥Tuple¨elements’’’£'d'ªtext/plain’’£'a'ªtext/plainÙ!application/vnd.pluto.tree+object’¡1ªtext/plain’’ƒ¨objectid®8a2b36114e0285¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'b'ªtext/plainÙ!application/vnd.pluto.tree+object’¡2ªtext/plain’’ƒ¨objectid°22c3d575ec3807ae¤type¥Tuple¨elements’’’£'b'ªtext/plain’’£'d'ªtext/plainÙ!application/vnd.pluto.tree+object’¡1ªtext/plain’’ƒ¨objectid°77568909a7790f49¤type¥Tuple¨elements’’’£'b'ªtext/plain’’£'a'ªtext/plainÙ!application/vnd.pluto.tree+object’¡1ªtext/plain’’ƒ¨objectid°fb8c8201152e6a3a¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'a'ªtext/plainÙ!application/vnd.pluto.tree+object’¡4ªtext/plain’’ƒ¨objectid°5e8af23f6f4a9c7f¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'c'ªtext/plainÙ!application/vnd.pluto.tree+object’¡1ªtext/plain°persist_js_stateÂÙ$1faa4ab2-6c93-47dc-b631-8be52780fe7dЧrunning§runtimeÎ#åȸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$1faa4ab2-6c93-47dc-b631-8be52780fe7d¹depends_on_disabled_cells¦queued¤logs‘ˆ¥group¦stdout¤lineÿ£msg’Ù.α = [0.3543436937742045, 0.6456563062257954] ªtext/plain¥level®LogLevel(-555)¢id´PlutoRunner_40f9f5d1§cell_idÙ$1faa4ab2-6c93-47dc-b631-8be52780fe7d¦kwargs�¤fileÙP/home/runner/.julia/packages/Pluto/F6SNP/src/runner/PlutoRunner/src/io/stdout.jl¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassigneeÀ²last_run_timestampËAÚªí¤'Ù·has_pluto_hook_features¤body…¦prefix§Float64¨objectid°e465378233daf45b¤type¥Array¬prefix_short ¨elements’’’¨0.291313ªtext/plain’’£1.0ªtext/plain°persist_js_stateÂÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2Чrunning§runtimeÍBÒ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassignee¦iter_3²last_run_timestampËAÚªí©à||·has_pluto_hook_features¤body…¦prefix£BPE¨objectid°dfb31997047f0187¤type¦struct¬prefix_short£BPE¨elements’’¤text’§"XdXac"ªtext/plain’¥pairs’…¦prefix½Dict{Tuple{Char, Char}, Char}¨objectid°faa3a9c9e360c5d2¤type¤Dict¬prefix_short¤Dict¨elements“’’ƒ¨objectid®8a2b36114e0285¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'b'ªtext/plainÙ!application/vnd.pluto.tree+object’£'Y'ªtext/plain’’ƒ¨objectid¯a7aaaa8210ca50b¤type¥Tuple¨elements’’’£'Z'ªtext/plain’’£'Y'ªtext/plainÙ!application/vnd.pluto.tree+object’£'X'ªtext/plain’’ƒ¨objectid°fb8c8201152e6a3a¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'a'ªtext/plainÙ!application/vnd.pluto.tree+object’£'Z'ªtext/plainÙ!application/vnd.pluto.tree+object°persist_js_stateÂÙ$e383bb72-49a1-4df1-84c3-b95a2ffe00f5Чrunning§runtimeΟ޸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e383bb72-49a1-4df1-84c3-b95a2ffe00f5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/hè·has_pluto_hook_features¤bodyÙT

Feed-Forward network

°persist_js_stateÂÙ$6aa690e9-389f-4398-abae-b95060db4d90Чrunning§runtimeÎãÒ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6aa690e9-389f-4398-abae-b95060db4d90¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-¸A·has_pluto_hook_features¤bodyÙL

Shared embedding

°persist_js_stateÂÙ$4b61363d-87c9-4755-8286-44df34e9dd6aЧrunning§runtimeÎ4Þ4¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4b61363d-87c9-4755-8286-44df34e9dd6a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¤+ú/·has_pluto_hook_features¤bodyÚ‚
Is the order between the tokens taken into account by the model ?

No. Since the same matrices $W_j^V$, $W_j^K$ and $W_j^Q$ multiply the different position. The position information is completely lost!

°persist_js_stateÂÙ$94ae440d-0644-49db-9461-f1a1ff1d7f87Чrunning§runtimeÎ1û¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$94ae440d-0644-49db-9461-f1a1ff1d7f87¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí¥ù¦·has_pluto_hook_features¤bodyÙ%cite (generic function with 1 method)°persist_js_stateÂÙ$55435b26-7fc3-4c8b-8013-6fd4fb65a08eЧrunning§runtimeΦÁ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$55435b26-7fc3-4c8b-8013-6fd4fb65a08e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.7¬·has_pluto_hook_features¤bodyÙT

Numerical dictionary

°persist_js_stateÂÙ$225e58ba-b78d-4a0a-be4f-ad642c879b93Чrunning§runtimeÎ ñò¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$225e58ba-b78d-4a0a-be4f-ad642c879b93¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦E®æ·has_pluto_hook_features¤bodyÚÊ

It's difficult to model long-term dependencies as their gradient either vanish or explodes exponentially (think of the power method) [GBC16; Section 10.7]

Gated extensions attempting to solve this issue [GBC16; Section 10.10]:

°persist_js_stateÂÙ$32621224-a782-4bf6-9570-562cf2bb7360Чrunning§runtimeÎA?¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$32621224-a782-4bf6-9570-562cf2bb7360¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí¢íò·has_pluto_hook_features¤body °persist_js_stateÂÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0Чrunning§runtimeι/V¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí£!Ö^·has_pluto_hook_features¤bodyÚP
Why don't we use $D = C^{-1}$ ?

The $C$ matrix is not invertible as it is not square. Given a vector $u$, the vector $v = Du$ could be computed to be the mininum norm solution of $u = Cv$, this is what is done by v = D \ u. Computing D \ u would be more computational work than D' \ u but that's not the only reason D' is used. If $v$ is chosen so that $u = Cv$, it means that $v$ is a linear combination allowing to reconstruct $u$ using the columns of $C$. From that perspective, $v_i$ could be nonzero even though the direction of the vector $u$ us far from the direction of the column $C_{:i}$. That does not correspond to what we want to do here.

Here, we want a probability vector $p$ with high probability $p_i$ when the direction of the column $C_{:i}$ is close to the direction of $u$. In the vector $v = C^\top u$, $v_i = \langle C_{:i}, u \rangle$ is the scalar product between the $i$th column of $C$ and $u$ hence it is a good measure of how close the direction are to each other. Of course this assumes that the column of $C$ have the same norm, but the hope is for the model to figure in training that the columns of $C$ should have unit norm. We can then simply apply softmax to turn these scalar products into probabilities $p = \text{softmax}(v)$.

°persist_js_stateÂÙ$0c0c1163-0aec-4089-9acc-539b3a86d0b3Чrunning§runtimeÎl¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$0c0c1163-0aec-4089-9acc-539b3a86d0b3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.ÂÛ·has_pluto_hook_features¤bodyÙ¶

$$\text{Masked-Attention}(V, K, Q)\ = V\text{softmax}(M + K^\top Q/\sqrt{d_k})$$

°persist_js_stateÂÙ$29474a70-32eb-4281-8626-87819afa7267Чrunning§runtimeÎ;¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$29474a70-32eb-4281-8626-87819afa7267¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©‚#}·has_pluto_hook_features¤bodyÙ)add_pair (generic function with 1 method)°persist_js_stateÂÙ$c5be3956-5102-4d88-bfdb-9813c0555fe1Чrunning§runtimeΦYt¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c5be3956-5102-4d88-bfdb-9813c0555fe1¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§{\}·has_pluto_hook_features¤bodyÚÕ

Cannot sum $Cx_i + e_i$ with one-hot encoding $e_i \in \mathbb{R}^{n_\text{ctx}}$ as the dimension of $Cx_i$ is $\mathbb{R}^{d_\text{emb}}$.

So we also add a positional embedding $P$ : $Cx_i + Pe_i = Cx_i + p_i$.

With Self-Attention:

$$\text{Self-MultiHead}(CX + P, CX + P, CX + P)$$

°persist_js_stateÂÙ$7e27c349-ee76-46bd-b1c2-a9ce54974e10Чrunning§runtimeÎ-%t¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$7e27c349-ee76-46bd-b1c2-a9ce54974e10¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí«N6Á·has_pluto_hook_features¤bodyÙ&table (generic function with 1 method)°persist_js_stateÂÙ$2e8b1a77-1f04-4035-8d82-4061d81ecb7aЧrunning§runtimeÎ"u¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$2e8b1a77-1f04-4035-8d82-4061d81ecb7a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�+zÕ·has_pluto_hook_features¤bodyÙ€

Increasing length of "past text"

°persist_js_stateÂÙ$6bff7bca-ea1d-44c6-b8c3-040250f90654Чrunning§runtimeÎ u¯¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6bff7bca-ea1d-44c6-b8c3-040250f90654¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�08½·has_pluto_hook_features¤bodyÙJ

Cross-Attention

°persist_js_stateÂÙ$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1bЧrunning§runtimeÎ��¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1b¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí£~ÅÅ·has_pluto_hook_features¤bodyÚõ
What are the number of columns of $W_1$ and number of rows of $W_2$ now ?

The matrix $W_1$ has $n_\text{ctx}d_\text{emb}$ columns. Assuming $d_\text{emb} \ll n_\text{voc}$ and $n_\text{ctx} \gg 1$, this is much smaller than the number $n_\text{ctx}n_\text{voc}$ that we would have without the embedding. The number of rows of $W_2$ is $n_\text{voc}$, unaffected by the embedding.

°persist_js_stateÂÙ$6f72e8a5-819d-474c-a725-7f7318d964d7Чrunning§runtimeÎ'§I¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6f72e8a5-819d-474c-a725-7f7318d964d7¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí¢Á¯ÿ·has_pluto_hook_features¤bodyÙ$qa (generic function with 2 methods)°persist_js_stateÂÙ$b7583418-f4fb-4c63-b421-b5b9af269768Чrunning§runtimeÎ�?¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b7583418-f4fb-4c63-b421-b5b9af269768¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.Û·has_pluto_hook_features¤bodyÙT

Multi-Head Attention

°persist_js_stateÂÙ$a873f760-bfc1-489f-a58e-75e12afa54f2Чrunning§runtimeÎq[¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a873f760-bfc1-489f-a58e-75e12afa54f2¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí§oßr·has_pluto_hook_features¤bodyÙ*highlight (generic function with 1 method)°persist_js_stateÂÙ$76ba4e9b-8bb0-47c4-b607-2ca711f035e6Чrunning§runtimeÎÚ3¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$76ba4e9b-8bb0-47c4-b607-2ca711f035e6¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.­Þ·has_pluto_hook_features¤bodyÙL

Masked Attention

°persist_js_stateÂÙ$a5b20939-9afa-48c0-aa67-cbca6bc99804Чrunning§runtimeΘµ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a5b20939-9afa-48c0-aa67-cbca6bc99804¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/¶R·has_pluto_hook_features¤bodyÙD

Cost of LLMs

°persist_js_stateÂÙ$91abc03b-fef7-4f93-96fc-13f1cf654f0dЧrunning§runtimeÎfÝÕ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$91abc03b-fef7-4f93-96fc-13f1cf654f0d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí«ÿ°’·has_pluto_hook_features¤bodyÚÜg

See the table below for the size of embeddings of large language models:

NameNum paramsRef$n_\text{voc}$$d_\text{emb}$
GPT-21.5B[RWCL19]50k768
Gemma2B[TMHD24]256k2048
Gemma7B[TMHD24]256k3072
Gemma-227B[TRPS24]256k4608
Gemma-22B[TRPS24]256k2304
Gemma-29B[TRPS24]256k3584
Llama-27B[TMSA23]32k4096
base[VSPU17]37k512
big[VSPU17]37k1024

°persist_js_stateÂÙ$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475Чrunning§runtimeΗ÷¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/Ÿb·has_pluto_hook_features¤bodyÙX

Transformer variations

°persist_js_stateÂÙ$beccf4e8-1b01-4cb2-b23c-bc5db604f21cЧrunning§runtimeÎ �~¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$beccf4e8-1b01-4cb2-b23c-bc5db604f21c¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�$¦Â·has_pluto_hook_features¤bodyÚ

Given a sequence of $n_\text{ctx}$ past vectors $x_{-1}, \ldots, x_{-n_\text{ctx}} \in \mathbb{R}^{n}$, "predict" the next ones. Key idea : receding horizon:

$$\begin{align} & p(x_0, x_1 | x_{-1}, \ldots, x_{-n_\text{ctx}})\\ & = p(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})p(x_1 | x_0, x_{-1}, \ldots, x_{-n_\text{ctx}+1}, \textcolor{red}{x_{-n_\text{ctx}}})\\ & \approx p(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}) p(x_{1} | x_0, x_{-1}, \ldots, x_{-n_\text{ctx}+1}) \end{align}$$

What about Language Models ?

Given "past text", predict the "following text". How to turn text into vectors of $\mathbb{R}^n$ ?

°persist_js_stateÂÙ$e41d13ca-1dc1-45ae-9fa6-a83c4101120dЧrunning§runtimeΗ®�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e41d13ca-1dc1-45ae-9fa6-a83c4101120d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§‰·has_pluto_hook_features¤bodyÚt

[BCB16] D. Bahdanau, K. Cho and Y. Bengio. Neural Machine Translation by Jointly Learning to Align and Translate (May 2016), arXiv:1409.0473. Accessed on Oct 23, 2024.

°persist_js_stateÂÙ$5058e4eb-c53d-4468-b7ff-4f04ded96418Чrunning§runtimeÎ'Îö’¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5058e4eb-c53d-4468-b7ff-4f04ded96418¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¢ê>Š·has_pluto_hook_features¤bodyÚV@

Transformers in Large Language Models (LLMs)

Benoît Legat

      °persist_js_stateÂÙ$5150d8f3-6e85-43f2-801a-eae5cc3e3095Чrunning§runtimeΠኸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5150d8f3-6e85-43f2-801a-eae5cc3e3095¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦F8C·has_pluto_hook_features¤bodyÚÏï

softmax is then applied to each column:

$$\text{softmax}(K^\top Q/\sqrt{d_k})$$

Division by $\sqrt{d_k}$ scales the input of softmax to preferable regions [VSP+17; Secton 3.2.1].

Illustrated on the right from [BCB16; Figure 3(a)].

$$\text{Attention}(V, K, Q) = V\text{softmax}(K^\top Q/\sqrt{d_k})$$

°persist_js_stateÂÙ$8c27b182-0c3c-4c19-9619-df62b7dd6bf0Чrunning§runtimeÎ i2C¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$8c27b182-0c3c-4c19-9619-df62b7dd6bf0¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¤*f÷has_pluto_hook_features¤bodyÚŸ

💡 Key idea In the model for $\hat{p}(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})$, incorporate sub-models

$$\begin{align} \bar{p}(&x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})\\ \bar{p}(&x_{-1} | x_{-2}, \ldots, x_{-n_\text{ctx}})\\ & \quad\qquad\vdots\\ \bar{p}(&x_{-n_\text{ctx}+1} | x_{-n_\text{ctx}}). \end{align}$$

Mask prevents $\hat{p}$ to look at inputs in the future:

$$M = \begin{bmatrix} 0 & 0 & \cdots & 0\\ -\infty & 0 & \ddots & \vdots\\ \vdots & \ddots & \ddots & 0\\ -\infty & \cdots & -\infty & 0 \end{bmatrix}$$

°persist_js_stateÂÙ$9a8eef1b-27c0-4d57-a389-53708ade9058Чrunning§runtimeÎǸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9a8eef1b-27c0-4d57-a389-53708ade9058¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/ë·has_pluto_hook_features¤bodyÚÒ

Let $\hat{Y}_{i}$ be the intermediate output of $i \in \{1, \ldots, N\}$. The columns of the matrix $\text{softmax}(C^\top \hat{Y}_i)$ (column-wise softmax) can be thought as intermediate probabilities that we denote $\hat{p}_i$:

$$(\hat{p}_i(x_{-n_\text{ctx}+1} | x_{-n_\text{ctx}}), \ldots, \hat{p}_i(x_{-1} | x_{-2}, \ldots, x_{-n_\text{ctx}}), \hat{p}_i(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}))$$

and we predict the next token using $\hat{p}_N(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})$.

°persist_js_stateÂÙ$f39305ea-f7f5-440e-ac55-c83e27f6e7fcЧrunning§runtimeÎi“Mz¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f39305ea-f7f5-440e-ac55-c83e27f6e7fc¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ"application/vnd.pluto.table+object¬rootassignee¤llms²last_run_timestampËAÚªí«4Ìy·has_pluto_hook_features¤bodyƒ¨objectid°a2e5563e9e96bc29¦schema‚¥types˜ªDataString«DataString?«DataString?ªDataString«DataString?«DataString?«DataString?«DataString?¥names˜¤NameªNum params£Ref°``n_\text{voc}``°``d_\text{emb}``°``n_\text{ctx}``¯``d_\text{ff}``©Tokenizer¤rowsœ’˜’¬"Gemini-1.5"ªtext/plain’§missingªtext/plain’ª"[TGLB24]"ªtext/plain’¦"256k"ªtext/plain’§missingªtext/plain’¥"10M"ªtext/plain’§missingªtext/plain’¯"SentencePiece"ªtext/plain’˜’ª"Gemini-1"ªtext/plain’ÙX"1.[8B/3.25B](https://storage.googleapis.com/deepmind-media/gemini/gemini_1_report.pdf)"ªtext/plain’ª"[TABA24]"ªtext/plain’¦"256k"ªtext/plain’§missingªtext/plain’¥"10M"ªtext/plain’§missingªtext/plain’¯"SentencePiece"ªtext/plain’˜’©"Gemma-2"ªtext/plain’¥"27B"ªtext/plain’ª"[TRPS24]"ªtext/plain’¦"256k"ªtext/plain’¦"4608"ªtext/plain’¦"8192"ªtext/plain’§"73728"ªtext/plain’¯"SentencePiece"ªtext/plain’˜’©"Gemma-2"ªtext/plain’¤"9B"ªtext/plain’ª"[TRPS24]"ªtext/plain’¦"256k"ªtext/plain’¦"3584"ªtext/plain’¦"8192"ªtext/plain’§"28672"ªtext/plain’¯"SentencePiece"ªtext/plain’˜’©"Gemma-2"ªtext/plain’¤"2B"ªtext/plain’ª"[TRPS24]"ªtext/plain’¦"256k"ªtext/plain’¦"2304"ªtext/plain’¦"8192"ªtext/plain’§"18432"ªtext/plain’¯"SentencePiece"ªtext/plain’˜’§"Gemma"ªtext/plain’¤"7B"ªtext/plain’ª"[TMHD24]"ªtext/plain’¦"256k"ªtext/plain’¦"3072"ªtext/plain’¦"8192"ªtext/plain’§"49152"ªtext/plain’¯"SentencePiece"ªtext/plain’˜’§"Gemma"ªtext/plain’¤"2B"ªtext/plain’ª"[TMHD24]"ªtext/plain’¦"256k"ªtext/plain’¦"2048"ªtext/plain’¦"8192"ªtext/plain’§"32768"ªtext/plain’¯"SentencePiece"ªtext/plain’˜’§"GPT-2"ªtext/plain’¦"1.5B"ªtext/plain’ª"[RWCL19]"ªtext/plain’Ù("[50k](https://github.com/openai/gpt-2)"ªtext/plain’Ùq"[768](https://github.com/huggingface/transformers/blob/main/src/transformers/models/gpt2/configuration_gpt2.py)"ªtext/plain’¦"1024"ªtext/plain’¦"3072"ªtext/plain’ª"tiktoken"ªtext/plain’ ˜’©"Llama-2"ªtext/plain’¤"7B"ªtext/plain’ª"[TMSA23]"ªtext/plain’Ù3"[32k](https://github.com/meta-llama/llama-models)"ªtext/plain’ÙN"[4096](https://huggingface.co/docs/transformers/v4.31.0/en/model_doc/llama2)"ªtext/plain’Ù2"[4k](https://github.com/meta-llama/llama-models)"ªtext/plain’§missingªtext/plain’¯"SentencePiece"ªtext/plain’ ˜’¨"GPT-4o"ªtext/plain’§missingªtext/plain’§missingªtext/plain’ÙX"[200k](https://github.com/kaisugi/gpt4_vocab_list/blob/main/o200k_base_vocab_list.txt)"ªtext/plain’§missingªtext/plain’¦"128k"ªtext/plain’§missingªtext/plain’ª"tiktoken"ªtext/plain¤more’˜’¥"big"ªtext/plain’§missingªtext/plain’ª"[VSPU17]"ªtext/plain’¥"37k"ªtext/plain’¦"1024"ªtext/plain’§missingªtext/plain’¦"4096"ªtext/plain’§missingªtext/plain°persist_js_stateÂÙ$f7ca738d-5215-4e91-a2f3-a5ff10911313Чrunning§runtimeΩ†¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f7ca738d-5215-4e91-a2f3-a5ff10911313¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§x£·has_pluto_hook_features¤bodyÚ´

[SVL14] I. Sutskever, O. Vinyals and Q. V. Le. Sequence to Sequence Learning with Neural Networks. In: Proceedings of the 27th International Conference on Neural Information Processing Systems - Volume 2, NIPS'14 (MIT Press, Cambridge, MA, USA, Dec 2014); pp. 3104–3112. Accessed on Oct 23, 2024.

°persist_js_stateÂÙ$c7f318b9-30e6-4b79-b7da-52f70904d246Чrunning§runtimeÎ <§¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c7f318b9-30e6-4b79-b7da-52f70904d246¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©¶üH·has_pluto_hook_features¤bodyÙ+substitute (generic function with 1 method)°persist_js_stateÂÙ$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfcЧrunning§runtimeÎ:ødepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfc¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§Ù¶·has_pluto_hook_features¤bodyÚ

[HZRS16] K. He, X. Zhang, S. Ren and J. Sun. Identity Mappings in Deep Residual Networks. In: Computer Vision – ECCV 2016, edited by B. Leibe, J. Matas, N. Sebe and M. Welling (Springer International Publishing, Cham, 2016); pp. 630–645.
[RWC+19] A. Radford, J. Wu, R. Child et al. Language Models Are Unsupervised Multitask Learners (2019). Accessed on Oct 23, 2024.
[SLP+23] J. Su, Y. Lu, S. Pan et al. RoFormer: Enhanced Transformer with Rotary Position Embedding (Nov 2023), arXiv:2104.09864. Accessed on Nov 12, 2024.

°persist_js_stateÂÙ$8b78360a-21cb-4574-a84d-46ea4d0cedb1Чrunning§runtimeÎÂ^¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$8b78360a-21cb-4574-a84d-46ea4d0cedb1¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¤4’·has_pluto_hook_features¤bodyÚWµ°persist_js_stateÂÙ$95ec4140-9147-11ef-2af4-5528bad0e6f5Чrunning§runtimeÎôŸ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$95ec4140-9147-11ef-2af4-5528bad0e6f5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�:½·has_pluto_hook_features¤bodyÙt

Large Language Models (LLMs)

°persist_js_stateÂÙ$77f446ac-6030-48f2-9bea-93c427f9fcb9Чrunning§runtimeÎ.=”¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$77f446ac-6030-48f2-9bea-93c427f9fcb9¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí£â}¢·has_pluto_hook_features¤bodyÙ/softmax_lookup (generic function with 1 method)°persist_js_stateÂÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4aЧrunning§runtimeκú¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4a¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.ó(·has_pluto_hook_features¤bodyÙ\

Decoder-only transformer

°persist_js_stateÂÙ$d54b5390-0ec0-4ff8-ab18-51726482ca46Чrunning§runtimeΫ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d54b5390-0ec0-4ff8-ab18-51726482ca46¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-ÿë·has_pluto_hook_features¤bodyÙP

Extensions of RNNs

°persist_js_stateÂÙ$f572e113-b36b-4a6b-96c7-c26f100e1ad4Чrunning§runtimeΣ•¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f572e113-b36b-4a6b-96c7-c26f100e1ad4¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�0Q�·has_pluto_hook_features¤bodyÙ6

Utils

°persist_js_stateÂÙ$c3db7eb2-356a-428f-9777-6369662d8b06Чrunning§runtimeÎI¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c3db7eb2-356a-428f-9777-6369662d8b06¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�+b,·has_pluto_hook_features¤bodyÙm

Note that the new tokens can also be part of the most frequence pair!

°persist_js_stateÂÙ$a21fbc70-9137-4d0e-8c8c-cbdc5269778fЧrunning§runtimeÎ`�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a21fbc70-9137-4d0e-8c8c-cbdc5269778f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦EÚ”·has_pluto_hook_features¤bodyÚ

Recently, Mamba suggests a solution to the complexity issue [GD24]. As it scales better with $n_\text{ctx}$, it is even suggested to get rid of the tokenizer : [WGYR24].

°persist_js_stateÂÙ$9e898325-e9e2-45bd-af74-3dd86f00f7b5Чrunning§runtimeÎÞ4¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9e898325-e9e2-45bd-af74-3dd86f00f7b5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-‡ß·has_pluto_hook_features¤bodyÚ¡

Consider one-hot encoding with vocabulary size $n_\text{voc}$ and a bigram model

$$\hat{p}(x_0 | x_{-1}) = \text{softmax}(W_d \tanh(\cdots\tanh(W_1 x_{-1})\cdots)$$

The matrix $W_d$ has $n_\text{voc}$ rows and $W_1$ has $n_\text{voc}$ columns → issue if $n_\text{voc}$ is large

Embedding : Use vectors $c_1, \ldots, c_{n_\text{voc}} \in \mathbb{R}^{d_\text{emb}}$ with embedding size (aka hidden size) $d_\text{emb} \ll n_\text{voc}$.

Equivalently, we still use one-hot encoding but we add an encoder $C \in \mathbb{R}^{d_\text{emb} \times n_\text{voc}}$ and decoder $D \in \mathbb{R}^{n_\text{voc} \times d_\text{emb}}$

$$\hat{p}(x_0 | x_{-1}) = \text{softmax}(D W_d \tanh(\cdots\tanh(W_1 C x_{-1})\cdots)$$

°persist_js_stateÂÙ$97463c54-7cc7-4497-a8a6-6422f5f582bdЧrunning§runtimeΆ&1¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$97463c54-7cc7-4497-a8a6-6422f5f582bd¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§j¾·has_pluto_hook_features¤bodyÚ .

[TGL+24] G. Team, P. Georgiev, V. I. Lei et al. Gemini 1.5: Unlocking Multimodal Understanding across Millions of Tokens of Context (Aug 2024), arXiv:2403.05530. Accessed on Nov 11, 2024.
[TAB+24] G. Team, R. Anil, S. Borgeaud et al. Gemini: A Family of Highly Capable Multimodal Models (Jun 2024), arXiv:2312.11805. Accessed on Nov 11, 2024.
[TMH+24] G. Team, T. Mesnard, C. Hardin et al. Gemma: Open Models Based on Gemini Research and Technology (Apr 2024), arXiv:2403.08295. Accessed on Nov 11, 2024.
[TRP+24] G. Team, M. Riviere, S. Pathak et al. Gemma 2: Improving Open Language Models at a Practical Size (Oct 2024), arXiv:2408.00118. Accessed on Nov 11, 2024.
[SHB16] R. Sennrich, B. Haddow and A. Birch. Neural Machine Translation of Rare Words with Subword Units (Jun 2016), arXiv:1508.07909. Accessed on Oct 23, 2024.
[RWC+19] A. Radford, J. Wu, R. Child et al. Language Models Are Unsupervised Multitask Learners (2019). Accessed on Oct 23, 2024.
[BMR+20] T. B. Brown, B. Mann, N. Ryder et al. Language Models Are Few-Shot Learners (Jul 2020), arXiv:2005.14165. Accessed on Nov 11, 2024.
[TMS+23] H. Touvron, L. Martin, K. Stone et al. Llama 2: Open Foundation and Fine-Tuned Chat Models (Jul 2023), arXiv:2307.09288. Accessed on Oct 23, 2024.
[YSF+23] L. Yu, D. Simig, C. Flaherty et al. MEGABYTE: Predicting Million-byte Sequences with Multiscale Transformers (May 2023), arXiv:2305.07185. Accessed on Oct 23, 2024.

°persist_js_stateÂÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012Чrunning§runtimeÎ ¯Å¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§Úì·has_pluto_hook_features¤bodyÚ

[CvMBB14] K. Cho, B. van Merrienboer, D. Bahdanau and Y. Bengio. On the Properties of Neural Machine Translation: Encoder-Decoder Approaches (Oct 2014), arXiv:1409.1259. Accessed on Nov 11, 2024.
[Gra14] A. Graves. Generating Sequences With Recurrent Neural Networks (Jun 2014), arXiv:1308.0850. Accessed on Nov 11, 2024.
[GBC16] I. Goodfellow, Y. Bengio and A. Courville. Deep Learning (MIT Press, 2016). Accessed on Aug 28, 2024.
[GD24] A. Gu and T. Dao. Mamba: Linear-Time Sequence Modeling with Selective State Spaces (May 2024), arXiv:2312.00752. Accessed on Nov 11, 2024.
[WGYR24] J. Wang, T. Gangavarapu, J. N. Yan and A. M. Rush. MambaByte: Token-free Selective State Space Model (Aug 2024), arXiv:2401.13660. Accessed on Nov 11, 2024.

°persist_js_stateÂÙ$89305cae-098f-4644-9109-d00f1e3bc04cЧrunning§runtimeÎ� ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$89305cae-098f-4644-9109-d00f1e3bc04c¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©‹ ·has_pluto_hook_features¤bodyÙ+pair_stats (generic function with 1 method)°persist_js_stateÂÙ$bcf7667f-f99b-4d10-af84-5d3879f1db5dЧrunning§runtimeÎó‚¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$bcf7667f-f99b-4d10-af84-5d3879f1db5d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¢ù7†·has_pluto_hook_features¤bodyÚÎ
What difference do you expect with respect to the previous model ?

The products $W_1C$ and $DW_d$ have the same dimension as the matrices $W_1$ and $W_d$ of the previous model. So the expressive power of the model was not improved while we increased the number of parameters and we potentially made the loss function "even more nonconvex".

If the hidden dimension (i.e., the number of rows of $C$ / columns of $W_1$ or the number of rows of $W_d$ / columns of $D$) is much smaller than $n_\text{voc}$, then it's faster to compute $W_1(Cx)$. Moreover, we are forcing the matrix $W_1C$ to have a low rank compared the model without $C$. This means less expressivness but it might also prevent overfitting so the case isn't so clear.

The case become clearer when the input embedding $C$ is shared between more than one character, i.e., $n_\text{ctx} > 1$. Same for the output embedding, $D$ is useful when it is not preceded by a linear with which it can just be merged.

°persist_js_stateÂÙ$25b79953-fd7c-46c1-b760-d57c09910981Чrunning§runtimeÎ ƒ ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$25b79953-fd7c-46c1-b760-d57c09910981¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦L›‘·has_pluto_hook_features¤bodyÚ{
How does the number of parameters of transformers compare with [BDV00] or RNNs for large $n_\text{ctx}$ ?
°persist_js_stateÂÙ$c032b3ff-c539-4e38-81d0-39b28b3a8076Чrunning§runtimeΦ€¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c032b3ff-c539-4e38-81d0-39b28b3a8076¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§B»·has_pluto_hook_features¤bodyÚØ

[BCB16] D. Bahdanau, K. Cho and Y. Bengio. Neural Machine Translation by Jointly Learning to Align and Translate (May 2016), arXiv:1409.0473. Accessed on Oct 23, 2024.
[VSP+17] A. Vaswani, N. Shazeer, N. Parmar et al. Attention Is All You Need. In: Advances in Neural Information Processing Systems, Vol. 30 (Curran Associates, Inc., 2017). Accessed on Oct 11, 2024.

°persist_js_stateÂÙ$45efc71d-d5f8-474e-9b89-e72fac7110fdЧrunning§runtimeÎñ�s¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$45efc71d-d5f8-474e-9b89-e72fac7110fd¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§ð®·has_pluto_hook_features¤bodyÚx

[BDV00] Y. Bengio, R. Ducharme and P. Vincent. A Neural Probabilistic Language Model. In: Advances in Neural Information Processing Systems, Vol. 13 (MIT Press, 2000). Accessed on Oct 11, 2024.

°persist_js_stateÂÙ$d050a7ee-3aa7-4539-a236-5b6446599dedЧrunning§runtimeÎó1¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d050a7ee-3aa7-4539-a236-5b6446599ded¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©vþô·has_pluto_hook_features¤body °persist_js_stateÂÙ$579a203b-e6f7-4190-b874-18b00a5c3f77Чrunning§runtimeÎ F ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$579a203b-e6f7-4190-b874-18b00a5c3f77¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©êƒ�·has_pluto_hook_features¤bodyÙ*load_llms (generic function with 1 method)°persist_js_stateÂÙ$570fa160-3adb-463e-99b8-b7dd05076908Чrunning§runtimeÎ|]¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$570fa160-3adb-463e-99b8-b7dd05076908¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí£Ù¼¡·has_pluto_hook_features¤bodyÙ(softmax (generic function with 1 method)°persist_js_stateÂÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904Чrunning§runtimeÎ|ê¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦6€€·has_pluto_hook_features¤bodyÚ)

With $n_\text{ctx} > 1$, the encoder $C$ is shared by all tokens: See for instance the network below taken from [BDV00; Figure 1], the first popular application of neural nets for languages:

°persist_js_stateÂÙ$75ca478c-916f-464a-9435-8208ee726d50Чrunning§runtimeÎöǸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$75ca478c-916f-464a-9435-8208ee726d50¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§z³·has_pluto_hook_features¤bodyÚj

[TMH+24] G. Team, T. Mesnard, C. Hardin et al. Gemma: Open Models Based on Gemini Research and Technology (Apr 2024), arXiv:2403.08295. Accessed on Nov 11, 2024.
[TRP+24] G. Team, M. Riviere, S. Pathak et al. Gemma 2: Improving Open Language Models at a Practical Size (Oct 2024), arXiv:2408.00118. Accessed on Nov 11, 2024.
[RWC+19] A. Radford, J. Wu, R. Child et al. Language Models Are Unsupervised Multitask Learners (2019). Accessed on Oct 23, 2024.
[TMS+23] H. Touvron, L. Martin, K. Stone et al. Llama 2: Open Foundation and Fine-Tuned Chat Models (Jul 2023), arXiv:2307.09288. Accessed on Oct 23, 2024.

°persist_js_stateÂÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03Чrunning§runtimeÎ2Bh¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§v5¤·has_pluto_hook_features¤bodyÚÓá

Self-Attention with embedding $C$ is:

$$\text{Masked-MultiHead}(CX, CX, CX)$$

The embedding vectors $CX$ take then different projections for value, key, query and also for different heads!

$$\text{head}_j = \text{Masked-Attention}(W_j^VCX, W_j^KCX, W_j^QCX)$$

°persist_js_stateÂÙ$9ff95a9a-192b-4a12-8e2e-7acd6659c066Чrunning§runtimeÎg~¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$9ff95a9a-192b-4a12-8e2e-7acd6659c066¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.–·has_pluto_hook_features¤bodyÚé

$$\begin{align} Q & = \begin{bmatrix} q_1 & \cdots & q_{n_\text{ctx}} \end{bmatrix} & K & = \begin{bmatrix} k_1 & \cdots & k_{n_\text{ctx}} \end{bmatrix} & K^\top Q & = \begin{bmatrix} \langle k_1, q_1 \rangle & \cdots & \langle k_1, q_{n_\text{ctx}} \rangle\\ \vdots & \ddots & \vdots\\ \langle k_{n_\text{ctx}}, q_1 \rangle & \cdots & \langle k_{n_\text{ctx}}, q_{n_\text{ctx}} \rangle \end{bmatrix} \end{align}$$

°persist_js_stateÂÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Чrunning§runtimeÎÈ Œ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¬HÔ·has_pluto_hook_features¤bodyÚÛ:

Different weights $W_1 \in \mathbb{R}^{d_\text{ff} \times d_\text{emb}}$, $W_2 \in \mathbb{R}^{d_\text{emb} \times d_\text{ff}}$ for each layer:

$$x \mapsto W_2\max(0, W_1x + b_1) + b_2$$

Expansion factor $d_\text{ff} / d_\text{emb}$ is typically 4× like suggested in [VSP+17] (but not for Gemma)

NameRef$d_\text{emb}$$d_\text{ff}$
GPT-2[RWCL19]7683072
Gemma[TMHD24]204832768
Gemma[TMHD24]307249152
Gemma-2[TRPS24]230418432
Gemma-2[TRPS24]358428672
Gemma-2[TRPS24]460873728
Llama-34096
base[VSPU17]5122048
big[VSPU17]10244096

°persist_js_stateÂÙ$ed5b5702-4cca-4116-a70f-4a562178f490Чrunning§runtimeηŸ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$ed5b5702-4cca-4116-a70f-4a562178f490¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-@%·has_pluto_hook_features¤bodyÚì

Challenging tradeoff: Encode text to increase length of "past text" while keeping $n_\text{ctx}$ and $n$ small enough.

Length of "past text" increases with vocabulary size $n_\text{voc}$ and context window $n_\text{ctx}$.

°persist_js_stateÂÙ$736920df-e4bb-4535-b982-e397aa0a782dЧrunning§runtimeÍMž¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$736920df-e4bb-4535-b982-e397aa0a782d¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassignee¦iter_2²last_run_timestampËAÚªí©ßÙ#·has_pluto_hook_features¤body…¦prefix£BPE¨objectid°e8c41df2d677c62d¤type¦struct¬prefix_short£BPE¨elements’’¤text’©"ZYdZYac"ªtext/plain’¥pairs’…¦prefix½Dict{Tuple{Char, Char}, Char}¨objectid°aaae13de44125bcc¤type¤Dict¬prefix_short¤Dict¨elements’’’ƒ¨objectid®8a2b36114e0285¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'b'ªtext/plainÙ!application/vnd.pluto.tree+object’£'Y'ªtext/plain’’ƒ¨objectid°fb8c8201152e6a3a¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'a'ªtext/plainÙ!application/vnd.pluto.tree+object’£'Z'ªtext/plainÙ!application/vnd.pluto.tree+object°persist_js_stateÂÙ$55a09acc-84da-491c-86ba-9a66f4ea52feЧrunning§runtimeÎjî}¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$55a09acc-84da-491c-86ba-9a66f4ea52fe¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦D�¿·has_pluto_hook_features¤bodyÛ[

$$\begin{align} h^{(t+1)} & = \tanh(Wh^{(t)} + Ux^{(t+1)} + b)\\ o^{(t)} &= Vh^{(t)} + c\\ \hat{y}^{(t)} &= \text{softmax}(o^{(t)}) \end{align}$$

Illustrated on the right [GBC16; Figure 10.3].

RNNs as language model showcased in [MKB+10].

Issue: Training time and space complexity is proportional to $n_\text{ctx}$ and cannot parallelize to speed up.

°persist_js_stateÂÙ$d558636d-c714-4033-ae73-5b92c3cdedf3Чrunning§runtimeÍUü¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d558636d-c714-4033-ae73-5b92c3cdedf3¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassignee¤dict²last_run_timestampËAÚªí£³û½·has_pluto_hook_features¤body…¦prefixÙ"Dict{Vector{Int64}, Vector{Int64}}¨objectid°7bbdd13b5a95540b¤type¤Dict¬prefix_short¤Dict¨elements’’’…¦prefix¥Int64¨objectid°35c90b47ceda2bfd¤type¥Array¬prefix_short ¨elements’’’¡0ªtext/plain’’¡1ªtext/plainÙ!application/vnd.pluto.tree+object’…¦prefix¥Int64¨objectid°457d2293ee424a95¤type¥Array¬prefix_short ¨elements’’’¢-1ªtext/plain’’¡1ªtext/plainÙ!application/vnd.pluto.tree+object’’…¦prefix¥Int64¨objectid°9b2c995799a1515f¤type¥Array¬prefix_short ¨elements’’’¡1ªtext/plain’’¡0ªtext/plainÙ!application/vnd.pluto.tree+object’…¦prefix¥Int64¨objectid°c8c9dd3b74dbd892¤type¥Array¬prefix_short ¨elements’’’¡1ªtext/plain’’¡1ªtext/plainÙ!application/vnd.pluto.tree+object°persist_js_stateÂÙ$8d231f2c-4b0c-4c37-a746-16e98d4cafc8Чrunning§runtimeΪ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$8d231f2c-4b0c-4c37-a746-16e98d4cafc8¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.h¾·has_pluto_hook_features¤bodyÙH

Attention head

°persist_js_stateÂÙ$f1afaf8c-d9ad-446a-9826-9c4cda19993fЧrunning§runtimeÎ�¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f1afaf8c-d9ad-446a-9826-9c4cda19993f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©Á�¿·has_pluto_hook_features¤bodyÙ*new_token (generic function with 1 method)°persist_js_stateÂÙ$f7ca3ff7-b5cf-452b-b955-7219e7397324Чrunning§runtimeÍNp¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f7ca3ff7-b5cf-452b-b955-7219e7397324¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassignee¦iter_1²last_run_timestampËAÚªí©ß+зhas_pluto_hook_features¤body…¦prefix£BPE¨objectid°189eeaf27bd5f4b8¤type¦struct¬prefix_short£BPE¨elements’’¤text’«"ZabdZabac"ªtext/plain’¥pairs’…¦prefix½Dict{Tuple{Char, Char}, Char}¨objectid°df935a2cecbf0298¤type¤Dict¬prefix_short¤Dict¨elements‘’’ƒ¨objectid°fb8c8201152e6a3a¤type¥Tuple¨elements’’’£'a'ªtext/plain’’£'a'ªtext/plainÙ!application/vnd.pluto.tree+object’£'Z'ªtext/plainÙ!application/vnd.pluto.tree+object°persist_js_stateÂÙ$5f05e717-a51a-4a99-bb11-cc493217f93fЧrunning§runtimeÎü[w¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$5f05e717-a51a-4a99-bb11-cc493217f93f¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§‹:d·has_pluto_hook_features¤bodyÚÙ*

Norm of gradient increases exponentially with depth. Issue for deep neural net. Consider output

$$\begin{bmatrix} y_{1,1} & \ldots & y_{1,d_\text{emb}}\\ \vdots & \ddots & \vdots\\ y_{d_\text{batch},1} & \ldots & y_{d_\text{batch},d_\text{emb}} \end{bmatrix}$$

Normalization : $y_{i,j} \mapsto g(y_{i,j} - \mu_{i,j})/\sigma_{i,j}$ for gain $g$, mean $\mu$ and standard deviation $\sigma$.

  • Batch normalization : $\sigma_{i,j} = \sigma_{j}$ [IS15]

  • Layer normalization : $\sigma_{i,j} = \sigma_{i}$ [BKH16]

Batch norm depends on the batch hence is tricky to implement. Layer normalization is used in [VSP+17].

°persist_js_stateÂÙ$b56e9e56-e74a-401b-b4b5-f36bb33341d5Чrunning§runtimeÎ`ĸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$b56e9e56-e74a-401b-b4b5-f36bb33341d5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËA򻒤 ž·has_pluto_hook_features¤bodyÚb

[HZRS15] K. He, X. Zhang, S. Ren and J. Sun. Deep Residual Learning for Image Recognition (Dec 2015), arXiv:1512.03385. Accessed on Nov 12, 2024.

°persist_js_stateÂÙ$57c2c944-0d91-489d-8ad7-f5520e71ef3eЧrunning§runtimeÎÊ4¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$57c2c944-0d91-489d-8ad7-f5520e71ef3e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�+II·has_pluto_hook_features¤bodyÙP

Byte Pair Encoding

°persist_js_stateÂÙ$bf563783-9784-4c74-a7b1-6d7a3ed618c5Чrunning§runtimeΨ ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$bf563783-9784-4c74-a7b1-6d7a3ed618c5¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�.€o·has_pluto_hook_features¤bodyÙ\

Matrix form of attention

°persist_js_stateÂÙ$f8330700-e964-4e19-9c55-2b11df45789eЧrunning§runtimeΣð¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f8330700-e964-4e19-9c55-2b11df45789e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-ÏÈ·has_pluto_hook_features¤bodyÙZ

Embedding sizes in LLMs

°persist_js_stateÂÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ecЧrunning§runtimeÎ5uï¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ec¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§fíš·has_pluto_hook_features¤bodyÚñÝ
What is the time complexity of inference with respect to $d_\text{emb}$, $n_\text{voc}$, $n_\text{ctx}$, $d_\text{ff}$, $h$ and $N$ ?
InputParametersTime
$CX + P \in \mathbb{R}^{d_\text{emb} \times n_\text{ctx}}$$W_j^V \in \mathbb{R}^{d_v \times d_\text{emb}}$$O(d_v d_\text{emb} n_\text{ctx})$
$CX + P \in \mathbb{R}^{d_\text{emb} \times n_\text{ctx}}$$W_j^K, W_j^Q \in \mathbb{R}^{d_k \times d_\text{emb}}$$O(d_k d_\text{emb} n_\text{ctx})$
$K, Q \in \mathbb{R}^{d_k \times n_\text{ctx}}$$O(d_k n_\text{ctx}^2)$
$V \in \mathbb{R}^{d_v \times n_\text{ctx}}, ... \in \mathbb{R}^{n_\text{ctx} \times n_\text{ctx}}$$O(d_v n_\text{ctx}^2)$
$... \in \mathbb{R}^{d_v \times n_\text{ctx}}$$W^O \in \mathbb{R}^{d_\text{emb} \times d_v}$$O(d_\text{emb} d_v n_\text{ctx})$
$... \in \mathbb{R}^{d_\text{emb} \times n_\text{ctx}}$$W_1 \in \mathbb{R}^{d_\text{ff} \times d_\text{emb}}$$O(d_\text{emb} d_\text{ff} n_\text{ctx})$
$... \in \mathbb{R}^{d_\text{ff} \times n_\text{ctx}}$$W_2 \in \mathbb{R}^{d_\text{emb} \times d_\text{ff}}$$O(d_\text{emb} d_\text{ff} n_\text{ctx})$

So for $N$ layers (ignoring the complexity of the embedding):

$$O(Nn_\text{ctx}(n_\text{ctx}(d_v + d_k) + d_\text{emb}(d_v+d_k+d_\text{ff})))$$

Assuming that $d_v, d_k, d_\text{ff}$ has the same scale as $d_\text{emb}$:

$$O(Nn_\text{ctx}^2d_\text{emb} + Nn_\text{ctx}d_\text{emb}^2)$$

°persist_js_stateÂÙ$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5ddЧrunning§runtimeÎÒ‹¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5dd¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§X2¥·has_pluto_hook_features¤bodyÚÝ+

Pre-activation for residual neural networks introduced in [HZRS16] and used in GPT-2 [RWC+19]. See figure on the right.

Rotary Positional Encoding [SLP+23] replaces $W^K(Cx_i + p_i)$ and $W^Q(Cx_i + p_i)$ by $R^i W^KCx_i$ and $R^i W^QCx_i$ where $R$ is a rotation matrix. Advantage : $\langle k_i, q_j \rangle$ contains $R^{i - j}$ → relative difference of position.

°persist_js_stateÂÙ$1d5b1b7c-828c-4a16-b446-cff21b015d45Чrunning§runtimeÎÒ¡j‚¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$1d5b1b7c-828c-4a16-b446-cff21b015d45¹depends_on_disabled_cells¦queued¤logs’ˆ¥group¥utils¤lineÿ£msg’ÙXLoading bibliography from `/home/runner/work/LINMA2472/LINMA2472/Lectures/biblio.bib`...ªtext/plain¥level¤Info¢id¹Main_workspace#3_a1ed908b§cell_idÙ$1d5b1b7c-828c-4a16-b446-cff21b015d45¦kwargs�¤fileÙ7/home/runner/work/LINMA2472/LINMA2472/Lectures/utils.jlˆ¥group¥utils¤lineÿ£msg’²Loading completed.ªtext/plain¥level¤Info¢id¹Main_workspace#3_d0c2d7aa§cell_idÙ$1d5b1b7c-828c-4a16-b446-cff21b015d45¦kwargs�¤fileÙ7/home/runner/work/LINMA2472/LINMA2472/Lectures/utils.jl¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassignee¦biblio²last_run_timestampËAÚªí¥ç%š·has_pluto_hook_features¤body…¦prefixÙ(DocumenterCitations.CitationBibliography¨objectid°23f63578dad2368d¤type¦struct¬prefix_short´CitationBibliography¨elementsœ’§bibfile’Ù;"/home/runner/work/LINMA2472/LINMA2472/Lectures/biblio.bib"ªtext/plain’¥style’¬AlphaStyle()ªtext/plain’ªinsert_css’¤trueªtext/plain’ªshow_hover’¤trueªtext/plain’®show_backlinks’¤trueªtext/plain’§entries’…¦prefixÙ9OrderedCollections.OrderedDict{String, BibInternal.Entry}¨objectid°76408bbddddfac7c¤type¤Dict¬prefix_short«OrderedDict¨elements›’’¸"arechiga2023Dragguided"ªtext/plain’…¦prefix¥Entry¨objectid°7a2dd77f3d074895¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°68075b1ba80f5eca¤type¦struct¬prefix_short¦Access¨elements“’£doi’»"10.48550/arXiv.2306.09935"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù!"http://arxiv.org/abs/2306.09935"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°a818cfd62ce9c5bc¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid°a0d40b694a180fcf¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’¦"June"ªtext/plain’¤year’¦"2023"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°ab336733b31e21f3¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°36bae899fa044eb1¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’¬"2306.09935"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’¸"arechiga2023Dragguided"ªtext/plain’¢in’…¦prefix¢In¨objectid°28458595d7e6fe6c¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’²"arXiv:2306.09935"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°b7764ee4ca211a89¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’Ù;"Drag-Guided Diffusion Models for Vehicle Image Generation"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’­"ba2016Layer"ªtext/plain’…¦prefix¥Entry¨objectid°3dbaf2a24442dbc6¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°37abcf945921f1f0¤type¦struct¬prefix_short¦Access¨elements“’£doi’»"10.48550/arXiv.1607.06450"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù!"http://arxiv.org/abs/1607.06450"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°448f9a81e613ad85¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid¯9a8b0c9b23cd8d0¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’¦"July"ªtext/plain’¤year’¦"2016"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°20da465e1ea8ee6e¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°3ea0d0b592b4e288¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’¬"1607.06450"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’­"ba2016Layer"ªtext/plain’¢in’…¦prefix¢In¨objectid°2a4267f7ab647e54¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’²"arXiv:1607.06450"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°fdccefbd590b9785¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’¹"Layer {{Normalization}}"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’´"bahdanau2016Neural"ªtext/plain’…¦prefix¥Entry¨objectid°ce4bae33d0c16eb4¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°2017f7d8f7250ba9¤type¦struct¬prefix_short¦Access¨elements“’£doi’º"10.48550/arXiv.1409.0473"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù "http://arxiv.org/abs/1409.0473"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°b7f51b3fccc46a5b¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid¯260a5932b24cd35¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’¥"May"ªtext/plain’¤year’¦"2016"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°7dd5ade158856507¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°e2ea2bb6648c272f¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’«"1409.0473"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’´"bahdanau2016Neural"ªtext/plain’¢in’…¦prefix¢In¨objectid°88cd3f3f233be78e¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’±"arXiv:1409.0473"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°960f124209bf3b50¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’ÙW"Neural {{Machine Translation}} by {{Jointly Learning}} to {{Align}} and {{Translate}}"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’²"bengio2000Neural"ªtext/plain’…¦prefix¥Entry¨objectid°ef491b95e78a5e67¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°74d9cd11d181380d¤type¦struct¬prefix_short¦Access¨elements“’£doi’¢""ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ùn"https://proceedings.neurips.cc/pape" ⋯ 36 bytes ⋯ "bf572b5940d7d9a8fa4c-Abstract.html"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°dd6ffab617c67a6f¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’Ù7"Advances in {{Neural Information Processing Systems}}"ªtext/plain’¤date’…¦prefix¤Date¨objectid¯57944a8d83589a4¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’¢""ªtext/plain’¤year’¦"2000"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°53a49f0a042fabad¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid¯fe27083dd4d44f3¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’¢""ªtext/plain’¦eprint’¢""ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’²"bengio2000Neural"ªtext/plain’¢in’…¦prefix¢In¨objectid°2542eca758e416e1¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’¢""ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’«"MIT Press"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¤"13"ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°756a2d5b48748d73¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’Ù+"A {{Neural Probabilistic Language Model}}"ªtext/plain’¤type’¯"inproceedings"ªtext/plainÙ!application/vnd.pluto.tree+object’’»"bradley2024ClassifierFree"ªtext/plain’…¦prefix¥Entry¨objectid°9b55f4d9bae2d0dc¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°7280b3075e91b7ef¤type¦struct¬prefix_short¦Access¨elements“’£doi’¢""ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù,"https://openreview.net/forum?id=8K36RkrI7N"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°490b6a7deea4589d¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’ÙM"The {{Thirteenth International Conference}} on {{Learning Representations}}"ªtext/plain’¤date’…¦prefix¤Date¨objectid°44de9f60e6938dd6¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’©"October"ªtext/plain’¤year’¦"2024"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°f0460c99bd1a3556¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid¯fe27083dd4d44f3¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’¢""ªtext/plain’¦eprint’¢""ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’»"bradley2024ClassifierFree"ªtext/plain’¢in’…¦prefix¢In¨objectid°d28d30fd8b040711¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’¢""ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’¢""ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°ba57ec10d2713261¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’Ù;"Classifier-{{Free Guidance}} Is a {{Predictor-Corrector}}"ªtext/plain’¤type’¯"inproceedings"ªtext/plainÙ!application/vnd.pluto.tree+object’’²"britz2017Massive"ªtext/plain’…¦prefix¥Entry¨objectid°30405f4d93baf8a0¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°f33ac8755b1482db¤type¦struct¬prefix_short¦Access¨elements“’£doi’»"10.48550/arXiv.1703.03906"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù!"http://arxiv.org/abs/1703.03906"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°9589686c07dc8a48¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid°98ec10656e7ef23f¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’§"March"ªtext/plain’¤year’¦"2017"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°502cd15cfd2b3b93¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°fc9d173602c735a8¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’¬"1703.03906"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’²"britz2017Massive"ªtext/plain’¢in’…¦prefix¢In¨objectid°4f435bfc3c88aa3f¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’²"arXiv:1703.03906"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°b1fb0e45c1b6cb73¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’ÙI"Massive {{Exploration}} of {{Neural Machine Translation Architectures}}"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’³"brown2020Language"ªtext/plain’…¦prefix¥Entry¨objectid°85472eecdc0fbf20¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid¯f87101c311760be¤type¦struct¬prefix_short¦Access¨elements“’£doi’»"10.48550/arXiv.2005.14165"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù!"http://arxiv.org/abs/2005.14165"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°b60cade3c8e6d23f¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid°b728a697c83fac4b¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’¦"July"ªtext/plain’¤year’¦"2020"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°a353803da37677b7¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°455f6406b6d8a88a¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’¬"2005.14165"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’³"brown2020Language"ªtext/plain’¢in’…¦prefix¢In¨objectid¯d286662406f2ed6¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’²"arXiv:2005.14165"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°17efd4980daea59e¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’Ù/"Language {{Models}} Are {{Few-Shot Learners}}"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’¶"burgert2023Diffusion"ªtext/plain’…¦prefix¥Entry¨objectid°d2228f8e6c6a3e57¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°3810f2ff67ef9db2¤type¦struct¬prefix_short¦Access¨elements“’£doi’»"10.48550/arXiv.2312.03817"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù!"http://arxiv.org/abs/2312.03817"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°efab6eb03bc280d7¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid°56a888664e909229¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’ª"December"ªtext/plain’¤year’¦"2023"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°3998ded9cf96aee5¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°9e63dc39e7c8fde4¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’¬"2312.03817"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’¶"burgert2023Diffusion"ªtext/plain’¢in’…¦prefix¢In¨objectid°1eb491b639b558b9¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’²"arXiv:2312.03817"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°5f2ecf789c49922a¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’Ù?"Diffusion {{Illusions}}: {{Hiding Images}} in {{Plain Sight}}"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’³"cho2014Properties"ªtext/plain’…¦prefix¥Entry¨objectid°352d4489631f73a5¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°a25124170145792a¤type¦struct¬prefix_short¦Access¨elements“’£doi’º"10.48550/arXiv.1409.1259"ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù "http://arxiv.org/abs/1409.1259"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°df18fd281acfe303¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’¢""ªtext/plain’¤date’…¦prefix¤Date¨objectid°530b3bc654338901¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’©"October"ªtext/plain’¤year’¦"2014"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°7beea50bd47ba8a8¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid°d25a39a73182c654¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’§"arXiv"ªtext/plain’¦eprint’«"1409.1259"ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’³"cho2014Properties"ªtext/plain’¢in’…¦prefix¢In¨objectid°111cbd91f193dae9¤type¦struct¬prefix_short¢In¨elementsž’§address’¢""ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’¢""ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’±"arXiv:1409.1259"ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¢""ªtext/plain’©publisher’§"arXiv"ªtext/plain’¦school’¢""ªtext/plain’¦series’¢""ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°e5887c6b2927c57d¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’ÙY"On the {{Properties}} of {{Neural Machine Translation}}: {{Encoder-Decoder Approaches}}"ªtext/plain’¤type’¦"misc"ªtext/plainÙ!application/vnd.pluto.tree+object’’·"dhariwal2024Diffusion"ªtext/plain’…¦prefix¥Entry¨objectid°c5b75739ad9b1bc9¤type¦struct¬prefix_short¥Entry¨elementsœ’¦access’…¦prefix¦Access¨objectid°c4166f508bcaff54¤type¦struct¬prefix_short¦Access¨elements“’£doi’¢""ªtext/plain’¬howpublished’¢""ªtext/plain’£url’Ù0"https://dl.acm.org/doi/10.5555/3540261.3540933"ªtext/plainÙ!application/vnd.pluto.tree+object’§authors’…¦prefix°BibInternal.Name¨objectid°1be005701466c3aa¤type¥Array¬prefix_short ¨elements‘¤moreÙ!application/vnd.pluto.tree+object’©booktitle’Ùo"Proceedings of the 35th {{Internati" ⋯ 27 bytes ⋯ "al Information Processing Systems}}"ªtext/plain’¤date’…¦prefix¤Date¨objectid°ba66609228f0847e¤type¦struct¬prefix_short¤Date¨elements“’£day’¢""ªtext/plain’¥month’¦"June"ªtext/plain’¤year’¦"2024"ªtext/plainÙ!application/vnd.pluto.tree+object’§editors’…¦prefix°BibInternal.Name¨objectid°e9d5b25430521117¤type¥Array¬prefix_short ¨elements�Ù!application/vnd.pluto.tree+object’¦eprint’…¦prefix¦Eprint¨objectid¯fe27083dd4d44f3¤type¦struct¬prefix_short¦Eprint¨elements“’®archive_prefix’¢""ªtext/plain’¦eprint’¢""ªtext/plain’­primary_class’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¢id’·"dhariwal2024Diffusion"ªtext/plain’¢in’…¦prefix¢In¨objectid°63ca47339d4bcb99¤type¦struct¬prefix_short¢In¨elementsž’§address’³"Red Hook, NY, USA"ªtext/plain’§chapter’¢""ªtext/plain’§edition’¢""ªtext/plain’«institution’¢""ªtext/plain’¤isbn’³"978-1-71384-539-3"ªtext/plain’¤issn’¢""ªtext/plain’§journal’¢""ªtext/plain’¦number’¢""ªtext/plain’¬organization’¢""ªtext/plain’¥pages’¬"8780--8794"ªtext/plain’©publisher’¸"Curran Associates Inc."ªtext/plain’¦school’¢""ªtext/plain’¦series’®"{{NIPS}} '21"ªtext/plain’¦volume’¢""ªtext/plainÙ!application/vnd.pluto.tree+object’¦fields’…¦prefix´Dict{String, String}¨objectid°7f40ee6ddc9d1250¤type¤Dict¬prefix_short¤Dict¨elements‘¤moreÙ!application/vnd.pluto.tree+object’¤note’¢""ªtext/plain’¥title’Ù3"Diffusion Models Beat {{GANs}} on Image Synthesis"ªtext/plain’¤type’¯"inproceedings"ªtext/plainÙ!application/vnd.pluto.tree+object¤moreÙ!application/vnd.pluto.tree+object’©citations’…¦prefixÙ-OrderedCollections.OrderedDict{String, Int64}¨objectid°a5a902d73c92608a¤type¤Dict¬prefix_short«OrderedDict¨elements�Ù!application/vnd.pluto.tree+object’®page_citations’…¦prefix¹Dict{String, Set{String}}¨objectid°1d2bd7cf2b980780¤type¤Dict¬prefix_short¤Dict¨elements�Ù!application/vnd.pluto.tree+object’ªanchor_map’…¦prefix´Documenter.AnchorMap¨objectid°25f8a7b5bc322855¤type¦struct¬prefix_short©AnchorMap¨elements’’£map’…¦prefixÙ5Dict{String, Dict{String, Vector{Documenter.Anchor}}}¨objectid¯d5127ec8be16b67¤type¤Dict¬prefix_short¤Dict¨elements�Ù!application/vnd.pluto.tree+object’¥count’¡0ªtext/plainÙ!application/vnd.pluto.tree+object’«anchor_keys’…¦prefixÙPBijections.Bijection{String, String, Dict{String, String}, Dict{String, String}}¨objectid°54dc6af582b2b169¤type¤Dict¬prefix_short©Bijection¨elements�Ù!application/vnd.pluto.tree+object’­hover_entries’…¦prefixÙ#Dict{String, Tuple{String, String}}¨objectid°d838e33421b0e08f¤type¤Dict¬prefix_short¤Dict¨elements�Ù!application/vnd.pluto.tree+object’©backlinks’…¦prefixÙ6Dict{String, Vector{DocumenterCitations.CitationSite}}¨objectid°5a56254925eb61ff¤type¤Dict¬prefix_short¤Dict¨elements�Ù!application/vnd.pluto.tree+object°persist_js_stateÂÙ$a14e505e-2e4a-4c73-8133-7560ba58916bЧrunning§runtimeΛõ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$a14e505e-2e4a-4c73-8133-7560ba58916b¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/Îd·has_pluto_hook_features¤bodyÙd

Key-Value (KV) cache

°persist_js_stateÂÙ$2a7e5096-1e8d-4506-96d2-86de0a7d39aaЧrunning§runtimeÎݸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$2a7e5096-1e8d-4506-96d2-86de0a7d39aa¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦6YG·has_pluto_hook_features¤bodyÙ´

Forcing $D = C^\top$ appears to work well in practice [PW17], this is what is used in [VSP+17].

°persist_js_stateÂÙ$6712c883-b407-47e1-a666-4de05f8f8d6eЧrunning§runtimeΟM\¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$6712c883-b407-47e1-a666-4de05f8f8d6e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí£~“§·has_pluto_hook_features¤bodyÛÇ

$$\begin{multline} \hat{p}(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}) = \\ \text{softmax}(W_2 \tanh(W_1 \begin{bmatrix} C x_{-1}\\ \vdots\\ C x_{-n_\text{ctx}} \end{bmatrix} )) \end{multline}$$

°persist_js_stateÂÙ$af8194a1-a358-4cf7-b446-6b377cb76687Чrunning§runtimeÎÍŠ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$af8194a1-a358-4cf7-b446-6b377cb76687¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/‡­·has_pluto_hook_features¤bodyÚo

The feed-forward network is implemented independently for the output of each query so each query can be processed independently through each layer. The next layer allows each queries to then look at the results of the previous layer for past (because of the mask) queries.

°persist_js_stateÂÙ$04e9b912-6712-4290-acc4-f24bb27a1469Чrunning§runtimeΟý¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$04e9b912-6712-4290-acc4-f24bb27a1469¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�0¹·has_pluto_hook_features¤bodyÙR

Machine translation

°persist_js_stateÂÙ$d014e6aa-92f6-4ca1-be47-516565d1bb20Чrunning§runtimeÎETĸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$d014e6aa-92f6-4ca1-be47-516565d1bb20¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦Fº4·has_pluto_hook_features¤bodyÛ¦8

Heads focus on different aspects. Their outputs are combined with $W^O \in \mathbb{R}^{d_\text{emb} \times hd_v}$:

$$\begin{align} \text{head}_j & = \text{Attention}(W_j^VV, W_j^KK, W_j^QQ)\\ \text{MultiHead}(V, K, Q) & = W^O\text{vcat}(\text{head}_1, \ldots, \text{head}_h) \end{align}$$

See [VSP+17; Figure 2] on the right.

Similarly, in the masked case:

$$\begin{align} \text{head}_j & = \text{Masked-Attention}(W_j^VV, W_j^KK, W_j^QQ)\\ \text{Masked-MultiHead}&(V, K, Q) = W^O\text{vcat}(\text{head}_1, \ldots, \text{head}_h) \end{align}$$

°persist_js_stateÂÙ$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03Чrunning§runtimeÎän¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí§Ú'·has_pluto_hook_features¤bodyÚæ

[SHB16] R. Sennrich, B. Haddow and A. Birch. Neural Machine Translation of Rare Words with Subword Units (Jun 2016), arXiv:1508.07909. Accessed on Oct 23, 2024.
[KR18] T. Kudo and J. Richardson. SentencePiece: A Simple and Language Independent Subword Tokenizer and Detokenizer for Neural Text Processing (Aug 2018), arXiv:1808.06226. Accessed on Oct 23, 2024.

°persist_js_stateÂÙ$e2eca085-9f99-4e3a-9db4-e7f692aedd34Чrunning§runtimeÎÿ¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$e2eca085-9f99-4e3a-9db4-e7f692aedd34¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�-\ÿ·has_pluto_hook_features¤bodyÙx

Text to vectors : step 2 → embedding

°persist_js_stateÂÙ$95504a74-d5ef-4fb7-83a0-88914c7cbc59Чrunning§runtimeÍ<š¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$95504a74-d5ef-4fb7-83a0-88914c7cbc59¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeÙ!application/vnd.pluto.tree+object¬rootassigneeÀ²last_run_timestampËAÚªí£Ñ¯·has_pluto_hook_features¤body…¦prefix¥Int64¨objectid°c8c9dd3b74dbd892¤type¥Array¬prefix_short ¨elements’’’¡1ªtext/plain’’¡1ªtext/plain°persist_js_stateÂÙ$453544fc-0e3e-4e04-8c0c-192f3a038884Чrunning§runtimeÎ÷¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$453544fc-0e3e-4e04-8c0c-192f3a038884¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�/"·has_pluto_hook_features¤bodyÙR

Positional encoding

°persist_js_stateÂÙ$0583ee0c-3802-4e81-b179-a80a82493b43Чrunning§runtimeÎ㮸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$0583ee0c-3802-4e81-b179-a80a82493b43¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦6&)·has_pluto_hook_features¤bodyÚ@

Byte Pair Encoding algorithm [SHB16] greedily merges the most frequent pair of tokens over the dataset into a new token. Most used implementations are SentencePiece [KR18] and tiktoken (play with it here). For instance, on this example, the pair ('a', 'a') is the most frequent so we substitute it by a new token, say 'Z':

°persist_js_stateÂÙ$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2Чrunning§runtimeÎø[¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¤,9“·has_pluto_hook_features¤bodyÚb
Should we discard all these intermediate $\hat{Y}_i$ we computated or can we reuse it for the following token ?

For the next token, the corresponding intermediate probabilities would be:

$$(\hat{p}_i(x_{-n_\text{ctx}+2} | x_{-n_\text{ctx}+1}), \ldots, \hat{p}_i(x_{0} | x_{-1}, \ldots, x_{-n_\text{ctx}+1}), \hat{p}_i(x_1 | x_{0}, \ldots, x_{-n_\text{ctx}+1}))$$

Note that

$$\begin{align} \hat{p}_i(x_{-n_\text{ctx}+2} | x_{-n_\text{ctx}+1}) & \approx \hat{p}_i(x_{-n_\text{ctx}+2} | x_{-n_\text{ctx}+1}, x_{-n_\text{ctx}})\\ \hat{p}_i(x_{0} | x_{-1}, \ldots, x_{-n_\text{ctx}+1}) & \approx \hat{p}_i(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}) \end{align}$$

So for any $j < n_\text{ctx}$, the $j$th column of the $\hat{Y}_i'$ that should be computed for the new token is approximately equal to the $(j+1)$th column of $\hat{Y}_i$ that we already computed for the previous token. What's more, the column of $\hat{Y}_i$ was computed with one more token as context compared to what we need to compute in $\hat{Y}_i'$. So even though it's not equal, reusing what we computed in $\hat{Y}_i$ should provide better result, assuming the trained transformers using this auto-regressive structure in his layers.

°persist_js_stateÂÙ$01372b00-ecb2-42bd-b408-13234717d969Чrunning§runtimeÎ cǸdepends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$01372b00-ecb2-42bd-b408-13234717d969¹depends_on_disabled_cells¦queued¤logs�¦output†¤mimeªtext/plain¬rootassigneeÀ²last_run_timestampËAÚªí©Âª·has_pluto_hook_features¤bodyÙ+new_token (generic function with 2 methods)°persist_js_stateÂÙ$f4366cf6-2be0-42b8-96c4-120be3f5c25eЧrunning§runtimeÎÐ’)¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$f4366cf6-2be0-42b8-96c4-120be3f5c25e¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí¦,Ǭ·has_pluto_hook_features¤bodyÚ

References

°persist_js_stateÂÙ$c8923675-e73e-4621-82b9-966d8b003b97Чrunning§runtimeΪ–¸depends_on_skipped_cellsµpublished_object_keys�§errored§cell_idÙ$c8923675-e73e-4621-82b9-966d8b003b97¹depends_on_disabled_cells¦queued¤logs�¦output†¤mime©text/html¬rootassigneeÀ²last_run_timestampËAÚªí�0°·has_pluto_hook_features¤bodyÙb

Encoder-decoder transformer

°persist_js_state±published_objects€­julia_version§v1.13.0±cell_dependenciesÞtÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3„´precedence_heuristic §cell_idÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3´downstream_cells_map€²upstream_cells_mapÞ°Luxor.background�¬LaTeXStrings‘Ù$32621224-a782-4bf6-9570-562cf2bb7360¬Luxor.origin�¤HTML�¦@L_str�¦HAlign�¤html�¥@draw�¬Luxor.sethue�¤text�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2¥Point�°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¬Luxor.finish�¸LaTeXStrings.latexstring�¤Base�§@md_str�¨fontsize�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bb„´precedence_heuristic §cell_idÙ$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bb´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6622f9f0-cecc-476e-9d49-7d651f433b9f„´precedence_heuristic §cell_idÙ$6622f9f0-cecc-476e-9d49-7d651f433b9f´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$70f395b2-f8c2-44d5-b0af-702659dd7fee„´precedence_heuristic §cell_idÙ$70f395b2-f8c2-44d5-b0af-702659dd7fee´downstream_cells_map€²upstream_cells_map�¤dict‘Ù$d558636d-c714-4033-ae73-5b92c3cdedf3Ù$771d39a5-74dc-494e-929e-1164bb08b983„´precedence_heuristic §cell_idÙ$771d39a5-74dc-494e-929e-1164bb08b983´downstream_cells_map€²upstream_cells_map‚¤llms‘Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc¥table‘Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf„´precedence_heuristic §cell_idÙ$85a10748-8d19-44a8-a1c5-0d13b093f1bf´downstream_cells_map�°draw_transformer™Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03Ù$c5be3956-5102-4d88-bfdb-9813c0555fe1Ù$18c26901-85eb-45ac-89bf-b03bd255007aÙ$5f05e717-a51a-4a99-bb11-cc493217f93fÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Ù$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5ddÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ecÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3²upstream_cells_map‹¥scale�¦sethue�ªsetopacity�¤text�§readpng�¥Point�°Luxor.placeimage�£box�©translate�¨fontsize�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8e„´precedence_heuristic §cell_idÙ$4f1d5112-dbac-4eb6-8518-0dc4193c3f8e´downstream_cells_map�£bibŸÙ$61dc1905-338f-4bfd-a158-2f6bacff769eÙ$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03Ù$97463c54-7cc7-4497-a8a6-6422f5f582bdÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221Ù$76e2f97b-1c06-40cd-b134-d5155aa5587dÙ$75ca478c-916f-464a-9435-8208ee726d50Ù$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2aÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012Ù$e41d13ca-1dc1-45ae-9fa6-a83c4101120dÙ$c032b3ff-c539-4e38-81d0-39b28b3a8076Ù$b56e9e56-e74a-401b-b4b5-f36bb33341d5Ù$2a8433e3-9a3b-487b-abf3-09278ea42389Ù$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfcÙ$45efc71d-d5f8-474e-9b89-e72fac7110fdÙ$f7ca738d-5215-4e91-a2f3-a5ff10911313²upstream_cells_map‚§bibrefs�¦biblio‘Ù$1d5b1b7c-828c-4a16-b446-cff21b015d45Ù$93200f46-7c8f-4362-a445-43c57b50a2d2„´precedence_heuristic §cell_idÙ$93200f46-7c8f-4362-a445-43c57b50a2d2´downstream_cells_map€²upstream_cells_map‚¤llms‘Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc¥names�Ù$2a8433e3-9a3b-487b-abf3-09278ea42389„´precedence_heuristic §cell_idÙ$2a8433e3-9a3b-487b-abf3-09278ea42389´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$61dc1905-338f-4bfd-a158-2f6bacff769e„´precedence_heuristic §cell_idÙ$61dc1905-338f-4bfd-a158-2f6bacff769e´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221„´precedence_heuristic §cell_idÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$76e2f97b-1c06-40cd-b134-d5155aa5587d„´precedence_heuristic §cell_idÙ$76e2f97b-1c06-40cd-b134-d5155aa5587d´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$f6f7376e-9984-4289-b8ff-9d47e5358791„´precedence_heuristic §cell_idÙ$f6f7376e-9984-4289-b8ff-9d47e5358791´downstream_cells_mapƒ³DocumenterCitations�§Logging�£CSV‘Ù$579a203b-e6f7-4190-b874-18b00a5c3f77²upstream_cells_map€Ù$b1a924f4-e2f0-445c-830f-94287a0e52f7„´precedence_heuristic §cell_idÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7´downstream_cells_map�°numerical_lookup‘Ù$95504a74-d5ef-4fb7-83a0-88914c7cbc59²upstream_cells_map…§findmax�§collect�¤keys�¦values�£dot�Ù$f2cba2aa-c541-4692-a441-e65741750a15„´precedence_heuristic §cell_idÙ$f2cba2aa-c541-4692-a441-e65741750a15´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6fc13413-53de-4c75-9b9e-620e0b7f8a1f„´precedence_heuristic §cell_idÙ$6fc13413-53de-4c75-9b9e-620e0b7f8a1f´downstream_cells_map€²upstream_cells_mapƒ¢qa�§@md_str�¨getindex�Ù$ccf2dc71-b883-497a-bc58-29ffaf9ea4ad„´precedence_heuristic §cell_idÙ$ccf2dc71-b883-497a-bc58-29ffaf9ea4ad´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2a„´precedence_heuristic §cell_idÙ$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2a´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$86101f07-67c5-4df2-911c-4013c44d6c5b„´precedence_heuristic §cell_idÙ$86101f07-67c5-4df2-911c-4013c44d6c5b´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$a3efd921-eb14-4901-9d6c-800cc812fe02„´precedence_heuristic §cell_idÙ$a3efd921-eb14-4901-9d6c-800cc812fe02´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6800afbf-8ac6-4308-b4cc-b37da57e42c1„´precedence_heuristic §cell_idÙ$6800afbf-8ac6-4308-b4cc-b37da57e42c1´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$18c26901-85eb-45ac-89bf-b03bd255007a„´precedence_heuristic §cell_idÙ$18c26901-85eb-45ac-89bf-b03bd255007a´downstream_cells_map€²upstream_cells_mapÞ°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87¬Luxor.finish�¤Base�£img�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$bcbb3db2-85b3-4cb0-9309-f5c032d14da5„´precedence_heuristic §cell_idÙ$bcbb3db2-85b3-4cb0-9309-f5c032d14da5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$4e10271c-49f8-4f1d-869c-5fa11275d7f6„´precedence_heuristic §cell_idÙ$4e10271c-49f8-4f1d-869c-5fa11275d7f6´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$c1437dcc-22cb-424f-9b8e-326172f82d86„´precedence_heuristic §cell_idÙ$c1437dcc-22cb-424f-9b8e-326172f82d86´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$c09ec483-9fcf-48e7-b3c0-2508289e3cf3„´precedence_heuristic §cell_idÙ$c09ec483-9fcf-48e7-b3c0-2508289e3cf3´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$1bbf2152-4fdf-4ed2-9bdf-95d699824d11„´precedence_heuristic §cell_idÙ$1bbf2152-4fdf-4ed2-9bdf-95d699824d11´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$728c16b7-50cd-43fe-a0d7-61d37952a6b3„´precedence_heuristic §cell_idÙ$728c16b7-50cd-43fe-a0d7-61d37952a6b3´downstream_cells_map€²upstream_cells_map�ªpair_stats‘Ù$89305cae-098f-4644-9109-d00f1e3bc04cÙ$1faa4ab2-6c93-47dc-b631-8be52780fe7d„´precedence_heuristic §cell_idÙ$1faa4ab2-6c93-47dc-b631-8be52780fe7d´downstream_cells_map€²upstream_cells_map‚¤dict‘Ù$d558636d-c714-4033-ae73-5b92c3cdedf3®softmax_lookup‘Ù$77f446ac-6030-48f2-9bea-93c427f9fcb9Ù$c3a9a0ce-3450-4b17-8696-2ab8534b29f2„´precedence_heuristic §cell_idÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2´downstream_cells_map�¦iter_3�²upstream_cells_map‚©new_token’Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993fÙ$01372b00-ecb2-42bd-b408-13234717d969¦iter_2‘Ù$736920df-e4bb-4535-b982-e397aa0a782dÙ$e383bb72-49a1-4df1-84c3-b95a2ffe00f5„´precedence_heuristic §cell_idÙ$e383bb72-49a1-4df1-84c3-b95a2ffe00f5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6aa690e9-389f-4398-abae-b95060db4d90„´precedence_heuristic §cell_idÙ$6aa690e9-389f-4398-abae-b95060db4d90´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$4b61363d-87c9-4755-8286-44df34e9dd6a„´precedence_heuristic §cell_idÙ$4b61363d-87c9-4755-8286-44df34e9dd6a´downstream_cells_map€²upstream_cells_map†¤Base�­Base.getindex�©@html_str�¢qa�§@md_str�®Base.Docs.HTML�Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87„´precedence_heuristic §cell_idÙ$94ae440d-0644-49db-9461-f1a1ff1d7f87´downstream_cells_map�¤citeÜÙ$f4366cf6-2be0-42b8-96c4-120be3f5c25eÙ$0583ee0c-3802-4e81-b179-a80a82493b43Ù$2a7e5096-1e8d-4506-96d2-86de0a7d39aaÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904Ù$55a09acc-84da-491c-86ba-9a66f4ea52feÙ$225e58ba-b78d-4a0a-be4f-ad642c879b93Ù$a21fbc70-9137-4d0e-8c8c-cbdc5269778fÙ$86101f07-67c5-4df2-911c-4013c44d6c5bÙ$5150d8f3-6e85-43f2-801a-eae5cc3e3095Ù$d014e6aa-92f6-4ca1-be47-516565d1bb20Ù$18c26901-85eb-45ac-89bf-b03bd255007aÙ$5f05e717-a51a-4a99-bb11-cc493217f93fÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Ù$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5ddÙ$25b79953-fd7c-46c1-b760-d57c09910981Ù$c1437dcc-22cb-424f-9b8e-326172f82d86²upstream_cells_map‚§bibcite�¦biblio‘Ù$1d5b1b7c-828c-4a16-b446-cff21b015d45Ù$55435b26-7fc3-4c8b-8013-6fd4fb65a08e„´precedence_heuristic §cell_idÙ$55435b26-7fc3-4c8b-8013-6fd4fb65a08e´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$225e58ba-b78d-4a0a-be4f-ad642c879b93„´precedence_heuristic §cell_idÙ$225e58ba-b78d-4a0a-be4f-ad642c879b93´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$32621224-a782-4bf6-9570-562cf2bb7360„´precedence_heuristic§cell_idÙ$32621224-a782-4bf6-9570-562cf2bb7360´downstream_cells_mapŠ­LinearAlgebra�­MathTeXEngine�°HypertextLiteral‘Ù$5058e4eb-c53d-4468-b7ff-4f04ded96418²PlutoTeachingTools‘Ù$5058e4eb-c53d-4468-b7ff-4f04ded96418§PlutoUI‘Ù$5058e4eb-c53d-4468-b7ff-4f04ded96418¬PrettyTables�¬LaTeXStrings‘Ù$3d8add97-59e1-444a-838b-85c2a2ac60b3²ExperimentalLayout�ªDataFrames�¥LuxoršÙ$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03Ù$c5be3956-5102-4d88-bfdb-9813c0555fe1Ù$18c26901-85eb-45ac-89bf-b03bd255007aÙ$5f05e717-a51a-4a99-bb11-cc493217f93fÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Ù$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5ddÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ecÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf²upstream_cells_map€Ù$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0„´precedence_heuristic §cell_idÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0´downstream_cells_map€²upstream_cells_mapƒ¢qa�§@md_str�¨getindex�Ù$0c0c1163-0aec-4089-9acc-539b3a86d0b3„´precedence_heuristic §cell_idÙ$0c0c1163-0aec-4089-9acc-539b3a86d0b3´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$29474a70-32eb-4281-8626-87819afa7267„´precedence_heuristic §cell_idÙ$29474a70-32eb-4281-8626-87819afa7267´downstream_cells_map�¨add_pair‘Ù$01372b00-ecb2-42bd-b408-13234717d969²upstream_cells_map†¤prod�¥push!�¤copy�§replace�£BPE‘Ù$d050a7ee-3aa7-4539-a236-5b6446599ded¢=>�Ù$c5be3956-5102-4d88-bfdb-9813c0555fe1„´precedence_heuristic §cell_idÙ$c5be3956-5102-4d88-bfdb-9813c0555fe1´downstream_cells_map€²upstream_cells_mapÞ°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¬Luxor.finish�¤Base�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10„´precedence_heuristic §cell_idÙ$7e27c349-ee76-46bd-b1c2-a9ce54974e10´downstream_cells_map�¥table“Ù$771d39a5-74dc-494e-929e-1164bb08b983Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3²upstream_cells_mapŒ§nothing�¦String�¡:�¨Markdown�¡!�©ismissing�®Markdown.parse�¤sort�©isnothing�¬pretty_table�¦unique�¥names�Ù$2e8b1a77-1f04-4035-8d82-4061d81ecb7a„´precedence_heuristic §cell_idÙ$2e8b1a77-1f04-4035-8d82-4061d81ecb7a´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$6bff7bca-ea1d-44c6-b8c3-040250f90654„´precedence_heuristic §cell_idÙ$6bff7bca-ea1d-44c6-b8c3-040250f90654´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1b„´precedence_heuristic §cell_idÙ$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1b´downstream_cells_map€²upstream_cells_mapƒ¢qa�§@md_str�¨getindex�Ù$6f72e8a5-819d-474c-a725-7f7318d964d7„´precedence_heuristic§cell_idÙ$6f72e8a5-819d-474c-a725-7f7318d964d7´downstream_cells_map€²upstream_cells_map�§include�Ù$b7583418-f4fb-4c63-b421-b5b9af269768„´precedence_heuristic §cell_idÙ$b7583418-f4fb-4c63-b421-b5b9af269768´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$a873f760-bfc1-489f-a58e-75e12afa54f2„´precedence_heuristic §cell_idÙ$a873f760-bfc1-489f-a58e-75e12afa54f2´downstream_cells_map�©highlight—Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03Ù$c5be3956-5102-4d88-bfdb-9813c0555fe1Ù$18c26901-85eb-45ac-89bf-b03bd255007aÙ$5f05e717-a51a-4a99-bb11-cc493217f93fÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Ù$3d8add97-59e1-444a-838b-85c2a2ac60b3²upstream_cells_map…£box�ªpolysmooth�¦sethue�¥Point�ªsetopacity�Ù$76ba4e9b-8bb0-47c4-b607-2ca711f035e6„´precedence_heuristic §cell_idÙ$76ba4e9b-8bb0-47c4-b607-2ca711f035e6´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$a5b20939-9afa-48c0-aa67-cbca6bc99804„´precedence_heuristic §cell_idÙ$a5b20939-9afa-48c0-aa67-cbca6bc99804´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0d„´precedence_heuristic §cell_idÙ$91abc03b-fef7-4f93-96fc-13f1cf654f0d´downstream_cells_map€²upstream_cells_mapÞ¤llms‘Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¥table‘Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¬Luxor.finish�¤Base�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475„´precedence_heuristic §cell_idÙ$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$beccf4e8-1b01-4cb2-b23c-bc5db604f21c„´precedence_heuristic §cell_idÙ$beccf4e8-1b01-4cb2-b23c-bc5db604f21c´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$e41d13ca-1dc1-45ae-9fa6-a83c4101120d„´precedence_heuristic §cell_idÙ$e41d13ca-1dc1-45ae-9fa6-a83c4101120d´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$5058e4eb-c53d-4468-b7ff-4f04ded96418„´precedence_heuristic §cell_idÙ$5058e4eb-c53d-4468-b7ff-4f04ded96418´downstream_cells_map€²upstream_cells_map‰°HypertextLiteral‘Ù$32621224-a782-4bf6-9570-562cf2bb7360¸HypertextLiteral.content�¤@htl�·HypertextLiteral.Result�·PlutoUI.TableOfContents�·HypertextLiteral.Bypass�§PlutoUI‘Ù$32621224-a782-4bf6-9570-562cf2bb7360²PlutoTeachingTools‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$PlutoTeachingTools.ChooseDisplayMode�Ù$5150d8f3-6e85-43f2-801a-eae5cc3e3095„´precedence_heuristic §cell_idÙ$5150d8f3-6e85-43f2-801a-eae5cc3e3095´downstream_cells_map€²upstream_cells_map…¦HAlign�£img�§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$8c27b182-0c3c-4c19-9619-df62b7dd6bf0„´precedence_heuristic §cell_idÙ$8c27b182-0c3c-4c19-9619-df62b7dd6bf0´downstream_cells_map€²upstream_cells_mapƒ¦HAlign�§@md_str�¨getindex�Ù$9a8eef1b-27c0-4d57-a389-53708ade9058„´precedence_heuristic §cell_idÙ$9a8eef1b-27c0-4d57-a389-53708ade9058´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc„´precedence_heuristic §cell_idÙ$f39305ea-f7f5-440e-ac55-c83e27f6e7fc´downstream_cells_map�¤llms”Ù$771d39a5-74dc-494e-929e-1164bb08b983Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Ù$93200f46-7c8f-4362-a445-43c57b50a2d2²upstream_cells_map�©load_llms‘Ù$579a203b-e6f7-4190-b874-18b00a5c3f77Ù$f7ca738d-5215-4e91-a2f3-a5ff10911313„´precedence_heuristic §cell_idÙ$f7ca738d-5215-4e91-a2f3-a5ff10911313´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$c7f318b9-30e6-4b79-b7da-52f70904d246„´precedence_heuristic §cell_idÙ$c7f318b9-30e6-4b79-b7da-52f70904d246´downstream_cells_map�ªsubstitute�²upstream_cells_map‹¦isless�¡-�§replace�£min�¤prod�¡+�¦String�¤Char�¢=>�¥Tuple�§minimum�Ù$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfc„´precedence_heuristic §cell_idÙ$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfc´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$8b78360a-21cb-4574-a84d-46ea4d0cedb1„´precedence_heuristic §cell_idÙ$8b78360a-21cb-4574-a84d-46ea4d0cedb1´downstream_cells_map€²upstream_cells_map�£img�Ù$95ec4140-9147-11ef-2af4-5528bad0e6f5„´precedence_heuristic §cell_idÙ$95ec4140-9147-11ef-2af4-5528bad0e6f5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$77f446ac-6030-48f2-9bea-93c427f9fcb9„´precedence_heuristic §cell_idÙ$77f446ac-6030-48f2-9bea-93c427f9fcb9´downstream_cells_map�®softmax_lookup‘Ù$1faa4ab2-6c93-47dc-b631-8be52780fe7d²upstream_cells_map‹¦values�¤keys�£sum�¥@show�£zip�©Base.repr�¬Base.println�¤Base�£dot�¡*�§softmax‘Ù$570fa160-3adb-463e-99b8-b7dd05076908Ù$d05e6f0f-0081-4fb6-91e9-ac2f58beda4a„´precedence_heuristic §cell_idÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4a´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$d54b5390-0ec0-4ff8-ab18-51726482ca46„´precedence_heuristic §cell_idÙ$d54b5390-0ec0-4ff8-ab18-51726482ca46´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f572e113-b36b-4a6b-96c7-c26f100e1ad4„´precedence_heuristic §cell_idÙ$f572e113-b36b-4a6b-96c7-c26f100e1ad4´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$c3db7eb2-356a-428f-9777-6369662d8b06„´precedence_heuristic §cell_idÙ$c3db7eb2-356a-428f-9777-6369662d8b06´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$a21fbc70-9137-4d0e-8c8c-cbdc5269778f„´precedence_heuristic §cell_idÙ$a21fbc70-9137-4d0e-8c8c-cbdc5269778f´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$9e898325-e9e2-45bd-af74-3dd86f00f7b5„´precedence_heuristic §cell_idÙ$9e898325-e9e2-45bd-af74-3dd86f00f7b5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$97463c54-7cc7-4497-a8a6-6422f5f582bd„´precedence_heuristic §cell_idÙ$97463c54-7cc7-4497-a8a6-6422f5f582bd´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012„´precedence_heuristic §cell_idÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$89305cae-098f-4644-9109-d00f1e3bc04c„´precedence_heuristic §cell_idÙ$89305cae-098f-4644-9109-d00f1e3bc04c´downstream_cells_map�ªpair_stats’Ù$728c16b7-50cd-43fe-a0d7-61d37952a6b3Ù$01372b00-ecb2-42bd-b408-13234717d969²upstream_cells_map�¦isless�§nextind�©eachindex�£get�©lastindex�¡+�¡>�¤Dict�¦String�¡<�¤Char�¥Tuple�£Int�Ù$bcf7667f-f99b-4d10-af84-5d3879f1db5d„´precedence_heuristic §cell_idÙ$bcf7667f-f99b-4d10-af84-5d3879f1db5d´downstream_cells_map€²upstream_cells_map†¤Base�­Base.getindex�©@html_str�¢qa�§@md_str�®Base.Docs.HTML�Ù$25b79953-fd7c-46c1-b760-d57c09910981„´precedence_heuristic §cell_idÙ$25b79953-fd7c-46c1-b760-d57c09910981´downstream_cells_map€²upstream_cells_map„¢qa�§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$c032b3ff-c539-4e38-81d0-39b28b3a8076„´precedence_heuristic §cell_idÙ$c032b3ff-c539-4e38-81d0-39b28b3a8076´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$45efc71d-d5f8-474e-9b89-e72fac7110fd„´precedence_heuristic §cell_idÙ$45efc71d-d5f8-474e-9b89-e72fac7110fd´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$d050a7ee-3aa7-4539-a236-5b6446599ded„´precedence_heuristic §cell_idÙ$d050a7ee-3aa7-4539-a236-5b6446599ded´downstream_cells_map�£BPE“Ù$29474a70-32eb-4281-8626-87819afa7267Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993fÙ$01372b00-ecb2-42bd-b408-13234717d969²upstream_cells_map„¦String�¥Tuple�¤Char�¤Dict�Ù$579a203b-e6f7-4190-b874-18b00a5c3f77„´precedence_heuristic §cell_idÙ$579a203b-e6f7-4190-b874-18b00a5c3f77´downstream_cells_map�©load_llms‘Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc²upstream_cells_map…¨CSV.File�©DataFrame�£CSV‘Ù$f6f7376e-9984-4289-b8ff-9d47e5358791¢=>�§rename!�Ù$570fa160-3adb-463e-99b8-b7dd05076908„´precedence_heuristic §cell_idÙ$570fa160-3adb-463e-99b8-b7dd05076908´downstream_cells_map�§softmax‘Ù$77f446ac-6030-48f2-9bea-93c427f9fcb9²upstream_cells_mapƒ¡/�£exp�£sum�Ù$9cb90e76-3bb5-41ff-bc79-c4949400d904„´precedence_heuristic §cell_idÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$75ca478c-916f-464a-9435-8208ee726d50„´precedence_heuristic §cell_idÙ$75ca478c-916f-464a-9435-8208ee726d50´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03„´precedence_heuristic §cell_idÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03´downstream_cells_map€²upstream_cells_mapÞ°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¬Luxor.finish�¤Base�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$9ff95a9a-192b-4a12-8e2e-7acd6659c066„´precedence_heuristic §cell_idÙ$9ff95a9a-192b-4a12-8e2e-7acd6659c066´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f95a6de6-5e02-4237-88ba-ec44ef3d38c3„´precedence_heuristic §cell_idÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3´downstream_cells_map€²upstream_cells_mapÞ¤llms‘Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¥table‘Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87¬Luxor.finish�¤Base�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$ed5b5702-4cca-4116-a70f-4a562178f490„´precedence_heuristic §cell_idÙ$ed5b5702-4cca-4116-a70f-4a562178f490´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$736920df-e4bb-4535-b982-e397aa0a782d„´precedence_heuristic §cell_idÙ$736920df-e4bb-4535-b982-e397aa0a782d´downstream_cells_map�¦iter_2‘Ù$c3a9a0ce-3450-4b17-8696-2ab8534b29f2²upstream_cells_map‚¦iter_1‘Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324©new_token’Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993fÙ$01372b00-ecb2-42bd-b408-13234717d969Ù$55a09acc-84da-491c-86ba-9a66f4ea52fe„´precedence_heuristic §cell_idÙ$55a09acc-84da-491c-86ba-9a66f4ea52fe´downstream_cells_map€²upstream_cells_map…¦HAlign�£img�§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$d558636d-c714-4033-ae73-5b92c3cdedf3„´precedence_heuristic §cell_idÙ$d558636d-c714-4033-ae73-5b92c3cdedf3´downstream_cells_map�¤dict“Ù$70f395b2-f8c2-44d5-b0af-702659dd7feeÙ$95504a74-d5ef-4fb7-83a0-88914c7cbc59Ù$1faa4ab2-6c93-47dc-b631-8be52780fe7d²upstream_cells_map‚¢=>�¤Dict�Ù$8d231f2c-4b0c-4c37-a746-16e98d4cafc8„´precedence_heuristic §cell_idÙ$8d231f2c-4b0c-4c37-a746-16e98d4cafc8´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993f„´precedence_heuristic §cell_idÙ$f1afaf8c-d9ad-446a-9826-9c4cda19993f´downstream_cells_map�©new_token“Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324Ù$736920df-e4bb-4535-b982-e397aa0a782dÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2²upstream_cells_mapƒ¦String�£BPE‘Ù$d050a7ee-3aa7-4539-a236-5b6446599ded¤Dict�Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324„´precedence_heuristic §cell_idÙ$f7ca3ff7-b5cf-452b-b955-7219e7397324´downstream_cells_map�¦iter_1‘Ù$736920df-e4bb-4535-b982-e397aa0a782d²upstream_cells_map�©new_token’Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993fÙ$01372b00-ecb2-42bd-b408-13234717d969Ù$5f05e717-a51a-4a99-bb11-cc493217f93f„´precedence_heuristic §cell_idÙ$5f05e717-a51a-4a99-bb11-cc493217f93f´downstream_cells_map€²upstream_cells_mapÞ°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�©highlight‘Ù$a873f760-bfc1-489f-a58e-75e12afa54f2°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87¬Luxor.finish�¤Base�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$b56e9e56-e74a-401b-b4b5-f36bb33341d5„´precedence_heuristic §cell_idÙ$b56e9e56-e74a-401b-b4b5-f36bb33341d5´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$57c2c944-0d91-489d-8ad7-f5520e71ef3e„´precedence_heuristic §cell_idÙ$57c2c944-0d91-489d-8ad7-f5520e71ef3e´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$bf563783-9784-4c74-a7b1-6d7a3ed618c5„´precedence_heuristic §cell_idÙ$bf563783-9784-4c74-a7b1-6d7a3ed618c5´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$f8330700-e964-4e19-9c55-2b11df45789e„´precedence_heuristic §cell_idÙ$f8330700-e964-4e19-9c55-2b11df45789e´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ec„´precedence_heuristic §cell_idÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ec´downstream_cells_map€²upstream_cells_mapÞ¥scale�°Luxor.background�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¬Luxor.sethue�§readpng�­Luxor.preview�­Luxor.Drawing�­Base.getindex�°Luxor.placeimage�°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¬Luxor.finish�¤Base�¢qa�©translate�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5dd„´precedence_heuristic §cell_idÙ$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5dd´downstream_cells_map€²upstream_cells_mapÞ¥scale�¦sethue�°Luxor.background�¥arrow�¬Luxor.origin�¤HTML�¦HAlign�¤html�¥@draw�¬Luxor.sethue�­Luxor.preview�­Luxor.Drawing�­Base.getindex�¥Point�°draw_transformer‘Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87¬Luxor.finish�¤Base�§@md_str�¥Luxor‘Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$1d5b1b7c-828c-4a16-b446-cff21b015d45„´precedence_heuristic §cell_idÙ$1d5b1b7c-828c-4a16-b446-cff21b015d45´downstream_cells_map�¦biblio’Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8e²upstream_cells_map�¬load_biblio!�Ù$a14e505e-2e4a-4c73-8133-7560ba58916b„´precedence_heuristic §cell_idÙ$a14e505e-2e4a-4c73-8133-7560ba58916b´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$2a7e5096-1e8d-4506-96d2-86de0a7d39aa„´precedence_heuristic §cell_idÙ$2a7e5096-1e8d-4506-96d2-86de0a7d39aa´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$6712c883-b407-47e1-a666-4de05f8f8d6e„´precedence_heuristic §cell_idÙ$6712c883-b407-47e1-a666-4de05f8f8d6e´downstream_cells_map€²upstream_cells_map…¦HAlign�£img�§@md_str�¨getindex�¢=>�Ù$af8194a1-a358-4cf7-b446-6b377cb76687„´precedence_heuristic §cell_idÙ$af8194a1-a358-4cf7-b446-6b377cb76687´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$04e9b912-6712-4290-acc4-f24bb27a1469„´precedence_heuristic §cell_idÙ$04e9b912-6712-4290-acc4-f24bb27a1469´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$d014e6aa-92f6-4ca1-be47-516565d1bb20„´precedence_heuristic §cell_idÙ$d014e6aa-92f6-4ca1-be47-516565d1bb20´downstream_cells_map€²upstream_cells_map†¦HAlign�£img�§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87¢=>�Ù$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03„´precedence_heuristic §cell_idÙ$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03´downstream_cells_map€²upstream_cells_map�£bib‘Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$e2eca085-9f99-4e3a-9db4-e7f692aedd34„´precedence_heuristic §cell_idÙ$e2eca085-9f99-4e3a-9db4-e7f692aedd34´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$95504a74-d5ef-4fb7-83a0-88914c7cbc59„´precedence_heuristic §cell_idÙ$95504a74-d5ef-4fb7-83a0-88914c7cbc59´downstream_cells_map€²upstream_cells_map‚°numerical_lookup‘Ù$b1a924f4-e2f0-445c-830f-94287a0e52f7¤dict‘Ù$d558636d-c714-4033-ae73-5b92c3cdedf3Ù$453544fc-0e3e-4e04-8c0c-192f3a038884„´precedence_heuristic §cell_idÙ$453544fc-0e3e-4e04-8c0c-192f3a038884´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�Ù$0583ee0c-3802-4e81-b179-a80a82493b43„´precedence_heuristic §cell_idÙ$0583ee0c-3802-4e81-b179-a80a82493b43´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2„´precedence_heuristic §cell_idÙ$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2´downstream_cells_map€²upstream_cells_mapƒ¢qa�§@md_str�¨getindex�Ù$01372b00-ecb2-42bd-b408-13234717d969„´precedence_heuristic §cell_idÙ$01372b00-ecb2-42bd-b408-13234717d969´downstream_cells_map�©new_token“Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324Ù$736920df-e4bb-4535-b982-e397aa0a782dÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2²upstream_cells_mapЦisless�§findmax�¡+�£min�§minimum�¨add_pair‘Ù$29474a70-32eb-4281-8626-87819afa7267ªpair_stats‘Ù$89305cae-098f-4644-9109-d00f1e3bc04c£BPE‘Ù$d050a7ee-3aa7-4539-a236-5b6446599ded¡-�¢=>�Ù$f4366cf6-2be0-42b8-96c4-120be3f5c25e„´precedence_heuristic §cell_idÙ$f4366cf6-2be0-42b8-96c4-120be3f5c25e´downstream_cells_map€²upstream_cells_mapƒ§@md_str�¨getindex�¤cite‘Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$c8923675-e73e-4621-82b9-966d8b003b97„´precedence_heuristic §cell_idÙ$c8923675-e73e-4621-82b9-966d8b003b97´downstream_cells_map€²upstream_cells_map‚§@md_str�¨getindex�®last_save_timeËAÚªí›×5«notebook_idÙ$585340e2-b27c-11f1-9d2c-d557321f93ff«in_temp_dir¨metadata€¥bonds€´cell_execution_orderÜtÙ$32621224-a782-4bf6-9570-562cf2bb7360Ù$6f72e8a5-819d-474c-a725-7f7318d964d7Ù$5058e4eb-c53d-4468-b7ff-4f04ded96418Ù$95ec4140-9147-11ef-2af4-5528bad0e6f5Ù$c09ec483-9fcf-48e7-b3c0-2508289e3cf3Ù$beccf4e8-1b01-4cb2-b23c-bc5db604f21cÙ$ccf2dc71-b883-497a-bc58-29ffaf9ea4adÙ$1bbf2152-4fdf-4ed2-9bdf-95d699824d11Ù$57c2c944-0d91-489d-8ad7-f5520e71ef3eÙ$c3db7eb2-356a-428f-9777-6369662d8b06Ù$2e8b1a77-1f04-4035-8d82-4061d81ecb7aÙ$ed5b5702-4cca-4116-a70f-4a562178f490Ù$e2eca085-9f99-4e3a-9db4-e7f692aedd34Ù$9e898325-e9e2-45bd-af74-3dd86f00f7b5Ù$bcf7667f-f99b-4d10-af84-5d3879f1db5dÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0Ù$6622f9f0-cecc-476e-9d49-7d651f433b9fÙ$6aa690e9-389f-4398-abae-b95060db4d90Ù$6712c883-b407-47e1-a666-4de05f8f8d6eÙ$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1bÙ$f8330700-e964-4e19-9c55-2b11df45789eÙ$4e10271c-49f8-4f1d-869c-5fa11275d7f6Ù$d54b5390-0ec0-4ff8-ab18-51726482ca46Ù$6800afbf-8ac6-4308-b4cc-b37da57e42c1Ù$55435b26-7fc3-4c8b-8013-6fd4fb65a08eÙ$bcbb3db2-85b3-4cb0-9309-f5c032d14da5Ù$d558636d-c714-4033-ae73-5b92c3cdedf3Ù$70f395b2-f8c2-44d5-b0af-702659dd7feeÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7Ù$95504a74-d5ef-4fb7-83a0-88914c7cbc59Ù$8d231f2c-4b0c-4c37-a746-16e98d4cafc8Ù$570fa160-3adb-463e-99b8-b7dd05076908Ù$77f446ac-6030-48f2-9bea-93c427f9fcb9Ù$1faa4ab2-6c93-47dc-b631-8be52780fe7dÙ$bf563783-9784-4c74-a7b1-6d7a3ed618c5Ù$9ff95a9a-192b-4a12-8e2e-7acd6659c066Ù$76ba4e9b-8bb0-47c4-b607-2ca711f035e6Ù$8c27b182-0c3c-4c19-9619-df62b7dd6bf0Ù$0c0c1163-0aec-4089-9acc-539b3a86d0b3Ù$b7583418-f4fb-4c63-b421-b5b9af269768Ù$6fc13413-53de-4c75-9b9e-620e0b7f8a1fÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4aÙ$a3efd921-eb14-4901-9d6c-800cc812fe02Ù$4b61363d-87c9-4755-8286-44df34e9dd6aÙ$453544fc-0e3e-4e04-8c0c-192f3a038884Ù$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bbÙ$f2cba2aa-c541-4692-a441-e65741750a15Ù$e383bb72-49a1-4df1-84c3-b95a2ffe00f5Ù$af8194a1-a358-4cf7-b446-6b377cb76687Ù$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475Ù$a5b20939-9afa-48c0-aa67-cbca6bc99804Ù$a14e505e-2e4a-4c73-8133-7560ba58916bÙ$9a8eef1b-27c0-4d57-a389-53708ade9058Ù$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2Ù$c8923675-e73e-4621-82b9-966d8b003b97Ù$04e9b912-6712-4290-acc4-f24bb27a1469Ù$8b78360a-21cb-4574-a84d-46ea4d0cedb1Ù$6bff7bca-ea1d-44c6-b8c3-040250f90654Ù$f572e113-b36b-4a6b-96c7-c26f100e1ad4Ù$f6f7376e-9984-4289-b8ff-9d47e5358791Ù$1d5b1b7c-828c-4a16-b446-cff21b015d45Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$f4366cf6-2be0-42b8-96c4-120be3f5c25eÙ$0583ee0c-3802-4e81-b179-a80a82493b43Ù$2a7e5096-1e8d-4506-96d2-86de0a7d39aaÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904Ù$55a09acc-84da-491c-86ba-9a66f4ea52feÙ$225e58ba-b78d-4a0a-be4f-ad642c879b93Ù$a21fbc70-9137-4d0e-8c8c-cbdc5269778fÙ$86101f07-67c5-4df2-911c-4013c44d6c5bÙ$5150d8f3-6e85-43f2-801a-eae5cc3e3095Ù$d014e6aa-92f6-4ca1-be47-516565d1bb20Ù$25b79953-fd7c-46c1-b760-d57c09910981Ù$c1437dcc-22cb-424f-9b8e-326172f82d86Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$61dc1905-338f-4bfd-a158-2f6bacff769eÙ$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03Ù$97463c54-7cc7-4497-a8a6-6422f5f582bdÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221Ù$76e2f97b-1c06-40cd-b134-d5155aa5587dÙ$75ca478c-916f-464a-9435-8208ee726d50Ù$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2aÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012Ù$e41d13ca-1dc1-45ae-9fa6-a83c4101120dÙ$c032b3ff-c539-4e38-81d0-39b28b3a8076Ù$b56e9e56-e74a-401b-b4b5-f36bb33341d5Ù$2a8433e3-9a3b-487b-abf3-09278ea42389Ù$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfcÙ$45efc71d-d5f8-474e-9b89-e72fac7110fdÙ$f7ca738d-5215-4e91-a2f3-a5ff10911313Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bfÙ$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5ddÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ecÙ$a873f760-bfc1-489f-a58e-75e12afa54f2Ù$b9caae1a-38aa-4d01-9cda-3d6782fb0e03Ù$c5be3956-5102-4d88-bfdb-9813c0555fe1Ù$18c26901-85eb-45ac-89bf-b03bd255007aÙ$5f05e717-a51a-4a99-bb11-cc493217f93fÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3Ù$d050a7ee-3aa7-4539-a236-5b6446599dedÙ$29474a70-32eb-4281-8626-87819afa7267Ù$89305cae-098f-4644-9109-d00f1e3bc04cÙ$728c16b7-50cd-43fe-a0d7-61d37952a6b3Ù$c7f318b9-30e6-4b79-b7da-52f70904d246Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993fÙ$01372b00-ecb2-42bd-b408-13234717d969Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324Ù$736920df-e4bb-4535-b982-e397aa0a782dÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2Ù$579a203b-e6f7-4190-b874-18b00a5c3f77Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fcÙ$93200f46-7c8f-4362-a445-43c57b50a2d2Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10Ù$771d39a5-74dc-494e-929e-1164bb08b983Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3®process_status¥readyªcell_orderÜtÙ$5058e4eb-c53d-4468-b7ff-4f04ded96418Ù$95ec4140-9147-11ef-2af4-5528bad0e6f5Ù$f4366cf6-2be0-42b8-96c4-120be3f5c25eÙ$61dc1905-338f-4bfd-a158-2f6bacff769eÙ$c09ec483-9fcf-48e7-b3c0-2508289e3cf3Ù$beccf4e8-1b01-4cb2-b23c-bc5db604f21cÙ$ccf2dc71-b883-497a-bc58-29ffaf9ea4adÙ$1bbf2152-4fdf-4ed2-9bdf-95d699824d11Ù$57c2c944-0d91-489d-8ad7-f5520e71ef3eÙ$0583ee0c-3802-4e81-b179-a80a82493b43Ù$728c16b7-50cd-43fe-a0d7-61d37952a6b3Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324Ù$736920df-e4bb-4535-b982-e397aa0a782dÙ$c3db7eb2-356a-428f-9777-6369662d8b06Ù$c3a9a0ce-3450-4b17-8696-2ab8534b29f2Ù$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03Ù$2e8b1a77-1f04-4035-8d82-4061d81ecb7aÙ$ed5b5702-4cca-4116-a70f-4a562178f490Ù$771d39a5-74dc-494e-929e-1164bb08b983Ù$97463c54-7cc7-4497-a8a6-6422f5f582bdÙ$e2eca085-9f99-4e3a-9db4-e7f692aedd34Ù$9e898325-e9e2-45bd-af74-3dd86f00f7b5Ù$bcf7667f-f99b-4d10-af84-5d3879f1db5dÙ$2a7e5096-1e8d-4506-96d2-86de0a7d39aaÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0Ù$eb18303f-3dfb-4b87-90f2-f6dc542d7221Ù$6622f9f0-cecc-476e-9d49-7d651f433b9fÙ$6aa690e9-389f-4398-abae-b95060db4d90Ù$9cb90e76-3bb5-41ff-bc79-c4949400d904Ù$6712c883-b407-47e1-a666-4de05f8f8d6eÙ$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1bÙ$76e2f97b-1c06-40cd-b134-d5155aa5587dÙ$f8330700-e964-4e19-9c55-2b11df45789eÙ$91abc03b-fef7-4f93-96fc-13f1cf654f0dÙ$75ca478c-916f-464a-9435-8208ee726d50Ù$4e10271c-49f8-4f1d-869c-5fa11275d7f6Ù$55a09acc-84da-491c-86ba-9a66f4ea52feÙ$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2aÙ$d54b5390-0ec0-4ff8-ab18-51726482ca46Ù$225e58ba-b78d-4a0a-be4f-ad642c879b93Ù$a21fbc70-9137-4d0e-8c8c-cbdc5269778fÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012Ù$6800afbf-8ac6-4308-b4cc-b37da57e42c1Ù$55435b26-7fc3-4c8b-8013-6fd4fb65a08eÙ$bcbb3db2-85b3-4cb0-9309-f5c032d14da5Ù$d558636d-c714-4033-ae73-5b92c3cdedf3Ù$70f395b2-f8c2-44d5-b0af-702659dd7feeÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7Ù$95504a74-d5ef-4fb7-83a0-88914c7cbc59Ù$8d231f2c-4b0c-4c37-a746-16e98d4cafc8Ù$86101f07-67c5-4df2-911c-4013c44d6c5bÙ$570fa160-3adb-463e-99b8-b7dd05076908Ù$77f446ac-6030-48f2-9bea-93c427f9fcb9Ù$1faa4ab2-6c93-47dc-b631-8be52780fe7dÙ$e41d13ca-1dc1-45ae-9fa6-a83c4101120dÙ$bf563783-9784-4c74-a7b1-6d7a3ed618c5Ù$9ff95a9a-192b-4a12-8e2e-7acd6659c066Ù$5150d8f3-6e85-43f2-801a-eae5cc3e3095Ù$c032b3ff-c539-4e38-81d0-39b28b3a8076Ù$76ba4e9b-8bb0-47c4-b607-2ca711f035e6Ù$8c27b182-0c3c-4c19-9619-df62b7dd6bf0Ù$0c0c1163-0aec-4089-9acc-539b3a86d0b3Ù$b7583418-f4fb-4c63-b421-b5b9af269768Ù$d014e6aa-92f6-4ca1-be47-516565d1bb20Ù$6fc13413-53de-4c75-9b9e-620e0b7f8a1fÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4aÙ$a3efd921-eb14-4901-9d6c-800cc812fe02Ù$b9caae1a-38aa-4d01-9cda-3d6782fb0e03Ù$4b61363d-87c9-4755-8286-44df34e9dd6aÙ$453544fc-0e3e-4e04-8c0c-192f3a038884Ù$c5be3956-5102-4d88-bfdb-9813c0555fe1Ù$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bbÙ$18c26901-85eb-45ac-89bf-b03bd255007aÙ$b56e9e56-e74a-401b-b4b5-f36bb33341d5Ù$f2cba2aa-c541-4692-a441-e65741750a15Ù$5f05e717-a51a-4a99-bb11-cc493217f93fÙ$2a8433e3-9a3b-487b-abf3-09278ea42389Ù$e383bb72-49a1-4df1-84c3-b95a2ffe00f5Ù$f95a6de6-5e02-4237-88ba-ec44ef3d38c3Ù$af8194a1-a358-4cf7-b446-6b377cb76687Ù$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475Ù$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5ddÙ$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfcÙ$a5b20939-9afa-48c0-aa67-cbca6bc99804Ù$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ecÙ$25b79953-fd7c-46c1-b760-d57c09910981Ù$45efc71d-d5f8-474e-9b89-e72fac7110fdÙ$a14e505e-2e4a-4c73-8133-7560ba58916bÙ$9a8eef1b-27c0-4d57-a389-53708ade9058Ù$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2Ù$c8923675-e73e-4621-82b9-966d8b003b97Ù$04e9b912-6712-4290-acc4-f24bb27a1469Ù$c1437dcc-22cb-424f-9b8e-326172f82d86Ù$8b78360a-21cb-4574-a84d-46ea4d0cedb1Ù$f7ca738d-5215-4e91-a2f3-a5ff10911313Ù$6bff7bca-ea1d-44c6-b8c3-040250f90654Ù$3d8add97-59e1-444a-838b-85c2a2ac60b3Ù$f572e113-b36b-4a6b-96c7-c26f100e1ad4Ù$32621224-a782-4bf6-9570-562cf2bb7360Ù$f6f7376e-9984-4289-b8ff-9d47e5358791Ù$6f72e8a5-819d-474c-a725-7f7318d964d7Ù$1d5b1b7c-828c-4a16-b446-cff21b015d45Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87Ù$4f1d5112-dbac-4eb6-8518-0dc4193c3f8eÙ$85a10748-8d19-44a8-a1c5-0d13b093f1bfÙ$a873f760-bfc1-489f-a58e-75e12afa54f2Ù$d050a7ee-3aa7-4539-a236-5b6446599dedÙ$29474a70-32eb-4281-8626-87819afa7267Ù$89305cae-098f-4644-9109-d00f1e3bc04cÙ$c7f318b9-30e6-4b79-b7da-52f70904d246Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993fÙ$01372b00-ecb2-42bd-b408-13234717d969Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fcÙ$579a203b-e6f7-4190-b874-18b00a5c3f77Ù$93200f46-7c8f-4362-a445-43c57b50a2d2Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10«cell_inputsÞtÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$3d8add97-59e1-444a-838b-85c2a2ac60b3«code_foldedäcodeÚ8HAlign( md""" *Cross-Attention* between * values and keys ``E(CX + P)`` where ``E`` is the encoder, and ``X`` is the matrix of input tokens * query ``Q`` depending on past output ``Y`` and number of layers already applied ```math \text{MultiHead}(E(CX + P), E(CX + P), Q) ``` The embedding vectors ``CX`` take then different projections for value, key, query and also for different heads! ```math \begin{multline} \text{head}_j = \text{Attention}(W_j^VV, W_j^KK, W_j^QQ)\\ \text{where } V = K = E(CX + P) \end{multline} ``` """, HTML(html(@draw begin draw_transformer(false) highlight(31, -97, 205, 5) #sethue("red") #setopacity(1) fontsize(32) text("CX + P", Point(-60, 252), halign = :center) text("CY + P", Point(65, 252), halign = :center) text(L"E(CX + P)", Point(-70, -160), halign = :center) end 300 400)), )Ù$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bb„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$92e01e21-ca77-43fc-9bf8-0c5a7aaed1bb«code_foldedäcodeºmd"## Residual connection"Ù$6622f9f0-cecc-476e-9d49-7d651f433b9f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6622f9f0-cecc-476e-9d49-7d651f433b9f«code_foldedäcodeÙ!md"# Pre-transformers approaches"Ù$70f395b2-f8c2-44d5-b0af-702659dd7fee„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$70f395b2-f8c2-44d5-b0af-702659dd7fee«code_folded¤code¬dict[[1, 0]]Ù$771d39a5-74dc-494e-929e-1164bb08b983„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$771d39a5-74dc-494e-929e-1164bb08b983«code_foldedäcodeÙ™table(llms, mandatory_columns = ["``n_\\text{ctx}``"], included_columns = [ "Name", "Ref", "``n_\\text{voc}``", "``n_\\text{ctx}``", "Tokenizer", ])Ù$85a10748-8d19-44a8-a1c5-0d13b093f1bf„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$85a10748-8d19-44a8-a1c5-0d13b093f1bf«code_folded¤codeÚ‰function draw_transformer(decoder_only = true) scale(0.4, 0.4) Luxor.placeimage(readpng("images/transformer.png"), centered = true) if decoder_only sethue("red") setopacity(0.4) box(Point(-350, -160), Point(320, 20), :fill) box(Point(-350, 20), Point(0, 460), :fill) translate(Point(-170, -190)) setopacity(1) fontsize(32) text("Not used for now", halign = :center) end endÙ$4f1d5112-dbac-4eb6-8518-0dc4193c3f8e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4f1d5112-dbac-4eb6-8518-0dc4193c3f8e«code_folded¤codeÙ'bib(args...) = bibrefs(biblio, args...)Ù$93200f46-7c8f-4362-a445-43c57b50a2d2„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$93200f46-7c8f-4362-a445-43c57b50a2d2«code_folded¤code«names(llms)Ù$2a8433e3-9a3b-487b-abf3-09278ea42389„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$2a8433e3-9a3b-487b-abf3-09278ea42389«code_foldedäcodeÙ?bib(["ioffe2015Batch", "ba2016Layer", "vaswani2017Attentiona"])Ù$61dc1905-338f-4bfd-a158-2f6bacff769e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$61dc1905-338f-4bfd-a158-2f6bacff769e«code_foldedäcodeÙ4bib(["goodfellow2016Deep", "vaswani2017Attentiona"])Ù$eb18303f-3dfb-4b87-90f2-f6dc542d7221„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$eb18303f-3dfb-4b87-90f2-f6dc542d7221«code_foldedäcodeÙ0bib(["press2017Using", "vaswani2017Attentiona"])Ù$76e2f97b-1c06-40cd-b134-d5155aa5587d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$76e2f97b-1c06-40cd-b134-d5155aa5587d«code_foldedäcode¹bib(["bengio2000Neural"])Ù$f6f7376e-9984-4289-b8ff-9d47e5358791„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f6f7376e-9984-4289-b8ff-9d47e5358791«code_folded¤codeÙ(import DocumenterCitations, CSV, LoggingÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b1a924f4-e2f0-445c-830f-94287a0e52f7«code_folded¤codeÙ„function numerical_lookup(dict, query) _, i = findmax([dot(query, key) for key in keys(dict)]) return collect(values(dict))[i] endÙ$f2cba2aa-c541-4692-a441-e65741750a15„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f2cba2aa-c541-4692-a441-e65741750a15«code_foldedäcodeºmd"## Layer normalization"Ù$6fc13413-53de-4c75-9b9e-620e0b7f8a1f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6fc13413-53de-4c75-9b9e-620e0b7f8a1f«code_foldedäcodeÙoqa(md"Is ``W^O`` needed if ``h = 1`` ?", md"No, if ``h = 1``, we can merge ``W^OW_1^V`` into a new ``W_1^V``.")Ù$ccf2dc71-b883-497a-bc58-29ffaf9ea4ad„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$ccf2dc71-b883-497a-bc58-29ffaf9ea4ad«code_foldedäcodeÙ0md"## Text to vectors : step 1 → tokenization"Ù$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$5b4a67a9-e33e-4dc6-b9f0-fd9a2cca6f2a«code_foldedäcodeÙ3bib(["mikolov2010Recurrent", "goodfellow2016Deep"])Ù$86101f07-67c5-4df2-911c-4013c44d6c5b„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$86101f07-67c5-4df2-911c-4013c44d6c5b«code_foldedäcodeÚ0md""" Attention head provides a differentiable numerical dictionary $(cite("bahdanau2016Neural")) ```math \begin{align} \alpha & = \text{softmax}(\langle q, k_1 \rangle, \ldots, \langle q, k_{n_\text{ctx}}\rangle) & \text{Attention}(q, k, v) & = \sum_{i=1}^{n_\text{ctx}} \alpha_i v_i \end{align} ``` """Ù$a3efd921-eb14-4901-9d6c-800cc812fe02„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a3efd921-eb14-4901-9d6c-800cc812fe02«code_foldedäcodeµmd"## Self-Attention"Ù$6800afbf-8ac6-4308-b4cc-b37da57e42c1„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6800afbf-8ac6-4308-b4cc-b37da57e42c1«code_foldedäcode¿md"# Attention is all you need"Ù$18c26901-85eb-45ac-89bf-b03bd255007a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$18c26901-85eb-45ac-89bf-b03bd255007a«code_foldedäcodeÚHAlign( md""" Residual connection $(cite("he2015Deep")) $(img("resnet")) """, HTML(html(@draw begin draw_transformer() highlight(210, -85, 290, -45) highlight(360, -75, 440, 50) highlight(210, 210, 290, 250) highlight(360, 220, 440, 420) end 300 400)) )Ù$bcbb3db2-85b3-4cb0-9309-f5c032d14da5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$bcbb3db2-85b3-4cb0-9309-f5c032d14da5«code_foldedäcodeÙ°md" What would a numerical dictionary look like ? Consider keys ``k_i \in \mathbb{R}^{d_k}`` and values ``v_i \in \mathbb{R}^{d_v}``. Given a query ``q \in \mathbb{R}^{d_k}``,"Ù$4e10271c-49f8-4f1d-869c-5fa11275d7f6„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4e10271c-49f8-4f1d-869c-5fa11275d7f6«code_foldedäcodeÙ&md"## Recurrent neural networks (RNN)"Ù$c1437dcc-22cb-424f-9b8e-326172f82d86„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c1437dcc-22cb-424f-9b8e-326172f82d86«code_foldedäcodeÙ÷md""" * LSTM **encoder** → **context** → LSTM **decoder** $(cite("sutskever2014Sequence")). See $(cite("sutskever2014Sequence", "Figure 1")) below. * Issue with *encoder bottleneck*. All information has to be summarized in the **context**. """Ù$c09ec483-9fcf-48e7-b3c0-2508289e3cf3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c09ec483-9fcf-48e7-b3c0-2508289e3cf3«code_foldedäcode¼md"## Autoregressive Models"Ù$1bbf2152-4fdf-4ed2-9bdf-95d699824d11„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$1bbf2152-4fdf-4ed2-9bdf-95d699824d11«code_foldedäcodeÚjmd""" #### Why not encode each letter ? * **Idea** : Turn each letter into its one-hot encoding in ``\mathbb{R}^{26}``. * **Issue** : The "past text" only has ``n_\text{ctx}`` characters so ``n_\text{ctx}`` must be **large** but transformers have a complexity **quadratic** in ``n_\text{ctx}``! * **Practical details** : Text is encoded with [UTF-8](https://en.wikipedia.org/wiki/UTF-8) so each character is encoded into 1 to 4 bytes. We encode each byte to a vector in ``\mathbb{R}^{256}`` but care must be taken not to generate invalid UTF-8. #### Why not encode each word ? * **Idea** : Turn each word into its one-hot encoding in ``\mathbb{R}^n``. The value of ``n`` is the number of words. Depending on the language ([source](https://en.wikipedia.org/wiki/List_of_dictionaries_by_number_of_words)): | Language | French | English | Dutch | German | |----------|---------|---------|---------|---------| | ``n`` | 408,078 | 350,000 | 350,000 | 200,000 | * **Issue** : The value of ``n`` is **too large**. We cannot trust the words of languages to be a tokenization that optimally compresses text for our dataset. """Ù$728c16b7-50cd-43fe-a0d7-61d37952a6b3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$728c16b7-50cd-43fe-a0d7-61d37952a6b3«code_folded¤code¹pair_stats("aaabdaaabac")Ù$1faa4ab2-6c93-47dc-b631-8be52780fe7d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$1faa4ab2-6c93-47dc-b631-8be52780fe7d«code_folded¤codeÙ softmax_lookup(dict, [0.8, 0.2])Ù$c3a9a0ce-3450-4b17-8696-2ab8534b29f2„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c3a9a0ce-3450-4b17-8696-2ab8534b29f2«code_folded¤codeºiter_3 = new_token(iter_2)Ù$e383bb72-49a1-4df1-84c3-b95a2ffe00f5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$e383bb72-49a1-4df1-84c3-b95a2ffe00f5«code_foldedäcode»md"## Feed-Forward network"Ù$6aa690e9-389f-4398-abae-b95060db4d90„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6aa690e9-389f-4398-abae-b95060db4d90«code_foldedäcode·md"## Shared embedding"Ù$4b61363d-87c9-4755-8286-44df34e9dd6a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4b61363d-87c9-4755-8286-44df34e9dd6a«code_foldedäcodeÙíqa( html"Is the order between the tokens taken into account by the model ?", md""" No. Since the same matrices ``W_j^V``, ``W_j^K`` and ``W_j^Q`` multiply the different position. The **position** information is completely **lost**! """ )Ù$94ae440d-0644-49db-9461-f1a1ff1d7f87„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$94ae440d-0644-49db-9461-f1a1ff1d7f87«code_folded¤codeÙ(cite(args...) = bibcite(biblio, args...)Ù$55435b26-7fc3-4c8b-8013-6fd4fb65a08e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$55435b26-7fc3-4c8b-8013-6fd4fb65a08e«code_foldedäcode»md"## Numerical dictionary"Ù$225e58ba-b78d-4a0a-be4f-ad642c879b93„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$225e58ba-b78d-4a0a-be4f-ad642c879b93«code_foldedäcodeÚšmd""" It's difficult to model long-term dependencies as their gradient either vanish or explodes exponentially (think of the power method) $(cite("goodfellow2016Deep", "Section 10.7")) *Gated* extensions attempting to solve this issue $(cite("goodfellow2016Deep", "Section 10.10")): * Long short-term memory (LSTM) $(cite("graves2014Generating")) * Gated recurrent unit (GRU) $(cite("cho2014Properties")) """Ù$32621224-a782-4bf6-9570-562cf2bb7360„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$32621224-a782-4bf6-9570-562cf2bb7360«code_folded¤codeÙ¥using PlutoUI, DataFrames, PrettyTables, LinearAlgebra, Luxor, LaTeXStrings, MathTeXEngine, PlutoUI, PlutoUI.ExperimentalLayout, HypertextLiteral, PlutoTeachingToolsÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$e86a57ae-3945-4cbf-b2e4-a96f4b5295e0«code_foldedäcodeÚ?qa(md"Why don't we use ``D = C^{-1}`` ?", md""" The ``C`` matrix is not invertible as it is not square. Given a vector ``u``, the vector ``v = Du`` could be computed to be the mininum norm solution of ``u = Cv``, this is what is done by `v = D \ u`. Computing `D \ u` would be more computational work than `D' \ u` but that's not the only reason `D'` is used. If ``v`` is chosen so that ``u = Cv``, it means that ``v`` is a linear combination allowing to reconstruct ``u`` using the columns of ``C``. From that perspective, ``v_i`` could be nonzero even though the direction of the vector ``u`` us far from the direction of the column ``C_{:i}``. That does not correspond to what we want to do here. Here, we want a probability vector ``p`` with high probability ``p_i`` when the direction of the column ``C_{:i}`` is close to the direction of ``u``. In the vector ``v = C^\top u``, ``v_i = \langle C_{:i}, u \rangle`` is the scalar product between the ``i``th column of ``C`` and ``u`` hence it is a good measure of how close the direction are to each other. Of course this assumes that the column of ``C`` have the same norm, but the hope is for the model to figure in training that the columns of ``C`` should have unit norm. We can then simply apply softmax to turn these scalar products into probabilities ``p = \text{softmax}(v)``. """)Ù$0c0c1163-0aec-4089-9acc-539b3a86d0b3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$0c0c1163-0aec-4089-9acc-539b3a86d0b3«code_foldedäcodeÙbmd""" ```math \text{Masked-Attention}(V, K, Q)\ = V\text{softmax}(M + K^\top Q/\sqrt{d_k}) ``` """Ù$29474a70-32eb-4281-8626-87819afa7267„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$29474a70-32eb-4281-8626-87819afa7267«code_folded¤codeÙ™function add_pair(bpe::BPE, subs) pairs = copy(bpe.pairs) push!(pairs, subs) return BPE(replace(bpe.text, prod(subs.first) => subs.second), pairs) endÙ$c5be3956-5102-4d88-bfdb-9813c0555fe1„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c5be3956-5102-4d88-bfdb-9813c0555fe1«code_foldedäcodeÚ�HAlign( md""" Cannot sum ``Cx_i + e_i`` with one-hot encoding ``e_i \in \mathbb{R}^{n_\text{ctx}}`` as the dimension of ``Cx_i`` is ``\mathbb{R}^{d_\text{emb}}``. So we also add a positional embedding ``P`` : ``Cx_i + Pe_i = Cx_i + p_i``. With Self-Attention: ```math \text{Self-MultiHead}(CX + P, CX + P, CX + P) ``` """, HTML(html(@draw begin draw_transformer() highlight(310, 410, 530, 510) end 300 400)) )Ù$7e27c349-ee76-46bd-b1c2-a9ce54974e10„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$7e27c349-ee76-46bd-b1c2-a9ce54974e10«code_folded¤codeÚ«function table(df; mandatory_columns = String[], included_columns = nothing) for col in mandatory_columns df = df[(!ismissing).(df[!, col]), :] end if !isnothing(included_columns) df = unique(df[!, included_columns]) end Markdown.parse(pretty_table( String, sort(df), backend = :markdown, column_labels = names(df), allow_markdown_in_cells = true, formatters = [(v, _, _) -> ismissing(v) ? "" : v], )) endÙ$2e8b1a77-1f04-4035-8d82-4061d81ecb7a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$2e8b1a77-1f04-4035-8d82-4061d81ecb7a«code_foldedäcodeÙ)md"## Increasing length of \"past text\""Ù$6bff7bca-ea1d-44c6-b8c3-040250f90654„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6bff7bca-ea1d-44c6-b8c3-040250f90654«code_foldedäcode¶md"## Cross-Attention"Ù$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1b„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c4bebd0d-eacf-4db4-b5b3-4dca50ab9e1b«code_foldedäcodeÚ£qa(md"What are the number of columns of ``W_1`` and number of rows of ``W_2`` now ?", md""" The matrix ``W_1`` has ``n_\text{ctx}d_\text{emb}`` columns. Assuming ``d_\text{emb} \ll n_\text{voc}`` and ``n_\text{ctx} \gg 1``, this is much smaller than the number ``n_\text{ctx}n_\text{voc}`` that we would have without the embedding. The number of rows of ``W_2`` is ``n_\text{voc}``, unaffected by the embedding. """)Ù$6f72e8a5-819d-474c-a725-7f7318d964d7„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6f72e8a5-819d-474c-a725-7f7318d964d7«code_folded¤code³include("utils.jl")Ù$b7583418-f4fb-4c63-b421-b5b9af269768„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b7583418-f4fb-4c63-b421-b5b9af269768«code_foldedäcode»md"## Multi-Head Attention"Ù$a873f760-bfc1-489f-a58e-75e12afa54f2„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a873f760-bfc1-489f-a58e-75e12afa54f2«code_folded¤codeÚfunction highlight(a, b, c, d) sethue("green") setopacity(0.4) #box(Point(a, b), Point(c, d), :fill) polysmooth(box(Point(a, b), Point(c, d), vertices=true), 10, action = :fill) setopacity(1) polysmooth(box(Point(a, b), Point(c, d), vertices=true), 10, action = :stroke) endÙ$76ba4e9b-8bb0-47c4-b607-2ca711f035e6„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$76ba4e9b-8bb0-47c4-b607-2ca711f035e6«code_foldedäcode·md"## Masked Attention"Ù$a5b20939-9afa-48c0-aa67-cbca6bc99804„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a5b20939-9afa-48c0-aa67-cbca6bc99804«code_foldedäcode³md"## Cost of LLMs"Ù$91abc03b-fef7-4f93-96fc-13f1cf654f0d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$91abc03b-fef7-4f93-96fc-13f1cf654f0d«code_foldedäcodeÚtHAlign( md""" See the table below for the size of embeddings of large language models: $(table(llms, mandatory_columns = ["``d_\\text{emb}``"], included_columns = [ "Name", "Num params", "Ref", "``n_\\text{voc}``", "``d_\\text{emb}``", ])) """, HTML(html(@draw begin draw_transformer() highlight(200, -160, 375, -110) highlight(200, 490, 375, 565) end 300 400)); )Ù$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$79e6c4a8-cc1e-40cc-bb09-e9a7a9a8e475«code_foldedäcode½md"## Transformer variations"Ù$beccf4e8-1b01-4cb2-b23c-bc5db604f21c„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$beccf4e8-1b01-4cb2-b23c-bc5db604f21c«code_foldedäcodeÚmd""" Given a sequence of ``n_\text{ctx}`` past vectors ``x_{-1}, \ldots, x_{-n_\text{ctx}} \in \mathbb{R}^{n}``, "predict" the next ones. Key idea : *receding horizon*: ```math \begin{align} & p(x_0, x_1 | x_{-1}, \ldots, x_{-n_\text{ctx}})\\ & = p(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})p(x_1 | x_0, x_{-1}, \ldots, x_{-n_\text{ctx}+1}, \textcolor{red}{x_{-n_\text{ctx}}})\\ & \approx p(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}) p(x_{1} | x_0, x_{-1}, \ldots, x_{-n_\text{ctx}+1}) \end{align} ``` * **Model** : Probability of next vector ``\hat{p}(x_0 | X)`` where ``X`` concatenates ``x_{-1}, \ldots, x_{-n_\text{ctx}}``. * **Loss** : Cross-entropy : ``\mathcal{L}_{\hat{p}}(X) \triangleq H(p, \hat{p}) = -\textbf{E}_p[\log(\hat{p})] = -\sum_{x_0} p(x_0 | X) \log(\hat{p}(x_0 | X))`` * Particular case for ``p(x_0 | X) = \delta_y`` : ``\mathcal{L}_{\hat{p}}(X) = -\log(\hat{p}(y | X))`` #### What about Language Models ? Given "past text", predict the "following text". How to turn text into vectors of ``\mathbb{R}^n`` ? """Ù$e41d13ca-1dc1-45ae-9fa6-a83c4101120d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$e41d13ca-1dc1-45ae-9fa6-a83c4101120d«code_foldedäcode»bib(["bahdanau2016Neural"])Ù$5058e4eb-c53d-4468-b7ff-4f04ded96418„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$5058e4eb-c53d-4468-b7ff-4f04ded96418«code_foldedäcodeÙÞ@htl("""

Transformers in Large Language Models (LLMs)

Benoît Legat

$(PlutoTeachingTools.ChooseDisplayMode()) $(PlutoUI.TableOfContents(depth=1)) """)Ù$5150d8f3-6e85-43f2-801a-eae5cc3e3095„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$5150d8f3-6e85-43f2-801a-eae5cc3e3095«code_foldedäcodeÚ§HAlign( md""" `softmax` is then applied to each **column**: ```math \text{softmax}(K^\top Q/\sqrt{d_k}) ``` Division by ``\sqrt{d_k}`` scales the input of softmax to preferable regions $(cite("vaswani2017Attentiona", "Secton 3.2.1")). Illustrated on the right from $(cite("bahdanau2016Neural", "Figure 3(a)")). ```math \text{Attention}(V, K, Q) = V\text{softmax}(K^\top Q/\sqrt{d_k}) ``` """, img("attention_matrix"), )Ù$8c27b182-0c3c-4c19-9619-df62b7dd6bf0„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$8c27b182-0c3c-4c19-9619-df62b7dd6bf0«code_foldedäcodeÚMHAlign( md""" 💡 **Key idea** In the model for ``\hat{p}(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})``, incorporate sub-models ```math \begin{align} \bar{p}(&x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})\\ \bar{p}(&x_{-1} | x_{-2}, \ldots, x_{-n_\text{ctx}})\\ & \quad\qquad\vdots\\ \bar{p}(&x_{-n_\text{ctx}+1} | x_{-n_\text{ctx}}). \end{align} ``` """, md""" Mask prevents ``\hat{p}`` to look at inputs in the future: ```math M = \begin{bmatrix} 0 & 0 & \cdots & 0\\ -\infty & 0 & \ddots & \vdots\\ \vdots & \ddots & \ddots & 0\\ -\infty & \cdots & -\infty & 0 \end{bmatrix} ``` """, )Ù$9a8eef1b-27c0-4d57-a389-53708ade9058„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$9a8eef1b-27c0-4d57-a389-53708ade9058«code_foldedäcodeÚmd""" Let ``\hat{Y}_{i}`` be the intermediate output of ``i \in \{1, \ldots, N\}``. The columns of the matrix ``\text{softmax}(C^\top \hat{Y}_i)`` (column-wise softmax) can be thought as intermediate probabilities that we denote ``\hat{p}_i``: ```math (\hat{p}_i(x_{-n_\text{ctx}+1} | x_{-n_\text{ctx}}), \ldots, \hat{p}_i(x_{-1} | x_{-2}, \ldots, x_{-n_\text{ctx}}), \hat{p}_i(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})) ``` and we predict the next token using ``\hat{p}_N(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}})``. """Ù$f39305ea-f7f5-440e-ac55-c83e27f6e7fc„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f39305ea-f7f5-440e-ac55-c83e27f6e7fc«code_folded¤code²llms = load_llms()Ù$f7ca738d-5215-4e91-a2f3-a5ff10911313„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f7ca738d-5215-4e91-a2f3-a5ff10911313«code_foldedäcode¼bib("sutskever2014Sequence")Ù$c7f318b9-30e6-4b79-b7da-52f70904d246„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c7f318b9-30e6-4b79-b7da-52f70904d246«code_folded¤codeÙ function substitute(text::String, pair::Tuple{Char,Char}) new_char = min('Z' + 1, minimum(text)) - 1 return replace(text, prod(pair.first) => pair.second) endÙ$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfc„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4dd7083a-e730-4f4b-bde8-fc1a5b08ebfc«code_foldedäcodeÙ@bib(["he2016Identity", "radford2019Language", "su2023RoFormer"])Ù$8b78360a-21cb-4574-a84d-46ea4d0cedb1„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$8b78360a-21cb-4574-a84d-46ea4d0cedb1«code_foldedäcode¼img("sutskever2014Sequence")Ù$95ec4140-9147-11ef-2af4-5528bad0e6f5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$95ec4140-9147-11ef-2af4-5528bad0e6f5«code_foldedäcodeÙ"md"# Large Language Models (LLMs)"Ù$77f446ac-6030-48f2-9bea-93c427f9fcb9„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$77f446ac-6030-48f2-9bea-93c427f9fcb9«code_folded¤codeÙ»function softmax_lookup(dict, query) ks = keys(dict) α = softmax([dot(query, key) for key in keys(dict)]) @show α return sum(α * value for (α, value) in zip(α, values(dict))) endÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4a„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d05e6f0f-0081-4fb6-91e9-ac2f58beda4a«code_foldedäcode¾md"# Decoder-only transformer"Ù$d54b5390-0ec0-4ff8-ab18-51726482ca46„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d54b5390-0ec0-4ff8-ab18-51726482ca46«code_foldedäcode¹md"## Extensions of RNNs"Ù$f572e113-b36b-4a6b-96c7-c26f100e1ad4„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f572e113-b36b-4a6b-96c7-c26f100e1ad4«code_foldedäcode¬md"## Utils"Ù$c3db7eb2-356a-428f-9777-6369662d8b06„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c3db7eb2-356a-428f-9777-6369662d8b06«code_foldedäcodeÙOmd""" Note that the new tokens can also be part of the most frequence pair! """Ù$a21fbc70-9137-4d0e-8c8c-cbdc5269778f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a21fbc70-9137-4d0e-8c8c-cbdc5269778f«code_foldedäcodeÙÙmd""" Recently, Mamba suggests a solution to the complexity issue $(cite("gu2024Mamba")). As it scales better with ``n_\text{ctx}``, it is even suggested to get rid of the tokenizer : $(cite("wang2024MambaByte")). """Ù$9e898325-e9e2-45bd-af74-3dd86f00f7b5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$9e898325-e9e2-45bd-af74-3dd86f00f7b5«code_foldedäcodeÚmd""" Consider one-hot encoding with vocabulary size ``n_\text{voc}`` and a *bigram model* ```math \hat{p}(x_0 | x_{-1}) = \text{softmax}(W_d \tanh(\cdots\tanh(W_1 x_{-1})\cdots) ``` The matrix ``W_d`` has ``n_\text{voc}`` rows and ``W_1`` has ``n_\text{voc}`` columns → issue if ``n_\text{voc}`` is large **Embedding** : Use vectors ``c_1, \ldots, c_{n_\text{voc}} \in \mathbb{R}^{d_\text{emb}}`` with *embedding size* (aka *hidden size*) ``d_\text{emb} \ll n_\text{voc}``. Equivalently, we still use one-hot encoding but we add an encoder ``C \in \mathbb{R}^{d_\text{emb} \times n_\text{voc}}`` and decoder ``D \in \mathbb{R}^{n_\text{voc} \times d_\text{emb}}`` ```math \hat{p}(x_0 | x_{-1}) = \text{softmax}(D W_d \tanh(\cdots\tanh(W_1 C x_{-1})\cdots) ``` """Ù$97463c54-7cc7-4497-a8a6-6422f5f582bd„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$97463c54-7cc7-4497-a8a6-6422f5f582bd«code_foldedäcodeÙµbib(["team2024Gemini", "team2024Geminia", "team2024Gemma", "team2024Gemmaa", "sennrich2016Neural", "radford2019Language", "brown2020Language", "touvron2023Llama", "yu2023MEGABYTE"])Ù$8eafcfed-9771-4d99-b0c5-bd75a6dab012„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$8eafcfed-9771-4d99-b0c5-bd75a6dab012«code_foldedäcodeÙlbib(["cho2014Properties", "graves2014Generating", "goodfellow2016Deep", "gu2024Mamba", "wang2024MambaByte"])Ù$89305cae-098f-4644-9109-d00f1e3bc04c„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$89305cae-098f-4644-9109-d00f1e3bc04c«code_folded¤codeÙÿfunction pair_stats(text::String) stats = Dict{Tuple{Char,Char},Int}() for i in eachindex(text) j = nextind(text, i) if j > lastindex(text) break end a = text[i] b = text[j] stats[(a, b)] = get(stats, (a, b), 0) + 1 end return stats endÙ$bcf7667f-f99b-4d10-af84-5d3879f1db5d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$bcf7667f-f99b-4d10-af84-5d3879f1db5d«code_foldedäcodeÚýqa( html"What difference do you expect with respect to the previous model ?", md""" The products ``W_1C`` and ``DW_d`` have the same dimension as the matrices ``W_1`` and ``W_d`` of the previous model. So the expressive power of the model was not improved while we increased the number of parameters and we potentially made the loss function "even more nonconvex". If the hidden dimension (i.e., the number of rows of ``C`` / columns of ``W_1`` or the number of rows of ``W_d`` / columns of ``D``) is much smaller than ``n_\text{voc}``, then it's faster to compute ``W_1(Cx)``. Moreover, we are forcing the matrix ``W_1C`` to have a low rank compared the model without ``C``. This means less expressivness but it might also prevent overfitting so the case isn't so clear. The case become clearer when the input embedding ``C`` is shared between more than one character, i.e., ``n_\text{ctx} > 1``. Same for the output embedding, ``D`` is useful when it is not preceded by a linear with which it can just be merged. """)Ù$25b79953-fd7c-46c1-b760-d57c09910981„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$25b79953-fd7c-46c1-b760-d57c09910981«code_foldedäcodeÚ+qa(md""" How does the number of parameters of transformers compare with $(cite("bengio2000Neural")) or RNNs for large ``n_\text{ctx}`` ? """, md""" * The number of parameters of the transformer does **not** depend on ``n_\text{ctx}``. * The number of parameters of $(cite("bengio2000Neural")) depends linearly with ``n_\text{ctx}``. Assuming that the number of hidden neurons scales proportionally with ``n_\text{ctx}``, the number of parameters even scales quadratically with ``n_\text{ctx}``! * For RNNs, if the dimension of the internal state scales proportionally with ``n_\text{ctx}``, the number of parameters is also proportional with ``n_\text{ctx}``! If the dimension of the internal state is kept too small, increasing the context won't be so helpful, due to *encoder bottleneck*, see next slide. """)Ù$c032b3ff-c539-4e38-81d0-39b28b3a8076„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c032b3ff-c539-4e38-81d0-39b28b3a8076«code_foldedäcodeÙ4bib(["bahdanau2016Neural", "vaswani2017Attentiona"])Ù$45efc71d-d5f8-474e-9b89-e72fac7110fd„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$45efc71d-d5f8-474e-9b89-e72fac7110fd«code_foldedäcode·bib("bengio2000Neural")Ù$d050a7ee-3aa7-4539-a236-5b6446599ded„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d050a7ee-3aa7-4539-a236-5b6446599ded«code_folded¤codeÙ@struct BPE text::String pairs::Dict{Tuple{Char,Char},Char} endÙ$579a203b-e6f7-4190-b874-18b00a5c3f77„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$579a203b-e6f7-4190-b874-18b00a5c3f77«code_folded¤codeÚAfunction load_llms() llms = DataFrame(CSV.File("llms.csv")) rename!(llms, "Embedding dimension" => "``d_\\text{emb}``") rename!(llms, "Vocabulary size" => "``n_\\text{voc}``") rename!(llms, "Context window" => "``n_\\text{ctx}``") rename!(llms, "Feed-Forward hidden dimension" => "``d_\\text{ff}``") return llms endÙ$570fa160-3adb-463e-99b8-b7dd05076908„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$570fa160-3adb-463e-99b8-b7dd05076908«code_folded¤codeÙ7function softmax(x) y = exp.(x) return y / sum(y) endÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$9cb90e76-3bb5-41ff-bc79-c4949400d904«code_foldedäcodeÙâmd""" With ``n_\text{ctx} > 1``, the encoder ``C`` is shared by all tokens: See for instance the network below taken from $(cite("bengio2000Neural", "Figure 1")), the first popular application of neural nets for languages: """Ù$75ca478c-916f-464a-9435-8208ee726d50„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$75ca478c-916f-464a-9435-8208ee726d50«code_foldedäcodeÙSbib(["team2024Gemma", "team2024Gemmaa", "radford2019Language", "touvron2023Llama"])Ù$b9caae1a-38aa-4d01-9cda-3d6782fb0e03„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b9caae1a-38aa-4d01-9cda-3d6782fb0e03«code_foldedäcodeÚ†HAlign(md""" *Self-Attention* with embedding ``C`` is: ```math \text{Masked-MultiHead}(CX, CX, CX) ``` The embedding vectors ``CX`` take then different projections for value, key, query and also for different heads! ```math \text{head}_j = \text{Masked-Attention}(W_j^VCX, W_j^KCX, W_j^QCX) ``` """, HTML(html(@draw begin draw_transformer() highlight(200, 250, 375, 350) end 300 400)), )Ù$9ff95a9a-192b-4a12-8e2e-7acd6659c066„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$9ff95a9a-192b-4a12-8e2e-7acd6659c066«code_foldedäcodeÚ»md""" ```math \begin{align} Q & = \begin{bmatrix} q_1 & \cdots & q_{n_\text{ctx}} \end{bmatrix} & K & = \begin{bmatrix} k_1 & \cdots & k_{n_\text{ctx}} \end{bmatrix} & K^\top Q & = \begin{bmatrix} \langle k_1, q_1 \rangle & \cdots & \langle k_1, q_{n_\text{ctx}} \rangle\\ \vdots & \ddots & \vdots\\ \langle k_{n_\text{ctx}}, q_1 \rangle & \cdots & \langle k_{n_\text{ctx}}, q_{n_\text{ctx}} \rangle \end{bmatrix} \end{align} ``` """Ù$f95a6de6-5e02-4237-88ba-ec44ef3d38c3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f95a6de6-5e02-4237-88ba-ec44ef3d38c3«code_foldedäcodeÚKHAlign( md""" Different weights ``W_1 \in \mathbb{R}^{d_\text{ff} \times d_\text{emb}}``, ``W_2 \in \mathbb{R}^{d_\text{emb} \times d_\text{ff}}`` for each layer: ```math x \mapsto W_2\max(0, W_1x + b_1) + b_2 ``` Expansion factor ``d_\text{ff} / d_\text{emb}`` is typically 4× like suggested in $(cite("vaswani2017Attentiona")) (but not for Gemma) $(table(llms, mandatory_columns = ["``d_\\text{ff}``"], included_columns = [ "Name", "Ref", "``d_\\text{emb}``", "``d_\\text{ff}``", ])) """, HTML(html(@draw begin draw_transformer() highlight(200, -45, 375, 25) end 300 400)), )Ù$ed5b5702-4cca-4116-a70f-4a562178f490„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$ed5b5702-4cca-4116-a70f-4a562178f490«code_foldedäcodeÙþmd""" > **Challenging tradeoff**: Encode text to **increase** length of "past text" while keeping ``n_\text{ctx}`` and ``n`` **small** enough. Length of "past text" increases with vocabulary size ``n_\text{voc}`` and context window ``n_\text{ctx}``. """Ù$736920df-e4bb-4535-b982-e397aa0a782d„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$736920df-e4bb-4535-b982-e397aa0a782d«code_folded¤codeºiter_2 = new_token(iter_1)Ù$55a09acc-84da-491c-86ba-9a66f4ea52fe„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$55a09acc-84da-491c-86ba-9a66f4ea52fe«code_foldedäcodeÚÁHAlign( md""" ```math \begin{align} h^{(t+1)} & = \tanh(Wh^{(t)} + Ux^{(t+1)} + b)\\ o^{(t)} &= Vh^{(t)} + c\\ \hat{y}^{(t)} &= \text{softmax}(o^{(t)}) \end{align} ``` Illustrated on the right $(cite("goodfellow2016Deep", "Figure 10.3")). RNNs as language model showcased in $(cite("mikolov2010Recurrent")). **Issue**: Training time and space complexity is proportional to ``n_\text{ctx}`` and **cannot parallelize** to speed up. """, img("RNN") )Ù$d558636d-c714-4033-ae73-5b92c3cdedf3„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d558636d-c714-4033-ae73-5b92c3cdedf3«code_folded¤codeÙ0dict = Dict([1, 0] => [1, 1], [0, 1] => [-1, 1])Ù$8d231f2c-4b0c-4c37-a746-16e98d4cafc8„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$8d231f2c-4b0c-4c37-a746-16e98d4cafc8«code_foldedäcodeµmd"## Attention head"Ù$f1afaf8c-d9ad-446a-9826-9c4cda19993f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f1afaf8c-d9ad-446a-9826-9c4cda19993f«code_folded¤codeÙ6new_token(text::String) = new_token(BPE(text, Dict()))Ù$f7ca3ff7-b5cf-452b-b955-7219e7397324„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f7ca3ff7-b5cf-452b-b955-7219e7397324«code_folded¤codeÙ!iter_1 = new_token("aaabdaaabac")Ù$5f05e717-a51a-4a99-bb11-cc493217f93f„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$5f05e717-a51a-4a99-bb11-cc493217f93f«code_foldedäcodeÚrHAlign( md""" Norm of gradient increases exponentially with depth. Issue for deep neural net. Consider output ```math \begin{bmatrix} y_{1,1} & \ldots & y_{1,d_\text{emb}}\\ \vdots & \ddots & \vdots\\ y_{d_\text{batch},1} & \ldots & y_{d_\text{batch},d_\text{emb}} \end{bmatrix} ``` Normalization : ``y_{i,j} \mapsto g(y_{i,j} - \mu_{i,j})/\sigma_{i,j}`` for gain ``g``, mean ``\mu`` and standard deviation ``\sigma``. * Batch normalization : ``\sigma_{i,j} = \sigma_{j}`` $(cite("ioffe2015Batch")) * Layer normalization : ``\sigma_{i,j} = \sigma_{i}`` $(cite("ba2016Layer")) Batch norm depends on the batch hence [is tricky to implement](https://www.youtube.com/watch?v=P6sfmUTpUmc). Layer normalization is used in $(cite("vaswani2017Attentiona")). """, HTML(html(@draw begin draw_transformer() highlight(290, -85, 360, -45) highlight(290, 210, 360, 250) end 300 400)) )Ù$b56e9e56-e74a-401b-b4b5-f36bb33341d5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$b56e9e56-e74a-401b-b4b5-f36bb33341d5«code_foldedäcode³bib(["he2015Deep"])Ù$57c2c944-0d91-489d-8ad7-f5520e71ef3e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$57c2c944-0d91-489d-8ad7-f5520e71ef3e«code_foldedäcode¹md"## Byte Pair Encoding"Ù$bf563783-9784-4c74-a7b1-6d7a3ed618c5„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$bf563783-9784-4c74-a7b1-6d7a3ed618c5«code_foldedäcode¿md"## Matrix form of attention"Ù$f8330700-e964-4e19-9c55-2b11df45789e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f8330700-e964-4e19-9c55-2b11df45789e«code_foldedäcode¾md"## Embedding sizes in LLMs"Ù$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ec„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$8d6ec2b3-997e-4df5-a3b2-c1dffa53d0ec«code_foldedäcodeÚ½qa( md"What is the time complexity of inference with respect to ``d_\text{emb}``, ``n_\text{voc}``, ``n_\text{ctx}``, ``d_\text{ff}``, ``h`` and ``N`` ?", HAlign( md""" | Input | Parameters | Time | |-------|------------|------| | ``CX + P \in \mathbb{R}^{d_\text{emb} \times n_\text{ctx}}`` | ``W_j^V \in \mathbb{R}^{d_v \times d_\text{emb}}`` | ``O(d_v d_\text{emb} n_\text{ctx})`` | | ``CX + P \in \mathbb{R}^{d_\text{emb} \times n_\text{ctx}}`` | ``W_j^K, W_j^Q \in \mathbb{R}^{d_k \times d_\text{emb}}`` | ``O(d_k d_\text{emb} n_\text{ctx})`` | | ``K, Q \in \mathbb{R}^{d_k \times n_\text{ctx}}`` | | ``O(d_k n_\text{ctx}^2)`` | | ``V \in \mathbb{R}^{d_v \times n_\text{ctx}}, ... \in \mathbb{R}^{n_\text{ctx} \times n_\text{ctx}}`` | | ``O(d_v n_\text{ctx}^2)`` | | ``... \in \mathbb{R}^{d_v \times n_\text{ctx}}`` | ``W^O \in \mathbb{R}^{d_\text{emb} \times d_v}`` | ``O(d_\text{emb} d_v n_\text{ctx})`` | | ``... \in \mathbb{R}^{d_\text{emb} \times n_\text{ctx}}`` | ``W_1 \in \mathbb{R}^{d_\text{ff} \times d_\text{emb}}`` | ``O(d_\text{emb} d_\text{ff} n_\text{ctx})`` | | ``... \in \mathbb{R}^{d_\text{ff} \times n_\text{ctx}}`` | ``W_2 \in \mathbb{R}^{d_\text{emb} \times d_\text{ff}}`` | ``O(d_\text{emb} d_\text{ff} n_\text{ctx})`` | So for ``N`` layers (ignoring the complexity of the embedding): ```math O(Nn_\text{ctx}(n_\text{ctx}(d_v + d_k) + d_\text{emb}(d_v+d_k+d_\text{ff}))) ``` Assuming that ``d_v, d_k, d_\text{ff}`` has the same scale as ``d_\text{emb}``: ```math O(Nn_\text{ctx}^2d_\text{emb} + Nn_\text{ctx}d_\text{emb}^2) ``` """, HTML(html(@draw begin draw_transformer() translate(-10, 150) scale(0.6) Luxor.placeimage(readpng("images/multi-head.png"), centered = true) end 300 400)) ) )Ù$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5dd„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d1ba8da3-add8-4dbe-9ebf-9a32fa5cd5dd«code_foldedäcodeÚ�HAlign( md""" *Pre-activation* for residual neural networks introduced in $(cite("he2016Identity")) and used in GPT-2 $(cite("radford2019Language")). See figure on the right. *Rotary Positional Encoding* $(cite("su2023RoFormer")) replaces ``W^K(Cx_i + p_i)`` and ``W^Q(Cx_i + p_i)`` by ``R^i W^KCx_i`` and ``R^i W^QCx_i`` where ``R`` is a rotation matrix. Advantage : ``\langle k_i, q_j \rangle`` contains ``R^{i - j}`` → **relative** difference of position. """, HTML(html(@draw begin draw_transformer() sethue("blue") scale(2, 2) arrow(Point(175, -36), Point(180, -43), Point(170, -48), Point(145, -53), :stroke, startarrow=false, finisharrow=true) arrow(Point(175, -30), Point(190, -10), Point(195, 10), Point(145, 17), :stroke, startarrow=false, finisharrow=true) arrow(Point(175, 120), Point(190, 130), Point(195, 150), Point(145, 192), :stroke, startarrow=false, finisharrow=true) end 300 400)), )Ù$1d5b1b7c-828c-4a16-b446-cff21b015d45„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$1d5b1b7c-828c-4a16-b446-cff21b015d45«code_folded¤code·biblio = load_biblio!()Ù$a14e505e-2e4a-4c73-8133-7560ba58916b„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$a14e505e-2e4a-4c73-8133-7560ba58916b«code_foldedäcode»md"## Key-Value (KV) cache"Ù$2a7e5096-1e8d-4506-96d2-86de0a7d39aa„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$2a7e5096-1e8d-4506-96d2-86de0a7d39aa«code_foldedäcodeÙ–md""" Forcing ``D = C^\top`` appears to work well in practice $(cite("press2017Using")), this is what is used in $(cite("vaswani2017Attentiona")). """Ù$6712c883-b407-47e1-a666-4de05f8f8d6e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$6712c883-b407-47e1-a666-4de05f8f8d6e«code_foldedäcodeÚHAlign( md""" ```math \begin{multline} \hat{p}(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}) = \\ \text{softmax}(W_2 \tanh(W_1 \begin{bmatrix} C x_{-1}\\ \vdots\\ C x_{-n_\text{ctx}} \end{bmatrix} )) \end{multline} ``` """, img("bengio2000Neural", :width => 250), )Ù$af8194a1-a358-4cf7-b446-6b377cb76687„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$af8194a1-a358-4cf7-b446-6b377cb76687«code_foldedäcodeÚ md"The feed-forward network is implemented **independently** for the output of each query so each query can be processed independently through each **layer**. The next layer allows each queries to then look at the results of the previous layer for **past** (because of the mask) queries."Ù$04e9b912-6712-4290-acc4-f24bb27a1469„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$04e9b912-6712-4290-acc4-f24bb27a1469«code_foldedäcodeºmd"## Machine translation"Ù$d014e6aa-92f6-4ca1-be47-516565d1bb20„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$d014e6aa-92f6-4ca1-be47-516565d1bb20«code_foldedäcodeÚžHAlign(( md""" Heads focus on different aspects. Their outputs are **combined** with ``W^O \in \mathbb{R}^{d_\text{emb} \times hd_v}``: ```math \begin{align} \text{head}_j & = \text{Attention}(W_j^VV, W_j^KK, W_j^QQ)\\ \text{MultiHead}(V, K, Q) & = W^O\text{vcat}(\text{head}_1, \ldots, \text{head}_h) \end{align} ``` See $(cite("vaswani2017Attentiona", "Figure 2")) on the right. Similarly, in the masked case: ```math \begin{align} \text{head}_j & = \text{Masked-Attention}(W_j^VV, W_j^KK, W_j^QQ)\\ \text{Masked-MultiHead}&(V, K, Q) = W^O\text{vcat}(\text{head}_1, \ldots, \text{head}_h) \end{align} ``` """, img("multi-head", :width => 250)), [70, 30], )Ù$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$4df0a18d-cb14-41b1-ba40-fd6bfcbb0b03«code_foldedäcodeÙ4bib(["sennrich2016Neural", "kudo2018SentencePiece"])Ù$e2eca085-9f99-4e3a-9db4-e7f692aedd34„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$e2eca085-9f99-4e3a-9db4-e7f692aedd34«code_foldedäcodeÙ-md"## Text to vectors : step 2 → embedding"Ù$95504a74-d5ef-4fb7-83a0-88914c7cbc59„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$95504a74-d5ef-4fb7-83a0-88914c7cbc59«code_folded¤codeÙ"numerical_lookup(dict, [0.8, 0.2])Ù$453544fc-0e3e-4e04-8c0c-192f3a038884„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$453544fc-0e3e-4e04-8c0c-192f3a038884«code_foldedäcodeºmd"## Positional encoding"Ù$0583ee0c-3802-4e81-b179-a80a82493b43„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$0583ee0c-3802-4e81-b179-a80a82493b43«code_foldedäcodeÚÜmd""" Byte Pair Encoding algorithm $(cite("sennrich2016Neural")) greedily merges the most frequent pair of tokens over the dataset into a new token. Most used implementations are `SentencePiece` $(cite("kudo2018SentencePiece")) and `tiktoken` (play with it [here](https://tiktokenizer.vercel.app/)). For instance, on [this example](https://en.wikipedia.org/wiki/Byte_pair_encoding), the pair `('a', 'a')` is the most frequent so we substitute it by a new token, say `'Z'`: """Ù$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$728f5fdf-77a5-46c7-b3ee-01064ef1b7e2«code_foldedäcodeÚöqa(md"Should we discard all these intermediate ``\hat{Y}_i`` we computated or can we reuse it for the following token ?", md""" For the next token, the corresponding intermediate probabilities would be: ```math (\hat{p}_i(x_{-n_\text{ctx}+2} | x_{-n_\text{ctx}+1}), \ldots, \hat{p}_i(x_{0} | x_{-1}, \ldots, x_{-n_\text{ctx}+1}), \hat{p}_i(x_1 | x_{0}, \ldots, x_{-n_\text{ctx}+1})) ``` Note that ```math \begin{align} \hat{p}_i(x_{-n_\text{ctx}+2} | x_{-n_\text{ctx}+1}) & \approx \hat{p}_i(x_{-n_\text{ctx}+2} | x_{-n_\text{ctx}+1}, x_{-n_\text{ctx}})\\ \hat{p}_i(x_{0} | x_{-1}, \ldots, x_{-n_\text{ctx}+1}) & \approx \hat{p}_i(x_0 | x_{-1}, \ldots, x_{-n_\text{ctx}}) \end{align} ``` So for any ``j < n_\text{ctx}``, the ``j``th column of the ``\hat{Y}_i'`` that should be computed for the new token is approximately equal to the ``(j+1)``th column of ``\hat{Y}_i`` that we already computed for the previous token. What's more, the column of ``\hat{Y}_i`` was computed with one more token as context compared to what we need to compute in ``\hat{Y}_i'``. So even though it's not equal, reusing what we computed in ``\hat{Y}_i`` should provide better result, assuming the trained transformers using this auto-regressive structure in his layers. """)Ù$01372b00-ecb2-42bd-b408-13234717d969„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$01372b00-ecb2-42bd-b408-13234717d969«code_folded¤codeÙ°function new_token(bpe::BPE) stats = pair_stats(bpe.text) pair = findmax(stats)[2] new_char = min('Z' + 1, minimum(bpe.text)) - 1 return add_pair(bpe, pair => new_char) endÙ$f4366cf6-2be0-42b8-96c4-120be3f5c25e„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$f4366cf6-2be0-42b8-96c4-120be3f5c25e«code_foldedäcodeÚµmd""" ### References * Recurrent neural networks : $(cite("goodfellow2016Deep", "Chapter 10")) and Section 4.7 of [The Elements of Differentiable Programming book](https://diffprog.github.io/) * Transformers : $(cite("vaswani2017Attentiona")) and Section 4.8 of [The Elements of Differentiable Programming book](https://diffprog.github.io/) * [Neural Networks: Zero to Hero](https://karpathy.ai/zero-to-hero.html) by Andrej Karpathy """Ù$c8923675-e73e-4621-82b9-966d8b003b97„¨metadataƒ¨disabled©show_logsîskip_as_script§cell_idÙ$c8923675-e73e-4621-82b9-966d8b003b97«code_foldedäcodeÙ!md"# Encoder-decoder transformer"´last_hot_reload_timeË©shortpath¯transformers.jl¥nbpkgŠ­busy_packages�Ù,waiting_for_permission_but_probably_disabled§enabledðterminal_outputs�°HypertextLiteralÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...­MathTeXEngineÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...¥LuxorÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...¬PrettyTablesÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...¬LaTeXStringsÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...³DocumenterCitationsÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...ªnbpkg_syncÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...£CSVÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...§LoggingÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...§PlutoUIÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...­LinearAlgebraÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...ªDataFramesÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...²PlutoTeachingToolsÚ Resolving... ===  Project No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Project.toml`  Manifest No packages added to or removed from `~/.julia/scratchspaces/c3e4b0f8-55cb-11ea-2926-15256bba5781/pkg_envs/env_ctnkygpefy/Manifest.toml` Instantiating... === Precompiling... === Waiting for notebook process to start... Done. Starting precompilation...¶waiting_for_permission·restart_recommended_msgÀ´restart_required_msgÀ²installed_versionsްHypertextLiteral¥1.0.0­MathTeXEngine¥0.6.9ªDataFrames¥1.8.2¬PrettyTables¥3.4.8¬LaTeXStrings¥1.4.1Ù!__internal_julia_manifest_version¦1.13.0³DocumenterCitations¥1.5.0£CSV¥1.0.0§Logging¦stdlib§PlutoUI¦0.7.83­LinearAlgebra¦stdlib¸__internal_julia_version¦1.13.0¥Luxor¥4.5.0²PlutoTeachingTools¥0.4.7¯install_time_nsÎÅ0¼¬instantiatedípluto_version¦v1.0.3