-
Notifications
You must be signed in to change notification settings - Fork 307
[WIP] Add FileDataset to read whole content of file into tf.data pipeline #366
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1baf9ee
Add FileDataset to read whole content of file into tf.data pipeline
yongtang ba6c6ad
Remove WebPDataset's C++ implementation, and replace with FileDataset…
yongtang c2636c2
Address review comments
yongtang 0aa22a4
Move tf.keras to separate function in test
yongtang 00ee796
Split tests in eager and non-eager mode
yongtang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* Copyright 2019 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 "kernels/dataset_ops.h" | ||
| #include "tensorflow/core/lib/io/buffered_inputstream.h" | ||
|
|
||
| namespace tensorflow { | ||
| namespace data { | ||
|
|
||
| class FileContentInput: public FileInput<bool> { | ||
| public: | ||
| Status ReadRecord(io::InputStreamInterface* s, IteratorContext* ctx, std::unique_ptr<bool>& state, int64 record_to_read, int64* record_read, std::vector<Tensor>* out_tensors) const override { | ||
| if (record_to_read != 1) { | ||
| return errors::InvalidArgument("FileDataset only accept reading one record at a time"); | ||
| } | ||
| if (state.get() == nullptr) { | ||
| state.reset(new bool(true)); | ||
| } else { | ||
| // We only read file once. | ||
| return Status::OK(); | ||
| } | ||
| int64 chunk_size = 4096; | ||
| SizedRandomAccessInputStreamInterface* sized_stream = dynamic_cast<SizedRandomAccessInputStreamInterface*>(s); | ||
| if (sized_stream != nullptr) { | ||
| // First try to find out the size of the file, depending on if size is available, we will set the chunk to read. | ||
| uint64 file_size = 0; | ||
| if (sized_stream->GetFileSize(&file_size) == Status::OK()) { | ||
| chunk_size = file_size; | ||
| } | ||
| } | ||
| std::vector<string> entries; | ||
| Status status = Status::OK(); | ||
| int64 total_size = 0; | ||
| while (status.ok()) { | ||
| string buffer; | ||
| status = s->ReadNBytes(chunk_size, &buffer); | ||
| if (status.ok() || errors::IsOutOfRange(status)) { | ||
| total_size += buffer.size(); | ||
| entries.emplace_back(std::move(buffer)); | ||
| } | ||
| } | ||
| if (!errors::IsOutOfRange(status)) { | ||
| return status; | ||
| } | ||
| Tensor value_tensor(ctx->allocator({}), DT_STRING, {1}); | ||
| if (entries.size() == 1) { | ||
| value_tensor.flat<string>()((*record_read)) = std::move(entries[0]); | ||
| } else { | ||
| string buffer; | ||
| buffer.reserve(total_size); | ||
| for (size_t i = 0; i < entries.size(); ++i) { | ||
| buffer.append(entries[i]); | ||
| } | ||
| value_tensor.flat<string>()((*record_read)) = std::move(buffer); | ||
| } | ||
| (*record_read)++; | ||
| out_tensors->emplace_back(std::move(value_tensor)); | ||
| return Status::OK(); | ||
| } | ||
| Status FromStream(io::InputStreamInterface* s) override { | ||
| return Status::OK(); | ||
| } | ||
| void EncodeAttributes(VariantTensorData* data) const override { | ||
| } | ||
| bool DecodeAttributes(const VariantTensorData& data) override { | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| REGISTER_UNARY_VARIANT_DECODE_FUNCTION(FileContentInput, "tensorflow::data::FileContentInput"); | ||
|
|
||
| REGISTER_KERNEL_BUILDER(Name("FileInput").Device(DEVICE_CPU), | ||
| FileInputOp<FileContentInput>); | ||
| REGISTER_KERNEL_BUILDER(Name("FileDataset").Device(DEVICE_CPU), | ||
| FileInputDatasetOp<FileContentInput, bool>); | ||
| } // namespace data | ||
| } // namespace tensorflow | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* Copyright 2019 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/framework/common_shape_fns.h" | ||
| #include "tensorflow/core/framework/op.h" | ||
| #include "tensorflow/core/framework/shape_inference.h" | ||
|
|
||
| namespace tensorflow { | ||
|
|
||
| REGISTER_OP("FileInput") | ||
| .Input("source: string") | ||
| .Output("handle: variant") | ||
| .Attr("filters: list(string) = []") | ||
| .Attr("columns: list(string) = []") | ||
| .Attr("schema: string = ''") | ||
| .SetShapeFn([](shape_inference::InferenceContext* c) { | ||
| c->set_output(0, c->MakeShape({c->UnknownDim()})); | ||
| return Status::OK(); | ||
| }); | ||
|
|
||
| REGISTER_OP("FileDataset") | ||
| .Input("input: T") | ||
| .Input("batch: int64") | ||
| .Output("handle: variant") | ||
| .Attr("output_types: list(type) >= 1") | ||
| .Attr("output_shapes: list(shape) >= 1") | ||
| .Attr("T: {string, variant} = DT_VARIANT") | ||
| .SetIsStateful() | ||
| .SetShapeFn([](shape_inference::InferenceContext* c) { | ||
| c->set_output(0, c->MakeShape({})); | ||
| return Status::OK(); | ||
| }); | ||
|
|
||
| } // namespace tensorflow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be helpful if we add a warning log here when
file_sizeis big?