• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 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 // See docs in ../ops/array_ops.cc.
17 #include "tensorflow/core/kernels/identity_op.h"
18 
19 #include "tensorflow/core/framework/op_kernel.h"
20 #include "tensorflow/core/framework/register_types.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/framework/types.h"
23 
24 namespace tensorflow {
25 
26 REGISTER_KERNEL_BUILDER(Name("Identity").Device(DEVICE_CPU), IdentityOp);
27 // StopGradient does the same thing as Identity, but has a different
28 // gradient registered.
29 REGISTER_KERNEL_BUILDER(Name("StopGradient").Device(DEVICE_CPU), IdentityOp);
30 // PreventGradient does the same thing as Identity, but has a NO
31 // gradient registered.
32 REGISTER_KERNEL_BUILDER(Name("PreventGradient").Device(DEVICE_CPU), IdentityOp);
33 
34 // PlaceholderWithDefault does the same thing as Identity, but has a
35 // different shape function (and constant value function) registered.
36 REGISTER_KERNEL_BUILDER(Name("PlaceholderWithDefault").Device(DEVICE_CPU),
37                         IdentityOp);
38 
39 REGISTER_KERNEL_BUILDER(Name("RefIdentity").Device(DEVICE_CPU), IdentityOp);
40 
41 // Identity op for gradients debugging in TensorFlow Debugger (hidden op in
42 // Python).
43 REGISTER_KERNEL_BUILDER(Name("DebugGradientIdentity").Device(DEVICE_CPU),
44                         IdentityOp);
45 REGISTER_KERNEL_BUILDER(Name("DebugGradientRefIdentity").Device(DEVICE_CPU),
46                         IdentityOp);
47 
48 #if TENSORFLOW_USE_SYCL
49 #define REGISTER_SYCL_KERNEL(type)                                           \
50   REGISTER_KERNEL_BUILDER(                                                   \
51       Name("Identity").Device(DEVICE_SYCL).TypeConstraint<type>("T"),        \
52       IdentityOp);                                                           \
53   REGISTER_KERNEL_BUILDER(                                                   \
54       Name("PreventGradient").Device(DEVICE_SYCL).TypeConstraint<type>("T"), \
55       IdentityOp);                                                           \
56   REGISTER_KERNEL_BUILDER(                                                   \
57       Name("RefIdentity").Device(DEVICE_SYCL).TypeConstraint<type>("T"),     \
58       IdentityOp);                                                           \
59   REGISTER_KERNEL_BUILDER(                                                   \
60       Name("StopGradient").Device(DEVICE_SYCL).TypeConstraint<type>("T"),    \
61       IdentityOp)
62 
63 TF_CALL_NUMBER_TYPES_NO_INT32(REGISTER_SYCL_KERNEL);
64 
65 #undef REGISTER_SYCL_KERNEL
66 
67 #define REGISTER_SYCL_HOST_KERNEL(type)                   \
68   REGISTER_KERNEL_BUILDER(Name("Identity")                \
69                               .Device(DEVICE_SYCL)        \
70                               .HostMemory("input")        \
71                               .HostMemory("output")       \
72                               .TypeConstraint<type>("T"), \
73                           IdentityOp);                    \
74   REGISTER_KERNEL_BUILDER(Name("RefIdentity")             \
75                               .Device(DEVICE_SYCL)        \
76                               .HostMemory("input")        \
77                               .HostMemory("output")       \
78                               .TypeConstraint<type>("T"), \
79                           IdentityOp)
80 
81 REGISTER_SYCL_HOST_KERNEL(int32);
82 REGISTER_SYCL_HOST_KERNEL(bool);
83 
84 #undef REGISTER_SYCL_HOST_KERNEL
85 
86 #endif  // TENSORFLOW_USE_SYCL
87 
88 #define REGISTER_GPU_KERNEL(type)                                           \
89   REGISTER_KERNEL_BUILDER(                                                  \
90       Name("Identity").Device(DEVICE_GPU).TypeConstraint<type>("T"),        \
91       IdentityOp);                                                          \
92   REGISTER_KERNEL_BUILDER(                                                  \
93       Name("PreventGradient").Device(DEVICE_GPU).TypeConstraint<type>("T"), \
94       IdentityOp);                                                          \
95   REGISTER_KERNEL_BUILDER(                                                  \
96       Name("RefIdentity").Device(DEVICE_GPU).TypeConstraint<type>("T"),     \
97       IdentityOp);                                                          \
98   REGISTER_KERNEL_BUILDER(                                                  \
99       Name("StopGradient").Device(DEVICE_GPU).TypeConstraint<type>("T"),    \
100       IdentityOp);                                                          \
101   REGISTER_KERNEL_BUILDER(Name("DebugGradientIdentity")                     \
102                               .Device(DEVICE_GPU)                           \
103                               .TypeConstraint<type>("T"),                   \
104                           IdentityOp);                                      \
105   REGISTER_KERNEL_BUILDER(Name("PlaceholderWithDefault")                    \
106                               .Device(DEVICE_GPU)                           \
107                               .TypeConstraint<type>("dtype"),               \
108                           IdentityOp)
109 
110 TF_CALL_NUMBER_TYPES_NO_INT32(REGISTER_GPU_KERNEL);
111 REGISTER_GPU_KERNEL(Variant);
112 
113 #undef REGISTER_GPU_KERNEL
114 
115 #if GOOGLE_CUDA
116 // A special GPU kernel for int32 and bool.
117 // TODO(b/25387198): Also enable int32 in device memory. This kernel
118 // registration requires all int32 inputs and outputs to be in host memory.
119 #define REGISTER_GPU_HOST_KERNEL(type)                        \
120   REGISTER_KERNEL_BUILDER(Name("Identity")                    \
121                               .Device(DEVICE_GPU)             \
122                               .HostMemory("input")            \
123                               .HostMemory("output")           \
124                               .TypeConstraint<type>("T"),     \
125                           IdentityOp);                        \
126   REGISTER_KERNEL_BUILDER(Name("RefIdentity")                 \
127                               .Device(DEVICE_GPU)             \
128                               .HostMemory("input")            \
129                               .HostMemory("output")           \
130                               .TypeConstraint<type>("T"),     \
131                           IdentityOp);                        \
132   REGISTER_KERNEL_BUILDER(Name("StopGradient")                \
133                               .Device(DEVICE_GPU)             \
134                               .HostMemory("input")            \
135                               .HostMemory("output")           \
136                               .TypeConstraint<type>("T"),     \
137                           IdentityOp);                        \
138   REGISTER_KERNEL_BUILDER(Name("PlaceholderWithDefault")      \
139                               .Device(DEVICE_GPU)             \
140                               .HostMemory("input")            \
141                               .HostMemory("output")           \
142                               .TypeConstraint<type>("dtype"), \
143                           IdentityOp)
144 
145 REGISTER_GPU_HOST_KERNEL(int32);
146 REGISTER_GPU_HOST_KERNEL(bool);
147 REGISTER_GPU_HOST_KERNEL(string);
148 REGISTER_GPU_HOST_KERNEL(ResourceHandle);
149 
150 #undef REGISTER_GPU_HOST_KERNEL
151 
152 #endif
153 
154 }  // namespace tensorflow
155