<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Jayden Yang</title>
  <subtitle>Tech blog</subtitle>
  <link href="https://bayernmuller.github.io/" rel="alternate" type="text/html" />
  <link href="https://bayernmuller.github.io/atom.xml" rel="self" type="application/atom+xml" />
  <link href="https://bayernmuller.github.io/rss.xml" rel="alternate" type="application/rss+xml" />
  <id>https://bayernmuller.github.io/</id>
  <updated>2026-03-02T00:00:00.000Z</updated>
  <author><name>Jayden Yang</name></author>
  
  <entry>
    <title>Attentive Low-Rank Filter Adaptation for Structure-Aware Cross-Domain Personalized Gaze Estimation</title>
    <link href="https://bayernmuller.github.io/posts/260302-attentive-low-rank-filter-adaptation/" />
    <id>https://bayernmuller.github.io/posts/260302-attentive-low-rank-filter-adaptation/</id>
    <published>2026-03-02T00:00:00.000Z</published>
    <updated>2026-03-02T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="Alfa" /><category term="LoRA" /><category term="Gaze Estimation" /><category term="Machine Learning" /><category term="ML" /><category term="Korean" />
    <summary>논문 리뷰: Attentive Low-Rank Filter Adaptation (Alfa)</summary>
    <content type="html">&lt;h1 id=&quot;0-backgrounds&quot;&gt;0. Backgrounds&lt;/h1&gt;
&lt;h2 id=&quot;01-model-agnostic-meta-learning-maml&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#01-model-agnostic-meta-learning-maml&quot; aria-label=&quot;Link to “0.1 Model-Agnostic Meta-Learning (MAML)”&quot;&gt;&lt;strong&gt;0.1 Model-Agnostic Meta-Learning (MAML)&lt;/strong&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1703.03400&quot;&gt;Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Meta-learning은 여러 task에서의 학습 과정을 관찰하여, 새로운 task에 대해 적은 데이터로도 빠르게 적응할 수 있는 모델 파라미터를 학습하는 방식이다.&lt;/p&gt;
&lt;p&gt;MAML은 모델 구조와 무관하게 적용 가능한 gradient-based meta-learning 방법으로, 각 데이터 샘플 마다 최적의 파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;가 존재한다는 가정하에, few-shot 학습 이후의 성능이 좋아지도록 공통 초기 파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;를 학습한다.&lt;/p&gt;
&lt;p&gt;이때 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;는 각 혹은 전체 데이터 샘플의 최적 파라미터가 아니라, 소수의 gradient update만으로 각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;에 빠르게 도달할 수 있는 출발점이다.&lt;/p&gt;
&lt;p&gt;MAML 논문에서는 reinforcement learning 에서도 용례도 함께 설명하지만 few-shot supervised learning 을 중심으로 설명한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;533&quot; height=&quot;346&quot;&gt;&lt;/p&gt;
&lt;p&gt;먼저 일반적인 supervised learning 에서의 최적화의 대상은 다음과 같다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;J&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 와 그에 대응하는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 있고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 와 제일 적은 loss 를 가지도록 하는 것이 목표이다. 하지만 Meta Learning 은 다음을 최적화 한다. 전체 Task 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 라고 하고, 그 중 샘플링한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 번째 데이터가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 라고 하자.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;J&lt;/mi&gt;&lt;mtext&gt;meta&lt;/mtext&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;munder&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/munder&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi scriptlevel=&quot;2&quot;&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;식을 풀어서 설명하자면,&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;먼저 전체 Task 에서 일부가 샘플링 된다.  (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)&lt;/li&gt;
&lt;li&gt;그 후 각 샘플 데이터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 경사 하강을 수행한다. (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)&lt;/li&gt;
&lt;li&gt;샘플 데이터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 을 해당 샘플 데이터의 다른 데이터로 평가한다 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)&lt;/li&gt;
&lt;li&gt;1~3 의 과정을 모든 샘플들에 대해 수행한 뒤 결과를 합한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;즉 MAML 은 task 안에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개 데이터를 샘플링 하여, base model &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서 각 샘플 데이터로 경사 하강 한 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 성능을 최적화 한다. 이것이 어떤 의미를 가지는지는 논문에 있는 알고리즘을 참고하면 알 수 있다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-1.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1228&quot; height=&quot;992&quot;&gt;&lt;/p&gt;
&lt;p&gt;각 샘플 데이터로 훈련된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 들의 gradient 로 base model &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;  를 업데이트 한다. 곧 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 특정 Task 샘플 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒾&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;에 대한 최적화를 하는게 아니라, 샘플링 된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 데이터들에서 각각의 몇 step 의 경사하강만 수행 한  모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 test data loss 에 대한 gradient 들로 업데이트 되는 것을 의미한다.&lt;/p&gt;
&lt;p&gt;여기서 각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 번째 Task 샘플에 대한 업데이트의 결과인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 만드는 과정을 Inner Loop 라 하고, 각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 Loss 를 합쳐 base model 을 업데이트 하는 과정을 Outer Loop 라고 한다.&lt;/p&gt;
&lt;p&gt;중요한 것은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;번째 Task 샘플에 대한 Loss 에 들어가는 데이터가 Inner Loop 와 Outer Loop 가 다르다는 점이다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 로 나누어진다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 만드는 loss 에 들어가는 데이터는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개 샘플한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 이며, Outer Loop 에서 사용되는 것은 또 다르게 샘플된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 이다. 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 일반화 성능을 향상하기 위함이다.&lt;/p&gt;
&lt;p&gt;Gaze Estimation 문제를 예시로 들어보자. 10명에 대한 전체 데이터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 있을 때, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 각 사람의 데이터셋이다. Inner Loop 에서 사용되는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 일부이며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 는 또 다른 일부이다. 즉 한 사람에 대해 few-shot learning 을 진행한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는, 동일한 사람의 또 다른 데이터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 로 평가되어 overfitting 을 방지한다.&lt;/p&gt;
&lt;p&gt;Outer Loop 에서는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;←&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 업데이트 하는데, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 loss 의 미분 결과를 base model &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 적용하는게 직관적이지 않을 수도 있지만 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로 , &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서부터 미분 가능하다.&lt;/p&gt;
&lt;p&gt;미분 과정에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로 outer loss 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 미분 할 때 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mi&gt;∂&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;∂&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 구해야 하므로, 2차 미분이 필요하다. 즉 Scalar 인 Loss 의 결과를 미분한 결과는 벡터이고,각 벡터 요소마다 미분을 수행하므로 2차 미분의 결과는 행렬이 된다.&lt;/p&gt;
&lt;p&gt;이 행렬의 크기는 파라미터 갯수의 제곱에 비례하므로 계산 비용 측면에서 굉장히 까다로우나, 논문에서는 이 항을 제거하여 근사하는 1차 미분으로도 유사한 성능을 달성하였다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;∂&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;∂&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉, 메타 러닝은 pre-train 시점에서의 task 의 최적 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 아닌 few-shot 으로의 fine-tune 이 몇 스텝만으로 이루어질 수 있도록 파라미터를 초기화 하는 것이 목표인 방법론이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∗&lt;/mo&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;arg&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;munder&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/munder&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;𝔼&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.8em&quot; maxsize=&quot;1.8em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒯&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;;&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;;&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.8em&quot; maxsize=&quot;1.8em&quot;&gt;)&lt;/mo&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h2 id=&quot;02-few-shot-adaptive-gaze-estimation&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#02-few-shot-adaptive-gaze-estimation&quot; aria-label=&quot;Link to “0.2 Few-shot Adaptive Gaze Estimation”&quot;&gt;0.2 Few-shot Adaptive Gaze Estimation&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1905.01941&quot;&gt;Few-Shot Adaptive Gaze Estimation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Appearance-based Gaze Estimation 문제에서 person-specific accuracy 를 위한 Test-time personalization 방법론 중 하나로, DT-ED 아키텍쳐와 MAML 전략을 사용한 AdaGEN 을 제안한다.&lt;/p&gt;
&lt;p&gt;이미지 기반 시선추적 문제에서 개인 간의 해부학적 차이 등의 문제를 해결하기 위해 고전적인 vision software 기반 모델에서 소수의 샘플로 personalization 하는 방법론들이 제안되었지만, 통제되지 않은 환경에서의 이미지 변동에 강건하지 못하는 한계가 있었고, CNN 기반의 모델에서는 few shot 으로 personalization 을 하는 것이 과적합으로 이어졌다.&lt;/p&gt;
&lt;p&gt;저자가 제안하는 FAZE 프레임워크에서는 rotation-aware latent representation 을 학습하고, 이 feature 들로 meta learning 을 이용해 few-shot adaptable 한 gaze estimator 를 학습한다.&lt;/p&gt;
&lt;h3 id=&quot;021-related-work-learned-equivarience&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#021-related-work-learned-equivarience&quot; aria-label=&quot;Link to “0.2.1 Related Work: Learned Equivarience”&quot;&gt;0.2.1 Related Work: Learned Equivarience&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;등변성 (Equivarience) 는 input 이 바뀌면 output 도 동일하게 바뀌는 성질을 말한다. 최근 연구들은 입력, 잠재 특징, 레이블 공간 등의 equivariant mapping 학습을 강제함으로써 개선이 이루어짐을 보여주었다. Interpretable transformations with encoder-decoder networks 논문에서는 encoder-decoder 구조를 통해 합성 광원 방향과 같은 복잡한 현상을 학습하였고, Unsupervised geometry-aware representation for 3d human pose estimation 에서는 multi-view 환경에서 이 방법을 적용해 pose estimation 성능을 향상시켰다.&lt;/p&gt;
&lt;p&gt;저자는 이 아이디어를 확장하여 gaze estimation task 에서 사용하는데, CNN 으로 부터 얻은 feature 는 그 자체로는 인간이 해석할 수 없는 벡터일 것이다. 하지만 만약 실제로 의미가 있는 representation 을 학습 시킬 수 있다면 큰 이점을 가질 수 있다.&lt;/p&gt;
&lt;p&gt;먼저 어떤 image 가 있는데, 여기엔 사람이 정면을 응시하고 있다고 가정한다. 정면이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 라고 설정하고, 이 이미지를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 라고 하자. 또 다른 이미지는 같은 사람이 얼굴을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 회전한 이미지이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 라고 표기한다.&lt;/p&gt;
&lt;p&gt;head rotation equivariance 가 보장된 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;⋅&lt;/mo&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 있다고 하면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서 head rotation equivariance 가 보장되었으므로&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이다. 이것은 모델이 서로 다른 두 이미지에서 얼굴 자세만 분리 (distengling) 했다는 것을 의미한다. 서로 다른 이미지이니 조명, appearance 등 전혀 다른 이미지임에도 불구하고 시선 추정에 중요하게 작용하는 head pose 만을 정확히 모델링 해냈으며, 다르게 이야기하면 시선 추정에 필요 없는 노이즈에 대해 강건하다는 의미로 해석된다.&lt;/p&gt;
&lt;h3 id=&quot;022-dt-ed&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#022-dt-ed&quot; aria-label=&quot;Link to “0.2.2 DT-ED”&quot;&gt;0.2.2 DT-ED&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-2.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;771&quot; height=&quot;362&quot;&gt;&lt;/p&gt;
&lt;p&gt;Disentangling Transforming Encoder-Decoder (DT-ED) 는 사람의 외형에 대한 잠재 표현이 실제로 머리, 눈의 회전을 반영하도록 하는데에 그 목적이 있다.&lt;/p&gt;
&lt;p&gt;우선 눈과 머리 회전에 대한 표기를 정리한다. 눈의 회전은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;, 머리의 회전은  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;ϕ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 쓰고, 다음 회전 행렬로 변환된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mrow class=&quot;tml-med-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;ϕ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;ϕ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;ϕ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;ϕ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;ϕ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;DT-ED 의 학습 과정에서 같은 사람의 두 이미지 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 입력한다고 하자. 샘플 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 갈때의 시선 방향의 변화를 표현하기 위해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/mrow&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 게산할 수 있다. 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 회전을 무마 (역행렬) 하고, 회전이 없는 상태에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 만큼 회전한다고 해석한다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;로 가는 회전의 정도이다. 마찬가지로 머리의 회전 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/mrow&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;h&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 도 구할 수 있을 것이다.&lt;/p&gt;
&lt;p&gt;여기서 실제로 두 샘플이 서로 어떻게 회전했는지에 대한 정답 데이터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 구할 수 있을것이다. 그렇다면 latent code &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 의 회전은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/mrow&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 표현될 수 있다. 학습 시에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 가  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 로 학습되도록 손실 함수를 설계하여 이를 달성할 수 있다.&lt;/p&gt;
&lt;p&gt;DT-ED 에는 인코더 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℰ&lt;/mi&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 와 디코더 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 있으며, Latent embedding &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 아래와 같이 정의된다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;외형 임베딩: &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;a&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;안구 회전 임베딩: &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;머리 회전 임베딩: &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;h&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;3가지 특징을 가지며, 단일 열을 만들기 위해 flatten 하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;a&lt;/mi&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;;&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;;&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;h&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 정의한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mi&gt;ℕ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;h&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;h&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;디코더는 이 시선 잠재 표현 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 에 어떤 Rotation Matrix 를 적용했을 때 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, 이를 바탕으로 디코더의 출력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/math&gt;&lt;/span&gt; 이 같은 사람의 이미지이되 ground-truth gaze/head rotation이 반영된 이미지가 만들어지도록 하는게 목표이며 이는 훈련 데이터셋에 있는 실제로 안구만 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 만큼 회전한 이미지와 픽셀 단위의 비교 Loss 를 이용하여 달성한다. 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;h&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 에 대해서도 적용된다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/equivariance.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3277&quot; height=&quot;2154&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;recon&lt;/mtext&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munder&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/munder&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mover&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-3.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;786&quot; height=&quot;414&quot;&gt;&lt;/p&gt;
&lt;p&gt;위 사진은 각 임베딩을 임의로 회전시켜 만든 디코더의 출력 예시이다.&lt;/p&gt;
&lt;p&gt;또다른 loss 의 항은 임베딩의 일관성을 위해 존재하는데, 아래와 같다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;EC&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;munder&gt;&lt;mi&gt;max&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/munder&gt;&lt;/mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 배치 크기 이고 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 즉 latent representation 이 frontalized 된 형태이다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 벡터간 거리를 뜻하며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 번째 샘플의 person-identity 이다.&lt;/p&gt;
&lt;p&gt;즉 이 손실은 동일 인물간 최대 특징 차이를 최소화한다. 어떤 사람에 대해, 시선 방향이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;30&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 과 시선 방향이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;48&lt;/mn&gt;&lt;mi&gt;°&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 있을 때, 둘을 각각 frontalization 한 벡터의 거리가 샘플 중 최대라면, 그것을 최소화 하도록 하는 것이다. 이상적인 상황은 어떤 사람의 모든 frontalized latent representation 간 거리가 0인 상태일 것이다.&lt;/p&gt;
&lt;p&gt;만약 이를 모든 사람에게 똑같이 적용해 버리면 시선 추정에 필수적인 개인 간의 해부학적 차이 마저 무시되어 성능이 심각하게 저하된다.&lt;/p&gt;
&lt;p&gt;마지막으로 agular errro 항이다. gaze latent 에서 MLP &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒢&lt;/mi&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 출력된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 손실이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;gaze&lt;/mtext&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;arccos&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;3가지의 항이 모여 모델의 full loss 가 정의된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;full&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mtext&gt;recon&lt;/mtext&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;recon&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mtext&gt;EC&lt;/mtext&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;EC&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mtext&gt;gaze&lt;/mtext&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;gaze&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h3 id=&quot;023-adagen-ips-gen&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#023-adagen-ips-gen&quot; aria-label=&quot;Link to “0.2.3 AdaGEN, iPS-GEN”&quot;&gt;0.2.3 AdaGEN, iPS-GEN&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;CNN 같이 구조가 복잡하고 over-parameterized 된 모델은 Few-shot 으로만 person-specific 모델을 만들어야 할 때 overfitting 의 위험이 있다. few-shot 에 너무 의존적으로 학습하여, 자칫하면 노이즈를 학습할 수 있기 때문이며 모델 복잡도 때문에 최적화의 어려움도 있다.&lt;/p&gt;
&lt;p&gt;때문에 저자는 CNN 을 personalization 하지 않고, DT-ED 는 그대로 유지한 채 gaze feature &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 로 부터 시선 방향을 추론하는 MLP 모델만 personalization 의 대상으로 본다. 여기서 DT-ED 가 출력한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 로 부터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;3&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 을 추론하는 MLP 네트워크를 AdaGEN 이라고 한다.&lt;/p&gt;
&lt;p&gt;또한 few-shot 상황에서 몇번의 경사 하강 만으로 성능을 끌어내기 위해 Meta Learning 방법론을 제안하며, AdaGEN 으로 부터 개인화된 네트워크를 PS-GEN 이라고 한다.&lt;/p&gt;
&lt;p&gt;AdaGEN 을 few-shot adaptive 하게 meta-learning 하는 방법에 대해서 설명한다. 전체 사람 집합 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒮&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 을 중복되지 않는 피험자들로 구성된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mtext class=&quot;tml-med-pad&quot;&gt;train&lt;/mtext&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 과 meta test &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mtext class=&quot;tml-med-pad&quot;&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 으로 나눈다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 번째 메타 학습에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mtext class=&quot;tml-med-pad&quot;&gt;train&lt;/mtext&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 내 한명을 무작위로 선택한다 → &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;. 해당 인물에 대해 데이터를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개 샘플링한다. 샘플된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개 데이터를 제외하고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개를 더 샘플링한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 은 일반적으로 작으며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 few-shot 학습에서 사용되는 샷 갯수를 의미한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msup&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;먼저 위와같이 모델의 파리미터가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 일 때, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 대한 Loss 에 대해 경사하강을 수행하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 을 구한다.&lt;/p&gt;
&lt;p&gt;이후 업데이트된 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;  평가된 Loss 를 계산하고, 해당 Loss 에 대한 경사하강을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에서 수행한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msubsup&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;train&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;수렴할 때 까지 학습을 반복하여  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∗&lt;/mo&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;  를 얻는다.  이제 DT-ED 와 AdaGEN 이 완성된 FAZE 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℳ&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∗&lt;/mo&gt;&lt;/msup&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 이 만들어졌다.&lt;/p&gt;
&lt;p&gt;Personalization 시점에서는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mtext class=&quot;tml-med-pad&quot;&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 로 부터 각 새로운 사람 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 개인별 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℳ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext scriptlevel=&quot;2&quot;&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 학습한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;g&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∗&lt;/mo&gt;&lt;/msup&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝓅&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∗&lt;/mo&gt;&lt;/msup&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이제 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒫&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msup&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mtext&gt;test&lt;/mtext&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 로 테스트한다.&lt;/p&gt;
&lt;h3 id=&quot;024-result&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#024-result&quot; aria-label=&quot;Link to “0.2.4 Result”&quot;&gt;0.2.4 Result&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-4.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1991&quot; height=&quot;408&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-5.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;969&quot; height=&quot;377&quot;&gt;&lt;/p&gt;
&lt;p&gt;DT-ED 를 그대로 사용하고, MLP 에 대해 Meta learning 혹은 fine-tune 사용 간 결과를 볼 수 있다. shot 이 적을 때 심한 overfitting 이 발생하는 것에 비해 MAML trained 모델은 적은 샘플에서도 안정적으로 학습하는 것을 알 수 있다.&lt;/p&gt;
&lt;p&gt;Auto Encoder (AE) + MAML 과 DT-ED + MAML 간 차이도 알 수 있는데, 두 모델 간 유일한 차이는 DT-ED 에서는 디코딩 하기 전에 latent 가 회전된다는 것이며, 저자는 더 어려운 작업임에도 불구하고 rotation-aware representation 이 최종 시선 추적 문제에 대해 더 유리한 것을 보여주며, 시선, 머리 자세, 외형을 분리하는 것이 시선 추정에 중요한 요소임을 주장한다.&lt;/p&gt;
&lt;h2 id=&quot;03-tpgaze&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#03-tpgaze&quot; aria-label=&quot;Link to “0.3 TPGaze”&quot;&gt;0.3 TPGaze&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/html/2401.01577v3&quot;&gt;Test-Time Personalization with Meta Prompt for Gaze Estimation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;TPGaze 는 Gaze Estimation 모델의 Fine-tune 을 하는 방법론으로, NLP 분야의 프롬프트 튜닝에서 영감을 받았다. CNN 에서의 Padding 을 Zero Padding 하지 않고 Tunable Padding 으로 대체하여 일종의 프롬프트로써 사용한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-6.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1632&quot; height=&quot;1326&quot;&gt;&lt;/p&gt;
&lt;p&gt;패딩 값을 변경하면 Convolution 이후 feature map 이 달라지므로, 이를 통해 네트워크의 동작을 특정 사용자에게 맞게 유도할 수 있다. 이는 모델 아키텍쳐를 그대로 가져갈 수 있다는 장점을 가지며, 전체 파라미터의 1% 미만을 업데이트 하므로 계산 비용이 매우 적다.&lt;/p&gt;
&lt;p&gt;즉 몇개의 unlabled data 만으로 Test time personalization 하는것이 목적이며, source domain 에서의 backbone 학습은 다음과 같다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;arg&lt;/mtext&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;mrow&gt;&lt;munder&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/munder&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;ı&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;ı&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;ı&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 은 supervised-loss, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 은 unsupervised loss 를 의미한다.&lt;/p&gt;
&lt;p&gt;Backbone 에 train 시  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝓊&lt;/mi&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝓃&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 사용하는 이유는 이후 test-time에 unsupervised loss로만 적응해야 하니, 그 loss를 미리 모델에 학습시켜 두는 것으로 이해할 수 있다. 이는 많은 UDA 연구들에서 사용되는 기법이며, TPGaze 저자는 후술할 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 사용한다.&lt;/p&gt;
&lt;p&gt;이제 Convolution Padding 을 Prompt 로 취급하고, Prompt 만 Meta-Learning 한다. 학습된 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 주어졌을 때 프롬프트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 도입하여 모델을 수정하고 새로운 모델 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 얻는다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;‖&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;‖&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;s&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 사람의 얼굴이 좌우 대칭이라는 점을 이용한 left-right symmetry loss 이다. 어떤 이미지에 대한 모델의 출력과, 그 이미지를 좌우 반전 시킨 이미지의 출력이 yaw 만 뒤집힌 형태로 나오도록 유도한다.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;참고:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-7.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2274&quot; height=&quot;966&quot;&gt;&lt;/p&gt;
&lt;p&gt;left-right symmetry loss 가 아닌 rotation consistency loss 도 선택지 중 하나였지만, 원본 이미지의 여러 회전 버전에 대한 네트워크 출력을 요구하여 모델 train 시 큰 비용이 필요해 선택되지 못했다.&lt;/p&gt;
&lt;p&gt;아래 알고리즘을 따라 메타 학습이 진행된다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-8.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;542&quot; height=&quot;396&quot;&gt;&lt;/p&gt;
&lt;p&gt;source domain data 셋인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℐ&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒴&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 mini-batch 를 샘플링 하고, mini-batch 의 각 입력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 프롬프트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mover&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-med wbk-med-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/math&gt;&lt;/span&gt; 을 구한다. (Inner loop) 이후 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mover&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-med wbk-med-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/math&gt;&lt;/span&gt; 들에 대한 loss 로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 업데이트된다. (Outer loop)&lt;/p&gt;
&lt;p&gt;여기서 중요한 것은 Inner loop 과 Outer loop 의 Loss 가 다른데, 이는 test-time 에 unsupervised learning 을 해야하기 떄문에 각 샘플에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 경사하강 결과인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mover&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-med wbk-med-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/math&gt;&lt;/span&gt; 을 구하긴 하지만 결국에 모델 자체의 의 성능을 유지시켜야 하기 때문에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 최적화 결과이다.&lt;/p&gt;
&lt;p&gt;이후 사람 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 Test-time personalization 은 아래 식으로 진행된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;arg&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;munder&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/munder&gt;&lt;/mrow&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒜&lt;/mi&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒿&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒜&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-9.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;632&quot; height=&quot;308&quot;&gt;&lt;/p&gt;
&lt;p&gt;시험에 따르면, ResNet-18의 17개 Convolutional layer 전체에 프롬프트를 적용하면 오히려 성능이 크게 하락한다. 이는 모델의 가장 깊은 층이 변경되면, 고정되어 있는 마지막 Linear layer와의 매핑이 어긋나기 때문이며  첫 9개의 Convolutional layer에만 프롬프트를 추가하는 것이 성능과 효율 면에서 가장 이상적이라고 제안한다.&lt;/p&gt;
&lt;p&gt;Alfa 는 이 TPGaze 논문에서 설계한 실험을 거의 그대로 수행하여 TPGaze 대비 성능 우위를 주장한다.&lt;/p&gt;
&lt;h2 id=&quot;04-singular-value-decomposition&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#04-singular-value-decomposition&quot; aria-label=&quot;Link to “0.4 Singular Value Decomposition”&quot;&gt;0.4 Singular Value Decomposition&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;먼저 SVD 를 설명하기 전에 eigenvalue 와 egienvector 를 알아야한다.&lt;/p&gt;
&lt;p&gt;어떤 정방 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 선형변환으로 봤을 때, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 의한 변환 결과가 자기 자신의 상수배가 되는 벡터를 eigenvector 라고 하고, 그 상수를 eigenvalue 라고 한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;eigenvector of &lt;/mtext&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;eigenvalue of &lt;/mtext&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;eigenvalue 와 eigenvector 는 행렬에 따라 아예 존재하지 않을 수도 있고, 1개에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개 까지 존재할 수 있다.&lt;/p&gt;
&lt;p&gt;이는 기하학적으로 보았을 때 선형 변환 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 의해 방향은 보존되고 scale 만 변화되는 방향 벡터를 나타내는 것으로, 이때 eigenvector 변환의 축, eigenvalue 는 변환의 scale 정도 라고 표현할 수 있다.&lt;/p&gt;
&lt;p&gt;이러한 성질을 이용하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 eigendecomposition 할 수 있는데, 정방 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 eigenvalue/vector 를 가진다고 하면 아래와 같이 표현할 수 있다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋱&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서, 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 eigenvector 를 열벡터로 하는 행렬을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;, eigenvalue 를 대각원소로 하는 대각 행렬을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Λ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 라고 하면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Λ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Λ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow class=&quot;tml-med-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이처럼 임의의 정방 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;  이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 일차 독립 eigenvector 를 가지면 대각화 분해 할 수 있다. 즉 벡터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에 대해, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 을 만들 수 없다면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 일차 독립하며, 모든 eigenvector 가 서로에게 일차 독립하면 eigendecomposition 을 할 수 있다.&lt;/p&gt;
&lt;p&gt;여기서 eigenvalue 는 행렬에 대해 유일하게 결정되지만 eigenvector 는 그렇지 않기 때문에, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 을 만족하는 벡터를 사용하는 것이 일반적이다.&lt;/p&gt;
&lt;p&gt;이제 정방 행렬이 아닌 임의의 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 선형 변환으로 보고 행렬을 분해해보자.&lt;/p&gt;
&lt;p&gt;먼저 대칭 행렬은 eigendecomposition 이 항상 가능하다. 그렇기 때문에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 과 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 도 이를 수행할 수 있다. 이제 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;를 분해하면 다음과 같다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 eigenvector 들을 열벡터로 가지는 직교 행렬, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;  의 eigenvector 를 열벡터로 가지는 직교 행렬이다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;   의 eigenvalue 의 제곱근을 대각 원소로 가지는 대각 행렬인데, 두 행렬의 eigenvalue 는 서로 같다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 양 변에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 곱하면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 곧 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 eigenvector 이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 eigenvector 임을 알 수 있고, eigenvalue 는  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 같다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋱&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;선형 변환에서 변환의 각 축을 의미하는 eigenvector 를 열벡터로 가지는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 모두 직교 행렬이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mrow class=&quot;tml-med-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 성립한다. 모든 열벡터 에 대해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;det&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;det&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;det&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;±&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다.&lt;/p&gt;
&lt;p&gt;곧 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 회전(회전 + 반사) 행렬이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 대각행렬 이므로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 에 의해 회전시킨 후 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 Scaling 을 한 뒤 다시 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 회전시키는 것으로 해석할 수 있다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-10.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1472&quot; height=&quot;1298&quot;&gt;&lt;/p&gt;
&lt;p&gt;그럼 왜 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 성립할까?&lt;/p&gt;
&lt;p&gt;각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서, 양 변에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 을 곱하면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 단위 벡터 이므로 , &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;‖&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 이제 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0em&quot;&gt;:&lt;/mo&gt;&lt;mo lspace=&quot;0em&quot;&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;  로 정의한다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 도 마찬가지로 단위 벡터이다.&lt;/p&gt;
&lt;p&gt;정의로부터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 이때 아까의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 양변을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 나누면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 고유벡터이다.&lt;/p&gt;
&lt;p&gt;이제 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 아닌 모든 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 정규 직교 고유벡터들을 모은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 풀어서 써보자:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로,&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 정규 고유 벡터 집합, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 정규 고유 벡터 집합, 두 행렬의 특이값의 대각 행렬로 완벽히 복원된다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/svd_d_rank.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3698&quot; height=&quot;1118&quot;&gt;&lt;/p&gt;
&lt;p&gt;truncated SVD는 SVD에서 특이값(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)이 큰 것부터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;개만 남기고 나머지를 버리는 것이다. 이렇게 하면 원래 행렬을 가장 비슷하게 유지하면서도 더 단순한(낮은 rank의) 행렬로 압축할 수 있다.&lt;/p&gt;
&lt;p&gt;이는 머신러닝의 가중치 관점에서 보면, 가중치가 가장 강하게 작용하는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;개의 방향만 남기고, 영향이 작은 방향은 제거하는 것이라고 볼 수 있다. 즉, 모델이 주로 사용하고 있는 큰 변화 방향을 보존하는 방법이라고 해석할 수 있다.&lt;/p&gt;
&lt;h2 id=&quot;05-low-rank-adaptation&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#05-low-rank-adaptation&quot; aria-label=&quot;Link to “0.5 Low-Rank Adaptation”&quot;&gt;0.5 Low-Rank Adaptation&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;자연어 처리 문제에서 중요한 문제중 하나는 일반 도메인 데이터에 대한 pretrained 모델에서 특정 작업 또는 도메인으로 adaptation 하는 것이다. 일반적으로 base model 의 크기가  커질 수록 모든 파라미터를 재학습 하는 full fine-tuning 은 현실적으로 어려워진다.&lt;/p&gt;
&lt;p&gt;GPT-3 175B 의 경우 각각 175B 파라미터를 가진 fine-tuned 모델을 배포하는 것은 터무니없는 비용이 들것이다. 논문 저자는 pretrained 모델의 가중치를 고정하고 Transformer 아키텍쳐의 각 계층에 learnable low-rank matrix를 주입하여 학습가능한 파라미터 수를 획기적으로 줄이는 LoRA 를 제안한다.&lt;/p&gt;
&lt;p&gt;LLM 연구들은 폭발적으로 증가했으며 이에 따라 모델 크기도 더욱 커졌다. GPT-2 수준의 크기에서는 대두되지 않았으나 175B 정도 사이즈인 GPT-3 에서는 언급했듯 fine-tuned 모델을 배포하는 것이 엄청난 부담이 되었다.&lt;/p&gt;
&lt;p&gt;기존 Fine-tune 방법론들은 일부 파라미터들만 adaptation 하거나 새로운 작업을 위해 Adapter 같은 모듈을 학습 시키는 방식으로 이 문제를 해결하려고 했으며 pretrained 모델에 더해 소량의 파리미터만을 저장/로딩하면 되어 배포 시 효율성이 크게 향상되었다.&lt;/p&gt;
&lt;p&gt;그러나 기존 방법론들은 모델 깊이를 확장해 Inference Latency 를 증가시키거나 모델의 시퀀스 길이를 줄이는 경우도 있었다. 제일 중요한것은 이런 방법들이 종종 fine-tune 기준 성능에 미치지 못해 효율성과 모델 퀄리티 사이에 trade-off 를 발생시킨다는 점이다.&lt;/p&gt;
&lt;p&gt;LoRA 는 over-parameterized 모델이 실제로 낮은 low intrinsic demension 에 존재한다는 것을 보여준 Chunyuan Li et al 의 “MEASURING THE INTRINSIC DIMENSION OF OBJECTIVE LANDSCAPES” 논문에서 아이디어를 얻는다.&lt;/p&gt;
&lt;p&gt;해당 논문에서는 최근 모델들이 방대한 수의 파라미터를 사용하며, 훈련 과정에서 데이터셋과 네트워크 아키텍쳐가 결정되는 순간 objective landscape 의 전체 구조가 고정되며, Loss 를 최소화하기 위해 이 다차원 landscape 를 탐색하며 최적의 경로를 찾는 과정이라는 것을 전제로, 문제를 기 위해 필요한 파라미터의 수를 파악하기 위해 진행된 연구이다. 즉 어떤 문제가 있을때, 실제로 문제를 푸는데 필요한 최소한의 파라미터는 몇개인가? 에 대한 답을 하기 위한 논문이다.&lt;/p&gt;
&lt;p&gt;Chunyuan Li et al 의 가설은 네트워크를 원래의 방대한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 차원 공간에서 최적화 하지 않고, 훨씬 더 작은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 차원을 가진 무작위 방향의 부분 공간으로 훈련을 제한하더라도 문제를 해결할 수 있을 것이라는 것이다. 곧 이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 점진적으로 늘려가며 유효한 solution 이 등장하는 시점을 intrinsic dimension &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 정의하고, 목적 함수 landsacpe 의 실질적인 복잡도 및 어려움을 나타는 척도가 된다고 설명한다. 저자는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 시점의 모델을 무작위 projection matrix &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 통해
&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 차원으로 사상하여 테스트를 진행한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;해당 실험으로 많은 문제들이 예상보다 훨씬 적은수의 차원 만으로도 해결 가능했으며, 네트워크의 Depth 나 Width 를 늘리더라도 측정된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 거의 변하지 않았음을 실험적 결과로 보였다. 즉 모델 규모가 증가하더라도 intrinsic dimension은 크게 변하지 않았으며, 이는 많은 추가 파라미터가 표현 redundancy를 제공하고 있음을 시사한다.&lt;/p&gt;
&lt;p&gt;다시 LoRA 로 돌아와서, LoRA는 full fine-tuning에서 발생하는 weight update &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 또한 low-rank 구조를 가진다고 가정하며, 이를 rank-&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; factorization으로 parameterize 한다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 fine-tune 할 때, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;←&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 정의한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;≪&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;차원을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 제한하면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;rank&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;rank&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;rank&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로, 업데이트 가중치는 반드시 low-rank 업데이트로 강제된다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-11.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;533&quot; height=&quot;607&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 무작위 Gaussian initialziation 을 하고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;는 영행렬로 초기화된다. 즉 초기 상태는  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로, 학습의 처음 상태는 base model 과 동일하다. 또한 어떤 입력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 부터 forward pass 는 다음과 같은데:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 분산은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 비례한다. 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 independent 한 rank-1 update 의 합으로 표현되기 때문이다. 따라서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 이 증가할수록 update 의 scale 이 커질 수 있으므로, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mfrac&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mfrac&gt;&lt;/math&gt;&lt;/span&gt; 를 곱해 magnitude 를 보정한다. 여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 하이퍼파라미터이다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-12.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;745&quot; height=&quot;488&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;FT&lt;/strong&gt;
모델 전체를 fine-tune 한다.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;BitFit&lt;/strong&gt;
모델의 Bias 항 만 업데이트한다.&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mtext&gt;Adapter&lt;/mtext&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;
Houlsby et al. (2019) 이 제안한 오리지널 어댑터이다.  Self-attention 모듈과 MLP 모듈 뒤에 각각 삽입된다.
&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mtext&gt;Adapter&lt;/mtext&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;
모델의 레이어들에서 출력 레이어로 갈 수록 task-specific representation 담당한다는 전제에서 상위 layer 의 adapter 를 제외하고 drop 시키는 방법론이다.&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mtext&gt;Adapter&lt;/mtext&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;
MLP 모듈과 LayerNorm 이 끝난 직후에 단 하나만 적용되는 Adapter이다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Table 2는 자연어 이해 성능을 평가하는 대표적인 벤치마크인 GLUE를 사용하여, 세 가지 서로 다른 규모의 언어 모델(RoBERTa base, RoBERTa large, DeBERTa XXL)에 다양한 adaptation 방법을 적용했을 때의 성능을 비교한 결과를 보여준다.&lt;/p&gt;
&lt;p&gt;LoRA가 전체 파라미터를 모두 업데이트하는 전통적인 Fine-Tuning 방식과 비교했을 때, 학습해야 하는 파라미터 수를 극단적으로 줄이면서도 성능은 동등하거나 오히려 더 높은 결과를 달성한다. 예를 들어 RoBERTa base 모델에서는 전체 Fine-Tuning이 약 1억 2,500만 개의 파라미터를 학습하여 평균 86.4의 성능을 기록한 반면, LoRA는 단 30만 개의 파라미터만 학습하면서도 평균 87.2의 성능을 달성하였다.&lt;/p&gt;
&lt;p&gt;RoBERTa large에서도 동일한 경향이 나타나는데, 전체 Fine-Tuning이 3억 5,500만 개의 파라미터로 평균 88.9를 기록한 반면 LoRA는 약 80만 개의 파라미터만으로 89.0을 기록하였다. 이러한 결과는 훨씬 더 큰 모델에서도 유지된다. 약 15억 개의 파라미터를 가진 DeBERTa XXL 모델에서도 전체 Fine-Tuning이 평균 91.1을 기록할 때 LoRA는 단 470만 개의 파라미터만을 사용하여 91.3의 성능을 달성하였다. 이는 LoRA가 큰 모델에서도 매우 높은 파라미터 효율성을 유지하면서 성능을 유지하거나 향상시킬 수 있음을 보여준다.&lt;/p&gt;
&lt;p&gt;또한 자연어 모델에서의 학습 관례 중 하나는 pre-trained 모델이 있을 때 MNLI 데이터셋으로 먼저 intermediate task training 를 진행 한 다음 하위 task 로 fine-tune 하는 것이다. 이는 pre-trained model 에서 바로 하위 task 로 fine-tune 하는것이 불안정하기 때문인데, Table 2의 실험에서 일부 데이터셋(MRPC, RTE, STS-B)에 대해서는 일반적인 fine-tune에서 사용하는 MNLI 을 사용한 초기화를 허용하지 않고 순수한 사전학습 모델에서 바로 학습하도록 제한하였다.&lt;/p&gt;
&lt;p&gt;이러한 조건은 LoRA에게 불리한 설정이지만, 그럼에도 불구하고 LoRA는 여전히 다른 Adapter 기반 방법들을 능가하는 성능을 보였다. 예를 들어 약 80만 개의 파라미터를 사용하는 LoRA는 평균 88.6을 기록하여, 300만 개의 파라미터를 사용하는 AdapterP(평균 88.4)와 600만 개의 파라미터를 사용하는 AdapterH(평균 87.8)보다 더 높은 성능을 달성하였다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-13.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;737&quot; height=&quot;262&quot;&gt;&lt;/p&gt;
&lt;p&gt;또한 저자는 Table 6 에서 전제한 “fine-tune 하여 찾을 수 있는 최적 가중치 업데이트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 low-rank matrix &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 근사할 수 있을 것이다” 라는 전제에 대한 실험적 결과를 보여준다.&lt;/p&gt;
&lt;p&gt;낮은 rank &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mn&gt;1,2,4,8&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서도 충분한 성능을 확인할 수 있었고, low-rank update 의 효용을 증명하였다.&lt;/p&gt;
&lt;p&gt;LoRA 는 기존 가중치에 흡수시킬 수 있는 형태의 learnable parameter 를 가지므로 배포에 용이하다는 장점과, Adapter 같은 방법론과 달리 흡수시킨 이후에는 추가적인 추론 비용 증가가 없다는 장점이 있다. 또한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 을 키울수록 full fine-tune 과 똑같아지기 때문에, 원래 모델을 학습하는 것에 수렴한다는 장점 등으로 인해 많은 개인화 문제에서 채택되고있다.&lt;/p&gt;
&lt;h1 id=&quot;1-introduction&quot;&gt;1. Introduction&lt;/h1&gt;
&lt;p&gt;Appearance-based Gaze Estimation 은 얼굴 이미지나 눈 영역의 이미지로부터 사람이 바라보는 방향을 추론하는 기술이다. 이 기술은 인간-컴퓨터 간 상호작용 등 다양한 분야에서 이용되고 있으며 시선 기반 응사소통 시스템에서는 이 기술이 필수적이다.&lt;/p&gt;
&lt;p&gt;Gaze Estimation 기술은 통제된 환경에서는 우수한 성능을 보이지만 실제 환경에서는 그렇지 않을 수 있다. 사용자 간 차이, 카메라 세팅, 조명, 머리 자세 등의 환경적 요인이 Train 과 Test(배포) 간 불일치를 야기한다. 이런 도메인 변화는 모델이 Train 조건을 벗어난 상황에 대해 모델이 Robust 하지 못하게 한다.&lt;/p&gt;
&lt;p&gt;Test-Time Personalization 은 Unsupervised Domain Adaption 의 한 변형이다. 이는 Test 상황에서 새로운 사용자에 대한 unlabeled 데이터로 모델을 적응시키는 방법론이다. 이는 Few-shot 환경에서 on-device adaptation 을 가능하게 하며 labeled 데이터가 필요하지 않는 점이 이점으로 작용한다.&lt;/p&gt;
&lt;p&gt;사람의 얼굴은 공간적으로 일관된 패턴(눈, 얼굴의 기하학적 구조)을 보일 수 있고, 충분한 학습을 거치면 모델은 이런 패턴에 대응하는 일반적 특정을 포착하고 가중치 내부의 공간적 구조로 인코딩한다. (feature extraction) 그너라 이런 패턴의 대한 작은 변화만으로도 예측 정확도는 크게 떨어질 수 있다. fine-tune 은 이 문제를 완화하는데에 도움이 될 수 있지만, 사전 학습된 모델로부터 더 많은 정보를 활용한다면 성능을 더욱 향상시킬 수 있다.&lt;/p&gt;
&lt;p&gt;저자는 완전히 새로운 feature 를 학습하는 대신 기존 feature 를 활용하는 것이 Gaze Estimation Task 에서 데이터가 제한된 상황 (few-shot) 에서 효과적임을 확인했다.&lt;/p&gt;
&lt;p&gt;본 논문에서는 사전 학습된 filter (CNN kernel) 에 인코딩된 공간적 구조를 reweighting 하므로써 Gaze Estimation 모델을 적응시키는 Attentive Low-Rank Filter Adaptation (Alfa) 을 제안한다.&lt;/p&gt;
&lt;p&gt;Alfa 는 사전학습된 Weight 에 대해 SVD 를 수행하여 공통적인 패턴에 대응되는 지배적인 공간 구조를 추출한다. 즉 새로운 filter 를 학습하거나 새로운 가중치를 초기화 하는 대신 대상 사용자로부터 얻은 few-shot unlabeled 샘플을 이용하여 지배적 패턴들의 영향력을 조절한다. 이를 통해 원래 학습된 representation 과 alignment 는 유지하면서도 최소한의 few-shot 데이터만으로 personalized prediction 이 가능해진다.&lt;/p&gt;
&lt;p&gt;이후 서술할 시각화 결과는 Alfa 가 사용자마다의 특징인 눈꺼풀과 같은 직관적이고 국소적인 공통 영역을 추가 가중하는 것을 확인할 수 있다.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Main Contribution&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Attentive Low-Rank Filter Adaptation(Alfa)은 가중치를 비구조적인 텐서로 취급하는 대신, SVD로부터 도출된 구조화된 공간 패턴에 주의를 기울이는 방식으로 시선 추정 모델을 적응시킨다.&lt;/li&gt;
&lt;li&gt;Alfa 내의 멀티헤드 Low-Rank 적응 모듈은 미세 조정 시 확장 가능한 개인화 용량을 제공한다. 사전 학습된 가중치를 truncated SVD 형태로 저장함으로써 모델 크기를 줄일 수 있으며, 배포 시 모델 크기를 증가시키지 않고도 Alfa의 업데이트를 완전히 병합 가능하게 한다.&lt;/li&gt;
&lt;li&gt;소수의 비라벨 테스트 시점 샘플만을 사용하여 네 개의 교차 도메인 시선 추정 벤치마크에서 Alfa가 기존 방법들을 능가함을 실증적으로 보인다.&lt;/li&gt;
&lt;li&gt;Alfa의 구조화된 적응 기법을 확산 기반 대규모 언어 모델(LLM)로 확장하여, 여러 벤치마크 전반에서 제로샷 추론 성능이 향상됨을 보인다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&quot;2-related-work&quot;&gt;2. Related Work&lt;/h1&gt;
&lt;h2 id=&quot;21-test-time-personalization-ttp&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#21-test-time-personalization-ttp&quot; aria-label=&quot;Link to “2.1 Test-Time Personalization (TTP)”&quot;&gt;2.1 Test-Time Personalization (TTP)&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;도메인 변화에 대한 일반화를 하기 위해 Gaze estimation 모델들은 Unsupervised Domain Adaptation 을 채택했다. 표준적인 UDA 설정에서는 labeled source-domain 데이터, unlabeled target-domain 데이터를 함께 사용한다.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://openaccess.thecvf.com/content/ICCV2021/papers/Liu_Generalizing_Gaze_Estimation_With_Outlier-Guided_Collaborative_Adaptation_ICCV_2021_paper.pdf&quot;&gt;PnP-GA&lt;/a&gt; 는 소수의 source-domain 데이터와 unlabeled target-domain 데이터를 사용하며 특정 모델이 타겟 도메인의 노이즈에 과적합될 수 있다는 불안정성을 다수 모델의 통계적 합의로 해결하려 한다. 이 아키텍처는 소스 데이터로 사전 학습된 여러 개의 모델을 하나의 앙상블 그룹으로 묶고, 이들이 타겟 데이터를 바라볼 때 내놓는 예측값들의 분포를 분석한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-14.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;992&quot; height=&quot;543&quot;&gt;&lt;/p&gt;
&lt;p&gt;outlier-guided loss 는 batch 단위에서, 시선 예측값 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 얼마나 평균으로 부터 떨어져있는지를 임계점 기준으로 판단하여 loss 에 패널티를 주는 방법론이며, annotation 자체가 정확하지 않을 수 있는 gaze estimation task 에서 효과를 어느정도 입증했다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-15.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;381&quot; height=&quot;327&quot;&gt;&lt;/p&gt;
&lt;p&gt;반면 RUDA는 통계적 합의 대신, 3차원 공간에서 시선 벡터가 가져야 하는 ‘물리적 불변성’에 집중한다. 사람의 얼굴 이미지를 특정 각도 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;R&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;만큼 회전시킨다면, 그 내부의 시선 방향 역시 동일한 기하학적 회전을 따라야 한다는 법칙을 이용하는 것이다.&lt;/p&gt;
&lt;p&gt;RUDA는 타겟 도메인의 unlabeled 이미지를 인위적으로 회전시켜 증강 집합을 만들고, 원본 이미지의 예측값과 회전된 이미지의 예측값 사이의 상대적 차이가 가해진 회전량과 일치하는지를 검증한다. 이를 Rotation Consistency 이라 정의하며, 역회전 모듈을 통해 복원된 시선 벡터들이 하나의 일관된 방향으로 수렴하도록 Distribution Loss를 적용한다. 예측값들의 표준편차를 최소화함으로써, 모델이 이미지의 회전이나 왜곡에 상관없이 안구의 기하학적 특징만을 추론하도록 강제한다.&lt;/p&gt;
&lt;p&gt;Few-shot Adaptive Gaze Estimation 은 소수의 개인별 labeled 데이터를 이용해 fine-tune 하지만, labeled data 를 얻는 과정이 강제되며 비현실적인 경우가 많다.&lt;/p&gt;
&lt;p&gt;TTP 는 추론 단계에서 소수의 unlabeled 개인 샘플만을 이용하여 각 사용자에 맞게 모델을 적응시킴으로써 이러한 문제를 해결한다. TPP 는 사용자 외형이 다양하고 전체 모델을 업데이트 하기에는 자원 부담이 큰 온디바이스 환경에 특히 적합하다. 이러한 제약들은 labeled 데이터나 source-domain 접근 없이도 personalization 을 지원할 수 있는 효율적이고 경량화된 adaptation 방법론이다.&lt;/p&gt;
&lt;p&gt;저자가 이 논문에서 제안하는 Alfa 는 추가적인 추론 비용 없이 TTP 를 지원하며, fine-tune 단계에서 multi-head adaptation 을 통해 확장하고 SVD 를 이용해 사중치의 핵심적인 성분을 선택함으로써 효율적인 모델을 구현한다.&lt;/p&gt;
&lt;h2 id=&quot;22-low-rank-adaptation&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#22-low-rank-adaptation&quot; aria-label=&quot;Link to “2.2 Low-Rank Adaptation”&quot;&gt;2.2 Low-Rank Adaptation&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/2106.09685&quot;&gt;LoRA: Low-Rank Adaptation of Large Language Models&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Low-Rank Adaptation 은 사전 학습된 모델의 원래 가중치를 변경하지 않고, 학습 가능한 작응 행렬을 삽입 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;) 하여 fine-tune 하는 기법이다. LoRA 는 기존 층에 rank 가 제한된 업데이트를 추가하며, 다양한 task 에서 우수한 성능을 보인다.&lt;/p&gt;
&lt;p&gt;기존 fine-tune 방법론 중 하나인 Adapter 에서는 모델에 adaptation 을 위한 레이어를 추가해 inference latency 가 큰 단점이었고, 얼마나 큰 Adapter Layer 를 두냐에 따라 파라미터 크기가 크게 늘어날 수 있었다.&lt;/p&gt;
&lt;p&gt;LoRA 의 목적은 성능은 유지하면서 Task 별로 저장해야하는 파라미터와 학습 시 메모리를 크게 줄이고, inference latency 도 유지하는 방향으로 연구되었다.&lt;/p&gt;
&lt;p&gt;LoRA 의 핵심 아이디어는 업데이트 될 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 low-rank 일 수 있다는 가정이다. 그래서 전체 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 직접 학습하는게 아닌 업데이트될 low-rank 가중치만 학습한다.&lt;/p&gt;
&lt;p&gt;만약 Linear Layer 의 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 fine-tune 할 때, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;←&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 정의한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;≪&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 은 하이퍼 파라미터로, 몇 rank 까지 fine-tune 할건지를 의미한다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 입력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 차원 latent subspace 로 압축하고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서는 출력 공간에서
&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 방향이 선택되는 구조이다. 일종의 bottleneck 구조라고 할 수 있다.&lt;/p&gt;
&lt;p&gt;LoRA 는 inference 시에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 합쳐서 저장할 수 있기 때문에 Adapter 와 다르게 fine-tune 했다고 해서 추가적인 연산이 필요해지지 않는다.&lt;/p&gt;
&lt;p&gt;Transformer 에서는 self-attention 에서 attention factor 를 만들기 위한 projection weights (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;) 등에 사용된다.&lt;/p&gt;
&lt;p&gt;LoRA 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; rank 만 fine-tune 해도 full fine tune 을 근사할 것이라는 전제 하에 연구되었는데, 실제로 GPT-3 같은 모델에서도 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1,2,4&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 같이 매우 작은 값에서도 유의미한 성능을 관측했으며 오히려 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 이 크면 노이즈로 동작할 수 있음도 관측되었다.&lt;/p&gt;
&lt;p&gt;본 논문에서 Related Work 로 제시하는 LoRA 를 차용한 선행 연구들은 다음과 같다:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-16.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2752&quot; height=&quot;988&quot;&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MiLoRA:&lt;/strong&gt;
SVD 로 부터 얻은 상위 주성분은 학습이 모두 진행되었다고 간주하고, Minor 한 성분들에서 finetune 을 시도한다.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;PiSSA:&lt;/strong&gt;
LoRA 의 기본 아이디어를 사용하되 상위 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 성분을을 기존 파라미터에서 추출하여 초기화 해 수렴 속도를 개선한다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;/&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;/&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;MoSLoRA:&lt;/strong&gt;
표현 능력을 향상시키기 위해 LoRA 모듈 사이에 Mixer Matrix 를 배치한다.
&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Spectral Adapter&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;&lt;/strong&gt;
&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의, 즉 singular value 의 새로운 coefficient 만 학습한다. 이는 기존에 가중치가 가지고있던 기저 방향을 유지 한 채로 특정 기저의 energy 만 업데이트한다고 볼 수 있다.
&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Σ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;T&lt;/mi&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo&gt;⊙&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;DoRA:&lt;/strong&gt;
고차원의 두꺼운 단일 LoRA 레이어를 구조화된 다수의 단일 랭크(Single-rank) 미니 전문가 구성요소들로 잘게 쪼개 앙상블한다.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;FLoRA:&lt;/strong&gt;
Shared Low-Rank core 를 통해 파라미터의 변화를 포착하기 위해 Tucker 분해를 적용한다. 터커 분해는 임의의 N차원 고차원 텐서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒳&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 데이터의 본질적 기하학 관계를 압축하고 있는 다차원 핵심 Core Tensor 하나와, 각 모드(차원 축)별 표상을 변환하는 직교 투영 행렬(Factor Matrices)들의 다중 모드 곱으로 분해하는 알고리즘이다.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;LoRA 기반의 다양한 Adaptation 전략에도 불구하고 이러한 방법론들은 가중치를 비구조적인 텐서로 취급하며 filter 에 인코딩 되어있는 공간적 구조를 간과한다.&lt;/p&gt;
&lt;p&gt;Alfa 는 가중치의 공간적 구조에 주의를 기울이고 personlization 과정에서 이를 reweighting 아여 구조 인지적 adaptation 을 가능하게 한다.&lt;/p&gt;
&lt;h1 id=&quot;3-alfa&quot;&gt;3. Alfa&lt;/h1&gt;
&lt;h2 id=&quot;31-problem-definition-and-preliminary&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#31-problem-definition-and-preliminary&quot; aria-label=&quot;Link to “3.1 Problem Definition and Preliminary”&quot;&gt;3.1 Problem Definition and Preliminary&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;저자는 Gaze estimation 을 위한 TTP 를 완전히 새로운 Task 로 정의하지 않고, UDA 계열 Task 로 재해석한다. UDA 의 목적과 마찬가지로 Alfa 의 목적도 source 도메인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에서 학습된 모델을 새로운 사용자에 해당하는소수의 unlabeled 데이터 만으로 target 도메인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒟&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 적응시키는 것이다.&lt;/p&gt;
&lt;p&gt;각 RGB 입력 이미지를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로, 그리고 매핑되는 시선 방향을 2차원 벡터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;(yaw와 pitch)로 표기한다. 소스 도메인은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;S&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;S&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/msub&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt;로 구성된 라벨된 데이터를 제공하며, target 도메인은 일반적으로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;인 소수의 비라벨 데이터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt;를 사용한다.&lt;/p&gt;
&lt;p&gt;Gaze estimation 모델에서 입력 및 출력 feature 는 각각 CNN 레이어 또는 Linear 레이어의 입력과 출력에 해당하며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-med-pad&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 표기한다. 여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-med-pad&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;는 각각 입력 및 출력 특징의 채널 수와 해상도의 높이 및 너비를 의미한다. 이러한 특징들은 사전 학습된 가중치 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;을 통해 연결된다.&lt;/p&gt;
&lt;p&gt;소수의 비라벨 샘플로부터 효율적인 adaptation 을 가능하게 하기 위해, 저자는 LoRA 를 기반으로 한 학습 가능한 Low-Rank 업데이트를 주입한다.&lt;/p&gt;
&lt;p&gt;LoRA 는 학습 가능한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 주입하는데, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이며 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;≪&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. LoRA 에서는 일반적으로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒩&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;으로 초기화된다. 그러나 이러한 초기화는 사전 학습된 가중치에 내재된 공간적, 기하학적 구조, 즉 사용자 전반에 걸친 중요한 공간적 패턴을 포착하는 구조를 간과한다.&lt;/p&gt;
&lt;p&gt;Alfa는 사전 학습된 가중치로부터 추출된 의미적 필터를 구조적으로 reweighting 함으로써 이러한 한계를 해결하고 시선 모델의 개인화를 수행한다. 후술할 SVD 기반의 분해를 도입하여 의미적 basis dictionary 를 추출하고, personalization 을 위해 의미적 성분을 선택적으로 적응시키는 구조적 reweighting 매커니즘을 도입한다.&lt;/p&gt;
&lt;h2 id=&quot;32-structured-decomposition-of-gaze-filters&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#32-structured-decomposition-of-gaze-filters&quot; aria-label=&quot;Link to “3.2 Structured Decomposition of Gaze Filters”&quot;&gt;3.2 Structured Decomposition of Gaze Filters&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Gaze estimation 은 눈과 그 주변 얼굴 영역의 해부학적 구조에 의해 형성된 구조화된 시각적 패턴에 의존한다. 개인마다 시각적 패턴의 차이가 존재하지만, 시선 위치의 변화나 주변 얼굴 근육의 작은 변화와 같이 소수의 일관된 공간적 변화 양상을 따르는 경향이 있다.&lt;/p&gt;
&lt;p&gt;이러한 일관성은 사전 학습된 가중치가 사용자 전반에 걸쳐 널리 공유되는 반복적인 공간 구조를 인코딩하고 있음을 시사한다.&lt;/p&gt;
&lt;p&gt;즉 사람마다 모두 패턴의 차이는 존재하나 모델 내부에서는 변형이 어느정도 정형화 되어있으며, 반복적으로 등장하는 공간 구조가 weight 에 인코딩 되어있다. 직관적 비유를 들자면 어떤 filter 는 눈동자 중심 패턴, 어떤 필터는 눈꺼풀 경계 등을 인코딩 할 것이며 사람마다 다른 것은 “활성화 정도” 이지 아예 새로운 패턴이 아니라는 의미이다.&lt;/p&gt;
&lt;p&gt;이 강력한 전제를 바탕으로 Alfa 는 pre-trained 모델이 가지고있는 가중치의 구조에 집중한다. 이 구조를 명시적으로 드러내기 위해 가중치 행렬에 대해 Singular Value Decomposition (SVD) 를 적용한다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이 CNN 또는 Linear 층의 가중치 행렬이라고 할 때 SVD 는 다음과 같이 계산된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/svd_d_rank.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3698&quot; height=&quot;1118&quot;&gt;&lt;/p&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 가중치 내부 기저에서 각 고유 벡터에 대한 scale factor 이고, 아래와 같은 형태이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mo&gt;⋱&lt;/mo&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0pt;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 어떤 두 scale factor &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 라면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;&gt;&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 즉 내림차순 정렬되어있다. 이는 singular value를 대응되는 고유값의 크기 순으로 정렬하기로 한 관례에 따른 것이다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;의 고유벡터들로 이루어진 행렬이며, 고유벡터의 순서는 본질적으로 임의적이므로 각 고유벡터에 대응되는 고유값 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;의 크기 순에 맞춰 정렬할 수 있다.&lt;/p&gt;
&lt;p&gt;각 행렬의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; rank truncated 표기의 정의는 다음과 같다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;
출력 projection matrix&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;
각 direction (가중치 내부 기저 고유 벡터의 importances/energies)&lt;/li&gt;
&lt;li&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;
입력 공간에서의 고유 벡터 집합&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;즉 하이퍼파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 가중치의 basis 중 제일 [energy | importance | strength] 가 높은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 rank 를 취한다는 의미이다.&lt;/p&gt;
&lt;p&gt;이는 큰 의미에서는 기존 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 소실된다고 볼 수 있으나, 언급했듯 Gaze estitmation task 에서는 사람의 얼굴이 공간적으로 일관된 패턴에 대응하는 일반적 특정을 포착하고 가중치 내부의 공간적 구조로 인코딩되어 있음을 전제로 하므로, 가중치의 내부 basis 중 몇개의 rank 만 취해도 기존 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 잘 근사한다는 것을 전제한다.&lt;/p&gt;
&lt;p&gt;오히려 상위 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;개의 rank만 유지함으로써 파라미터 수를 줄이는 동시에, pretraining 동안 중요도가 낮았던 방향에 모델이 의존하지 않도록 암묵적으로 제한하는 효과를 가진다.&lt;/p&gt;
&lt;p&gt;또한 가중치의 truncated SVD 는 아래와 같이 표현 할 수 있다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 가중치 행렬의 한 성분이 아니라, 하나의 rank-1 행렬 성분이며, 이를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;번째 rank-slice라 부른다. 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;번째 고유벡터에 대응하는 공간 방향을 의미하며, Gaze Estimation을 위해 학습된 얼굴의 주요 기하학적 패턴을 인코딩한다. 논문에서 시각화하는 slice는 바로 이 singular component 하나를 선택한 것이다.&lt;/p&gt;
&lt;p&gt;저자는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0em&quot;&gt;:&lt;/mo&gt;&lt;mo lspace=&quot;0em&quot;&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 을 정의하는데, 이를 “semantic basis dictionary” 라고 한다. 여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 dictionary 라고 하는 이유는 후술할 self-attention 부분에서 Key / Value 로 사용되기 때문이다. 즉 가중치 내부 기저 방향으로 rotation, reflection (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt;) 및 스케일링 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;) 한 행렬은 가중치 내부에서 어떤 벡터가 중요하게 작용되는지를 확인할 수 있는 lookup table 과 같은 개념이므로, Query 로 부터 탐색될 대상이 된다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 성분들은 시선 행동과 관련된 핵심적인 공간 패턴(예: 홍채 및 안와 주변 단서 등)을 포착한다. 사전 학습 동안 시선 손실을 줄이는 데 가장 자주 사용된 성분일수록 가장 높은 에너지를 갖는다. SVD는 가중치 공간의 패턴을 에너지 기준으로 순위화하므로, 이러한 주요 성분들로 절단하면 시선과 관련된 지배적인 구조를 보존할 수 있으며, 개인화를 위한 컴팩트하고 구조 인지적인 basis를 제공한다. Alfa에서는 이러한 의미적 구조가 이후 단계에서 효율적이고 개인화된 업데이트 학습을 가능하게 한다.&lt;/p&gt;
&lt;h2 id=&quot;33-personalizing-the-semantic-basis-dictionary&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#33-personalizing-the-semantic-basis-dictionary&quot; aria-label=&quot;Link to “3.3 Personalizing the Semantic Basis Dictionary”&quot;&gt;3.3 Personalizing the Semantic Basis Dictionary&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;SVD를 통해 추출된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;은 시선 추정에서 개인 전반에 걸쳐 잘 일반화되는 공간 패턴들의 컴팩트한 집합을 제공한다. 그러나 이러한 패턴들이 각 사용자의 고유한 외형적 특성을 완전히 포착하지는 못할 수 있다. Alfa는 사전 학습 과정에서 학습된 공유 구조를 유지하면서, 의미적 기저 사전의 성분들을 reweighting 하는 LoRA를 도입한다. 이 접근법은 사전 학습된 필터에 인코딩된 시선 관련 정보를 보존하면서도, 개인별 차이에 맞게 모델을 적응시킬 수 있게 한다. 모델을 개인화하기 위해, 저자는 사전 학습된 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 위에 Low-Rank 업데이트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 추가한다. 적응된 가중치는 다음과 같이 정의된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;는 Low-Rank 개인화 항으로, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 계산되며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;는 Alfa 모듈에 의해 학습되는 행렬이다.&lt;/p&gt;
&lt;h3 id=&quot;331-attending-to-semantic-basis-dictionary&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#331-attending-to-semantic-basis-dictionary&quot; aria-label=&quot;Link to “3.3.1 Attending to Semantic Basis Dictionary”&quot;&gt;3.3.1 Attending to Semantic Basis Dictionary&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-17.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;968&quot; height=&quot;516&quot;&gt;&lt;/p&gt;
&lt;p&gt;Alfa 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에 멀티헤드 어텐션 메커니즘을 적용하여 personalization 을 수행한다. 이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;  는 사전 학습으로부터 공유된 공간 패턴을 포착하며, Alfa 는 어텐션을 사용해 target 사용자와 가장 관련성이 높은 슬라이스들을 reweighting 한다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 어텐션 헤드 갯수라고 할 때, 서로 다른 헤드들은 서로 다른 rank 슬라이스에 주의를 기울여 개인 샘플이 적을 때 상호 보완적인 단서를 결합하고 드리프트(개인 샘플이 너무 적을 때, 모델 파라미터가 우연히 관측된 단서 하나에 끌려가 pre-trained 의미 구조에서 벗어나는 현상)를 줄인다.&lt;/p&gt;
&lt;p&gt;각 어텐션 헤드 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;에 대해, 쿼리 projection을 위한 Low-Rank 행렬 쌍 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;와 쿼리 back-projection 을 위한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 정의한다. 쿼리 projection은 다음과 같이 초기화된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒩&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;각 헤드는 다음과 같이 쿼리 행렬을 계산한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/atten.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2928&quot; height=&quot;838&quot;&gt;&lt;/p&gt;
&lt;p&gt;키와 값 행렬은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;로부터 직접 도출되며, 모든 어텐션 헤드 간에 공유된다. 키 행렬은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒦&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 정의되며, 값 행렬은 그 전치로서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒱&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;이다. 각 헤드에 대해, 자신의 쿼리 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를ㅈ 사용하여 Scaled dot product 을 계산한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;Attn&lt;/mtext&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒦&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;0.5em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒱&lt;/mi&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mtext&gt;Attn&lt;/mtext&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;각 출력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;는 전치된 후 모든 헤드에 대해 스택되어 다음을 이룬다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h3 id=&quot;332-integrating-multi-head-adaptation&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#332-integrating-multi-head-adaptation&quot; aria-label=&quot;Link to “3.3.2 Integrating Multi-Head Adaptation”&quot;&gt;3.3.2 Integrating Multi-Head Adaptation&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;멀티헤드 출력을 집계하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 얻은 후, 두 개의 Low-Rank 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒫&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;P&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 사용해 이를 의미 공간으로 다시 투영한다. 이들은 다음과 같이 초기화된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;tml-lrg-pad&quot;&gt;P&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∼&lt;/mo&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒩&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;P&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;개인화된 업데이트는 다음과 같이 계산된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;Alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒫&lt;/mi&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒫&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이로써 적응 과정이 완료되며, 최종적으로 Low-Rank 업데이트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;Alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;가 생성된다.&lt;/p&gt;
&lt;h3 id=&quot;333-jayden-why-attention&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#333-jayden-why-attention&quot; aria-label=&quot;Link to “3.3.3 Jayden - Why Attention?”&quot;&gt;3.3.3 Jayden - Why Attention?&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;언급했듯 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 가중치 내부의 SVD basis를 singular value로 스케일한 형태로, 가중치가 학습 과정에서 어떤 방향들을 얼마나 중요하게 사용해 왔는지를 정리해 놓은 dictionary라고 볼 수 있다.&lt;/p&gt;
&lt;p&gt;그렇다면 저자는 왜 이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 위에서 attention 메커니즘을 사용했을까? Alfa에서 attention의 목적은 새로운 의미적 방향을 학습하는 것이 아니라, 새롭게 들어온 target 사용자의 데이터가 기존 basis들 중 어떤 방향을 더 쓰고, 어떤 방향을 덜 써야 하는지를 결정하기 위함이다.&lt;/p&gt;
&lt;p&gt;이를 위해 각 head &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;에 대해 다음과 같은 query를 정의한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이때 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;는 특정 rank slice 하나를 가리키는 것이 아니라, 여러 rank slice들에 대해 “이 방향들을 이런 비율로 써라”라고 요청하는 쿼리로 이해하는 것이 정확하다. Attention score를 전개하면 다음과 같다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 중요한 점은 역할의 분리이다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 target 사용자의 데이터로부터 만들어진 가중치 조합으로, 어떤 rank slice들을 상대적으로 더 강조할지에 대한 제안(proposal)에 해당한다.&lt;/p&gt;
&lt;p&gt;Attention is All You Need 에서 Attention 의 대상은 시퀀스 내 단어 임베딩 간 서로의 관계라면, Alfa 에서는 Test-time 에서 personalization 의 대상이 되는 사람의 데이터가 만들어내는 loss 로 학습되는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 와 base model 의 prior 와의 관계를 인코딩한다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 는 기존 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 pre-training 동안 각 rank slice를 얼마나 강하게 사용해 왔는지를 나타내는 값으로, 기존 모델이 어떤 방향을 더 신뢰해 왔는지에 대한 사전 정보라고 볼 수 있다. 따라서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒦&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 새 사용자 데이터가 제안한 변화 방향을, 기존 가중치가 중요하게 사용해 온 방향에 따라 가중해서 평가한 결과라고 해석할 수 있다.&lt;/p&gt;
&lt;p&gt;이후 softmax를 적용하고 value인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; 와 결합하면 다음과 같은 출력이 얻어진다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;0.5em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이때  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 새로운 feature를 만들어내는 것이 아니라, 기존 가중치가 가지고 있던 semantic basis들을 target 사용자에 맞게 다시 섞어 놓은 결과이다. 즉, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 target 사용자의 데이터가 기존 가중치가 중요하게 여겨 온 방향들을 어떤 비율로 재사용해야 하는지를 반영한 표현이라 볼 수 있다.&lt;/p&gt;
&lt;p&gt;정리하자면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒬&lt;/mi&gt;&lt;msup&gt;&lt;mi class=&quot;mathcal&quot;&gt;𝒦&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;는 타겟 사용자의 데이터로부터 만들어진 변화 방향 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 기존 가중치의 각 basis별 strength와 곱해봄으로써, 이 변화가 기존 basis들에 대해 어떤 가중 분포를 갖는지를 평가한다. 이를 통해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msup&gt;&lt;msup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;의 조합에 따라 기존 basis들을 어떤 비율로 재가중해야 하는지를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 통해 결정한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-18.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;282&quot; height=&quot;175&quot;&gt;&lt;/p&gt;
&lt;p&gt;추상적인 비유도 들어보자. 만약 fine-tune 단계에서 사용되는 Loss 에 의해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-med-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi class=&quot;mathcal tml-lrg-pad&quot;&gt;𝒬&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 22번 성분이 작아졌다고 가정해보자. 이 22번은 사람 얼굴의 눈썹과 눈꺼풀에 대한 패턴을 인식한다고 가정한다. 즉 22번 성분이 작아졌다는 것은, 현재 target user 에게 22번 rank slice 가 베이스 모델에서 보다 낮아야 fine-tune loss 가 줄어드는 것을 의미한다.  fine-tune loss에 의해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;의 22번 계수가 감소하면, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-med-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;에서 22번의 attention weight가 감소한다. 그 결과 최종 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;에서 slice 22의 기여도가 줄어들고, 정규화 특성 때문에 다른 slice들의 상대적 비율이 증가한다.&lt;/p&gt;
&lt;p&gt;즉 기존 가중치가 가지고있던 기하학적 구조를 어느정도 유지하면서, 눈썹과 눈꺼풀이 시선 추적에 덜 중요한 특정 사용자에게 맞춰 reweighting 되는 것이다.&lt;/p&gt;
&lt;p&gt;하지만 가중치의 기하학적 구조를 완벽히 유지하고 scaling 만 하는것은 아니다. 만약 strength 만 조정한다면&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;adapt&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;diag&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1.1&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;0.9&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1.01&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;⋯&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;와 같은 형태여도 충분할 것이다. Attention 의 사용 이유는 slice 간의 상호작용 가능성 때문이다. 단순히 특정 rank slice 이 flip-consistency 에 좋지 않은 영향을 끼쳤을 때 단순히 해당 slice 의 strength 를 낮추는 것은 rank slice 간 관계를 간과하는 것이다. rank-slice 가 얼굴의 특정 부분을 인코딩 한다는 것은 어디까지나 추상적인 비유일 뿐이고, 실제로는 여러 slice 가 복합적으로 기하학적 구조를 인코딩 하므로 엄밀히 말하면 personalization 을 위해 특정 사람에게 특별히 더 반응하는 slice 를 찾아 strength 를 올리는 것이 아닌, 특정 사람에게 유리한 slice 들의 조합을 찾기 위함이다.&lt;/p&gt;
&lt;p&gt;이후 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 projection 해서 최종적인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;adapt&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 만들어내는데, 이는 각 헤드별로 다른 slice 조합을 학습할 수 있게 한다.&lt;/p&gt;
&lt;h2 id=&quot;34-fine-tuning-and-inference-with-alfa&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#34-fine-tuning-and-inference-with-alfa&quot; aria-label=&quot;Link to “3.4 Fine-tuning and Inference with Alfa”&quot;&gt;3.4 Fine-tuning and Inference with Alfa&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;341-fine-tuning&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#341-fine-tuning&quot; aria-label=&quot;Link to “3.4.1 Fine-Tuning”&quot;&gt;3.4.1 Fine-Tuning&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;사람의 얼굴은 일반적으로 좌우 대칭성을 가지며, 시선 행동은 수평 반전에 대해서도 일관성을 유지해야 한다. 이 특성을 활용하기 위해 다섯 개의 비라벨 개인 샘플 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt; (여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)을 사용하여 대칭 손실을 적용한다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;⋅&lt;/mo&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 시선 모델이라 하자.&lt;/p&gt;
&lt;p&gt;각 이미지에 대해 수평 반전된 버전 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mrow class=&quot;tml-med-pad&quot;&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;flip&lt;/mtext&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt;을 생성하고 다음과 같은 예측을 얻는다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;flip&lt;/mtext&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;I&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mrow class=&quot;tml-med-pad&quot;&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;flip&lt;/mtext&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mtext&gt;yaw&lt;/mtext&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mtext&gt;pitch&lt;/mtext&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;이다. 대칭 손실은 다음과 같이 계산된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;fine-tune&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;‖&lt;/mo&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mtext&gt;FlipYaw&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;flip&lt;/mtext&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;‖&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h3 id=&quot;342-inference&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#342-inference&quot; aria-label=&quot;Link to “3.4.2 Inference”&quot;&gt;3.4.2 Inference&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;적응 과정은 사전 학습된 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;로부터 좌측 기저 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 재사용하여 원래의 모델 구조를 보존한다. 이는 추론 시 효율적인 업데이트 병합을 가능하게 한다. 전체 적응된 가중치는 다음과 같다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;Alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;Alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;괄호 안의 합을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;adapt&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;라는 인자로 정의하면 다음을 얻는다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;adapt&lt;/mtext&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mtext&gt;where &lt;/mtext&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;adapt&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;Alfa&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;적응된 가중치는 Low-Rank 형태를 유지하여, 원래의 압축된 모델과 동일한 구조를 갖는다. 따라서 전체 가중치 행렬을 먼저 재구성하지 않고도, 우측 기저만을 갱신하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;adapt&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;을 얻을 수 있다. 반면 표준 LoRA 변형들은 Low-Rank 항 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 전체 행렬에 추가한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mi&gt;.&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 병합하려면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;의 전체 크기로 확장해야 하며, 이는 파라미터 압축의 이점을 무효화한다(예: 모델 크기가 약 200만 개에서 1,100만 개의 파라미터로 증가). &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 truncated SVD 형태 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 저장할 경우, Low-Rank 항 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;A&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 직접 병합하는 것이 불가능하다. Alfa는 SVD의 우측 인자 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;만을 업데이트하고 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;는 고정된 상태로 유지함으로써, 적응 과정에서 Low-Rank 를 유지하고 직접 병합 가능하도록 보장한다.&lt;/p&gt;
&lt;h1 id=&quot;4-experiments&quot;&gt;4. Experiments&lt;/h1&gt;
&lt;h2 id=&quot;41-datasets&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#41-datasets&quot; aria-label=&quot;Link to “4.1 Datasets”&quot;&gt;4.1 Datasets&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;시선 추정을 위해, 저자는 cross-domain 설정을 사용한다. 소스 도메인으로는 ETH-XGaze(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;) 또는 Gaze360(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;G&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;)에서 학습된 모델을 사용하고, 타깃 도메인으로는 MPIIGaze(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;)와 EyeDiap(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;)에서 평가한다. 모든 전처리는 TPGaze 를 따른다. LLM 실험의 경우, s1K 추론 데이터셋으로 미세 조정하고, GSM8K, MATH500, Countdown, Sudoku(Arel 2025)에서 제로샷 성능을 평가한다.&lt;/p&gt;
&lt;h2 id=&quot;42-experiment-setup&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#42-experiment-setup&quot; aria-label=&quot;Link to “4.2 Experiment Setup”&quot;&gt;4.2 Experiment Setup&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;시선 추정 실험에는 NVIDIA RTX 4090 한 대를 사용하고, LLM 실험에는 NVIDIA A40 GPU 두 대를 사용한다. TTP 사전 학습과 개인화 과정에서 학습률은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;로 설정한다. LLM 적응의 경우, 기본 모델로 LLaDA-8B-Instruct 를 사용하여 d1 기반 미세 조정을 따른다. 아래에서는 TTP에 대한 세부 설정을 설명한다.&lt;/p&gt;
&lt;h3 id=&quot;421-pre-training&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#421-pre-training&quot; aria-label=&quot;Link to “4.2.1 Pre-training”&quot;&gt;4.2.1 Pre-training&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;소스 도메인 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;G&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;에서 배치 크기 120으로 Adam 을 사용해 50 epochs 동안 학습한다. truncated SVD를 적용한 이후, 정보 손실을 완화하기 위해 동일한 데이터에서 모든 파라미터를 추가로 25 epochs 동안 fine-tune 하여, truncated  사전 학습 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 얻는다.&lt;/p&gt;
&lt;p&gt;즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 상위 에너지의 basis 만 남겨버리면 하위 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 개의 rank slice 가 없어지므로, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 truncated 시킨 이후에 25 epochs 동안 추가로 train 을 하여 truncated 모델이 source domain 에서 계속 잘 동작호도록 유도한다.&lt;/p&gt;
&lt;h3 id=&quot;422-personalization&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#422-personalization&quot; aria-label=&quot;Link to “4.2.2 Personalization”&quot;&gt;4.2.2 Personalization&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;TPGaze 를 따라 각 사용자당 처음 5장의 이미지를 사용한다. 각 이미지는 한 번씩 반복되며 ColorJitter, GaussianBlur, RandomAffine로 증강된다. ResNet-18의 layer3와 layer4에는 학습률을 10배로 설정한 AdamW을 사용하고, 공정한 비교를 위해 모든 LoRA 변형에 동일한 미세 조정 방식을 적용한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-19.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;850&quot; height=&quot;399&quot;&gt;&lt;/p&gt;
&lt;p&gt;여기서 layer3, 4 에 learning-rate 를 강하게 주는 이유는 TTP 의 상황에서 데이터가 극도로 적고, overfitting 위험이 있으므로 공통 구조는 유지하고, 사용자 특이적 high-level mapping만 조정하기 위해 gaze 와 더 직접적으로 연결된 고수준 표현 레이어를 더 적극적으로 훈련한다.&lt;/p&gt;
&lt;p&gt;또한 pre-training 시에 Adam 을 사용하는 것과 다르게 Presonalization 시 AdamW 를 사용한다.&lt;/p&gt;
&lt;p&gt;먼저 Adam 의 식은 다음과 같다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msubsup&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mn class=&quot;tml-sml-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mspace width=&quot;2em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;파라미터가 업데이트 되는 과정에서 평균과 분산을 지속적으로 추적하여 다음 업데이트에 반영하는 방법인데, 만약 Alfa 의 상황처럼 데이터가 너무 적어 overfitting 을 걱정해야하는 경우 L2 regularization 을 시도할 수 있는데, 이때 Adam 은 문제가 된다.&lt;/p&gt;
&lt;p&gt;먼저 Adam 을 행렬로 바꿔서 쓰면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mpadded lspace=&quot;0&quot;&gt;&lt;mi&gt;diag&lt;/mi&gt;&lt;/mpadded&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉, 각 파라미터마다 다른 스케일이 붙는다. 이는 자주 흔들리는 업데이트가 있었던 파라미터에겐 작은 기울기를 주고 비교적 흔들림이 없던 업데이트가 있었던 파라미터에게 상대적으로 큰 기울기를 주기 위함이다. → 수렴하고 있는 방향을 계속 진행하도록 유도한다.&lt;/p&gt;
&lt;p&gt;여기서 L2 term 을 넣으면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-sml-pad&quot;&gt;′&lt;/mo&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 파라미터의 L2 term 과 분산 행렬   &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 곱해지며 파라미터 weight decay 가 모든 파라미터에 대해 동일하게 적용해야 하는데 그것이 깨져버린다.&lt;/p&gt;
&lt;p&gt;그래서 AdamW는:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;η&lt;/mi&gt;&lt;mi&gt;λ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;L2 를 별개의 항으로 두어 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;D&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 행렬과 무관하게 regularization 한다.&lt;/p&gt;
&lt;h3 id=&quot;423-evaluation-metric&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#423-evaluation-metric&quot; aria-label=&quot;Link to “4.2.3 Evaluation Metric”&quot;&gt;4.2.3 Evaluation Metric&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;저자는 예측된 시선 방향과 실제 시선 방향 사이의 각도를 측정하는 각도 시선 오차를 보고한다. 기존 연구들을 따라, 모델 출력인 2차원 yaw와 pitch를 3차원 벡터로 변환하여 오차를 계산한다.&lt;/p&gt;
&lt;h2 id=&quot;43-comparison-with-sota-methods&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#43-comparison-with-sota-methods&quot; aria-label=&quot;Link to “4.3 Comparison with SOTA Methods”&quot;&gt;4.3 Comparison with SOTA Methods&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-20.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1116&quot; height=&quot;295&quot;&gt;&lt;/p&gt;
&lt;p&gt;Table 1은 네 개의 교차 도메인 시선 추정 벤치마크에서 Alfa를 최신 TTP 방법들과 비교한 결과를 보여준다. 저자는 ETH-XGaze 또는 Gaze360에서 MPIIGaze와 EyeDiap으로의 Domain Adaptation 을 평가한다. 기준선은 fine-tune 을 수행하지 않은 ResNet-18 모델이다. 공정한 비교를 위해 backbone으로 역시 ResNet-18을 사용한다. Alfa는 모든 벤치마크에서 가장 낮은 평균 각도 시선 오차를 달성하면서도, 다른 방법들에 비해 모델 크기가 약 5배 작게 유지된다.&lt;/p&gt;
&lt;h2 id=&quot;44-comparison-with-lora-based-methods&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#44-comparison-with-lora-based-methods&quot; aria-label=&quot;Link to “4.4 Comparison with LoRA-based Methods”&quot;&gt;4.4 Comparison with LoRA-based Methods&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-21.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1138&quot; height=&quot;361&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-22.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1002&quot; height=&quot;464&quot;&gt;&lt;/p&gt;
&lt;p&gt;Table 2에서는 Alfa를 다른 LoRA 기반 방법들과 비교하고, Figure 3에서는 이들의 구조적 차이를 설명한다. 모든 방법은 동일한 truncated 사전 학습 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 위에서 동작한다. 그러나 모든 LoRA 기반 변형이 추론 시 이러한 분해된 형태의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;에 업데이트를 병합하는 것을 지원하지는 않는다. 반면 Alfa는 사전 학습된 가중치의 공간적 구조를 활용하고, 의미적으로 중요한 패턴을 재가중한다. Alfa의 개인화된 업데이트는 truncated SVD 형태와 완전히 호환되며, 다른 방법들에 비해 추론 시 추가적인 비용을 발생시키지 않는다. 이러한 시선 특화 구조적 사전 지식은 사전 학습된 시맨틱을 훼손하지 않으면서 의미 있는 필터 재가중 방향으로 적응을 유도하며, 그 결과 네 개의 교차 도메인 벤치마크 전반에서 가장 낮은 평균 시선 오차를 달성한다.&lt;/p&gt;
&lt;h2 id=&quot;45-ablation-studies&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#45-ablation-studies&quot; aria-label=&quot;Link to “4.5 Ablation Studies”&quot;&gt;4.5 Ablation Studies&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-23.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;707&quot; height=&quot;429&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 어텐션 헤드 수와 LoRA 랭크가 Alfa의 성능에 미치는 영향을 평가하기 위해 제거 실험을 수행한다. 모든 설정에서 SVD 랭크는 64로 고정한다. Table 4에 나타난 바와 같이, 어텐션 헤드 수를 증가시키면 일반적으로 개인화 성능이 향상되며, 시선 오차가 1개 헤드에서는 6.20에서 16개 헤드에서는 5.72로 감소한다. Alfa는 사전 학습된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;로부터 동일한 좌측 기저 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 재사용하므로, 모든 적응은 추론 시 기본 가중치에 병합되며 헤드 수와 무관하게 추가적인 계산 비용이 발생하지 않는다.&lt;/p&gt;
&lt;h2 id=&quot;46-visualization-of-pre-trained-spatial-patterns&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#46-visualization-of-pre-trained-spatial-patterns&quot; aria-label=&quot;Link to “4.6 Visualization of Pre-trained Spatial Patterns”&quot;&gt;4.6 Visualization of Pre-trained Spatial Patterns&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-24.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;955&quot; height=&quot;1015&quot;&gt;&lt;/p&gt;
&lt;p&gt;Figure 4는 ETH-XGaze로 학습된 ResNet-18 모델의 SVD 분해 가중치에서 개별 랭크 슬라이스를 시각화함으로써 사전학습 동안 인코딩된 일부 공간 패턴을 보여준다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;번째 SVD 슬라이스는 다음과 같이 계산된다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;는 분해의 랭크 구성요소를 인덱싱한다. 왼쪽 열은 해당 슬라이스에 의해 인코딩된 공간 구조를 나타내며, 가운데와 오른쪽 열은 서로 다른 두 피험자(피험자 0과 피험자 16)에 대한 활성화를 보여준다. 예를 들어, 22번째 슬라이스는 눈썹, 아래 눈꺼풀, 그리고 아래 입 부분을 강조하는 반면, 24번째 슬라이스는 코 옆 부분, 눈 옆 영역, 그리고 입 주변의 얼굴 근육 부위에서 활성화된다. 개인 간에 일관된 패턴은 사전학습이 시선과 관련된 얼굴 기하 구조에 정렬된 재사용 가능한 공간 구조를 포착함을 보여준다.&lt;/p&gt;
&lt;h2 id=&quot;47-visualization-of-adaptation-behavior&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#47-visualization-of-adaptation-behavior&quot; aria-label=&quot;Link to “4.7 Visualization of Adaptation Behavior”&quot;&gt;4.7 Visualization of Adaptation Behavior&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-25.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;535&quot; height=&quot;676&quot;&gt;&lt;/p&gt;
&lt;p&gt;Figure 5에서는 MPIIGaze 데이터셋의 세 명 사용자에 대해 Low-Rank 업데이트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 시각화한다. Alfa의 업데이트는 눈과 그 주변 근육과 같은 시선과 관련된 얼굴 영역에 일관되게 집중한다. 이러한 패턴은 사용자마다 약간씩 차이를 보이는데, 이는 모델의 원래 공간적 sementic 과의 정합성을 유지하면서도 개인화된 조정을 반영한 결과이다.&lt;/p&gt;
&lt;p&gt;반면 LoRA는 동일한 백본 계층을 사용해 개인화를 수행하더라도, 분산되고 일관되지 않은 업데이트를 생성한다. 이는 LoRA가 핵심 영역을 명시적으로 회피하지 않기 때문이며, 목표 지향적인 semantic 가이드를 결여한 채 다른 영역의 시선 관련 신호(예: pose cues)에 의존할 수 있다.&lt;/p&gt;
&lt;p&gt;이러한 시각화 결과는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mtext&gt;base&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 으로부터 유용한 성분을 식별하는 Alfa의 능력을 강조한다. 이 성분들은 사전 학습된 소스 모델과 타깃 사용자 간의 도메인 특화 불일치를 반영하므로, 효과적인 사용자별 적응을 위해서는 이들을 재가중하는 것만으로 충분하다.&lt;/p&gt;
&lt;h2 id=&quot;48-applying-alfa-to-diffusion-based-llms&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#48-applying-alfa-to-diffusion-based-llms&quot; aria-label=&quot;Link to “4.8 Applying Alfa to Diffusion-Based LLMs”&quot;&gt;4.8 Applying Alfa to Diffusion-Based LLMs&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-26.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1012&quot; height=&quot;215&quot;&gt;&lt;/p&gt;
&lt;p&gt;LLaDA-8B-Instruct 모델에 Alfa를 적용했을 때의 성능을, 네 가지 제로샷 추론 벤치마크(GSM8K, MATH500, Countdown, Sudoku)에서 LoRA 및 DoRA와 비교한 결과를 보여준다. 적응에는 1,000개의 샘플을 사용한다. LLM 실험을 포함한 이유는 추론 과제가 토큰 상호작용 패턴(예: 포맷, 단계 표시)을 재사용하며, 데이터가 제한된 경우 이러한 패턴을 재가중하는 것이 유리함이 알려져 있기 때문이다. LoRA 결과는 d1에서 가져왔으며, DoRA는 HuggingFace PEFT 라이브러리를 사용해 재구현하였다.&lt;/p&gt;
&lt;p&gt;LoRA와 DoRA는 LoRA 랭크 128을 사용하는 반면, Alfa는 더 낮은 랭크인 64를 사용하며 전체 모델 파라미터의 0.85%만을 튜닝한다. 표 3의 모든 실험에서는 SVD 절단 없이 전체 사전 학습 가중치 행렬 W를 유지한다. Alfa는 저랭크 업데이트 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi mathvariant=&quot;normal&quot;&gt;Δ&lt;/mi&gt;&lt;mspace&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 계산하기 위해 SVD 랭크 128과 8개의 어텐션 헤드를 사용한다. 이러한 더 작은 파라미터 규모에도 불구하고, Alfa는 전반적인 벤치마크에서 동등하거나 더 우수한 정확도를 달성한다. 이는 언어 모델의 추론 패턴 역시 사전 학습 과정에서 인코딩된 일반화 가능한 성분으로 표현될 수 있음을 시사하며, 향후 연구를 위한 흥미로운 가능성을 제시한다.&lt;/p&gt;
&lt;h1 id=&quot;5-conclusion&quot;&gt;5. Conclusion&lt;/h1&gt;
&lt;p&gt;본 논문에서는 시선 추정 모델의 테스트 시점 개인화를 위한 구조 인지형 방법인 Alfa를 제안하였다. Alfa는 SVD를 통해 추출된 공간 패턴에 어텐션을 적용함으로써, 사전 학습된 필터에 내재된 의미 있는 성분을 재사용하고 멀티헤드 Low-Rank 설계를 통해 효율적인 도메인 적응을 가능하게 한다. 이 접근법은 미세 조정 과정에서 확장 가능한 개인화를 지원하는 동시에, 추론 비용을 증가시키지 않으면서 컴팩트한 모델을 유지한다. 네 개의 교차 도메인 시선 추정 벤치마크 실험을 통해, 소수의 비라벨 샘플만으로도 최신 수준의 성능을 달성함을 입증하였다. 더 나아가, Alfa의 구조화된 적응 기법은 확산 기반 언어 모델의 제로샷 추론 과제와 같은 다른 응용 분야에서도 잠재력을 보인다.&lt;/p&gt;
&lt;h1 id=&quot;6-limitation&quot;&gt;6. Limitation&lt;/h1&gt;
&lt;h2 id=&quot;61-fine-tuning-in-on-device-environment&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#61-fine-tuning-in-on-device-environment&quot; aria-label=&quot;Link to “6.1 Fine-tuning in on-device environment”&quot;&gt;6.1 Fine-tuning in on-device environment&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;on-device 환경에서의 fine-tuning은 여전히 도전적인 문제이다. 특히 Alfa는 truncated SVD와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 재사용을 통해 저장해야 하는 파라미터 수를 줄이지만, 이는 compute efficiency를 직접적으로 보장하지는 않는다. 실제로 factorized parameterization은 추가적인 projection 연산 및 decomposition 과정으로 인해 training-time computational overhead를 유발할 가능성이 있다.&lt;/p&gt;
&lt;p&gt;Experiments의 Table에서도 확인되듯이, parameter efficiency와 training-time compute efficiency 사이에는 trade-off가 존재하는 것으로 보인다. 특히 truncated SVD의 rank &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;, LoRA rank &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;, 그리고 attention head 수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;가 증가할수록 fine-tuning 단계에서 요구되는 연산량 또한 증가할 수 있다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260302/image-21.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1138&quot; height=&quot;361&quot;&gt;&lt;/p&gt;
&lt;p&gt;따라서 inference 단계에서는 backbone과 유사한 compute cost를 유지할 가능성이 있으나, on-device 환경에서 fine-tuning 자체의 실용성은 추가적인 분석이 필요해 보인다.&lt;/p&gt;
&lt;h2 id=&quot;62-flip-consistency-gaze-estimation-accuracy&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#62-flip-consistency-gaze-estimation-accuracy&quot; aria-label=&quot;Link to “6.2 Flip-consistency ≠ Gaze estimation Accuracy”&quot;&gt;6.2 Flip-consistency ≠ Gaze estimation Accuracy&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Alfa는 개인화 단계에서 다음과 같은 loss를 사용한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi class=&quot;mathcal&quot;&gt;ℒ&lt;/mi&gt;&lt;mtext&gt;fine-tune&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;‖&lt;/mo&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mtext&gt;FlipYaw&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mover&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;flip&lt;/mtext&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;‖&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉, 원본 이미지와 수평 반전 이미지의 예측이 좌우 대칭적으로 일관되도록 만드는 것이 목표이다. 그러나 실제 성능 평가는 ground-truth 3D gaze vector와의 angular error이다. 이 둘은 동일한 목적이 아니다. 모델이 자기 자신과 일관되다고 해서, 정답에 가까워진다는 보장은 없다.&lt;/p&gt;
&lt;p&gt;모델이 특정 사용자에 대해 yaw를 항상 +5° 만큼 예측한다고 가정하자.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;원본 예측: +15° (실제는 +10°)&lt;/li&gt;
&lt;li&gt;flip 이미지 예측: -15° (실제는 -10°)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;FlipYaw 처리 후 비교하면 perfectly consistent하며 loss 는 거의 0이 된다. 즉 bias는 줄지 않으면서 flip-consistency만 만족하는 해가 존재한다.&lt;/p&gt;
&lt;p&gt;또한 Alfa는 인간 얼굴의 좌우 대칭성을 전제하나 이 전제가 틀릴 가능성이 있다. 조명 비대칭, 안경/머리카락 등의 방해 요인, 얼굴 자체의 비대칭, 사시 등의 경우, flip-consistency를 강제하면 오히려 잘 학습된 비대칭 정보를 제거할 위험이 있다.&lt;/p&gt;
&lt;h2 id=&quot;63-the-main-contributor-to-accuracy-improvement&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#63-the-main-contributor-to-accuracy-improvement&quot; aria-label=&quot;Link to “6.3 The main contributor to accuracy improvement”&quot;&gt;6.3 The main contributor to accuracy improvement&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;논문에서 보고된 성능 향상은 multi-head attention, SVD 기반 structured basis reuse, low-rank constraint, 그리고 flip-consistency loss가 결합된 결과이다. 그러나 실험 설계 상 이 네 요소의 기여도가 명확히 분리되어 있지 않으며, 어떤 요인이 실제 성능 향상의 핵심 동인인지 인과적으로 검증되지 않았다.&lt;/p&gt;
&lt;p&gt;Alfa는 기존 LoRA 기반 방법들과의 차별점으로 “semantic basis 위에서의 attention 기반 재가중”을 강조한다. 하지만 제시된 비교 실험은 전체 시스템 단위의 비교에 머물러 있으며, attention 모듈의 필수성을 직접적으로 입증하지는 않는다. 예를 들어, attention을 제거하고 단순한 선형 재가중 또는 diagonal gating만 적용했을 때의 성능, 혹은 softmax를 제거한 단순 저랭크 변환과의 비교는 제시되지 않는다. 따라서 현재 결과만으로는 attention이 구조적으로 필요한 요소였는지, 아니면 단지 표현력 증가에 따른 일반적 성능 향상인지 구분하기 어렵다.&lt;/p&gt;
&lt;p&gt;성능 개선이 attention 때문이 아니라 SVD truncation 자체의 regularization 효과, low-rank 제약에 따른 안정성 향상, 또는 flip-consistency loss의 self-supervised alignment 효과에서 비롯되었을 가능성도 배제되지 않는다.&lt;/p&gt;
&lt;p&gt;또한 multi-head 구조는 rank 증가 효과와 표현력 증가를 동반한다. 그러나 이 증가가 attention 메커니즘의 질적 장점 때문인지, 단순히 파라미터 재배치에 따른 양적 효과인지 분리되지 않았다. attention이라는 연산적으로 복잡한 구조가 정말 필요했는가? 혹은 단순한 저랭크 재가중 방식으로도 동일한 성능을 달성할 수 있었던 것은 아닌가?&lt;/p&gt;
&lt;p&gt;결과적으로, Alfa의 성능 향상은 여러 설계 요소가 결합된 종합 효과로 보이지만, 각 구성 요소의 인과적 기여는 충분히 분해되지 않았다. 따라서 “attentive structured reweighting이 핵심이다”라는 해석은 현재 제시된 실험만으로는 강하게 뒷받침되기 어렵다. 설계의 설득력을 강화하기 위해서는 attention 제거 ablation, flip-consistency 제거 실험, SVD 없이 동일 구조 적용 실험 등 구성 요소별 분리 검증이 필요하다. 이러한 분석 없이는 attention 기반 구조가 필연적인 선택이었는지, 아니면 여러 가능한 설계 중 하나였는지 판단하기 어렵다.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Attention is All You Need</title>
    <link href="https://bayernmuller.github.io/posts/260114-attention-is-all-you-need/" />
    <id>https://bayernmuller.github.io/posts/260114-attention-is-all-you-need/</id>
    <published>2026-01-14T00:00:00.000Z</published>
    <updated>2026-01-14T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="Transformer" /><category term="Machine Learning" /><category term="ML" /><category term="Korean" />
    <summary>논문 리뷰: Attention is All You Need</summary>
    <content type="html">&lt;h1 id=&quot;backgrounds-1&quot;&gt;Backgrounds&lt;/h1&gt;
&lt;h2 id=&quot;layer-normalization&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#layer-normalization&quot; aria-label=&quot;Link to “Layer Normalization”&quot;&gt;Layer Normalization&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Layer Normalization (LayerNorm)은 한 샘플 내부에서 feature 차원 기준으로 정규화하는 기법이다. 배치 크기와 무관하게 동작하도록 설계되었다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;μ&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;2em&quot;&gt;&lt;/mspace&gt;&lt;msup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;μ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;μ&lt;/mi&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;/mrow&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;γ&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;Transformer에서 한 토큰의 representation은 보통 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이고, LayerNorm은 각 토큰마다 independently 적용된다. 이는 attention 출력의 scale 폭주를 방지하여 학습 안정성이 증가한다.&lt;/p&gt;
&lt;h2 id=&quot;recurrent-neural-network&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#recurrent-neural-network&quot; aria-label=&quot;Link to “Recurrent Neural Network”&quot;&gt;Recurrent Neural Network&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;RNN 은 단일 입력이 아닌 여러개의 입력 (시퀀스) 에서, 이전 상태와 현재 입력으로 새로운 상태를 만드는 순환 연산 블록이다.&lt;/p&gt;
&lt;p&gt;길이가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 인 시퀀스 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 있을 때, RNN 의 연산을 아래와 같이 정의된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 같은 시간 스텝 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서는 같은 파리미터 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;) 를 공유한다.&lt;/p&gt;
&lt;p&gt;hidden state &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 굳이 만드는 이유는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/math&gt;&lt;/span&gt; ~ &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;  범위의 데이터를 모두 참조하려면 계산 비용과 메모리를 많이 요구하기 때문이며 길이가 가변인 시퀀스를 고정 차원으로 처리하기 위한 구조적 장치로 사용하기 위함이다. 즉 이전의 상태를 하나의 벡터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 표현하는 것이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.22em&quot; rspace=&quot;0.22em&quot; stretchy=&quot;false&quot;&gt;|&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.22em&quot; rspace=&quot;0.22em&quot; stretchy=&quot;false&quot;&gt;|&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;각 시퀀스에서 계산된 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 사용하여 출력을 만들 수도 있다. 최종 상태 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 시퀀스 전체를 표현하는 벡터이며, 이를 이용해 출력을 하나만 만들 수도 있고, 중간 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 이용하여 출력을 시퀀스로 만들 수도 있다.&lt;/p&gt;
&lt;p&gt;이러한 RNN 구조는 입력과 출력 시퀀스의 길이가 동일한 문제에는 자연스럽게 적용되지만, 기계 번역과 같이 입력 시퀀스의 길이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;와 출력 시퀀스의 길이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;가 다른 문제에는 직접 적용하기 어렵다. 이를 해결하기 위해 이후 Encoder–Decoder 구조가 제안되었다.&lt;/p&gt;
&lt;h2 id=&quot;encoder-decoder&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#encoder-decoder&quot; aria-label=&quot;Link to “Encoder-Decoder”&quot;&gt;Encoder-Decoder&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Encoder-Decoder 는 기계 번역, 요약, 질의 응답 등의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;≠&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 인 Task에서 단일 RNN 의 구조적 한계를 극복하기 위해 고안된 구조이다.&lt;/p&gt;
&lt;p&gt;기본적으로 RNN 을 두개 사용하며, 입력 시퀀스를 하나의 표현 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;) 으로 인코딩하는 Encoder 와 그 표현을 조건으로 새로운 시퀀스를 디코딩하는 Decoder 로 나뉜다.&lt;/p&gt;
&lt;p&gt;첫번쨰 RNN 이 모든 시퀀스를 전부 연산하였을 때, 해당 시퀀스는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 요약된다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/decode.png&quot; alt=&quot;decode.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3419&quot; height=&quot;1187&quot;&gt;&lt;/p&gt;
&lt;p&gt;디코더는 이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 사용하여 다음 출력의 확률을 생성한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/decoder.png&quot; alt=&quot;decoder.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3423&quot; height=&quot;1187&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 해당 출력 토큰의 embedding 값을 의미한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Embed&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;⟨&lt;/mo&gt;&lt;mtext&gt;SOS&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;⟩&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉 디코더는 인코더가 요약한 정보 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 바탕으로 출력을 생성하는데, 언어 모델에서 출력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 은 어휘 테이블 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서의 정수 토큰 인덱스를 의미한다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 또 다른 파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 와 곱해져 logit 을 만드는데, 이 logit 을 softmax 함수에 통과시키면 모든 토큰에 대한 확률 분포를 구할 수 있다. 추론 시에는 이 분포로부터 확률이 가장 높은 토큰을 선택하여 출력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 결정한다.&lt;/p&gt;
&lt;p&gt;Encoder–Decoder 구조는 Seq2Seq 문제를 가능하게 한 아키텍처이지만, 입력 시퀀스 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;를 단일 벡터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;로 요약함으로써 과도한 정보 압축이 발생하는 한계를 가진다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;FC Bayern is the best club in the world.&lt;/mtext&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;You’re right. Germany International is the best.&lt;/mtext&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;위 예시와 같이 입력 시퀀스 초반에 등장한 핵심 토큰에 대한 정보는 최종 hidden state &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;에 충분히 보존되기 어렵고, 이로 인해 디코더가 입력의 일부 의미를 제대로 반영하지 못한 출력을 생성할 수 있다.&lt;/p&gt;
&lt;p&gt;이러한 한계를 해결하기 위해 Attention 메커니즘이 제안되었다.&lt;/p&gt;
&lt;h2 id=&quot;attention-mechanisms&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#attention-mechanisms&quot; aria-label=&quot;Link to “Attention Mechanisms”&quot;&gt;Attention Mechanisms&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1409.0473&quot;&gt;Neural Machine Translation by Jointly Learning to Align and Translate&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;우선 “Neural Machine Translation by Jointly Learning to Align and Translate” 에서의 RNN 은 Bi-directional 구조이다. 양방향 RNN 이라는 뜻으로, 두개의 RNN 이 서로 다른 방향으로 진행한다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; style=&quot;math-depth:0;&quot;&gt;→&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; style=&quot;math-depth:0;&quot;&gt;→&lt;/mo&gt;&lt;/mover&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; style=&quot;math-depth:0;&quot;&gt;←&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; style=&quot;math-depth:0;&quot;&gt;←&lt;/mo&gt;&lt;/mover&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉 왼쪽 → 오른쪽은 과거의 context, 오른쪽 → 왼쪽은 미래의 문맥을 담당한다. 이는 입력 시퀀스를 서로 다른 방향으로 읽어 토큰별로 더 풍부한 표현이 가능하게 한다. 출력을 미리 예측할 수 없는 Encoder-Decoder 의 구조 상, 인코더에만 두개의 RNN 이 각 방향을 담당하며, 두 방향의 hidden states 를 concatenation 한 벡터를 annotation   &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 으로 정의한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; style=&quot;math-depth:0;&quot;&gt;→&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0.2222em&quot;&gt;:&lt;/mo&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo stretchy=&quot;true&quot; style=&quot;math-depth:0;&quot;&gt;←&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;Attention 메커니즘은 Encoder-Decoder 의 입력 시퀀스가 단일 벡터로 요약된다는 단점을 보완하기 위해 고안되었으며, 핵심 아이디어는 출력 시점 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 마다 입력 시퀀스 전체를 다시 참조하는 것이다. 즉 디코더의 입력이 하나의 벡터에서, annotation 들의 집합이 된다.&lt;/p&gt;
&lt;p&gt;Encoder RNN 이 만든 annotation 의 셋은 아래와 같이 정의된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;디코더는 각 출력을 생성할 때 아래와 같은 과정을 거친다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mtext&gt;tanh&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;U&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;exp&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mi&gt;exp&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 라는 작은 신경망을 사용하여, 현재 출력 시점 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 입력 annotation들에 대한 중요도를 확률 분포 형태 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 계산한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;각 annotation을 attention weight로 가중합하여 context vector &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 만든다.  이 식을 풀어서 설명하면, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 번째 토큰의 annotation 이 얼마나 중요한지를 의미하고, 이 값은 상수이다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원 벡터이며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;  이다.&lt;/p&gt;
&lt;p&gt;이렇게 각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 곱해진 벡터를 전부 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 방향으로 합한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/weightedsum.png&quot; alt=&quot;weightedsum.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3662&quot; height=&quot;1480&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;Embed&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이후 이전 시점의 디코더 출력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;, 디코더 상태 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;, 그리고 입력 시퀀스 전체에 대한 attention 정보가 담긴 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 이용해 다음 시점의 디코더 상태를 계산한다.&lt;/p&gt;
&lt;p&gt;즉, 모든 출력 시점 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;마다 입력 시퀀스를 요약하는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 새로 만들어서, 출력 시 사용하게 된다.&lt;/p&gt;
&lt;h2 id=&quot;adam-optimizer&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#adam-optimizer&quot; aria-label=&quot;Link to “Adam Optimizer”&quot;&gt;Adam Optimizer&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Adam(Adaptive Moment Estimation)은 loss를 감소시키는 gradient의 방향을 1차 모멘트 추정을 통해 관성적으로 유지하고, 동시에 gradient 크기의 변동성을 2차 모멘트 추정을 통해 추적함으로써, 파라미터별로 신뢰도에 기반한 업데이트 크기를 조절하는 최적화 알고리즘이다.&lt;/p&gt;
&lt;p&gt;모든 파라미터에 대해 동일한 learning rate를 적용하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 와 같이 업데이트가 이루어진다. 이는 파라미터별 gradient scale이나 변동성을 고려하지 않기 때문에, gradient noise가 크거나 파라미터 간 스케일 차이가 큰 경우 학습이 불안정해질 수 있다.&lt;/p&gt;
&lt;p&gt;Adam에서는 시점 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;에서의 파라미터와 gradient를 다음과 같이 정의한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;parameters&lt;/mtext&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;∇&lt;/mo&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;지수 가중 평균을 이용하여 기울기의 방향을 누적시킨다. 이는 gradient의 평균 방향을 누적하여 추정하는 항으로, noise를 완화하고 일관된 하강 방향을 유지하는 역할을 한다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 이 0.9 일 경우 약 10개의 최근 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 값들의 평균을 근사한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msubsup&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mn class=&quot;tml-sml-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;g&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mn class=&quot;tml-sml-pad&quot;&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/math&gt;&lt;/span&gt;는 element-wise square이다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;는 gradient 크기의 변동성을 추적하는 항으로, gradient가 얼마나 크게 흔들리는지를 나타내는 지표이며, 해당 방향에 대한 업데이트 크기를 조절하는 데 사용된다. 이는 gradient 방향의 옳고 그름을 판단하기보다는, 해당 방향의 신뢰도를 스케일링하기 위한 정보이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mspace width=&quot;2em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 모두 지수가중 평균을 사용하므로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 0에 가까우면 0에 대한 편향이 생긴다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 승을 사용하여 학습 초반에는 거의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 의 값이 사용되다가, 학습이 진행되며 편향에 대한 보정이 자연스럽게 사라지게 된다.&lt;/p&gt;
&lt;p&gt;최종적으로 Adam의 파라미터 업데이트는 다음과 같이 이루어진다:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;θ&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;wbk-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mover&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;방향은 평균적으로 이쪽인 것 같긴 한데 step마다 너무 요동친다 → 진짜 경사인지 확신 못 하겠다 → 그러면 조금만 움직이자&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h1 id=&quot;introduction-1&quot;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;RNN은 언어 모델링과 기계 번역과 같은 시퀀스 모델링 및 변환 문제에서 효과적인 방법론으로 자리를 잡아왔고, Recurrent 언어 모델과 Encoder-Decoder 구조의 한계를 확장하려는 연구들이 이루어지고 있다.&lt;/p&gt;
&lt;p&gt;Recurrent 모델은 일반적으로 시퀀스의 입력 순서에 따라 계산을 나눈다. 위치를 계산 시점으로 간주하므로써, 이전 입력에 대한 hidden state &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 와 현재 입력 시점 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 입력을 받는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 생성한다. → &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;더 나아가 Attention 메커니즘이 등장하고 시퀀스 모델의 핵심 구성요소가 되었다. 이는 기존 순환 모델이 가지고 있던 long-term dependency 표현력 문제를 어느정도 해결했으며, RNN 과 함께 사용되어 성과를 냈다.&lt;/p&gt;
&lt;p&gt;하지만 RNN 은 본질적으로 순차적인 특성을 가지며, 이는 내부에서 연산을 병렬화할 수 없음을 의미한다. 또한 역전파 시 시퀀스 전체의 hidden state &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 을 모두 저장해야 하므로 메모리 사용량은 시퀀스 길이에 비례하게 되고, GPU 의 메모리는 한정되어 있으므로 무작정 배치 크기를 키울 수 없다.&lt;/p&gt;
&lt;p&gt;저자는 이를 해결하기 위해 Recurrence 를 완전히 버리고, Attention 만으로 입-출력 간 관계, 모든 포지션의 관계, 거리에 상관 없는 연결 표현하는 새로운 모델 Transformer 를 제안한다.&lt;/p&gt;
&lt;p&gt;Transformer 는 recurrence 기반 sequence modeling 이 아니므로 모든 포지션을 동시에 계산할 수 있어 sequence 내부 병렬화가 가능하고 GPU 친화적으로 연산이 최적화 된다고 주장한다.&lt;/p&gt;
&lt;p&gt;또한 Transformer 는 BLEU 기준 SOTA 를 달성하였으며 타 모델에 비해 학습시간을 획기적으로 단축한다.&lt;/p&gt;
&lt;h1 id=&quot;related-works-1&quot;&gt;Related Works&lt;/h1&gt;
&lt;h2 id=&quot;cnn-based-sequence-models&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#cnn-based-sequence-models&quot; aria-label=&quot;Link to “CNN Based Sequence Models”&quot;&gt;CNN Based Sequence Models&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Recurrent 계산을 줄이려는 목표는 Transformer 만의 것이 아니다.  CNN 을 기본 블럭으로 사용한 Extended Neural GPU, ByteNet, ConvS2S 와 같은 연구들이 있었고, 1D convolution 연산을 통해 RNN 의 순차성을 제거하고 sequence 내부 병렬화를 목표로 했다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Embedding Dimension&lt;/mtext&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;위와 같은 입력 시퀀스에서 kernel size가 3인 1d convolution 연산은 (경계 처리를 단순화하여 표현하였다):&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이고, 의미적으로 현재 토큰 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 은 주변 토큰들과 context 를 형성하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 만들게 되고, 레이어를 쌓으며 한 토큰의 표현이 점점 더 넓은 범위의 시퀀스 정보에 의존하게 된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;하지만 CNN 기반 모델의 한계는 두 포지션 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 사이의 정보가 서로 영향을 주려면 여러개의 레이어를 필수적으로 거쳐야 하며, 거리가 멀수록 연산 단계 수(computational path length)가 증가한다는 점이다.&lt;/p&gt;
&lt;p&gt;두 포지션 간 정보 전달에 필요한 연산 단계 수를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;⋅&lt;/mo&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 으로 표기한다면, ConvS2S 에서는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이고, ByteNet 은 Dilated 1D convolution 사용, dilation 을 지수적으로 증가시켜 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이 된다.&lt;/p&gt;
&lt;p&gt;이처럼 정보 간의 연결이 거리에 따라 비례하게 되는 단점이 있고, 이는 장거리 의존성 학습을 어렵게 만들며, 모델의 최적화 난이도를 증가시킨다. Transformer 에서는 두 포지션간 정보를 연결하는데 필요한 연산 단계 수는 상수로 줄이는 것을 목표로 한다.&lt;/p&gt;
&lt;h2 id=&quot;self-attention&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#self-attention&quot; aria-label=&quot;Link to “Self-attention”&quot;&gt;Self-attention&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Self-attention(=intra-attention) 은 하나의 시퀀스 내부에서 서로 다른 포지션들 간 관계를 계산해 그 결과로 토큰별 representation 을 만드는 attention 매커니즘이다. 즉 encoder–decoder attention처럼 입력–출력 간 hidden state 참조를 모델링하는 것이 아니라, 시퀀스 내부에서 각 토큰의 관계를 표현하는 방식이다.&lt;/p&gt;
&lt;p&gt;Self-Attention 은 Transformer 이전에도 다양한 NLP 태스크에서 성공적으로 사용되어 왔으며, reading comprehension, textual entailment, sentence representation 처럼 문장 내 내부 관계가 중요하고 토큰 간 전역적 상호작용이 중요한 문제에서 특히 좋은 성능을 보였다.&lt;/p&gt;
&lt;p&gt;그러나 대부분 입력 시퀀스로부터 하나의 표현을 얻거나 분류를 수행하는 데에 초점이 맞추어져 있었고, 입력 시퀀스를 출력 시퀀스로 변환하는 일반적인 Seq2Seq(transduction) 문제를 직접적으로 다루지는 못했다. 특히 출력이 가변 길이의 시퀀스이며 이전 출력이 이후 출력에 영향을 미치는 생성 구조를 포함하지 않았다는 점에서, 기계 번역과 같은 문제에 일반적으로 적용하기에는 한계가 있었다.&lt;/p&gt;
&lt;h2 id=&quot;end-to-end-memory-networks&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#end-to-end-memory-networks&quot; aria-label=&quot;Link to “End-to-end Memory Networks”&quot;&gt;End-to-end Memory Networks&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;End-to-end 메모리 네트워크는 시퀀스 위치에 맞춰 반복되는 recurrent 가 아니라 attention 을 반복 적용하는 구조를 기반으로 한다. 즉 입력을 순서대로 읽지 않고, 같은 입력 전체에 대해 attention 만 여러번 적용한다.&lt;/p&gt;
&lt;p&gt;이런 구조는 단순한 언어 Q/A, 언어 모델링에서는 좋은 성능을 낸다는 것이 증명되었다.&lt;/p&gt;
&lt;p&gt;하지만 이러한 모델들은 attention을 여러 번 적용하는 추론 구조를 사용함에도 불구하고, 입력과 출력 시퀀스 간의 일반적인 변환을 수행하는 완전한 Seq2Seq 프레임워크로 확장되지는 않았다. 또한 attention 반복 횟수(hop 수)가 하이퍼파라미터로 고정되어 있으며, 입력과 출력 간의 명시적인 위치 정렬이나 출력 시점별 조건부 생성 구조가 부족하다는 점에서, 복잡한 시퀀스 변환 문제를 해결하는 데에는 제약이 있었다.&lt;/p&gt;
&lt;h1 id=&quot;transformer-architecture&quot;&gt;Transformer Architecture&lt;/h1&gt;
&lt;p&gt;현재 성능이 잘 나오는 Seq2Seq 모델들은 대부분 Encoder–Decoder 구조를 사용한다. 여기서 Encoder 는 입력 시퀀스 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에 대한 annotation &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 으로 매핑한다. Decoder 는 주어진 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 출력 시퀀스 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 한번에 하나씩 생성한다. 각 단계에서 모델은 auto-regressive 하며, 다음 기호 생성할 때 이전에 생성된 기호들을 추가 입력을 사용한다.&lt;/p&gt;
&lt;p&gt;Transformer 는 이 전체적인 구조를 따르며, Encoder 와 Decoder 모두에서 여러 층으로 쌓인 self-attention 과 point-wise, FC 레이어들을 사용한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;1064&quot; height=&quot;1374&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;encoder-and-decoder&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#encoder-and-decoder&quot; aria-label=&quot;Link to “Encoder and Decoder”&quot;&gt;Encoder and Decoder&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;encoder&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#encoder&quot; aria-label=&quot;Link to “Encoder”&quot;&gt;Encoder&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Encoder는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 개의 동일한 레이어를 쌓아올린 구조로 구성되며, 각 레이어는 두개의 서브 레이어를 가진다.&lt;/p&gt;
&lt;p&gt;첫번째 서브 레이어는 Multi head self-attention 매커니즘이고, 두번째는 간단한 position-wise fully connected feed-forward 네트워크이다.&lt;/p&gt;
&lt;p&gt;두 서브 레이어 각각에 대해 residual connection 을 적용하고 layer nomalization 을 적용한다. 즉 각 서브 레이어의 출력은 아래와 같다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtext&gt;LayerNorm(x+SubLayer(x))&lt;/mtext&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;residual connection 을 가능하게 하기 위해 모델 내 모든 서브레이어와 임베딩 레이어는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;512&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 출력을 생성한다.&lt;/p&gt;
&lt;h3 id=&quot;decoder&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#decoder&quot; aria-label=&quot;Link to “Decoder”&quot;&gt;Decoder&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Decoder 는 Encoder 와 마찬가지로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 개의 동일한 레이어를 쌓은 구조로 구성된다. Encoder 의 한 레이어 출력에 대해 multi-head attention 을 수행하는 세번째 서브 레이어가 존재한다.&lt;/p&gt;
&lt;p&gt;마찬가지로 각 서브레이어는 residual connection 을 사용하고 그 뒤에 Layer Normalization 을 적용한다.&lt;/p&gt;
&lt;p&gt;또한 Encoder 와는 다르게 첫번째 서브 레이어는 Masked Multi-Head Attention 을 수행하는데, 이는 학습 시 각 위치가 자기보다 이후의 위치를 참조할 수 없도록 Masking 하는 것을 뜻한다. ****즉 위치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;에 대한 예측이 오직 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;보다 작은 위치의 이미 알려진 출력들에만 의존하도록 보장한다.&lt;/p&gt;
&lt;h2 id=&quot;attention&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#attention&quot; aria-label=&quot;Link to “Attention”&quot;&gt;Attention&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;attention 함수는 하나의 Query 와 Key-Value 쌍들의 집합을 입력으로 받아 하나의 출력 벡터로 매핑하는 함수로 설명할 수 있고, Query, Key, Value, 그리고 출력은 모두 벡터이다.&lt;/p&gt;
&lt;p&gt;출력은 Value 들의 Weighted sum 으로 계산되며 각 Value 에 할당되는 가중치는 해당 Value 에 대응되는 Key 와 Query 사이의 compatibility function 에 의해 계산된다.&lt;/p&gt;
&lt;h3 id=&quot;scaled-dot-product-attention&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#scaled-dot-product-attention&quot; aria-label=&quot;Link to “Scaled Dot-Product Attention”&quot;&gt;Scaled Dot-Product Attention&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%201.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 제안하는 attention 메커니즘을 Scaled Dot-Product Attention 이라고 부른다.&lt;/p&gt;
&lt;p&gt;입력은 차원이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 인 Query와 Key, 그리고 차원이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 인 Value 로 구성된다. 즉, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;≠&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 일 수 있다. 저자는 Query 와 모든 Key 의 내적을 계산한 뒤 각각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/math&gt;&lt;/span&gt; 로 나누고 softmax 함수로 각 Value 들에 대한 가중치를 얻는다.&lt;/p&gt;
&lt;p&gt;실제로는 여러개의 Query 에 대해 Attention 을 동시에 계산하며, 전체 쿼리를 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 묶는다. Key 와 Value 도 마찬가지로 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 묶는다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;q&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;Attention&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;https://www.notion.so/Attention-Mechanisms-2e457d94cb8c80f4b376d308e3205bb0?pvs=21&quot;&gt;Attention Mechanisms&lt;/a&gt; 에서의 구조로 비유하자면, 현재 디코더의 상태 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에서, 인코더의 모든 annotation 에 대한 중요도를 알기 위해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 범위에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 수행했고, 확률 분포로 변환되어 가중합을 계산하였다.&lt;/p&gt;
&lt;p&gt;즉 찾고자 하는 것 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;) 은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 이고, 그것과 비교할 대상(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;) 은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 이 된다. 기존 Attentio n 메커니즘 에서는 비교할 대상과 가지고 오는 값이 같았다면 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;), Transformer는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;의 유사도를 기반으로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;들을 가중합하여 관련 정보를 추출한다.&lt;/p&gt;
&lt;p&gt;위 식에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;는 각 query 벡터와 모든 key 벡터 간의 내적을 계산한 것으로, 벡터들이 서로 얼마나 유사한 방향을 가리키는지를 나타낸다. query와 방향이 유사한 key일수록 더 큰 값을 가지며, 이를 softmax 함수에 통과시켜 각 key에 대한 중요도 분포를 얻는다. 이후 이 가중치를 value 벡터들에 적용해 가중합을 계산함으로써, query에 가장 관련된 정보를 효과적으로 추출한다.&lt;/p&gt;
&lt;p&gt;저자는 여기에서 softmax 함수에 넣기 전에 내적 값을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/math&gt;&lt;/span&gt;  로 나누어 정규화 하는데, 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 커질수록 내적값의 분산이 증가하여 softmax 출력이 지나치게 뾰족해지기 때문이다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 작은 경우에는 두 어텐션 메커니즘이 유사한 성능을 보이지만 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 커질수록 스케일링이 없는 dot-product attention 은 additive attention 보다 성능이 떨어진다고 주장하며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;가 클 때 내적 값의 크기가 커져서 softmax 함수가 그래디언트가 극도로 작은 영역으로 밀려난다고 추측한다.&lt;/p&gt;
&lt;h3 id=&quot;multi-head-attention&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#multi-head-attention&quot; aria-label=&quot;Link to “Multi-head Attention”&quot;&gt;Multi-head Attention&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%202.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원의 key, value, query 로 하나의 어텐션만 수행하는 대신, 서로 다른 학습된 선형 변환으로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;번 투영하여 각각 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;   차원으로 만드는 것이 더 효과적이라고 주장한다.&lt;/p&gt;
&lt;p&gt;투영된 각 query, key, value 쌍에 대해 병렬로 attention 을 여러번 수행하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원의 출력을 얻는 것이다. 이 출력들을 concat 한 후 다시 한번 선형 변환하여 최종 출력을 얻는다. 이런 방법을 Multi-Head Attention 이라고 한다.&lt;/p&gt;
&lt;p&gt;저자가 주장하는 기존 방법론 (single head-attention) 의 문제점은, 중요도의 관계가 평균화 된다는 것이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;FC Bayern München, stern des südens&lt;/mtext&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;FC Bayern Munich, star of the south&lt;/mtext&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;위와같은 번역 입/출력 시퀀스가 있을 때, 출력 시점을 stern 을 star 로 번역해야 하는 타이밍으로 가정한다. single head attention 같은 경우, 중요도 분포는 아래와 비슷할 것이다.&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;












































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;strong&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/th&gt;&lt;th&gt;&lt;strong&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;α&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/th&gt;&lt;th&gt;&lt;strong&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;0.10&lt;/td&gt;&lt;td&gt;FC&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;0.10&lt;/td&gt;&lt;td&gt;Bayern&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;0.20&lt;/td&gt;&lt;td&gt;München&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;0.05&lt;/td&gt;&lt;td&gt;,&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;5&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;0.25&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;&lt;strong&gt;stern&lt;/strong&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;0.05&lt;/td&gt;&lt;td&gt;des&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;0.15&lt;/td&gt;&lt;td&gt;südens&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;그리고 이걸 가중합 해서 하나의 context 벡터를 만들게 되는데, 이때 서로 다른 역할의 단어들이 하나의 attention 분포로 평균된다는 것이다. 즉, 주어, 동사, 부사, 목적어 등의 정보를 하나로 묶어서 요약하게 된다.&lt;/p&gt;
&lt;p&gt;Multi-head Attention에서는 이 과정을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;개의 head에 대해 병렬로 수행함으로써, 모델이 서로 다른 선형 투영 공간에서 다양한 관계를 분리해 학습할 수 있도록 한다. 즉 head 는 서로 다른 선형 투영을 통해 관계를 분리해 학습할 수 있는 독립적인 attention 공간을 의미한다. 이렇게 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;개의 head 출력을 concatenate한 뒤, 다시 선형 변환을 적용하여 최종 출력 표현을 만든다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;MultiHead&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Concat&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;where&lt;/mtext&gt;&lt;msub&gt;&lt;mtext&gt; head&lt;/mtext&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Attention&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;입력 표현은 항상 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원이며, 각 head 는 더 작은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 차원으로 투영한다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 head 별로 각각 다른 가중치를 통해 선형 투영되기 때문에, 서로 다른 subspace 를 가지게 된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원을 concat 하면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 차원이므로,  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 의 입력 차원은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이고 출력 차원을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 로 하여 다시 기존 차원을 복구한다. 이는 언급했던 Residual Connection 을 위해서이다.&lt;/p&gt;
&lt;p&gt;저자는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;64&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 설정하였다. 이렇게 하면 head 수는 1에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 증가하였지만, head 의 차원을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 나눔으로써 어텐션을 여러번 해도 연산량이 single-head attention 과 유사하다고 설명한다.&lt;/p&gt;
&lt;h3 id=&quot;applications-of-attention-in-transformer&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#applications-of-attention-in-transformer&quot; aria-label=&quot;Link to “Applications of Attention in Transformer”&quot;&gt;Applications of Attention in Transformer&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Transformer 는 아래 3가지의 서로 다른 방식으로 multi-head attention 을 사용한다.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;encoder–decoder attention 층에서는, query는 이전 디코더 레이어의 출력에서 나오고, key와 value는 인코더의 출력에서 나온다. 이를 통해 디코더의 각 위치는 입력 시퀀스의 모든 위치에 대해 attention을 수행할 수 있다. 이는 Seq2Seq 모델에서 사용되던 전형적인 encoder–decoder attention 메커니즘을 모방한 것이다.&lt;/li&gt;
&lt;li&gt;인코더는 self-attention 레이어를 포함한다. self-attention 레이어에서는 key, value, query가 모두 동일한 출처에서 나오며, 여기서는 인코더의 이전 레이어 출력이다. 인코더의 각 위치는 이전 인코더 레이어의 모든 위치에 대해 attention을 수행할 수 있다. 즉 Encoder self-attention에서는 입력 시퀀스의 각 위치가 동일한 시퀀스 내 모든 다른 위치와의 관계를 모델링함으로써, 전역적인 문맥 표현을 학습한다.&lt;/li&gt;
&lt;li&gt;마찬가지로, 디코더의 self-attention 레이어는 디코더의 각 위치가 자기 자신을 포함하여 그 위치까지의 모든 위치에 대해 attention을 수행할 수 있도록 한다. auto-regressive 특성을 유지하기 위해, 디코더에서는 미래 방향(왼쪽이 아닌 오른쪽)으로부터의 정보 흐름을 차단해야 한다. 저자는 이를 scaled dot-product attention 내부에서 구현하는데, softmax의 입력 중 허용되지 않는 연결에 해당하는 값들을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 설정하여 마스킹한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;{&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;&gt;&lt;/mo&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;FC&lt;/mtext&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;Bayern&lt;/mtext&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;Munich&lt;/mtext&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;star&lt;/mtext&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;of&lt;/mtext&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;south&lt;/mtext&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;FC&lt;/mtext&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;1,1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;Bayern&lt;/mtext&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;2,1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;2,2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;Munich&lt;/mtext&gt;&lt;mn&gt;3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;3,1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;3,2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;3,3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;star&lt;/mtext&gt;&lt;mn&gt;4&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;4,1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;4,2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;4,3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;4,4&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;of&lt;/mtext&gt;&lt;mn&gt;5&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;5,1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;5,2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;5,3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;5,4&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;5,5&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mtext&gt;south&lt;/mtext&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;6,1&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;6,2&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;6,3&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;6,4&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:5.9776pt;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;6,5&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;6,6&lt;/mn&gt;&lt;/msub&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;exp&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로, 내적값이 softmax 를 지나면 0으로 처리되어 마스킹된다.&lt;/p&gt;
&lt;h2 id=&quot;position-wise-feed-forward&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#position-wise-feed-forward&quot; aria-label=&quot;Link to “Position-wise Feed-forward”&quot;&gt;Position-wise Feed-forward&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Attention 서브레이어에 더해, 인코더와 디코더의 각 레이어는 feed-forward 네트워크를 포함한다. 이 네트워크는 각 위치에 대해 독립적으로, 그리고 동일하게 적용된다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;ff&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;ff&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;max&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;선형 변환은 서로 다른 위치들에 대해 동일하게 적용되지만, 레이어가 바뀔 때마다 서로 다른 파라미터를 사용한다.  이를 다른 방식으로 표현하면, 커널 크기가 1인 두 개의 convolution 연산으로 볼 수 있다.&lt;/p&gt;
&lt;p&gt;입력과 출력의 차원은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;512&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;이며, 내부 레이어의 차원은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;2048&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;이다.&lt;/p&gt;
&lt;p&gt;Position-wise FFN은 각 토큰 위치에 대해 동일한 비선형 변환을 독립적으로 적용함으로써, attention을 통해 수집된 정보를 토큰 단위에서 재해석하고 정제하는 역할을 한다. 이 과정에서 입력 차원 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;을 더 큰 중간 차원 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;로 확장한 뒤 다시 원래 차원으로 축소함으로써, 모델은 더 높은 표현력을 가진 비선형 변환을 학습할 수 있다. 이는 토큰 간 상호작용을 담당하는 attention과 달리, 토큰 내부 표현을 깊게 만드는 역할을 수행한다.&lt;/p&gt;
&lt;h2 id=&quot;embedding-and-softmax&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#embedding-and-softmax&quot; aria-label=&quot;Link to “Embedding and Softmax”&quot;&gt;Embedding and Softmax&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Transformer는 다른 시퀀스 변환 모델들과 마찬가지로, 입력 토큰과 출력 토큰을 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원의 벡터로 변환하기 위해 임베딩 레이어의 파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;를 학습한다.&lt;/p&gt;
&lt;p&gt;또한 디코더의 출력을 다음 토큰의 확률 분포로 변환하기 위해 선형 변환과 softmax 함수를 적용한다. 저자는 Transformer 에서 입력 임베딩, 출력 임베딩, 그리고 softmax 이전 선형 변환이 동일한 가중치 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;을 공유하도록 설계하였으며, 이는 기존 연구와 유사한 방식이다.&lt;/p&gt;
&lt;p&gt;이때 임베딩 레이어에서는 해당 가중치 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;m&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/math&gt;&lt;/span&gt; 스케일링을 적용한다. embedding 은 값의 분산이 작으므로 크기를 키워 attention 출력과 스케일을 맞춘다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;입력 토큰 시퀀스에 대한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 사용:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;{&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;if &lt;/mtext&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;index&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mtext&gt;otherwise&lt;/mtext&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;출력 임베딩에 대한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 사용:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Softmax 이전 선형 변환에서의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 사용:&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;msub&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.22em&quot; rspace=&quot;0.22em&quot; stretchy=&quot;false&quot;&gt;|&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;Transformer는 모델 정의 상 입력 임베딩, 출력 임베딩, 그리고 softmax 이전 선형 변환에서 동일한 가중치 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;를 공유하며, 이 공유된 파라미터는 학습 과정 전반에 걸쳐 업데이트된다.&lt;/p&gt;
&lt;h2 id=&quot;positional-encoding&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#positional-encoding&quot; aria-label=&quot;Link to “Positional Encoding”&quot;&gt;Positional Encoding&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Transformer 는 순환 구조도 없고, convolution 연산도 없기 때문에 모델이 시퀀스의 순서 정보를 활용할 수 있도록 토큰들의 상대적 혹은 절대적 위치에 대한 정보를 주입하여야 한다. 이를 위해 입력 입베딩 시 Positional Encoding 을 더한다.&lt;/p&gt;
&lt;p&gt;Positional Encoding 은 임베딩과 동일한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 차원이며, 두 벡터를 덧셈으로 결합할 수 있다.&lt;/p&gt;
&lt;p&gt;저자는 인코딩에 두가지 방법론이 있다고 설명하는데, 학습 가능한 방식과 고정된 방식이 있다. 본 논문에서는 서로 다른 주파수를 갖는 sine, cosine 함수를 사용한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mn&gt;10000&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mn&gt;10000&lt;/mn&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;/&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%203.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 토큰이 문장에서 몇번째 위치인지, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 벡터 안에서 몇번째 차원인지를 의미한다. 포지셔널 인코딩의 각 차원은 하나의 사인/코사인 파형에 해당한다.&lt;/p&gt;
&lt;p&gt;식에서 삼각함수의 파장은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 부터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;10000&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;π&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 까지 지수적으로 증가한다.&lt;/p&gt;
&lt;p&gt;positional encoding 벡터는 여러 주파수 성분으로 구성되어 있으며, 저차원에서는 짧은 파장, 고차원에서는 긴 파장의 신호가 사용된다.&lt;/p&gt;
&lt;p&gt;저자는 이 함수를 선택하게 된 것이 모델이 상대적 위치에 기반한 attention 을 쉽게 학습할 것이라고 가정했기 때문이라고 설명하며, 실제로 positional embedding 을 학습시켜보았으나 성능은 거의 같았다고 주장한다. 또한 학습시키는 positional embedding 을 사용할 때 보다 삼각함수로 구현할 때의 이점은 학습 때보다 더 긴 문장에도 적용될 가능성이 있기 때문이다.&lt;/p&gt;
&lt;p&gt;어떤 고정된 거리 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; (position 상 거리를 의미한다.) 에 대해, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 선형 함수로 표현할 수 있다. 이것을 증명하기 위해 512 차원이 아니라 2 차원의 경우를 예로 들어 보자면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 Positional Encoding 은:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서, 삼각함수 덧셈 공식에 의해:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:5.9776pt;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd style=&quot;padding-left:5.9776pt;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;sin&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mrow&gt;&lt;mi&gt;cos&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;p&lt;/mi&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 는 오직 offset &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 와 주파수  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;ω&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에만 의존한다. 곧 이 성질로 인해 self-attention과 같은 선형 연산 기반 구조에서 상대적 위치 관계를 쉽게 학습할 수 있다.&lt;/p&gt;
&lt;h1 id=&quot;model-formula&quot;&gt;Model Formula&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;주의:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;수식의 가독성을 위해 인코더와 디코더의 각 레이어 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;msub&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 와 같이 표기하지 않고 통일하여 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 등으로 표기한다. 그러나 실제 모델에서는 인코더/디코더의 각 레이어 및 각 서브 레이어 (Self-Attention, Cross-Attention, FFN)의 파라미터는 모두 서로 독립적이며, 임베딩 파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;만이 공유된다.&lt;/p&gt;
&lt;p&gt;또한 수식은 학습용 수식을 전제로 작성했고, 학습 시에는 고정된 길이의 타깃 시퀀스를 병렬 처리하므로 미래 토큰을 차단하기 위한 causal mask가 필요하다. 반면 추론 시에는 디코더 입력이 현재까지 생성된 토큰만 포함하므로 마스크는 전부 0이거나 생략될 수 있다.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;Set-up&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;512&lt;/mn&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;8&lt;/mn&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;64&lt;/mn&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;2048&lt;/mn&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Input Embedding&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Positional Encoding&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mtext&gt;PE&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Encoder Self-Attention&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Attention&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mtext&gt;MultiHead&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Concat&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Residual Connection + LayerNorm&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mtext&gt;PE&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Position Wise Feed-Forward Network&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;ff&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;ff&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;max&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Residual Connection + LayerNorm&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;SA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;repeat &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; times&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;EncoderLayer&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;MultiHead&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mi&gt;Z&lt;/mi&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;Y&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;EncoderLayer&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Output Embedding&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;⟨&lt;/mo&gt;&lt;mtext&gt;SOS&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;⟩&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;⟨&lt;/mo&gt;&lt;mtext&gt;SOS&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;⟩&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Positional Encoding&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mtext&gt;PE&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Masked Self-Attention&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;{&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;if &lt;/mtext&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;≤&lt;/mo&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;−&lt;/mo&gt;&lt;mi&gt;∞&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;if &lt;/mtext&gt;&lt;mi&gt;j&lt;/mi&gt;&lt;mo&gt;&gt;&lt;/mo&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;MaskedAttention&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mtext&gt;MultiHead&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Concat&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Residual Connection + LayerNorm&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Cross Attention&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Attention&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;msubsup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;v&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mtext&gt;MultiHead&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Concat&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;head&lt;/mtext&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Residual Connection + LayerNorm&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;MSA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Position Wise Feed-Forward Network&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;ff&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;ff&lt;/mtext&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;max&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;[&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;⋮&lt;/mi&gt;&lt;mspace width=&quot;0pt&quot; height=&quot;14.944pt&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mtext&gt;out&lt;/mtext&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Residual Connection + LayerNorm&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mrow style=&quot;margin-left:-0.1667em;&quot;&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;CA&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;u&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mrow&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mi&gt;w&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;repeat &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; times&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mtext&gt;PE&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mover&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml&quot; style=&quot;math-depth:0;&quot;&gt;~&lt;/mo&gt;&lt;/mover&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;MaskedAttention&lt;/mtext&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;M&lt;/mi&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mover&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;(&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml&quot; style=&quot;math-depth:0;&quot;&gt;~&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;CrossAttention&lt;/mtext&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml&quot; style=&quot;math-depth:0;&quot;&gt;~&lt;/mo&gt;&lt;/mover&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mtext&gt;enc&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-right&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;(&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;FFN&lt;/mtext&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;e&lt;/mi&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;Embedding and softmax&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mtext&gt;dec&lt;/mtext&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;msup&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mi class=&quot;tml-med-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;mi&gt;|&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo lspace=&quot;0.22em&quot; rspace=&quot;0.22em&quot; stretchy=&quot;false&quot;&gt;|&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mrow&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;softmax&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;o&lt;/mi&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h1 id=&quot;why-self-attention&quot;&gt;Why Self-Attention&lt;/h1&gt;
&lt;p&gt;저자는 이 절에서 시퀀스 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;를 동일한 길이의 또 다른 시퀀스 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mo&gt;…&lt;/mo&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 변환하는 데 일반적으로 사용되는 순환(recurrent) 레이어와 합성곱(convolutional) 레이어를 self-attention 레이어의 여러 측면과 비교한다. (여기서,  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)&lt;/p&gt;
&lt;p&gt;Self-Attention 이 다른 방법론과 비교하여 크게 세가지 이점이 있는데, 하나는 레이어 당 연산 복잡도의 우위이고, 또 하나는 병렬화 될 수 있는 계산의 양으로, 이는 필요한 최소 순차 연산의 개수로 측정한다. 즉 선행되어야하는 연산이 얼마나 있는가가 병렬화 가능성을 의미한다.&lt;/p&gt;
&lt;p&gt;마지막 세번째는 장거리 의존성을 연결하는 경로의 길이이다. 장거리 의존성을 학습하는 것은 많은 시퀀스 변환 문제에서 핵심적인 어려움이며, 순전파 및 역전파 신호가 네트워크를 통해 이동해야 하는 경로의 길이를 의미하기 때문이다. 입력과 출력 시퀀스의 어떤 위치 쌍에 대해서든, 이 경로가 짧을수록 장거리 의존성을 학습하기가 더 쉬워진다.&lt;/p&gt;
&lt;p&gt;따라서 저자는 서로 다른 레이어 타입(RNN, CNN, self-attention)으로 구성된 네트워크에서, 임의의 두 입력/출력 위치 사이의 최대 경로 길이를 비교한다.&lt;/p&gt;
&lt;h3 id=&quot;vs-rnn&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#vs-rnn&quot; aria-label=&quot;Link to “vs. RNN”&quot;&gt;vs. RNN&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;self-attention 레이어는 모든 위치를 상수 개수의 순차 연산으로 연결하는 반면, recurrent 레이어는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;개의 순차 연산이 필요하다. 계산 복잡도 측면에서 보면, 시퀀스 길이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;이 표현 차원 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;보다 작을 때 self-attention 레이어는 recurrent 레이어보다 빠르다.&lt;/p&gt;
&lt;p&gt;RNN 에서 hidden state 를 계산하기 위해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;msub&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;t&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 계산하는데, 여기서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;h&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로, 계산 복잡도는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 스텝을 계산해야하므로 RNN 의 계산 복잡도는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다.&lt;/p&gt;
&lt;p&gt;Self-Attention 은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi class=&quot;tml-sml-pad&quot;&gt;⊤&lt;/mi&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 즉 행렬 곱에서 각 원소 계산 비용은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 이고 원소는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 이므로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이다. 곧:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;RNN&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;SelfAttention&lt;/mtext&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이기 때문에, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 Self-Attention 이 RNN 보다 계산 복잡도가 낮다.&lt;/p&gt;
&lt;p&gt;아주 긴 시퀀스를 다루는 작업의 계산 성능을 개선하기 위해 self-attention을 출력 위치를 중심으로 한 크기 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;의 이웃 영역만 고려하도록 제한할 수도 있다. 이렇게 하면 최대 경로 길이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;/&lt;/mi&gt;&lt;mi&gt;r&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 증가하는데, 이 접근법은 본 논문에서는 더 연구되지는 않는다.&lt;/p&gt;
&lt;h3 id=&quot;vs-cnn&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#vs-cnn&quot; aria-label=&quot;Link to “vs. CNN”&quot;&gt;vs. CNN&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;커널 폭이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;&amp;#x3C;&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 인 단일 convolution 레이어는 입력과 출력 위치의 모든 쌍을 연결하지 못한다. 이를 달성하려면 연속된 커널을 사용하는 경우 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mi&gt;/&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)개의 합성곱 레이어를 쌓아야 한다. 혹은 dilated convolution의 경우에는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;개의 레이어가 필요하다. 또한 동일한 표현 차원에서, 커널 폭이 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;인 convolution은 한 시점당 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;개의 입력을 처리하므로 순환 레이어보다 일반적으로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;배 많은 연산을 요구한다.&lt;/p&gt;
&lt;p&gt;하지만 separable convolution 로 계산 복잡도를 상당히 줄일 수 있는데, 먼저 각 채널을 독립적으로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 로 convolution 한다. 이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 에서 각 채널 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 에 대해서만 convolution 하는 것을 의미한다. 즉 요소 갯수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 개 에서, 각 요소마다 곱셈 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 회 이므로  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;),&lt;/p&gt;
&lt;p&gt;이후 kernel size 1의 point wise convolution을 적용하여 채널 방향으로 선형 결합한다. 선형 결합은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 의미하는데, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;X&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로, 원소 갯수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt;, 원소 당 연산 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 개이므로, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;결론적으로 separable convolution 의 계산 복잡도는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mn class=&quot;tml-lrg-pad&quot;&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;. 그러나 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 인 경우에 분리 합성곱의 계산 복잡도는 self-attention 레이어와 point wise feed-forward 레이어를 결합한 것과 동일하다.&lt;/p&gt;
&lt;p&gt;즉 CNN 으로 Self-Attention 의 path length 에 도달하려면 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이어야 하며, 이 경우 계산 복잡도는 self-attention + point-wise feed forward 와 동일하다는 것이다.&lt;/p&gt;
&lt;h3 id=&quot;side-benefit&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#side-benefit&quot; aria-label=&quot;Link to “Side Benefit”&quot;&gt;Side Benefit&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;부수적인 이점으로, self-attention은 더 해석하기 쉽다. 저자는 모델에서 나온 attention 분포를 분석하였고, 개별 attention head 들이 서로 다른 역할을 학습한다는 점을 시각화를 통해 관찰하였고 많은 head들이 문장의 syntactic 및 semantic 구조와 관련된 행동이 보인다고 주장한다.&lt;/p&gt;
&lt;p&gt;즉 Self-attention은 단순히 성능이 좋은 구조일 뿐 아니라, attention weight를 통해 모델이 무엇을 보고 판단하는지 비교적 명확하게 해석할 수 있는 구조다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%204.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;h1 id=&quot;training&quot;&gt;Training&lt;/h1&gt;
&lt;h2 id=&quot;tokenizers&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#tokenizers&quot; aria-label=&quot;Link to “Tokenizers”&quot;&gt;Tokenizers&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;byte-pair-encodoing&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#byte-pair-encodoing&quot; aria-label=&quot;Link to “Byte-Pair Encodoing”&quot;&gt;&lt;strong&gt;Byte-Pair Encodoing&lt;/strong&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;BPE는 단어를 문자 단위에서 시작해, 가장 자주 함께 등장하는 문자(또는 서브워드) 쌍을 반복적으로 병합하여 서브워드 어휘를 만드는 빈도 기반 토크나이징 기법이다.&lt;/p&gt;
&lt;p&gt;학습 과정은 단순히 말뭉치에서 가장 빈번한 쌍을 찾고 이를 하나의 토큰으로 합치는 작업을 정해진 횟수만큼 반복하는 방식이며, 이로써 어휘 크기를 제한하면서도 희귀 단어를 여러 서브워드의 조합으로 표현할 수 있다. BPE는 계산이 간단하고 데이터 규모가 크지 않아도 안정적으로 동작하기 때문에, WMT 영어–독일어 번역과 같이 비교적 중간 규모 데이터에서 표준적으로 사용되어 왔다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;playing, played, player, plays&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;의 예시에서,  p + l, pl + a, pla + y 같이  자주 같이 등장하는 문자쌍 위주로 병합하는 방식이다.&lt;/p&gt;
&lt;h3 id=&quot;wordpiece&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#wordpiece&quot; aria-label=&quot;Link to “WordPiece”&quot;&gt;&lt;strong&gt;WordPiece&lt;/strong&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;WordPiece는 단어를 서브워드 단위로 분해한다는 점에서는 BPE와 유사하지만, 병합 기준으로 단순 빈도가 아니라 언어 모델의 확률(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;log&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mspace width=&quot;0.1667em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; likelihood)을 가장 크게 개선하는 쌍을 선택하는 확률 기반 토크나이징 기법이다.&lt;/p&gt;
&lt;p&gt;즉, 두 토큰을 합쳤을 때 문장 전체의 생성 확률이 얼마나 좋아지는지를 기준으로 병합을 결정한다. 이 때문에 WordPiece는 이론적으로 더 정교한 기준을 사용하지만, 충분한 데이터가 있을 때 효과적이며 학습 비용은 BPE보다 다소 높다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;playing, played, player, plays&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;의 예시에서,  play + (ing, ed, er) 일 때 모델이 likelihood 를 최대로 만드므로, play라는 서브워드를 만들었을 때 전체 문장 확률이 올라가면, 그 병합을 채택한다.&lt;/p&gt;
&lt;h2 id=&quot;training-data-and-batching&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#training-data-and-batching&quot; aria-label=&quot;Link to “Training Data and Batching”&quot;&gt;Training Data and Batching&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;WMT 2014 번역 벤치마크에서 저자는 두 가지 데이터셋으로 Transformer를 학습했다. 영어-독일어는 약 450만 개의 문장 쌍으로 구성된 표준 WMT14 데이터를 사용했고, 영어-프랑스어는 훨씬 규모가 큰 3,600만 문장 쌍의 WMT14 데이터를 사용했다. 두 실험 모두 단어를 그대로 쓰지 않고, 희귀 단어를 더 작은 단위로 쪼개는 서브워드 토크나이징을 적용했다. 영어-독일어에서는 Byte-Pair Encoding(BPE)을 사용해 소스와 타깃이 공유하는 약 37,000개 토큰 어휘를 만들었고, 영어-프랑스어에서는 word-piece 방식으로 약 32,000개 토큰 어휘를 사용했다. 이런 서브워드 기반 어휘를 쓰면 OOV(어휘 밖 단어) 문제가 줄고, 언어 간 형태 변화가 큰 단어도 조합적으로 표현할 수 있다.&lt;/p&gt;
&lt;p&gt;BPE 는 빈도 기반이므로, 데이터가 아주 크지 않아도 안정적으로 동작한다. 병합 기준이 단순하기 때문이다. 반면 WordPiece 는 확률 기반이므로, 충분히 큰 말뭉치가 있어야 병합 판단이 의미있다. 즉 데이터가 적으면 불안정 할 수 있다. 그래서 불어에 비해 데이터가 적은 독일어 쪽에 BPE 를 사용했다고 해석된다.&lt;/p&gt;
&lt;p&gt;저자는 배치 구성 방식도 Transformer 특성에 맞춰 설계했다. 문장 수를 고정해서 배치를 만드는 대신, 문장 쌍을 대략 비슷한 길이끼리 묶은 뒤 한 배치가 포함하는 토큰 수를 거의 일정하게 맞췄다. 구체적으로는 한 배치가 대략 소스 토큰 25,000개와 타깃 토큰 25,000개를 포함하도록 문장 쌍을 모았다. 이렇게 하면 길이가 제각각인 문장을 무작정 섞었을 때 발생하는 과도한 padding 낭비가 줄어들고, 특히 시퀀스 길이에 따라 비용이 커지는 self-attention(대략 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)을 더 효율적으로 학습할 수 있다.&lt;/p&gt;
&lt;h2 id=&quot;hardware-and-schedule&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#hardware-and-schedule&quot; aria-label=&quot;Link to “Hardware and Schedule”&quot;&gt;Hardware and Schedule&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;저자는 8개의 P100 GPU 환경에서 Transformer를 학습했으며, base 모델은 약 12시간, big 모델은 약 3.5일이 소요되었다. 이는 Transformer가 병렬화에는 유리하지만, 대규모 연산 자원을 전제로 한 모델임을 보여준다.&lt;/p&gt;
&lt;h2 id=&quot;optimizer&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#optimizer&quot; aria-label=&quot;Link to “Optimizer”&quot;&gt;Optimizer&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;저자는 Adam 옵티마이저를 사용했으며, 1차 모멘트 계수는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.9&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, 2차 모멘트 계수는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.98&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, 수치 안정화를 위한 상수는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mn&gt;10&lt;/mn&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;9&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 설정했다.&lt;/p&gt;
&lt;p&gt;보편적으로 사용하는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.999&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 사용하지 않은 것이 포인트인데, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mn&gt;0.98&lt;/mn&gt;&lt;/math&gt;&lt;/span&gt; 은 훨씬 빠른 분산 반응을 보인다. 이는 Transformer 에서 attention score 분포가 step 마다 크게 변하기 때문이며, 특정 head, token 에 몰렸다가 사라져 과거의 분산 정보가 금방 쓸모없어지기 때문이다.&lt;/p&gt;
&lt;p&gt;또한 학습 과정 전체에 걸쳐 학습률을 변화시켰다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;lrate&lt;/mtext&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mtext&gt;model&lt;/mtext&gt;&lt;mrow class=&quot;tml-lrg-pad&quot;&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;0.5&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;min&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;(&lt;/mo&gt;&lt;msup&gt;&lt;mtext&gt;step&lt;/mtext&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;0.5&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mtext&gt;step&lt;/mtext&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.222em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;⋅&lt;/mo&gt;&lt;mspace width=&quot;0.222em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mtext&gt;warmup_steps&lt;/mtext&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot; lspace=&quot;0em&quot; rspace=&quot;0em&quot;&gt;−&lt;/mo&gt;&lt;mn&gt;1.5&lt;/mn&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;Transformer는 학습 초반 불안정을 막기 위해 처음 4000 step 동안 학습률을 step에 비례해 선형적으로 증가시키고, 이후에는 step 수의 역제곱근에 비례하도록 감소시키는 learning rate schedule을 사용한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%205.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;h2 id=&quot;regularization&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#regularization&quot; aria-label=&quot;Link to “Regularization”&quot;&gt;Regularization&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;residual-dropout&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#residual-dropout&quot; aria-label=&quot;Link to “Residual Dropout”&quot;&gt;&lt;strong&gt;Residual Dropout&lt;/strong&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Transformer에서는 각 서브레이어의 출력에 dropout을 적용한 뒤 residual connection과 layer normalization을 수행하는 residual dropout을 사용한다. 이는 identity path의 안정성을 유지하면서 서브레이어 출력에만 정규화를 가하기 위함이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;LayerNorm&lt;/mtext&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mtext&gt;Dropout&lt;/mtext&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mtext&gt;Sublayer&lt;/mtext&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;mo fence=&quot;false&quot; symmetric=&quot;true&quot; minsize=&quot;1.2em&quot; maxsize=&quot;1.2em&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;또한 입력 단계에서 token embedding과 positional encoding의 합에도 dropout을 적용하여 위치 정보에 대한 과도한 의존을 방지한다. Base 모델에서는 dropout 비율로 0.1을 사용한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow class=&quot;tml-sml-pad&quot;&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;Dropout&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;E&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h3 id=&quot;label-smoothing&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#label-smoothing&quot; aria-label=&quot;Link to “Label Smoothing”&quot;&gt;&lt;strong&gt;Label Smoothing&lt;/strong&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;퍼플렉시티(perplexity)는 언어 모델이 정답 토큰을 얼마나 “확신하지 못하는지”를 나타내는 지표로, 각 시점에서 정답 단어에 할당한 로그 확률의 평균을 지수화한 값이다. 직관적으로는 모델이 다음 단어를 예측할 때 평균적으로 몇 개의 후보 앞에서 망설이고 있는지를 의미하며, 값이 낮을수록 모델이 정답에 높은 확률을 부여하고 있음을 뜻한다.&lt;/p&gt;
&lt;p&gt;라벨 스무딩(label smoothing)은 학습 시 정답 레이블을 one-hot 벡터로 두지 않고, 정답 클래스의 확률을 1보다 작게 낮추고 나머지 확률을 다른 클래스들에 소량 분산시키는 정규화 기법이다. 이로 인해 모델은 특정 토큰에 대해 100% 확신을 갖도록 강제되지 않으며, 출력 분포가 지나치게 뾰족해지는 것을 방지한다. 결과적으로 모델은 “정답일 가능성이 가장 높다”는 정보는 유지하되, 대안 토큰들에 대해서도 일정 수준의 확률을 할당하도록 학습된다. 이는 over-confidence를 줄이고, softmax 출력의 포화를 방지하는 역할을 하며, 출력 분포 자체에 작용하는 regularization으로 볼 수 있다.&lt;/p&gt;
&lt;p&gt;저자는 학습 과정에서 라벨 스무딩(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;l&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/mrow&gt;&lt;/msub&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0.1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;)을 적용하였으며, 이로 인해 퍼플렉시티는 오히려 악화되지만 정확도와 BLEU 점수는 향상된다고 보고한다. 이는 라벨 스무딩이 정답 토큰의 확률을 의도적으로 낮추기 때문에 퍼플렉시티 기준에서는 손해처럼 보이지만, 번역 과제에서 중요한 것은 단일 토큰의 확률 최대화가 아니라 문장 전체의 상대적 품질이기 때문이다.&lt;/p&gt;
&lt;h1 id=&quot;results&quot;&gt;Results&lt;/h1&gt;
&lt;h2 id=&quot;machine-translation&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#machine-translation&quot; aria-label=&quot;Link to “Machine Translation”&quot;&gt;Machine Translation&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;저자는 Transformer 논문에서 저자들은 WMT 2014 영어–독일어와 영어–프랑스어 번역 과제를 통해 모델의 성능과 효율성을 평가하였다. 영어–독일어 번역에서는 약 450만 문장 쌍으로 구성된 데이터셋을 사용했으며, Transformer(big) 모델은 BLEU 28.4를 기록해 기존 최고 성능 모델들(앙상블 포함)을 2 BLEU 이상 앞지르며 새로운 최고 성능을 달성했다. 영어–프랑스어 번역에서는 3,600만 문장으로 이루어진 훨씬 더 큰 데이터셋을 사용했으며, Transformer(big)은 BLEU 41.0을 기록해 기존 모든 단일 모델을 능가했고, 이전 최고 성능 모델 대비 훈련 비용은 1/4 이하에 불과했다. 심지어 base 모델조차도 기존의 모든 단일 모델과 앙상블을 뛰어넘는 성능을 훨씬 적은 계산 비용으로 달성했다.&lt;/p&gt;
&lt;p&gt;base 모델과 대부분의 실험에서는 dropout 비율 0.1을 적용했다. 반면 Transformer(big)은 기본적으로 파라미터 수가 많아 과적합 위험이 크기 때문에 dropout 0.3을 사용했지만, 데이터 규모가 매우 큰 영어–프랑스어 번역 실험에서는 과도한 정규화를 피하기 위해 dropout을 0.1로 낮췄다. 이는 모델 크기뿐 아니라 데이터 규모에 따라 적절한 정규화 강도가 달라져야 함을 보여준다.&lt;/p&gt;
&lt;h2 id=&quot;model-variations&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#model-variations&quot; aria-label=&quot;Link to “Model Variations”&quot;&gt;Model Variations&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%206.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/260114/image%207.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 Transformer의 각 구성 요소가 성능에 미치는 영향을 분석하기 위해, base 모델을 다양한 방식으로 변형하는 테스트를 수행하였다. 평가는 영어–독일어 번역 개발셋(newstest2013)에서 이루어졌으며, beam search는 사용하되 checkpoint averaging은 적용하지 않았다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;multi-head attention의 중요성이 확인되었다. 단일 head attention은 최적 설정 대비 약 0.9 BLEU 낮은 성능을 보였으며, 이는 여러 head를 통해 서로 다른 관계를 병렬적으로 학습하는 구조가 번역 성능에 실질적으로 기여함을 의미한다. 다만 head 수를 지나치게 늘릴 경우 각 head의 차원이 너무 작아져 성능이 다시 저하되었다.&lt;/li&gt;
&lt;li&gt;attention key 차원 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 은 모델 성능에 중요한 요소였다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;k&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;를 줄이면 번역 품질이 감소했는데, 이는 query–key 간의 유사도를 판단하는 문제가 단순하지 않으며 충분한 표현 공간이 필요함을 시사한다.&lt;/li&gt;
&lt;li&gt;모델 크기와 정규화의 효과도 명확히 나타났다. 모델을 크게 만들수록 성능은 향상되었고, dropout은 과적합을 방지하는 데 매우 중요한 역할을 했다. 특히 Transformer처럼 파라미터 수가 많은 모델에서는 dropout이 없을 경우 성능이 크게 악화되었다.&lt;/li&gt;
&lt;li&gt;positional encoding 방식에 대한 실험에서는 사인-코사인 기반 고정 positional encoding과 학습 가능한 positional embedding 사이에 성능 차이가 거의 없었다. 이는 Transformer가 고정된 위치 인코딩만으로도 충분히 위치 정보를 활용할 수 있음을 보여준다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;english-constituency-parsing&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#english-constituency-parsing&quot; aria-label=&quot;Link to “English Constituency Parsing”&quot;&gt;English Constituency Parsing&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;저자는 Transformer가 기계 번역을 넘어 다른 시퀀스 변환 문제에도 일반화될 수 있는지를 검증하기 위해 영어 구문 분석(constituency parsing) 실험을 수행하였다. 구문 분석은 출력이 강한 구조적 제약을 따르고 출력 시퀀스가 입력보다 훨씬 길어지는 어려운 과제로, 모델의 장거리 의존성 처리 능력과 구조적 표현력을 평가하기에 적합하다.&lt;/p&gt;
&lt;p&gt;실험 결과, Transformer는 과제 특화된 구조나 복잡한 튜닝 없이도 매우 우수한 성능을 보였으며, 기존 RNN 기반 sequence-to-sequence 모델들을 모두 능가하였다. 특히 데이터 규모가 작은 환경에서도 전통적인 구문 분석 모델인 Berkeley Parser보다 더 나은 성능을 기록하였다. 이는 self-attention 기반 Transformer가 장거리 의존성과 전역적 구조를 효과적으로 모델링할 수 있음을 보여주며, Transformer가 번역을 넘어 다양한 구조적 시퀀스 변환 문제에서도 강한 일반화 성능을 갖는다는 점을 입증한다.&lt;/p&gt;
&lt;h1 id=&quot;conclusion-1&quot;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;저자는 본 논문에서 Transformer를 제안하였다. Transformer는 기존 encoder–decoder 구조에서 널리 사용되던 순환(recurrent) 레이어를 완전히 제거하고, multi-head self-attention만으로 구성된 최초의 시퀀스 변환 모델이다.&lt;/p&gt;
&lt;p&gt;기계 번역 실험에서 Transformer는 RNN이나 CNN 기반 모델들에 비해 훨씬 빠르게 학습될 수 있었으며, WMT 2014 영어–독일어 및 영어–프랑스어 번역 과제 모두에서 SOTA를 달성하였다. 특히 영어–독일어 번역에서는 기존의 모든 앙상블 모델들까지도 능가하는 성능을 기록하였다.&lt;/p&gt;
&lt;p&gt;저자들은 이러한 결과를 바탕으로 attention 기반 모델의 잠재력에 큰 기대를 표하며, 향후 Transformer를 텍스트를 넘어 이미지, 오디오, 비디오와 같은 다양한 입력·출력 모달리티로 확장할 계획임을 밝힌다. 또한 매우 큰 입력과 출력을 효율적으로 처리하기 위해 국소적(local) 또는 제한된 attention 메커니즘을 연구하고, 생성 과정의 순차성을 더욱 줄이는 방향도 중요한 향후 연구 과제로 제시한다.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Deep Residual Learning for Image Recognition</title>
    <link href="https://bayernmuller.github.io/posts/251231-deep-residual-learning-for-image-recognition/" />
    <id>https://bayernmuller.github.io/posts/251231-deep-residual-learning-for-image-recognition/</id>
    <published>2025-12-31T00:00:00.000Z</published>
    <updated>2025-12-31T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="ResNet" /><category term="Image Recognition" /><category term="Machine Learning" /><category term="ML" /><category term="Korean" />
    <summary>논문 리뷰: 잔차 학습을 통한 이미지 인식</summary>
    <content type="html">&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1512.03385&quot;&gt;https://arxiv.org/abs/1512.03385&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&quot;backgrounds&quot;&gt;Backgrounds&lt;/h1&gt;
&lt;h2 id=&quot;batch-normalization&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#batch-normalization&quot; aria-label=&quot;Link to “Batch Normalization”&quot;&gt;Batch Normalization&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1502.03167&quot;&gt;https://arxiv.org/abs/1502.03167&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Batch Normalization(이하 BN) 은 활성함수에 입력되기 직전의 선형 출력을 미니배치 단위로 평균 0, 분산 1로 정규화 하는 기법이며, 기울기 소실/폭발 현상을 완화할 수 있는 방법이다.&lt;/p&gt;
&lt;p&gt;레이어의 forward 연산은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이고 (바이어스 생략), 역전파는:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;⊙&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-lrg-pad&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-lrg-pad&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 는 활성함수의 도함수이므로:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mtext&gt;sigmoid&lt;/mtext&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msup&gt;&lt;mi&gt;tanh&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;msup&gt;&lt;mtext&gt;ReLU&lt;/mtext&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이다.  즉 활성함수의 도함수는 기울기를 증폭시키지 못하므로, 역전파에서 기울기 폭발의 주요 원인은 가중치 행렬 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msup&gt;&lt;/math&gt;&lt;/span&gt; 의 스케일이다.&lt;/p&gt;
&lt;p&gt;또, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 스케일이 큰 경우, 작은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 변화에도 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 값이 크게 변화하게 된다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 분산이  커지며, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 분포의 상당 부분이 활성함수의 포화 영역에 위치하게 된다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/bn.png&quot; alt=&quot;bn&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;3153&quot; height=&quot;2112&quot;&gt;&lt;/p&gt;
&lt;p&gt;사진처럼 활성함수가 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mtext&gt;tanh&lt;/mtext&gt;&lt;/math&gt;&lt;/span&gt; 인 경우, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 분포가 1 이상일 때 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msup&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-lrg-pad&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 극단적으로 작아지게 된다. 곧 역전파에서 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;L&lt;/mi&gt;&lt;/mrow&gt;&lt;mrow&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;mi&gt;a&lt;/mi&gt;&lt;/mrow&gt;&lt;/mfrac&gt;&lt;mo&gt;⊙&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;f&lt;/mi&gt;&lt;mo lspace=&quot;0em&quot; rspace=&quot;0em&quot; class=&quot;tml-prime tml-lrg-pad&quot;&gt;′&lt;/mo&gt;&lt;/msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 값이 작아지므로, 기울기가 소실된다.&lt;/p&gt;
&lt;p&gt;그래서 BN 은 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 활성함수로 들어가기 전에 미니배치(&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mrow&gt;&lt;/mrow&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;) 단위로 정규화 하여 활성함수의 중앙 부분에 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 분포하도록 강제한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mfrac&gt;&lt;mrow&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;μ&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;msqrt&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mi&gt;B&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;ϵ&lt;/mi&gt;&lt;/mrow&gt;&lt;/msqrt&gt;&lt;/mfrac&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;γ&lt;/mi&gt;&lt;mover&gt;&lt;mi&gt;z&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml-acc&quot; style=&quot;math-depth:0;&quot;&gt;^&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;단순히 스케일을 정규화하는 것이 아니라, 학습 파라미터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;γ&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 사용하여 입력 분포를 스케일 하거나 이동시킬 수 있게 한다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;γ&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;β&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 정규화로 사라진 스케일과 자유도를 복원하여 네트워크가 필요한 경우 선형 변환을 다시 학습할 수 있도록 한다.&lt;/p&gt;
&lt;h2 id=&quot;vgg&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#vgg&quot; aria-label=&quot;Link to “VGG”&quot;&gt;VGG&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1409.1556&quot;&gt;https://arxiv.org/abs/1409.1556&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;VGG는 작은 3×3 convolution을 반복적으로 쌓아 깊이를 늘리면 성능이 좋아진다는 것을 처음으로 명확히 보여준 CNN이다. 공간 해상도는 pooling으로 줄이고 채널 수를 늘리며, 저수준 특징에서 고수준 개념으로 가는 계층적 표현을 형성한다는 점에서 다양한 CNN 설계의 레퍼런스가 되었다.&lt;/p&gt;
&lt;p&gt;반면 한계도 분명하다. fully connected 레이어로 인해 파라미터 수와 연산량이 과도하게 크고, BN이나 residual connection이 없어 더 깊은 구조로 확장하기 어렵다. 또한 pooling 중심의 downsampling은 위치 정보 손실을 초래한다.&lt;/p&gt;
&lt;h2 id=&quot;faster-r-cnn&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#faster-r-cnn&quot; aria-label=&quot;Link to “Faster R-CNN”&quot;&gt;Faster R-CNN&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;https://arxiv.org/abs/1506.01497&quot;&gt;https://arxiv.org/abs/1506.01497&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;R-CNN은 객체 검출을 “영역 제안 + CNN 분류”로 분해한 최초의 실용적 방법이다. 먼저 Selective Search로 이미지당 약 2천 개의 region proposal을 만들고, 각 영역을 개별적으로 CNN에 통과시켜 feature를 뽑은 뒤 분류하고 bounding box regression으로 위치를 보정한다.&lt;/p&gt;
&lt;p&gt;이 방식은 CNN의 강력한 표현력을 detection에 처음 성공적으로 도입했다는 점에서 의미가 크지만, 모든 proposal을 독립적으로 CNN에 넣는 구조 때문에 계산량과 저장 비용이 비현실적으로 크고, 학습 파이프라인도 여러 단계로 분리돼 있다.&lt;/p&gt;
&lt;p&gt;Faster R-CNN은 R-CNN 계열의 병목이던 region proposal 자체를 신경망 안으로 흡수했다. Selective Search를 제거하고, convolution feature map 위에서 RPN(Region Proposal Network)이 proposal을 생성한다. 이 proposal들은 RoI(Region of Interest) Pooling을 통해 고정 크기 feature로 변환되어, 하나의 CNN backbone을 proposal 생성과 분류가 공유한다.&lt;/p&gt;
&lt;p&gt;결과적으로 Faster R-CNN은 정확도를 유지하면서도 속도를 획기적으로 개선했다.&lt;/p&gt;
&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;깊은 합성공 신경망(CNN) 은 이미지 분류 분야에서 엄청난 돌파구를 만들어냈다. 전통적인 이미지 인색 파이프라인은  가장자리, 곡선 등을 검출하는 Low-level 단계, part, pattern 을 검출하는 Mid-level, 얼굴, 사물 등을 인식하는 High-level, 그리고 classifier 까지, 명시적으로 구분된 단계가 있었으며 이를 사람이 단계별로 설계했고, 각 단계는 따로 최적화 되었다.&lt;/p&gt;
&lt;p&gt;CNN 은 Image 로 부터 classifier 까지 전 과정을 하나의 네트워크가 알아서 학습하도록 하며, 계층적 표현 + 분류기가 하나의 깊은 함수로 묶여 있어 end-to-end 모델을 구성할 수 있었다.&lt;/p&gt;
&lt;p&gt;CNN 에서 이러한 특징의 Level 은 레이어를 더 깊이 쌓을 수록 더욱 풍부해질 수 있다. 이는 네트워크의 깊이가 늘어날 수록 표현력이 증가함을 의미하고, 관련 연구들이 네트워크의 깊이가 결정적으로 중요하다는 것을 보여준다. 난이도가 높은 ImageNet 데이터셋에서 최고 성능을 낸 모델들은 모두 매우 깊은 모델을 사용한다. (VGG-16, VGG-19, GoogLeNet 등)&lt;/p&gt;
&lt;p&gt;detection, segmentation, pose estimation 같은 고난도의 시각 인식 문제들에서도 깊은 모델은 효과가 있었다. 이는 깊은 네트워크가 classification 뿐만 아니라 범용적인 이점으로 작용함을 의미한다.&lt;/p&gt;
&lt;p&gt;이처럼 깊은 네트워크의 중요성이 커지면서 의문이 생긴다.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;단순히 레이어만 더 쌓는 것만으로 더 좋은 네트워크를 학습할 수 있는가?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;이 질문에 대한 답은 gradient vanishing/exploding 에 의해 가로막힌다. 이 문제는 학습 초반부터 모델 최적화를 방해한다. 이 문제는 normalized initialization, batch normalization 에 의해 상당 부분 해결되었고, 이로 인해 깊은 네트워크에서도 SGD + 역전파로 학습을 시작할 수 있게 되었다.&lt;/p&gt;
&lt;p&gt;그러나 학습을 시작할 수 있는 것과 얼마나 최적화할 수 있는지는 별개의 문제이다. 저자는 실험 결과를 통해 깊은 네트워크에서 얇은 네트워크보다 training/test error 가 높게 나타나는 현상(degradation)에 주목한다.&lt;/p&gt;
&lt;p&gt;degradation은 적절히 깊은 모델에 더 많은 레이어를 추가했을 때 관측되며, 이는 training accuracy 자체가 떨어지기 때문에 과적합에 의한 문제가 아니다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;930&quot; height=&quot;478&quot;&gt;&lt;/p&gt;
&lt;p&gt;이는 표현력 관점에서 말이 되지 않는다. 만약 적당한 깊이의 네트워크가 있을 때, 여기서 레이어를 더 추가한 깊은 네트워크를 생각해보자.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/deep.png&quot; alt=&quot;deep.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2892&quot; height=&quot;967&quot;&gt;&lt;/p&gt;
&lt;p&gt;더 깊은 네트워크를 만들기 위해 기존 네트워크 위에 새로운 레이어들을 추가하고, 기존 Original Network의 가중치와 바이어스를 그대로 복사한다고 가정하자.&lt;/p&gt;
&lt;p&gt;이때 새로 추가된 레이어들이 모두 항등함수(identity mapping)를 구현한다면, 확장된 깊은 네트워크는 깊어지기 전의 Original Network와 동일한 training/test error를 갖는 해(solution)를 표현할 수 있다.&lt;/p&gt;
&lt;p&gt;즉, 더 깊은 네트워크는 얕은 네트워크를 포함하는 함수 공간을 가지며, 추가된 레이어가 항등함수를 구현할 수 있다면 얕은 네트워크에서 얻어진 해는 깊은 네트워크에서도 그대로 재현 가능하다. 따라서 깊은 네트워크의 최소 training error는 얕은 네트워크보다 클 수 없다.&lt;/p&gt;
&lt;p&gt;그럼에도 불구하고 실제 학습 과정에서 SGD가 이러한 해를 찾지 못한다는 사실은 관측되는 degradation 현상이 overfitting과 무관하며, 모델 구조 자체가 최적화 관점에서 어려움을 가지고 있음을 시사한다.&lt;/p&gt;
&lt;p&gt;이에 저자는 identity mapping 을 모델이 구조적으로 쉽게 표현할 수 있도록 residual function을 도입한 ResNet 구조를 제안한다. 이는 단순히 더 깊은 네트워크를 가능하게 한 것이 아니라, 존재하지만 SGD가 찾기 어려웠던 해를 구조적으로 쉽게 최적화할 수 있도록 만든 설계이다.&lt;/p&gt;
&lt;h2 id=&quot;residual-block&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#residual-block&quot; aria-label=&quot;Link to “Residual Block”&quot;&gt;Residual Block&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%201.png&quot; alt=&quot;image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 degradation 문제를 해결하기 위해 여러 개의 레이어를 단순히 쌓아서 원하는 함수를 직접 근사하길 기대하는 대신, 레이어들이 Residual 함수를 학습하도록 명시적으로 설계한다.&lt;/p&gt;
&lt;p&gt;학습되길 원하는 목표 함수를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 라고 할 때, 비선형 레이어들이&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo lspace=&quot;0.2222em&quot; rspace=&quot;0em&quot;&gt;:&lt;/mo&gt;&lt;mo lspace=&quot;0em&quot;&gt;=&lt;/mo&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;라는 또 다른 함수를 학습하도록 한다.&lt;/p&gt;
&lt;p&gt;즉 네트워크가 직접 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 학습하는 것이 아니라, 입력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에서 얼마나 변화해야 하는지, 즉 변화량을 의미하는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 만을 학습하게 하는 것이다. 이를 이항하면 목표 함수는&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;로 다시 표현된다.&lt;/p&gt;
&lt;p&gt;저자는 참조 없이 처음부터 학습해야 하는 기존의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 보다, Residual 함수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 최적화하는 것이 더 쉽다고 주장한다. 특히 최적의 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 항등 함수인 경우, 여러 비선형 레이어를 통해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 학습하는 것보다 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 을 만드는 것이 훨씬 쉽기 때문이다.&lt;/p&gt;
&lt;p&gt;즉, Plain Network에서는 모델이 항등함수를 스스로 만들어야 하므로 최적화가 어렵지만, ResNet에서는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 0이 되기만 하면 되므로 최적화가 훨씬 수월해진다는 것이 핵심 주장이다.&lt;/p&gt;
&lt;p&gt;저자는 Residual block이&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;형태를 가질 때, 입력 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 가 조금 변하더라도 출력은 항상 입력 변화가 그대로 전달되는 경로를 포함하게 되므로 여러 residual block을 깊게 쌓더라도 기울기가 레이어를 지나며 급격히 줄어들거나 커지지 않고 안정적으로 전달되며 결과적으로 깊은 네트워크에서도 기울기 소실이나 폭발을 완화할 수 있다고 주장한다.&lt;/p&gt;
&lt;p&gt;이러한 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 구조는 shortcut connection을 가진 forward 신경망으로 구현할 수 있으며, 곱셈이 아닌 덧셈 연산만을 사용하므로 추가적인 파라미터나 계산 복잡도를 요구하지 않는다. 전체 네트워크는 기존과 동일하게 SGD와 역전파를 통해 end-to-end로 학습할 수 있다.&lt;/p&gt;
&lt;p&gt;저자는 CIFAR-10 데이터셋에서도 plain network에서 degradation 현상이 관측됨을 보이고, 이를 residual network로 해결함으로써 제안한 방법이 특정 데이터셋에 국한된 현상이 아니라 깊은 네트워크의 최적화 문제 전반에 적용 가능한 일반적인 방법임을 주장한다.&lt;/p&gt;
&lt;p&gt;또한 기존 네트워크보다 훨씬 더 깊은 구조에서도 모델을 안정적으로 학습시켰으며, 1000개 이상의 레이어를 가진 네트워크까지 탐구함으로써 제안한 구조가 깊이에 따른 최적화 한계를 실질적으로 극복함을 보였다.&lt;/p&gt;
&lt;h1 id=&quot;related-works&quot;&gt;Related Works&lt;/h1&gt;
&lt;h2 id=&quot;residual-representations&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#residual-representations&quot; aria-label=&quot;Link to “Residual Representations”&quot;&gt;Residual Representations&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;vlad&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#vlad&quot; aria-label=&quot;Link to “VLAD”&quot;&gt;VLAD&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;이미지 분류/검색 문제에서는 이미지의 수천개의 로컬 특징들을 하나의 고정길이 벡터로 요약하여야 하는데, 고전적인 BoVW 기법에서는 “어떤 패턴이 몇번 나왔나” 만 카운팅한다. 곧 평균적인 정보만 남고 세부 구조는 소실되는데, VLAD는 각 로컬 특징 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 에 대해 가장 가까운 대표 벡터 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 를 찾아 residual &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;c&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 을 누적한다. 즉 특징 자체가 아니라 기준점에서의 잔차를 표현으로 사용한다.&lt;/p&gt;
&lt;p&gt;residual 은 평균이 0 근처이며 분포가 단순하여 shallow 구조에서도 높은 성능을 냈다. 즉 VLAD 는 네트워크가 깊지 않아도 residual 표현 만으로 높은 성능을 기록했으며, 이는 residual learning 으로 문제를 해결한 연구 중 하나이다.&lt;/p&gt;
&lt;h3 id=&quot;multigrid&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#multigrid&quot; aria-label=&quot;Link to “Multigrid”&quot;&gt;Multigrid&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;편미분방정식을 직접 풀면 변수가 많고 수렴이 매우 느리다. multigrid 는 해를 한번에 정확하게 구하는 것이 아닌 Coarse grid 에서 근사값을 찾고, 그 결과를 finer grid 로 가져와 남은 오차(residual) 을 보정한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mtext&gt;solution&lt;/mtext&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mtext&gt;coarse solution&lt;/mtext&gt;&lt;mrow&gt;&lt;mspace width=&quot;0.222em&quot;&gt;&lt;/mspace&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mspace width=&quot;0.222em&quot;&gt;&lt;/mspace&gt;&lt;/mrow&gt;&lt;mtext&gt;residual correction&lt;/mtext&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;즉, 전채 해가 아니라 이미 있는 근사에서 residual 을 품으로써 수렴 속도가 빨라진다.&lt;/p&gt;
&lt;p&gt;저자는 VLAD와 Multigrid에서 residual representation을 도입함으로써 문제를 직접 해결하는 대신 기준 해로부터의 차이만을 다루도록 재정식화하면 최적화가 단순해지고 수렴이 빨라진다는 점을 보여준다.&lt;/p&gt;
&lt;p&gt;이러한 관찰을 바탕으로, 신경망에서도 전체 함수를 직접 학습하는 대신 입력에 대한 residual function을 학습하도록 문제를 재정식화한 것이 논문의 residual learning이며, 이는 구조적 트릭이 아니라 정당한 최적화 전략임을 주장한다.&lt;/p&gt;
&lt;h2 id=&quot;shortcuts-connections&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#shortcuts-connections&quot; aria-label=&quot;Link to “Shortcuts Connections”&quot;&gt;Shortcuts Connections&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id=&quot;shortcut-used-researches&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#shortcut-used-researches&quot; aria-label=&quot;Link to “Shortcut used researches”&quot;&gt;Shortcut used researches&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;shortcut / skip connection 개념은 ResNet 논문의 저자가 처음 주장한 기법이 아니고 아이디어 자체는 오래되었다. shortcut 을 사용한 대표적인 사례 중 하나는 초기 MLP 에서 입력을 그대로 출력 그대로 보내는 선형 경로는 추가하는 것이었다. 하지만 이는 residual learning 의 목적은 아니었고, 학습 초기에 출력이 완전히 엉뚱해지는 것을 방지하기 위함이었다.&lt;/p&gt;
&lt;p&gt;또 다른 사례는 auxiliary classifiers 로, deep network 에서 기울기 소실을 막기 위해 도입되었다. 중간 레이어에 보조 분류기를 추가하므로써 loss 를 중간에서 직접 걸어준다. 즉 gradient 를 강제로 중간까지 끌어오는 아이디어이다. 하지만  이는 학습 과정에서만 gradient를 보조적으로 공급하는 장치이며,  inference 시의 함수 형태에는 아무런 제약도 가하지 않기 때문에 학습 타이밍의 테크닉에 그친다.&lt;/p&gt;
&lt;p&gt;Centering layer responses 는 BN 과 유사한 방법론으로, activation 의 평균을 0 근처로 유지해 gradient 의 스케일을 안정화한다. 하지만 BN과 마찬가지로 통계적 안정화가 목적이기 때문에, 네트워크가 근사해야 할 함수 공간 자체는 변하지 않았으며, 단지 activation과 gradient의 분포만을 안정화하는 역할에 그친다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%202.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;Inception network 에서도 비슷한 개념이 사용되었는데, 이전 레이어의 출력에서 1x1 conv, 3x3 conv, 5x5 conv, 3x3 max pool 의 누적 값을 연산에서 사용하는데, 이때 3x3 max pool 을 identity mapping 과 유사한 값으로 볼 수 있다. 하지만 3개 커널의 conv 결과와 pool 의 결과를 concatenation 한다는 점에서 완전한 shortcut 으로 볼 수 없고, pool 이 기본 해가 아니기 때문에 residual 만 학습한다는 제약은 없다.&lt;/p&gt;
&lt;h3 id=&quot;highway-network&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#highway-network&quot; aria-label=&quot;Link to “Highway Network”&quot;&gt;Highway Network&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;highway network 는 깊은 네트워크에서의 vanishing gradient 문제를 해결하기 위해 연구된 주제로, ResNet 의 등장 배경과 가장 유사하다. 기본적인 접근은 vanishing gradient 문제를 정보 흐름을 제어하는 gate 로 해결하는 방식이다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;b&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msub&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 추가되는 파라미터로, 레이어를 항등함수로 사용할건지 (즉, 레이어를 건너뛸지) 혹은 레이어에서 값을 transfrom (레이어를 사용할지) 할건지 결정하게 된다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이고, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이면 레이어를 사용, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이면 레이어를 건너뛰게 된다. 즉, 필요한 레이어인지 아닌지 모델이 판단하게 하여 gradient 의 흐름을 유지한다.&lt;/p&gt;
&lt;p&gt;하지만 ResNet 논문의 저자는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 학습되는 값이라는 점에서 Highway Network는 항상 residual function을 학습하도록 구조적으로 보장하지 못한다고 지적한다. 또한 gating을 위한 추가 파라미터가 레이어마다 도입되므로, 네트워크 깊이가 증가할수록 파라미터 수와 연산량, 그리고 최적화 복잡도가 함께 증가한다.&lt;/p&gt;
&lt;h1 id=&quot;deep-residual-learning&quot;&gt;Deep Residual Learning&lt;/h1&gt;
&lt;h2 id=&quot;residual-learning&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#residual-learning&quot; aria-label=&quot;Link to “Residual Learning”&quot;&gt;Residual Learning&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;깊은 비선형 네트워크가 복잡한 함수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 를 근사할 수 있다면, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 도 근사할 수 있어야한다. 즉 표현력의 관점에서 두 함수는 동일한 표현력 안에서 근사될 수 있다.&lt;/p&gt;
&lt;p&gt;Introduce 에서 언급되었 듯 Residual Learning 은 레이어가 출력 전체를 만들지 않고, 입력에서 얼마나 바뀌는 지만 학습하게 하고, ( &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; ) 레이어가 항등함수로 학습되어야 한다면 여러 비선형 레이어가 정교하게 상쇄되어야 하는 것에 비해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 가중치가 0이 되면 되므로 난이도가 훨씬 쉽다.&lt;/p&gt;
&lt;p&gt;이러한 재정식화는 최적 함수가 항등함수 자체일 필요는 없지만, 항등함수에 가까운 경우 특히 효과적이다. 만약 최적 함수가 plain network의 기준점인 zero mapping보다 항등함수&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;에 가깝다면, Residual Learning 이 최적화에 유리할 것 이다. 저자는 이후의 실험을 통해 실제로 학습된 residual function의 출력이 작다는 점을 보이며, 많은 경우 최적 함수가 identity mapping 근처에 위치함을 경험적으로 확인한다.&lt;/p&gt;
&lt;p&gt;즉, 저자는 표현력은 동일하더라도 학습해야 할 함수를 어떻게 정의하느냐에 따라 최적화 난이도가 달라질 수 있으며, 특히 항등함수를 기준점으로 삼는 residual learning이 깊은 네트워크의 최적화를 크게 용이하게 만든다고 주장한다.&lt;/p&gt;
&lt;h2 id=&quot;identity-mapping-by-shortcuts&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#identity-mapping-by-shortcuts&quot; aria-label=&quot;Link to “Identity Mapping by Shortcuts”&quot;&gt;Identity Mapping by Shortcuts&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Residual 은 네트워크 전체에 한번만 사용되는 것이 아니며 여러 레이어를 묶은 블록 단위로 반복 적용된다. 즉 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 몇개의 레이어가 학습해야할 목표 함수이며, 여기서 말하는 레이어는 전체 네트워크가 아니라 일부 블록을 의미한다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 블록의 입력, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 블록의 출력, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 은 모델이 학습해야할 residual mapping, &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 은 항상 통과되는 identity shortcut 을 의미한다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 는 보통 2~3개의 레이어로 이루어진 작은 서브 네트워크이며, 레이어가 2개라고 가정하면:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mtext&gt;max&lt;/mtext&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;이다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이후 한번의 활성함수를 거친 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 블럭의 출력이 된다. (second nonlinearity after the addition**)**&lt;/p&gt;
&lt;p&gt;이 식의 shortcut connection 은 계산 복잡도를 올리지 않는다. 이는 실용적일 뿐만 아니라 Plain Network 와 Residual Network 를 비교하는 데 있어서도 중요하다. 무시할 수 있을 정도의 원소별 덧셈을 제외하고, 파라미터 수, 깊이, 너비, 계산 비용이 동일한 공정한 비교가 가능해진다.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 가 원소별 덧셈이기 때문에 두 값의 차원은 동일해야 한다. 만약 그렇지 않을 경우 선형 투영 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 를 수행하여 차원을 맞출 수 있다.&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;실험을 통해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 와 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 의 차원을 같게 만들어도 충분히 효과적임이 확인되었으므로 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt;는 차원을 맞춰야 하는 경우에만 사용한다. residual 함수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt;의 형태는 유연하다. 본 논문의 실험에서는 두 개 또는 세 개의 층을 갖는 함수 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;/math&gt;&lt;/span&gt; 만을 사용하지만 그보다 많은 층을 사용하는 것도 가능하다.&lt;/p&gt;
&lt;p&gt;하지만 만약 레이어 하나로만 구성하는 경우 Plain Network 와 유사해진다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/msub&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이 경우 특별한 이점을 관측할 수 없다.&lt;/p&gt;
&lt;p&gt;위 식들은 표기의 단순함을 위해 FC 층을 기준으로 설명되었지만 Convolution 층에도 그대로 적용될 수 있다. &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;{&lt;/mo&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;}&lt;/mo&gt;&lt;/mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 는 여러개의 Convolution 층으로 나타낼 수 있고, 원소별 덧셈 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mo form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 은 두 개의 feature map 에 대해 채널별로 수행된다.&lt;/p&gt;
&lt;h2 id=&quot;vgg-plain-network-and-residual-network&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#vgg-plain-network-and-residual-network&quot; aria-label=&quot;Link to “VGG, Plain Network and Residual Network”&quot;&gt;VGG, Plain Network and Residual Network&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;저자는 여러 종류의 plain 네트워크와 residual 네트워크를 실험했고, 깊어질수록 plain 네트워크는 training error 자체가 증가하는 반면 residual 네트워크는 안정적으로 최적화된다는 일관된 현상을 관찰했다.&lt;/p&gt;
&lt;p&gt;이러한 차이가 다른 요인 때문이라는 반박을 차단하기 위해, “Residual Network는 깊은 네트워크에서도 학습에 유리하다”는 주장을 검증하기에 충분히 강한 Plain Network를 의도적으로 설계하였다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;VGG 의 철학을 따른다. → 이미 검증된 깊이의 CNN 구조를 유지한다.&lt;/li&gt;
&lt;li&gt;모든 Conv 레이어에서 커널 차원을 3x3 로 고정한다. → 레이어의 깊이만이 핵심 변수로 남도록 한다.&lt;/li&gt;
&lt;li&gt;feature map 해상도가 내려가면 채널 수를 올린다. → 레이어 간 연산양(FLOPs)을 일관되도록 유지한다.&lt;/li&gt;
&lt;li&gt;다운샘플링은 pooling 대신 stride conv 로 한다 → 사람이 네트워크의 구조에 개입하지 않는다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;즉 저자는 ResNet 의 효용을 regularization도, 파라미터 수도, 연산량도 아니라 최적화 가능한 구조 때문임을 증명하려고 한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%203.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;구성한 Plain Network 에 Shortcut 을 추가한 Residual Network 도 구성한다. 이 때 Block 의 입력과 출력의 채널 수가 다를 때 두가지 옵션을 테스트한다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Option A - 채널 증가 시 shortcut은 identity를 유지하고, 부족한 채널만 0으로 padding&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Option B - 차원을 맞추기 위해 1x1 conv 를 통해 선형 투영을 수행한다.&lt;/strong&gt;&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot;&gt;→&lt;/mo&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;두 옵션 모두, shortcut 이 해상도를 건너뛰어야 할 때는 stride of 2 로 구현한다.&lt;/p&gt;
&lt;p&gt;즉, 동일한 해상도 간 연결:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;해상도가 바뀌는 지점 (&lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;/math&gt;&lt;/span&gt;):&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mtable displaystyle=&quot;true&quot; class=&quot;tml-jot&quot;&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mi&gt;H&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mi scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;H&lt;/mi&gt;&lt;mn scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mfrac&gt;&lt;mi scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;W&lt;/mi&gt;&lt;mn scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml&quot; style=&quot;math-depth:0;&quot;&gt;~&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;true&quot;&gt;{&lt;/mo&gt;&lt;mtable&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;[&lt;/mo&gt;&lt;msub&gt;&lt;mtext&gt;stride&lt;/mtext&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;]&lt;/mo&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mtext&gt;(Option A)&lt;/mtext&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;msubsup&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;mrow&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mo separator=&quot;true&quot;&gt;,&lt;/mo&gt;&lt;mspace width=&quot;0.2778em&quot;&gt;&lt;/mspace&gt;&lt;mtext&gt;stride&lt;/mtext&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mrow&gt;&lt;/msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;mtd class=&quot;tml-left&quot; style=&quot;padding-left:1em;padding-right:0em;&quot;&gt;&lt;mtext&gt;(Option B)&lt;/mtext&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;true&quot;&gt;&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mspace width=&quot;1em&quot;&gt;&lt;/mspace&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml&quot; style=&quot;math-depth:0;&quot;&gt;~&lt;/mo&gt;&lt;/mover&gt;&lt;mo&gt;∈&lt;/mo&gt;&lt;msup&gt;&lt;mi&gt;ℝ&lt;/mi&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mi scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;H&lt;/mi&gt;&lt;mn scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mfrac&gt;&lt;mi scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;W&lt;/mi&gt;&lt;mn scriptlevel=&quot;2&quot; style=&quot;math-depth:2;&quot;&gt;2&lt;/mn&gt;&lt;/mfrac&gt;&lt;mo&gt;×&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;C&lt;/mi&gt;&lt;/mrow&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;mtr&gt;&lt;mtd style=&quot;padding-left:0em;padding-right:0em;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;y&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mover&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo stretchy=&quot;false&quot; class=&quot;chr-sml wbk-sml&quot; style=&quot;math-depth:0;&quot;&gt;~&lt;/mo&gt;&lt;/mover&gt;&lt;/mrow&gt;&lt;/mtd&gt;&lt;/mtr&gt;&lt;/mtable&gt;&lt;/math&gt;&lt;/div&gt;
&lt;h3 id=&quot;implementation-details&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#implementation-details&quot; aria-label=&quot;Link to “Implementation Details”&quot;&gt;Implementation Details&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;저자는 구성한 네트워크를 ImageNet 으로 실험하기 위해 다음과 같이 셋업하였다:&lt;/p&gt;
&lt;p&gt;| &lt;strong&gt;Data Augmentation&lt;/strong&gt; | 1. Scale Augmentation&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;이미지의 짧은 변을 256 ~ 480 사이에서 랜덤하게 resize&lt;/li&gt;
&lt;/ul&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;Random crop + flip&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;resized 이미지에서 224x224 를 랜덤 crop 하고 mean subtraction&lt;/li&gt;
&lt;/ul&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;Color Augmentation&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;
































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;항목&lt;/th&gt;&lt;th&gt;세부 내용&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Color Augmentation&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;AlexNet 계열에서 도입된 색상 노이즈 적용&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Batch Normalization&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;각 convolution 뒤, activation 앞에 BN 적용&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Weight Initialization&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;ReLU 친화적 He initialization 사용&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Optimization &amp;#x26; Batch Size&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;mini-batch 256의 SGD 사용, learning rate 0.1에서 시작, 성능 정체 시 1/10로 감소, momentum 0.9 적용&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Training Length&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;최대 600k iteration까지 학습&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;strong&gt;Regularization&lt;/strong&gt;&lt;/td&gt;&lt;td&gt;weight decay 1e-4&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;10-crop testing:&lt;/strong&gt;
CNN 기반 이미지 분류에서 테스트 시점에 입력을 여러 번 변형해서 추론하고, 그 결과를 평균(또는 투표)하는 기법&lt;/li&gt;
&lt;/ul&gt;
&lt;h1 id=&quot;experiments&quot;&gt;Experiments&lt;/h1&gt;
&lt;h2 id=&quot;imagenet-classification&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#imagenet-classification&quot; aria-label=&quot;Link to “ImageNet Classification”&quot;&gt;ImageNet Classification&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%204.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;plain-networks&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#plain-networks&quot; aria-label=&quot;Link to “Plain Networks”&quot;&gt;Plain Networks&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%205.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 residual 연결이 없는 plain 네트워크에서 깊이만 증가시켰을 때 어떤 현상이 발생하는지를 확인하기 위해, 구조는 거의 동일하고 깊이만 다른 18-layer와 34-layer plain 네트워크를 비교하였다. 실험 결과, 더 깊은 34-layer plain 네트워크가 오히려 18-layer 네트워크보다 validation error가 더 높게 나타났다. 이 차이가 과적합 때문인지 확인하기 위해 학습 과정 전반의 training/validation error를 분석한 결과, 34-layer 모델은 학습 초기부터 종료까지 training error 자체가 더 높게 유지되는 degradation 문제를 보였다.&lt;/p&gt;
&lt;p&gt;저자는 깊은 plain 네트워크에서 나타나는 최적화 어려움이 단순히 gradient vanishing 때문이라고 보기는 어렵다고 주장한다. 네트워크는 모두 Batch Normalization을 사용해 학습되었으며, BN은 순전파 과정에서 활성값의 분산을 안정적으로 유지해 신호가 사라지는 현상을 완화한다. 더 나아가 저자는 BN을 사용한 상태에서 역전파되는 gradient의 크기를 실제로 관측해 보았고, 그 값들이 정상적인 범위에 있음을 확인하였다.&lt;/p&gt;
&lt;p&gt;따라서 gradient가 소실되어 학습이 실패하는 상황은 아니라는 결론에 이른다. 실제로 34-layer plain 네트워크는 비록 얕은 모델보다 성능이 낮지만 완전히 학습이 붕괴된 것은 아니며, 이는 최적화가 어느 정도는 작동하고 있음을 보여준다. 저자는 대신 깊은 plain 네트워크가 깊이에 따라 수렴 속도가 급격히 느려지는 문제를 겪고 있을 가능성을 제기하며, 이러한 최적화 난이도의 근본 원인은 향후 연구 과제로 남긴다.&lt;/p&gt;
&lt;h3 id=&quot;residual-network&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#residual-network&quot; aria-label=&quot;Link to “Residual Network”&quot;&gt;Residual Network&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;저자는 이제 residual network 를 평가한다. 기본 아키텍쳐는 이미 언급하였듯 plain 네트워크와 동일하며, shortcut 의 사용 여부만 다르다. 해상도나 채널 수가 증가하는 지점에서는 Option A 를 사용하여 테스트한다. Option A 를 사용할 경우, plain network 와 residual network 의 파라미터 수는 완벽히 동일하게 된다. (Option B 사용 시 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;msub&gt;&lt;mi&gt;W&lt;/mi&gt;&lt;mi&gt;s&lt;/mi&gt;&lt;/msub&gt;&lt;/math&gt;&lt;/span&gt; 가 추가됨)&lt;/p&gt;
&lt;p&gt;실험 결과는 plain 네트워크와는 달리 상황이 완전히 뒤집혀, 34-layer ResNet이 18-layer ResNet보다 더 좋은 성능을 보인다(약 2.8% 향상). 둘째이자 더 중요한 점은 34-layer ResNet이 training error 자체가 크게 낮아졌고, 그 성능이 validation 데이터에도 잘 일반화된다는 것이다. 이는 앞서 plain 네트워크에서 관찰되었던 degradation 문제가 residual learning을 통해 효과적으로 해결되었음을 의미한다.&lt;/p&gt;
&lt;p&gt;34-layer 기준에서 residual 구조를 적용한 ResNet은 plain 네트워크보다 training error를 효과적으로 낮추며, 그 결과 top-1 error를 크게 개선한다. 이는 residual learning이 깊은 네트워크에서도 최적화를 가능하게 만들어 깊이 증가가 실제 성능 향상으로 이어질 수 있음을 보여준다. 반면 18-layer처럼 상대적으로 얕은 네트워크에서는 plain과 residual의 최종 정확도 차이는 거의 없지만, ResNet은 학습 초반 수렴 속도를 개선해 최적화를 더 빠르고 안정적으로 만드는 역할을 한다.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Identity vs Projection Shortcuts&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%206.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 identity shortcut만으로도 깊은 네트워크의 학습이 충분히 개선된다는 점을 보인 뒤, projection shortcut의 필요성을 추가로 검증한다. zero-padding 기반 identity shortcut(Option A), 해상도 변경 지점에서만 projection을 쓰는 방식(Option B), 모든 shortcut을 projection으로 사용하는 방식(Option C)을 비교한 결과, 세 옵션 모두 plain 네트워크보다 크게 우수했으며, 성능 차이는 매우 작았다. B와 C가 A보다 약간 나은 성능을 보이긴 했지만, 이는 zero-padding된 채널이 residual 학습을 하지 못하는 점이나 projection으로 인한 추가 파라미터 효과로 설명 가능하다.&lt;/p&gt;
&lt;p&gt;저자는 메모리 사용량과 연산량, 모델 크기를 줄이기 위해 이후의 실험에서는 Option C를 사용하지 않는다. 특히 identity shortcut은 이후에 도입될 bottleneck 구조에서도 모델 복잡도를 증가시키지 않는다는 점에서 중요하다.&lt;/p&gt;
&lt;h3 id=&quot;bottleneck-architectures&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#bottleneck-architectures&quot; aria-label=&quot;Link to “Bottleneck Architectures”&quot;&gt;Bottleneck Architectures&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%207.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;더 깊은 ResNet을 효율적으로 학습하기 위해 저자는 bottleneck 구조를 도입했으며, 이는 1×1–3×3–1×1 convolution으로 구성되어 비싼 3×3 연산을 저차원 공간에서만 수행하도록 만든 설계다. 이 구조는 레이어 수를 늘리면서도 연산량을 기존 설계와 비슷하게 유지할 수 있게 해준다. 이러한 bottleneck 구조에서는 shortcut이 고차원 feature에 직접 연결되므로, projection shortcut을 사용하면 연산량과 파라미터 수가 크게 증가한다. 따라서 파라미터와 연산 비용이 없는 identity shortcut (Option A) 이 bottleneck 구조에서 특히 중요하며, 이를 통해 매우 깊으면서도 효율적인 ResNet 모델을 구성할 수 있다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%208.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;또한 bottleneck 구조를 도입해 50, 101, 152-layer ResNet을 구성한 결과, 네트워크 깊이를 크게 늘렸음에도 연산량은 VGG보다 낮은 수준으로 유지되었고, degradation 문제 없이 깊이 증가가 곧바로 정확도 향상으로 이어졌다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%209.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;ResNet은 단일 모델만으로도 기존 최고 성능의 앙상블 모델을 뛰어넘는 결과를 달성했으며, 특히 152-layer ResNet은 단일 모델 기준 top-5 error 4.49%로 당시 state-of-the-art를 갱신했다. 서로 다른 깊이의 ResNet들을 앙상블로 결합하면 top-5 error를 3.57%까지 낮출 수 있었고, 이 결과로 ResNet은 ILSVRC 2015 이미지 분류 대회에서 우승을 차지했다.&lt;/p&gt;
&lt;h2 id=&quot;cifar-10&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#cifar-10&quot; aria-label=&quot;Link to “CIFAR-10”&quot;&gt;CIFAR-10&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%2010.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 CIFAR-10 데이터셋에서 추가 실험을 수행하며, 이 실험의 목적은 최고 성능을 달성하는 것이 아니라 매우 깊은 네트워크가 어떻게 학습되는지를 관찰하는 데 있다. 이를 위해 의도적으로 단순한 plain 및 residual 아키텍처를 사용하고, 입력은 32×32 이미지에 대해 mean subtraction 만 수행한다. 네트워크는 3x3 convolution을 기본으로 하여 feature map 크기 32, 16, 8의 세 단계로 구성되며, 각 단계마다 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;개의 레이어를 쌓아 전체 깊이를 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mn&gt;6&lt;/mn&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;+&lt;/mo&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;로 조절한다. 다운샘플링은 stride 2 convolution으로 수행되고, 마지막에는 global average pooling과 10-class 분류기를 둔다. 이 설정을 통해 저자는 구조적 트릭의 영향을 최소화한 상태에서, 네트워크 깊이가 증가할 때 plain 네트워크와 residual 네트워크의 추이를 직접 비교한다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%2011.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;네트워크 깊이는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;3,5,7,9&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 로 조절해 20층부터 56층까지 확장한다. 실험 결과, plain 네트워크는 깊이가 증가할수록 training error가 오히려 커지는 degradation 현상을 보였으며, 이는 ImageNet이나 MNIST에서도 관찰된 것과 동일한 근본적인 최적화 문제임을 시사한다. 반면 ResNet은 깊이가 깊어질수록 이러한 최적화 어려움을 극복하고, 일관된 정확도 향상을 보인다. 즉, CIFAR-10에서도 residual 구조는 깊은 네트워크의 학습을 가능하게 하며, 깊이 증가의 이점을 안정적으로 실현시킨다는 점이 확인된다.&lt;/p&gt;
&lt;p&gt;저자는 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;18&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 까지 확장하여 테스트한다. 아주 깊은 110-layer 네트워크에서도 residual 구조는 간단한 warm-up만으로 안정적인 학습을 가능하게 하며, 적은 파라미터로도 최고 수준의 성능을 달성한다.&lt;/p&gt;
&lt;h3 id=&quot;analysis-of-layer-response&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#analysis-of-layer-response&quot; aria-label=&quot;Link to “Analysis of Layer Response”&quot;&gt;Analysis of Layer Response&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/assets/251231/image%2012.png&quot; alt=&quot;251231/image.png&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;&gt;&lt;/p&gt;
&lt;p&gt;저자는 각 3x3 convolution 레이어의 출력값 std 을 분석한다. 여기서 말하는 레이어 응답은 BN 이후, ReLU나 shortcut 덧셈 이전의 값으로, ResNet에서는 곧 residual function &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 의 크기를 의미한다.&lt;/p&gt;
&lt;p&gt;레이어 응답의 std 라고 하면 레이어 유닛들의 응답값에 대한 분포를 의미하지만:&lt;/p&gt;
&lt;div class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;block&quot; class=&quot;tml-display&quot; style=&quot;display:block math;&quot;&gt;&lt;mrow&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;msqrt&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mfrac&gt;&lt;mrow&gt;&lt;munderover&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/munderover&gt;&lt;/mrow&gt;&lt;msup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;msub&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;/msub&gt;&lt;mo&gt;−&lt;/mo&gt;&lt;mi&gt;μ&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msup&gt;&lt;/mrow&gt;&lt;/msqrt&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/div&gt;
&lt;p&gt;에서, BN에 의해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;μ&lt;/mi&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;mn&gt;0&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이므로,  &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;σ&lt;/mi&gt;&lt;mo&gt;≈&lt;/mo&gt;&lt;msqrt&gt;&lt;mrow&gt;&lt;mfrac&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/mfrac&gt;&lt;msubsup&gt;&lt;mo movablelimits=&quot;false&quot;&gt;∑&lt;/mo&gt;&lt;mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;1&lt;/mn&gt;&lt;/mrow&gt;&lt;mi&gt;N&lt;/mi&gt;&lt;/msubsup&gt;&lt;mi&gt;F&lt;/mi&gt;&lt;msubsup&gt;&lt;mrow&gt;&lt;mo fence=&quot;true&quot; form=&quot;prefix&quot; stretchy=&quot;false&quot;&gt;(&lt;/mo&gt;&lt;mi&gt;x&lt;/mi&gt;&lt;mo fence=&quot;true&quot; form=&quot;postfix&quot; stretchy=&quot;false&quot;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;mi&gt;i&lt;/mi&gt;&lt;mn&gt;2&lt;/mn&gt;&lt;/msubsup&gt;&lt;/mrow&gt;&lt;/msqrt&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt; 이 되므로 레이어 응답의 strength 로 볼 수도 있다.&lt;/p&gt;
&lt;p&gt;분석 결과, ResNet의 레이어 응답은 대응되는 plain 네트워크보다 전반적으로 더 작은 값을 가지며, 이는 residual 함수가 원래의 non-residual function 에 비해 0에 더 가까운 값을 학습한다는 저자의 가정을 뒷받침한다. 또한 ResNet-20, 56, 110을 비교해 보면, 네트워크가 더 깊어질수록 각 레이어의 응답 크기가 더 작아지는 경향이 관찰된다. 이는 ResNet이 깊어질수록 개별 레이어가 입력 신호를 크게 변형하기보다는, 아주 작은 수정만을 점진적으로 누적하는 방식으로 동작한다는 것을 의미한다.&lt;/p&gt;
&lt;p&gt;즉, 저자가 의도한 최적화 경로로 실제로 SGD가 움직였다.&lt;/p&gt;
&lt;h3 id=&quot;over-1000-resnet-layers&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#over-1000-resnet-layers&quot; aria-label=&quot;Link to “Over 1000 ResNet Layers”&quot;&gt;Over 1000 ResNet Layers&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;저자는 CIFAR-10에서 residual 구조의 한계를 시험하기 위해 &lt;span class=&quot;arithmatex&quot;&gt;&lt;math display=&quot;inline&quot;&gt;&lt;mrow&gt;&lt;mi&gt;n&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mn&gt;200&lt;/mn&gt;&lt;/mrow&gt;&lt;/math&gt;&lt;/span&gt;, 즉 1202-layer에 달하는 초심층 ResNet까지 실험을 확장한다. 이 모델은 학습 과정에서 최적화 문제를 전혀 보이지 않으며, training error를 0.1% 이하로 낮출 수 있을 정도로 안정적으로 수렴한다. 이는 residual 구조가 깊이가 1000층을 넘어가더라도 최적화 자체에는 본질적인 한계가 없음을 보여준다. 다만 테스트 성능에서는 1202-layer 모델이 110-layer 모델보다 오히려 나쁜 결과를 보이는데, 두 모델의 training error가 비슷하다는 점에서 이를 과적합 문제로 해석한다.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>10x Faster C++ String Split</title>
    <link href="https://bayernmuller.github.io/posts/250809-10x-faster-cpp-string-split/" />
    <id>https://bayernmuller.github.io/posts/250809-10x-faster-cpp-string-split/</id>
    <published>2025-08-09T00:00:00.000Z</published>
    <updated>2025-08-09T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="C++" /><category term="Performance" /><category term="std::string" /><category term="blazing.cpp" /><category term="Korean" />
    <summary>How to split a string 10x faster in C++</summary>
    <content type="html">&lt;p&gt;지난 포스트들에서 SIMD 인스트럭션과 SIMD 를 string 에 활용하는 법에 대해 포스팅을 했었는데요, 오늘은 &lt;a href=&quot;https://ashvardanian.com/posts/splitting-strings-cpp/&quot;&gt;@ashvardanian&lt;/a&gt;의 &lt;code&gt;splitting-strings-cpp&lt;/code&gt; 포스팅을 리뷰하면서 &lt;code&gt;split&lt;/code&gt; 함수를 구현해보려고 합니다.&lt;/p&gt;
&lt;p&gt;특히 저의 경우 Python 에서 제일 많이 사용하는 함수 중 하나가 &lt;code&gt;split&lt;/code&gt; 입니다.&lt;/p&gt;
&lt;p&gt;이 포스트에서는 문자열을 나누는 여러 방법을 살펴보고, 그 중 가장 빠른 방법과 그 구현, 장단점을 살펴보려고 합니다.&lt;/p&gt;
&lt;h2 id=&quot;problem-definition&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#problem-definition&quot; aria-label=&quot;Link to “Problem Definition”&quot;&gt;Problem Definition&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;문제를 정의 해봅시다. &lt;code&gt;split&lt;/code&gt; 은 바이트 시퀀스와 delimiter 문자 집합이 주어졌을 때, 이 delimiter 를 기준으로 문자열을 나누는 함수입니다.&lt;/p&gt;
&lt;p&gt;일반적으로 공백을 기준으로 문자열을 나누죠. C언어의 로케일에서는 &lt;code&gt;&quot;\f&quot;&lt;/code&gt;, &lt;code&gt;&quot;\r&quot;&lt;/code&gt;, &lt;code&gt;&quot;\n&quot;&lt;/code&gt;, &lt;code&gt;&quot;\t&quot;&lt;/code&gt;, &lt;code&gt;&quot;\v&quot;&lt;/code&gt;, &lt;code&gt;&quot; &quot;&lt;/code&gt; 의 6개 문자가 공백 문자로 정의되어 있습니다.&lt;/p&gt;
&lt;p&gt;보통 &lt;code&gt;&quot; &quot;&lt;/code&gt; 하나만을 delimiter 로 사용하는 경우가 많습니다. 하지만 파싱을 하다 보면 XML 의 &lt;code&gt;&amp;#x3C;&lt;/code&gt;, &lt;code&gt;&gt;&lt;/code&gt;, JSON 의 &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;, &lt;code&gt;[&lt;/code&gt;, &lt;code&gt;]&lt;/code&gt; 등의 2개 이상 문자들을 delimiter 로 사용해야 하는 경우가 있습니다.&lt;/p&gt;
&lt;h3 id=&quot;solution-1-using-stdstring_viewfind_first_of&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#solution-1-using-stdstring_viewfind_first_of&quot; aria-label=&quot;Link to “Solution 1: using std::string_view::find_first_of”&quot;&gt;Solution 1: using &lt;code&gt;std::string_view::find_first_of&lt;/code&gt;&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; callback_type_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; delimiters&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;           callback_type_&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;find_first_of&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(delimiters, pos);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(pos, next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::npos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;                                                 :&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;std::string_view::find_first_of&lt;/code&gt; 는 문자열에서 주어진 문자 집합에 포함된 문자가 처음으로 나타나는 위치를 찾아줍니다.&lt;/p&gt;
&lt;p&gt;내부적으로 순차 탐색을 하는데, delimiters 의 길이가 늘어나면 성능이 확 떨어지죠.&lt;/p&gt;
&lt;p&gt;굳이 시간 복잡도로 보면 &lt;code&gt;O(n * len(delimiters))&lt;/code&gt; 라고 볼 수 있습니다.&lt;/p&gt;
&lt;h3 id=&quot;solution-2-using-stdfind_if-and-lambda&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#solution-2-using-stdfind_if-and-lambda&quot; aria-label=&quot;Link to “Solution 2: using std::find_if and lambda”&quot;&gt;Solution 2: using &lt;code&gt;std::find_if&lt;/code&gt; and lambda&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;만약 delimiter 를 판단하는 커스텀 람다 함수를 전달하면, &lt;code&gt;O(n)&lt;/code&gt; 으로 해결할 수 있겠습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; callback_type_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; predicate_type_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split_with_predicate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;predicate_type_&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; is_delimiter&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;callback_type_&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;()) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;find_if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos, str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(), is_delimiter) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(pos, next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; next_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id=&quot;benchmark-solution-1-vs-solution-2&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#benchmark-solution-1-vs-solution-2&quot; aria-label=&quot;Link to “Benchmark: solution 1 vs solution 2”&quot;&gt;Benchmark: solution 1 vs solution 2&lt;/a&gt;&lt;/h4&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;inline&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; is_delimiter&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; ' '&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\f&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\v&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_Split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;This is a sample text to be split using delimiters.&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot; &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\t\n\r\f\v&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, [](&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;            benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_SplitWithPredicate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;This is a sample text to be split using delimiters.&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        split_with_predicate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, is_delimiter, [](&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;            benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_Split);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_SplitWithPredicate);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK_MAIN&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;






















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;strong&gt;-O0&lt;/strong&gt; Benchmark&lt;/th&gt;&lt;th&gt;Time&lt;/th&gt;&lt;th&gt;CPU&lt;/th&gt;&lt;th&gt;Iterations&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;BM_Split&lt;/td&gt;&lt;td&gt;1678 ns&lt;/td&gt;&lt;td&gt;1678 ns&lt;/td&gt;&lt;td&gt;416055&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_SplitWithPredicate&lt;/td&gt;&lt;td&gt;569 ns&lt;/td&gt;&lt;td&gt;568 ns&lt;/td&gt;&lt;td&gt;1226564&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;






















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;strong&gt;-O3&lt;/strong&gt; Benchmark&lt;/th&gt;&lt;th&gt;Time&lt;/th&gt;&lt;th&gt;CPU&lt;/th&gt;&lt;th&gt;Iterations&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;BM_Split&lt;/td&gt;&lt;td&gt;41.8 ns&lt;/td&gt;&lt;td&gt;41.8 ns&lt;/td&gt;&lt;td&gt;16705647&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_SplitWithPredicate&lt;/td&gt;&lt;td&gt;37.2 ns&lt;/td&gt;&lt;td&gt;37.2 ns&lt;/td&gt;&lt;td&gt;18774709&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;컴파일러 최적화 옵션에 따라 성능 차이가 있지만, &lt;code&gt;BM_SplitWithPredicate&lt;/code&gt; 가 &lt;code&gt;BM_Split&lt;/code&gt; 보다 빠르다는 것을 확인할 수 있습니다.&lt;/p&gt;
&lt;h2 id=&quot;solution-3-using-character-bit-set&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#solution-3-using-character-bit-set&quot; aria-label=&quot;Link to “Solution 3: using character bit-set”&quot;&gt;Solution 3: using character bit-set&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;결국 문자열을 나누는 로직의 첫번쨰 과정은 문자열 내부의 delimiter 위치를 찾는 것이고, 문제를 더 잘게 쪼개면 문자열 내부의 문자가 delimiter 인지 아닌지를 판단하는 것입니다.&lt;/p&gt;
&lt;p&gt;문자가 delimiter 인지 아닌지를 판단하는 것을 비트 연산으로 접근해보곘습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; set[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; delimiters &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot; &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\t\n\r\f\v&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c : &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(delimiters)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    set[c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; is_delimiter&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; set[c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;set&lt;/code&gt; 은 문자 집합을 비트 플래그로 표현한 집합입니다. 8비트 자료형인 char 가 32개 있으니, 총 256비트의 비트 플래그를 사용할 수 있습니다.&lt;/p&gt;
&lt;p&gt;즉 만약 &lt;code&gt;A&lt;/code&gt; 가 delimiter 라면, 정수로 해석한 값은 65이므로 &lt;code&gt;set&lt;/code&gt; 의 &lt;code&gt;65 &gt;&gt; 3&lt;/code&gt; 인덱스의 &lt;code&gt;65 &amp;#x26; 7&lt;/code&gt; 번째 비트가 1이 됩니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;set[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]  &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0b&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;00000000&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;set[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]  &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0b&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;00000000&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;set[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]  &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0b&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;00000010&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt; // 'A' 의 비트 플래그가 켜짐&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;set[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;31&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0b&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;00000000&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;is_delimiter&lt;/code&gt; 함수는 문자를 정수로 해석한 값을 8로 나눈(&lt;code&gt;c / 8 == c &gt;&gt; 3&lt;/code&gt;) 몫을 인덱스로 사용합니다. 그리고 나머지(&lt;code&gt;c &amp;#x26; 7 == c % 8&lt;/code&gt;) 를 비트 마스크로 사용합니다.&lt;/p&gt;
&lt;img src=&quot;/assets/charset_0x20.png&quot; alt=&quot;charset_0x20&quot;&gt;
&lt;img src=&quot;/assets/charset_0x09.png&quot; alt=&quot;charset_0x09&quot;&gt;
&lt;p&gt;여기서 모듈러연산이 비트 연산으로 대체되는데, &lt;code&gt;c % n&lt;/code&gt; 에서 &lt;code&gt;n&lt;/code&gt; 이 2의 거듭제곱이라면 &lt;code&gt;c &amp;#x26; (n - 1)&lt;/code&gt; 로 대체할 수 있습니다.&lt;/p&gt;
&lt;p&gt;두 연산이 본질적으로 “&lt;code&gt;c&lt;/code&gt;의 하위 &lt;code&gt;k&lt;/code&gt; 비트를 분리해내는” 동일한 작업을 수행하기 때문입니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;n&lt;/code&gt; 이 2의 거듭제곱 (&lt;code&gt;2^k&lt;/code&gt;) 이라면, 2진수로 표현했을 때 단 하나의 비트만 1이고, &lt;code&gt;n - 1&lt;/code&gt; 의 경우 하위 &lt;code&gt;k&lt;/code&gt; 개 비트가 1이 됩니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TEXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TEXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0b10000000 -&gt; 2^7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0b01111111 -&gt; 2^7 - 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;c &amp;#x26; 0b01111111 == c &amp;#x26; 0b11111111&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;즉 &lt;code&gt;k&lt;/code&gt; 번째 비트까지 마스킹하는 것은 본질적으로 &lt;code&gt;c % 2^k&lt;/code&gt; 와 동일합니다.&lt;/p&gt;
&lt;p&gt;다시 문자열 비교로 돌아와서, &lt;code&gt;set&lt;/code&gt; 은 메모리 지역성이 좋은 배열이므로, 캐시 히트율이 높을 것이고 정수 연산이 아닌 비트 연산을 사용하므로 더 빠를 것입니다. L1 캐시에 상주할 가능성이 매우 높죠.&lt;/p&gt;
&lt;p&gt;이제 &lt;code&gt;==&lt;/code&gt; 연산자를 반복적으로 사용하는 이전의 방법 대신 방금 구현한 비트연산 기반 함수를 사용하여 벤치마크 해보겠습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;inline&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; is_delimiter&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; ' '&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\t&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\r&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\f&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; '&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\v&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_SplitWithPredicate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;This is a sample text to be split using delimiters.&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        split_with_predicate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, is_delimiter, [](&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;            benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_SplitWithPredicate_CharSet&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; set[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; delimiters &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot; &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;\t\n\r\f\v&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c : &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(delimiters)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        set[c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; is_delimiter_char_set &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;](&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; set[c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;This is a sample text to be split using delimiters.&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        split_with_predicate&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, is_delimiter_char_set, [](&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;            benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        });&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;






















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Benchmark&lt;/th&gt;&lt;th&gt;Time&lt;/th&gt;&lt;th&gt;CPU&lt;/th&gt;&lt;th&gt;Iterations&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;BM_SplitWithPredicate&lt;/td&gt;&lt;td&gt;44.8 ns&lt;/td&gt;&lt;td&gt;44.8 ns&lt;/td&gt;&lt;td&gt;15622768&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_SplitWithPredicate_CharSet&lt;/td&gt;&lt;td&gt;32.0 ns&lt;/td&gt;&lt;td&gt;32.0 ns&lt;/td&gt;&lt;td&gt;21921995&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;확실히 빠른 것을 알 수 있습니다. 28% 정도 빠르네요.&lt;/p&gt;
&lt;h2 id=&quot;solution-4-using-simd&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#solution-4-using-simd&quot; aria-label=&quot;Link to “Solution 4: using SIMD”&quot;&gt;Solution 4: using SIMD&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;지금까지 solution 3 의 아이디어에 SIMD 를 적용해보겠습니다.&lt;/p&gt;
&lt;p&gt;비트 마스크 기반 비교 로직을 동시에 여러 문자에 대해 적용하면 더 빨라지겠죠? ARM NEON 기반으로 구현해보겠습니다.&lt;/p&gt;
&lt;img src=&quot;/assets/split-simd.png&quot; alt=&quot;neon_split&quot;&gt;
&lt;p&gt;이 사진처럼 원리 자체는 똑같지만, 16개의 문자를 한번에 처리한다는 것이 포인트입니다.&lt;/p&gt;
&lt;p&gt;16개씩 쭉 진행하다가 자투리 부분은 스칼라로 처리하는 구현입니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;arm_neon.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;string_view&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;typedef&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; union&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; u128_vec_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u8x16;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint16x8_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u16x8;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u32x4;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint64x2_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u64x2;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u64s[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint32_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u32s[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u16s[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint8_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; u8s[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;} &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;u128_vec_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; _find_charset_neon_register&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;u128_vec_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;                                          uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; set_top&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;                                          uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; set_bottom&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* haystack 은 &quot;ABCDEFGHIJKLMNOP&quot; 라고 가정 */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; byte_index_vec &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vshrq_n_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack.u8x16, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* c &gt;&gt; 3 을 8비트 데이터 16개에 대해 동시에 수행&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     즉, 각 바이트의 인덱스가 계산된다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     - byte_index_vec 는 [65 &gt;&gt; 3, 66 &gt;&gt; 3, 67 &gt;&gt; 3, ... ] 이 된다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; byte_mask_vec &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;      vshlq_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vdupq_n_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;               vreinterpretq_s8_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vandq_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack.u8x16, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vdupq_n_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;))));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* 1 &amp;#x3C;&amp;#x3C; (c &amp;#x26; 7) 을 8비트 데이터 16개에 대해 동시에 수행 */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches_top_vec &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vqtbl1q_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(set_top, byte_index_vec);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* set_top 에서 바이트 단위 인덱싱&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     - matches_top_vec 는&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;       [set_top[65 &gt;&gt; 3], set_top[66 &gt;&gt; 3], set_top[67 &gt;&gt; 3], ... ] 이 된다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     - 즉, byte_index_vec 에 대응하는 set_top 의 값을 가져온다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches_bottom_vec &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;      vqtbl1q_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(set_bottom, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vsubq_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(byte_index_vec, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vdupq_n_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* - set_bottom 에 대해서도 바이트 단위 인덱싱 수행&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     - 16을 빼는 이유는 set_bottom 의 바이트 인덱스가 0부터 시작하기 때문이다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     - 똑같이 matches_bottom_vec 는&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;       [set_bottom[(65 &gt;&gt; 3) - 16], set_bottom[(66 &gt;&gt; 3) - 16], ... ] 이 된다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     - vqtbl1q_u8 은 범위를 넘어가면 0을 반환한다. (65 &gt;&gt; 3) - 16 은 범위를&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;       넘어가므로 (underflow), matches_bottom_vec 는 모두 0이 된다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches_vec &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vorrq_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches_top_vec, matches_bottom_vec);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* set_top 과 set_bottom 의 결과를 합친다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;  matches_vec &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vtstq_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches_vec, byte_mask_vec);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* matches_vec 와 byte_mask_vec 를 비교한다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vget_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vreinterpret_u64_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;                           vshrn_n_u16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vreinterpretq_u16_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches_vec), &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;                       0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;         0x&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;8888888888888888&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;ull&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* 1. vreinterpretq_u16_u8 로 matches_vec 를 16비트 데이터 8개로 재해석한다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     2. 비트를 오른쪽으로 4비트 시프트 후, 하위 8비트만 취한다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;        (예시: static_cast&amp;#x3C;uint8_t&gt;(n &gt;&gt; 4))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     3. vreinterpret_u64_u8 로 64비트 데이터 1개로 재해석한다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     4. vget_lane_u64 으로 그 값을 스칼라로 가져온다.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;     5. 마스킹 연산으로, 4비트 마다의 최상위 비트만 남긴다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; &amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; callback_type_&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split_neon&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string_view&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; delimiters&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;                callback_type_&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt;callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint8_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; charset[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; c : delimiters) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    charset[c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (c &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; set_top_vec_u8x16 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(charset);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; uint8x16_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; set_bottom_vec_u8x16 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(charset &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; bit_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; char_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;  u128_vec_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_vec;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; length &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;  std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; last_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* 16바이트 청크 단위로 처리한다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; length; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    haystack_vec.u8x16 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        vld1q_u8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; unsigned&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    matches &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; _find_charset_neon_register&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack_vec, set_top_vec_u8x16,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;                                          set_bottom_vec_u8x16);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (matches) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;      bit_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; __builtin_ctzll&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;      char_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; bit_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;      callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(last_pos, char_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; last_pos));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;      last_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; char_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;      matches &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;ull&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; bit_pos);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  /* 16바이트 청크 단위로 처리하지 못한 나머지 부분은 스칼라로 처리한다. */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;  if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (length &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; length &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (length &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 15&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; length) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;      if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (charset[str[pos] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (str[pos] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 7&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; length &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;        callback&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(last_pos, pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; last_pos));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        last_pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; pos &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;      pos&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;코드에 최대한 주석을 달아봤습니다. 꼼꼼히 읽어보시면 이해가 어렵지 않을 것 같습니다.&lt;/p&gt;
&lt;h2 id=&quot;benchmark&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#benchmark&quot; aria-label=&quot;Link to “Benchmark”&quot;&gt;Benchmark&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; generate_random_string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; length&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;    srand&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;cafe&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(length, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;' '&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; length; i&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        str[i] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; 'a'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; rand&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 26&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; rand&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;%&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;이 코드로 문자 1000개 정도의 문자열을 생성하여 벤치마크를 진행했습니다. (&lt;code&gt;std::mt19937&lt;/code&gt; 를 쓸 걸 그랬네요.)&lt;/p&gt;
&lt;img src=&quot;/assets/split-benchmark.png&quot; alt=&quot;split-benchmark&quot;&gt;
&lt;p&gt;최적화 결과가 매우 좋아서 Y 축은 log scale 로 표시했습니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;-O0&lt;/code&gt; 에서는 모두 고만고만 하지만, Solution 2 와 3의 경우 최적화 레벨 1~3 에서 큰 차이가 없었던 반면 SIMD 구현은 &lt;code&gt;-O1&lt;/code&gt; 에서도 좋은 성능을 보여주었습니다.&lt;/p&gt;
&lt;p&gt;결과적으로 &lt;code&gt;-O1&lt;/code&gt; 에서 solution 1 의 방식을 10배 이상 빠르게 만들 수 있었습니다.&lt;/p&gt;
&lt;h2 id=&quot;마치며&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#마치며&quot; aria-label=&quot;Link to “마치며”&quot;&gt;마치며&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;항상 그렇진 않겠지만 최적화 수준과 가독성은 trade-off 관계에 있는 것 같습니다.&lt;/p&gt;
&lt;p&gt;주석을 달아야 이해할 수 있는 코드를 그렇게 좋아하지는 않지만 어쩔 수 없는 경우도 있는 것 같네요.&lt;/p&gt;
&lt;p&gt;현업에서 이런 최적화 테크닉을 적용할 때는, 여러가지를 고려해야겠습니다.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>What’s Wrong with std::string</title>
    <link href="https://bayernmuller.github.io/posts/250802-what-wrong-with-std-string/" />
    <id>https://bayernmuller.github.io/posts/250802-what-wrong-with-std-string/</id>
    <published>2025-08-02T00:00:00.000Z</published>
    <updated>2025-08-02T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="C++" /><category term="Performance" /><category term="std::string" /><category term="blazing.cpp" /><category term="Korean" />
    <summary>Problems with std::string and how to solve them</summary>
    <content type="html">&lt;p&gt;C/C++ 의 문자열 표준은 정말 오랫동안 사용되어 왔습니다. &lt;code&gt;&amp;#x3C;string&gt;&lt;/code&gt;, &lt;code&gt;&amp;#x3C;string_view&gt;&lt;/code&gt;, &lt;code&gt;&amp;#x3C;string.h&gt;&lt;/code&gt; 등 여러 헤더파일이 있고 유용한 메서드들이 많이 있는데요, 유용하게 사용해왔던건 맞지만 타 언어에 비해 지원하는 메서드나 기능이 부족한 것은 사실입니다.&lt;/p&gt;
&lt;p&gt;이번 포수트에서는 &lt;code&gt;std::string&lt;/code&gt; 의 문제점과 이를 해결하기 위한 다양한 기법들을 &lt;a href=&quot;https://ashvardanian.com/posts/painful-strings/&quot;&gt;@ashvardanian 의 포스트&lt;/a&gt; 를 참고하여 살펴보겠습니다.&lt;/p&gt;
&lt;h2 id=&quot;모호한-오버로딩&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#모호한-오버로딩&quot; aria-label=&quot;Link to “모호한 오버로딩”&quot;&gt;모호한 오버로딩&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;std::string&lt;/code&gt; 의 &lt;code&gt;replace&lt;/code&gt; 메서드의 프로토타입은 14가지나 있습니다.&lt;/p&gt;
&lt;p&gt;아래 코드를 보고 결과를 바로 알 수 있나요?&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'a'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, {&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'a'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'b'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// and more..&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;아무리 경력이 많은 개발자라도 전부 알기는 힘들겠죠.&lt;/p&gt;
&lt;p&gt;아래는 결과입니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// F123Bayern Munich&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// F23Bayern Munich&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// F1Bayern Munich&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// F2Bayern Munich&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;123&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// F2Bayern Munich&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'a'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// FaaaBayern Munich&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, {&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'a'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'b'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;});&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// FabBayern Munich&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;오랜시간 표준이 업데이트 되면서 이런 오버라이딩이 쌓여왔죠. 메서드 뿐만 아니라 &lt;code&gt;std::string&lt;/code&gt; 을 위한 서브클래스들도 추가되어왔습니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::string_view&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::span&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;이 두 클래스는 문자열의 일부분을 참조하여 데이터 인스턴스가 중복되는 것을 방지하죠.&lt;/p&gt;
&lt;p&gt;이렇게 문자열의 일부를 표현할 수 있는 클래스들이 있음에도, 왜 여전히 &lt;code&gt;std::string&lt;/code&gt; 의 메서드들은 정수기반 range 를 받는 것만 지원하는 걸까요?&lt;/p&gt;
&lt;p&gt;또, &lt;code&gt;iterator&lt;/code&gt; 는 왜 사용하지 않을까요?&lt;/p&gt;
&lt;p&gt;정수도 나쁜건 아닙니다만, 중요한 것은 정수 인자들에 대한 메서드의 처리입니다.&lt;/p&gt;
&lt;h2 id=&quot;out-of-bounds-처리&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#out-of-bounds-처리&quot; aria-label=&quot;Link to “Out of bounds 처리”&quot;&gt;Out of bounds 처리&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;std::string&lt;/code&gt; 의 문자열의 특정 문자를 가져오는 메서드는 &lt;code&gt;at&lt;/code&gt; 과 &lt;code&gt;operator[]&lt;/code&gt; 가 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string str &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;at&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;``&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;```txt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;F&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;C&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;두 메서드의 기능은 같아보이지만, 예외 처리에서 차이가 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string str &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;str[2013] = &quot;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2013&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;str[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2013&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt; // 실제로는 보이지 않지만 '\x00' 이 있음. 상황에 따라 '\x00' 이 아닌 다른 값이 있을 수 있음.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;operator[]&lt;/code&gt; 는 인덱스가 범위를 벗어나면 undefined behavior 를 발생시킵니다. 즉 지시된 인덱스에 있는 메모리의 값을 반환합니다. 예외 처리가 없는거죠. 그렇기 때문에 빠르게 동작합니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string str &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;str.at(2013) = &quot;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;at&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2013&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;libc++abi: terminating due to uncaught exception of type std::out_of_range: basic_string&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;at&lt;/code&gt; 메서드는 인덱스가 범위를 벗어나면 &lt;code&gt;std::out_of_range&lt;/code&gt; 예외를 던집니다. 그렇기 떄문에 느리죠.&lt;/p&gt;
&lt;p&gt;내부적으로 &lt;code&gt;try-catch&lt;/code&gt; 를 사용하여 예외 처리를 하고 있습니다.&lt;/p&gt;
&lt;p&gt;벤치마크를 돌려보면…&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;BM_StringOperatorBracket     714988 ns       714933 ns          980&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;BM_StringAt                 1489270 ns      1489178 ns          467&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;operator[]&lt;/code&gt; 가 더 빠른 것을 확인할 수 있습니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;at&lt;/code&gt; 과 &lt;code&gt;operator[]&lt;/code&gt; 의 차이가 예외 처리 여부라는 것을 알 수 있었습니다. 그럼 다른 함수는 어떨까요?&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;std::string::substr&lt;/code&gt; 은 해당 범위의 슬라이드를 반환합니다.&lt;/p&gt;
&lt;p&gt;각 프로토타입이 예외처리를 어떻게 하는지 알 수 있나요?&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)           &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; Musiala&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)      &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; Musiala&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)         &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::out_of_range&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)       &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::out_of_range&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)       &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::out_of_range&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;substr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)       &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; Jamal Musiala&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;그다지 일관성이 없죠. 심지어는 첫번째 인자에만 경계 체크를 하고 두번째 인자에는 경계 체크를 하지 않습니다.&lt;/p&gt;
&lt;p&gt;또한 &lt;code&gt;substr(-1, 5)&lt;/code&gt; 의 경우 &lt;code&gt;-1&lt;/code&gt;이 &lt;code&gt;unsigned&lt;/code&gt; 로 변환되기까지 하는데 컴파일 옵션을 따로 두지 않는 한 경고도 없습니다.&lt;/p&gt;
&lt;p&gt;저도 C++ 을 주력으로 삼는 개발자이지만, 사람들이 C++ 이 복잡하다고 생각하는 이유가 이런 것들 아닐까 싶습니다.&lt;/p&gt;
&lt;p&gt;복잡한 템플릿, 모던 기법들까지 가기도 전에 이런 일관성 없고 오버로딩이 난무하는 것 부터 개발자에게 혼란을 주는 포인트가 되는 것 같습니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;@ashvardanian&lt;/code&gt; 은 이 경우 Python 의 슬라이스 문법이 직관적이라고 주장하는데요, 저도 어느정도 동의합니다. Python 에서는 아래와 같은 표현이 가능하거든요.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot; aria-label=&quot;Code block&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'C '&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;:]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'siala'&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ia'&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;C++ 로 이 문법을 흉내 내자면 이렇게 할 수 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;iostream&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;string&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;namespace&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; jayden&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;public:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;    string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        this&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;-&gt;_str &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; operator&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;initializer_list&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt; &lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; it &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; range.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; first &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;it&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; second &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;it;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; static_cast&amp;#x3C;int&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(_str.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (first &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) first &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; len;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (second &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) second &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; len;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (first &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; second) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; index &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; first; index &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; second; index&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            result &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _str[index];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;private:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string _str;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; operator&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; &quot;&quot;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; _jayden&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;unsigned&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; long&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt; // namespace jayden&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    using&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; namespace&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; jayden&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;Jamal Musiala&quot;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;_jayden&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[{&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;ia&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;여러 예외 처리가 빠진 미완 코드지만, C++ 에서 &lt;code&gt;literal operator&lt;/code&gt;, &lt;code&gt;std::initializer_list&lt;/code&gt; 를 사용하여 파이썬을 어느정도 흉내낼 수 있었습니다.&lt;/p&gt;
&lt;p&gt;다른 기능도 살펴보겠습니다.&lt;/p&gt;
&lt;h3 id=&quot;split&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#split&quot; aria-label=&quot;Link to “split”&quot;&gt;split&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot; aria-label=&quot;Code block&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;.split()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'FC'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'Bayern'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'Munich'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;.split(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;B&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'F'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ayern Munich'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;split 은 delimiter 를 기준으로 문자열을 분리하는 기능입니다. Python 으로 코드를 작성하다보면 반드시 사용하게되는 유용한 함수인데요, C/C++ 에서는 어떨까요?&lt;/p&gt;
&lt;p&gt;C 표준에 &lt;code&gt;strtok&lt;/code&gt; 함수가 있긴 하지만, thread-safe 하지 않습니다. 내부적으로 static 변수를 사용하기 떄문입니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;C&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;c&quot; aria-label=&quot;Code block&quot; data-lang=&quot;C&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; str &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(str, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;    printf&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#F47067&quot;&gt;%s\n&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, token);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;NULL&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;C++ 에서는 &lt;code&gt;std::stringstream&lt;/code&gt; 과 &lt;code&gt;std::getline&lt;/code&gt; 을 사용하는 방법이 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;stringstream&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; ss&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;&quot;FC Bayern Munich&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string token;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;getline&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(ss, token, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;' '&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;하지만 너무 번거롭고 복사도 발생해서 굉장히 느립니다.&lt;/p&gt;
&lt;p&gt;C++23 에서는 &lt;code&gt;std::ranges::views::split&lt;/code&gt; 이 추가되어 복사 없는 문자열 분리가 가능해졌습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;one,two,three,four&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; ch : text) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; ch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot; -&gt; &quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::hex &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; static_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;ch)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; part : &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;ranges&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;views&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;','&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::cout &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::hex &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; static_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;part.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;()) &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot; -&gt; &quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;part.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        &amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::endl;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;txt&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;o -&gt; 0x16f6d2850&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;n -&gt; 0x16f6d2851&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;e -&gt; 0x16f6d2852&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;, -&gt; 0x16f6d2853&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;t -&gt; 0x16f6d2854&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;w -&gt; 0x16f6d2855&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;o -&gt; 0x16f6d2856&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;, -&gt; 0x16f6d2857&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;t -&gt; 0x16f6d2858&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;h -&gt; 0x16f6d2859&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;r -&gt; 0x16f6d285a&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;e -&gt; 0x16f6d285b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;e -&gt; 0x16f6d285c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;, -&gt; 0x16f6d285d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;f -&gt; 0x16f6d285e&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;o -&gt; 0x16f6d285f&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;u -&gt; 0x16f6d2860&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;r -&gt; 0x16f6d2861&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0x16f6d2850 -&gt; o&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0x16f6d2854 -&gt; t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0x16f6d2858 -&gt; t&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;0x16f6d285e -&gt; f&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;text&lt;/code&gt; 변수의 데이터가 복사되지 않고 참조 기반 &lt;code&gt;split&lt;/code&gt; 이 이루어져 빨리 동작하게 되었고, 코드도 훨씬 직관적으로 작성할 수 있게 되었습니다.&lt;/p&gt;
&lt;p&gt;특수한 케이스에서 더 빠르게 동작 시킬 수 있는데, 바로 &lt;code&gt;lazy_split&lt;/code&gt; 을 사용하는 겁니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;lazy_split&lt;/code&gt; 은 문자열을 한번에 분리하지 않고 필요할 때마다 분리하는 방식입니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; part : &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;ranges&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;views&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;lazy_split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;','&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (some_condition) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        break&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;이걸 사용하면 loop 중간에 탈출해야할 때 나머지 부분을 split 하지 않으므로 더 빠르게 동작할 수 있습니다.&lt;/p&gt;
&lt;p&gt;그럼 이제 여러 기능들에 대한 벤치마크를 해봅시다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;benchmark/benchmark.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;sstream&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;string&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;vector&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;cstring&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;boost/algorithm/string.hpp&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;ranges&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// Helper: stringstream split&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt; &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split_stringstream&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; delim&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::vector&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::stringstream &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;ss&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(s);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string item;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;getline&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(ss, item, delim)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        result.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;push_back&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(item);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// Helper: strtok split&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt; &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split_strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; delim&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::vector&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    char*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; cstr &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;[s.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;strcpy&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(cstr, s.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;());&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; delim_str[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {delim, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    char*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(cstr, delim_str);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (token) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        result.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;push_back&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(token);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        token &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;nullptr&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, delim_str);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    delete[]&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; cstr;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// Helper: boost::algorithm::split&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt; &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split_boost&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; delim&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::vector&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    boost&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(result, s, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;boost&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;is_any_of&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, delim)));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;vector&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;&gt; &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split_ranges&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; delim&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::vector&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; split_view &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;ranges&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;views&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(s, delim);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&amp;#x26;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; part : split_view) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        result.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;emplace_back&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(part.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;begin&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(), part.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;()));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_StringStream&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;one,two,three,four,five,six,seven,eight,nine,ten&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split_stringstream&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;','&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(v);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_StringStream);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_Strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;one,two,three,four,five,six,seven,eight,nine,ten&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split_strtok&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;','&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(v);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_Strtok);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_Boost&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;one,two,three,four,five,six,seven,eight,nine,ten&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split_boost&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;','&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(v);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_Boost);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_RangesSplit&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string text &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;one,two,three,four,five,six,seven,eight,nine,ten&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; v &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; split_ranges&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;','&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(v);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_RangesSplit);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK_MAIN&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;


































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Benchmark&lt;/th&gt;&lt;th&gt;Time (ns)&lt;/th&gt;&lt;th&gt;Time (ns)&lt;/th&gt;&lt;th&gt;Iterations&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;BM_StringStream&lt;/td&gt;&lt;td&gt;478&lt;/td&gt;&lt;td&gt;478&lt;/td&gt;&lt;td&gt;1,476,828&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_Strtok&lt;/td&gt;&lt;td&gt;388&lt;/td&gt;&lt;td&gt;388&lt;/td&gt;&lt;td&gt;1,793,534&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_Boost&lt;/td&gt;&lt;td&gt;537&lt;/td&gt;&lt;td&gt;537&lt;/td&gt;&lt;td&gt;1,251,385&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_RangesSplit&lt;/td&gt;&lt;td&gt;297&lt;/td&gt;&lt;td&gt;297&lt;/td&gt;&lt;td&gt;2,401,372&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;역시 참조 기반 split 이 가장 빠르고 직관적인 코드를 작성할 수 있었습니다.&lt;/p&gt;
&lt;h2 id=&quot;정리&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#정리&quot; aria-label=&quot;Link to “정리”&quot;&gt;정리&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;이처럼 C++ 표준 기능에는 유용한 것도 있고, 문제가 있는 것도 있습니다. 대형 회사들은 거의 모두 직접 해시테이블을 짜거나, 대안 오픈소스를 사용합니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;std::string&lt;/code&gt; 도 예외가 아니죠.&lt;/p&gt;
&lt;p&gt;일부 연산은 LibC를 호출하는데, LibC는 전반적으로 훨씬 최적화되어 있지만 하드웨어 성능을 완전히 뽑지 못하고, C++ 클래스가 요구하는 기능도 다 커버하지 못합니다.&lt;/p&gt;
&lt;p&gt;STL 과 C++ 표준을 맹신하지 말고, 필요한 경우 대안 오픈소스를 사용하는 거나 직접 구현하는 것도 좋은 방법이라는 것을 알 수 있었습니다.&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Vector Cosine Distance in JavaScript</title>
    <link href="https://bayernmuller.github.io/posts/250719-javascript-vector-cosine-distance/" />
    <id>https://bayernmuller.github.io/posts/250719-javascript-vector-cosine-distance/</id>
    <published>2025-07-19T00:00:00.000Z</published>
    <updated>2025-07-19T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="JavaScript" /><category term="NAPI" /><category term="WebAssembly" /><category term="Performance" /><category term="blazing.cpp" /><category term="Korean" />
    <summary>Various methods to calculate cosine distance in JavaScript</summary>
    <content type="html"></content>
  </entry>
  <entry>
    <title>String utilities with SIMD</title>
    <link href="https://bayernmuller.github.io/posts/250712-string-and-simd/" />
    <id>https://bayernmuller.github.io/posts/250712-string-and-simd/</id>
    <published>2025-07-12T00:00:00.000Z</published>
    <updated>2025-07-12T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="SIMD" /><category term="NEON" /><category term="CPU" /><category term="Performance" /><category term="blazing.cpp" /><category term="Korean" />
    <summary>Searching for substrings with SIMD and Pseudo-SIMD</summary>
    <content type="html">&lt;p&gt;&lt;a href=&quot;/posts/250705-simd-instruction/&quot;&gt;지난 SIMD 포스트&lt;/a&gt; 에서는 SIMD Instruction 의 간단한 예제를 배웠습니다.&lt;/p&gt;
&lt;p&gt;이번에는 &lt;a href=&quot;https://ashvardanian.com&quot;&gt;@ashvardanian&lt;/a&gt; 의 &lt;a href=&quot;https://github.com/ashvardanian/StringZilla&quot;&gt;StringZilla&lt;/a&gt; 프로젝트에서 사용된 문자열 관련 함수들을 살펴보고 SIMD 가 어떻게 활용되었는지 알아보겠습니다.&lt;/p&gt;
&lt;h2 id=&quot;find-substring&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#find-substring&quot; aria-label=&quot;Link to “Find substring”&quot;&gt;Find substring&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;어떤 문자열이 다른 문자열에 포함되어 있는지 확인하는 함수는 문자열 처리에서 가장 기본적인 함수 중 하나입니다.&lt;/p&gt;
&lt;p&gt;스칼라 변수에 대한 처리는 간단하게 구현할 수 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; contains&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack_len&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; needle&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; needle_len&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; needle_len; i&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        bool&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; needle_len; j&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (haystack[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; needle[j]) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;                match &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;                break&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (match) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;contains&lt;/code&gt; 함수는 나름 합리적인 구현체입니다.&lt;/p&gt;
&lt;p&gt;하지만 SIMD 최적화의 여지가 있습니다.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://ashvardanian.com&quot;&gt;@ashvardanian&lt;/a&gt; 는 &lt;code&gt;contains&lt;/code&gt; 를 SIMD 로 구현하기 위해 한가지를 전제했는데요, 바로 &lt;code&gt;needle&lt;/code&gt; 의 첫 4글자만 먼저 검색한 후 매치되는 부분이 없으면 더 이상 검색을 진행하지 않는 방식이 대부분의 경우 빠를 것이라는 겁니다.&lt;/p&gt;
&lt;p&gt;SIMD 로 이 방식을 구현하면 아래와 같습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint32_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prefix_val;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;memcpy&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;prefix_val, needle, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prefix &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vdupq_n_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(prefix_val);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block0 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block1 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block2 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block3 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches0 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block0, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches1 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block1, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches2 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block2, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches3 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block3, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches0, matches1), &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches2, matches3));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint64x2_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; reduced &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vreinterpretq_u64_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; has_match &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vgetq_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(reduced, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vgetq_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(reduced, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;img src=&quot;/assets/haystack-example.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;4079&quot; height=&quot;655&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;haystack&lt;/code&gt; 이 “FC BAYERN MUNICH FOREVER NO. 1” 이고, 여기서 &lt;code&gt;needle&lt;/code&gt; 인 “FOREVER” 이 substring 으로 존재하는지를 확인하려 합니다.&lt;/p&gt;
&lt;h3 id=&quot;1-needle-의-첫-4글자를-uint32_t-로-복사&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#1-needle-의-첫-4글자를-uint32_t-로-복사&quot; aria-label=&quot;Link to “1. needle 의 첫 4글자를 uint32_t 로 복사”&quot;&gt;1. &lt;code&gt;needle&lt;/code&gt; 의 첫 4글자를 &lt;code&gt;uint32_t&lt;/code&gt; 로 복사&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint32_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prefix_val;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;memcpy&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;prefix_val, needle, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prefix &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vdupq_n_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(prefix_val);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;코드의 처음 부분에서 &lt;code&gt;needle&lt;/code&gt; 의 첫 4글자를 &lt;code&gt;uint32_t&lt;/code&gt; 로 복사합니다.&lt;/p&gt;
&lt;p&gt;이제 &lt;code&gt;prefix_val&lt;/code&gt; 은 &lt;code&gt;0x464f5245&lt;/code&gt; 가 됩니다. (char 로 보면 &lt;code&gt;F&lt;/code&gt; &lt;code&gt;O&lt;/code&gt; &lt;code&gt;R&lt;/code&gt; &lt;code&gt;E&lt;/code&gt; 가 됩니다.)&lt;/p&gt;
&lt;p&gt;이 값을 &lt;code&gt;vdupq_n_u32&lt;/code&gt; 를 사용해 32비트 값 4개를 가진 총 128비트 벡터 레지스터로 확장합니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;prefix&lt;/code&gt; 벡터 레지스터는 이제 &lt;code&gt;[ 0x464f5245, 0x464f5245, 0x464f5245, 0x464f5245 ]&lt;/code&gt; 가 됩니다.&lt;/p&gt;
&lt;p&gt;실제로는 byte order 에 따라 벡터 레지스터에 로드되는 값 설명과 다를 수 있지만, 편의상 &lt;code&gt;[&quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;]&lt;/code&gt; 로 표현하겠습니다.&lt;/p&gt;
&lt;h3 id=&quot;2-haystack-를-벡터-레지스터에-로드&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#2-haystack-를-벡터-레지스터에-로드&quot; aria-label=&quot;Link to “2. haystack 를 벡터 레지스터에 로드”&quot;&gt;2. &lt;code&gt;haystack&lt;/code&gt; 를 벡터 레지스터에 로드&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block0 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block1 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block2 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block3 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;이제 &lt;code&gt;haystack&lt;/code&gt; 의 첫 4글자를 벡터 레지스터에 로드합니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;vld1q_u32&lt;/code&gt; 는 벡터 레지스터에 32비트 값 4개를 로드시키므로, &lt;code&gt;block0&lt;/code&gt; 의 경우 &lt;code&gt;haystack&lt;/code&gt; 의 첫 16글자를 4등분한 각 4글자를 벡터 레지스터에 로드합니다.&lt;/p&gt;
&lt;p&gt;즉 &lt;code&gt;block0&lt;/code&gt; 은 &lt;code&gt;[&quot;FC B&quot;, &quot;AYER&quot;, &quot;N MU&quot;, &quot;NICH&quot;]&lt;/code&gt; 가 됩니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;block1&lt;/code&gt; 부터는 haystack 의 1번째 인덱스부터 로드하므로 &lt;code&gt;[&quot;C BA&quot;, &quot;YERN&quot;, &quot; MUN&quot;, &quot;NICH&quot;]&lt;/code&gt; 가 됩니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;block3&lt;/code&gt; 까지 모두 로드하면 아래와 같은 벡터 레지스터가 생성됩니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TEXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TEXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;haystack: &quot;FC BAYERN MUNICH FOREVER NO. 1&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block0: [&quot;FC B&quot;, &quot;AYER&quot;, &quot;N MU&quot;, &quot;NICH&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block1: [&quot;C BA&quot;, &quot;YERN&quot;, &quot; MUN&quot;, &quot;ICH &quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block2: [&quot; BAY&quot;, &quot;ERN &quot;, &quot;MUNI&quot;, &quot;CH F&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block3: [&quot;BAYE&quot;, &quot;RN M&quot;, &quot;UNIC&quot;, &quot;H FO&quot;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;3-block0-부터-block3-까지-prefix-와-비교&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#3-block0-부터-block3-까지-prefix-와-비교&quot; aria-label=&quot;Link to “3. block0 부터 block3 까지 prefix 와 비교”&quot;&gt;3. &lt;code&gt;block0&lt;/code&gt; 부터 &lt;code&gt;block3&lt;/code&gt; 까지 &lt;code&gt;prefix&lt;/code&gt; 와 비교&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches0 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block0, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches1 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block1, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches2 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block2, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches3 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block3, prefix);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;vceqq_u32&lt;/code&gt; 는 벡터 레지스터의 각 요소를 비교하여 결과를 벡터 레지스터로 반환합니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TEXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TEXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;예시: vceqq_u32(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    [0x12345678, 0x56789abc, 0xcafebabe, 0xdeadbeef],&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;    [0x464f5245, 0x464f5245, 0xcafebabe, 0x464f5245]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;결과: [0x00000000, 0x00000000, 0xFFFFFFFF, 0x00000000]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;이제 &lt;code&gt;matches0&lt;/code&gt; 부터 &lt;code&gt;matches3&lt;/code&gt; 까지 모두 &lt;code&gt;prefix&lt;/code&gt; 벡터와 비교하면 아래와 같은 결과가 나옵니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;TEXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot; aria-label=&quot;Code block&quot; data-lang=&quot;TEXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block0:   [&quot;FC B&quot;, &quot;AYER&quot;, &quot;N MU&quot;, &quot;NICH&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;prefix:   [&quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;matches0: [0,       0,      0,     0]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block1:   [&quot;C BA&quot;, &quot;YERN&quot;, &quot; MUN&quot;, &quot;ICH &quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;prefix:   [&quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;matches1: [0,       0,      0,     0]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block2:   [&quot; BAY&quot;, &quot;ERN &quot;, &quot;MUNI&quot;, &quot;CH F&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;prefix:   [&quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;matches2: [0,       0,      0,     0]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;block3:   [&quot;BAYE&quot;, &quot;RN M&quot;, &quot;UNIC&quot;, &quot;H FO&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;prefix:   [&quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;, &quot;FORE&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;matches3: [0,       0,      0,     0]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;4-matches0-부터-matches3-까지의-비교-결과-수합&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#4-matches0-부터-matches3-까지의-비교-결과-수합&quot; aria-label=&quot;Link to “4. matches0 부터 matches3 까지의 비교 결과 수합”&quot;&gt;4. &lt;code&gt;matches0&lt;/code&gt; 부터 &lt;code&gt;matches3&lt;/code&gt; 까지의 비교 결과 수합&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches0, matches1), &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches2, matches3));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;uint64x2_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; reduced &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vreinterpretq_u64_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; has_match &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vgetq_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(reduced, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vgetq_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(reduced, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;벡터 간 연산 결과는 항상 벡터이므로, &lt;code&gt;matchesN&lt;/code&gt; 에서 바로 &lt;code&gt;matchesN != 0&lt;/code&gt; 와 같이 스칼라 기반 비교는 할 수 없습니다.&lt;/p&gt;
&lt;p&gt;따라서 &lt;code&gt;vreinterpretq_u64_u32&lt;/code&gt; 를 사용해 32비트 값 4개를 가진 총 128비트 벡터 레지스터를 or 연산 하여 64비트 값 2개를 가진 총 128비트 벡터 레지스터로 변환합니다.&lt;/p&gt;
&lt;p&gt;이제 &lt;code&gt;reduced&lt;/code&gt; 벡터 레지스터는 &lt;code&gt;[0, 0]&lt;/code&gt; 이 됩니다.&lt;/p&gt;
&lt;p&gt;마지막으로 &lt;code&gt;vgetq_lane_u64&lt;/code&gt; 를 사용해 64비트 값 2개를 가진 총 128비트 벡터 레지스터에서 64bit 씩 두개의 스칼라 값을 가져옵니다.&lt;/p&gt;
&lt;p&gt;이제 &lt;code&gt;has_match&lt;/code&gt; 는 &lt;code&gt;false&lt;/code&gt; 가 됩니다.&lt;/p&gt;
&lt;h3 id=&quot;5-반복&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#5-반복&quot; aria-label=&quot;Link to “5. 반복”&quot;&gt;5. 반복&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;첫번째 시도에서 &lt;code&gt;has_match&lt;/code&gt; 가 &lt;code&gt;false&lt;/code&gt; 이므로, haystack 의 4번째 인덱스부터 다시 같은 방식을 끝날 때 까지 반복합니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;반복하는 과정에서 &lt;code&gt;has_match&lt;/code&gt; 가 &lt;code&gt;true&lt;/code&gt; 이면, 그 위치부터 스칼라 구현처럼 문자열을 비교하면 됩니다.&lt;/p&gt;
&lt;h3 id=&quot;6-벤치마크&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#6-벤치마크&quot; aria-label=&quot;Link to “6. 벤치마크”&quot;&gt;6. 벤치마크&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;arm_neon.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;cstring&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;cstdio&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;cstdint&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;benchmark/benchmark.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; contains_scalar&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack_len&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; needle&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; needle_len&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (needle_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; needle_len; i&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        bool&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; needle_len; j&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (haystack[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; needle[j]) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;                match &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;                break&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (match) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; true&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; contains_simd&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; haystack_len&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; needle&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; needle_len&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (needle_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; contains_scalar&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack, haystack_len, needle, needle_len);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    uint32_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prefix_val;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;    memcpy&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;prefix_val, needle, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;    uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prefix &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vdupq_n_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(prefix_val);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block0 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block1 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block2 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; block3 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;reinterpret_cast&amp;#x3C;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; uint32_t*&gt;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches0 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block0, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches1 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block1, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches2 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block2, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches3 &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vceqq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(block3, prefix);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; matches &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches0, matches1), &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vorrq_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches2, matches3));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        uint64x2_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; reduced &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vreinterpretq_u64_u32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(matches);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;vgetq_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(reduced, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vgetq_lane_u64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(reduced, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; contains_scalar&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i, haystack_len &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i, needle, needle_len);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;constexpr&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; TEXT_SIZE &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1000000&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;constexpr&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; SUBSTRING[] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &quot;WXYZ&quot;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;constexpr&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; SUBSTRING_LEN &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; sizeof&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(SUBSTRING) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_ContainsScalar&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(TEXT_SIZE, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'A'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    text.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(TEXT_SIZE &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; SUBSTRING_LEN, SUBSTRING_LEN, SUBSTRING);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;contains_scalar&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(), TEXT_SIZE, SUBSTRING, SUBSTRING_LEN));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; BM_ContainsSimd&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;State&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; state&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::string &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(TEXT_SIZE, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'A'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    text.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;replace&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(TEXT_SIZE &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; SUBSTRING_LEN, SUBSTRING_LEN, SUBSTRING);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; _ : state) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;        benchmark&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;DoNotOptimize&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;contains_simd&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(text.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(), TEXT_SIZE, SUBSTRING, SUBSTRING_LEN));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_ContainsScalar);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(BM_ContainsSimd);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;BENCHMARK_MAIN&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;아래는 벤치마크 결과입니다.&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;






















&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Benchmark&lt;/th&gt;&lt;th&gt;Time (ns)&lt;/th&gt;&lt;th&gt;CPU (ns)&lt;/th&gt;&lt;th&gt;Iterations&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;BM_ContainsScalar&lt;/td&gt;&lt;td&gt;327339&lt;/td&gt;&lt;td&gt;326595&lt;/td&gt;&lt;td&gt;2124&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;BM_ContainsSimd&lt;/td&gt;&lt;td&gt;243883&lt;/td&gt;&lt;td&gt;243700&lt;/td&gt;&lt;td&gt;2836&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;SIMD 구현이 스칼라 구현보다 빠른 것을 알 수 있습니다.&lt;/p&gt;
&lt;p&gt;이처럼 SIMD 연산 뿐만 아니라 어떤 데이터 내에서 데이터를 찾아내는 것에 유용한 기능입니다.&lt;/p&gt;
&lt;p&gt;응용하면 더 복잡한 문제에도 적용할 수 있습니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;strlen&lt;/code&gt; -&gt; 0 으로 초기화된 벡터 레지스터를 슬라이딩 윈도우로 사용하여 옮겨가며 비교&lt;/li&gt;
&lt;li&gt;&lt;code&gt;tolower&lt;/code&gt;, &lt;code&gt;toupper&lt;/code&gt; -&gt; ‘A’ ~ ‘Z’ 범위해 대해 벡터 기반 +32 적용 (소문자화)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;pseudo-simd&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#pseudo-simd&quot; aria-label=&quot;Link to “Pseudo-SIMD”&quot;&gt;Pseudo-SIMD&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;SIMD 는 유용하지만, 최대 단점은 하드웨어에 종속되는 기능이라는 것입니다.&lt;/p&gt;
&lt;p&gt;같은 기능을 여러 하드웨어에서 구동시키려면 여러 하드웨어가 제공하는 API 로 모두 구현해야 하니까요.&lt;/p&gt;
&lt;p&gt;지금 소개할 Pseudo-SIMD 는 하드웨어에 종속되지 않는 기능으로, SIMD 는 아니지만 SIMD 를 흉내내는 기능입니다.&lt;/p&gt;
&lt;p&gt;문자열에서 특정 문자 &lt;code&gt;n&lt;/code&gt;의 위치를 찾아내는 코드를 살펴보겠습니다.&lt;/p&gt;
&lt;p&gt;이번 &lt;code&gt;haystack&lt;/code&gt; 은 “MANUEL NEUER” 이고, 여기서 &lt;code&gt;n&lt;/code&gt; 은 ‘U’ 라고 해보겠습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;  // broadcast `n` into `nnnnnnnn`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt; // broadcast `n` into `nnnnnnnn`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt; // broadcast `n` into `nnnnnnnn`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (; haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; &amp;#x3C;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; end; haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_part &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)haystack;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack_part &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0101010101010101&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; begin &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; ctz64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(match_indicators) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;1-n-을-broadcast&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#1-n-을-broadcast&quot; aria-label=&quot;Link to “1. n 을 broadcast”&quot;&gt;1. &lt;code&gt;n&lt;/code&gt; 을 broadcast&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;n&lt;/code&gt;을 8비트씩 오른쪽으로 시프트하면서 비트를 채워넣습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;n &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;55&lt;/span&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt; // ('U')&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// nnnnnnnn = 0x55&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// nnnnnnnn = 0x5555&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// nnnnnnnn = 0x55555555&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6A737D;--shiki-dark:#768390&quot;&gt;// nnnnnnnn = 0x5555555555555555&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3 id=&quot;2-haystack-에서-8바이트씩-추출&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#2-haystack-에서-8바이트씩-추출&quot; aria-label=&quot;Link to “2. haystack 에서 8바이트씩 추출”&quot;&gt;2. &lt;code&gt;haystack&lt;/code&gt; 에서 8바이트씩 추출&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack_part &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)haystack;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;haystack&lt;/code&gt; 에서 첫 8글자가 64비트 값으로 추출됩니다.&lt;/p&gt;
&lt;h3 id=&quot;3-haystack_part-와-nnnnnnnn-의-xor-not-을-통한-비교&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#3-haystack_part-와-nnnnnnnn-의-xor-not-을-통한-비교&quot; aria-label=&quot;Link to “3. haystack_part 와 nnnnnnnn 의 XOR + NOT 을 통한 비교”&quot;&gt;3. &lt;code&gt;haystack_part&lt;/code&gt; 와 &lt;code&gt;nnnnnnnn&lt;/code&gt; 의 XOR + NOT 을 통한 비교&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;uint64_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(haystack_part &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; nnnnnnnn);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;haystack_part&lt;/code&gt; 와 &lt;code&gt;nnnnnnnn&lt;/code&gt; 의 XOR 연산 결과를 NOT 연산 하여 비교 결과를 얻습니다.&lt;/p&gt;
&lt;p&gt;그럼 데이터는 아래와 같을겁니다&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;PLAINTEXT&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;plaintext&quot; aria-label=&quot;Code block&quot; data-lang=&quot;PLAINTEXT&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;haystack_part:    4e204c45554e414d&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;nnnnnnnn:         5555555555555555&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;match_indicators: e48ae6efffe4ebe7&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;XOR 연산은 서로 같으면 0, 다르면 1 이 되기 때문에, XOR 이후 NOT 연산을 하면 동일한 부분의 바이트는 0xff 가 되는 것을 알 수 있습니다.&lt;/p&gt;
&lt;h3 id=&quot;4-match_indicators-에서-결과-추출&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#4-match_indicators-에서-결과-추출&quot; aria-label=&quot;Link to “4. match_indicators 에서 결과 추출”&quot;&gt;4. &lt;code&gt;match_indicators&lt;/code&gt; 에서 결과 추출&lt;/a&gt;&lt;/h3&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&gt;&gt;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;=&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0101010101010101&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (match_indicators &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;!=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; haystack &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; begin &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; ctz64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(match_indicators) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;이제 비교 결과를 알 수 있도록 쉬프트와 AND 연산을 사용하는데요, 쉽게 설명하기 위해 2바이트만 예시로 살펴보겠습니다.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/pseudo-simd.png&quot; alt=&quot;&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; width=&quot;2672&quot; height=&quot;1416&quot;&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&gt;&gt; 1&lt;/code&gt;, AND, &lt;code&gt;&gt;&gt; 2&lt;/code&gt;, AND, &lt;code&gt;&gt;&gt; 4&lt;/code&gt; 를 통해 각 바이트에 0이 포함되어있으면 해당 0이 인접 비트까지 0으로 만들어버려 결과적으로 0이 하나라도 포함된 바이트는 0이 되고, 오직 0xFF 만 최하위 비트가 1 이 되는 것을 알 수 있습니다.&lt;/p&gt;
&lt;p&gt;이후 &lt;code&gt;match_indicators&lt;/code&gt; 가 0이 아니라면 &lt;code&gt;haystack_part&lt;/code&gt; 에 &lt;code&gt;n&lt;/code&gt; 이 포함되어있다는 뜻이고, &lt;code&gt;ctz64&lt;/code&gt; 를 통해 1 이 몇번째 비트에 있는지 알 수 있습니다.&lt;/p&gt;
&lt;p&gt;이 값을 8로 나누면 문자열 속 &lt;code&gt;n&lt;/code&gt; 의 위치를 알 수 있습니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;ctz64&lt;/code&gt; 는 표준은 아니지만, &lt;code&gt;__builtin_ctzll&lt;/code&gt; 처럼 컴파일러 내장 함수로 제공되기도 합니다.&lt;/p&gt;
&lt;p&gt;C++20 부터는 표준으로 지원하기도 합니다. &lt;a href=&quot;https://en.cppreference.com/w/cpp/numeric/countr_zero&quot;&gt;std::countr_zero&lt;/a&gt; 를 참고하세요.&lt;/p&gt;
&lt;h2 id=&quot;마치며-1&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#마치며-1&quot; aria-label=&quot;Link to “마치며”&quot;&gt;마치며&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;이번 포스트에서는 SIMD 를 이용한 문자열 검색 방법과, 하드웨어에 종속되지 않는 Pseudo-SIMD 를 살펴보았습니다.&lt;/li&gt;
&lt;li&gt;SIMD 를 활용하는 방식은 연산에 한정되어있지 않고, 데이터를 찾아내는 것에도 유용하다는 것을 알 수 있었습니다.&lt;/li&gt;
&lt;li&gt;Pseudo-SIMD 는 하드웨어에 종속되어 있지 않으니 도입이 쉬워 많은 곳에서 활용할 수 있겠습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;a href=&quot;https://theintrance.github.io/blazing.cpp/&quot;&gt;
        &lt;img src=&quot;https://theintrance.github.io/blazing.cpp/assets/banner.svg&quot; alt=&quot;Scalar vs SIMD&quot; width=&quot;100%&quot;&gt;
    &lt;/a&gt;
&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>SIMD Instruction</title>
    <link href="https://bayernmuller.github.io/posts/250705-simd-instruction/" />
    <id>https://bayernmuller.github.io/posts/250705-simd-instruction/</id>
    <published>2025-07-05T00:00:00.000Z</published>
    <updated>2025-07-05T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="SIMD" /><category term="NEON" /><category term="CPU" /><category term="Performance" /><category term="blazing.cpp" /><category term="Korean" />
    <summary>Single Instruction, Multiple Data.</summary>
    <content type="html">&lt;p&gt;Python 에서 사용되는 대표적인 라이브러리로 &lt;code&gt;numpy&lt;/code&gt; 를 꼽을 수 있습니다. &lt;code&gt;numpy&lt;/code&gt; 는 벡터화된 연산을 지원합니다. 배열/행렬 끼리의 덧셈, 곱셈 등의 연산을 아주 빠르게 수행할 수 있습니다.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;numpy&lt;/code&gt; 는 C/C++ 로 구현되어 있습니다. 그래서 느린 Python 을 극복하고 빠르게 동작할 수 있죠.&lt;/p&gt;
&lt;p&gt;그럼 C++ 로 &lt;code&gt;numpy&lt;/code&gt; 처럼 행렬 연산을 직접 구현해보겠습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; matrix_multiply&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n; &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;i) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n; &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;j) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            c[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; k &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; k &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n; &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;k) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;                c[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; a[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; k] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; b[k &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;직관적으로 위와같은 코드를 생각해볼 수 있습니다.&lt;/p&gt;
&lt;p&gt;이 코드는 작은 행렬에 대해서는 Python 이나 Javascript 같은 스크립트 언어들보다 훨씬 빠르게 동작합니다.&lt;/p&gt;
&lt;p&gt;하지만 큰 행렬에 대해서는 어떨까요? 두 행렬의 크기를 &lt;em&gt;1024x1024&lt;/em&gt; 로 설정하고 &lt;code&gt;numpy&lt;/code&gt; 와 비교해보겠습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot; aria-label=&quot;Code block&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; matrix_multiply&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(a: np.ndarray, b: np.ndarray) -&gt; np.ndarray&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; np.dot(a, b)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;SHELL&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot; aria-label=&quot;Code block&quot; data-lang=&quot;SHELL&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;❯&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; g++&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; -O3&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; matmul.cpp&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; -std=c++17&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; matmul&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;❯&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; ./matmul&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;Time&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; taken:&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 1.3178&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; seconds&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;❯&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; python3&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; matmul.py&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;Time&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; taken:&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0.0216&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; seconds&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;동일한 행렬곱 연산을 수행하는데 &lt;code&gt;-O3&lt;/code&gt; 최적화 레벨을 적용한 C++ 코드보다 &lt;code&gt;numpy&lt;/code&gt; 의 Python 구현이 더 훨씬 더 빠릅니다. 어떻게 이게 가능할까요?&lt;/p&gt;
&lt;h2 id=&quot;single-instruction-multiple-data-simd&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#single-instruction-multiple-data-simd&quot; aria-label=&quot;Link to “Single Instruction Multiple Data, SIMD”&quot;&gt;Single Instruction Multiple Data, SIMD&lt;/a&gt;&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;numpy&lt;/code&gt;가 빠른 이유는 C/C++ 구현체 이기 때문이 아니라, &lt;mark&gt;백엔드에서 SIMD와 같은 최적화를 활용하기 때문&lt;/mark&gt;입니다.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;SIMD 는 Single Instruction Multiple Data 의 약자로, 하나의 명령어로 여러 개의 데이터를 동시에 처리하는 CPU 명령어 집합을 말합니다.&lt;/p&gt;
&lt;p&gt;변수 여러개를 하나의 레지스터에 저장한 뒤, 하나의 CPU 명령어로 레지스터에 대한 연산을 수행하는 방식이죠.&lt;/p&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;img src=&quot;/assets/scalar-vs-simd.jpg&quot; alt=&quot;Scalar vs SIMD&quot; width=&quot;500&quot;&gt;
&lt;/p&gt;
&lt;p&gt;즉 이 SIMD은 CPU 차원에서 지원하는 기능이며, CPU 의 종류별로 지원 여부가 갈리며 지원하는 범위, 사용하는 방법이 모두 다릅니다.&lt;/p&gt;
&lt;p&gt;우리가 흔히 아는 x86 아키텍처에서는 SSE, AVX, AVX2, AVX512 등의 SIMD 명령어가 있으며, ARM 아키텍처에서는 NEON 이라는 SIMD 명령어가 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; &amp;#x3C;arm_neon.h&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; matmul4x4_neon&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; A&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; B&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; C&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;i) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;        float32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; a_row &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vld1q_f32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x26;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;A[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; j &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;j) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;            float32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; b_col &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;                B[j], B[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j], B[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j], B[&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;            float32x4_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; prod &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vmulq_f32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(a_row, b_col);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            float&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; vaddvq_f32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(prod);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;            C[i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; j] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; result;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;위 코드는 NEON 명령어를 사용하여 4x4 행렬곱을 수행하는 코드입니다.&lt;/p&gt;
&lt;p&gt;A의 &lt;code&gt;i&lt;/code&gt;번째 행 4개의 원소를 하나의 레지스터에 저장하고, B의 &lt;code&gt;j&lt;/code&gt;번째 열 4개의 원소를 하나의 레지스터에 저장합니다.&lt;/p&gt;
&lt;p&gt;그 뒤 두 레지스터에 대한 곱셈을 수행하고, 결과를 더합니다.&lt;/p&gt;
&lt;p&gt;이렇게 하면 SIMD 를 사용하여 4x4 행렬곱을 수행할 수 있습니다.&lt;/p&gt;
&lt;p&gt;하지만 아직도 &lt;code&gt;numpy&lt;/code&gt; 의 구현을 따라가기는 어렵습니다. 우리가 구현한 코드는 4x4 행렬곱만 수행할 수 있습니다. SIMD 레지스터의 크기가 정해져있기 때문입니다. &lt;code&gt;numpy&lt;/code&gt; 에는 지원하는 SIMD 레지스터의 크기를 고려하여 SIMD 를 사용하는 더 복잡한 로직이 구현되어 있는거죠.&lt;/p&gt;
&lt;p&gt;이처럼 SIMD 는 사용하기에 제약조건이 많습니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CPU 에 따라 지원하는 SIMD 명령어가 다르고,&lt;/li&gt;
&lt;li&gt;레지스터의 크기가 정해져있고,&lt;/li&gt;
&lt;li&gt;메모리가 정렬되어 있어야 하고,&lt;/li&gt;
&lt;li&gt;복잡한 분기가 필요한 경우 사용하기 어렵습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;이런 제약조건이 있음에도 실시간 데이터 처리, 머신러닝 등의 분야에서 여전히 강력한 기능이며 더 빠르게 동작하는 코드를 작성하기 위해 계속 사용되고 있습니다.&lt;/p&gt;
&lt;p&gt;또한 어떤 컴파일러들은 최적화를 위해 자동으로 SIMD 명령어를 사용하는 기능을 제공하기도 합니다. 이런 경우 개발자가 명시적으로 SIMD 명령어를 사용하지 않아도 최적화된 코드를 실행할 수 있습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;CPP&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;cpp&quot; aria-label=&quot;Code block&quot; data-lang=&quot;CPP&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; sum_arrays&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; dst&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; float*&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;&amp;#x3C;&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; n; &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;i) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        dst[i] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; a[i] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; b[i];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;이 코드를 &lt;code&gt;-O3&lt;/code&gt; 최적화 레벨을 사용하여 어셈블리 코드로 변환해보겠습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;SHELL&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;bash&quot; aria-label=&quot;Code block&quot; data-lang=&quot;SHELL&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;❯&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; g++&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; -O3&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; -S&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; -o&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; output.s&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; sum_arrays.cpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#F69D50&quot;&gt;❯&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; cat&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt; output.s&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;ASM&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;asm&quot; aria-label=&quot;Code block&quot; data-lang=&quot;ASM&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;	fadd.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;4s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;	v0, v0, v4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;	fadd.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;4s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;	v1, v1, v5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;	fadd.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;4s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;	v2, v2, v6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;	fadd.&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt;4s&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;	v3, v3, v7&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;...&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;단순히 두 배열을 서로 더하는 코드를 for loop 로 구현했는데, 어셈블리 코드를 보면 ARM NEON 의 &lt;a href=&quot;https://developer.arm.com/documentation/100076/0100/A64-Instruction-Set-Reference/A64-SIMD-Vector-Instructions/FADD--vector-?lang=en&quot;&gt;FADD&lt;/a&gt; 명령어가 사용됨을 알 수 있습니다.&lt;/p&gt;
&lt;p&gt;어셈블리 코드에 SIMD 명령어가 있다고 해서 SIMD 명령어가 무조건 사용되는 것은 아니고, 컴파일러는 SIMD 가 사용될 수 있는지 검사하는 코드도 추가합니다. 모든 조건이 맞으면 SIMD 명령어가 사용됩니다. -&gt; &lt;a href=&quot;https://chatgpt.com/share/686872a1-4404-8002-aa81-528e059672a8&quot;&gt;참고&lt;/a&gt;&lt;/p&gt;
&lt;h2 id=&quot;1-의-소프트웨어-만이-simd-의-장점을-활용한다&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#1-의-소프트웨어-만이-simd-의-장점을-활용한다&quot; aria-label=&quot;Link to “1% 의 소프트웨어 만이 SIMD 의 장점을 활용한다.”&quot;&gt;1% 의 소프트웨어 만이 SIMD 의 장점을 활용한다.&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;SIMD 가 무엇인지 간단히 알아보았으니 &lt;a href=&quot;https://ashvardanian.com/posts/simd-popularity/&quot;&gt;@ashvardanian 의 포스팅&lt;/a&gt; 을 리뷰해보겠습니다.&lt;/p&gt;
&lt;p&gt;x86 에는 지난 20년동안 계속해서 새로운 SIMD 명령어를 추가되어 왔습니다.&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;



























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;년도&lt;/th&gt;&lt;th&gt;확장&lt;/th&gt;&lt;th&gt;추가된 명령어&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1997&lt;/td&gt;&lt;td&gt;MMX&lt;/td&gt;&lt;td&gt;+ 46&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1999&lt;/td&gt;&lt;td&gt;SSE&lt;/td&gt;&lt;td&gt;+ 62&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2001&lt;/td&gt;&lt;td&gt;SSE2&lt;/td&gt;&lt;td&gt;+ 70&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2004&lt;/td&gt;&lt;td&gt;SSE3&lt;/td&gt;&lt;td&gt;+ 10&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2006&lt;/td&gt;&lt;td&gt;SSSE3&lt;/td&gt;&lt;td&gt;+ 16&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2006&lt;/td&gt;&lt;td&gt;SSE4.1/2&lt;/td&gt;&lt;td&gt;+ 54&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2008&lt;/td&gt;&lt;td&gt;AVX&lt;/td&gt;&lt;td&gt;+ 89&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2011&lt;/td&gt;&lt;td&gt;FMA&lt;/td&gt;&lt;td&gt;+ 20&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2013&lt;/td&gt;&lt;td&gt;AVX2&lt;/td&gt;&lt;td&gt;+ 135&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2015&lt;/td&gt;&lt;td&gt;AVX-512&lt;/td&gt;&lt;td&gt;+ 347&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;h3 id=&quot;simd-가-활용되고-있긴-한걸까&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#simd-가-활용되고-있긴-한걸까&quot; aria-label=&quot;Link to “SIMD 가 활용되고 있긴 한걸까?”&quot;&gt;SIMD 가 활용되고 있긴 한걸까?&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;문자열 검색을 AVX2 기반으로 구현하면, 일반적인 libc나 STL 함수보다 4×에서 많게는 12×까지 빠른 처리 속도를 기록합니다. SIMD 는 빠르다는 것과 그 이유는 설명 했지만, 실제로 얼마나 많은 소프트웨어가 SIMD 를 활용하고 있을까요?&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://ashvardanian.com&quot;&gt;@ashvardanian&lt;/a&gt; 은 리눅스에서 사용되는 대표적인 바이너리들을 분석하여 SIMD 를 활용하고 있는 비율을 조사했습니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot; aria-label=&quot;Code block&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color:#6F42C1;--shiki-dark:#DCBDFB&quot;&gt; yield_instructions_from_binary&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(path: &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) -&gt; Generator[InstructionSpecs, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;    with&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; open&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(path, &lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt;mode&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rb'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        code &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E36209;--shiki-dark:#F69D50&quot;&gt; file&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;.read()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        md &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; Cs(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;CS_ARCH_X86&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;CS_MODE_64&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        md.detail &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;        md.skipdata &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; True&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; instruction &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; md.disasm(code, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;            yield&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; parse_instruction(instruction)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/aquynh/capstone&quot;&gt;Capstone&lt;/a&gt; 라이브러리를 사용하여 바이너리 파일에서 실행되는 명령어를 추출합니다.&lt;/p&gt;
&lt;div class=&quot;highlight&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;pre class=&quot;astro-code astro-code-themes github-light github-dark-dimmed&quot; style=&quot;background-color:#fff;--shiki-dark-bg:#22272e;color:#24292e;--shiki-dark:#adbac7; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot; aria-label=&quot;Code block&quot; data-lang=&quot;PYTHON&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_512b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'zmm'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(i) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_256b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ymm'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(i) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_128b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'xmm'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(i) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_64b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rax'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rbx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rcx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rdx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rsp'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rbp'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rsi'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rdi'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'rip'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;                    [&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'r'&lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; str&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(i) &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;)])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_32b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'eax'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ebx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ecx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'edx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;                     'esp'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ebp'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'esi'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'edi'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'eip'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'eflags'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_16b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ax'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'bx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'cx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'dx'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'sp'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'bp'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;                     'si'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'di'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'cs'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ss'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'es'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ds'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ip'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'flags'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;registers_8b &lt;/span&gt;&lt;span style=&quot;color:#D73A49;--shiki-dark:#F47067&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#005CC5;--shiki-dark:#6CB6FF&quot;&gt; set&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ah'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'al'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'bh'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'bl'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'ch'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'cl'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'dh'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#032F62;--shiki-dark:#96D0FF&quot;&gt;'dl'&lt;/span&gt;&lt;span style=&quot;color:#24292E;--shiki-dark:#ADBAC7&quot;&gt;])&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;x86 에서 사용되는 SIMD 명령어를 레지스터 크기별로 분류합니다.&lt;/p&gt;
&lt;p&gt;이렇게 실제 프로그램들에서 SIMD 가 사용되는 비율을 조사해보면 흥미로운 결과를 확인할 수 있습니다.&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;




















































































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Binary&lt;/th&gt;&lt;th&gt;Instructions&lt;/th&gt;&lt;th&gt;All SIMD&lt;/th&gt;&lt;th&gt;128-bit SIMD&lt;/th&gt;&lt;th&gt;256-bit SIMD&lt;/th&gt;&lt;th&gt;512-bit SIMD&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/dockerd&lt;/td&gt;&lt;td&gt;43M&lt;/td&gt;&lt;td&gt;0.478%&lt;/td&gt;&lt;td&gt;0.469%&lt;/td&gt;&lt;td&gt;0.009%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/docker&lt;/td&gt;&lt;td&gt;26M&lt;/td&gt;&lt;td&gt;0.582%&lt;/td&gt;&lt;td&gt;0.568%&lt;/td&gt;&lt;td&gt;0.014%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/containerd&lt;/td&gt;&lt;td&gt;21M&lt;/td&gt;&lt;td&gt;0.560%&lt;/td&gt;&lt;td&gt;0.543%&lt;/td&gt;&lt;td&gt;0.017%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/gitlab-runner&lt;/td&gt;&lt;td&gt;19M&lt;/td&gt;&lt;td&gt;0.851%&lt;/td&gt;&lt;td&gt;0.828%&lt;/td&gt;&lt;td&gt;0.022%&lt;/td&gt;&lt;td&gt;0.001%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/mongod&lt;/td&gt;&lt;td&gt;16M&lt;/td&gt;&lt;td&gt;0.547%&lt;/td&gt;&lt;td&gt;0.545%&lt;/td&gt;&lt;td&gt;0.002%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/mongoperf&lt;/td&gt;&lt;td&gt;15M&lt;/td&gt;&lt;td&gt;0.544%&lt;/td&gt;&lt;td&gt;0.542%&lt;/td&gt;&lt;td&gt;0.002%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/kubectl&lt;/td&gt;&lt;td&gt;15M&lt;/td&gt;&lt;td&gt;1.002%&lt;/td&gt;&lt;td&gt;0.978%&lt;/td&gt;&lt;td&gt;0.024%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/helm&lt;/td&gt;&lt;td&gt;14M&lt;/td&gt;&lt;td&gt;1.040%&lt;/td&gt;&lt;td&gt;1.015%&lt;/td&gt;&lt;td&gt;0.025%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/ctr&lt;/td&gt;&lt;td&gt;12M&lt;/td&gt;&lt;td&gt;0.564%&lt;/td&gt;&lt;td&gt;0.533%&lt;/td&gt;&lt;td&gt;0.030%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/containerd-stress&lt;/td&gt;&lt;td&gt;10M&lt;/td&gt;&lt;td&gt;0.551%&lt;/td&gt;&lt;td&gt;0.516%&lt;/td&gt;&lt;td&gt;0.035%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/mongos&lt;/td&gt;&lt;td&gt;9M&lt;/td&gt;&lt;td&gt;0.602%&lt;/td&gt;&lt;td&gt;0.600%&lt;/td&gt;&lt;td&gt;0.002%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/mongo&lt;/td&gt;&lt;td&gt;9M&lt;/td&gt;&lt;td&gt;0.566%&lt;/td&gt;&lt;td&gt;0.564%&lt;/td&gt;&lt;td&gt;0.002%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/bin/snap&lt;/td&gt;&lt;td&gt;8M&lt;/td&gt;&lt;td&gt;0.679%&lt;/td&gt;&lt;td&gt;0.635%&lt;/td&gt;&lt;td&gt;0.044%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;p&gt;프로그램에서 SIMD 명령어가 사용되는 비율이 대부분 1% 미만이라는 것을 확인할 수 있습니다.&lt;/p&gt;
&lt;p&gt;MacOS 라고 해도 이 현상은 크게 다르지 않습니다.&lt;/p&gt;
&lt;div class=&quot;table-wrapper&quot; role=&quot;region&quot; tabindex=&quot;0&quot; aria-label=&quot;Horizontally scrollable table&quot;&gt;




























































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Binary&lt;/th&gt;&lt;th&gt;Instructions&lt;/th&gt;&lt;th&gt;All SIMD&lt;/th&gt;&lt;th&gt;128-bit SIMD&lt;/th&gt;&lt;th&gt;256-bit SIMD&lt;/th&gt;&lt;th&gt;512-bit SIMD&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/mongod&lt;/td&gt;&lt;td&gt;26M&lt;/td&gt;&lt;td&gt;0.636%&lt;/td&gt;&lt;td&gt;0.635%&lt;/td&gt;&lt;td&gt;0.001%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/libHalide.dylib&lt;/td&gt;&lt;td&gt;25M&lt;/td&gt;&lt;td&gt;0.289%&lt;/td&gt;&lt;td&gt;0.288%&lt;/td&gt;&lt;td&gt;0.001%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/influxd&lt;/td&gt;&lt;td&gt;23M&lt;/td&gt;&lt;td&gt;0.714%&lt;/td&gt;&lt;td&gt;0.694%&lt;/td&gt;&lt;td&gt;0.020%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/influx&lt;/td&gt;&lt;td&gt;19M&lt;/td&gt;&lt;td&gt;0.662%&lt;/td&gt;&lt;td&gt;0.638%&lt;/td&gt;&lt;td&gt;0.023%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/hugo&lt;/td&gt;&lt;td&gt;18M&lt;/td&gt;&lt;td&gt;0.760%&lt;/td&gt;&lt;td&gt;0.740%&lt;/td&gt;&lt;td&gt;0.019%&lt;/td&gt;&lt;td&gt;0.001%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/mongo&lt;/td&gt;&lt;td&gt;17M&lt;/td&gt;&lt;td&gt;0.614%&lt;/td&gt;&lt;td&gt;0.613%&lt;/td&gt;&lt;td&gt;0.002%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/bazel-real&lt;/td&gt;&lt;td&gt;16M&lt;/td&gt;&lt;td&gt;0.141%&lt;/td&gt;&lt;td&gt;0.120%&lt;/td&gt;&lt;td&gt;0.020%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/node&lt;/td&gt;&lt;td&gt;14M&lt;/td&gt;&lt;td&gt;0.779%&lt;/td&gt;&lt;td&gt;0.737%&lt;/td&gt;&lt;td&gt;0.042%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/lto-dump-10&lt;/td&gt;&lt;td&gt;12M&lt;/td&gt;&lt;td&gt;0.037%&lt;/td&gt;&lt;td&gt;0.035%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;td&gt;0.002%&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;/usr/local/bin/gs-X11&lt;/td&gt;&lt;td&gt;8M&lt;/td&gt;&lt;td&gt;1.168%&lt;/td&gt;&lt;td&gt;1.167%&lt;/td&gt;&lt;td&gt;0.000%&lt;/td&gt;&lt;td&gt;0.001%&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;SIMD 명령어 셋은 지속적으로 업데이트 되어가고 있고 점점 레지스터의 크기는 커지고 있으나 대부분의 프로그램에서 SIMD 명령어가 거의 사용되지 않는다.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;이 결과를 여러가지 방향으로 해석해볼 수 있습니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;가설:
&lt;ul&gt;
&lt;li&gt;컴파일러의 자동 SIMD 최적화 기능이 부족하다.&lt;/li&gt;
&lt;li&gt;개발자가 컴파일러의 자동 SIMD 최적화 기능을 사용할 수 없는 코드를 작성하고 있다.&lt;/li&gt;
&lt;li&gt;SIMD 가 없어도 상용 프로그램을 만드는 것에 문제가 없다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;결과를 어떻게 받아드릴지는 개인의 몫이지만, 3가지의 가설 모두 참일 가능성이 높습니다.&lt;/p&gt;
&lt;p&gt;SIMD 라는 기술 특성 상 적용할 수 있는 경우가 한정되어있기에 컴파일러가 이런 경우를 캐치하기 쉽지 않을 것이며, 아주 특화된 도메인이 아닌 이상 개발자가 이정도의 최적화 수준을 고려해서 코드를 작성하지도 않을 것이고, 대부분의 프로그램이 SIMD 없이도 충분히 빠르게 동작하고 있는 것도 사실입니다.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;https://ashvardanian.com&quot;&gt;@ashvardanian&lt;/a&gt; 는 SIMD 사용을 고려하기 전에 다른 최적화 기법을 먼저 적용해보라고 조언합니다.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;분기 줄이기 (if, switch, loop)&lt;/li&gt;
&lt;li&gt;메모리 점프, 힙 할당 최소화 (space locality, dynamic allocation)&lt;/li&gt;
&lt;li&gt;런타임 다형성 회피 (virtual function, dynamic dispatch)&lt;/li&gt;
&lt;li&gt;함수 인라인 가능성 최대화&lt;/li&gt;
&lt;li&gt;컴파일 및 링킹 설정 조정&lt;/li&gt;
&lt;li&gt;I/O 병목에 비동기 처리 적용&lt;/li&gt;
&lt;li&gt;병렬 컴퓨팅에서 lock free data 구조 사용 (CAS, atomic operation)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;마치며-2&quot;&gt;&lt;a class=&quot;toclink&quot; href=&quot;#마치며-2&quot; aria-label=&quot;Link to “마치며”&quot;&gt;마치며&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;SIMD 명령어와 간단한 용례, 그리고 실제 프로그램에서 SIMD 명령어가 사용되는 비율을 알아보았습니다.&lt;/li&gt;
&lt;li&gt;SIMD 는 아주 강력한 기능이지만 그 사용처가 한정되어있고 최적화가 필요할 때 후순위가 되는 경우가 많다는 것도 알 수 있었습니다.&lt;/li&gt;
&lt;li&gt;사용하기 어렵고 제약이 따르는 기능인 만큼 현업에서는 프로젝트의 목적과 크기, 팀의 규모 등 여러 요소를 고려하여 사용 여부를 결정하는게 중요합니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p align=&quot;center&quot;&gt;
    &lt;a href=&quot;https://theintrance.github.io/blazing.cpp/&quot;&gt;
        &lt;img src=&quot;https://theintrance.github.io/blazing.cpp/assets/banner.svg&quot; alt=&quot;Scalar vs SIMD&quot; width=&quot;100%&quot;&gt;
    &lt;/a&gt;
&lt;/p&gt;</content>
  </entry>
  <entry>
    <title>Compiler — The code you write doesn’t run exactly as is.</title>
    <link href="https://bayernmuller.github.io/posts/241013-compiler-optimization/" />
    <id>https://bayernmuller.github.io/posts/241013-compiler-optimization/</id>
    <published>2024-10-13T00:00:00.000Z</published>
    <updated>2024-10-13T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="compiler" /><category term="optimization" /><category term="C++" /><category term="Development" /><category term="English" />
    <summary>Trust compiler.</summary>
    <content type="html"></content>
  </entry>
  <entry>
    <title>Vrew—웹 기반 영상 편집 앱 분석, 그리고 더 빠르게 만들기</title>
    <link href="https://bayernmuller.github.io/posts/240214-vrew-and-vroom/" />
    <id>https://bayernmuller.github.io/posts/240214-vrew-and-vroom/</id>
    <published>2024-02-14T00:00:00.000Z</published>
    <updated>2024-02-14T00:00:00.000Z</updated>
    <author><name>Jayden Yang</name></author>
    <category term="video" /><category term="C++" /><category term="FFmpeg" /><category term="Project" /><category term="Korean" />
    <summary>웹에서 동영상을 어떻게 편집하는거야?</summary>
    <content type="html"></content>
  </entry>
</feed>