1 /* Copyright 2020 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 "tensorflow/c/experimental/op_handler/internal.h"
17
18 #include "absl/types/span.h"
19 #include "tensorflow/c/eager/c_api_unified_experimental.h"
20 #include "tensorflow/c/eager/c_api_unified_experimental_internal.h"
21 #include "tensorflow/core/platform/errors.h"
22 #include "tensorflow/core/platform/test.h"
23
24 namespace tensorflow {
25
26 class TestOpHandler : public OpHandler {
27 public:
TestOpHandler()28 TestOpHandler() : last_operation_(new std::string("")) {}
Execute(OpHandlerOperation * operation,absl::Span<AbstractTensorHandle * > retvals,int * num_retvals)29 Status Execute(OpHandlerOperation* operation,
30 absl::Span<AbstractTensorHandle*> retvals,
31 int* num_retvals) override {
32 CHECK(operation->get_handler() == this);
33 *last_operation_ = operation->Name();
34 operation->set_handler(next_handler_.get());
35 return operation->Execute(retvals, num_retvals);
36 }
Merge(OpHandler * next_handler,core::RefCountPtr<OpHandler> & merged_handler)37 Status Merge(OpHandler* next_handler,
38 core::RefCountPtr<OpHandler>& merged_handler) override {
39 merged_handler.reset(new TestOpHandler(next_handler, last_operation_));
40 return Status::OK();
41 }
42
43 core::RefCountPtr<OpHandler> next_handler_ = nullptr;
44 // Shared between merged handlers of this type.
45 std::shared_ptr<std::string> last_operation_;
46
47 private:
TestOpHandler(OpHandler * next_handler,std::shared_ptr<std::string> last_operation)48 TestOpHandler(OpHandler* next_handler,
49 std::shared_ptr<std::string> last_operation)
50 : next_handler_(next_handler), last_operation_(last_operation) {
51 next_handler->Ref();
52 }
53 };
54
TEST(INTERNAL_TEST,UseOpHandler)55 TEST(INTERNAL_TEST, UseOpHandler) {
56 std::unique_ptr<TF_Status, decltype(&TF_DeleteStatus)> status(
57 TF_NewStatus(), TF_DeleteStatus);
58 std::unique_ptr<TFE_ContextOptions, decltype(&TFE_DeleteContextOptions)> opts(
59 TFE_NewContextOptions(), TFE_DeleteContextOptions);
60 std::unique_ptr<TF_ExecutionContext, decltype(&TF_DeleteExecutionContext)>
61 c_ctx(TF_NewEagerExecutionContext(opts.get(), status.get()),
62 TF_DeleteExecutionContext);
63 OpHandlerContext ctx(unwrap(c_ctx.get()));
64 core::RefCountPtr<TestOpHandler> outer_handler(new TestOpHandler());
65 core::RefCountPtr<TestOpHandler> inner_handler(new TestOpHandler());
66 ctx.set_default_handler(outer_handler.get());
67 OpHandlerOperationPtr op(ctx.CreateOperation());
68 Status s = op->Reset("NoOp", "");
69 ASSERT_EQ(errors::OK, s.code()) << s.error_message();
70 std::vector<AbstractTensorHandle*> retvals;
71 int num_retvals = 0;
72 EXPECT_EQ("", *outer_handler->last_operation_);
73 s = op->Execute(absl::Span<AbstractTensorHandle*>(retvals), &num_retvals);
74 ASSERT_EQ(errors::OK, s.code()) << s.error_message();
75
76 EXPECT_EQ("NoOp", *outer_handler->last_operation_);
77 *outer_handler->last_operation_ = "";
78 EXPECT_EQ("", *inner_handler->last_operation_);
79
80 // This op executes on both handlers, changing the state of `inner_handler`
81 // since the handler has decided to preserve that state across merges.
82 core::RefCountPtr<OpHandler> merged;
83 s = inner_handler->Merge(outer_handler.get(), merged);
84 ctx.set_default_handler(merged.get());
85 op.reset(ctx.CreateOperation());
86 s = op->Reset("NoOp", "");
87 ASSERT_EQ(errors::OK, s.code()) << s.error_message();
88 s = op->Execute(absl::Span<AbstractTensorHandle*>(retvals), &num_retvals);
89 ASSERT_EQ(errors::OK, s.code()) << s.error_message();
90 EXPECT_EQ("NoOp", *inner_handler->last_operation_);
91 EXPECT_EQ("NoOp", *outer_handler->last_operation_);
92
93 inner_handler.reset();
94 outer_handler.reset();
95 op.reset(ctx.CreateOperation());
96 s = op->Reset("NoOp", "");
97 ASSERT_EQ(errors::OK, s.code()) << s.error_message();
98 s = op->Execute(absl::Span<AbstractTensorHandle*>(retvals), &num_retvals);
99 ASSERT_EQ(errors::OK, s.code()) << s.error_message();
100 }
101
102 } // namespace tensorflow
103