Repository navigation
[AIR] Add predict_pandas implementation - #25534
Conversation
krfricke
left a comment
There was a problem hiding this comment.
Data conversion LGTM, just a question for dependencies
| return convert_pandas_to_batch_type(predictions_df, type=type(data)) | ||
|
|
||
| @DeveloperAPI | ||
| def _predict_pandas(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame: |
There was a problem hiding this comment.
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.
| def _predict_pandas(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame: | |
| def _predict_pandas(self, data: pd.DataFrame, **kwargs) -> "pd.DataFrame": |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yeah...I think it makes sense to take on a hard dependency on Pandas. Train now depends on Tune which requires this anyways.
Co-authored-by: Kai Fricke <[email protected]>
Co-authored-by: Kai Fricke <[email protected]>
Co-authored-by: Balaji Veeramani <[email protected]>
| 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() |
There was a problem hiding this comment.
Should we require the column to be named __value__?
There was a problem hiding this comment.
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])
There was a problem hiding this comment.
My preference is not to have this type of distinction, and to always squeeze for single column dataframes.
There was a problem hiding this comment.
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__.
There was a problem hiding this comment.
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
- Always return as same type as input
- 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?
There was a problem hiding this comment.
Why not:
- 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.
There was a problem hiding this comment.
Ok that sounds fine.
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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
Co-authored-by: Clark Zinzow <[email protected]>
ericl
left a comment
There was a problem hiding this comment.
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.
| 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() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Implements conversion utilities and a default predict implementation for Predictor. Depends on ray-project#25517
Implements conversion utilities and a default
predictimplementation forPredictor.Depends on #25517
Why are these changes needed?
Related issue number
Checks
scripts/format.shto lint the changes in this PR.