1 /* Copyright 2018 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/compiler/tf2xla/xla_op_registry.h"
17 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
18 #include "tensorflow/core/platform/test.h"
19
20 namespace tensorflow {
21 namespace {
22
23 // This test is to verify the correctness of XLA op registration with specific
24 // backend overrides.
25
26 // A dummy backend-specific OpKernel for CPU.
27 class DummyCPUOp : public XlaOpKernel {
28 public:
DummyCPUOp(OpKernelConstruction * ctx)29 explicit DummyCPUOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
Compile(XlaOpKernelContext * ctx)30 void Compile(XlaOpKernelContext* ctx) override {
31 ctx->SetOutput(0, ctx->Input(0));
32 }
33 };
34
35 // A dummy generic OpKernel for all backends.
36 class DummyGenericOp : public XlaOpKernel {
37 public:
DummyGenericOp(OpKernelConstruction * ctx)38 explicit DummyGenericOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
Compile(XlaOpKernelContext * ctx)39 void Compile(XlaOpKernelContext* ctx) override {
40 ctx->SetOutput(0, ctx->Input(0));
41 }
42 };
43
44 REGISTER_OP("DummyDuplicateOp")
45 .Attr("T: {float, int32}")
46 .Input("input: int32")
47 .Output("output: int32")
48 .Doc(R"doc(
49 A dummy Op.
50
51 input: dummy input.
52 output: dummy output.
53 )doc");
54
55 // Register the DummyCPUOp kernel for CPU with type INT32.
56 REGISTER_XLA_OP(Name("DummyDuplicateOp")
57 .Device(DEVICE_CPU_XLA_JIT)
58 .TypeConstraint("T", DT_INT32),
59 DummyCPUOp);
60 // Register the DummyGeneric kernel for all registered device (except CPU since
61 // it is already registered), with type FLOAT.
62 REGISTER_XLA_OP(Name("DummyDuplicateOp").TypeConstraint("T", DT_FLOAT),
63 DummyGenericOp);
64
65 // Test the correctness of registered kernels. The kernel registered for CPU
66 // should have type INT32 while all other kernels should have type FLOAT.
TEST(XlaOpRegistryTest,XlaOpRegistrationWithOverride)67 TEST(XlaOpRegistryTest, XlaOpRegistrationWithOverride) {
68 XlaOpRegistry::RegisterCompilationKernels();
69 auto registered_kernels = GetAllRegisteredKernels().kernel();
70 for (const auto& kernels : registered_kernels) {
71 if (kernels.op() == "DummyDuplicateOp") {
72 EXPECT_EQ(kernels.constraint_size(), 1);
73 EXPECT_EQ(kernels.constraint(0).name(), "T");
74 if (kernels.device_type() == "XLA_CPU_JIT") {
75 EXPECT_EQ(kernels.constraint(0).allowed_values().list().type(0),
76 DT_INT32);
77 } else {
78 EXPECT_EQ(kernels.constraint(0).allowed_values().list().type(0),
79 DT_FLOAT);
80 }
81 }
82 }
83 }
84
85 // A dummy generic OpKernel for all backends.
86 class DummyInfeasibleTypeConstraintOp : public XlaOpKernel {
87 public:
DummyInfeasibleTypeConstraintOp(OpKernelConstruction * ctx)88 explicit DummyInfeasibleTypeConstraintOp(OpKernelConstruction* ctx)
89 : XlaOpKernel(ctx) {}
Compile(XlaOpKernelContext * ctx)90 void Compile(XlaOpKernelContext* ctx) override {
91 LOG(FATAL) << "unreachable";
92 }
93 };
94
95 REGISTER_OP("DummyInfeasibleTypeConstraintOp")
96 .Attr("T: {float, string}")
97 .Input("input: T")
98 .Output("output: T")
99 .Doc(R"doc(
100 A dummy Op.
101
102 input: dummy input.
103 output: dummy output.
104 )doc");
105 REGISTER_XLA_OP(
106 Name("DummyInfeasibleTypeConstraintOp").TypeConstraint("T", DT_STRING),
107 DummyInfeasibleTypeConstraintOp);
108
TEST(XlaOpRegistryTest,OpWithInfeasibleTypeConstraintIsNotRegistered)109 TEST(XlaOpRegistryTest, OpWithInfeasibleTypeConstraintIsNotRegistered) {
110 XlaOpRegistry::RegisterCompilationKernels();
111 auto registered_kernels = GetAllRegisteredKernels().kernel();
112 for (const auto& kernels : registered_kernels) {
113 // The operator should not be registered.
114 EXPECT_NE(kernels.op(), "DummyInfeasibleTypeConstraintOp");
115 }
116 }
117
118 } // namespace
119 } // namespace tensorflow
120