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.

client_example.py 3.9 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2020 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 random
  16. import grpc
  17. import numpy as np
  18. import ms_service_pb2
  19. import ms_service_pb2_grpc
  20. import mindspore.dataset as de
  21. from mindspore import Tensor, context
  22. from mindspore import log as logger
  23. from tests.st.networks.models.bert.src.bert_model import BertModel
  24. from .generate_model import AddNet, bert_net_cfg
  25. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  26. random.seed(1)
  27. np.random.seed(1)
  28. de.config.set_seed(1)
  29. def test_add():
  30. channel = grpc.insecure_channel('localhost:5500')
  31. stub = ms_service_pb2_grpc.MSServiceStub(channel)
  32. request = ms_service_pb2.PredictRequest()
  33. x = request.data.add()
  34. x.tensor_shape.dims.extend([4])
  35. x.tensor_type = ms_service_pb2.MS_FLOAT32
  36. x.data = (np.ones([4]).astype(np.float32)).tobytes()
  37. y = request.data.add()
  38. y.tensor_shape.dims.extend([4])
  39. y.tensor_type = ms_service_pb2.MS_FLOAT32
  40. y.data = (np.ones([4]).astype(np.float32)).tobytes()
  41. result = stub.Predict(request)
  42. result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(result.result[0].tensor_shape.dims)
  43. print("ms client received: ")
  44. print(result_np)
  45. net = AddNet()
  46. net_out = net(Tensor(np.ones([4]).astype(np.float32)), Tensor(np.ones([4]).astype(np.float32)))
  47. print("add net out: ")
  48. print(net_out)
  49. assert np.allclose(net_out.asnumpy(), result_np, 0.001, 0.001, equal_nan=True)
  50. def test_bert():
  51. MAX_MESSAGE_LENGTH = 0x7fffffff
  52. input_ids = np.random.randint(0, 1000, size=(2, 32), dtype=np.int32)
  53. segment_ids = np.zeros((2, 32), dtype=np.int32)
  54. input_mask = np.zeros((2, 32), dtype=np.int32)
  55. channel = grpc.insecure_channel('localhost:5500', options=[('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
  56. ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH)])
  57. stub = ms_service_pb2_grpc.MSServiceStub(channel)
  58. request = ms_service_pb2.PredictRequest()
  59. x = request.data.add()
  60. x.tensor_shape.dims.extend([2, 32])
  61. x.tensor_type = ms_service_pb2.MS_INT32
  62. x.data = input_ids.tobytes()
  63. y = request.data.add()
  64. y.tensor_shape.dims.extend([2, 32])
  65. y.tensor_type = ms_service_pb2.MS_INT32
  66. y.data = segment_ids.tobytes()
  67. z = request.data.add()
  68. z.tensor_shape.dims.extend([2, 32])
  69. z.tensor_type = ms_service_pb2.MS_INT32
  70. z.data = input_mask.tobytes()
  71. result = stub.Predict(request)
  72. result_np = np.frombuffer(result.result[0].data, dtype=np.float32).reshape(result.result[0].tensor_shape.dims)
  73. print("ms client received: ")
  74. print(result_np)
  75. net = BertModel(bert_net_cfg, False)
  76. bert_out = net(Tensor(input_ids), Tensor(segment_ids), Tensor(input_mask))
  77. print("bert out: ")
  78. print(bert_out)
  79. bert_out_size = len(bert_out)
  80. for i in range(bert_out_size):
  81. result_np = np.frombuffer(result.result[i].data, dtype=np.float32).reshape(result.result[i].tensor_shape.dims)
  82. logger.info("i:{}, result_np:{}, bert_out:{}".
  83. format(i, result.result[i].tensor_shape.dims, bert_out[i].asnumpy().shape))
  84. assert np.allclose(bert_out[i].asnumpy(), result_np, 0.001, 0.001, equal_nan=True)