Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ColossalAI
Commits
4341f5e8
Unverified
Commit
4341f5e8
authored
Apr 17, 2023
by
Hongxin Liu
Committed by
GitHub
Apr 17, 2023
Browse files
[lazyinit] fix clone and deepcopy (#3553)
parent
1c7734bc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
2 deletions
+24
-2
colossalai/utils/model/experimental.py
colossalai/utils/model/experimental.py
+24
-2
No files found.
colossalai/utils/model/experimental.py
View file @
4341f5e8
...
...
@@ -14,8 +14,8 @@ from colossalai.tensor.d_tensor.layout import Layout
# reference: https://pytorch.org/cppdocs/notes/tensor_creation.html
_NORMAL_FACTORY
=
[
"arange"
,
"empty"
,
"full"
,
"empty"
,
"linspace"
,
"logspace"
,
"ones"
,
...
...
@@ -324,7 +324,9 @@ class LazyTensor(torch.Tensor):
def
clone
(
self
)
->
"LazyTensor"
:
def
factory_fn
():
return
self
.
materialize
().
clone
()
# if self is materialized, return self
new_tensor
=
self
.
materialize
()
if
type
(
self
)
is
LazyTensor
else
self
return
new_tensor
.
clone
()
target
=
LazyTensor
(
factory_fn
,
meta_data
=
self
.
_meta_data
)
...
...
@@ -333,6 +335,26 @@ class LazyTensor(torch.Tensor):
def
detach
(
self
)
->
Tensor
:
return
self
def
__deepcopy__
(
self
,
memo
):
if
not
self
.
is_leaf
:
raise
RuntimeError
(
"Only Tensors created explicitly by the user "
"(graph leaves) support the deepcopy protocol at the moment"
)
if
id
(
self
)
in
memo
:
return
memo
[
id
(
self
)]
def
factory_fn
():
# if self is materialized, return self
new_tensor
=
self
.
materialize
()
if
type
(
self
)
is
LazyTensor
else
self
copied
=
new_tensor
.
detach
().
clone
()
if
new_tensor
.
requires_grad
:
copied
.
requires_grad_
()
return
copied
target
=
LazyTensor
(
factory_fn
,
meta_data
=
self
.
_meta_data
)
memo
[
id
(
self
)]
=
target
return
target
@
property
def
data
(
self
):
return
self
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment