1/* Copyright 2017 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// See docs in ../ops/array_ops.cc.
17#include "tensorflow/core/kernels/snapshot_op.h"
18
19#include "tensorflow/core/framework/op_kernel.h"
20#include "tensorflow/core/framework/register_types.h"
21#include "tensorflow/core/framework/types.h"
22
23namespace tensorflow {
24typedef Eigen::ThreadPoolDevice CPUDevice;
25typedef Eigen::GpuDevice GPUDevice;
26
27template <typename Device, typename Scalar>
28class SnapshotOp : public OpKernel {
29 public:
30 explicit SnapshotOp(OpKernelConstruction* context) : OpKernel(context) {}
31
32 void Compute(OpKernelContext* context) override {
33 const Tensor& input = context->input(0);
34 Tensor* output = nullptr;
35 // Try to use buffer forwarding to avoid an explicit copy.
36 OP_REQUIRES_OK(context, context->forward_input_or_allocate_output(
37 {0}, 0, input.shape(), &output));
38 if (!output->SharesBufferWith(input)) {
39 functor::Snapshot<Device, Scalar> functor;
40 functor(context->eigen_device<Device>(), input.flat<Scalar>(),
41 output->flat<Scalar>());
42 }
43 }
44};
45
46#define REGISTER_KERNEL(TYPE) \
47 REGISTER_KERNEL_BUILDER( \
48 Name("Snapshot").Device(DEVICE_CPU).TypeConstraint<TYPE>("T"), \
49 SnapshotOp<CPUDevice, TYPE>);
50
51TF_CALL_POD_TYPES(REGISTER_KERNEL);
52#undef REGISTER_KERNEL
53
54#if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
55#define REGISTER_KERNEL(TYPE) \
56 REGISTER_KERNEL_BUILDER( \
57 Name("Snapshot").Device(DEVICE_GPU).TypeConstraint<TYPE>("T"), \
58 SnapshotOp<GPUDevice, TYPE>);
59
60TF_CALL_POD_TYPES(REGISTER_KERNEL);
61#undef REGISTER_KERNEL
62#endif
63
64
65} // namespace tensorflow
66