import{s as al,o as nl,n as V}from"../chunks/scheduler.defa9a21.js";import{S as sl,i as il,g as r,s,r as d,A as ol,h as c,f as l,c as i,j as ll,u as m,x as h,k as xe,y as rl,a,v as f,d as u,t as g,w}from"../chunks/index.fe795e71.js";import{T as W}from"../chunks/Tip.179eb360.js";import{C as _e}from"../chunks/CodeBlock.204b6c34.js";import{H as b,E as cl}from"../chunks/getInferenceSnippets.2234a8dd.js";function hl(M){let n,p="You can also set the <code>cp_size</code> and <code>cp_comm_strategy</code> in the <code>accelerate config</code> command, which will save them in your <code>accelerate</code> configuration file, so you don’t have to pass them every time you launch your script.";return{c(){n=r("p"),n.innerHTML=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-jjecnd"&&(n.innerHTML=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function pl(M){let n,p=`Context parallelism is compatible with other parallelism strategies, such as data parallelism, tensor parallelism and FSDP2.
You can simply combine them by setting your parallelism sizes to the desired values, e.g. <code>--parallelism-config-dp-size 8 --parallelism-config-tp-size 2 --parallelism-config-cp-size 8</code>. Or you can use the <code>ParallelismConfig</code> class to set them programmatically.`;return{c(){n=r("p"),n.innerHTML=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-9cdxby"&&(n.innerHTML=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function dl(M){let n,p=`Context parallelism is tightly coupled  with <code>FSDP2</code>, which you can learn more about in the <a href="fsdp1_vs_fsdp2.md">FSDP2 introduction</a>. Meaning, context parallelism only works if you use <code>FullyShardedDataParallelPlugin</code> or <code>--use-fsdp</code> with version set to 2 to your
program. If no <code>FSDP2</code> is used, error will be raised.`;return{c(){n=r("p"),n.innerHTML=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-lopeo0"&&(n.innerHTML=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function ml(M){let n,p='Context parallelism works only with <a href="https://docs.pytorch.org/docs/stable/generated/torch.nn.functional.scaled_dot_product_attention.html" rel="nofollow">SDPA</a> and only with no mask or causal mask. We can’t properly detect this for you, so it’s your responsibility to ensure that you are using <code>SDPA</code> with no mask or causal mask. If you use any other attention implementation, it will raise an error.';return{c(){n=r("p"),n.innerHTML=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-189ycww"&&(n.innerHTML=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function fl(M){let n,p="This context manager has to be recreated with each training step, as shown in the example above. It’s crucial to do so.";return{c(){n=r("p"),n.textContent=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-yv9i8u"&&(n.textContent=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function ul(M){let n,p='These examples were created with a script you can find <a href="https://github.com/huggingface/accelerate/blob/main/examples/fsdp2/nd_parallel.py" rel="nofollow">in the examples folder</a>. To run the example on 8 H100 GPUs (128k sequence length), you can use the following command:',o,$,_;return $=new _e({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMC0tdXNlLWZzZHAlMjAtLWZzZHAtYWN0aXZhdGlvbi1jaGVja3BvaW50aW5nJTNEVFJVRSUyMGV4YW1wbGVzJTJGZnNkcDIlMkZuZF9wYXJhbGxlbC5weSUyMC0tY3Atc2l6ZSUzRDglMjAtLXNlcXVlbmNlLWxlbmd0aCUzRDEyODAwMA==",highlighted:"accelerate launch --use-fsdp --fsdp-activation-checkpointing=TRUE examples/fsdp2/nd_parallel.py --cp-size=8 --sequence-length=128000",wrap:!1}}),{c(){n=r("p"),n.innerHTML=p,o=s(),d($.$$.fragment)},l(y){n=c(y,"P",{"data-svelte-h":!0}),h(n)!=="svelte-le4i2w"&&(n.innerHTML=p),o=i(y),m($.$$.fragment,y)},m(y,T){a(y,n,T),a(y,o,T),f($,y,T),_=!0},p:V,i(y){_||(u($.$$.fragment,y),_=!0)},o(y){g($.$$.fragment,y),_=!1},d(y){y&&(l(n),l(o)),w($,y)}}}function gl(M){let n,p=`Context parallelism is not compatible with <code>labels</code> that are a copy of <code>input_ids</code>, which models from 🤗 transformers can shift to enable causal language modeling themselves.
Imagine this case:
labels = [l1, l2, l3, l4, … li]
if we apply context parallelism, each rank would end up with a part of labels, such as this:
labels_rank_0 = [l1, l2], labels_rank_1 = [l3, l4], …
after transformers modelling code shifts the labels, it would end up with:
labels_rank_0 = [l2, PAD], labels_rank_1 = [l3, PAD], …
where <code>PAD</code> is a padding token. This would result in incorrect loss computation, as the labels are not aligned with the inputs anymore.
Because of this, you need to manually shift the labels before passing them in the model`;return{c(){n=r("p"),n.innerHTML=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-1nx5uwl"&&(n.innerHTML=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function wl(M){let n,p="This section is fairly technical, so if you don’t need to learn the internals of context parallelism, you can skip it and start building 🚀";return{c(){n=r("p"),n.textContent=p},l(o){n=c(o,"P",{"data-svelte-h":!0}),h(n)!=="svelte-1ibuwz8"&&(n.textContent=p)},m(o,$){a(o,n,$)},p:V,d(o){o&&l(n)}}}function $l(M){let n,p,o,$,_,y,T,Tt="This guide will cover basics of using context parallelism in 🤗<code>accelerate</code>, for the more curious readers, we will also cover some technicalities in the later sections.",ve,z,Ce,F,xt=`With the advent of large language models, and recently reasoning models, the sequence length has been growing rapidly. This, combined with quadratic memory complexity of attention, has lead to a need for more efficient ways to train models with long sequences.
With sequence length of 128k, the memory requirement of the attention matrix is <code>128k * 128k * 2 bytes * num_heads = ~32 GB * num_heads</code> for <code>bf16</code> precision, given vanilla attention implementation. Granted, with usage of <code>flash attention</code> or <code>SDPA</code> which do not materialize these attention weights, this decreases drastically, but the growth in memory requirements is still considerable.`,Ue,L,vt="Context parallelism allows us to shard the inputs to the attention computation along the sequence dimension and compute the attention in parallel on multiple GPUs. With this, we can train models with long sequences, scaling potentially to 1M+ sequence length.",je,A,Je,B,ke,Y,Ct=`As with any other feature in 🤗<code>accelerate</code>, you can enabled context parallelism also by passing the corresponding flags to <code>accelerate launch</code>.
In this case, it’s no different:`,Ge,S,Ie,x,He,v,Pe,C,We,U,Ve,Z,Ut=`After enabling context parallelism with the methods mentioned above, you can then apply it to your training loop. We provide a thin wrapper around <a href="https://docs.pytorch.org/docs/stable/distributed.tensor.html#torch.distributed.tensor.experimental.context_parallel" rel="nofollow"><code>torch.distributed.tensor.experimental.context_parallel</code></a> that you can use in your training loop, that abstracts some of the complexity of using it (more on this later). To minimize the changes you have to do your training loop, we provide a context manager than is a <code>noop</code> if context parallelism is not enabled, and applies the context parallelism if it is enabled. This way, you can use it in your training loop without changing any code based on your parallelism configuration.
You can use it as follows:`,ze,q,Fe,j,Le,Q,jt="This can scale your context size to 1M+ sequence length potentially. Below, we showcase speed and memory usage of context parallelism for up-to 256k context size. We can see that when we double the context size and number of GPUs, we can achieve consistent memory usage, potentiall enabling endless context length scaling.",Ae,J,Jt='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/accelerate/examples/fsdp2/cp_perf.png" alt="context parallelism memory usage"/> <br/> <em>Figure 1: Memory usage and speed of context parallelism for up-to 256k context size.</em>',Be,k,Ye,N,Se,X,kt="The context manager takes a few arguments, that are used to configure the context parallelism.",Ze,E,Gt=`<li><code>buffers</code>: This is a list of tensors that are to be sharded across the sequence dimension. These tensors are usually input ids, labels and attention mask.</li> <li><code>buffer_seq_dims</code>: This is a list of integers, that specify the sequence dimension of the buffers, in the order of the <code>buffers</code> list. If you pass <code>buffers=[input_ids, shift_labels]</code> with both having shape <code>[batch_size, sequence_length]</code>, you would pass <code>buffer_seq_dims=[1, 1]</code>.
as the sequence dimension is the second dimension of the tensors. This is required for correct computation of the model outputs.</li> <li><code>no_restore_buffers</code>: The implementation of context parallelism modifies the buffers in-place, converting them to <code>torch.distributed.tensor.Dtensor</code>s. After the context manager exits, a communication kernel would need to be launched to restore the buffers to their original state (usually all-gather). This takes some time, so it is recommended to pass the same tensors as in the <code>buffers</code> argument, to avoid unnecessary communication, unless you are sure that you need to use the buffers after the context manager exits.</li>`,qe,G,Qe,R,Ne,D,It="Accelerate provides only a single option to configure context parallelism (except of <code>cp_size</code>)",Xe,K,Ht="<li><code>cp_comm_strategy</code>: The rotation method to use for the shards. We strongly recommend keeping this as <code>&quot;allgather&quot;</code>, as it’s very likely it will outperform <code>&quot;alltoall&quot;</code> in most cases.</li>",Ee,O,Pt=`Context parallel size is rather self-explanatory, it’s the number of ranks across which the inputs are to be-sharded.
Context parallel shard rotation defines how the shards of the inputs are rotated across ranks. We’ll cover the 2 options in more detail in the next section.`,Re,ee,Wt='You can see an end-to-end example in the <a href="https://github.com/huggingface/accelerate/blob/main/examples/fsdp2/nd_parallel.py" rel="nofollow">ND parallel example</a> file, where you can train an 8B model with up-to 128k context length on a single 8xH100 node. Using multi-node training, you can scale this to 1M+ sequence length on multiple GPUs. You can also seamlessly combine it with other parallelism strategies to fit your needs.',De,te,Ke,I,Oe,le,Vt="We’re going to be using word <code>shard</code> extensively in the following sections, so let’s define it first. If we call tensor <code>sharded</code> across <code>Dth</code> dimension, across <code>N</code> ranks, we mean that this tensor is split into <code>N</code> parts, where each part of the tensor has shape <code>[..., D//N, ...]</code>.",et,ae,tt,ne,zt=`Context parallelism works on sharding the <code>Q, K and V</code> matrices across the sequence dimension. Each rank has its assigned shard of <code>Q</code>, let’s call it <code>Q_i</code>. This matrix stays only on this rank, during the whole computation. Similarly, each rank has its own shard of <code>K</code> and <code>V</code>, let’s call them <code>K_i</code> and <code>V_i</code>. Then, each rank calculates attention with its own shard of <code>Q_i</code>, <code>K_i</code> and <code>V_i</code>, let’s call it <code>attn_i</code>. During this computation, a communication kernel is launched to gather the <code>Ks</code> and <code>Vs</code> from all other ranks. What communication primitive is used, depends on the <code>context_parallel_shard_rotation</code> option.
This way, each rank gets to calculate local attention, first with <code>Q_i</code>, <code>K_i</code> and <code>V_i</code>, then with <code>K_j</code> and <code>V_j</code> from all other ranks. As each rank holds <code>Q, K and V</code> matrices that are sharded across the sequence dimension, the resulting matrices are smaller and can fit on a single GPU.`,lt,se,Ft="We can formalize this in a following pseudocode:",at,ie,nt,oe,st,re,it,ce,Lt=`So what’s the difference between all-to-all and all-gather? With all-gather, the communication is very simple. After (well, before, as it usually takes longer) we compute the local attention <code>attn_i</code> we launch an all-gather to gather all other <code>Ks</code> and <code>Vs</code> from all other ranks. As this communication is done, each rank has all the <code>Ks</code> and <code>Vs</code> from all other ranks, and can compute the attention with them sequentially.
In ideal scenario, all-gather finishes in the exact moment as the calculation of <code>attn_i</code> is done. However, this never happens in practice, so the ideal real overlap is achieved when the full <code>attn_i</code> is overlapped with a part of the communication, then to start the computation with <code>K_j</code> and <code>V_j</code>, we wait for the all-gather to finish.`,ot,he,rt,pe,At="All-to-all, or sometimes called <code>ring-rotation</code> utilizes a ring-like communication pattern. After concluding <code>attn_i</code> computation, an all-to-all is launched to send <code>K_i</code> and <code>V_i</code> to the neighbouring ranks. We then repeat this <code>context_parallel_size-1</code> times, so that each rank sees all the shards of <code>K</code> and <code>V</code> from all other ranks once. In ideal scenario, we prefetch shards <code>K_i+1</code> and <code>V_i+1</code> from the neighbouring rank and this communication is exactly overlapped with computation of our current <code>attn_i</code>. Again, realistically, this perfect overlap doesn’t ever happen. Given the nature of this approach, if we don’t achieve perfect overlap, the penalty is way larger than with all-gather.",ct,de,ht,me,Bt='In theory, all-to-all should be the better choice. Though in practice, it rarely is. Therefore, we default to all-gather, as it’s more likely to achieve better performance. Extensive <a href="https://discuss.pytorch.org/t/distributed-w-torchtitan-breaking-barriers-training-long-context-llms-with-1m-sequence-length-in-pytorch-using-context-parallel/215082" rel="nofollow">benchmarks</a> from the <code>torchtitan</code> team also shows that all-to-all rarely outperforms all-gather. Though, we still provide both options, as you might find one to be better for your use case.',pt,fe,Yt="You can directly see this issue in the profiler output in the image below:",dt,H,St='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/accelerate/examples/fsdp2/cp_all_to_all.png" alt="all-to-all profiler output"/> <br/> <em>Figure 1: In red you can see the idle time, while we wait for the all-to-all kernel to finish. Highlighted in the first blue bar, you can see that it takes ~250us to finish, which is repeated N-1 times for each attention call, where N is the context parallel size.</em>',mt,ue,ft,ge,Zt=`We only support context parallelism with <code>FSDP2</code>, as we create a joint mesh of <code>context_parallel_size</code> and <code>dp_shard_size</code> to
utilize its full potential.
How it works is: we shard the model across the joint mesh of size <code>cp_size*dp_shard_size</code>, which maximizes the memory savings.
This is a “free lunch” of sorts, as <code>FSDP</code> communication is fully overlapped with the computation of attention, as shown in the images below.`,ut,P,qt='<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/accelerate/examples/fsdp2/cp_why_fsdp2.png" alt="why FSDP2+CP"/> <br/> <em>Figure 2: In blue rectangles (Stream 23), you can see that the pre-fetch of `FSDP` shard is fully overlapped with the computation of attention (Stream 7), while in red rectangles (Stream 24), you can see that the all-gather kernel results in a bubble of idle time, in which our compute stream (7) is idle.</em>',gt,we,Qt="In the figure above, you can also note the difference between all-to-all and all-gather. While in all-to-all (Figure 1), we launch a communication kernel N-1 times for each attention call, in all-gather (Figure 2), we launch a communication kernel only once. This results in a bigger bubble, but it only happens once per attention call, while in all-to-all, it happens N-1 times.",wt,$e,$t,ye,Nt=`We make sure to dispatch the same batch of data to the whole <code>cp</code> subgroup, so that the results are correct. (Meaning each rank in <code>cp</code> subgroup gets the same batch of data.) However, we also dispatch different batches to each rank of <code>dp_shard</code> group.
Imagine it like this:`,yt,Me,Mt,be,bt,Te,_t;return _=new b({props:{title:"Context Parallel in 🤗 accelerate",local:"context-parallel-in--accelerate",headingTag:"h1"}}),z=new b({props:{title:"Why context parallelism?",local:"why-context-parallelism",headingTag:"h2"}}),A=new b({props:{title:"How to use context parallelism?",local:"how-to-use-context-parallelism",headingTag:"h2"}}),B=new _e({props:{code:"ZnJvbSUyMGFjY2VsZXJhdGUudXRpbHMlMjBpbXBvcnQlMjBQYXJhbGxlbGlzbUNvbmZpZyUyQyUyMFRvcmNoQ29udGV4dFBhcmFsbGVsQ29uZmlnJTBBJTBBJTJCJTIwY3BfY29uZmlnJTIwJTNEJTIwVG9yY2hDb250ZXh0UGFyYWxsZWxDb25maWcoJTBBJTJCJTIwJTIwJTIwJTIwJTIwJTIwJTIwY3BfY29tbV9zdHJhdGVneSUzRCUyMmFsbHRvYWxsJTIyJTJDJTIwJTIzJTIwbm8lMjBuZWVkJTIwdG8lMjB1c2UlMjBjcF9jb25maWclMjBhdCUyMGFsbCUyQyUyMGlmJTIweW91JTIwd2FudCUyMHRvJTIwdXNlJTIwdGhlJTIwZGVmYXVsdCUyMCUyMmFsbGdhdGhlciUyMiUwQSUyQiUyMCklMEElMEElMkIlMjBwYXJhbGxlbGlzbV9jb25maWclMjAlM0QlMjBQYXJhbGxlbGlzbUNvbmZpZyglMEElMkIlMjAlMjAlMjAlMjAlMjBjcF9zaXplJTNEOCUyQyUwQSUyQiUyMCUyMCUyMCUyMCUyMGNwX2hhbmRsZXIlM0RjcF9jb25maWclMkMlMjAlMjAlMjMlMjBvciUyMGp1c3QlMjBjcF9zaXplJTNEOCUyQyUyMGlmJTIweW91JTIwd2FudCUyMHRvJTIwdXNlJTIwdGhlJTIwZGVmYXVsdCUyMCUyMmFsbGdhdGhlciUyMiUwQSUyQiUyMCklMEElMEFhY2NlbGVyYXRvciUyMCUzRCUyMEFjY2VsZXJhdG9yKCUwQSUyMCUyMCUyMCUyMC4uLiUyQyUwQSUyMCUyMCUyMCUyMHBhcmFsbGVsaXNtX2NvbmZpZyUzRHBhcmFsbGVsaXNtX2NvbmZpZyUyQyUwQSk=",highlighted:`from accelerate.utils import ParallelismConfig, TorchContextParallelConfig

<span class="hljs-addition">+ cp_config = TorchContextParallelConfig(</span>
<span class="hljs-addition">+       cp_comm_strategy=&quot;alltoall&quot;, # no need to use cp_config at all, if you want to use the default &quot;allgather&quot;</span>
<span class="hljs-addition">+ )</span>

<span class="hljs-addition">+ parallelism_config = ParallelismConfig(</span>
<span class="hljs-addition">+     cp_size=8,</span>
<span class="hljs-addition">+     cp_handler=cp_config,  # or just cp_size=8, if you want to use the default &quot;allgather&quot;</span>
<span class="hljs-addition">+ )</span>

accelerator = Accelerator(
    ...,
    parallelism_config=parallelism_config,
)`,wrap:!1}}),S=new _e({props:{code:"YWNjZWxlcmF0ZSUyMGxhdW5jaCUyMC0tcGFyYWxsZWxpc20tY29uZmlnLWNwLXNpemUlMjA4JTIwLS1wYXJhbGxlbGlzbS1jb25maWctY3AtY29tbS1zdHJhdGVneSUyMCU1QmFsbGdhdGhlciU3Q2FsbHRvYWxsJTVEJTIwLi4u",highlighted:"accelerate launch --parallelism-config-cp-size 8 --parallelism-config-cp-comm-strategy [allgather|alltoall] ...",wrap:!1}}),x=new W({props:{warning:!1,$$slots:{default:[hl]},$$scope:{ctx:M}}}),v=new W({props:{warning:!1,$$slots:{default:[pl]},$$scope:{ctx:M}}}),C=new W({props:{warning:!0,$$slots:{default:[dl]},$$scope:{ctx:M}}}),U=new W({props:{warning:!0,$$slots:{default:[ml]},$$scope:{ctx:M}}}),q=new _e({props:{code:"Zm9yJTIwYmF0Y2glMjBpbiUyMGRhdGFsb2FkZXIlM0ElMEElMjAlMjAlMjAlMjB3aXRoJTIwYWNjZWxlcmF0b3IubWF5YmVfY29udGV4dF9wYXJhbGxlbCglMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBidWZmZXJzJTNEJTVCYmF0Y2glNUIlMjJpbnB1dF9pZHMlMjIlNUQlMkMlMjBiYXRjaCU1QiUyMmF0dGVudGlvbl9tYXNrJTIyJTVEJTVEJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwYnVmZmVyX3NlcV9kaW1zJTNEJTVCMSUyQyUyMDElNUQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBub19yZXN0b3JlX2J1ZmZlcnMlM0QlN0JiYXRjaCU1QiUyMmlucHV0X2lkcyUyMiU1RCUyQyUyMGJhdGNoJTVCJTIybGFiZWxzJTIyJTVEJTdEJTJDJTBBJTIwJTIwJTIwJTIwKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dHMlMjAlM0QlMjBtb2RlbCgqKmJhdGNoKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMC4uLg==",highlighted:`<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> dataloader:
    <span class="hljs-keyword">with</span> accelerator.maybe_context_parallel(
        buffers=[batch[<span class="hljs-string">&quot;input_ids&quot;</span>], batch[<span class="hljs-string">&quot;attention_mask&quot;</span>]],
        buffer_seq_dims=[<span class="hljs-number">1</span>, <span class="hljs-number">1</span>],
        no_restore_buffers={batch[<span class="hljs-string">&quot;input_ids&quot;</span>], batch[<span class="hljs-string">&quot;labels&quot;</span>]},
    ):
        outputs = model(**batch)
        ...`,wrap:!1}}),j=new W({props:{warning:!0,$$slots:{default:[fl]},$$scope:{ctx:M}}}),k=new W({props:{warning:!1,$$slots:{default:[ul]},$$scope:{ctx:M}}}),N=new b({props:{title:"Accelerate’s interface",local:"accelerates-interface",headingTag:"h2"}}),G=new W({props:{warning:!0,$$slots:{default:[gl]},$$scope:{ctx:M}}}),R=new b({props:{title:"Configurable options",local:"configurable-options",headingTag:"h2"}}),te=new b({props:{title:"Technical details",local:"technical-details",headingTag:"h2"}}),I=new W({props:{warning:!1,$$slots:{default:[wl]},$$scope:{ctx:M}}}),ae=new b({props:{title:"So how does it work?",local:"so-how-does-it-work",headingTag:"h2"}}),ie=new _e({props:{code:"Y29tbV9rZXJuZWwlMjAlM0QlMjAlN0IlMjJhbGxnYXRoZXIlMjIlM0ElMjBhbGxnYXRoZXIlMkMlMjAlMjJhbGx0b2FsbCUyMiUzQSUyMGFsbHRvYWxsJTdEJTVCY29udGV4dF9wYXJhbGxlbF9zaGFyZF9yb3RhdGlvbiU1RCUwQVFpJTJDJTIwS2klMkMlMjBWaSUyMCUzRCUyMHNoYXJkKFElMkMlMjBLJTJDJTIwViUyQyUyMHNlcV9kaW0pJTBBYXR0biU1QmklNUQlMjAlM0QlMjBhdHRuKFFpJTJDJTIwS2klMkMlMjBWaSklMEFmb3IlMjBqJTIwaW4lMjByYW5nZShjb250ZXh0X3BhcmFsbGVsX3NpemUpJTNBJTBBJTIwJTIwJTIwJTIwS2olMkMlMjBWaiUyMCUzRCUyMGNvbW1fa2VybmVsKCklMEElMjAlMjAlMjAlMjBhdHRuJTVCaiU1RCUyMCUzRCUyMGF0dG4oUWklMkMlMjBLaiUyQyUyMFZqKSUyMCUyMyUyMCU1QmJhdGNoJTJDJTIwbnVtX2hlYWRzJTJDJTIwc2VxX2xlbiUyMCUyRiUyRiUyMGNvbnRleHRfcGFyYWxsZWxfc2l6ZSUyQyUyMGhlYWRfZGltJTVEJTBBJTBBZmluYWxfYXR0biUyMCUzRCUyMGNvbWJpbmUoYXR0bik=",highlighted:`comm_kernel = {<span class="hljs-string">&quot;allgather&quot;</span>: allgather, <span class="hljs-string">&quot;alltoall&quot;</span>: alltoall}[context_parallel_shard_rotation]
Qi, Ki, Vi = shard(Q, K, V, seq_dim)
attn[i] = attn(Qi, Ki, Vi)
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(context_parallel_size):
    Kj, Vj = comm_kernel()
    attn[j] = attn(Qi, Kj, Vj) <span class="hljs-comment"># [batch, num_heads, seq_len // context_parallel_size, head_dim]</span>

final_attn = combine(attn)`,wrap:!1}}),oe=new b({props:{title:"all-to-all vs all-gather",local:"all-to-all-vs-all-gather",headingTag:"h2"}}),re=new b({props:{title:"all-gather",local:"all-gather",headingTag:"h3"}}),he=new b({props:{title:"all-to-all",local:"all-to-all",headingTag:"h3"}}),de=new b({props:{title:"How to choose the right rotation method?",local:"how-to-choose-the-right-rotation-method",headingTag:"h2"}}),ue=new b({props:{title:"Why only FSDP2?",local:"why-only-fsdp2",headingTag:"h2"}}),$e=new b({props:{title:"Data dispatching in joint mesh",local:"data-dispatching-in-joint-mesh",headingTag:"h2"}}),Me=new _e({props:{code:"YW5kJTIwc28lMjBvbi4=",highlighted:`<span class="hljs-comment"># 8 GPUS, --dp_shard_size 4, --cp_size 2</span>
<span class="hljs-comment"># mesh = [[0, 1], [2, 3], [4, 5], [6, 7]]</span>
<span class="hljs-comment"># model is sharded across the whole mesh (each GPU holds 1/8 of the model)</span>
<span class="hljs-comment"># GPUs 0,1 = batch 0</span>
<span class="hljs-comment"># GPUs 2,3 = batch 1</span>
... <span class="hljs-keyword">and</span> so <span class="hljs-keyword">on</span>.`,wrap:!1}}),be=new cl({props:{source:"https://github.com/huggingface/accelerate/blob/main/docs/source/concept_guides/context_parallelism.md"}}),{c(){n=r("meta"),p=s(),o=r("p"),$=s(),d(_.$$.fragment),y=s(),T=r("p"),T.innerHTML=Tt,ve=s(),d(z.$$.fragment),Ce=s(),F=r("p"),F.innerHTML=xt,Ue=s(),L=r("p"),L.textContent=vt,je=s(),d(A.$$.fragment),Je=s(),d(B.$$.fragment),ke=s(),Y=r("p"),Y.innerHTML=Ct,Ge=s(),d(S.$$.fragment),Ie=s(),d(x.$$.fragment),He=s(),d(v.$$.fragment),Pe=s(),d(C.$$.fragment),We=s(),d(U.$$.fragment),Ve=s(),Z=r("p"),Z.innerHTML=Ut,ze=s(),d(q.$$.fragment),Fe=s(),d(j.$$.fragment),Le=s(),Q=r("p"),Q.textContent=jt,Ae=s(),J=r("p"),J.innerHTML=Jt,Be=s(),d(k.$$.fragment),Ye=s(),d(N.$$.fragment),Se=s(),X=r("p"),X.textContent=kt,Ze=s(),E=r("ul"),E.innerHTML=Gt,qe=s(),d(G.$$.fragment),Qe=s(),d(R.$$.fragment),Ne=s(),D=r("p"),D.innerHTML=It,Xe=s(),K=r("ul"),K.innerHTML=Ht,Ee=s(),O=r("p"),O.textContent=Pt,Re=s(),ee=r("p"),ee.innerHTML=Wt,De=s(),d(te.$$.fragment),Ke=s(),d(I.$$.fragment),Oe=s(),le=r("p"),le.innerHTML=Vt,et=s(),d(ae.$$.fragment),tt=s(),ne=r("p"),ne.innerHTML=zt,lt=s(),se=r("p"),se.textContent=Ft,at=s(),d(ie.$$.fragment),nt=s(),d(oe.$$.fragment),st=s(),d(re.$$.fragment),it=s(),ce=r("p"),ce.innerHTML=Lt,ot=s(),d(he.$$.fragment),rt=s(),pe=r("p"),pe.innerHTML=At,ct=s(),d(de.$$.fragment),ht=s(),me=r("p"),me.innerHTML=Bt,pt=s(),fe=r("p"),fe.textContent=Yt,dt=s(),H=r("p"),H.innerHTML=St,mt=s(),d(ue.$$.fragment),ft=s(),ge=r("p"),ge.innerHTML=Zt,ut=s(),P=r("p"),P.innerHTML=qt,gt=s(),we=r("p"),we.textContent=Qt,wt=s(),d($e.$$.fragment),$t=s(),ye=r("p"),ye.innerHTML=Nt,yt=s(),d(Me.$$.fragment),Mt=s(),d(be.$$.fragment),bt=s(),Te=r("p"),this.h()},l(e){const t=ol("svelte-u9bgzb",document.head);n=c(t,"META",{name:!0,content:!0}),t.forEach(l),p=i(e),o=c(e,"P",{}),ll(o).forEach(l),$=i(e),m(_.$$.fragment,e),y=i(e),T=c(e,"P",{"data-svelte-h":!0}),h(T)!=="svelte-1vuwxvs"&&(T.innerHTML=Tt),ve=i(e),m(z.$$.fragment,e),Ce=i(e),F=c(e,"P",{"data-svelte-h":!0}),h(F)!=="svelte-xoe8wo"&&(F.innerHTML=xt),Ue=i(e),L=c(e,"P",{"data-svelte-h":!0}),h(L)!=="svelte-1u7kh92"&&(L.textContent=vt),je=i(e),m(A.$$.fragment,e),Je=i(e),m(B.$$.fragment,e),ke=i(e),Y=c(e,"P",{"data-svelte-h":!0}),h(Y)!=="svelte-hsccy5"&&(Y.innerHTML=Ct),Ge=i(e),m(S.$$.fragment,e),Ie=i(e),m(x.$$.fragment,e),He=i(e),m(v.$$.fragment,e),Pe=i(e),m(C.$$.fragment,e),We=i(e),m(U.$$.fragment,e),Ve=i(e),Z=c(e,"P",{"data-svelte-h":!0}),h(Z)!=="svelte-9b3tbk"&&(Z.innerHTML=Ut),ze=i(e),m(q.$$.fragment,e),Fe=i(e),m(j.$$.fragment,e),Le=i(e),Q=c(e,"P",{"data-svelte-h":!0}),h(Q)!=="svelte-l714n4"&&(Q.textContent=jt),Ae=i(e),J=c(e,"P",{align:!0,"data-svelte-h":!0}),h(J)!=="svelte-zw16sw"&&(J.innerHTML=Jt),Be=i(e),m(k.$$.fragment,e),Ye=i(e),m(N.$$.fragment,e),Se=i(e),X=c(e,"P",{"data-svelte-h":!0}),h(X)!=="svelte-jcewir"&&(X.textContent=kt),Ze=i(e),E=c(e,"UL",{"data-svelte-h":!0}),h(E)!=="svelte-1npdv1z"&&(E.innerHTML=Gt),qe=i(e),m(G.$$.fragment,e),Qe=i(e),m(R.$$.fragment,e),Ne=i(e),D=c(e,"P",{"data-svelte-h":!0}),h(D)!=="svelte-1jcjqxa"&&(D.innerHTML=It),Xe=i(e),K=c(e,"UL",{"data-svelte-h":!0}),h(K)!=="svelte-19ddlaf"&&(K.innerHTML=Ht),Ee=i(e),O=c(e,"P",{"data-svelte-h":!0}),h(O)!=="svelte-pt2d1o"&&(O.textContent=Pt),Re=i(e),ee=c(e,"P",{"data-svelte-h":!0}),h(ee)!=="svelte-8bfpem"&&(ee.innerHTML=Wt),De=i(e),m(te.$$.fragment,e),Ke=i(e),m(I.$$.fragment,e),Oe=i(e),le=c(e,"P",{"data-svelte-h":!0}),h(le)!=="svelte-5gcsxu"&&(le.innerHTML=Vt),et=i(e),m(ae.$$.fragment,e),tt=i(e),ne=c(e,"P",{"data-svelte-h":!0}),h(ne)!=="svelte-1q8d3hy"&&(ne.innerHTML=zt),lt=i(e),se=c(e,"P",{"data-svelte-h":!0}),h(se)!=="svelte-1qbnxlz"&&(se.textContent=Ft),at=i(e),m(ie.$$.fragment,e),nt=i(e),m(oe.$$.fragment,e),st=i(e),m(re.$$.fragment,e),it=i(e),ce=c(e,"P",{"data-svelte-h":!0}),h(ce)!=="svelte-6a32kj"&&(ce.innerHTML=Lt),ot=i(e),m(he.$$.fragment,e),rt=i(e),pe=c(e,"P",{"data-svelte-h":!0}),h(pe)!=="svelte-jzgpzc"&&(pe.innerHTML=At),ct=i(e),m(de.$$.fragment,e),ht=i(e),me=c(e,"P",{"data-svelte-h":!0}),h(me)!=="svelte-oa58fl"&&(me.innerHTML=Bt),pt=i(e),fe=c(e,"P",{"data-svelte-h":!0}),h(fe)!=="svelte-1ldmtvt"&&(fe.textContent=Yt),dt=i(e),H=c(e,"P",{align:!0,"data-svelte-h":!0}),h(H)!=="svelte-1c3ofsn"&&(H.innerHTML=St),mt=i(e),m(ue.$$.fragment,e),ft=i(e),ge=c(e,"P",{"data-svelte-h":!0}),h(ge)!=="svelte-1ikc9rn"&&(ge.innerHTML=Zt),ut=i(e),P=c(e,"P",{align:!0,"data-svelte-h":!0}),h(P)!=="svelte-1jnhmn3"&&(P.innerHTML=qt),gt=i(e),we=c(e,"P",{"data-svelte-h":!0}),h(we)!=="svelte-10ujnkx"&&(we.textContent=Qt),wt=i(e),m($e.$$.fragment,e),$t=i(e),ye=c(e,"P",{"data-svelte-h":!0}),h(ye)!=="svelte-14bdjd0"&&(ye.innerHTML=Nt),yt=i(e),m(Me.$$.fragment,e),Mt=i(e),m(be.$$.fragment,e),bt=i(e),Te=c(e,"P",{}),ll(Te).forEach(l),this.h()},h(){xe(n,"name","hf:doc:metadata"),xe(n,"content",yl),xe(J,"align","center"),xe(H,"align","center"),xe(P,"align","center")},m(e,t){rl(document.head,n),a(e,p,t),a(e,o,t),a(e,$,t),f(_,e,t),a(e,y,t),a(e,T,t),a(e,ve,t),f(z,e,t),a(e,Ce,t),a(e,F,t),a(e,Ue,t),a(e,L,t),a(e,je,t),f(A,e,t),a(e,Je,t),f(B,e,t),a(e,ke,t),a(e,Y,t),a(e,Ge,t),f(S,e,t),a(e,Ie,t),f(x,e,t),a(e,He,t),f(v,e,t),a(e,Pe,t),f(C,e,t),a(e,We,t),f(U,e,t),a(e,Ve,t),a(e,Z,t),a(e,ze,t),f(q,e,t),a(e,Fe,t),f(j,e,t),a(e,Le,t),a(e,Q,t),a(e,Ae,t),a(e,J,t),a(e,Be,t),f(k,e,t),a(e,Ye,t),f(N,e,t),a(e,Se,t),a(e,X,t),a(e,Ze,t),a(e,E,t),a(e,qe,t),f(G,e,t),a(e,Qe,t),f(R,e,t),a(e,Ne,t),a(e,D,t),a(e,Xe,t),a(e,K,t),a(e,Ee,t),a(e,O,t),a(e,Re,t),a(e,ee,t),a(e,De,t),f(te,e,t),a(e,Ke,t),f(I,e,t),a(e,Oe,t),a(e,le,t),a(e,et,t),f(ae,e,t),a(e,tt,t),a(e,ne,t),a(e,lt,t),a(e,se,t),a(e,at,t),f(ie,e,t),a(e,nt,t),f(oe,e,t),a(e,st,t),f(re,e,t),a(e,it,t),a(e,ce,t),a(e,ot,t),f(he,e,t),a(e,rt,t),a(e,pe,t),a(e,ct,t),f(de,e,t),a(e,ht,t),a(e,me,t),a(e,pt,t),a(e,fe,t),a(e,dt,t),a(e,H,t),a(e,mt,t),f(ue,e,t),a(e,ft,t),a(e,ge,t),a(e,ut,t),a(e,P,t),a(e,gt,t),a(e,we,t),a(e,wt,t),f($e,e,t),a(e,$t,t),a(e,ye,t),a(e,yt,t),f(Me,e,t),a(e,Mt,t),f(be,e,t),a(e,bt,t),a(e,Te,t),_t=!0},p(e,[t]){const Xt={};t&2&&(Xt.$$scope={dirty:t,ctx:e}),x.$set(Xt);const Et={};t&2&&(Et.$$scope={dirty:t,ctx:e}),v.$set(Et);const Rt={};t&2&&(Rt.$$scope={dirty:t,ctx:e}),C.$set(Rt);const Dt={};t&2&&(Dt.$$scope={dirty:t,ctx:e}),U.$set(Dt);const Kt={};t&2&&(Kt.$$scope={dirty:t,ctx:e}),j.$set(Kt);const Ot={};t&2&&(Ot.$$scope={dirty:t,ctx:e}),k.$set(Ot);const el={};t&2&&(el.$$scope={dirty:t,ctx:e}),G.$set(el);const tl={};t&2&&(tl.$$scope={dirty:t,ctx:e}),I.$set(tl)},i(e){_t||(u(_.$$.fragment,e),u(z.$$.fragment,e),u(A.$$.fragment,e),u(B.$$.fragment,e),u(S.$$.fragment,e),u(x.$$.fragment,e),u(v.$$.fragment,e),u(C.$$.fragment,e),u(U.$$.fragment,e),u(q.$$.fragment,e),u(j.$$.fragment,e),u(k.$$.fragment,e),u(N.$$.fragment,e),u(G.$$.fragment,e),u(R.$$.fragment,e),u(te.$$.fragment,e),u(I.$$.fragment,e),u(ae.$$.fragment,e),u(ie.$$.fragment,e),u(oe.$$.fragment,e),u(re.$$.fragment,e),u(he.$$.fragment,e),u(de.$$.fragment,e),u(ue.$$.fragment,e),u($e.$$.fragment,e),u(Me.$$.fragment,e),u(be.$$.fragment,e),_t=!0)},o(e){g(_.$$.fragment,e),g(z.$$.fragment,e),g(A.$$.fragment,e),g(B.$$.fragment,e),g(S.$$.fragment,e),g(x.$$.fragment,e),g(v.$$.fragment,e),g(C.$$.fragment,e),g(U.$$.fragment,e),g(q.$$.fragment,e),g(j.$$.fragment,e),g(k.$$.fragment,e),g(N.$$.fragment,e),g(G.$$.fragment,e),g(R.$$.fragment,e),g(te.$$.fragment,e),g(I.$$.fragment,e),g(ae.$$.fragment,e),g(ie.$$.fragment,e),g(oe.$$.fragment,e),g(re.$$.fragment,e),g(he.$$.fragment,e),g(de.$$.fragment,e),g(ue.$$.fragment,e),g($e.$$.fragment,e),g(Me.$$.fragment,e),g(be.$$.fragment,e),_t=!1},d(e){e&&(l(p),l(o),l($),l(y),l(T),l(ve),l(Ce),l(F),l(Ue),l(L),l(je),l(Je),l(ke),l(Y),l(Ge),l(Ie),l(He),l(Pe),l(We),l(Ve),l(Z),l(ze),l(Fe),l(Le),l(Q),l(Ae),l(J),l(Be),l(Ye),l(Se),l(X),l(Ze),l(E),l(qe),l(Qe),l(Ne),l(D),l(Xe),l(K),l(Ee),l(O),l(Re),l(ee),l(De),l(Ke),l(Oe),l(le),l(et),l(tt),l(ne),l(lt),l(se),l(at),l(nt),l(st),l(it),l(ce),l(ot),l(rt),l(pe),l(ct),l(ht),l(me),l(pt),l(fe),l(dt),l(H),l(mt),l(ft),l(ge),l(ut),l(P),l(gt),l(we),l(wt),l($t),l(ye),l(yt),l(Mt),l(bt),l(Te)),l(n),w(_,e),w(z,e),w(A,e),w(B,e),w(S,e),w(x,e),w(v,e),w(C,e),w(U,e),w(q,e),w(j,e),w(k,e),w(N,e),w(G,e),w(R,e),w(te,e),w(I,e),w(ae,e),w(ie,e),w(oe,e),w(re,e),w(he,e),w(de,e),w(ue,e),w($e,e),w(Me,e),w(be,e)}}}const yl='{"title":"Context Parallel in 🤗 accelerate","local":"context-parallel-in--accelerate","sections":[{"title":"Why context parallelism?","local":"why-context-parallelism","sections":[],"depth":2},{"title":"How to use context parallelism?","local":"how-to-use-context-parallelism","sections":[],"depth":2},{"title":"Accelerate’s interface","local":"accelerates-interface","sections":[],"depth":2},{"title":"Configurable options","local":"configurable-options","sections":[],"depth":2},{"title":"Technical details","local":"technical-details","sections":[],"depth":2},{"title":"So how does it work?","local":"so-how-does-it-work","sections":[],"depth":2},{"title":"all-to-all vs all-gather","local":"all-to-all-vs-all-gather","sections":[{"title":"all-gather","local":"all-gather","sections":[],"depth":3},{"title":"all-to-all","local":"all-to-all","sections":[],"depth":3}],"depth":2},{"title":"How to choose the right rotation method?","local":"how-to-choose-the-right-rotation-method","sections":[],"depth":2},{"title":"Why only FSDP2?","local":"why-only-fsdp2","sections":[],"depth":2},{"title":"Data dispatching in joint mesh","local":"data-dispatching-in-joint-mesh","sections":[],"depth":2}],"depth":1}';function Ml(M){return nl(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Cl extends sl{constructor(n){super(),il(this,n,Ml,$l,al,{})}}export{Cl as component};
