You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_self_attention.py 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2019 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. import numpy as np
  15. import mindspore as ms
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.context import set_auto_parallel_context
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from mindspore.common.initializer import initializer
  24. from mindspore.common.parameter import Parameter
  25. from tests.ut.python.ops.test_math_ops import VirtualLoss
  26. grad_all = C.GradOperation(get_all=True)
  27. class NetWithLoss(nn.Cell):
  28. def __init__(self, network):
  29. super(NetWithLoss, self).__init__()
  30. self.loss = VirtualLoss()
  31. self.network = network
  32. def construct(self, x):
  33. predict = self.network(x)
  34. return self.loss(predict)
  35. class GradWrap(nn.Cell):
  36. def __init__(self, network):
  37. super(GradWrap, self).__init__()
  38. self.network = network
  39. def construct(self, x):
  40. return grad_all(self.network)(x)
  41. def compile_net(net, x):
  42. net.set_auto_parallel()
  43. net.set_train()
  44. _executor.compile(net, x)
  45. class Net(nn.Cell):
  46. def __init__(self, strategy1, strategy2, strategy3, strategy4, strategy5):
  47. super().__init__()
  48. self.query_w = Parameter(initializer(
  49. "normal", [8, 16], ms.float32), name='query')
  50. self.query = P.MatMul().shard(strategy1)
  51. self.key_w = Parameter(initializer(
  52. "normal", [8, 16], ms.float32), name='key')
  53. self.key = P.MatMul().shard(strategy2)
  54. self.value_w = Parameter(initializer(
  55. "normal", [8, 16], ms.float32), name='value')
  56. self.value = P.MatMul().shard(strategy3)
  57. self.score = P.MatMul().shard(strategy4)
  58. self.context = P.MatMul().shard(strategy5)
  59. self.transpose1 = P.Transpose()
  60. self.transpose2 = P.Transpose()
  61. self.relu = P.ReLU()
  62. def construct(self, x):
  63. q = self.query(x, self.query_w)
  64. k = self.key(x, self.key_w)
  65. v = self.value(x, self.value_w)
  66. k = self.transpose1(k, (1, 0))
  67. s = self.score(q, k)
  68. v = self.transpose2(v, (1, 0))
  69. c = self.context(v, s)
  70. out = self.relu(c)
  71. return out
  72. def test_self_attention_standalone():
  73. set_auto_parallel_context(device_num=8, global_rank=0)
  74. context.set_auto_parallel_context(parallel_mode="stand_alone")
  75. net = GradWrap(NetWithLoss(
  76. Net(None, None, None, None, None)))
  77. x = Tensor(np.ones([32, 8]), dtype=ms.float32)
  78. compile_net(net, x)
  79. def test_self_attention_semi():
  80. set_auto_parallel_context(device_num=8, global_rank=0)
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  82. strategy1 = ((2, 2), (2, 2))
  83. strategy2 = ((2, 2), (2, 2))
  84. strategy3 = ((2, 2), (2, 2))
  85. strategy4 = ((2, 4), (4, 1))
  86. strategy5 = ((2, 1), (1, 4))
  87. net = GradWrap(NetWithLoss(
  88. Net(strategy1, strategy2, strategy3, strategy4, strategy5)))
  89. x = Tensor(np.ones([32, 8]), dtype=ms.float32)
  90. compile_net(net, x)
  91. def test_self_attention_dp():
  92. set_auto_parallel_context(device_num=8, global_rank=0)
  93. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  94. strategy1 = ((8, 1), (1, 1))
  95. strategy2 = ((8, 1), (1, 1))
  96. strategy3 = ((8, 1), (1, 1))
  97. strategy4 = ((8, 1), (1, 1))
  98. strategy5 = ((8, 1), (1, 1))
  99. net = GradWrap(NetWithLoss(
  100. Net(strategy1, strategy2, strategy3, strategy4, strategy5)))
  101. x = Tensor(np.ones([32, 8]), dtype=ms.float32)
  102. compile_net(net, x)
  103. def test_self_attention_auto():
  104. set_auto_parallel_context(device_num=8, global_rank=0)
  105. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  106. net = GradWrap(NetWithLoss(
  107. Net(None, None, None, None, None)))
  108. x = Tensor(np.ones([32, 8]), dtype=ms.float32)
  109. compile_net(net, x)