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_auto_star_elimination.py 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. from mindspore.nn.loss.loss import _Loss
  15. from mindspore.ops import operations as P
  16. from mindspore.ops import functional as F
  17. from mindspore import Tensor, Parameter
  18. from mindspore.common import dtype as mstype
  19. from mindspore.common.initializer import initializer
  20. import mindspore.nn as nn
  21. import math
  22. import numpy as np
  23. import os
  24. from tests.ut.python.ops.test_math_ops import VirtualLoss
  25. from mindspore.ops import composite as C
  26. from mindspore import context
  27. from mindspore.common.api import _executor
  28. import mindspore as ms
  29. class NetWithLoss(nn.Cell):
  30. def __init__(self, network):
  31. super(NetWithLoss, self).__init__()
  32. self.loss = VirtualLoss()
  33. self.network = network
  34. def construct(self, x, y):
  35. predict = self.network(x, y)
  36. return self.loss(predict)
  37. class GradWrap(nn.Cell):
  38. def __init__(self, network):
  39. super(GradWrap, self).__init__()
  40. self.network = network
  41. def construct(self, x, y):
  42. return C.grad_all(self.network)(x, y)
  43. class CustomMatMul(nn.Cell):
  44. def __init__(self, transpose_a=False, transpose_b=False):
  45. super(CustomMatMul, self).__init__()
  46. self.fc = P.MatMul(transpose_a=transpose_a, transpose_b=transpose_b)
  47. def construct(self, x1, x2):
  48. out = self.fc(x1, x2)
  49. return out
  50. class MarginCE(_Loss):
  51. def __init__(self):
  52. super(MarginCE, self).__init__()
  53. self.fc = CustomMatMul(transpose_b=True)
  54. self.fc1 = CustomMatMul(transpose_b=True)
  55. self.fc2 = CustomMatMul(transpose_b=True)
  56. self.fc3 = CustomMatMul(transpose_b=True)
  57. self.fc4 = CustomMatMul(transpose_b=True)
  58. self.param = Parameter(Tensor(np.ones([512, 512]), dtype=mstype.float32), name="param", requires_grad=False)
  59. self.param2 = Parameter(Tensor(np.ones([512, 512]), dtype=mstype.float32), name="param", requires_grad=False)
  60. def construct(self, feature, label):
  61. fc_out = self.fc(feature, label)
  62. fc1_out = self.fc1(self.param2, self.param)
  63. fc2_out = self.fc2(fc1_out, fc_out)
  64. fc3_out = self.fc3(fc1_out, fc_out)
  65. fc4_out = self.fc4(fc2_out, fc3_out)
  66. return fc4_out
  67. def test_marin_loss():
  68. context.set_auto_parallel_context(device_num=4, global_rank=0)
  69. x = Tensor(np.ones([512, 512]), dtype=ms.float32)
  70. y = Tensor(np.ones([512, 512]), dtype=ms.float32)
  71. net = GradWrap(NetWithLoss(MarginCE()))
  72. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  73. _executor.compile(net, x, y)