aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/ops/rpc_ops.cc
blob: 136f96d9ea764e7272940d0f2ae2fd316bf90780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"

namespace tensorflow {

using tensorflow::shape_inference::InferenceContext;
using tensorflow::shape_inference::ShapeHandle;

Status RpcShapeOp(InferenceContext* c, bool try_rpc) {
  ShapeHandle address;
  ShapeHandle method;
  ShapeHandle request;
  ShapeHandle output;
  TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(0), 1, &address));
  if (c->Rank(address) == 1) {
    TF_RETURN_IF_ERROR(c->Merge(output, address, &output));
  }
  TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(1), 1, &method));
  if (c->Rank(method) == 1) {
    TF_RETURN_IF_ERROR(c->Merge(output, method, &output));
  }
  TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(2), 1, &request));
  if (c->Rank(request) == 1) {
    TF_RETURN_IF_ERROR(c->Merge(output, request, &output));
  }
  if (!c->RankKnown(output)) {
    output = request;
  }
  c->set_output(0, output);  // response
  if (try_rpc) {
    c->set_output(1, output);  // status_code
    c->set_output(2, output);  // status_message
  }
  return Status::OK();
}

REGISTER_OP("Rpc")
    .Input("address: string")
    .Input("method: string")
    .Input("request: string")
    .Attr("protocol: string = ''")
    .Attr("fail_fast: bool = true")
    .Attr("timeout_in_ms: int = 0")
    .Output("response: string")
    .SetIsStateful()
    .SetShapeFn([](InferenceContext* c) {
      return RpcShapeOp(c, /*try_rpc=*/false);
    });

REGISTER_OP("TryRpc")
    .Input("address: string")
    .Input("method: string")
    .Input("request: string")
    .Attr("protocol: string = ''")
    .Attr("fail_fast: bool = true")
    .Attr("timeout_in_ms: int = 0")
    .Output("response: string")
    .Output("status_code: int32")
    .Output("status_message: string")
    .SetIsStateful()
    .SetShapeFn([](InferenceContext* c) {
      return RpcShapeOp(c, /*try_rpc=*/true);
    });

}  // namespace tensorflow