1 | /* Copyright 2016 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 | #define EIGEN_USE_THREADS |
17 | |
18 | #include <algorithm> |
19 | #include <numeric> |
20 | #include <unordered_map> |
21 | #include <utility> |
22 | |
23 | #include "tensorflow/core/framework/op_kernel.h" |
24 | #include "tensorflow/core/framework/register_types.h" |
25 | #include "tensorflow/core/framework/tensor.h" |
26 | #include "tensorflow/core/framework/types.h" |
27 | #include "tensorflow/core/kernels/reshape_util.h" |
28 | #include "tensorflow/core/lib/gtl/inlined_vector.h" |
29 | #include "tensorflow/core/platform/errors.h" |
30 | |
31 | namespace tensorflow { |
32 | |
33 | using CPUDevice = Eigen::ThreadPoolDevice; |
34 | using GPUDevice = Eigen::GpuDevice; |
35 | |
36 | template <typename Device> |
37 | class SparseReshapeOp : public OpKernel { |
38 | public: |
39 | explicit SparseReshapeOp(OpKernelConstruction* context) : OpKernel(context) {} |
40 | |
41 | void Compute(OpKernelContext* context) override { |
42 | const Tensor& input_indices_in = context->input(0); |
43 | const Tensor& input_shape_in = context->input(1); |
44 | |
45 | OP_REQUIRES(context, TensorShapeUtils::IsMatrix(input_indices_in.shape()), |
46 | errors::InvalidArgument("Input must be a matrix." )); |
47 | OP_REQUIRES(context, TensorShapeUtils::IsVector(input_shape_in.shape()), |
48 | errors::InvalidArgument("Input shape must be a vector." )); |
49 | OP_REQUIRES(context, |
50 | input_indices_in.dim_size(1) == input_shape_in.dim_size(0), |
51 | errors::InvalidArgument( |
52 | "Input tensor rank must match input shape length." )); |
53 | ReshapeSparseTensor<Device>(context, context->input(0), context->input(1), |
54 | context->input(2), 0 /* output indices index */, |
55 | 1 /* output shape index */); |
56 | } |
57 | }; |
58 | |
59 | REGISTER_KERNEL_BUILDER(Name("SparseReshape" ).Device(DEVICE_CPU), |
60 | SparseReshapeOp<CPUDevice>) |
61 | |
62 | #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM |
63 | REGISTER_KERNEL_BUILDER(Name("SparseReshape" ) |
64 | .Device(DEVICE_GPU) |
65 | .HostMemory("input_shape" ) |
66 | .HostMemory("new_shape" ) |
67 | .HostMemory("output_shape" ), |
68 | SparseReshapeOp<GPUDevice>) |
69 | #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM |
70 | |
71 | } // namespace tensorflow |
72 | |