Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
9f5a18bb
Unverified
提交
9f5a18bb
authored
1月 03, 2021
作者:
Glenn Jocher
提交者:
GitHub
1月 03, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Torch CUDA synchronize update (#1826)
* torch.cuda.synchronize() update * torch.cuda.synchronize() update * torch.cuda.synchronize() update * newline
上级
0b6266f5
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
20 行增加
和
21 行删除
+20
-21
torch_utils.py
utils/torch_utils.py
+20
-21
没有找到文件。
utils/torch_utils.py
浏览文件 @
9f5a18bb
...
@@ -36,42 +36,41 @@ def init_torch_seeds(seed=0):
...
@@ -36,42 +36,41 @@ def init_torch_seeds(seed=0):
# Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
# Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html
torch
.
manual_seed
(
seed
)
torch
.
manual_seed
(
seed
)
if
seed
==
0
:
# slower, more reproducible
if
seed
==
0
:
# slower, more reproducible
cudnn
.
deterministic
=
True
cudnn
.
benchmark
,
cudnn
.
deterministic
=
False
,
True
cudnn
.
benchmark
=
False
else
:
# faster, less reproducible
else
:
# faster, less reproducible
cudnn
.
deterministic
=
False
cudnn
.
benchmark
,
cudnn
.
deterministic
=
True
,
False
cudnn
.
benchmark
=
True
def
select_device
(
device
=
''
,
batch_size
=
None
):
def
select_device
(
device
=
''
,
batch_size
=
None
):
# device = 'cpu' or '0' or '0,1,2,3'
# device = 'cpu' or '0' or '0,1,2,3'
cpu_request
=
device
.
lower
()
==
'cpu'
s
=
f
'Using torch {torch.__version__} '
# string
if
device
and
not
cpu_request
:
# if device requested other than 'cpu'
cpu
=
device
.
lower
()
==
'cpu'
if
cpu
:
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
'-1'
# force torch.cuda.is_available() = False
elif
device
:
# non-cpu device requested
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
device
# set environment variable
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
device
# set environment variable
assert
torch
.
cuda
.
is_available
(),
f
'CUDA unavailable, invalid device {device} requested'
# check availablity
assert
torch
.
cuda
.
is_available
(),
f
'CUDA unavailable, invalid device {device} requested'
# check availab
i
lity
cuda
=
False
if
cpu_request
else
torch
.
cuda
.
is_available
()
cuda
=
torch
.
cuda
.
is_available
()
and
not
cpu
if
cuda
:
if
cuda
:
c
=
1024
**
2
# bytes to MB
n
=
torch
.
cuda
.
device_count
()
ng
=
torch
.
cuda
.
device_count
()
if
n
>
1
and
batch_size
:
# check that batch_size is compatible with device_count
if
ng
>
1
and
batch_size
:
# check that batch_size is compatible with device_count
assert
batch_size
%
n
==
0
,
f
'batch-size {batch_size} not multiple of GPU count {n}'
assert
batch_size
%
ng
==
0
,
f
'batch-size {batch_size} not multiple of GPU count {ng}'
space
=
' '
*
len
(
s
)
x
=
[
torch
.
cuda
.
get_device_properties
(
i
)
for
i
in
range
(
ng
)]
for
i
,
d
in
enumerate
(
device
.
split
(
','
)
if
device
else
range
(
n
)):
s
=
f
'Using torch {torch.__version__} '
p
=
torch
.
cuda
.
get_device_properties
(
i
)
for
i
,
d
in
enumerate
((
device
or
'0'
)
.
split
(
','
)):
s
+=
f
"{'' if i == 0 else space}CUDA:{d} ({p.name}, {p.total_memory / 1024 ** 2}MB)
\n
"
# bytes to MB
if
i
==
1
:
s
=
' '
*
len
(
s
)
logger
.
info
(
f
"{s}CUDA:{d} ({x[i].name}, {x[i].total_memory / c}MB)"
)
else
:
else
:
logger
.
info
(
f
'Using torch {torch.__version__} CPU'
)
s
+=
'CPU'
logger
.
info
(
'
'
)
# skip a line
logger
.
info
(
f
'{s}
\n
'
)
# skip a line
return
torch
.
device
(
'cuda:0'
if
cuda
else
'cpu'
)
return
torch
.
device
(
'cuda:0'
if
cuda
else
'cpu'
)
def
time_synchronized
():
def
time_synchronized
():
# pytorch-accurate time
# pytorch-accurate time
torch
.
cuda
.
synchronize
()
if
torch
.
cuda
.
is_available
()
else
None
if
torch
.
cuda
.
is_available
():
torch
.
cuda
.
synchronize
()
return
time
.
time
()
return
time
.
time
()
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论