-
Notifications
You must be signed in to change notification settings - Fork 74k
/
broadcast_to_op.cc
168 lines (138 loc) · 6.51 KB
/
broadcast_to_op.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#define EIGEN_USE_THREADS
#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
(defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM)
#define EIGEN_USE_GPU
#endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
#if !defined(PLUGGABLE_DEVICE_SUPPORTED_MACOS) && defined(__APPLE__) && \
!defined(ANDROID) && !defined(__ANDROID__) && \
(!defined(TARGET_OS_IOS) || !TARGET_OS_IOS)
#define PLUGGABLE_DEVICE_SUPPORTED_MACOS 1
#endif
#include "tensorflow/core/kernels/broadcast_to_op.h"
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/register_types.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/tensor_util.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/util/bcast.h"
namespace tensorflow {
typedef Eigen::ThreadPoolDevice CPUDevice;
typedef Eigen::GpuDevice GPUDevice;
template <typename Device, typename T>
class BroadcastToOp : public OpKernel {
public:
explicit BroadcastToOp(OpKernelConstruction* ctx) : OpKernel(ctx) {}
void Compute(OpKernelContext* ctx) override {
const Tensor& input_tensor = ctx->input(0);
const TensorShape& input_shape = input_tensor.shape();
const Tensor& shape_tensor = ctx->input(1);
TensorShape output_shape;
OP_REQUIRES_OK(ctx, tensor::MakeShape(shape_tensor, &output_shape));
// Handle copy.
if (output_shape == input_shape) {
ctx->set_output(0, input_tensor);
return;
}
OP_REQUIRES(ctx, input_shape.dims() <= output_shape.dims(),
errors::InvalidArgument(
"Rank of input (", input_shape.dims(),
") must be no greater than rank of output shape (",
output_shape.dims(), ")."));
Tensor* output_tensor = nullptr;
OP_REQUIRES_OK(ctx, ctx->allocate_output(0, output_shape, &output_tensor));
// Handle broadcast from Scalar.
const Device& device = ctx->eigen_device<Device>();
if (input_shape.dims() == 0) {
functor::FillFunctor<Device, T>()(device, output_tensor->flat<T>(),
input_tensor.scalar<T>());
return;
}
// Check whether the broadcast is valid.
BCast bcast(BCast::FromShape(input_shape), BCast::FromShape(output_shape),
/*fewer_dims_optimization=*/true);
OP_REQUIRES(ctx, bcast.IsValid(),
errors::InvalidArgument(
"Incompatible shapes: ", input_shape.DebugString(), " vs. ",
output_shape.DebugString()));
OP_REQUIRES(ctx, BCast::ToShape(bcast.output_shape()) == output_shape,
errors::InvalidArgument("Unable to broadcast tensor of shape ",
input_shape, " to tensor of shape ",
output_shape));
// Handle empty case.
if (output_shape.num_elements() == 0) {
return;
}
functor::BroadcastTo<Device, T>()(device, ctx, *output_tensor, output_shape,
input_tensor, input_shape, bcast);
}
};
// As tensor::MakeShape is able to handle both DT_INT32 and DT_INT64,
// no need to have TypeConstraint for `Tidx`
#define REGISTER_KERNEL(type) \
REGISTER_KERNEL_BUILDER( \
Name("BroadcastTo").Device(DEVICE_CPU).TypeConstraint<type>("T"), \
BroadcastToOp<CPUDevice, type>);
TF_CALL_ALL_TYPES(REGISTER_KERNEL);
TF_CALL_float8_e5m2(REGISTER_KERNEL);
TF_CALL_float8_e4m3fn(REGISTER_KERNEL);
#undef REGISTER_KERNEL
#if (defined(GOOGLE_CUDA) && GOOGLE_CUDA) || \
(defined(TENSORFLOW_USE_ROCM) && TENSORFLOW_USE_ROCM)
namespace functor {
#define DECLARE_GPU_TEMPLATE(Type) \
template <> \
void BroadcastTo<GPUDevice, Type>::operator()( \
const GPUDevice& d, OpKernelContext* ctx, Tensor& output, \
const TensorShape& output_shape, const Tensor& input, \
const TensorShape& input_shape, const BCast& bcast) const; \
extern template struct BroadcastTo<GPUDevice, Type>;
TF_CALL_GPU_ALL_TYPES(DECLARE_GPU_TEMPLATE);
TF_CALL_int64(DECLARE_GPU_TEMPLATE);
TF_CALL_float8_e5m2(DECLARE_GPU_TEMPLATE);
TF_CALL_float8_e4m3fn(DECLARE_GPU_TEMPLATE);
#undef DECLARE_GPU_KERNEL
} // namespace functor
#define REGISTER_KERNEL(type) \
REGISTER_KERNEL_BUILDER(Name("BroadcastTo") \
.Device(DEVICE_GPU) \
.TypeConstraint<type>("T") \
.HostMemory("shape"), \
BroadcastToOp<GPUDevice, type>);
TF_CALL_GPU_ALL_TYPES(REGISTER_KERNEL);
TF_CALL_int64(REGISTER_KERNEL);
TF_CALL_float8_e5m2(REGISTER_KERNEL);
TF_CALL_float8_e4m3fn(REGISTER_KERNEL);
#undef REGISTER_KERNEL
// A special GPU kernel for int32.
// TODO(b/25387198): Also enable int32 in device memory. This kernel
// registration requires all int32 inputs and outputs to be in host memory.
REGISTER_KERNEL_BUILDER(Name("BroadcastTo")
.Device(DEVICE_GPU)
.TypeConstraint<int32>("T")
.HostMemory("input")
.HostMemory("shape")
.HostMemory("output"),
BroadcastToOp<CPUDevice, int32>);
#endif
#if defined(PLUGGABLE_DEVICE_SUPPORTED_MACOS)
REGISTER_KERNEL_BUILDER(Name("BroadcastTo")
.Device(DEVICE_DEFAULT)
.TypeConstraint<int32>("T")
.HostMemory("input")
.HostMemory("shape")
.HostMemory("output"),
BroadcastToOp<CPUDevice, int32>);
#endif
} // namespace tensorflow