Skip to content

[AIR] Add predict_pandas implementation - #25534

Merged
amogkam merged 23 commits into
ray-project:masterfrom
amogkam:add-predictor-impl
Jun 9, 2022
Merged

amogkam merged 23 commits into
ray-project:masterfrom
amogkam:add-predictor-impl

Conversation

@amogkam

@amogkam amogkam commented Jun 7, 2022 •

Copy link
Copy Markdown
Contributor

Implements conversion utilities and a default predict implementation for Predictor.

Depends on #25517

Why are these changes needed?

Related issue number

Checks

@krfricke krfricke 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.

Data conversion LGTM, just a question for dependencies

Comment thread python/ray/air/predictor.py Outdated
return convert_pandas_to_batch_type(predictions_df, type=type(data))

@DeveloperAPI
def _predict_pandas(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame:

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.

Can we have conditional imports here as well (in TYPE_CHECKING)? Otherwise the import in air/__init__.py will effectively introduce a dependency of Train and AIR on pandas.

Also happy with just accepting this dependency, but it seems this is currently the only place where this is leaked.

Suggested change
def _predict_pandas(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame:
def _predict_pandas(self, data: pd.DataFrame, **kwargs) -> "pd.DataFrame":

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.

Ah it seems with the data conversion util we can't really avoid this dependency (except we move this to a local import).

In that case we should probably update the Ray train dependencies and minimal install test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah...I think it makes sense to take on a hard dependency on Pandas. Train now depends on Tune which requires this anyways.

elif type == np.ndarray:
# If just a single column, return as a single tensor.
if len(data.columns) == 1:
return data.iloc[:, 0].to_numpy()

@ericl ericl Jun 7, 2022 •

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.

Should we require the column to be named __value__?

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.

+1

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this what you are suggesting?

For single column df with "value" column -> return as squeezed numpy array
For single column df without "value" column -> return as unsqueezed numpy array

pd.DataFrame({"x": [1, 2]}) -> array([[1],[2]])
pd.DataFrame({"__value__": [1, 2]}) -> array([1,2])

@amogkam amogkam Jun 7, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My preference is not to have this type of distinction, and to always squeeze for single column dataframes.

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.

No, just that the former should be returned as {"x": array([1, 2])}. We only unpack it as a single numpy array if (1) there is a single column, and (2) that single column name is __value__.

@amogkam amogkam Jun 8, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The other alternative is to always return a Pandas Dataframe.

But in general I would like to have stronger guarantees on the output type. Either

  1. Always return as same type as input
  2. Always return pd.Dataframe (or pyarrow.Table for _predict_arrow)

I would prefer not to do something like option 1, but then special case numpy input which may or may not have a dict output. For example, this leads to problems with BatchPredictor if the input batch format is a numpy array, but the output is dict, since Datasets does not support dict batch format right?

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.

Why not:

  1. Always return as same type as input, but for numpy this can be either (1) single numpy or (2) dict of numpy.

That should satisfy all the use cases. For BatchPredictor, we can convert dict of numpy to pandas explicitly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok that sounds fine.

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.

Serve wants exactly the same type so we can do batching and splitting. But we also have a last escape hatch to just take a list of object to get around the default splitting behavior.

i.e. the following is supported:

  • array inp -> array out
  • df inp -> df out
  • array inp -> list out
  • df inp -> list out

but "array inp -> dict of array out" is definitely not supported atm. (how do you split things back?)

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.

here's the code for batching and splitting for np array, you see the error checking we are doing here:
https://github.com/ray-project/ray/blob/master/python/ray/serve/model_wrappers.py#L39-L57

Comment thread python/ray/air/predictor.py Resolved

@ericl ericl 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.

Just one comment on the value column handling. I don't have a very strong opinion there but I think it would be nice to be consistent with the dataset convention.

@amogkam amogkam mentioned this pull request Jun 9, 2022
4 of 6 tasks
elif type == DataType.NUMPY:
if len(data.columns) == 1:
# If just a single column, return as a single numpy array.
return data.iloc[:, 0].to_numpy()

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.

Did we determine that we want to always return single-tensor-column tables as a single ndarray even if the column name is not TENSOR_COL_NAME? cc @ericl

@amogkam amogkam Jun 9, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would prefer to hide TENSOR_COL_NAME from developers. If we go with this approach, developers adding new Predictors need to be aware of this special column name when returning predictions from _predict_pandas.

Instead, I think it's more intuitive for NUMPY data type to be a numpy array for single column, and Dict[str, np.ndarray] for multiple columns, without needing to be concerned about what the column name is.

Though I understand when going from numpy -> pandas, we have to use some arbitrary column name.

@amogkam
amogkam merged commit 2614c24 into ray-project:master Jun 9, 2022
@amogkam
amogkam deleted the add-predictor-impl branch June 9, 2022 23:55
bushshrub pushed a commit to bushshrub/ray that referenced this pull request Jun 10, 2022
Implements conversion utilities and a default predict implementation for Predictor.

Depends on ray-project#25517
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.

7 participants