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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2021 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. # ============================================================================
  15. from typing import Generic
  16. import mindspore.context as context
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. import mindspore.numpy as mnp
  20. import mindspore.common.dtype as mstype
  21. from mindspore.ops import PrimitiveWithInfer
  22. from mindspore.ops import prim_attr_register
  23. import scipy as scp
  24. import numpy as np
  25. import pytest
  26. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  27. class LU(PrimitiveWithInfer):
  28. """
  29. LU decomposition with partial pivoting
  30. P.A = L.U
  31. """
  32. @prim_attr_register
  33. def __init__(self):
  34. super().__init__(name="LU")
  35. self.init_prim_io_names(inputs=['x'], outputs=['lu', 'pivots', 'permutation'])
  36. def __infer__(self, x):
  37. x_shape = list(x['shape'])
  38. x_dtype = x['dtype']
  39. pivots_shape = []
  40. permutation_shape = []
  41. ndim = len(x_shape)
  42. if ndim == 0:
  43. pivots_shape = x_shape
  44. permutation_shape = x_shape
  45. elif ndim == 1:
  46. pivots_shape = x_shape[:-1]
  47. # permutation_shape = x_shape[:-1]
  48. else:
  49. pivots_shape = x_shape[-2:-1]
  50. # permutation_shape = x_shape[-2:-1]
  51. output = {
  52. 'shape': (x_shape, pivots_shape, permutation_shape),
  53. 'dtype': (x_dtype, mstype.int32, mstype.int32),
  54. 'value': None
  55. }
  56. return output
  57. class LuNet(nn.Cell):
  58. def __init__(self):
  59. super(LuNet, self).__init__()
  60. self.lu = LU()
  61. def construct(self, a):
  62. return self.lu(a)
  63. @pytest.mark.platform_x86_gpu
  64. @pytest.mark.parametrize('n', [10, 20])
  65. @pytest.mark.parametrize('dtype', [np.float32, np.float64])
  66. def test_lu_net(n: int, dtype: Generic):
  67. """
  68. Feature: ALL To ALL
  69. Description: test cases for lu decomposition test cases for A[N,N]x = b[N,1]
  70. Expectation: the result match to scipy
  71. """
  72. a = (np.random.random((n, n)) + np.eye(n)).astype(dtype)
  73. expect, _ = scp.linalg.lu_factor(a)
  74. mscp_lu_net = LuNet()
  75. # mindspore tensor is row major but gpu cusolver is col major, so we should transpose it.
  76. tensor_a = Tensor(a)
  77. tensor_a = mnp.transpose(tensor_a)
  78. output, _, _ = mscp_lu_net(tensor_a)
  79. # mindspore tensor is row major but gpu cusolver is col major, so we should transpose it.
  80. output = mnp.transpose(output)
  81. rtol = 1.e-4
  82. atol = 1.e-5
  83. assert np.allclose(expect, output.asnumpy(), rtol=rtol, atol=atol)