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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2022 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. import mindspore as ms
  16. from mindspore import context
  17. from mindspore.ops import operations as P
  18. from mindspore.common.api import ms_function
  19. from mindspore.common.tensor import Tensor
  20. import mindspore.nn as nn
  21. import numpy as np
  22. import pytest
  23. class MAPPOCriticNet(nn.Cell):
  24. def __init__(self):
  25. super().__init__()
  26. self.linear1_actor = nn.Dense(54, # input local obs shape
  27. 64,
  28. weight_init='XavierUniform',
  29. # paper uses orthogonal with gain 5/3 for every dense123
  30. has_bias=False,
  31. activation=nn.Tanh())
  32. def construct(self, x):
  33. # Feature Extraction
  34. x = self.linear1_actor(x)
  35. return x
  36. class MAPPOActor(nn.Cell):
  37. def __init__(self, actor_net):
  38. super().__init__()
  39. self.actor_net = actor_net
  40. def construct(self, inputs_data):
  41. _, global_obs = inputs_data
  42. out = self.actor_net(global_obs)
  43. return out
  44. class TestClass(nn.Cell):
  45. def __init__(self, actor_list):
  46. super().__init__()
  47. self.zero = Tensor(0, ms.int32)
  48. self.actor_list = actor_list
  49. self.less = P.Less()
  50. self.zeros = P.Zeros()
  51. def train(self):
  52. state = Tensor(np.random.random((3, 128, 18)), ms.float32)
  53. init_global_obs = self.zeros((128, 54), ms.float32)
  54. out = self.test(state, init_global_obs)
  55. return out
  56. @ms_function
  57. def test(self, state, init_global_obs):
  58. num_agent = self.zero
  59. while self.less(num_agent, 3):
  60. samples = (state[num_agent], init_global_obs)
  61. self.actor_list[num_agent](samples)
  62. num_agent += 1
  63. return num_agent
  64. @pytest.mark.level0
  65. @pytest.mark.platform_x86_gpu_training
  66. @pytest.mark.env_onecard
  67. def test_net():
  68. """
  69. Feature: Tuple arg transform.
  70. Description: Test the pass: transform tuple arg to tensor arg.
  71. Expectation: Compile done without error.
  72. """
  73. context.set_context(mode=context.GRAPH_MODE, save_graphs=False, save_graphs_path="./graph_ir")
  74. actor_list = nn.CellList()
  75. for _ in range(3):
  76. net = MAPPOCriticNet()
  77. actor = MAPPOActor(net)
  78. actor_list.append(actor)
  79. test = TestClass(actor_list)
  80. graph_out = test.train()
  81. assert np.allclose(graph_out.asnumpy(), graph_out.asnumpy(), 0.0001, 0.0001)