Skip to content

CsvDataset loses rows when restoring an iterator from a checkpoint with header=True #129842

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0.dev20261028

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Python version

3.11.7

Current behavior?

Restoring a tf.data.experimental.CsvDataset iterator from a checkpoint with header=True loses data. Depending on the read buffer size, it either silently ends early, dropping most of the remaining rows, or fails with Expect N fields but have M in record. make_csv_dataset is affected too, since its header argument defaults to True. With two files, the rest of the first file is dropped and reading moves straight to the second. Without a header, resuming works correctly.

So a training job that resumes from a checkpoint can continue on a fraction of its CSV data with no error. The number of rows returned also varied between runs of the same script.

The cause is in tensorflow/core/kernels/data/experimental/csv_dataset_op.cc. RestoreInternal does this:

TF_RETURN_IF_ERROR(SetupStreamsLocked(ctx->env()));
num_buffer_reads_ = size_t(num_buffer_reads - 1);
absl::Status s = input_stream_->SkipNBytes(
    num_buffer_reads_ * dataset()->options_.input_buffer_size);
...
absl::Status s2 = FillBuffer(&buffer_);
...
pos_ = size_t(pos);

This assumes that the stream is at the start of the file after SetupStreamsLocked. But with header=True, SetupStreamsLocked also reads the header record, which consumes at least one buffer. The saved num_buffer_reads already includes those reads, so the stream ends up past the buffer that pos refers to.

CsvDatasetCheckpointTest only uses a file without a header, so the tests don't catch this.

Expected: after resuming at row 3, the iterator returns rows 3 to 11, as it does without a header.

Standalone code to reproduce the issue

import os, tempfile
import tensorflow as tf

d = tempfile.mkdtemp()
path = os.path.join(d, "data.csv")
with open(path, "w") as f:
  f.write("a,b\n" + "".join(f"{i},{i * 10}\n" for i in range(12)))

def make():
  return tf.data.experimental.CsvDataset(
      path, [tf.constant([0]), tf.constant([0])], header=True)

print("full:", [int(a) for a, _ in make()])

# Read 3 rows, save the iterator, restore it into a new one, read the rest.
it = iter(make())
for _ in range(3):
  next(it)
ckpt = tf.train.Checkpoint(it=it).write(os.path.join(d, "ckpt"))
it2 = iter(make())
tf.train.Checkpoint(it=it2).read(ckpt).assert_consumed()
print("after resuming at row 3:", [int(a) for a, _ in it2])

# Same file with a small read buffer.
small = lambda: tf.data.experimental.CsvDataset(
    path, [tf.constant([0]), tf.constant([0])], header=True, buffer_size=4)
it = iter(small())
for _ in range(3):
  next(it)
ckpt = tf.train.Checkpoint(it=it).write(os.path.join(d, "ckpt2"))
it2 = iter(small())
tf.train.Checkpoint(it=it2).read(ckpt).assert_consumed()
try:
  print("small buffer:", [int(a) for a, _ in it2])
except tf.errors.InvalidArgumentError as e:
  print("small buffer: InvalidArgumentError:", str(e).split("}}")[-1].strip()[:60])

# make_csv_dataset, whose header argument defaults to True.
mk = lambda: tf.data.experimental.make_csv_dataset(
    path, batch_size=1, num_epochs=1, shuffle=False, num_parallel_reads=1,
    sloppy=False)
print("make_csv_dataset full:", [int(x["a"][0]) for x in mk()])
it = iter(mk())
for _ in range(3):
  next(it)
ckpt = tf.train.Checkpoint(it=it).write(os.path.join(d, "ckpt3"))
it2 = iter(mk())
tf.train.Checkpoint(it=it2).read(ckpt).assert_consumed()
print("make_csv_dataset after resuming at row 3:", [int(x["a"][0]) for x in it2])

Relevant log output

full: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
after resuming at row 3: [3, 4]
small buffer: InvalidArgumentError: Expect 2 fields but have 1 in record [Op:IteratorGetNext] na
make_csv_dataset full: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
make_csv_dataset after resuming at row 3: [3, 4, 5, 6]

On a second run of the same script, after resuming at row 3 printed [3].

I have a fix ready and will open a PR that links here.

Activity

  1. added
    comp:data
    awaiting PR merge
    2.21.0
    on Nov 3, 2026
  2. google-ml-butler commented on Nov 4, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:datatype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions