forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AutoParallel] Support disttensor for Tensor.copy_ (PaddlePaddle#58369)
* support disttensor for tensor.copy_
- Loading branch information
1 parent
e65abf5
commit 1350d64
Showing
4 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import numpy as np | ||
from semi_auto_parallel_simple_net import MPDemoNetRecompute | ||
|
||
import paddle | ||
import paddle.distributed as dist | ||
from paddle import nn | ||
|
||
BATCH_SIZE = 16 | ||
BATCH_NUM = 4 | ||
IMAGE_SIZE = 784 | ||
CLASS_NUM = 10 | ||
|
||
|
||
def run_dynamic(layer, image, label): | ||
# create loss | ||
loss_fn = nn.MSELoss() | ||
# run forward and backward | ||
image = paddle.to_tensor(image) | ||
image.stop_gradient = False | ||
out = layer(image) | ||
|
||
label = paddle.to_tensor(label) | ||
loss = loss_fn(out, label) | ||
|
||
loss.backward() | ||
return loss, layer.w0.grad, layer.w1.grad | ||
|
||
|
||
class TestSemiAutoParallelRecompute: | ||
def test_recompute(): | ||
mesh = dist.ProcessMesh([0, 1], dim_names=["x"]) | ||
image = np.random.random([BATCH_SIZE, IMAGE_SIZE]).astype('float32') | ||
label = np.random.random([BATCH_SIZE, CLASS_NUM]).astype('float32') | ||
w0 = np.random.random([IMAGE_SIZE, IMAGE_SIZE]).astype('float32') | ||
w1 = np.random.random([IMAGE_SIZE, CLASS_NUM]).astype('float32') | ||
run_dynamic( | ||
layer=MPDemoNetRecompute(w0, w1, mesh), image=image, label=label | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
TestSemiAutoParallelRecompute.test_recompute() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters