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_dense_matmul.py 1.9 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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.nn as nn
  16. from mindspore import Tensor, context
  17. from mindspore.common.api import _executor
  18. from mindspore.ops import operations as P
  19. from ....train_step_wrap import train_step_with_loss_warp
  20. class DenseMutMulNet(nn.Cell):
  21. def __init__(self):
  22. super(DenseMutMulNet, self).__init__()
  23. self.fc1 = nn.Dense(128, 768, activation='relu')
  24. self.fc2 = nn.Dense(128, 768, activation='relu')
  25. self.fc3 = nn.Dense(128, 768, activation='relu')
  26. self.fc4 = nn.Dense(768, 768, activation='relu')
  27. self.relu4 = nn.ReLU()
  28. self.relu5 = nn.ReLU()
  29. self.transpose = P.Transpose()
  30. self.matmul1 = P.MatMul()
  31. self.matmul2 = P.MatMul()
  32. def construct(self, x):
  33. q = self.fc1(x)
  34. k = self.fc2(x)
  35. v = self.fc3(x)
  36. k = self.transpose(k, (1, 0))
  37. c = self.relu4(self.matmul1(q, k))
  38. s = self.relu5(self.matmul2(c, v))
  39. s = self.fc4(s)
  40. return s
  41. def test_dmnet_train_step():
  42. context.reset_auto_parallel_context()
  43. input_ = Tensor(np.ones([32, 128]).astype(np.float32) * 0.01)
  44. label = Tensor(np.zeros([32, 768]).astype(np.float32))
  45. net = DenseMutMulNet()
  46. net = train_step_with_loss_warp(DenseMutMulNet())
  47. _executor.compile(net, input_, label)