• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/core/framework/op.h"
16 #include "tensorflow/core/framework/op_kernel.h"
17 #include "tensorflow/core/framework/shape_inference.h"
18 
19 namespace tensorflow {
20 
21 REGISTER_OP("Double")
22     .Input("input: T")
23     .Output("doubled: T")
24     .Attr("T: {int32, float}")
__anond842aa050102(::tensorflow::shape_inference::InferenceContext* c) 25     .SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
26       c->set_output(0, c->input(0));
27       return Status::OK();
28     });
29 
30 template <typename T>
31 class DoubleOp : public OpKernel {
32  public:
DoubleOp(OpKernelConstruction * context)33   explicit DoubleOp(OpKernelConstruction* context) : OpKernel(context) {}
34 
Compute(OpKernelContext * context)35   void Compute(OpKernelContext* context) override {
36     // Grab the input tensor
37     const Tensor& input_tensor = context->input(0);
38     auto input_flat = input_tensor.flat<T>();
39 
40     // Create an output tensor
41     Tensor* output_tensor = nullptr;
42     OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
43                                                      &output_tensor));
44     auto output_flat = output_tensor->flat<T>();
45 
46     // Set all but the first element of the output tensor to 0.
47     const int N = input_flat.size();
48     for (int i = 0; i < N; i++) {
49       output_flat(i) = 2 * input_flat(i);
50     }
51   }
52 };
53 
54 REGISTER_KERNEL_BUILDER(
55     Name("Double").Device(DEVICE_CPU).TypeConstraint<int32>("T"),
56     DoubleOp<int32>);
57 REGISTER_KERNEL_BUILDER(
58     Name("Double").Device(DEVICE_CPU).TypeConstraint<float>("T"),
59     DoubleOp<float>);
60 }  // namespace tensorflow
61