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.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(4);
  40. *data.mutable_tensor_shape() = shape;
  41. data.set_tensor_type(ms_serving::MS_FLOAT32);
  42. std::vector<float> input_data{1, 2, 3, 4};
  43. data.set_data(input_data.data(), input_data.size() * sizeof(float));
  44. *request.add_data() = data;
  45. *request.add_data() = data;
  46. std::cout << "intput tensor size is " << request.data_size() << std::endl;
  47. // Container for the data we expect from the server.
  48. PredictReply reply;
  49. // Context for the client. It could be used to convey extra information to
  50. // the server and/or tweak certain RPC behaviors.
  51. ClientContext context;
  52. // The actual RPC.
  53. Status status = stub_->Predict(&context, request, &reply);
  54. std::cout << "Compute [1, 2, 3, 4] + [1, 2, 3, 4]" << std::endl;
  55. // Act upon its status.
  56. if (status.ok()) {
  57. std::cout << "Add result is";
  58. for (size_t i = 0; i < reply.result(0).data().size() / sizeof(float); i++) {
  59. std::cout << " " << (reinterpret_cast<const float *>(reply.mutable_result(0)->mutable_data()->data()))[i];
  60. }
  61. std::cout << std::endl;
  62. return "RPC OK";
  63. } else {
  64. std::cout << status.error_code() << ": " << status.error_message() << std::endl;
  65. return "RPC failed";
  66. }
  67. }
  68. private:
  69. std::unique_ptr<MSService::Stub> stub_;
  70. };
  71. int main(int argc, char **argv) {
  72. // Instantiate the client. It requires a channel, out of which the actual RPCs
  73. // are created. This channel models a connection to an endpoint specified by
  74. // the argument "--target=" which is the only expected argument.
  75. // We indicate that the channel isn't authenticated (use of
  76. // InsecureChannelCredentials()).
  77. std::string target_str;
  78. std::string arg_target_str("--target");
  79. if (argc > 1) {
  80. // parse target
  81. std::string arg_val = argv[1];
  82. size_t start_pos = arg_val.find(arg_target_str);
  83. if (start_pos != std::string::npos) {
  84. start_pos += arg_target_str.size();
  85. if (arg_val[start_pos] == '=') {
  86. target_str = arg_val.substr(start_pos + 1);
  87. } else {
  88. std::cout << "The only correct argument syntax is --target=" << std::endl;
  89. return 0;
  90. }
  91. } else {
  92. target_str = "localhost:5500";
  93. }
  94. } else {
  95. target_str = "localhost:5500";
  96. }
  97. MSClient client(grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials()));
  98. std::string reply = client.Predict();
  99. std::cout << "client received: " << reply << std::endl;
  100. return 0;
  101. }