• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import numpy as np
16
17import mindspore as ms
18from mindspore import Tensor
19from mindspore.parallel._utils import _to_full_shapes, _to_full_tensor
20
21
22def test_to_full_shapes():
23    device_num = 16
24    shapes = [[32, 128], [12], [24, 1, 12]]
25    full_shapes = _to_full_shapes(shapes, device_num)
26    assert full_shapes == [(512, 128), (192,), (384, 1, 12)]
27
28
29def test_to_full_tensor_1():
30    elem = Tensor([[1, 2, 3], [4, 5, 6]], dtype=ms.float32)
31    device_num = 4
32    global_rank = 2
33    full_tensor = _to_full_tensor(elem, device_num, global_rank, scaling_sens=None)
34
35    expect = ([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]])
36    expect_tensor = Tensor(expect, dtype=ms.float32)
37
38    assert np.all(full_tensor[0].asnumpy() == expect_tensor.asnumpy())
39
40
41def test_to_full_tensor_2():
42    elem0 = Tensor([[1, 2, 3], [4, 5, 6]], dtype=ms.float32)
43    elem1 = Tensor([[1], [4]], dtype=ms.int32)
44    elem = (elem0, elem1,)
45    device_num = 4
46    global_rank = 2
47    full_tensor = _to_full_tensor(elem, device_num, global_rank, scaling_sens=None)
48
49    expect0 = ([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]])
50    expect_tensor0 = Tensor(expect0, dtype=ms.float32)
51    expect1 = ([[0], [0], [0], [0], [1], [4], [0], [0]])
52    expect_tensor1 = Tensor(expect1, dtype=ms.int32)
53    expect_tensors = (expect_tensor0, expect_tensor1)
54
55    assert np.all(full_tensor[0].asnumpy() == expect_tensors[0].asnumpy())
56    assert np.all(full_tensor[1].asnumpy() == expect_tensors[1].asnumpy())
57
58
59def test_to_full_tensor_sens_2():
60    elem0 = Tensor([[1, 2, 3], [4, 5, 6]], dtype=ms.float32)
61    elem1 = Tensor([[1], [4]], dtype=ms.int32)
62    elem = (elem0, elem1,)
63    device_num = 4
64    global_rank = 2
65    full_tensor = _to_full_tensor(elem, device_num, global_rank, scaling_sens=0.1)
66
67    expect0 = ([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [1, 2, 3], [4, 5, 6], [0, 0, 0], [0, 0, 0]])
68    expect_tensor0 = Tensor(expect0, dtype=ms.float32)
69    expect1 = ([[0], [0], [0], [0], [1], [4], [0], [0]])
70    expect_tensor1 = Tensor(expect1, dtype=ms.int32)
71    expect_tensor_sens = Tensor(0.1, dtype=ms.float32)
72    expect_tensors = (expect_tensor0, expect_tensor1, expect_tensor_sens)
73
74    assert np.all(full_tensor[0].asnumpy() == expect_tensors[0].asnumpy())
75    assert np.all(full_tensor[1].asnumpy() == expect_tensors[1].asnumpy())
76    assert np.all(full_tensor[2].asnumpy() == expect_tensors[2].asnumpy())
77