• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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 
16 #include "absl/strings/str_cat.h"
17 #include "tensorflow/c/eager/c_api.h"
18 #include "tensorflow/c/eager/c_api_experimental.h"
19 #include "tensorflow/c/eager/c_api_internal.h"
20 #include "tensorflow/c/eager/c_api_remote_test_util.h"
21 #include "tensorflow/c/eager/c_api_test_util.h"
22 #include "tensorflow/c/eager/tfe_tensorhandle_internal.h"
23 #include "tensorflow/core/common_runtime/eager/eager_operation.h"
24 #include "tensorflow/core/common_runtime/function_optimization_registry.h"
25 #include "tensorflow/core/distributed_runtime/rpc/grpc_server_lib.h"
26 #include "tensorflow/core/framework/function.h"
27 #include "tensorflow/core/graph/graph.h"
28 #include "tensorflow/core/platform/casts.h"
29 #include "tensorflow/core/platform/errors.h"
30 #include "tensorflow/core/platform/protobuf.h"
31 #include "tensorflow/core/platform/test.h"
32 #include "tensorflow/core/protobuf/cluster.pb.h"
33 #include "tensorflow/core/protobuf/config.pb.h"
34 #include "tensorflow/core/protobuf/tensorflow_server.pb.h"
35 
36 namespace {
37 
38 using ::tensorflow::string;
39 
TestRemoteExecute(bool async)40 void TestRemoteExecute(bool async) {
41   tensorflow::ServerDef server_def = GetServerDef(2);
42 
43   // This server def has the task index set to 0.
44   string serialized = server_def.SerializeAsString();
45 
46   server_def.set_task_index(1);
47 
48   std::unique_ptr<tensorflow::GrpcServer> worker_server;
49   ASSERT_TRUE(tensorflow::GrpcServer::Create(
50                   server_def, tensorflow::Env::Default(), &worker_server)
51                   .ok());
52   ASSERT_TRUE(worker_server->Start().ok());
53 
54   TF_Status* status = TF_NewStatus();
55   TFE_ContextOptions* opts = TFE_NewContextOptions();
56   TFE_ContextOptionsSetAsync(opts, static_cast<unsigned char>(async));
57   TFE_ContextOptionsSetDevicePlacementPolicy(opts,
58                                              TFE_DEVICE_PLACEMENT_EXPLICIT);
59   TFE_Context* ctx = TFE_NewContext(opts, status);
60   EXPECT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
61   TFE_DeleteContextOptions(opts);
62 
63   TFE_ContextSetServerDef(ctx, 0, serialized.data(), serialized.size(), status);
64   EXPECT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
65 
66   TFE_TensorHandle* h0_task0 = TestMatrixTensorHandle(ctx);
67   TFE_TensorHandle* h1_task0 = TestMatrixTensorHandle(ctx);
68   const char remote_device_name[] =
69       "/job:localhost/replica:0/task:1/device:CPU:0";
70   auto* h0_task1 =
71       TFE_TensorHandleCopyToDevice(h0_task0, ctx, remote_device_name, status);
72   ASSERT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
73   auto* h1_task1 =
74       TFE_TensorHandleCopyToDevice(h1_task0, ctx, remote_device_name, status);
75   ASSERT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
76 
77   TFE_Op* matmul = MatMulOp(ctx, h0_task1, h1_task1);
78   TFE_OpSetDevice(matmul, remote_device_name, status);
79   EXPECT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
80 
81   TFE_TensorHandle* retvals[1];
82   int num_retvals = 1;
83   TFE_Execute(matmul, &retvals[0], &num_retvals, status);
84   EXPECT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
85 
86   TF_Tensor* t = TFE_TensorHandleResolve(retvals[0], status);
87   ASSERT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
88   float product[4] = {0};
89   EXPECT_EQ(sizeof(product), TF_TensorByteSize(t));
90   memcpy(&product[0], TF_TensorData(t), TF_TensorByteSize(t));
91   TF_DeleteTensor(t);
92   EXPECT_EQ(7, product[0]);
93   EXPECT_EQ(10, product[1]);
94   EXPECT_EQ(15, product[2]);
95   EXPECT_EQ(22, product[3]);
96 
97   TFE_DeleteTensorHandle(h0_task0);
98   TFE_DeleteTensorHandle(h1_task0);
99   TFE_DeleteTensorHandle(h0_task1);
100   TFE_DeleteTensorHandle(h1_task1);
101   TFE_DeleteTensorHandle(retvals[0]);
102 
103   TFE_DeleteOp(matmul);
104 
105   TFE_Executor* executor = TFE_ContextGetExecutorForThread(ctx);
106   TFE_ExecutorWaitForAllPendingNodes(executor, status);
107   ASSERT_EQ(TF_OK, TF_GetCode(status)) << TF_Message(status);
108   TFE_DeleteExecutor(executor);
109   TFE_DeleteContext(ctx);
110 
111   TF_DeleteStatus(status);
112 
113   // TODO(b/136478427): Figure out how to correctly shut the server down.
114   worker_server.release();
115 }
116 
TEST(CAPI,RemoteExecute)117 TEST(CAPI, RemoteExecute) { TestRemoteExecute(false); }
TEST(CAPI,RemoteExecuteAsync)118 TEST(CAPI, RemoteExecuteAsync) { TestRemoteExecute(true); }
119 
TestRemoteExecuteSilentCopiesOp(bool async,bool remote,bool remote_func_outputs=false)120 void TestRemoteExecuteSilentCopiesOp(bool async, bool remote,
121                                      bool remote_func_outputs = false) {
122   return TestRemoteExecuteSilentCopies(async, remote, /*func=*/false,
123                                        /*heavy_load_on_streaming_rpc=*/false,
124                                        remote_func_outputs);
125 }
126 
TEST(CAPI,RemoteExecuteSilentCopies)127 TEST(CAPI, RemoteExecuteSilentCopies) {
128   TestRemoteExecuteSilentCopiesOp(/*async=*/false, /*remote=*/true);
129 }
TEST(CAPI,RemoteExecuteSilentCopiesAsync)130 TEST(CAPI, RemoteExecuteSilentCopiesAsync) {
131   TestRemoteExecuteSilentCopiesOp(/*async=*/true, /*remote=*/true);
132 }
TEST(CAPI,RemoteExecuteSilentCopiesLocal)133 TEST(CAPI, RemoteExecuteSilentCopiesLocal) {
134   TestRemoteExecuteSilentCopiesOp(/*async=*/false, /*remote=*/false);
135 }
TEST(CAPI,RemoteExecuteSilentCopiesLocalAsync)136 TEST(CAPI, RemoteExecuteSilentCopiesLocalAsync) {
137   TestRemoteExecuteSilentCopiesOp(/*async=*/true, /*remote=*/false);
138 }
139 
140 }  // namespace
141