aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/kernels/data/filter_dataset_op.cc
blob: bf0aecaf3c114643f7b963eafc2386cd4308cc9b (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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* 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.
==============================================================================*/
#include "tensorflow/core/common_runtime/function.h"
#include "tensorflow/core/framework/partial_tensor_shape.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/kernels/data/captured_function.h"
#include "tensorflow/core/kernels/data/dataset.h"
#include "tensorflow/core/lib/gtl/cleanup.h"
#include "tensorflow/core/lib/random/random.h"

namespace tensorflow {
namespace data {
namespace {

// See documentation in ../ops/dataset_ops.cc for a high-level
// description of the following op.

class FilterDatasetOp : public UnaryDatasetOpKernel {
 public:
  explicit FilterDatasetOp(OpKernelConstruction* ctx)
      : UnaryDatasetOpKernel(ctx),
        graph_def_version_(ctx->graph_def_version()) {
    OP_REQUIRES_OK(ctx, ctx->GetAttr("predicate", &func_));
  }

  void MakeDataset(OpKernelContext* ctx, DatasetBase* input,
                   DatasetBase** output) override {
    OpInputList inputs;
    OP_REQUIRES_OK(ctx, ctx->input_list("other_arguments", &inputs));
    std::vector<Tensor> other_arguments;
    other_arguments.reserve(inputs.size());
    for (const Tensor& t : inputs) {
      other_arguments.push_back(t);
    }

    FunctionLibraryRuntime::Handle pred_handle;
    OP_REQUIRES_OK(ctx,
                   ctx->function_library()->Instantiate(
                       func_.name(), AttrSlice(&func_.attr()), &pred_handle));
    auto cleanup = gtl::MakeCleanup([ctx, pred_handle]() {
      OP_REQUIRES_OK(ctx, ctx->function_library()->ReleaseHandle(pred_handle));
    });

    const FunctionBody* pred_body =
        ctx->function_library()->GetFunctionBody(pred_handle);
    OP_REQUIRES(ctx, pred_body->ret_nodes.size() == 1,
                errors::InvalidArgument(
                    "predicate function must have a single return value."));
    Node* ret_node = pred_body->ret_nodes[0];
    Node* ret_input_node;
    OP_REQUIRES_OK(ctx, ret_node->input_node(0, &ret_input_node));
    std::unique_ptr<CapturedFunction> captured_func;
    OP_REQUIRES_OK(ctx, CapturedFunction::Create(
                            func_, std::move(other_arguments), &captured_func));

    if (ret_input_node->def().op() == "_Arg") {
      int32 index = -1;
      OP_REQUIRES_OK(ctx, GetNodeAttr(ret_input_node->def(), "index", &index));
      *output = new FilterTensorDataset(ctx, input, func_,
                                        std::move(captured_func), index);
    } else {
      *output = new FilterFunctionDataset(ctx, input, func_,
                                          std::move(captured_func));
    }
  }

 private:
  const int graph_def_version_;

  class FilterDatasetBase : public DatasetBase {
   public:
    FilterDatasetBase(OpKernelContext* ctx, const DatasetBase* input,
                      const NameAttrList& func,
                      std::unique_ptr<CapturedFunction> captured_func)
        : DatasetBase(DatasetContext(ctx)),
          input_(input),
          func_(func),
          captured_func_(std::move(captured_func)) {
      input_->Ref();
    }

    ~FilterDatasetBase() override { input_->Unref(); }

    std::unique_ptr<IteratorBase> MakeIteratorInternal(
        const string& prefix) const override {
      return std::unique_ptr<IteratorBase>(
          new Iterator({this, strings::StrCat(prefix, "::Filter")}));
    }

    const DataTypeVector& output_dtypes() const override {
      return input_->output_dtypes();
    }
    const std::vector<PartialTensorShape>& output_shapes() const override {
      return input_->output_shapes();
    }

    string DebugString() const override { return "FilterDatasetOp::Dataset"; }

   protected:
    Status AsGraphDefInternal(SerializationContext* ctx,
                              DatasetGraphDefBuilder* b,
                              Node** output) const override {
      TF_RETURN_IF_ERROR(b->AddFunction(ctx, func_.name()));
      Node* input_graph_node;
      TF_RETURN_IF_ERROR(b->AddInputDataset(ctx, input_, &input_graph_node));

      DataTypeVector other_arguments_types;
      other_arguments_types.reserve(captured_func_->captured_inputs().size());
      std::vector<Node*> other_arguments;
      other_arguments.reserve(captured_func_->captured_inputs().size());
      for (const Tensor& t : captured_func_->captured_inputs()) {
        Node* node;
        TF_RETURN_IF_ERROR(b->AddTensor(t, &node));
        other_arguments.emplace_back(node);
        other_arguments_types.emplace_back(t.dtype());
      }
      AttrValue f;
      b->BuildAttrValue(func_, &f);
      AttrValue other_arguments_types_attr;
      b->BuildAttrValue(other_arguments_types, &other_arguments_types_attr);

      TF_RETURN_IF_ERROR(b->AddDataset(
          this, {{0, input_graph_node}}, {{1, other_arguments}},
          {{"predicate", f}, {"Targuments", other_arguments_types_attr}},
          output));
      return Status::OK();
    }

    virtual Status EvaluatePredicate(IteratorContext* ctx,
                                     const std::vector<Tensor>& element,
                                     bool* out_matched) const = 0;

   private:
    class Iterator : public DatasetIterator<FilterDatasetBase> {
     public:
      explicit Iterator(const Params& params)
          : DatasetIterator<FilterDatasetBase>(params) {}

      Status Initialize(IteratorContext* ctx) override {
        TF_RETURN_IF_ERROR(
            dataset()->input_->MakeIterator(ctx, prefix(), &input_impl_));
        return dataset()->captured_func_->Instantiate(ctx);
      }

      Status GetNextInternal(IteratorContext* ctx,
                             std::vector<Tensor>* out_tensors,
                             bool* end_of_sequence) override {
        // NOTE(mrry): This method is thread-safe as long as
        // `input_impl_` and `f` are thread-safe. However, if multiple
        // threads enter this method, outputs may be observed in a
        // non-deterministic order.
        bool matched;
        do {
          {
            tf_shared_lock l(mu_);
            if (!input_impl_) {
              *end_of_sequence = true;
              return Status::OK();
            }
            TF_RETURN_IF_ERROR(
                input_impl_->GetNext(ctx, out_tensors, end_of_sequence));
          }
          if (*end_of_sequence) {
            mutex_lock l(mu_);
            input_impl_.reset();
            return Status::OK();
          }

          TF_RETURN_IF_ERROR(
              dataset()->EvaluatePredicate(ctx, *out_tensors, &matched));
          if (!matched) {
            // Clear the output tensor list since it didn't match.
            out_tensors->clear();
          }
        } while (!matched);
        *end_of_sequence = false;
        return Status::OK();
      }

     protected:
      Status SaveInternal(IteratorStateWriter* writer) override {
        mutex_lock l(mu_);
        if (input_impl_)
          TF_RETURN_IF_ERROR(SaveInput(writer, input_impl_));
        else
          TF_RETURN_IF_ERROR(
              writer->WriteScalar(full_name("input_impls_empty"), ""));
        return Status::OK();
      }

      Status RestoreInternal(IteratorContext* ctx,
                             IteratorStateReader* reader) override {
        mutex_lock l(mu_);
        if (reader->Contains(full_name("input_impls_empty")))
          input_impl_.reset();
        else
          TF_RETURN_IF_ERROR(RestoreInput(ctx, reader, input_impl_));
        return Status::OK();
      }

     private:
      mutex mu_;
      std::unique_ptr<IteratorBase> input_impl_ GUARDED_BY(mu_);
    };

    const DatasetBase* const input_;
    const NameAttrList func_;

   protected:
    const std::unique_ptr<CapturedFunction> captured_func_;
  };

  class FilterFunctionDataset : public FilterDatasetBase {
   public:
    using FilterDatasetBase::FilterDatasetBase;

   protected:
    Status EvaluatePredicate(IteratorContext* ctx,
                             const std::vector<Tensor>& element,
                             bool* out_matched) const override {
      // TODO(mrry): Avoid blocking a threadpool thread. We will need to
      // stack-rip the iterators and use async kernels.
      std::vector<Tensor> result;
      TF_RETURN_IF_ERROR(
          captured_func_->RunWithBorrowedArgs(ctx, element, &result));

      if (result.size() != 1 || result[0].dtype() != DT_BOOL ||
          result[0].NumElements() != 1) {
        return errors::InvalidArgument(
            "Filter predicate `f` must return a scalar bool.");
      }
      *out_matched = result[0].scalar<bool>()();
      return Status::OK();
    }
  };

  class FilterTensorDataset : public FilterDatasetBase {
   public:
    FilterTensorDataset(OpKernelContext* ctx, const DatasetBase* input,
                        const NameAttrList& func,
                        std::unique_ptr<CapturedFunction> captured_func,
                        int32 index)
        : FilterDatasetBase(ctx, input, func, std::move(captured_func)),
          index_(index) {}

   protected:
    Status EvaluatePredicate(IteratorContext* ctx,
                             const std::vector<Tensor>& element,
                             bool* out_matched) const override {
      const Tensor& predicate = element[index_];
      if (predicate.dtype() != DT_BOOL || predicate.NumElements() != 1) {
        return errors::InvalidArgument(
            "Filter predicate `f` must return a scalar bool.");
      }
      *out_matched = predicate.scalar<bool>()();
      return Status::OK();
    }

   private:
    const int32 index_;
  };

 private:
  NameAttrList func_;
};

REGISTER_KERNEL_BUILDER(Name("FilterDataset").Device(DEVICE_CPU),
                        FilterDatasetOp);

}  // namespace
}  // namespace data
}  // namespace tensorflow