aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/kernels/sparse_conditional_accumulator_op.cc
blob: 1e542a26a78bd340d8b60e7accf202bb6432584a (plain)
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
/* 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_base_op.h"
#include "tensorflow/core/kernels/sparse_conditional_accumulator.h"

namespace tensorflow {

/**
 * Defines a SparseConditionalAccumulatorOp, which constructs a
 * SparseConditionalAccumulator and returns its handle.
 */
template <typename Device, typename T>
class SparseConditionalAccumulatorOp : public ConditionalAccumulatorBaseOp {
 public:
  explicit SparseConditionalAccumulatorOp(OpKernelConstruction* context)
      : ConditionalAccumulatorBaseOp(context) {}

 protected:
  Creator GetCreator() const override {
    return [this](ConditionalAccumulatorBase** ret) {
      SparseConditionalAccumulator<Device, T>* accumulator =
          new SparseConditionalAccumulator<Device, T>(
              dtype_, shape_, cinfo_.name(), reduction_type_);
      *ret = accumulator;
      return Status::OK();
    };
  }

  TF_DISALLOW_COPY_AND_ASSIGN(SparseConditionalAccumulatorOp);
};

#define REGISTER_KERNELS(type, dev)                            \
  REGISTER_KERNEL_BUILDER(Name("SparseConditionalAccumulator") \
                              .Device(DEVICE_##dev)            \
                              .TypeConstraint<type>("dtype"),  \
                          SparseConditionalAccumulatorOp<dev##Device, type>)

#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

/**
 * Defines a SparseAccumulateGradientOp, the execution of which adds a gradient
 * to the given SparseConditionalAccumulator.
 */
class SparseAccumulatorApplyGradientOp
    : public ConditionalAccumulatorBaseApplyGradientOp {
 public:
  explicit SparseAccumulatorApplyGradientOp(OpKernelConstruction* context)
      : ConditionalAccumulatorBaseApplyGradientOp(context) {}

 protected:
  void CheckSignature(OpKernelContext* ctx,
                      ConditionalAccumulatorBase* accumulator) override {
    // Check input signature
    DataTypeVector expected_inputs = {DT_STRING_REF, DT_INT64, DT_INT64};
    expected_inputs.push_back(accumulator->dtype());
    expected_inputs.push_back(DT_INT64);
    OP_REQUIRES_OK(ctx, ctx->MatchSignature(expected_inputs, {}));
  }

 private:
  TF_DISALLOW_COPY_AND_ASSIGN(SparseAccumulatorApplyGradientOp);
};

REGISTER_KERNEL_BUILDER(
    Name("SparseAccumulatorApplyGradient").Device(DEVICE_CPU),
    SparseAccumulatorApplyGradientOp);

/**
 * Defines a SparseAccumulatorTakeGradientOp, the execution of which returns the
 * average sparse gradient accumulated by the given ConditionalAccumulator.
 */
class SparseAccumulatorTakeGradientOp
    : public ConditionalAccumulatorBaseTakeGradientOp {
 public:
  explicit SparseAccumulatorTakeGradientOp(OpKernelConstruction* context)
      : ConditionalAccumulatorBaseTakeGradientOp(context) {}

 protected:
  void CheckSignature(OpKernelContext* ctx,
                      ConditionalAccumulatorBase* accumulator,
                      DoneCallback callback) override {
    // Check signature
    OP_REQUIRES_OK_ASYNC(
        ctx,
        ctx->MatchSignature({DT_STRING_REF, DT_INT32},
                            {DT_INT64, accumulator->dtype(), DT_INT64}),
        callback);
  }

 private:
  TF_DISALLOW_COPY_AND_ASSIGN(SparseAccumulatorTakeGradientOp);
};

REGISTER_KERNEL_BUILDER(
    Name("SparseAccumulatorTakeGradient").Device(DEVICE_CPU),
    SparseAccumulatorTakeGradientOp);

}  // namespace tensorflow