Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ICV-mmengine_basecode
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florian Schiffel
ICV-mmengine_basecode
Commits
1c67f9eb
Unverified
Commit
1c67f9eb
authored
1 year ago
by
Yinlei Sun
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
[Enhancement] Support BoolTensor and LongTensor on Ascend NPU (#1011)
parent
8bf1ecad
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mmengine/structures/instance_data.py
+17
-10
17 additions, 10 deletions
mmengine/structures/instance_data.py
with
17 additions
and
10 deletions
mmengine/structures/instance_data.py
+
17
−
10
View file @
1c67f9eb
# Copyright (c) OpenMMLab. All rights reserved.
import
itertools
from
collections.abc
import
Sized
from
typing
import
List
,
Union
from
typing
import
Any
,
List
,
Union
import
numpy
as
np
import
torch
from
mmengine.device
import
get_device
from
.base_data_element
import
BaseDataElement
IndexType
=
Union
[
str
,
slice
,
int
,
list
,
torch
.
LongTensor
,
torch
.
cuda
.
LongTensor
,
torch
.
BoolTensor
,
torch
.
cuda
.
BoolTensor
,
np
.
ndarray
]
BoolTypeTensor
:
Union
[
Any
]
LongTypeTensor
:
Union
[
Any
]
if
get_device
()
==
'
npu
'
:
BoolTypeTensor
=
Union
[
torch
.
BoolTensor
,
torch
.
npu
.
BoolTensor
]
LongTypeTensor
=
Union
[
torch
.
LongTensor
,
torch
.
npu
.
LongTensor
]
else
:
BoolTypeTensor
=
Union
[
torch
.
BoolTensor
,
torch
.
cuda
.
BoolTensor
]
LongTypeTensor
=
Union
[
torch
.
LongTensor
,
torch
.
cuda
.
LongTensor
]
IndexType
:
Union
[
Any
]
=
Union
[
str
,
slice
,
int
,
list
,
LongTypeTensor
,
BoolTypeTensor
,
np
.
ndarray
]
# Modified from
...
...
@@ -156,6 +166,7 @@ class InstanceData(BaseDataElement):
Returns:
:obj:`InstanceData`: Corresponding values.
"""
assert
isinstance
(
item
,
IndexType
.
__args__
)
if
isinstance
(
item
,
list
):
item
=
np
.
array
(
item
)
if
isinstance
(
item
,
np
.
ndarray
):
...
...
@@ -165,9 +176,6 @@ class InstanceData(BaseDataElement):
# More details in https://github.com/numpy/numpy/issues/9464
item
=
item
.
astype
(
np
.
int64
)
if
item
.
dtype
==
np
.
int32
else
item
item
=
torch
.
from_numpy
(
item
)
assert
isinstance
(
item
,
(
str
,
slice
,
int
,
torch
.
LongTensor
,
torch
.
cuda
.
LongTensor
,
torch
.
BoolTensor
,
torch
.
cuda
.
BoolTensor
))
if
isinstance
(
item
,
str
):
return
getattr
(
self
,
item
)
...
...
@@ -183,7 +191,7 @@ class InstanceData(BaseDataElement):
if
isinstance
(
item
,
torch
.
Tensor
):
assert
item
.
dim
()
==
1
,
'
Only support to get the
'
\
'
values along the first dimension.
'
if
isinstance
(
item
,
(
torch
.
BoolTensor
,
torch
.
cuda
.
BoolTensor
)
):
if
isinstance
(
item
,
BoolT
ypeT
ensor
.
__args__
):
assert
len
(
item
)
==
len
(
self
),
'
The shape of the
'
\
'
input(BoolTensor)
'
\
f
'
{
len
(
item
)
}
'
\
...
...
@@ -202,8 +210,7 @@ class InstanceData(BaseDataElement):
v
,
(
str
,
list
,
tuple
))
or
(
hasattr
(
v
,
'
__getitem__
'
)
and
hasattr
(
v
,
'
cat
'
)):
# convert to indexes from BoolTensor
if
isinstance
(
item
,
(
torch
.
BoolTensor
,
torch
.
cuda
.
BoolTensor
)):
if
isinstance
(
item
,
BoolTypeTensor
.
__args__
):
indexes
=
torch
.
nonzero
(
item
).
view
(
-
1
).
cpu
().
numpy
().
tolist
()
else
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment