日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pytorch算子torch.arange在CPU?GPU?NPU中支持?jǐn)?shù)據(jù)類型格式_python

作者:ctrlA_ctrlC_ctrlV ? 更新時(shí)間: 2022-11-10 編程語言

正文

CPU(Central Processing Unit):中央處理器 GPU(Graphics Processing Unit):圖形處理器 NPU(Neural Network Processing Unit):神經(jīng)網(wǎng)絡(luò)處理器,是基于神經(jīng)網(wǎng)絡(luò)算法與加速的新型處理器總稱。

一、 torch.arange() 和 torch.range() 的用法

pytorch官網(wǎng)介紹:

torch.arange(start,end,step) 用于產(chǎn)生一個(gè)從start開始,到end結(jié)束(注意不包括end),步長(zhǎng)為step的Tensor, 并且可以設(shè)置 Tensor 的 device 和 dtype

torch.arange 與 torch.range 功能及其相似,不同之處在于 torch.range(start,end,step) 生成的 Tensor, 包括 end

如:

a=torch.arange(1, 7, 2)
b=torch.range(1, 7, 2)
print(a)
print(b)

輸出:

tensor([1, 3, 5])
tensor([1., 3., 5., 7.])

但是建議使用 torch.arange ,因?yàn)?torch.range 即將被pytorch 移除:

二、 torch.arange 支持的數(shù)據(jù)類型格式

只考慮 float 類型

cpu 不支持 float16,支持 float32 和 float64 cpu 支持 float16 、float32 和 float64 npu 不支持 float16 和 float64 ,只支持 float32

事實(shí)上 npu 基本不支持所有的 64位類型,包括 int64 和 float64,與算子無關(guān)。當(dāng)然cpu 是支持 16位數(shù)據(jù)類型的,只是 torch.arange 不支持而已。

驗(yàn)證代碼如下:

import torch
# CPU
a=torch.arange(1, 10, 2,device="cpu",dtype=torch.float16)    # 不可以
a=torch.arange(1, 10, 2,device="cpu",dtype=torch.float32)    # 可以
a=torch.arange(1, 10, 2,device="cpu",dtype=torch.float64)    # 可以
# GPU
a=torch.arange(1, 10, 2, device="cuda:0",dtype=torch.float16)    # 可以
a=torch.arange(1, 10, 2, device="cuda:0",dtype=torch.float32)    # 可以
a=torch.arange(1, 10, 2, device="cuda:0",dtype=torch.float64)    # 可以
# NPU
a=torch.arange(1, 10, 2, device="npu:0",dtype=torch.float16)    # 不可以
a=torch.arange(1, 10, 2, device="npu:0",dtype=torch.float32)    # 可以
a=torch.arange(1, 10, 2, device="npu:0",dtype=torch.float64)    # 不可以
print(a)

原文鏈接:https://juejin.cn/post/7144578070131769374

欄目分類
最近更新