-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
python/paddle/fluid/tests/unittests/npu/test_memcpy_op_npu.py
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,103 @@ | ||
# Copyright (c) 2021 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. | ||
|
||
from __future__ import print_function | ||
|
||
import numpy as np | ||
import unittest | ||
import sys | ||
sys.path.append("..") | ||
from op_test import OpTest | ||
import paddle | ||
import paddle.fluid as fluid | ||
import paddle.fluid.core as core | ||
|
||
paddle.enable_static() | ||
SEED = 2021 | ||
|
||
|
||
@unittest.skipIf(not paddle.is_compiled_with_npu(), | ||
"core is not compiled with NPU") | ||
class TestMemcpy_FillConstant(unittest.TestCase): | ||
def get_prog(self): | ||
paddle.enable_static() | ||
main_program = Program() | ||
with program_guard(main_program): | ||
cpu_var_name = "tensor@Cpu" | ||
npu_var_name = "tensor@Npu" | ||
cpu_var = main_program.global_block().create_var( | ||
name=cpu_var_name, | ||
shape=[10, 10], | ||
dtype='float32', | ||
persistable=False, | ||
stop_gradient=True) | ||
npu_var = main_program.global_block().create_var( | ||
name=npu_var_name, | ||
shape=[10, 10], | ||
dtype='float32', | ||
persistable=False, | ||
stop_gradient=True) | ||
main_program.global_block().append_op( | ||
type="fill_constant", | ||
outputs={"Out": npu_var_name}, | ||
attrs={ | ||
"shape": [10, 10], | ||
"dtype": npu_var.dtype, | ||
"value": 1.0, | ||
"place_type": 1 | ||
}) | ||
main_program.global_block().append_op( | ||
type="fill_constant", | ||
outputs={"Out": cpu_var_name}, | ||
attrs={ | ||
"shape": [10, 10], | ||
"dtype": npu_var.dtype, | ||
"value": 0.0, | ||
"place_type": 2 | ||
}) | ||
return main_program, npu_var, cpu_var | ||
|
||
def test_npu_cpoy_to_cpu(self): | ||
main_program, npu_var, cpu_var = self.get_prog() | ||
main_program.global_block().append_op( | ||
type='memcpy', | ||
inputs={'X': npu_var}, | ||
outputs={'Out': cpu_var}, | ||
attrs={'dst_place_type': 3}) | ||
place = fluid.NPUPlace(0) | ||
exe = fluid.Executor(place) | ||
npu_, cpu_ = exe.run(main_program, | ||
feed={}, | ||
fetch_list=[npu_var.name, cpu_var.name]) | ||
self.assertTrue(np.allclose(npu_, cpu_)) | ||
self.assertTrue(np.allclose(cpu_, np.ones((10, 10)))) | ||
|
||
def test_cpu_cpoy_gpu(self): | ||
main_program, npu_var, cpu_var = self.get_prog() | ||
main_program.global_block().append_op( | ||
type='memcpy', | ||
inputs={'X': cpu_var}, | ||
outputs={'Out': npu_var}, | ||
attrs={'dst_place_type': 2}) | ||
place = fluid.NPUPlace(0) | ||
exe = fluid.Executor(place) | ||
npu_, cpu_ = exe.run(main_program, | ||
feed={}, | ||
fetch_list=[npu_var.name, cpu_var.name]) | ||
self.assertTrue(np.allclose(npu_, cpu_)) | ||
self.assertTrue(np.allclose(npu_, np.zeros((10, 10)))) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |