import{S as No,i as Ho,s as Io,e as n,k as c,t as a,w as m,M as Ro,c as p,d as s,m as d,a as i,h as l,x as f,b as g,G as t,g as o,y as u,q as _,o as h,B as $,v as Bo}from"../../chunks/vendor-hf-doc-builder.js";import{T as Fo}from"../../chunks/Tip-hf-doc-builder.js";import{Y as Go}from"../../chunks/Youtube-hf-doc-builder.js";import{I as Jo}from"../../chunks/IconCopyLink-hf-doc-builder.js";import{C as k}from"../../chunks/CodeBlock-hf-doc-builder.js";import{D as Yo}from"../../chunks/DocNotebookDropdown-hf-doc-builder.js";function Qo(We){let b,q,z,le,I;return{c(){b=n("p"),q=a("\u270F\uFE0F "),z=n("strong"),le=a("\u8BD5\u8BD5\u770B\uFF01"),I=a(" \u4FEE\u6539\u4E4B\u524D\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4EE5\u5728 SST-2 \u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u60A8\u7684\u6A21\u578B\u3002")},l(A){b=p(A,"P",{});var x=i(b);q=l(x,"\u270F\uFE0F "),z=p(x,"STRONG",{});var U=i(z);le=l(U,"\u8BD5\u8BD5\u770B\uFF01"),U.forEach(s),I=l(x," \u4FEE\u6539\u4E4B\u524D\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4EE5\u5728 SST-2 \u6570\u636E\u96C6\u4E0A\u5FAE\u8C03\u60A8\u7684\u6A21\u578B\u3002"),x.forEach(s)},m(A,x){o(A,b,x),t(b,q),t(b,z),t(z,le),t(b,I)},d(A){A&&s(b)}}}function Ko(We){let b;return{c(){b=a("\u26A0\uFE0F \u4E3A\u4E86\u4F7F\u4E91\u7AEF TPU \u63D0\u4F9B\u7684\u52A0\u901F\u53D1\u6325\u6700\u5927\u7684\u6548\u76CA\uFF0C\u6211\u4EEC\u5EFA\u8BAE\u4F7F\u7528\u6807\u8BB0\u5668(tokenizer)\u7684 `padding=max_length` \u548C `max_length` \u53C2\u6570\u5C06\u60A8\u7684\u6837\u672C\u586B\u5145\u5230\u56FA\u5B9A\u957F\u5EA6\u3002")},l(q){b=l(q,"\u26A0\uFE0F \u4E3A\u4E86\u4F7F\u4E91\u7AEF TPU \u63D0\u4F9B\u7684\u52A0\u901F\u53D1\u6325\u6700\u5927\u7684\u6548\u76CA\uFF0C\u6211\u4EEC\u5EFA\u8BAE\u4F7F\u7528\u6807\u8BB0\u5668(tokenizer)\u7684 `padding=max_length` \u548C `max_length` \u53C2\u6570\u5C06\u60A8\u7684\u6837\u672C\u586B\u5145\u5230\u56FA\u5B9A\u957F\u5EA6\u3002")},m(q,z){o(q,b,z)},d(q){q&&s(b)}}}function Vo(We){let b,q,z,le,I,A,x,U,Jt,R,oa,Ze,na,pa,Yt,re,Qt,oe,ia,Kt,W,ca,et,da,ma,tt,fa,ua,Vt,M,N,_a,st,ha,$a,at,ba,ka,va,T,ja,lt,Ea,wa,rt,ga,za,ot,qa,ya,Ca,nt,Aa,Xt,B,Da,pt,Pa,Oa,Zt,ne,es,Me,Sa,ts,pe,ss,Le,xa,as,ie,ls,Fe,Ta,rs,ce,os,de,ns,L,Ua,it,Wa,Ma,ct,La,Fa,ps,Ge,Ga,is,me,cs,J,Na,dt,Ha,Ia,ds,fe,ms,ue,fs,j,Ra,mt,Ba,Ja,ft,Ya,Qa,ut,Ka,Va,_t,Xa,Za,ht,el,tl,us,E,sl,$t,al,ll,bt,rl,ol,kt,nl,pl,vt,il,cl,_e,dl,ml,_s,he,hs,F,fl,jt,ul,_l,Et,hl,$l,$s,$e,bs,be,ks,ke,bl,vs,D,kl,wt,vl,jl,gt,El,wl,zt,gl,zl,js,ve,Es,je,ws,Y,ql,qt,yl,Cl,gs,Ee,zs,Ne,Al,qs,we,Dl,ys,w,Pl,yt,Ol,Sl,Ct,xl,Tl,At,Ul,Wl,Dt,Ml,Ll,Pt,Fl,Gl,Cs,ge,As,ze,Ds,He,Nl,Ps,Q,Os,H,K,Ot,qe,Hl,St,Il,Ss,ye,xs,V,Rl,Ce,Bl,Jl,Ts,Ae,Us,Ie,Yl,Ws,De,Ms,y,Ql,xt,Kl,Vl,Tt,Xl,Zl,Ut,er,tr,Wt,sr,ar,Ls,v,lr,Mt,rr,or,Lt,nr,pr,Ft,ir,cr,Gt,dr,mr,Nt,fr,ur,Ht,_r,hr,Fs,X,Gs,Re,$r,Ns,Pe,Hs,Z,br,It,kr,vr,Is,Oe,Rs,Be,jr,Bs,Se,Js,Je,Er,Ys,ee,wr,Rt,gr,zr,Qs,xe,Ks,te,qr,Te,yr,Cr,Vs;return A=new Yo({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/notebooks/blob/master/course/chapter3/section4.ipynb"},{label:"Aws Studio",value:"https://studiolab.sagemaker.aws/import/github/huggingface/notebooks/blob/master/course/chapter3/section4.ipynb"}]}}),U=new Go({props:{id:"Dh9CL8fyG80"}}),re=new k({props:{code:`from datasets import load_dataset
from transformers import AutoTokenizer, DataCollatorWithPadding

raw_datasets = load_dataset("glue", "mrpc")
checkpoint = "bert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)


def tokenize_function(example):
    return tokenizer(example["sentence1"], example["sentence2"], truncation=True)


tokenized_datasets = raw_datasets.map(tokenize_function, batched=True)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)`,highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoTokenizer, DataCollatorWithPadding

raw_datasets = load_dataset(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;mrpc&quot;</span>)
checkpoint = <span class="hljs-string">&quot;bert-base-uncased&quot;</span>
tokenizer = AutoTokenizer.from_pretrained(checkpoint)


<span class="hljs-keyword">def</span> <span class="hljs-title function_">tokenize_function</span>(<span class="hljs-params">example</span>):
    <span class="hljs-keyword">return</span> tokenizer(example[<span class="hljs-string">&quot;sentence1&quot;</span>], example[<span class="hljs-string">&quot;sentence2&quot;</span>], truncation=<span class="hljs-literal">True</span>)


tokenized_datasets = raw_datasets.<span class="hljs-built_in">map</span>(tokenize_function, batched=<span class="hljs-literal">True</span>)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)`}}),ne=new k({props:{code:`tokenized_datasets = tokenized_datasets.remove_columns(["sentence1", "sentence2", "idx"])
tokenized_datasets = tokenized_datasets.rename_column("label", "labels")
tokenized_datasets.set_format("torch")
tokenized_datasets["train"].column_names`,highlighted:`tokenized_datasets = tokenized_datasets.remove_columns([<span class="hljs-string">&quot;sentence1&quot;</span>, <span class="hljs-string">&quot;sentence2&quot;</span>, <span class="hljs-string">&quot;idx&quot;</span>])
tokenized_datasets = tokenized_datasets.rename_column(<span class="hljs-string">&quot;label&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>)
tokenized_datasets.set_format(<span class="hljs-string">&quot;torch&quot;</span>)
tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>].column_names`}}),pe=new k({props:{code:'["attention_mask", "input_ids", "labels", "token_type_ids"]',highlighted:'[<span class="hljs-string">&quot;attention_mask&quot;</span>, <span class="hljs-string">&quot;input_ids&quot;</span>, <span class="hljs-string">&quot;labels&quot;</span>, <span class="hljs-string">&quot;token_type_ids&quot;</span>]'}}),ie=new k({props:{code:`from torch.utils.data import DataLoader

train_dataloader = DataLoader(
    tokenized_datasets["train"], shuffle=True, batch_size=8, collate_fn=data_collator
)
eval_dataloader = DataLoader(
    tokenized_datasets["validation"], batch_size=8, collate_fn=data_collator
)`,highlighted:`<span class="hljs-keyword">from</span> torch.utils.data <span class="hljs-keyword">import</span> DataLoader

train_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;train&quot;</span>], shuffle=<span class="hljs-literal">True</span>, batch_size=<span class="hljs-number">8</span>, collate_fn=data_collator
)
eval_dataloader = DataLoader(
    tokenized_datasets[<span class="hljs-string">&quot;validation&quot;</span>], batch_size=<span class="hljs-number">8</span>, collate_fn=data_collator
)`}}),ce=new k({props:{code:`for batch in train_dataloader:
    break
{k: v.shape for k, v in batch.items()}`,highlighted:`<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
    <span class="hljs-keyword">break</span>
{k: v.shape <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}`}}),de=new k({props:{code:`{'attention_mask': torch.Size([8, 65]),
 'input_ids': torch.Size([8, 65]),
 'labels': torch.Size([8]),
 'token_type_ids': torch.Size([8, 65])}`,highlighted:`{<span class="hljs-string">&#x27;attention_mask&#x27;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>]),
 <span class="hljs-string">&#x27;input_ids&#x27;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>]),
 <span class="hljs-string">&#x27;labels&#x27;</span>: torch.Size([<span class="hljs-number">8</span>]),
 <span class="hljs-string">&#x27;token_type_ids&#x27;</span>: torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">65</span>])}`}}),me=new k({props:{code:`from transformers import AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForSequenceClassification

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)`}}),fe=new k({props:{code:`outputs = model(**batch)
print(outputs.loss, outputs.logits.shape)`,highlighted:`outputs = model(**batch)
<span class="hljs-built_in">print</span>(outputs.loss, outputs.logits.shape)`}}),ue=new k({props:{code:"tensor(0.5441, grad_fn=<NllLossBackward>) torch.Size([8, 2])",highlighted:'tensor(<span class="hljs-number">0.5441</span>, grad_fn=&lt;NllLossBackward&gt;) torch.Size([<span class="hljs-number">8</span>, <span class="hljs-number">2</span>])'}}),he=new k({props:{code:`from transformers import AdamW

optimizer = AdamW(model.parameters(), lr=5e-5)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW

optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">5e-5</span>)`}}),$e=new k({props:{code:`from transformers import get_scheduler

num_epochs = 3
num_training_steps = num_epochs * len(train_dataloader)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)
print(num_training_steps)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> get_scheduler

num_epochs = <span class="hljs-number">3</span>
num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader)
lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)
<span class="hljs-built_in">print</span>(num_training_steps)`}}),be=new k({props:{code:"1377",highlighted:'<span class="hljs-number">1377</span>'}}),ve=new k({props:{code:`import torch

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)
device`,highlighted:`<span class="hljs-keyword">import</span> torch

device = torch.device(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
model.to(device)
device`}}),je=new k({props:{code:"device(type='cuda')",highlighted:'device(<span class="hljs-built_in">type</span>=<span class="hljs-string">&#x27;cuda&#x27;</span>)'}}),Ee=new k({props:{code:`from tqdm.auto import tqdm

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dataloader:
        batch = {k: v.to(device) for k, v in batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)`,highlighted:`<span class="hljs-keyword">from</span> tqdm.auto <span class="hljs-keyword">import</span> tqdm

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
        batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)`}}),ge=new k({props:{code:`from datasets import load_metric

metric = load_metric("glue", "mrpc")
model.eval()
for batch in eval_dataloader:
    batch = {k: v.to(device) for k, v in batch.items()}
    with torch.no_grad():
        outputs = model(**batch)

    logits = outputs.logits
    predictions = torch.argmax(logits, dim=-1)
    metric.add_batch(predictions=predictions, references=batch["labels"])

metric.compute()`,highlighted:`<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_metric

metric = load_metric(<span class="hljs-string">&quot;glue&quot;</span>, <span class="hljs-string">&quot;mrpc&quot;</span>)
model.<span class="hljs-built_in">eval</span>()
<span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> eval_dataloader:
    batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
    <span class="hljs-keyword">with</span> torch.no_grad():
        outputs = model(**batch)

    logits = outputs.logits
    predictions = torch.argmax(logits, dim=-<span class="hljs-number">1</span>)
    metric.add_batch(predictions=predictions, references=batch[<span class="hljs-string">&quot;labels&quot;</span>])

metric.compute()`}}),ze=new k({props:{code:"{'accuracy': 0.8431372549019608, 'f1': 0.8907849829351535}",highlighted:'{<span class="hljs-string">&#x27;accuracy&#x27;</span>: <span class="hljs-number">0.8431372549019608</span>, <span class="hljs-string">&#x27;f1&#x27;</span>: <span class="hljs-number">0.8907849829351535</span>}'}}),Q=new Fo({props:{$$slots:{default:[Qo]},$$scope:{ctx:We}}}),qe=new Jo({}),ye=new Go({props:{id:"s7dy8QRgjJ0"}}),Ae=new k({props:{code:`from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
optimizer = AdamW(model.parameters(), lr=3e-5)

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)

num_epochs = 3
num_training_steps = num_epochs * len(train_dataloader)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dataloader:
        batch = {k: v.to(device) for k, v in batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)`,highlighted:`<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoModelForSequenceClassification, get_scheduler

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)
optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">3e-5</span>)

device = torch.device(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-keyword">if</span> torch.cuda.is_available() <span class="hljs-keyword">else</span> torch.device(<span class="hljs-string">&quot;cpu&quot;</span>)
model.to(device)

num_epochs = <span class="hljs-number">3</span>
num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dataloader)
lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dataloader:
        batch = {k: v.to(device) <span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> batch.items()}
        outputs = model(**batch)
        loss = outputs.loss
        loss.backward()

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)`}}),De=new k({props:{code:`+ from accelerate import Accelerator
  from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

+ accelerator = Accelerator()

  model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
  optimizer = AdamW(model.parameters(), lr=3e-5)

- device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
- model.to(device)

+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(
+     train_dataloader, eval_dataloader, model, optimizer
+ )

  num_epochs = 3
  num_training_steps = num_epochs * len(train_dataloader)
  lr_scheduler = get_scheduler(
      "linear",
      optimizer=optimizer,
      num_warmup_steps=0,
      num_training_steps=num_training_steps
  )

  progress_bar = tqdm(range(num_training_steps))

  model.train()
  for epoch in range(num_epochs):
      for batch in train_dataloader:
-         batch = {k: v.to(device) for k, v in batch.items()}
          outputs = model(**batch)
          loss = outputs.loss
-         loss.backward()
+         accelerator.backward(loss)

          optimizer.step()
          lr_scheduler.step()
          optimizer.zero_grad()
          progress_bar.update(1)`,highlighted:`<span class="hljs-addition">+ from accelerate import Accelerator</span>
  from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

<span class="hljs-addition">+ accelerator = Accelerator()</span>

  model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
  optimizer = AdamW(model.parameters(), lr=3e-5)

<span class="hljs-deletion">- device = torch.device(&quot;cuda&quot;) if torch.cuda.is_available() else torch.device(&quot;cpu&quot;)</span>
<span class="hljs-deletion">- model.to(device)</span>

<span class="hljs-addition">+ train_dataloader, eval_dataloader, model, optimizer = accelerator.prepare(</span>
<span class="hljs-addition">+     train_dataloader, eval_dataloader, model, optimizer</span>
<span class="hljs-addition">+ )</span>

  num_epochs = 3
  num_training_steps = num_epochs * len(train_dataloader)
  lr_scheduler = get_scheduler(
      &quot;linear&quot;,
      optimizer=optimizer,
      num_warmup_steps=0,
      num_training_steps=num_training_steps
  )

  progress_bar = tqdm(range(num_training_steps))

  model.train()
  for epoch in range(num_epochs):
      for batch in train_dataloader:
<span class="hljs-deletion">-         batch = {k: v.to(device) for k, v in batch.items()}</span>
          outputs = model(**batch)
          loss = outputs.loss
<span class="hljs-deletion">-         loss.backward()</span>
<span class="hljs-addition">+         accelerator.backward(loss)</span>

          optimizer.step()
          lr_scheduler.step()
          optimizer.zero_grad()
          progress_bar.update(1)`}}),X=new Fo({props:{$$slots:{default:[Ko]},$$scope:{ctx:We}}}),Pe=new k({props:{code:`from accelerate import Accelerator
from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

accelerator = Accelerator()

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
optimizer = AdamW(model.parameters(), lr=3e-5)

train_dl, eval_dl, model, optimizer = accelerator.prepare(
    train_dataloader, eval_dataloader, model, optimizer
)

num_epochs = 3
num_training_steps = num_epochs * len(train_dl)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dl:
        outputs = model(**batch)
        loss = outputs.loss
        accelerator.backward(loss)

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)`,highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> Accelerator
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AdamW, AutoModelForSequenceClassification, get_scheduler

accelerator = Accelerator()

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=<span class="hljs-number">2</span>)
optimizer = AdamW(model.parameters(), lr=<span class="hljs-number">3e-5</span>)

train_dl, eval_dl, model, optimizer = accelerator.prepare(
    train_dataloader, eval_dataloader, model, optimizer
)

num_epochs = <span class="hljs-number">3</span>
num_training_steps = num_epochs * <span class="hljs-built_in">len</span>(train_dl)
lr_scheduler = get_scheduler(
    <span class="hljs-string">&quot;linear&quot;</span>,
    optimizer=optimizer,
    num_warmup_steps=<span class="hljs-number">0</span>,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(<span class="hljs-built_in">range</span>(num_training_steps))

model.train()
<span class="hljs-keyword">for</span> epoch <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(num_epochs):
    <span class="hljs-keyword">for</span> batch <span class="hljs-keyword">in</span> train_dl:
        outputs = model(**batch)
        loss = outputs.loss
        accelerator.backward(loss)

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(<span class="hljs-number">1</span>)`}}),Oe=new k({props:{code:"accelerate config",highlighted:"accelerate config"}}),Se=new k({props:{code:"accelerate launch train.py",highlighted:'accelerate <span class="hljs-built_in">launch</span> train.py'}}),xe=new k({props:{code:`from accelerate import notebook_launcher

notebook_launcher(training_function)`,highlighted:`<span class="hljs-keyword">from</span> accelerate <span class="hljs-keyword">import</span> notebook_launcher

notebook_launcher(training_function)`}}),{c(){b=n("meta"),q=c(),z=n("h1"),le=a("\u4E00\u4E2A\u5B8C\u6574\u7684\u8BAD\u7EC3"),I=c(),m(A.$$.fragment),x=c(),m(U.$$.fragment),Jt=c(),R=n("p"),oa=a("\u73B0\u5728\uFF0C\u6211\u4EEC\u5C06\u4E86\u89E3\u5982\u4F55\u5728\u4E0D\u4F7F\u7528"),Ze=n("code"),na=a("Trainer"),pa=a("\u7C7B\u7684\u60C5\u51B5\u4E0B\u83B7\u5F97\u4E0E\u4E0A\u4E00\u8282\u76F8\u540C\u7684\u7ED3\u679C\u3002\u540C\u6837\uFF0C\u6211\u4EEC\u5047\u8BBE\u60A8\u5DF2\u7ECF\u5B66\u4E60\u4E86\u7B2C 2 \u8282\u4E2D\u7684\u6570\u636E\u5904\u7406\u3002\u4E0B\u9762\u662F\u4E00\u4E2A\u7B80\u77ED\u7684\u603B\u7ED3\uFF0C\u6DB5\u76D6\u4E86\u60A8\u9700\u8981\u7684\u6240\u6709\u5185\u5BB9:"),Yt=c(),m(re.$$.fragment),Qt=c(),oe=n("h3"),ia=a("\u8BAD\u7EC3\u524D\u7684\u51C6\u5907"),Kt=c(),W=n("p"),ca=a("\u5728\u5B9E\u9645\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E4B\u524D\uFF0C\u6211\u4EEC\u9700\u8981\u5B9A\u4E49\u4E00\u4E9B\u5BF9\u8C61\u3002\u7B2C\u4E00\u4E2A\u662F\u6211\u4EEC\u5C06\u7528\u4E8E\u8FED\u4EE3\u6279\u6B21\u7684\u6570\u636E\u52A0\u8F7D\u5668\u3002\u6211\u4EEC\u9700\u8981\u5BF9\u6211\u4EEC\u7684"),et=n("code"),da=a("tokenized_datasets"),ma=a("\u505A\u4E00\u4E9B\u5904\u7406\uFF0C\u6765\u5904\u7406"),tt=n("code"),fa=a("Trainer"),ua=a("\u81EA\u52A8\u4E3A\u6211\u4EEC\u505A\u7684\u4E00\u4E9B\u4E8B\u60C5\u3002\u5177\u4F53\u6765\u8BF4\uFF0C\u6211\u4EEC\u9700\u8981:"),Vt=c(),M=n("ul"),N=n("li"),_a=a("\u5220\u9664\u4E0E\u6A21\u578B\u4E0D\u671F\u671B\u7684\u503C\u76F8\u5BF9\u5E94\u7684\u5217\uFF08\u5982"),st=n("code"),ha=a("sentence1"),$a=a("\u548C"),at=n("code"),ba=a("sentence2"),ka=a("\u5217\uFF09\u3002"),va=c(),T=n("li"),ja=a("\u5C06\u5217\u540D"),lt=n("code"),Ea=a("label"),wa=a("\u91CD\u547D\u540D\u4E3A"),rt=n("code"),ga=a("labels"),za=a("\uFF08\u56E0\u4E3A\u6A21\u578B\u671F\u671B\u53C2\u6570\u662F"),ot=n("code"),qa=a("labels"),ya=a("\uFF09\u3002"),Ca=c(),nt=n("li"),Aa=a("\u8BBE\u7F6E\u6570\u636E\u96C6\u7684\u683C\u5F0F\uFF0C\u4F7F\u5176\u8FD4\u56DE PyTorch \u5F20\u91CF\u800C\u4E0D\u662F\u5217\u8868\u3002"),Xt=c(),B=n("p"),Da=a("\u9488\u5BF9\u4E0A\u9762\u7684\u6BCF\u4E2A\u6B65\u9AA4\uFF0C\u6211\u4EEC\u7684 "),pt=n("code"),Pa=a("tokenized_datasets"),Oa=a(" \u90FD\u6709\u4E00\u4E2A\u65B9\u6CD5:"),Zt=c(),m(ne.$$.fragment),es=c(),Me=n("p"),Sa=a("\u7136\u540E\uFF0C\u6211\u4EEC\u53EF\u4EE5\u68C0\u67E5\u7ED3\u679C\u4E2D\u662F\u5426\u53EA\u6709\u6A21\u578B\u80FD\u591F\u63A5\u53D7\u7684\u5217:"),ts=c(),m(pe.$$.fragment),ss=c(),Le=n("p"),xa=a("\u81F3\u6B64\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8F7B\u677E\u5B9A\u4E49\u6570\u636E\u52A0\u8F7D\u5668:"),as=c(),m(ie.$$.fragment),ls=c(),Fe=n("p"),Ta=a("\u4E3A\u4E86\u5FEB\u901F\u68C0\u9A8C\u6570\u636E\u5904\u7406\u4E2D\u6CA1\u6709\u9519\u8BEF\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8FD9\u6837\u68C0\u9A8C\u5176\u4E2D\u7684\u4E00\u4E2A\u6279\u6B21:"),rs=c(),m(ce.$$.fragment),os=c(),m(de.$$.fragment),ns=c(),L=n("p"),Ua=a("\u8BF7\u6CE8\u610F\uFF0C\u5B9E\u9645\u7684\u5F62\u72B6\u53EF\u80FD\u4E0E\u60A8\u7565\u6709\u4E0D\u540C\uFF0C\u56E0\u4E3A\u6211\u4EEC\u4E3A\u8BAD\u7EC3\u6570\u636E\u52A0\u8F7D\u5668\u8BBE\u7F6E\u4E86"),it=n("code"),Wa=a("shuffle=True"),Ma=a("\uFF0C\u5E76\u4E14\u6A21\u578B\u4F1A\u5C06\u53E5\u5B50\u586B\u5145\u5230"),ct=n("code"),La=a("batch"),Fa=a("\u4E2D\u7684\u6700\u5927\u957F\u5EA6\u3002"),ps=c(),Ge=n("p"),Ga=a("\u73B0\u5728\u6211\u4EEC\u5DF2\u7ECF\u5B8C\u5168\u5B8C\u6210\u4E86\u6570\u636E\u9884\u5904\u7406\uFF08\u5BF9\u4E8E\u4EFB\u4F55 ML \u4ECE\u4E1A\u8005\u6765\u8BF4\u90FD\u662F\u4E00\u4E2A\u4EE4\u4EBA\u6EE1\u610F\u4F46\u96BE\u4EE5\u5B9E\u73B0\u7684\u76EE\u6807\uFF09\uFF0C\u8BA9\u6211\u4EEC\u5C06\u6CE8\u610F\u529B\u8F6C\u5411\u6A21\u578B\u3002\u6211\u4EEC\u5B8C\u5168\u50CF\u5728\u4E0A\u4E00\u8282\u4E2D\u6240\u505A\u7684\u90A3\u6837\u5B9E\u4F8B\u5316\u5B83:"),is=c(),m(me.$$.fragment),cs=c(),J=n("p"),Na=a("\u4E3A\u4E86\u786E\u4FDD\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u4E00\u5207\u987A\u5229\uFF0C\u6211\u4EEC\u5C06"),dt=n("code"),Ha=a("batch"),Ia=a("\u4F20\u9012\u7ED9\u8FD9\u4E2A\u6A21\u578B:"),ds=c(),m(fe.$$.fragment),ms=c(),m(ue.$$.fragment),fs=c(),j=n("p"),Ra=a("\u5F53\u6211\u4EEC\u63D0\u4F9B "),mt=n("code"),Ba=a("labels"),Ja=a(" \u65F6\uFF0C \u{1F917} Transformers \u6A21\u578B\u90FD\u5C06\u8FD4\u56DE\u8FD9\u4E2A"),ft=n("code"),Ya=a("batch"),Qa=a("\u7684"),ut=n("code"),Ka=a("loss"),Va=a("\uFF0C\u6211\u4EEC\u8FD8\u5F97\u5230\u4E86 "),_t=n("code"),Xa=a("logits"),Za=a("("),ht=n("code"),el=a("batch"),tl=a("\u4E2D\u7684\u6BCF\u4E2A\u8F93\u5165\u6709\u4E24\u4E2A\uFF0C\u6240\u4EE5\u5F20\u91CF\u5927\u5C0F\u4E3A 8 x 2)\u3002"),us=c(),E=n("p"),sl=a("\u6211\u4EEC\u51E0\u4E4E\u51C6\u5907\u597D\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E86\uFF01\u6211\u4EEC\u53EA\u662F\u7F3A\u5C11\u4E24\u4EF6\u4E8B\uFF1A\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002\u7531\u4E8E\u6211\u4EEC\u8BD5\u56FE\u81EA\u884C\u5B9E\u73B0 "),$t=n("code"),al=a("Trainer"),ll=a("\u7684\u529F\u80FD\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528\u76F8\u540C\u7684\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002"),bt=n("code"),rl=a("Trainer"),ol=a(" \u4F7F\u7528\u7684\u4F18\u5316\u5668\u662F "),kt=n("code"),nl=a("AdamW"),pl=a(" , \u4E0E "),vt=n("code"),il=a("Adam"),cl=a(" \u76F8\u540C\uFF0C\u4F46\u5728\u6743\u91CD\u8870\u51CF\u6B63\u5219\u5316\u65B9\u9762\u6709\u6240\u4E0D\u540C(\u53C2\u89C1"),_e=n("a"),dl=a("\u201CDecoupled Weight Decay Regularization\u201D"),ml=a("\u4F5C\u8005:Ilya Loshchilov \u548C Frank Hutter):"),_s=c(),m(he.$$.fragment),hs=c(),F=n("p"),fl=a("\u6700\u540E\uFF0C\u9ED8\u8BA4\u4F7F\u7528\u7684\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u53EA\u662F\u4ECE\u6700\u5927\u503C (5e-5) \u5230 0 \u7684\u7EBF\u6027\u8870\u51CF\u3002 \u4E3A\u4E86\u5B9A\u4E49\u5B83\uFF0C\u6211\u4EEC\u9700\u8981\u77E5\u9053\u6211\u4EEC\u8BAD\u7EC3\u7684\u6B21\u6570\uFF0C\u5373\u6240\u6709\u6570\u636E\u8BAD\u7EC3\u7684\u6B21\u6570(epochs)\u4E58\u4EE5\u7684\u6570\u636E\u91CF\uFF08\u8FD9\u662F\u6211\u4EEC\u6240\u6709\u8BAD\u7EC3\u6570\u636E\u7684\u6570\u91CF\uFF09\u3002"),jt=n("code"),ul=a("Trainer"),_l=a("\u9ED8\u8BA4\u60C5\u51B5\u4E0B\u4F7F\u7528\u4E09\u4E2A"),Et=n("code"),hl=a("epochs"),$l=a("\uFF0C\u56E0\u6B64\u6211\u4EEC\u5B9A\u4E49\u8BAD\u7EC3\u8FC7\u7A0B\u5982\u4E0B:"),$s=c(),m($e.$$.fragment),bs=c(),m(be.$$.fragment),ks=c(),ke=n("h3"),bl=a("\u8BAD\u7EC3\u5FAA\u73AF"),vs=c(),D=n("p"),kl=a("\u6700\u540E\u4E00\u4EF6\u4E8B\uFF1A\u5982\u679C\u6211\u4EEC\u53EF\u4EE5\u8BBF\u95EE GPU,\u6211\u4EEC\u5C06\u5E0C\u671B\u4F7F\u7528 GPU(\u5728 CPU \u4E0A\uFF0C\u8BAD\u7EC3\u53EF\u80FD\u9700\u8981\u51E0\u4E2A\u5C0F\u65F6\u800C\u4E0D\u662F\u51E0\u5206\u949F)\u3002\u4E3A\u6B64\uFF0C\u6211\u4EEC\u5B9A\u4E49\u4E86\u4E00\u4E2A "),wt=n("code"),vl=a("device"),jl=a(",\u5B83\u5728GPU\u53EF\u7528\u7684\u60C5\u51B5\u4E0B\u6307\u5411GPU \u6211\u4EEC\u5C06\u628A\u6211\u4EEC\u7684\u6A21\u578B\u548C"),gt=n("code"),El=a("batche"),wl=a("\u653E\u5728"),zt=n("code"),gl=a("device"),zl=a("\u4E0A:"),js=c(),m(ve.$$.fragment),Es=c(),m(je.$$.fragment),ws=c(),Y=n("p"),ql=a("\u6211\u4EEC\u73B0\u5728\u51C6\u5907\u597D\u8BAD\u7EC3\u4E86\uFF01\u4E3A\u4E86\u4E86\u89E3\u8BAD\u7EC3\u4F55\u65F6\u7ED3\u675F\uFF0C\u6211\u4EEC\u4F7F\u7528 "),qt=n("code"),yl=a("tqdm"),Cl=a(" \u5E93,\u5728\u8BAD\u7EC3\u6B65\u9AA4\u6570\u4E0A\u6DFB\u52A0\u4E86\u4E00\u4E2A\u8FDB\u5EA6\u6761:"),gs=c(),m(Ee.$$.fragment),zs=c(),Ne=n("p"),Al=a("\u60A8\u53EF\u4EE5\u770B\u5230\u8BAD\u7EC3\u5FAA\u73AF\u7684\u6838\u5FC3\u4E0E\u4ECB\u7ECD\u4E2D\u7684\u975E\u5E38\u76F8\u4F3C\u3002\u6211\u4EEC\u6CA1\u6709\u8981\u6C42\u4EFB\u4F55\u68C0\u9A8C\uFF0C\u6240\u4EE5\u8FD9\u4E2A\u8BAD\u7EC3\u5FAA\u73AF\u4E0D\u4F1A\u544A\u8BC9\u6211\u4EEC\u4EFB\u4F55\u5173\u4E8E\u6A21\u578B\u76EE\u524D\u7684\u72B6\u6001\u3002\u6211\u4EEC\u9700\u8981\u4E3A\u6B64\u6DFB\u52A0\u4E00\u4E2A\u8BC4\u4F30\u5FAA\u73AF\u3002"),qs=c(),we=n("h3"),Dl=a("\u8BC4\u4F30\u5FAA\u73AF"),ys=c(),w=n("p"),Pl=a("\u6B63\u5982\u6211\u4EEC\u4E4B\u524D\u6240\u505A\u7684\u90A3\u6837\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528 \u{1F917} Datasets \u5E93\u63D0\u4F9B\u7684\u6307\u6807\u3002\u6211\u4EEC\u5DF2\u7ECF\u4E86\u89E3\u4E86 "),yt=n("code"),Ol=a("metric.compute()"),Sl=a(" \u65B9\u6CD5\uFF0C\u5F53\u6211\u4EEC\u4F7F\u7528 "),Ct=n("code"),xl=a("add_batch()"),Tl=a("\u65B9\u6CD5\u8FDB\u884C\u9884\u6D4B\u5FAA\u73AF\u65F6\uFF0C\u5B9E\u9645\u4E0A\u8BE5\u6307\u6807\u53EF\u4EE5\u4E3A\u6211\u4EEC\u7D2F\u79EF\u6240\u6709 "),At=n("code"),Ul=a("batch"),Wl=a(" \u7684\u7ED3\u679C\u3002\u4E00\u65E6\u6211\u4EEC\u7D2F\u79EF\u4E86\u6240\u6709 "),Dt=n("code"),Ml=a("batch"),Ll=a(" \uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u4F7F\u7528 "),Pt=n("code"),Fl=a("metric.compute()"),Gl=a(" \u5F97\u5230\u6700\u7EC8\u7ED3\u679C .\u4EE5\u4E0B\u662F\u5728\u8BC4\u4F30\u5FAA\u73AF\u4E2D\u5B9E\u73B0\u6240\u6709\u8FD9\u4E9B\u7684\u65B9\u6CD5:"),Cs=c(),m(ge.$$.fragment),As=c(),m(ze.$$.fragment),Ds=c(),He=n("p"),Nl=a("\u540C\u6837\uFF0C\u7531\u4E8E\u6A21\u578B\u5934\u90E8\u521D\u59CB\u5316\u548C\u6570\u636E\u6539\u7EC4\u7684\u968F\u673A\u6027\uFF0C\u60A8\u7684\u7ED3\u679C\u4F1A\u7565\u6709\u4E0D\u540C\uFF0C\u4F46\u5B83\u4EEC\u5E94\u8BE5\u5728\u540C\u4E00\u4E2A\u8303\u56F4\u5185\u3002"),Ps=c(),m(Q.$$.fragment),Os=c(),H=n("h3"),K=n("a"),Ot=n("span"),m(qe.$$.fragment),Hl=c(),St=n("span"),Il=a("S\u4F7F\u7528\u{1F917} Accelerate\u52A0\u901F\u60A8\u7684\u8BAD\u7EC3\u5FAA\u73AF"),Ss=c(),m(ye.$$.fragment),xs=c(),V=n("p"),Rl=a("\u6211\u4EEC\u4E4B\u524D\u5B9A\u4E49\u7684\u8BAD\u7EC3\u5FAA\u73AF\u5728\u5355\u4E2A CPU \u6216 GPU \u4E0A\u8FD0\u884C\u826F\u597D\u3002\u4F46\u662F\u4F7F\u7528"),Ce=n("a"),Bl=a("\u{1F917} Accelerate"),Jl=a("\u5E93\uFF0C\u53EA\u9700\u8FDB\u884C\u4E00\u4E9B\u8C03\u6574\uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u5728\u591A\u4E2A GPU \u6216 TPU \u4E0A\u542F\u7528\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u4ECE\u521B\u5EFA\u8BAD\u7EC3\u548C\u9A8C\u8BC1\u6570\u636E\u52A0\u8F7D\u5668\u5F00\u59CB\uFF0C\u6211\u4EEC\u7684\u624B\u52A8\u8BAD\u7EC3\u5FAA\u73AF\u5982\u4E0B\u6240\u793A\uFF1A"),Ts=c(),m(Ae.$$.fragment),Us=c(),Ie=n("p"),Yl=a("\u4EE5\u4E0B\u662F\u53D8\u5316:"),Ws=c(),m(De.$$.fragment),Ms=c(),y=n("p"),Ql=a("\u8981\u6DFB\u52A0\u7684\u7B2C\u4E00\u884C\u662F\u5BFC\u5165"),xt=n("code"),Kl=a("Accelerator"),Vl=a("\u3002\u7B2C\u4E8C\u884C\u5B9E\u4F8B\u5316\u4E00\u4E2A "),Tt=n("code"),Xl=a("Accelerator"),Zl=a("\u5BF9\u8C61 \uFF0C\u5B83\u5C06\u67E5\u770B\u73AF\u5883\u5E76\u521D\u59CB\u5316\u9002\u5F53\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u3002 \u{1F917} Accelerate \u4E3A\u60A8\u5904\u7406\u6570\u636E\u5728\u8BBE\u5907\u95F4\u7684\u4F20\u9012\uFF0C\u56E0\u6B64\u60A8\u53EF\u4EE5\u5220\u9664\u5C06\u6A21\u578B\u653E\u5728\u8BBE\u5907\u4E0A\u7684\u90A3\u884C\u4EE3\u7801\uFF08\u6216\u8005\uFF0C\u5982\u679C\u60A8\u613F\u610F\uFF0C\u53EF\u4F7F\u7528 "),Ut=n("code"),er=a("accelerator.device"),tr=a(" \u4EE3\u66FF "),Wt=n("code"),sr=a("device"),ar=a(" \uFF09\u3002"),Ls=c(),v=n("p"),lr=a("\u7136\u540E\u5927\u90E8\u5206\u5DE5\u4F5C\u4F1A\u5728\u5C06\u6570\u636E\u52A0\u8F7D\u5668\u3001\u6A21\u578B\u548C\u4F18\u5316\u5668\u53D1\u9001\u5230\u7684"),Mt=n("code"),rr=a("accelerator.prepare()"),or=a("\u4E2D\u5B8C\u6210\u3002\u8FD9\u5C06\u4F1A\u628A\u8FD9\u4E9B\u5BF9\u8C61\u5305\u88C5\u5728\u9002\u5F53\u7684\u5BB9\u5668\u4E2D\uFF0C\u4EE5\u786E\u4FDD\u60A8\u7684\u5206\u5E03\u5F0F\u8BAD\u7EC3\u6309\u9884\u671F\u5DE5\u4F5C\u3002\u8981\u8FDB\u884C\u7684\u5176\u4F59\u66F4\u6539\u662F\u5220\u9664\u5C06"),Lt=n("code"),nr=a("batch"),pr=a("\u653E\u5728 "),Ft=n("code"),ir=a("device"),cr=a(" \u7684\u90A3\u884C\u4EE3\u7801\uFF08\u540C\u6837\uFF0C\u5982\u679C\u60A8\u60F3\u4FDD\u7559\u5B83\uFF0C\u60A8\u53EF\u4EE5\u5C06\u5176\u66F4\u6539\u4E3A\u4F7F\u7528 "),Gt=n("code"),dr=a("accelerator.device"),mr=a(" ) \u5E76\u5C06 "),Nt=n("code"),fr=a("loss.backward()"),ur=a(" \u66FF\u6362\u4E3A"),Ht=n("code"),_r=a("accelerator.backward(loss)"),hr=a("\u3002"),Fs=c(),m(X.$$.fragment),Gs=c(),Re=n("p"),$r=a("\u5982\u679C\u60A8\u60F3\u590D\u5236\u5E76\u7C98\u8D34\u6765\u76F4\u63A5\u8FD0\u884C\uFF0C\u4EE5\u4E0B\u662F \u{1F917} Accelerate \u7684\u5B8C\u6574\u8BAD\u7EC3\u5FAA\u73AF:"),Ns=c(),m(Pe.$$.fragment),Hs=c(),Z=n("p"),br=a("\u628A\u8FD9\u4E2A\u653E\u5728 "),It=n("code"),kr=a("train.py"),vr=a(" \u6587\u4EF6\u4E2D\uFF0C\u53EF\u4EE5\u8BA9\u5B83\u5728\u4EFB\u4F55\u7C7B\u578B\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E0A\u8FD0\u884C\u3002\u8981\u5728\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E2D\u8BD5\u7528\u5B83\uFF0C\u8BF7\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4:"),Is=c(),m(Oe.$$.fragment),Rs=c(),Be=n("p"),jr=a("\u8FD9\u5C06\u8BE2\u95EE\u60A8\u51E0\u4E2A\u914D\u7F6E\u7684\u95EE\u9898\u5E76\u5C06\u60A8\u7684\u56DE\u7B54\u8F6C\u50A8\u5230\u6B64\u547D\u4EE4\u4F7F\u7528\u7684\u914D\u7F6E\u6587\u4EF6\u4E2D:"),Bs=c(),m(Se.$$.fragment),Js=c(),Je=n("p"),Er=a("\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3"),Ys=c(),ee=n("p"),wr=a("\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u5982\u679C\u60A8\u60F3\u5728 Notebook \u4E2D\u5C1D\u8BD5\u6B64\u64CD\u4F5C\uFF08\u4F8B\u5982\uFF0C\u5728 Colab \u4E0A\u4F7F\u7528 TPU \u8FDB\u884C\u6D4B\u8BD5\uFF09\uFF0C\u53EA\u9700\u5C06\u4EE3\u7801\u7C98\u8D34\u5230 "),Rt=n("code"),gr=a("training_function()"),zr=a(" \u5E76\u4F7F\u7528\u4EE5\u4E0B\u547D\u4EE4\u8FD0\u884C\u6700\u540E\u4E00\u4E2A\u5355\u5143\u683C:"),Qs=c(),m(xe.$$.fragment),Ks=c(),te=n("p"),qr=a("\u60A8\u53EF\u4EE5\u5728"),Te=n("a"),yr=a("\u{1F917} Accelerate repo"),Cr=a("\u627E\u5230\u66F4\u591A\u7684\u793A\u4F8B\u3002"),this.h()},l(e){const r=Ro('[data-svelte="svelte-1phssyn"]',document.head);b=p(r,"META",{name:!0,content:!0}),r.forEach(s),q=d(e),z=p(e,"H1",{id:!0});var Bt=i(z);le=l(Bt,"\u4E00\u4E2A\u5B8C\u6574\u7684\u8BAD\u7EC3"),Bt.forEach(s),I=d(e),f(A.$$.fragment,e),x=d(e),f(U.$$.fragment,e),Jt=d(e),R=p(e,"P",{});var Ue=i(R);oa=l(Ue,"\u73B0\u5728\uFF0C\u6211\u4EEC\u5C06\u4E86\u89E3\u5982\u4F55\u5728\u4E0D\u4F7F\u7528"),Ze=p(Ue,"CODE",{});var Ar=i(Ze);na=l(Ar,"Trainer"),Ar.forEach(s),pa=l(Ue,"\u7C7B\u7684\u60C5\u51B5\u4E0B\u83B7\u5F97\u4E0E\u4E0A\u4E00\u8282\u76F8\u540C\u7684\u7ED3\u679C\u3002\u540C\u6837\uFF0C\u6211\u4EEC\u5047\u8BBE\u60A8\u5DF2\u7ECF\u5B66\u4E60\u4E86\u7B2C 2 \u8282\u4E2D\u7684\u6570\u636E\u5904\u7406\u3002\u4E0B\u9762\u662F\u4E00\u4E2A\u7B80\u77ED\u7684\u603B\u7ED3\uFF0C\u6DB5\u76D6\u4E86\u60A8\u9700\u8981\u7684\u6240\u6709\u5185\u5BB9:"),Ue.forEach(s),Yt=d(e),f(re.$$.fragment,e),Qt=d(e),oe=p(e,"H3",{id:!0});var Dr=i(oe);ia=l(Dr,"\u8BAD\u7EC3\u524D\u7684\u51C6\u5907"),Dr.forEach(s),Kt=d(e),W=p(e,"P",{});var Ye=i(W);ca=l(Ye,"\u5728\u5B9E\u9645\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E4B\u524D\uFF0C\u6211\u4EEC\u9700\u8981\u5B9A\u4E49\u4E00\u4E9B\u5BF9\u8C61\u3002\u7B2C\u4E00\u4E2A\u662F\u6211\u4EEC\u5C06\u7528\u4E8E\u8FED\u4EE3\u6279\u6B21\u7684\u6570\u636E\u52A0\u8F7D\u5668\u3002\u6211\u4EEC\u9700\u8981\u5BF9\u6211\u4EEC\u7684"),et=p(Ye,"CODE",{});var Pr=i(et);da=l(Pr,"tokenized_datasets"),Pr.forEach(s),ma=l(Ye,"\u505A\u4E00\u4E9B\u5904\u7406\uFF0C\u6765\u5904\u7406"),tt=p(Ye,"CODE",{});var Or=i(tt);fa=l(Or,"Trainer"),Or.forEach(s),ua=l(Ye,"\u81EA\u52A8\u4E3A\u6211\u4EEC\u505A\u7684\u4E00\u4E9B\u4E8B\u60C5\u3002\u5177\u4F53\u6765\u8BF4\uFF0C\u6211\u4EEC\u9700\u8981:"),Ye.forEach(s),Vt=d(e),M=p(e,"UL",{});var Qe=i(M);N=p(Qe,"LI",{});var Ke=i(N);_a=l(Ke,"\u5220\u9664\u4E0E\u6A21\u578B\u4E0D\u671F\u671B\u7684\u503C\u76F8\u5BF9\u5E94\u7684\u5217\uFF08\u5982"),st=p(Ke,"CODE",{});var Sr=i(st);ha=l(Sr,"sentence1"),Sr.forEach(s),$a=l(Ke,"\u548C"),at=p(Ke,"CODE",{});var xr=i(at);ba=l(xr,"sentence2"),xr.forEach(s),ka=l(Ke,"\u5217\uFF09\u3002"),Ke.forEach(s),va=d(Qe),T=p(Qe,"LI",{});var se=i(T);ja=l(se,"\u5C06\u5217\u540D"),lt=p(se,"CODE",{});var Tr=i(lt);Ea=l(Tr,"label"),Tr.forEach(s),wa=l(se,"\u91CD\u547D\u540D\u4E3A"),rt=p(se,"CODE",{});var Ur=i(rt);ga=l(Ur,"labels"),Ur.forEach(s),za=l(se,"\uFF08\u56E0\u4E3A\u6A21\u578B\u671F\u671B\u53C2\u6570\u662F"),ot=p(se,"CODE",{});var Wr=i(ot);qa=l(Wr,"labels"),Wr.forEach(s),ya=l(se,"\uFF09\u3002"),se.forEach(s),Ca=d(Qe),nt=p(Qe,"LI",{});var Mr=i(nt);Aa=l(Mr,"\u8BBE\u7F6E\u6570\u636E\u96C6\u7684\u683C\u5F0F\uFF0C\u4F7F\u5176\u8FD4\u56DE PyTorch \u5F20\u91CF\u800C\u4E0D\u662F\u5217\u8868\u3002"),Mr.forEach(s),Qe.forEach(s),Xt=d(e),B=p(e,"P",{});var Xs=i(B);Da=l(Xs,"\u9488\u5BF9\u4E0A\u9762\u7684\u6BCF\u4E2A\u6B65\u9AA4\uFF0C\u6211\u4EEC\u7684 "),pt=p(Xs,"CODE",{});var Lr=i(pt);Pa=l(Lr,"tokenized_datasets"),Lr.forEach(s),Oa=l(Xs," \u90FD\u6709\u4E00\u4E2A\u65B9\u6CD5:"),Xs.forEach(s),Zt=d(e),f(ne.$$.fragment,e),es=d(e),Me=p(e,"P",{});var Fr=i(Me);Sa=l(Fr,"\u7136\u540E\uFF0C\u6211\u4EEC\u53EF\u4EE5\u68C0\u67E5\u7ED3\u679C\u4E2D\u662F\u5426\u53EA\u6709\u6A21\u578B\u80FD\u591F\u63A5\u53D7\u7684\u5217:"),Fr.forEach(s),ts=d(e),f(pe.$$.fragment,e),ss=d(e),Le=p(e,"P",{});var Gr=i(Le);xa=l(Gr,"\u81F3\u6B64\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8F7B\u677E\u5B9A\u4E49\u6570\u636E\u52A0\u8F7D\u5668:"),Gr.forEach(s),as=d(e),f(ie.$$.fragment,e),ls=d(e),Fe=p(e,"P",{});var Nr=i(Fe);Ta=l(Nr,"\u4E3A\u4E86\u5FEB\u901F\u68C0\u9A8C\u6570\u636E\u5904\u7406\u4E2D\u6CA1\u6709\u9519\u8BEF\uFF0C\u6211\u4EEC\u53EF\u4EE5\u8FD9\u6837\u68C0\u9A8C\u5176\u4E2D\u7684\u4E00\u4E2A\u6279\u6B21:"),Nr.forEach(s),rs=d(e),f(ce.$$.fragment,e),os=d(e),f(de.$$.fragment,e),ns=d(e),L=p(e,"P",{});var Ve=i(L);Ua=l(Ve,"\u8BF7\u6CE8\u610F\uFF0C\u5B9E\u9645\u7684\u5F62\u72B6\u53EF\u80FD\u4E0E\u60A8\u7565\u6709\u4E0D\u540C\uFF0C\u56E0\u4E3A\u6211\u4EEC\u4E3A\u8BAD\u7EC3\u6570\u636E\u52A0\u8F7D\u5668\u8BBE\u7F6E\u4E86"),it=p(Ve,"CODE",{});var Hr=i(it);Wa=l(Hr,"shuffle=True"),Hr.forEach(s),Ma=l(Ve,"\uFF0C\u5E76\u4E14\u6A21\u578B\u4F1A\u5C06\u53E5\u5B50\u586B\u5145\u5230"),ct=p(Ve,"CODE",{});var Ir=i(ct);La=l(Ir,"batch"),Ir.forEach(s),Fa=l(Ve,"\u4E2D\u7684\u6700\u5927\u957F\u5EA6\u3002"),Ve.forEach(s),ps=d(e),Ge=p(e,"P",{});var Rr=i(Ge);Ga=l(Rr,"\u73B0\u5728\u6211\u4EEC\u5DF2\u7ECF\u5B8C\u5168\u5B8C\u6210\u4E86\u6570\u636E\u9884\u5904\u7406\uFF08\u5BF9\u4E8E\u4EFB\u4F55 ML \u4ECE\u4E1A\u8005\u6765\u8BF4\u90FD\u662F\u4E00\u4E2A\u4EE4\u4EBA\u6EE1\u610F\u4F46\u96BE\u4EE5\u5B9E\u73B0\u7684\u76EE\u6807\uFF09\uFF0C\u8BA9\u6211\u4EEC\u5C06\u6CE8\u610F\u529B\u8F6C\u5411\u6A21\u578B\u3002\u6211\u4EEC\u5B8C\u5168\u50CF\u5728\u4E0A\u4E00\u8282\u4E2D\u6240\u505A\u7684\u90A3\u6837\u5B9E\u4F8B\u5316\u5B83:"),Rr.forEach(s),is=d(e),f(me.$$.fragment,e),cs=d(e),J=p(e,"P",{});var Zs=i(J);Na=l(Zs,"\u4E3A\u4E86\u786E\u4FDD\u8BAD\u7EC3\u8FC7\u7A0B\u4E2D\u4E00\u5207\u987A\u5229\uFF0C\u6211\u4EEC\u5C06"),dt=p(Zs,"CODE",{});var Br=i(dt);Ha=l(Br,"batch"),Br.forEach(s),Ia=l(Zs,"\u4F20\u9012\u7ED9\u8FD9\u4E2A\u6A21\u578B:"),Zs.forEach(s),ds=d(e),f(fe.$$.fragment,e),ms=d(e),f(ue.$$.fragment,e),fs=d(e),j=p(e,"P",{});var P=i(j);Ra=l(P,"\u5F53\u6211\u4EEC\u63D0\u4F9B "),mt=p(P,"CODE",{});var Jr=i(mt);Ba=l(Jr,"labels"),Jr.forEach(s),Ja=l(P," \u65F6\uFF0C \u{1F917} Transformers \u6A21\u578B\u90FD\u5C06\u8FD4\u56DE\u8FD9\u4E2A"),ft=p(P,"CODE",{});var Yr=i(ft);Ya=l(Yr,"batch"),Yr.forEach(s),Qa=l(P,"\u7684"),ut=p(P,"CODE",{});var Qr=i(ut);Ka=l(Qr,"loss"),Qr.forEach(s),Va=l(P,"\uFF0C\u6211\u4EEC\u8FD8\u5F97\u5230\u4E86 "),_t=p(P,"CODE",{});var Kr=i(_t);Xa=l(Kr,"logits"),Kr.forEach(s),Za=l(P,"("),ht=p(P,"CODE",{});var Vr=i(ht);el=l(Vr,"batch"),Vr.forEach(s),tl=l(P,"\u4E2D\u7684\u6BCF\u4E2A\u8F93\u5165\u6709\u4E24\u4E2A\uFF0C\u6240\u4EE5\u5F20\u91CF\u5927\u5C0F\u4E3A 8 x 2)\u3002"),P.forEach(s),us=d(e),E=p(e,"P",{});var O=i(E);sl=l(O,"\u6211\u4EEC\u51E0\u4E4E\u51C6\u5907\u597D\u7F16\u5199\u6211\u4EEC\u7684\u8BAD\u7EC3\u5FAA\u73AF\u4E86\uFF01\u6211\u4EEC\u53EA\u662F\u7F3A\u5C11\u4E24\u4EF6\u4E8B\uFF1A\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002\u7531\u4E8E\u6211\u4EEC\u8BD5\u56FE\u81EA\u884C\u5B9E\u73B0 "),$t=p(O,"CODE",{});var Xr=i($t);al=l(Xr,"Trainer"),Xr.forEach(s),ll=l(O,"\u7684\u529F\u80FD\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528\u76F8\u540C\u7684\u4F18\u5316\u5668\u548C\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u3002"),bt=p(O,"CODE",{});var Zr=i(bt);rl=l(Zr,"Trainer"),Zr.forEach(s),ol=l(O," \u4F7F\u7528\u7684\u4F18\u5316\u5668\u662F "),kt=p(O,"CODE",{});var eo=i(kt);nl=l(eo,"AdamW"),eo.forEach(s),pl=l(O," , \u4E0E "),vt=p(O,"CODE",{});var to=i(vt);il=l(to,"Adam"),to.forEach(s),cl=l(O," \u76F8\u540C\uFF0C\u4F46\u5728\u6743\u91CD\u8870\u51CF\u6B63\u5219\u5316\u65B9\u9762\u6709\u6240\u4E0D\u540C(\u53C2\u89C1"),_e=p(O,"A",{href:!0,rel:!0});var so=i(_e);dl=l(so,"\u201CDecoupled Weight Decay Regularization\u201D"),so.forEach(s),ml=l(O,"\u4F5C\u8005:Ilya Loshchilov \u548C Frank Hutter):"),O.forEach(s),_s=d(e),f(he.$$.fragment,e),hs=d(e),F=p(e,"P",{});var Xe=i(F);fl=l(Xe,"\u6700\u540E\uFF0C\u9ED8\u8BA4\u4F7F\u7528\u7684\u5B66\u4E60\u7387\u8C03\u5EA6\u5668\u53EA\u662F\u4ECE\u6700\u5927\u503C (5e-5) \u5230 0 \u7684\u7EBF\u6027\u8870\u51CF\u3002 \u4E3A\u4E86\u5B9A\u4E49\u5B83\uFF0C\u6211\u4EEC\u9700\u8981\u77E5\u9053\u6211\u4EEC\u8BAD\u7EC3\u7684\u6B21\u6570\uFF0C\u5373\u6240\u6709\u6570\u636E\u8BAD\u7EC3\u7684\u6B21\u6570(epochs)\u4E58\u4EE5\u7684\u6570\u636E\u91CF\uFF08\u8FD9\u662F\u6211\u4EEC\u6240\u6709\u8BAD\u7EC3\u6570\u636E\u7684\u6570\u91CF\uFF09\u3002"),jt=p(Xe,"CODE",{});var ao=i(jt);ul=l(ao,"Trainer"),ao.forEach(s),_l=l(Xe,"\u9ED8\u8BA4\u60C5\u51B5\u4E0B\u4F7F\u7528\u4E09\u4E2A"),Et=p(Xe,"CODE",{});var lo=i(Et);hl=l(lo,"epochs"),lo.forEach(s),$l=l(Xe,"\uFF0C\u56E0\u6B64\u6211\u4EEC\u5B9A\u4E49\u8BAD\u7EC3\u8FC7\u7A0B\u5982\u4E0B:"),Xe.forEach(s),$s=d(e),f($e.$$.fragment,e),bs=d(e),f(be.$$.fragment,e),ks=d(e),ke=p(e,"H3",{id:!0});var ro=i(ke);bl=l(ro,"\u8BAD\u7EC3\u5FAA\u73AF"),ro.forEach(s),vs=d(e),D=p(e,"P",{});var ae=i(D);kl=l(ae,"\u6700\u540E\u4E00\u4EF6\u4E8B\uFF1A\u5982\u679C\u6211\u4EEC\u53EF\u4EE5\u8BBF\u95EE GPU,\u6211\u4EEC\u5C06\u5E0C\u671B\u4F7F\u7528 GPU(\u5728 CPU \u4E0A\uFF0C\u8BAD\u7EC3\u53EF\u80FD\u9700\u8981\u51E0\u4E2A\u5C0F\u65F6\u800C\u4E0D\u662F\u51E0\u5206\u949F)\u3002\u4E3A\u6B64\uFF0C\u6211\u4EEC\u5B9A\u4E49\u4E86\u4E00\u4E2A "),wt=p(ae,"CODE",{});var oo=i(wt);vl=l(oo,"device"),oo.forEach(s),jl=l(ae,",\u5B83\u5728GPU\u53EF\u7528\u7684\u60C5\u51B5\u4E0B\u6307\u5411GPU \u6211\u4EEC\u5C06\u628A\u6211\u4EEC\u7684\u6A21\u578B\u548C"),gt=p(ae,"CODE",{});var no=i(gt);El=l(no,"batche"),no.forEach(s),wl=l(ae,"\u653E\u5728"),zt=p(ae,"CODE",{});var po=i(zt);gl=l(po,"device"),po.forEach(s),zl=l(ae,"\u4E0A:"),ae.forEach(s),js=d(e),f(ve.$$.fragment,e),Es=d(e),f(je.$$.fragment,e),ws=d(e),Y=p(e,"P",{});var ea=i(Y);ql=l(ea,"\u6211\u4EEC\u73B0\u5728\u51C6\u5907\u597D\u8BAD\u7EC3\u4E86\uFF01\u4E3A\u4E86\u4E86\u89E3\u8BAD\u7EC3\u4F55\u65F6\u7ED3\u675F\uFF0C\u6211\u4EEC\u4F7F\u7528 "),qt=p(ea,"CODE",{});var io=i(qt);yl=l(io,"tqdm"),io.forEach(s),Cl=l(ea," \u5E93,\u5728\u8BAD\u7EC3\u6B65\u9AA4\u6570\u4E0A\u6DFB\u52A0\u4E86\u4E00\u4E2A\u8FDB\u5EA6\u6761:"),ea.forEach(s),gs=d(e),f(Ee.$$.fragment,e),zs=d(e),Ne=p(e,"P",{});var co=i(Ne);Al=l(co,"\u60A8\u53EF\u4EE5\u770B\u5230\u8BAD\u7EC3\u5FAA\u73AF\u7684\u6838\u5FC3\u4E0E\u4ECB\u7ECD\u4E2D\u7684\u975E\u5E38\u76F8\u4F3C\u3002\u6211\u4EEC\u6CA1\u6709\u8981\u6C42\u4EFB\u4F55\u68C0\u9A8C\uFF0C\u6240\u4EE5\u8FD9\u4E2A\u8BAD\u7EC3\u5FAA\u73AF\u4E0D\u4F1A\u544A\u8BC9\u6211\u4EEC\u4EFB\u4F55\u5173\u4E8E\u6A21\u578B\u76EE\u524D\u7684\u72B6\u6001\u3002\u6211\u4EEC\u9700\u8981\u4E3A\u6B64\u6DFB\u52A0\u4E00\u4E2A\u8BC4\u4F30\u5FAA\u73AF\u3002"),co.forEach(s),qs=d(e),we=p(e,"H3",{id:!0});var mo=i(we);Dl=l(mo,"\u8BC4\u4F30\u5FAA\u73AF"),mo.forEach(s),ys=d(e),w=p(e,"P",{});var S=i(w);Pl=l(S,"\u6B63\u5982\u6211\u4EEC\u4E4B\u524D\u6240\u505A\u7684\u90A3\u6837\uFF0C\u6211\u4EEC\u5C06\u4F7F\u7528 \u{1F917} Datasets \u5E93\u63D0\u4F9B\u7684\u6307\u6807\u3002\u6211\u4EEC\u5DF2\u7ECF\u4E86\u89E3\u4E86 "),yt=p(S,"CODE",{});var fo=i(yt);Ol=l(fo,"metric.compute()"),fo.forEach(s),Sl=l(S," \u65B9\u6CD5\uFF0C\u5F53\u6211\u4EEC\u4F7F\u7528 "),Ct=p(S,"CODE",{});var uo=i(Ct);xl=l(uo,"add_batch()"),uo.forEach(s),Tl=l(S,"\u65B9\u6CD5\u8FDB\u884C\u9884\u6D4B\u5FAA\u73AF\u65F6\uFF0C\u5B9E\u9645\u4E0A\u8BE5\u6307\u6807\u53EF\u4EE5\u4E3A\u6211\u4EEC\u7D2F\u79EF\u6240\u6709 "),At=p(S,"CODE",{});var _o=i(At);Ul=l(_o,"batch"),_o.forEach(s),Wl=l(S," \u7684\u7ED3\u679C\u3002\u4E00\u65E6\u6211\u4EEC\u7D2F\u79EF\u4E86\u6240\u6709 "),Dt=p(S,"CODE",{});var ho=i(Dt);Ml=l(ho,"batch"),ho.forEach(s),Ll=l(S," \uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u4F7F\u7528 "),Pt=p(S,"CODE",{});var $o=i(Pt);Fl=l($o,"metric.compute()"),$o.forEach(s),Gl=l(S," \u5F97\u5230\u6700\u7EC8\u7ED3\u679C .\u4EE5\u4E0B\u662F\u5728\u8BC4\u4F30\u5FAA\u73AF\u4E2D\u5B9E\u73B0\u6240\u6709\u8FD9\u4E9B\u7684\u65B9\u6CD5:"),S.forEach(s),Cs=d(e),f(ge.$$.fragment,e),As=d(e),f(ze.$$.fragment,e),Ds=d(e),He=p(e,"P",{});var bo=i(He);Nl=l(bo,"\u540C\u6837\uFF0C\u7531\u4E8E\u6A21\u578B\u5934\u90E8\u521D\u59CB\u5316\u548C\u6570\u636E\u6539\u7EC4\u7684\u968F\u673A\u6027\uFF0C\u60A8\u7684\u7ED3\u679C\u4F1A\u7565\u6709\u4E0D\u540C\uFF0C\u4F46\u5B83\u4EEC\u5E94\u8BE5\u5728\u540C\u4E00\u4E2A\u8303\u56F4\u5185\u3002"),bo.forEach(s),Ps=d(e),f(Q.$$.fragment,e),Os=d(e),H=p(e,"H3",{class:!0});var ta=i(H);K=p(ta,"A",{id:!0,class:!0,href:!0});var ko=i(K);Ot=p(ko,"SPAN",{});var vo=i(Ot);f(qe.$$.fragment,vo),vo.forEach(s),ko.forEach(s),Hl=d(ta),St=p(ta,"SPAN",{});var jo=i(St);Il=l(jo,"S\u4F7F\u7528\u{1F917} Accelerate\u52A0\u901F\u60A8\u7684\u8BAD\u7EC3\u5FAA\u73AF"),jo.forEach(s),ta.forEach(s),Ss=d(e),f(ye.$$.fragment,e),xs=d(e),V=p(e,"P",{});var sa=i(V);Rl=l(sa,"\u6211\u4EEC\u4E4B\u524D\u5B9A\u4E49\u7684\u8BAD\u7EC3\u5FAA\u73AF\u5728\u5355\u4E2A CPU \u6216 GPU \u4E0A\u8FD0\u884C\u826F\u597D\u3002\u4F46\u662F\u4F7F\u7528"),Ce=p(sa,"A",{href:!0,rel:!0});var Eo=i(Ce);Bl=l(Eo,"\u{1F917} Accelerate"),Eo.forEach(s),Jl=l(sa,"\u5E93\uFF0C\u53EA\u9700\u8FDB\u884C\u4E00\u4E9B\u8C03\u6574\uFF0C\u6211\u4EEC\u5C31\u53EF\u4EE5\u5728\u591A\u4E2A GPU \u6216 TPU \u4E0A\u542F\u7528\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u4ECE\u521B\u5EFA\u8BAD\u7EC3\u548C\u9A8C\u8BC1\u6570\u636E\u52A0\u8F7D\u5668\u5F00\u59CB\uFF0C\u6211\u4EEC\u7684\u624B\u52A8\u8BAD\u7EC3\u5FAA\u73AF\u5982\u4E0B\u6240\u793A\uFF1A"),sa.forEach(s),Ts=d(e),f(Ae.$$.fragment,e),Us=d(e),Ie=p(e,"P",{});var wo=i(Ie);Yl=l(wo,"\u4EE5\u4E0B\u662F\u53D8\u5316:"),wo.forEach(s),Ws=d(e),f(De.$$.fragment,e),Ms=d(e),y=p(e,"P",{});var G=i(y);Ql=l(G,"\u8981\u6DFB\u52A0\u7684\u7B2C\u4E00\u884C\u662F\u5BFC\u5165"),xt=p(G,"CODE",{});var go=i(xt);Kl=l(go,"Accelerator"),go.forEach(s),Vl=l(G,"\u3002\u7B2C\u4E8C\u884C\u5B9E\u4F8B\u5316\u4E00\u4E2A "),Tt=p(G,"CODE",{});var zo=i(Tt);Xl=l(zo,"Accelerator"),zo.forEach(s),Zl=l(G,"\u5BF9\u8C61 \uFF0C\u5B83\u5C06\u67E5\u770B\u73AF\u5883\u5E76\u521D\u59CB\u5316\u9002\u5F53\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u3002 \u{1F917} Accelerate \u4E3A\u60A8\u5904\u7406\u6570\u636E\u5728\u8BBE\u5907\u95F4\u7684\u4F20\u9012\uFF0C\u56E0\u6B64\u60A8\u53EF\u4EE5\u5220\u9664\u5C06\u6A21\u578B\u653E\u5728\u8BBE\u5907\u4E0A\u7684\u90A3\u884C\u4EE3\u7801\uFF08\u6216\u8005\uFF0C\u5982\u679C\u60A8\u613F\u610F\uFF0C\u53EF\u4F7F\u7528 "),Ut=p(G,"CODE",{});var qo=i(Ut);er=l(qo,"accelerator.device"),qo.forEach(s),tr=l(G," \u4EE3\u66FF "),Wt=p(G,"CODE",{});var yo=i(Wt);sr=l(yo,"device"),yo.forEach(s),ar=l(G," \uFF09\u3002"),G.forEach(s),Ls=d(e),v=p(e,"P",{});var C=i(v);lr=l(C,"\u7136\u540E\u5927\u90E8\u5206\u5DE5\u4F5C\u4F1A\u5728\u5C06\u6570\u636E\u52A0\u8F7D\u5668\u3001\u6A21\u578B\u548C\u4F18\u5316\u5668\u53D1\u9001\u5230\u7684"),Mt=p(C,"CODE",{});var Co=i(Mt);rr=l(Co,"accelerator.prepare()"),Co.forEach(s),or=l(C,"\u4E2D\u5B8C\u6210\u3002\u8FD9\u5C06\u4F1A\u628A\u8FD9\u4E9B\u5BF9\u8C61\u5305\u88C5\u5728\u9002\u5F53\u7684\u5BB9\u5668\u4E2D\uFF0C\u4EE5\u786E\u4FDD\u60A8\u7684\u5206\u5E03\u5F0F\u8BAD\u7EC3\u6309\u9884\u671F\u5DE5\u4F5C\u3002\u8981\u8FDB\u884C\u7684\u5176\u4F59\u66F4\u6539\u662F\u5220\u9664\u5C06"),Lt=p(C,"CODE",{});var Ao=i(Lt);nr=l(Ao,"batch"),Ao.forEach(s),pr=l(C,"\u653E\u5728 "),Ft=p(C,"CODE",{});var Do=i(Ft);ir=l(Do,"device"),Do.forEach(s),cr=l(C," \u7684\u90A3\u884C\u4EE3\u7801\uFF08\u540C\u6837\uFF0C\u5982\u679C\u60A8\u60F3\u4FDD\u7559\u5B83\uFF0C\u60A8\u53EF\u4EE5\u5C06\u5176\u66F4\u6539\u4E3A\u4F7F\u7528 "),Gt=p(C,"CODE",{});var Po=i(Gt);dr=l(Po,"accelerator.device"),Po.forEach(s),mr=l(C," ) \u5E76\u5C06 "),Nt=p(C,"CODE",{});var Oo=i(Nt);fr=l(Oo,"loss.backward()"),Oo.forEach(s),ur=l(C," \u66FF\u6362\u4E3A"),Ht=p(C,"CODE",{});var So=i(Ht);_r=l(So,"accelerator.backward(loss)"),So.forEach(s),hr=l(C,"\u3002"),C.forEach(s),Fs=d(e),f(X.$$.fragment,e),Gs=d(e),Re=p(e,"P",{});var xo=i(Re);$r=l(xo,"\u5982\u679C\u60A8\u60F3\u590D\u5236\u5E76\u7C98\u8D34\u6765\u76F4\u63A5\u8FD0\u884C\uFF0C\u4EE5\u4E0B\u662F \u{1F917} Accelerate \u7684\u5B8C\u6574\u8BAD\u7EC3\u5FAA\u73AF:"),xo.forEach(s),Ns=d(e),f(Pe.$$.fragment,e),Hs=d(e),Z=p(e,"P",{});var aa=i(Z);br=l(aa,"\u628A\u8FD9\u4E2A\u653E\u5728 "),It=p(aa,"CODE",{});var To=i(It);kr=l(To,"train.py"),To.forEach(s),vr=l(aa," \u6587\u4EF6\u4E2D\uFF0C\u53EF\u4EE5\u8BA9\u5B83\u5728\u4EFB\u4F55\u7C7B\u578B\u7684\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E0A\u8FD0\u884C\u3002\u8981\u5728\u5206\u5E03\u5F0F\u8BBE\u7F6E\u4E2D\u8BD5\u7528\u5B83\uFF0C\u8BF7\u8FD0\u884C\u4EE5\u4E0B\u547D\u4EE4:"),aa.forEach(s),Is=d(e),f(Oe.$$.fragment,e),Rs=d(e),Be=p(e,"P",{});var Uo=i(Be);jr=l(Uo,"\u8FD9\u5C06\u8BE2\u95EE\u60A8\u51E0\u4E2A\u914D\u7F6E\u7684\u95EE\u9898\u5E76\u5C06\u60A8\u7684\u56DE\u7B54\u8F6C\u50A8\u5230\u6B64\u547D\u4EE4\u4F7F\u7528\u7684\u914D\u7F6E\u6587\u4EF6\u4E2D:"),Uo.forEach(s),Bs=d(e),f(Se.$$.fragment,e),Js=d(e),Je=p(e,"P",{});var Wo=i(Je);Er=l(Wo,"\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3"),Wo.forEach(s),Ys=d(e),ee=p(e,"P",{});var la=i(ee);wr=l(la,"\u8FD9\u5C06\u542F\u52A8\u5206\u5E03\u5F0F\u8BAD\u7EC3\u3002\u5982\u679C\u60A8\u60F3\u5728 Notebook \u4E2D\u5C1D\u8BD5\u6B64\u64CD\u4F5C\uFF08\u4F8B\u5982\uFF0C\u5728 Colab \u4E0A\u4F7F\u7528 TPU \u8FDB\u884C\u6D4B\u8BD5\uFF09\uFF0C\u53EA\u9700\u5C06\u4EE3\u7801\u7C98\u8D34\u5230 "),Rt=p(la,"CODE",{});var Mo=i(Rt);gr=l(Mo,"training_function()"),Mo.forEach(s),zr=l(la," \u5E76\u4F7F\u7528\u4EE5\u4E0B\u547D\u4EE4\u8FD0\u884C\u6700\u540E\u4E00\u4E2A\u5355\u5143\u683C:"),la.forEach(s),Qs=d(e),f(xe.$$.fragment,e),Ks=d(e),te=p(e,"P",{});var ra=i(te);qr=l(ra,"\u60A8\u53EF\u4EE5\u5728"),Te=p(ra,"A",{href:!0,rel:!0});var Lo=i(Te);yr=l(Lo,"\u{1F917} Accelerate repo"),Lo.forEach(s),Cr=l(ra,"\u627E\u5230\u66F4\u591A\u7684\u793A\u4F8B\u3002"),ra.forEach(s),this.h()},h(){g(b,"name","hf:doc:metadata"),g(b,"content",JSON.stringify(Xo)),g(z,"id",""),g(oe,"id",""),g(_e,"href","https://arxiv.org/abs/1711.05101"),g(_e,"rel","nofollow"),g(ke,"id",""),g(we,"id",""),g(K,"id","s-accelerate"),g(K,"class","header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full"),g(K,"href","#s-accelerate"),g(H,"class","relative group"),g(Ce,"href","https://github.com/huggingface/accelerate"),g(Ce,"rel","nofollow"),g(Te,"href","https://github.com/huggingface/accelerate/tree/main/examples"),g(Te,"rel","nofollow")},m(e,r){t(document.head,b),o(e,q,r),o(e,z,r),t(z,le),o(e,I,r),u(A,e,r),o(e,x,r),u(U,e,r),o(e,Jt,r),o(e,R,r),t(R,oa),t(R,Ze),t(Ze,na),t(R,pa),o(e,Yt,r),u(re,e,r),o(e,Qt,r),o(e,oe,r),t(oe,ia),o(e,Kt,r),o(e,W,r),t(W,ca),t(W,et),t(et,da),t(W,ma),t(W,tt),t(tt,fa),t(W,ua),o(e,Vt,r),o(e,M,r),t(M,N),t(N,_a),t(N,st),t(st,ha),t(N,$a),t(N,at),t(at,ba),t(N,ka),t(M,va),t(M,T),t(T,ja),t(T,lt),t(lt,Ea),t(T,wa),t(T,rt),t(rt,ga),t(T,za),t(T,ot),t(ot,qa),t(T,ya),t(M,Ca),t(M,nt),t(nt,Aa),o(e,Xt,r),o(e,B,r),t(B,Da),t(B,pt),t(pt,Pa),t(B,Oa),o(e,Zt,r),u(ne,e,r),o(e,es,r),o(e,Me,r),t(Me,Sa),o(e,ts,r),u(pe,e,r),o(e,ss,r),o(e,Le,r),t(Le,xa),o(e,as,r),u(ie,e,r),o(e,ls,r),o(e,Fe,r),t(Fe,Ta),o(e,rs,r),u(ce,e,r),o(e,os,r),u(de,e,r),o(e,ns,r),o(e,L,r),t(L,Ua),t(L,it),t(it,Wa),t(L,Ma),t(L,ct),t(ct,La),t(L,Fa),o(e,ps,r),o(e,Ge,r),t(Ge,Ga),o(e,is,r),u(me,e,r),o(e,cs,r),o(e,J,r),t(J,Na),t(J,dt),t(dt,Ha),t(J,Ia),o(e,ds,r),u(fe,e,r),o(e,ms,r),u(ue,e,r),o(e,fs,r),o(e,j,r),t(j,Ra),t(j,mt),t(mt,Ba),t(j,Ja),t(j,ft),t(ft,Ya),t(j,Qa),t(j,ut),t(ut,Ka),t(j,Va),t(j,_t),t(_t,Xa),t(j,Za),t(j,ht),t(ht,el),t(j,tl),o(e,us,r),o(e,E,r),t(E,sl),t(E,$t),t($t,al),t(E,ll),t(E,bt),t(bt,rl),t(E,ol),t(E,kt),t(kt,nl),t(E,pl),t(E,vt),t(vt,il),t(E,cl),t(E,_e),t(_e,dl),t(E,ml),o(e,_s,r),u(he,e,r),o(e,hs,r),o(e,F,r),t(F,fl),t(F,jt),t(jt,ul),t(F,_l),t(F,Et),t(Et,hl),t(F,$l),o(e,$s,r),u($e,e,r),o(e,bs,r),u(be,e,r),o(e,ks,r),o(e,ke,r),t(ke,bl),o(e,vs,r),o(e,D,r),t(D,kl),t(D,wt),t(wt,vl),t(D,jl),t(D,gt),t(gt,El),t(D,wl),t(D,zt),t(zt,gl),t(D,zl),o(e,js,r),u(ve,e,r),o(e,Es,r),u(je,e,r),o(e,ws,r),o(e,Y,r),t(Y,ql),t(Y,qt),t(qt,yl),t(Y,Cl),o(e,gs,r),u(Ee,e,r),o(e,zs,r),o(e,Ne,r),t(Ne,Al),o(e,qs,r),o(e,we,r),t(we,Dl),o(e,ys,r),o(e,w,r),t(w,Pl),t(w,yt),t(yt,Ol),t(w,Sl),t(w,Ct),t(Ct,xl),t(w,Tl),t(w,At),t(At,Ul),t(w,Wl),t(w,Dt),t(Dt,Ml),t(w,Ll),t(w,Pt),t(Pt,Fl),t(w,Gl),o(e,Cs,r),u(ge,e,r),o(e,As,r),u(ze,e,r),o(e,Ds,r),o(e,He,r),t(He,Nl),o(e,Ps,r),u(Q,e,r),o(e,Os,r),o(e,H,r),t(H,K),t(K,Ot),u(qe,Ot,null),t(H,Hl),t(H,St),t(St,Il),o(e,Ss,r),u(ye,e,r),o(e,xs,r),o(e,V,r),t(V,Rl),t(V,Ce),t(Ce,Bl),t(V,Jl),o(e,Ts,r),u(Ae,e,r),o(e,Us,r),o(e,Ie,r),t(Ie,Yl),o(e,Ws,r),u(De,e,r),o(e,Ms,r),o(e,y,r),t(y,Ql),t(y,xt),t(xt,Kl),t(y,Vl),t(y,Tt),t(Tt,Xl),t(y,Zl),t(y,Ut),t(Ut,er),t(y,tr),t(y,Wt),t(Wt,sr),t(y,ar),o(e,Ls,r),o(e,v,r),t(v,lr),t(v,Mt),t(Mt,rr),t(v,or),t(v,Lt),t(Lt,nr),t(v,pr),t(v,Ft),t(Ft,ir),t(v,cr),t(v,Gt),t(Gt,dr),t(v,mr),t(v,Nt),t(Nt,fr),t(v,ur),t(v,Ht),t(Ht,_r),t(v,hr),o(e,Fs,r),u(X,e,r),o(e,Gs,r),o(e,Re,r),t(Re,$r),o(e,Ns,r),u(Pe,e,r),o(e,Hs,r),o(e,Z,r),t(Z,br),t(Z,It),t(It,kr),t(Z,vr),o(e,Is,r),u(Oe,e,r),o(e,Rs,r),o(e,Be,r),t(Be,jr),o(e,Bs,r),u(Se,e,r),o(e,Js,r),o(e,Je,r),t(Je,Er),o(e,Ys,r),o(e,ee,r),t(ee,wr),t(ee,Rt),t(Rt,gr),t(ee,zr),o(e,Qs,r),u(xe,e,r),o(e,Ks,r),o(e,te,r),t(te,qr),t(te,Te),t(Te,yr),t(te,Cr),Vs=!0},p(e,[r]){const Bt={};r&2&&(Bt.$$scope={dirty:r,ctx:e}),Q.$set(Bt);const Ue={};r&2&&(Ue.$$scope={dirty:r,ctx:e}),X.$set(Ue)},i(e){Vs||(_(A.$$.fragment,e),_(U.$$.fragment,e),_(re.$$.fragment,e),_(ne.$$.fragment,e),_(pe.$$.fragment,e),_(ie.$$.fragment,e),_(ce.$$.fragment,e),_(de.$$.fragment,e),_(me.$$.fragment,e),_(fe.$$.fragment,e),_(ue.$$.fragment,e),_(he.$$.fragment,e),_($e.$$.fragment,e),_(be.$$.fragment,e),_(ve.$$.fragment,e),_(je.$$.fragment,e),_(Ee.$$.fragment,e),_(ge.$$.fragment,e),_(ze.$$.fragment,e),_(Q.$$.fragment,e),_(qe.$$.fragment,e),_(ye.$$.fragment,e),_(Ae.$$.fragment,e),_(De.$$.fragment,e),_(X.$$.fragment,e),_(Pe.$$.fragment,e),_(Oe.$$.fragment,e),_(Se.$$.fragment,e),_(xe.$$.fragment,e),Vs=!0)},o(e){h(A.$$.fragment,e),h(U.$$.fragment,e),h(re.$$.fragment,e),h(ne.$$.fragment,e),h(pe.$$.fragment,e),h(ie.$$.fragment,e),h(ce.$$.fragment,e),h(de.$$.fragment,e),h(me.$$.fragment,e),h(fe.$$.fragment,e),h(ue.$$.fragment,e),h(he.$$.fragment,e),h($e.$$.fragment,e),h(be.$$.fragment,e),h(ve.$$.fragment,e),h(je.$$.fragment,e),h(Ee.$$.fragment,e),h(ge.$$.fragment,e),h(ze.$$.fragment,e),h(Q.$$.fragment,e),h(qe.$$.fragment,e),h(ye.$$.fragment,e),h(Ae.$$.fragment,e),h(De.$$.fragment,e),h(X.$$.fragment,e),h(Pe.$$.fragment,e),h(Oe.$$.fragment,e),h(Se.$$.fragment,e),h(xe.$$.fragment,e),Vs=!1},d(e){s(b),e&&s(q),e&&s(z),e&&s(I),$(A,e),e&&s(x),$(U,e),e&&s(Jt),e&&s(R),e&&s(Yt),$(re,e),e&&s(Qt),e&&s(oe),e&&s(Kt),e&&s(W),e&&s(Vt),e&&s(M),e&&s(Xt),e&&s(B),e&&s(Zt),$(ne,e),e&&s(es),e&&s(Me),e&&s(ts),$(pe,e),e&&s(ss),e&&s(Le),e&&s(as),$(ie,e),e&&s(ls),e&&s(Fe),e&&s(rs),$(ce,e),e&&s(os),$(de,e),e&&s(ns),e&&s(L),e&&s(ps),e&&s(Ge),e&&s(is),$(me,e),e&&s(cs),e&&s(J),e&&s(ds),$(fe,e),e&&s(ms),$(ue,e),e&&s(fs),e&&s(j),e&&s(us),e&&s(E),e&&s(_s),$(he,e),e&&s(hs),e&&s(F),e&&s($s),$($e,e),e&&s(bs),$(be,e),e&&s(ks),e&&s(ke),e&&s(vs),e&&s(D),e&&s(js),$(ve,e),e&&s(Es),$(je,e),e&&s(ws),e&&s(Y),e&&s(gs),$(Ee,e),e&&s(zs),e&&s(Ne),e&&s(qs),e&&s(we),e&&s(ys),e&&s(w),e&&s(Cs),$(ge,e),e&&s(As),$(ze,e),e&&s(Ds),e&&s(He),e&&s(Ps),$(Q,e),e&&s(Os),e&&s(H),$(qe),e&&s(Ss),$(ye,e),e&&s(xs),e&&s(V),e&&s(Ts),$(Ae,e),e&&s(Us),e&&s(Ie),e&&s(Ws),$(De,e),e&&s(Ms),e&&s(y),e&&s(Ls),e&&s(v),e&&s(Fs),$(X,e),e&&s(Gs),e&&s(Re),e&&s(Ns),$(Pe,e),e&&s(Hs),e&&s(Z),e&&s(Is),$(Oe,e),e&&s(Rs),e&&s(Be),e&&s(Bs),$(Se,e),e&&s(Js),e&&s(Je),e&&s(Ys),e&&s(ee),e&&s(Qs),$(xe,e),e&&s(Ks),e&&s(te)}}}const Xo={local:"",sections:[{local:"",title:"\u8BAD\u7EC3\u524D\u7684\u51C6\u5907"},{local:"",title:"\u8BAD\u7EC3\u5FAA\u73AF"},{local:"",title:"\u8BC4\u4F30\u5FAA\u73AF"},{local:"s-accelerate",title:"S\u4F7F\u7528\u{1F917} Accelerate\u52A0\u901F\u60A8\u7684\u8BAD\u7EC3\u5FAA\u73AF"}],title:"\u4E00\u4E2A\u5B8C\u6574\u7684\u8BAD\u7EC3"};function Zo(We){return Bo(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class on extends No{constructor(b){super();Ho(this,b,Zo,Vo,Io,{})}}export{on as default,Xo as metadata};
