-
Notifications
You must be signed in to change notification settings - Fork 74k
/
conditional_accumulator_op.cc
212 lines (171 loc) · 7.16 KB
/
conditional_accumulator_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* Copyright 2015 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
#include "tensorflow/core/kernels/conditional_accumulator.h"
#include "tensorflow/core/kernels/conditional_accumulator_base_op.h"
namespace tensorflow {
/**
* Defines a ConditionalAccumulatorOp, which constructs a ConditionalAccumulator
* and returns its handle.
*/
template <typename Device, typename T>
class ConditionalAccumulatorOp : public ConditionalAccumulatorBaseOp {
public:
explicit ConditionalAccumulatorOp(OpKernelConstruction* context)
: ConditionalAccumulatorBaseOp(context) {}
protected:
Creator GetCreator() const override {
return [this](ConditionalAccumulatorBase** ret) {
ConditionalAccumulator<Device, T>* accumulator =
new ConditionalAccumulator<Device, T>(dtype_, shape_, cinfo_.name(),
reduction_type_);
*ret = accumulator;
return OkStatus();
};
}
Status CheckSignature(OpKernelContext* ctx) override {
TF_RETURN_IF_ERROR(ctx->MatchSignature({}, {DT_STRING_REF}));
return OkStatus();
}
void SetHandleToOutput(OpKernelContext* ctx)
TF_SHARED_LOCKS_REQUIRED(mu_) override {
ctx->set_output_ref(0, &mu_, &accumulator_);
}
TF_DISALLOW_COPY_AND_ASSIGN(ConditionalAccumulatorOp);
};
#define REGISTER_KERNELS(type, dev) \
REGISTER_KERNEL_BUILDER(Name("ConditionalAccumulator") \
.Device(DEVICE_##dev) \
.TypeConstraint<type>("dtype"), \
ConditionalAccumulatorOp<dev##Device, type>)
// Resource conditional accumulator
template <typename Device, typename T>
class ResourceConditionalAccumulatorOp : public ConditionalAccumulatorBaseOp {
public:
explicit ResourceConditionalAccumulatorOp(OpKernelConstruction* context)
: ConditionalAccumulatorBaseOp(context) {}
protected:
Creator GetCreator() const override {
return [this](ConditionalAccumulatorBase** ret) {
ConditionalAccumulator<Device, T>* accumulator =
new ConditionalAccumulator<Device, T>(dtype_, shape_, cinfo_.name(),
reduction_type_);
*ret = accumulator;
return OkStatus();
};
}
Status CheckSignature(OpKernelContext* ctx) override {
TF_RETURN_IF_ERROR(ctx->MatchSignature({}, {DT_RESOURCE}));
return OkStatus();
}
void SetHandleToOutput(OpKernelContext* ctx)
TF_SHARED_LOCKS_REQUIRED(mu_) override {
auto h = accumulator_.template flat<tstring>();
h(0) = cinfo_.container();
h(1) = cinfo_.name();
OP_REQUIRES_OK(ctx, MakeResourceHandleToOutput(
ctx, 0, cinfo_.container(), cinfo_.name(),
TypeIndex::Make<ConditionalAccumulatorBase>()));
}
TF_DISALLOW_COPY_AND_ASSIGN(ResourceConditionalAccumulatorOp);
};
#define REGISTER_RESOURCE_KERNELS(type, dev) \
REGISTER_KERNEL_BUILDER(Name("ResourceConditionalAccumulator") \
.Device(DEVICE_##dev) \
.TypeConstraint<type>("dtype"), \
ResourceConditionalAccumulatorOp<dev##Device, type>)
// End of Resource conditional accumulator
#define REGISTER_KERNELS_CPU(type) REGISTER_KERNELS(type, CPU)
TF_CALL_half(REGISTER_KERNELS_CPU);
TF_CALL_float(REGISTER_KERNELS_CPU);
TF_CALL_double(REGISTER_KERNELS_CPU);
#undef REGISTER_KERNELS_CPU
#undef REGISTER_KERNELS
#define REGISTER_RESOURCE_KERNELS_CPU(type) REGISTER_RESOURCE_KERNELS(type, CPU)
TF_CALL_half(REGISTER_RESOURCE_KERNELS_CPU);
TF_CALL_float(REGISTER_RESOURCE_KERNELS_CPU);
TF_CALL_double(REGISTER_RESOURCE_KERNELS_CPU);
#undef REGISTER_KERNELS_CPU
#undef REGISTER_KERNELS
/**
* Defines a AccumulateGradientOp, the execution of which adds a gradient to the
* given ConditionalAccumulator.
*/
class AccumulatorApplyGradientOp
: public ConditionalAccumulatorBaseApplyGradientOp {
public:
explicit AccumulatorApplyGradientOp(OpKernelConstruction* context)
: ConditionalAccumulatorBaseApplyGradientOp(context) {}
DataTypeVector GetExpectedInputs(
ConditionalAccumulatorBase* accumulator) override {
DataTypeVector expected_inputs;
expected_inputs = {DT_STRING_REF, DT_INT64};
expected_inputs.push_back(accumulator->dtype());
return expected_inputs;
}
private:
TF_DISALLOW_COPY_AND_ASSIGN(AccumulatorApplyGradientOp);
};
REGISTER_KERNEL_BUILDER(Name("AccumulatorApplyGradient").Device(DEVICE_CPU),
AccumulatorApplyGradientOp);
class ResourceAccumulatorApplyGradientOp
: public ConditionalAccumulatorBaseApplyGradientOp {
public:
explicit ResourceAccumulatorApplyGradientOp(OpKernelConstruction* context)
: ConditionalAccumulatorBaseApplyGradientOp(context) {}
DataTypeVector GetExpectedInputs(
ConditionalAccumulatorBase* accumulator) override {
DataTypeVector expected_inputs;
expected_inputs = {DT_RESOURCE, DT_INT64};
expected_inputs.push_back(accumulator->dtype());
return expected_inputs;
}
private:
TF_DISALLOW_COPY_AND_ASSIGN(ResourceAccumulatorApplyGradientOp);
};
REGISTER_KERNEL_BUILDER(
Name("ResourceAccumulatorApplyGradient").Device(DEVICE_CPU),
ResourceAccumulatorApplyGradientOp);
/**
* Defines a ConditionalAccumulatorBaseTakeGradientOp, the execution of which
* returns the average gradient accumulated by the given ConditionalAccumulator.
*/
class AccumulatorTakeGradientOp
: public ConditionalAccumulatorBaseTakeGradientOp {
public:
explicit AccumulatorTakeGradientOp(OpKernelConstruction* context)
: ConditionalAccumulatorBaseTakeGradientOp(context) {}
DataTypeVector GetExpectedInputs(
ConditionalAccumulatorBase* accumulator) override {
return {DT_STRING_REF, DT_INT32};
}
private:
TF_DISALLOW_COPY_AND_ASSIGN(AccumulatorTakeGradientOp);
};
REGISTER_KERNEL_BUILDER(Name("AccumulatorTakeGradient").Device(DEVICE_CPU),
AccumulatorTakeGradientOp);
class ResourceAccumulatorTakeGradientOp
: public ConditionalAccumulatorBaseTakeGradientOp {
public:
explicit ResourceAccumulatorTakeGradientOp(OpKernelConstruction* context)
: ConditionalAccumulatorBaseTakeGradientOp(context) {}
DataTypeVector GetExpectedInputs(
ConditionalAccumulatorBase* accumulator) override {
return {DT_RESOURCE, DT_INT32};
}
private:
TF_DISALLOW_COPY_AND_ASSIGN(ResourceAccumulatorTakeGradientOp);
};
REGISTER_KERNEL_BUILDER(
Name("ResourceAccumulatorTakeGradient").Device(DEVICE_CPU),
ResourceAccumulatorTakeGradientOp);
} // namespace tensorflow