1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/core/framework/op.h"
17#include "tensorflow/core/framework/op_kernel.h"
18
19using namespace tensorflow;  // NOLINT(build/namespaces)
20
21REGISTER_OP("AddOne")
22    .Input("input: int32")
23    .Output("output: int32")
24    .Doc(R"doc(
25Adds 1 to all elements of the tensor.
26
27output: A Tensor.
28  output = input + 1
29)doc");
30
31void AddOneKernelLauncher(const int* in, const int N, int* out);
32
33class AddOneOp : public OpKernel {
34 public:
35  explicit AddOneOp(OpKernelConstruction* context) : OpKernel(context) {}
36
37  void Compute(OpKernelContext* context) override {
38    // Grab the input tensor
39    const Tensor& input_tensor = context->input(0);
40    auto input = input_tensor.flat<int32>();
41
42    // Create an output tensor
43    Tensor* output_tensor = nullptr;
44    OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
45                                                     &output_tensor));
46    auto output = output_tensor->template flat<int32>();
47
48    // Set all but the first element of the output tensor to 0.
49    const int N = input.size();
50    // Call the cuda kernel launcher
51    AddOneKernelLauncher(input.data(), N, output.data());
52  }
53};
54
55REGISTER_KERNEL_BUILDER(Name("AddOne").Device(DEVICE_GPU), AddOneOp);
56