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.

ms_client.cc 3.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <grpcpp/grpcpp.h>
  17. #include <iostream>
  18. #include <vector>
  19. #include <string>
  20. #include <fstream>
  21. #include "./ms_service.grpc.pb.h"
  22. using grpc::Channel;
  23. using grpc::ClientContext;
  24. using grpc::Status;
  25. using ms_serving::MSService;
  26. using ms_serving::PredictReply;
  27. using ms_serving::PredictRequest;
  28. using ms_serving::Tensor;
  29. using ms_serving::TensorShape;
  30. class MSClient {
  31. public:
  32. explicit MSClient(std::shared_ptr<Channel> channel) : stub_(MSService::NewStub(channel)) {}
  33. ~MSClient() = default;
  34. std::string Predict() {
  35. // Data we are sending to the server.
  36. PredictRequest request;
  37. Tensor data;
  38. TensorShape shape;
  39. shape.add_dims(2);
  40. shape.add_dims(2);
  41. *data.mutable_tensor_shape() = shape;
  42. data.set_tensor_type(ms_serving::MS_FLOAT32);
  43. std::vector<float> input_data{1, 2, 3, 4};
  44. data.set_data(input_data.data(), input_data.size() * sizeof(float));
  45. *request.add_data() = data;
  46. *request.add_data() = data;
  47. std::cout << "intput tensor size is " << request.data_size() << std::endl;
  48. // Container for the data we expect from the server.
  49. PredictReply reply;
  50. // Context for the client. It could be used to convey extra information to
  51. // the server and/or tweak certain RPC behaviors.
  52. ClientContext context;
  53. // The actual RPC.
  54. Status status = stub_->Predict(&context, request, &reply);
  55. std::cout << "Compute [[1, 2], [3, 4]] + [[1, 2], [3, 4]]" << std::endl;
  56. // Act upon its status.
  57. if (status.ok()) {
  58. std::cout << "Add result is";
  59. for (size_t i = 0; i < reply.result(0).data().size() / sizeof(float); i++) {
  60. std::cout << " " << (reinterpret_cast<const float *>(reply.mutable_result(0)->mutable_data()->data()))[i];
  61. }
  62. std::cout << std::endl;
  63. return "RPC OK";
  64. } else {
  65. std::cout << status.error_code() << ": " << status.error_message() << std::endl;
  66. return "RPC failed";
  67. }
  68. }
  69. private:
  70. std::unique_ptr<MSService::Stub> stub_;
  71. };
  72. int main(int argc, char **argv) {
  73. // Instantiate the client. It requires a channel, out of which the actual RPCs
  74. // are created. This channel models a connection to an endpoint specified by
  75. // the argument "--target=" which is the only expected argument.
  76. // We indicate that the channel isn't authenticated (use of
  77. // InsecureChannelCredentials()).
  78. std::string target_str;
  79. std::string arg_target_str("--target");
  80. if (argc > 1) {
  81. // parse target
  82. std::string arg_val = argv[1];
  83. size_t start_pos = arg_val.find(arg_target_str);
  84. if (start_pos != std::string::npos) {
  85. start_pos += arg_target_str.size();
  86. if (start_pos < arg_val.size() && arg_val[start_pos] == '=') {
  87. target_str = arg_val.substr(start_pos + 1);
  88. } else {
  89. std::cout << "The only correct argument syntax is --target=" << std::endl;
  90. return 0;
  91. }
  92. } else {
  93. target_str = "localhost:5500";
  94. }
  95. } else {
  96. target_str = "localhost:5500";
  97. }
  98. MSClient client(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  99. std::string reply = client.Predict();
  100. std::cout << "client received: " << reply << std::endl;
  101. return 0;
  102. }