Repository navigation
Fix restoring CsvDataset iterators when the file has a header - #129843
copybara-service[bot] merged 3 commits into
Conversation
RestoreInternal reopens the current file with SetupStreamsLocked, skips `num_buffer_reads - 1` buffers and refills the buffer. But for `header=True`, SetupStreamsLocked also reads the header, which consumes at least one buffer. The saved `num_buffer_reads` already counts those reads from the start of the file, so the stream ends up past the saved buffer and `pos` is applied to the wrong data. Rows are silently lost, or parsing fails with "Expect N fields but have M in record", depending on the buffer size. `make_csv_dataset`, which defaults to `header=True`, is affected too. Split the stream setup into OpenStreamsLocked, which restoring now uses, and SetupStreamsLocked, which also skips the header when moving on to a file. The existing checkpoint test had no header; add one for several buffer sizes, with and without compression.
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where restoring a CsvDataset iterator from a checkpoint with header=True loses rows or fails with a parse error. It separates stream opening from header skipping to ensure that headers are not skipped again during restoration. However, a critical issue was identified in the restoration logic: if num_buffer_reads is 0, subtracting 1 will cause an underflow when cast to size_t, setting num_buffer_reads_ to SIZE_MAX. A code suggestion is provided to safely handle this case.
dmiltr3
left a comment
There was a problem hiding this comment.
In tensorflow/core/kernels/data/experimental/csv_dataset_op.cc (around lines 867 and 896), please update the comment on OpenStreamsLocked and add a short comment on SetupStreamsLocked. This clarifies which helper skips the header row:
// Opens the file at `current_file_index_` and resets the buffer state
// without skipping the header record.
absl::Status OpenStreamsLocked(Env* env)
TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) { // Opens the file at `current_file_index_` and skips the header record
// when `dataset()->header_` is true.
absl::Status SetupStreamsLocked(Env* env)
TF_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
dmiltr3
left a comment
There was a problem hiding this comment.
This PR has merge conflicts with the base branch. Please rebase the PR and resolve the conflicts so we can continue running internal presubmit tests.
…der-restore # Conflicts: # RELEASE.md
|
Updated in e83a313: added the comments on I resolved the
|
Fixes #129842
Problem
Restoring a
CsvDatasetiterator from a checkpoint withheader=Trueloses data. Depending on the read buffer size, the iterator silently ends early or fails withExpect N fields but have M in record.make_csv_datasetdefaults toheader=True, so it is affected too. On tf-nightly, with a 12-row file and resuming at row 3:RestoreInternalcallsSetupStreamsLocked, then skipsnum_buffer_reads - 1buffers and refills the buffer at the savedpos. Withheader=True,SetupStreamsLockedalso reads the header, which consumes at least one buffer. The savednum_buffer_readsalready counts those reads from the start of the file, so the stream ends up past the saved buffer.Change
Split the stream setup in two:
OpenStreamsLockedopens the file and resets the buffer state.RestoreInternalnow calls this one.SetupStreamsLockedcalls it and then skips the header.GetNextInternalstill uses this when moving to the next file.The saved state format is unchanged, so checkpoints written before this change should restore correctly with it, though I haven't tested that case.
I also considered subtracting the header's reads in
RestoreInternal, but that needs extra saved state. Reopening without the header is simpler.CsvDatasetCheckpointTestonly used a file without a header.testHeaderadds one, across buffer sizes 2, 16 and the default, with and without GZIP.Testing
I don't have a Bazel build, so CI on this PR will be the first in-tree build of this change. To test the C++ on a real runtime, I compiled
csv_dataset_op.ccout of tree withclang++against the tf-nightly 2.23.0.dev20261028 macOS arm64 headers, once from master and once with this change. Each was built as atf.load_op_librarykernel under its own op name, andCsvDatasetwas routed to it by replacinggen_experimental_dataset_ops.csv_dataset_v2.select_cols,take,skip, one column)header=Truecsv_dataset_test.py(339 tests, 173 skipped)testHeaderThe script from the issue, including
make_csv_dataset, returns rows 3 to 11 in every case with this PR's kernel.pylint --rcfile=tensorflow/tools/ci_build/pylintrcon the test file: 10/10.clang-format --style=googleon the.ccfile reports no new differences. I did not test other platforms.Notes
I added a
RELEASE.mdentry.