Skip to content

[Dataset] Enable read_tfrecords to accept features with missing values - #31922

Merged
clarkzinzow merged 36 commits into
ray-project:masterfrom
scottjlee:tfrecords-missing-features
Feb 15, 2023
Merged

clarkzinzow merged 36 commits into
ray-project:masterfrom
scottjlee:tfrecords-missing-features

Conversation

@scottjlee

Copy link
Copy Markdown
Contributor

Signed-off-by: Scott Lee [email protected]

Why are these changes needed?

Currently, ray.data.read_tfrecords does not allow a feature with missing values when reading a TFRecords Dataset. This PR bridges the gap and handles tf.train.Features with missing values.

Related issue number

Closes #31831

Checks

@daikeshi

Copy link
Copy Markdown
Contributor

Thanks for the speedy PR to address the issue!

@scottjlee
scottjlee marked this pull request as draft January 30, 2023 20:14
@richardliaw

Copy link
Copy Markdown
Contributor

@c21 @scottjlee Can we prioritize getting this over the line btw?

This is blocking @daikeshi

@scottjlee
scottjlee marked this pull request as ready for review February 6, 2023 21:16
Signed-off-by: Scott Lee <[email protected]>

@clarkzinzow clarkzinzow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall, only a few nits

Comment on lines +216 to +230
if not pa.types.is_list(c.type) or not pa.types.is_null(
c.type.value_type
):
continue
elif pa.types.is_null(c.type.value_type):
if pa.types.is_list(scalar_type):
# If we are dealing with a list input,
# cast the array to the scalar_type found above.
col_chunked_arrays[c_idx] = c.cast(scalar_type)
else:
# If we are dealing with a single value, construct
# a new array with null values filled.
col_chunked_arrays[c_idx] = pa.chunked_array(
[pa.nulls(c.length(), type=scalar_type)]
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Instead of an if A: continue, elif B: blah, could have an if not A and B: blah. And since we happen to have that not A = True ==> B = True (since pa.types.is_null(c.type.value_type) is necessary for not A), this simplifies to just not A, or pa.types.is_list(c.type) and pa.types.is_null(c.type.value_type).

Suggested change
if not pa.types.is_list(c.type) or not pa.types.is_null(
c.type.value_type
):
continue
elif pa.types.is_null(c.type.value_type):
if pa.types.is_list(scalar_type):
# If we are dealing with a list input,
# cast the array to the scalar_type found above.
col_chunked_arrays[c_idx] = c.cast(scalar_type)
else:
# If we are dealing with a single value, construct
# a new array with null values filled.
col_chunked_arrays[c_idx] = pa.chunked_array(
[pa.nulls(c.length(), type=scalar_type)]
)
if pa.types.is_list(c.type) and pa.types.is_null(
c.type.value_type
):
if pa.types.is_list(scalar_type):
# If we are dealing with a list input,
# cast the array to the scalar_type found above.
col_chunked_arrays[c_idx] = c.cast(scalar_type)
else:
# If we are dealing with a single value, construct
# a new array with null values filled.
col_chunked_arrays[c_idx] = pa.chunked_array(
[pa.nulls(c.length(), type=scalar_type)]
)

else:
# If the feature value is empty, set the type to null for now
# to allow pyarrow to construct a valid Array; later, infer the
# type from other records which have non-empty values for the feature.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comment!


# Read the TFRecords.
readback_ds = ray.data.read_tfrecords(tmp_path)
readback_ds = ray.data.read_tfrecords(tmp_path, parallelism=1)

@clarkzinzow clarkzinzow Feb 14, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could we actually remove this parallelism specification, along with the above parallelism specification for from_items(), and change the below assert to:

# Repartition each dataset into single block so column types are unified.
assert ds.repartition(1).take() == readback_ds.repartition(1).take()

That way we can ensure that multi-block roundtrips work (provided that it does actually work, lmk if it doesn't), and the single-block thing is only part of checking that the roundtrip is equal.

Scott Lee added 2 commits February 14, 2023 17:03
@clarkzinzow
clarkzinzow merged commit 8b9c0bc into ray-project:master Feb 15, 2023
edoakes pushed a commit to edoakes/ray that referenced this pull request Mar 22, 2023
…ues (ray-project#31922)

Currently, ray.data.read_tfrecords does not allow a feature with missing values when reading a TFRecords Dataset. This PR bridges the gap and handles tf.train.Features with missing values.

Signed-off-by: Scott Lee <[email protected]>
Signed-off-by: Edward Oakes <[email protected]>
peytondmurray pushed a commit to peytondmurray/ray that referenced this pull request Mar 22, 2023
…ues (ray-project#31922)

Currently, ray.data.read_tfrecords does not allow a feature with missing values when reading a TFRecords Dataset. This PR bridges the gap and handles tf.train.Features with missing values.

Signed-off-by: Scott Lee <[email protected]>
elliottower pushed a commit to elliottower/ray that referenced this pull request Apr 22, 2023
…ues (ray-project#31922)

Currently, ray.data.read_tfrecords does not allow a feature with missing values when reading a TFRecords Dataset. This PR bridges the gap and handles tf.train.Features with missing values.

Signed-off-by: Scott Lee <[email protected]>
Signed-off-by: elliottower <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] read_tfrecords doesn't allow missing feature values

6 participants