Home | History | Annotate | Download | only in kernels
      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 
    106 TF_CALL_NUMBER_TYPES_NO_INT32(REGISTER_GPU_KERNEL);
    107 REGISTER_GPU_KERNEL(Variant);
    108 
    109 #undef REGISTER_GPU_KERNEL
    110 
    111 #if GOOGLE_CUDA
    112 // A special GPU kernel for int32 and bool.
    113 // TODO(b/25387198): Also enable int32 in device memory. This kernel
    114 // registration requires all int32 inputs and outputs to be in host memory.
    115 #define REGISTER_GPU_HOST_KERNEL(type)                    \
    116   REGISTER_KERNEL_BUILDER(Name("Identity")                \
    117                               .Device(DEVICE_GPU)         \
    118                               .HostMemory("input")        \
    119                               .HostMemory("output")       \
    120                               .TypeConstraint<type>("T"), \
    121                           IdentityOp);                    \
    122   REGISTER_KERNEL_BUILDER(Name("RefIdentity")             \
    123                               .Device(DEVICE_GPU)         \
    124                               .HostMemory("input")        \
    125                               .HostMemory("output")       \
    126                               .TypeConstraint<type>("T"), \
    127                           IdentityOp)
    128 
    129 REGISTER_GPU_HOST_KERNEL(int32);
    130 REGISTER_GPU_HOST_KERNEL(bool);
    131 REGISTER_GPU_HOST_KERNEL(string);
    132 
    133 #undef REGISTER_GPU_HOST_KERNEL
    134 
    135 #endif
    136 
    137 }  // namespace tensorflow
    138