chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

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 ? 來(lái)源:雁回晴空 ? 作者:雁回晴空 ? 2021-02-15 09:55 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

前言

本文參考PyTorch官網(wǎng)的教程,分為五個(gè)基本模塊來(lái)介紹PyTorch。為了避免文章過(guò)長(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使得你的模型可以在過(guò)個(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ī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • gpu
    gpu
    +關(guān)注

    關(guān)注

    28

    文章

    5090

    瀏覽量

    134387
  • Data
    +關(guān)注

    關(guān)注

    0

    文章

    63

    瀏覽量

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

    關(guān)注

    73

    文章

    5589

    瀏覽量

    123880
  • pytorch
    +關(guān)注

    關(guān)注

    2

    文章

    812

    瀏覽量

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

掃碼添加小助手

加入工程師交流群

    評(píng)論

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

    學(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í)是否已過(guò)時(shí)”的激烈爭(zhēng)論。然而,從技術(shù)原理、算力成本、安全需求與實(shí)際落地路徑等維度來(lái)看,Transforme
    的頭像 發(fā)表于 08-13 09:15 ?3892次閱讀
    自動(dòng)駕駛中Transformer大模型會(huì)取代<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>嗎?

    跟老齊學(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 ?1177次閱讀
    <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 ?2916次閱讀

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

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

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

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

    軍事應(yīng)用中深度學(xué)習(xí)的挑戰(zhàn)與機(jī)遇

    人工智能尤其是深度學(xué)習(xí)技術(shù)的最新進(jìn)展,加速了不同應(yīng)用領(lǐng)域的創(chuàng)新與發(fā)展。深度學(xué)習(xí)技術(shù)的發(fā)展深刻影響了軍事發(fā)展趨勢(shì),導(dǎo)致戰(zhàn)爭(zhēng)形式和模式發(fā)生重大變化。本文將概述
    的頭像 發(fā)表于 02-14 11:15 ?810次閱讀

    BP神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)的關(guān)系

    BP神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)之間存在著密切的關(guān)系,以下是對(duì)它們之間關(guān)系的介紹: 一、BP神經(jīng)網(wǎng)絡(luò)的基本概念 BP神經(jīng)網(wǎng)絡(luò),即反向傳播神經(jīng)網(wǎng)絡(luò)(Backpropagation Neural Network
    的頭像 發(fā)表于 02-12 15:15 ?1323次閱讀

    操作指南:pytorch云服務(wù)器怎么設(shè)置?

    設(shè)置PyTorch云服務(wù)器需選擇云平臺(tái),創(chuàng)建合適的GPU實(shí)例,安裝操作系統(tǒng)、Python及Anaconda,創(chuàng)建虛擬環(huán)境,根據(jù)CUDA版本安裝PyTorch,配置環(huán)境變量,最后驗(yàn)證安裝。過(guò)程中需考慮
    的頭像 發(fā)表于 02-08 10:33 ?593次閱讀

    Triton編譯器在機(jī)器學(xué)習(xí)中的應(yīng)用

    多種深度學(xué)習(xí)框架,如TensorFlow、PyTorch、ONNX等,使得開發(fā)者能夠輕松地將不同框架下訓(xùn)練的模型部署到GPU上。 2. Triton編譯器的工作原理 Triton編譯器
    的頭像 發(fā)表于 12-24 18:13 ?1601次閱讀

    利用Arm Kleidi技術(shù)實(shí)現(xiàn)PyTorch優(yōu)化

    PyTorch 是一個(gè)廣泛應(yīng)用的開源機(jī)器學(xué)習(xí) (ML) 庫(kù)。近年來(lái),Arm 與合作伙伴通力協(xié)作,持續(xù)改進(jìn) PyTorch 的推理性能。本文將詳細(xì)介紹如何利用 Arm Kleidi 技術(shù)提升 Arm
    的頭像 發(fā)表于 12-23 09:19 ?1593次閱讀
    利用Arm Kleidi技術(shù)實(shí)現(xiàn)<b class='flag-5'>PyTorch</b>優(yōu)化

    深度學(xué)習(xí)工作負(fù)載中GPU與LPU的主要差異

    ,一個(gè)新的競(jìng)爭(zhēng)力量——LPU(Language Processing Unit,語(yǔ)言處理單元)已悄然登場(chǎng),LPU專注于解決自然語(yǔ)言處理(NLP)任務(wù)中的順序性問(wèn)題,是構(gòu)建AI應(yīng)用不可或缺的一環(huán)。 本文旨在探討深度學(xué)習(xí)工作負(fù)載中GPU
    的頭像 發(fā)表于 12-09 11:01 ?3898次閱讀
    <b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>工作負(fù)載中<b class='flag-5'>GPU</b>與LPU的主要差異

    Arm KleidiAI助力提升PyTorch上LLM推理性能

    熱門的深度學(xué)習(xí)框架尤為突出,許多企業(yè)均會(huì)選擇其作為開發(fā) AI 應(yīng)用的庫(kù)。通過(guò)部署 Arm Kleidi 技術(shù),Arm 正在努力優(yōu)化 PyTorch,以加速在基于 Arm 架構(gòu)的處理器上運(yùn)行 LLM 的性能。Arm 通過(guò)將 Kle
    的頭像 發(fā)表于 12-03 17:05 ?1921次閱讀
    Arm KleidiAI助力提升<b class='flag-5'>PyTorch</b>上LLM推理性能

    PyTorch 2.5.1: Bugs修復(fù)版發(fā)布

    ? 一,前言 在深度學(xué)習(xí)框架的不斷迭代中,PyTorch 社區(qū)始終致力于提供更穩(wěn)定、更高效的工具。最近,PyTorch 2.5.1 版本正式發(fā)布,這個(gè)版本主要針對(duì) 2.5.0 中發(fā)現(xiàn)的
    的頭像 發(fā)表于 12-03 16:11 ?1939次閱讀
    <b class='flag-5'>PyTorch</b> 2.5.1: Bugs修復(fù)版發(fā)布