Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions challenges/medium/165_multi_tensor_lamb/challenge.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<p>
Implement one step of the <strong>LAMB</strong> optimizer (Layer-wise Adaptive Moments,
used to pretrain BERT with batch sizes of 32K and more) over a whole model's parameters
at once. All <code>T</code> parameter tensors are packed back to back in one flat
<code>float32</code> buffer <code>params</code> of length <code>N</code>; tensor
<code>k</code> occupies the elements <code>offsets[k]</code> &le; <code>i</code> &lt;
<code>offsets[k+1]</code>. <code>grad</code>, <code>m</code> and <code>v</code> use the
same layout. LAMB takes an Adam step but rescales it separately for each tensor by a
<em>trust ratio</em>, the norm of that tensor's parameters divided by the norm of its
update. Update <code>params</code>, <code>m</code> and <code>v</code> in place.
</p>

<svg width="700" height="190" viewBox="0 0 700 190" xmlns="http://www.w3.org/2000/svg" style="display:block; margin:20px auto;">
<rect width="700" height="190" fill="#222"/>
<text x="20" y="28" fill="#ccc" font-size="13" font-family="monospace">params (flat, N elements)</text>
<rect x="20" y="40" width="380" height="36" fill="#1a3a5c" stroke="#4a9eff" stroke-width="1.5"/>
<text x="210" y="63" text-anchor="middle" fill="#cce" font-size="12" font-family="monospace">tensor 0 (embedding, 23.4M)</text>
<rect x="400" y="40" width="14" height="36" fill="#3a2a1a" stroke="#ffaa44" stroke-width="1.5"/>
<rect x="414" y="40" width="150" height="36" fill="#1a4a2a" stroke="#44cc77" stroke-width="1.5"/>
<text x="489" y="63" text-anchor="middle" fill="#cec" font-size="12" font-family="monospace">tensor 2</text>
<rect x="564" y="40" width="10" height="36" fill="#3a1a3a" stroke="#cc88ff" stroke-width="1.5"/>
<rect x="574" y="40" width="106" height="36" fill="#333" stroke="#888" stroke-width="1.5"/>
<text x="627" y="63" text-anchor="middle" fill="#aaa" font-size="12" font-family="monospace">...</text>
<text x="20" y="94" fill="#888" font-size="10" font-family="monospace">offsets[0]=0</text>
<text x="400" y="94" fill="#888" font-size="10" font-family="monospace">offsets[1]</text>
<text x="407" y="108" fill="#ffaa44" font-size="10" font-family="monospace">tensor 1 (LayerNorm, 768)</text>
<text x="680" y="94" text-anchor="end" fill="#888" font-size="10" font-family="monospace">offsets[T]=N</text>
<text x="20" y="138" fill="#ccc" font-size="12" font-family="monospace">per tensor k: r_k = ||&#952;_k|| / ||u_k|| (1 if either norm is 0)</text>
<text x="20" y="162" fill="#ccc" font-size="12" font-family="monospace">every element of tensor k: &#952; &#8592; &#952; &#8722; lr &#183; r_k &#183; u</text>
</svg>

<p>
For step <code>t</code> (1-indexed), every element computes
</p>
<p>
\[
\begin{align}
m &\leftarrow \beta_1 m + (1 - \beta_1) g \\
v &\leftarrow \beta_2 v + (1 - \beta_2) g^2 \\
u &= \frac{m / (1 - \beta_1^{t})}{\sqrt{v / (1 - \beta_2^{t})} + \epsilon} + \lambda \theta
\end{align}
\]
</p>
<p>
where <code>&theta;</code> is the parameter value <em>before</em> this step, <code>g</code>
is <code>grad</code>, and <code>&beta;<sub>1</sub></code>, <code>&beta;<sub>2</sub></code>,
<code>&epsilon;</code>, <code>&lambda;</code> are <code>beta1</code>, <code>beta2</code>,
<code>eps</code>, <code>weight_decay</code>. Then, for each tensor <code>k</code>, using the
L2 norms over that tensor's elements only:
</p>
<p>
\[
r_k = \begin{cases}
\dfrac{\lVert \theta_k \rVert_2}{\lVert u_k \rVert_2} & \text{if } \lVert \theta_k \rVert_2 > 0
\text{ and } \lVert u_k \rVert_2 > 0 \\
1 & \text{otherwise}
\end{cases}
\qquad
\theta \leftarrow \theta - \eta \, r_k \, u
\]
</p>
<p>
with <code>&eta;</code> = <code>lr</code>. Note that the weight decay term is part of
<code>u</code>, so it is scaled by the trust ratio too, and that <code>r<sub>k</sub></code> is
not clipped.
</p>

<h2>Implementation Requirements</h2>
<ul>
<li>Do not change the function signature.</li>
<li>Do not use external libraries beyond what is available in the starter.</li>
<li><code>params</code>, <code>m</code> and <code>v</code> are updated in place; there is
no separate output buffer.</li>
<li>Both norms of tensor <code>k</code> use the values before this step's parameter update:
<code>&theta;</code> is the old parameter value and <code>u</code> is computed from the
updated <code>m</code> and <code>v</code>. No element of <code>params</code> may be
overwritten before the norms that depend on it are complete.</li>
<li>When either norm of a tensor is exactly zero (for example a zero-initialized bias, or a
tensor whose gradient, optimizer state and weight decay term are all zero), its trust ratio
is 1.</li>
</ul>

<h2>Example</h2>
<pre>
N = 3, T = 2, offsets = [0, 2, 3], t = 1
params = [3.0, 4.0, 0.0]
grad = [0.5, -0.5, 2.0]
m = [0.0, 0.0, 0.0]
v = [0.0, 0.0, 0.0]
lr = 0.1, beta1 = 0.9, beta2 = 0.999, eps = 1e-6, weight_decay = 0.0

m -> 0.1 * grad = [0.05, -0.05, 0.2]
v -> 0.001 * grad^2 = [0.00025, 0.00025, 0.004]
u = (m / 0.1) / (sqrt(v / 0.001) + eps) ~ [1.0, -1.0, 1.0]

tensor 0: ||theta|| = 5, ||u|| = sqrt(2), r = 3.535534
params[0:2] -> [3.0 - 0.353553, 4.0 + 0.353553]
tensor 1: ||theta|| = 0, so r = 1
params[2] -> 0.0 - 0.1 * 1.0

params -> [2.646447, 4.353553, -0.100000]
m -> [0.05, -0.05, 0.2]
v -> [0.00025, 0.00025, 0.004]
</pre>

<h2>Constraints</h2>
<ul>
<li>1 &le; <code>T</code> &le; 512 (number of tensors)</li>
<li>1 &le; <code>N</code> &le; 120,000,000</li>
<li><code>offsets[0]</code> = 0, <code>offsets[T]</code> = <code>N</code>, and
<code>offsets</code> is strictly increasing (every tensor has at least one element)</li>
<li><code>params</code>, <code>grad</code>, <code>m</code> and <code>v</code> are 32-bit
floats; <code>offsets</code> is 32-bit integer</li>
<li><code>v</code> is non-negative; values of <code>params</code> lie in [&minus;500, 500] and
values of <code>grad</code> in [&minus;10, 10]</li>
<li>1 &le; <code>t</code> &le; 100,000; 0 &lt; <code>lr</code> &le; 0.1;
0 &le; <code>beta1</code>, <code>beta2</code> &lt; 1;
1e-8 &le; <code>eps</code> &le; 1e-2; 0 &le; <code>weight_decay</code> &le; 1</li>
<li>Performance is measured with <code>N</code> = 109,482,240, <code>T</code> = 199,
<code>t</code> = 1,000 (the full BERT-base parameter list, from the 23,440,896-element word
embedding down to 768-element LayerNorm vectors)</li>
</ul>
Loading
Loading