91欧美超碰AV自拍|国产成年人性爱视频免费看|亚洲 日韩 欧美一厂二区入|人人看人人爽人人操aV|丝袜美腿视频一区二区在线看|人人操人人爽人人爱|婷婷五月天超碰|97色色欧美亚州A√|另类A√无码精品一级av|欧美特级日韩特级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

基于PyTorch的深度學(xué)習(xí)入門教程之DataParallel使用多GPU

ss ? 來源:雁回晴空 ? 作者:雁回晴空 ? 2021-02-15 09:55 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

前言

本文參考PyTorch官網(wǎng)的教程,分為五個(gè)基本模塊來介紹PyTorch。為了避免文章過長(zhǎng),這五個(gè)模塊分別在五篇博文中介紹。

Part1:PyTorch簡(jiǎn)單知識(shí)

Part2:PyTorch的自動(dòng)梯度計(jì)算

Part3:使用PyTorch構(gòu)建一個(gè)神經(jīng)網(wǎng)絡(luò)

Part4:訓(xùn)練一個(gè)神經(jīng)網(wǎng)絡(luò)分類器

Part5:數(shù)據(jù)并行化

本文是關(guān)于Part5的內(nèi)容。

Part5:數(shù)據(jù)并行化

本文中,將會(huì)講到DataParallel使用多GPU。

在PyTorch中使用GPU比較簡(jiǎn)單,可以這樣把模型放到GPU上。

model.gpu()

還可以復(fù)制所有的tensors到GPU上。

mytensor = my_tensor.gpu()

請(qǐng)注意,單純調(diào)用mytensor.gpu()不會(huì)拷貝tensor到GPU上。你需要把它分配給一個(gè)新的tensor,然后在GPU上使用這個(gè)新的tensor。

前向和反向傳播可以在多個(gè)GPU上運(yùn)行。但是,PyTorch默認(rèn)只使用一個(gè)GPU。你可以使用DataParallel使得你的模型可以在過個(gè)GPU上并行運(yùn)算。

model = nn.DataParallel(model)

1 Package導(dǎo)入和參數(shù)設(shè)置

導(dǎo)入PyTorch的模塊并且設(shè)置參數(shù)。

2 虛擬數(shù)據(jù)集

制作虛擬(隨機(jī))數(shù)據(jù)集,只需要執(zhí)行g(shù)etitem。

class RandomDataset(Dataset):

    def __init__(self, size, length):
        self.len = length
        self.data = torch.randn(length, size)

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len

rand_loader = DataLoader(dataset=RandomDataset(input_size, 100),
                         batch_size=batch_size, shuffle=True)

3 簡(jiǎn)單模型

作為實(shí)例,我們的模型只是獲取輸入,進(jìn)行線性運(yùn)算,給出結(jié)果。但是,你可以把DataParallel應(yīng)用到任何模型(CNN,RNN,Capsule Net 等等)。

class Model(nn.Module):
    # Our model

    def __init__(self, input_size, output_size):
        super(Model, self).__init__()
        self.fc = nn.Linear(input_size, output_size)

    def forward(self, input):
        output = self.fc(input)
        print("  In Model: input size", input.size(),
              "output size", output.size())

        return output

4 創(chuàng)建模型和數(shù)據(jù)并行

這是本篇教程的核心內(nèi)容。我們需要制作一個(gè)模型實(shí)例,并檢查是否有多個(gè)GPU。如果有多GPU,可以使用nn.DataParallel打包我們的model。之后,我們可以把利用model.gpu()把模型放到GPU上。

model = Model(input_size, output_size)
if torch.cuda.device_count() > 1:
  print("Let's use", torch.cuda.device_count(), "GPUs!")
  # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
  model = nn.DataParallel(model)

if torch.cuda.is_available():
   model.cuda()

5 運(yùn)行模型

for data in rand_loader:
    if torch.cuda.is_available():
        input_var = Variable(data.cuda())
    else:
        input_var = Variable(data)

    output = model(input_var)
    print("Outside: input size", input_var.size(),
          "output_size", output.size())

期望輸出:

In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  In Model: input size torch.Size([30, 5]) output size torch.Size([30, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
  In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

6 結(jié)果

(1)如果有2 GPUs,可以看到

# on 2 GPUs
Let's use 2 GPUs!
    In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
    In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
    In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
    In Model: input size torch.Size([15, 5]) output size torch.Size([15, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
    In Model: input size torch.Size([5, 5]) output size torch.Size([5, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

2)如果有3 GPUs,可以看到

Let's use 3 GPUs!
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
    In Model: input size torch.Size([10, 5]) output size torch.Size([10, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

3)如果有8 GPUs,可以看到

Let's use 8 GPUs!
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([4, 5]) output size torch.Size([4, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([30, 5]) output_size torch.Size([30, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
    In Model: input size torch.Size([2, 5]) output size torch.Size([2, 2])
Outside: input size torch.Size([10, 5]) output_size torch.Size([10, 2])

7 總結(jié)

DataParallel將數(shù)據(jù)自動(dòng)分割送到不同的GPU上處理,在每個(gè)模塊完成工作后,DataParallel再收集整合這些結(jié)果返回。

責(zé)任編輯:xj


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • gpu
    gpu
    +關(guān)注

    關(guān)注

    28

    文章

    5196

    瀏覽量

    135506
  • Data
    +關(guān)注

    關(guān)注

    0

    文章

    63

    瀏覽量

    39157
  • 深度學(xué)習(xí)
    +關(guān)注

    關(guān)注

    73

    文章

    5599

    瀏覽量

    124406
  • pytorch
    +關(guān)注

    關(guān)注

    2

    文章

    813

    瀏覽量

    14856
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    PyTorch 中RuntimeError分析

    ? 錯(cuò)誤原因 這個(gè) RuntimeError 是因?yàn)樵?PyTorch 中,upsample_nearest2d_out_frame(最近鄰2D上采樣)操作尚未對(duì) BFloat16 數(shù)據(jù)類型提供
    發(fā)表于 03-06 06:02

    Pytorch 與 Visionfive2 兼容嗎?

    Pytorch 與 Visionfive2 兼容嗎? $ pip3 install torch torchvision torchaudio --index-url https
    發(fā)表于 02-06 08:28

    機(jī)器學(xué)習(xí)深度學(xué)習(xí)中需避免的 7 個(gè)常見錯(cuò)誤與局限性

    無論你是剛入門還是已經(jīng)從事人工智能模型相關(guān)工作一段時(shí)間,機(jī)器學(xué)習(xí)深度學(xué)習(xí)中都存在一些我們需要時(shí)刻關(guān)注并銘記的常見錯(cuò)誤。如果對(duì)這些錯(cuò)誤置之不理,日后可能會(huì)引發(fā)諸多麻煩!只要我們密切關(guān)注
    的頭像 發(fā)表于 01-07 15:37 ?203次閱讀
    機(jī)器<b class='flag-5'>學(xué)習(xí)</b>和<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>中需避免的 7 個(gè)常見錯(cuò)誤與局限性

    【團(tuán)購】獨(dú)家全套珍藏!龍哥LabVIEW視覺深度學(xué)習(xí)實(shí)戰(zhàn)課(11大系列課程,共5000+分鐘)

    、GPU加速訓(xùn)練(可選) 雙軌教學(xué):傳統(tǒng)視覺算法+深度學(xué)習(xí)方案全覆蓋 輕量化部署:8.6M超輕OCR模型,適合嵌入式設(shè)備集成 無監(jiān)督學(xué)習(xí):無需缺陷樣本即可訓(xùn)練高精度檢測(cè)模型 持續(xù)更新:
    發(fā)表于 12-04 09:28

    【團(tuán)購】獨(dú)家全套珍藏!龍哥LabVIEW視覺深度學(xué)習(xí)實(shí)戰(zhàn)課程(11大系列課程,共5000+分鐘)

    、GPU加速訓(xùn)練(可選) 雙軌教學(xué):傳統(tǒng)視覺算法+深度學(xué)習(xí)方案全覆蓋 輕量化部署:8.6M超輕OCR模型,適合嵌入式設(shè)備集成 無監(jiān)督學(xué)習(xí):無需缺陷樣本即可訓(xùn)練高精度檢測(cè)模型 持續(xù)更新:
    發(fā)表于 12-03 13:50

    學(xué)習(xí)物聯(lián)網(wǎng)怎么入門?

    景等。同時(shí),學(xué)習(xí)物聯(lián)網(wǎng)的基本技術(shù),如傳感器技術(shù)、通信技術(shù)、云計(jì)算等,也是非常重要的。 其次,選擇適合自己的學(xué)習(xí)方式也是入門學(xué)習(xí)物聯(lián)網(wǎng)的重要一步。
    發(fā)表于 10-14 10:34

    自動(dòng)駕駛中Transformer大模型會(huì)取代深度學(xué)習(xí)嗎?

    持續(xù)討論。特別是在自動(dòng)駕駛領(lǐng)域,部分廠商開始嘗試將模態(tài)大模型(MLLM)引入到感知、規(guī)劃與決策系統(tǒng),引發(fā)了“傳統(tǒng)深度學(xué)習(xí)是否已過時(shí)”的激烈爭(zhēng)論。然而,從技術(shù)原理、算力成本、安全需求與實(shí)際落地路徑等維度來看,Transforme
    的頭像 發(fā)表于 08-13 09:15 ?4189次閱讀
    自動(dòng)駕駛中Transformer大模型會(huì)取代<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>嗎?

    別讓 GPU 故障拖后腿,捷智算GPU維修室來救場(chǎng)!

    在AI浪潮洶涌的當(dāng)下,GPU已然成為眾多企業(yè)與科研機(jī)構(gòu)的核心生產(chǎn)力。從深度學(xué)習(xí)模型訓(xùn)練,到影視渲染、復(fù)雜科學(xué)計(jì)算,GPU憑借強(qiáng)大并行計(jì)算能力,極大提升運(yùn)算效率。然而,就像高速運(yùn)轉(zhuǎn)的精密
    的頭像 發(fā)表于 07-17 18:56 ?1151次閱讀
    別讓 <b class='flag-5'>GPU</b> 故障拖后腿,捷智算<b class='flag-5'>GPU</b>維修室來救場(chǎng)!

    【「算力芯片 | 高性能 CPU/GPU/NPU 微架構(gòu)分析」閱讀體驗(yàn)】+NVlink技術(shù)從應(yīng)用到原理

    自家GPU 提出的卡算力互連技術(shù),是早期為了應(yīng)對(duì)深度學(xué)習(xí)對(duì)超高算力需求而單卡算力不足的局面的解決方案,當(dāng)然這都是官方用來吹牛的話術(shù)。我自己在2019年左右第一次接觸到
    發(fā)表于 06-18 19:31

    跟老齊學(xué)Python:從入門到精通

    本帖最后由 yuu_cool 于 2025-6-3 16:52 編輯 本資料是面向編程零基礎(chǔ)讀者的Python 入門教程,內(nèi)容涵蓋了Python 的基礎(chǔ)知識(shí)和初步應(yīng)用。以比較輕快的風(fēng)格,向零基
    發(fā)表于 06-03 16:10

    GPU架構(gòu)深度解析

    GPU架構(gòu)深度解析從圖形處理到通用計(jì)算的進(jìn)化之路圖形處理單元(GPU),作為現(xiàn)代計(jì)算機(jī)中不可或缺的一部分,已經(jīng)從最初的圖形渲染專用處理器,發(fā)展成為強(qiáng)大的并行計(jì)算引擎,廣泛應(yīng)用于人工智能、科學(xué)計(jì)算
    的頭像 發(fā)表于 05-30 10:36 ?1871次閱讀
    <b class='flag-5'>GPU</b>架構(gòu)<b class='flag-5'>深度</b>解析

    ARM Mali GPU 深度解讀

    ARM Mali GPU 深度解讀 ARM Mali 是 Arm 公司面向移動(dòng)設(shè)備、嵌入式系統(tǒng)和基礎(chǔ)設(shè)施市場(chǎng)設(shè)計(jì)的圖形處理器(GPU)IP 核,憑借其異構(gòu)計(jì)算架構(gòu)、能效優(yōu)化和生態(tài)協(xié)同,成為全球移動(dòng)
    的頭像 發(fā)表于 05-29 10:12 ?4343次閱讀

    摩爾線程發(fā)布Torch-MUSA v2.0.0版本 支持原生FP8和PyTorch 2.5.0

    近日,摩爾線程正式發(fā)布Torch-MUSA v2.0.0版本,這是其面向PyTorch深度學(xué)習(xí)框架的MUSA擴(kuò)展庫的重要升級(jí)。新版本基于MUSA Compute Capability 3.1計(jì)算架構(gòu)
    的頭像 發(fā)表于 05-11 16:41 ?1732次閱讀

    海思SS626開發(fā)板

    識(shí)別/運(yùn)動(dòng)跟蹤等)。 主板集成常用視頻硬件接口,所有外設(shè)接口電路均通過嚴(yán)格的抗干擾測(cè)試,使產(chǎn)品在 EMC 及穩(wěn)定性方面 具有良好表現(xiàn);使用通過穩(wěn)定性測(cè)試及深度優(yōu)化的系統(tǒng)軟件(Linux 系統(tǒng)),支持業(yè)界主流深度學(xué)習(xí)框架(如 Ca
    發(fā)表于 04-24 10:04

    摩爾線程與當(dāng)虹科技達(dá)成深度合作

    近日,摩爾線程與當(dāng)虹科技達(dá)成深度合作,基于國產(chǎn)GPU成功完成了與BlackEye模態(tài)視聽大模型的深度融合。雙方聯(lián)手打造專業(yè)級(jí)視聽“引擎”,并在超高清
    的頭像 發(fā)表于 03-20 15:22 ?1514次閱讀