Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate the shape of TakeDataset and SkipDataset #18267

Merged
merged 4 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def testSkipVarious(self):
# Skip nothing
self.run_core_tests(lambda: self._build_skip_dataset(0), None, 10)

def testInvalidSkip(self):
with self.assertRaisesRegexp(
ValueError, 'Shape must be rank 0 but is rank 1'):
self.run_core_tests(lambda: self._build_skip_dataset([1, 2]), None, 0)

def _build_take_dataset(self, count):
components = (np.arange(10),)
return dataset_ops.Dataset.from_tensor_slices(components).take(count)
Expand All @@ -69,6 +74,11 @@ def testTakeVarious(self):
# Take nothing
self.run_core_tests(lambda: self._build_take_dataset(0), None, 0)

def testInvalidTake(self):
with self.assertRaisesRegexp(
ValueError, 'Shape must be rank 0 but is rank 1'):
self.run_core_tests(lambda: self._build_take_dataset([1, 2]), None, 0)

def _build_repeat_dataset(self, count, take_count=3):
components = (np.arange(10),)
return dataset_ops.Dataset.from_tensor_slices(components).take(
Expand Down
12 changes: 10 additions & 2 deletions tensorflow/core/ops/dataset_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,23 @@ REGISTER_OP("TakeDataset")
.Output("handle: variant")
.Attr("output_types: list(type) >= 1")
.Attr("output_shapes: list(shape) >= 1")
.SetShapeFn(shape_inference::ScalarShape);
.SetShapeFn([](shape_inference::InferenceContext* c) {
shape_inference::ShapeHandle count_shape;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &count_shape));
return shape_inference::ScalarShape(c);
});

REGISTER_OP("SkipDataset")
.Input("input_dataset: variant")
.Input("count: int64")
.Output("handle: variant")
.Attr("output_types: list(type) >= 1")
.Attr("output_shapes: list(shape) >= 1")
.SetShapeFn(shape_inference::ScalarShape);
.SetShapeFn([](shape_inference::InferenceContext* c) {
shape_inference::ShapeHandle count_shape;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &count_shape));
return shape_inference::ScalarShape(c);
});

REGISTER_OP("BytesProducedStatsDataset")
.Input("input_dataset: variant")
Expand Down