HyukjinKwon opened a new pull request, #418:
URL: https://github.com/apache/spark-connect-rust/pull/418

   ### What changes were proposed in this pull request?
   
   [SPARK-61238](https://issues.apache.org/jira/browse/SPARK-61238): Fit the 
native `StandardScaler`, `MaxAbsScaler` and `StringIndexer` on the server, and 
add the ML cache queries of the client.
   
   - The Rust core fits `StandardScaler`, `MaxAbsScaler` and `StringIndexer` 
through `MlCommand.Fit`, as `LogisticRegression` and `LinearSVC` already do. 
Their models hold the server's cached model, and their transform sends 
`MlRelation.Transform.obj_ref`, as PySpark's fitted models do in 
`pyspark/ml/connect/proto.py` (`TransformerRelation(is_model=True)`). Before, 
the models were built locally and their transform sent a transformer of type 
`MODEL`, which the server rejects.
   - `SparkConnectClient._get_ml_cache_info` sends `MlCommand.get_cache_info` 
and returns the JSON of the cached objects, and `_query_model_size` sends 
`MlCommand.get_model_size`, both as `pyspark/sql/connect/client/core.py` does.
   - Official PySpark 4.3.0 gives the same values for `StandardScaler`, 
`MaxAbsScaler` and `StringIndexer` fit and transform on `array_to_vector` 
features (for example `[1.0, 2.0]` and `[0.3333..., 0.6666...]`). The scalers 
take ML vector features, as the server requires.
   - Not changed, and out of scope here: the native `Pipeline` and 
`CrossValidator` still fit locally, and their transform sends a transformer of 
type `MODEL`, so they fail on the server as before (see below).
   - Rust API change: the models of these three estimators no longer have 
public `input_col`/`output_col` fields (they were never read), and their 
constructors are private to the module.
   
   ### Not covered (follow-up needed, native API redesign)
   
   Checked live on port 15018 with master's build (`scr-sweep`) and with this 
build; results are identical:
   - `Pipeline(stages=["StandardScaler", 
"LogisticRegression"]).fit(df).transform(df)` fails with `UnknownException: 
(java.lang.AssertionError)` in `MLHandler._transformMLRelation` (the 
transformer of type `MODEL`). `fit` alone returns without a server call. 
`Pipeline(stages=[StandardScaler(...), LogisticRegression(...)])` raises 
`TypeError: argument 'stages': 'StandardScaler' object is not an instance of 
'str'`, because the native `Pipeline` takes stage names only.
   - `CrossValidator(numFolds=2).fit(df).transform(df)` fails with the same 
assertion. `CrossValidator` takes no `estimator`, `evaluator` or param grid 
(`TypeError: unexpected keyword argument 'estimator'`), and its `fit` builds a 
model without a fitted sub-model.
   A fix needs the native `Pipeline` to take stage objects, fit them in order 
on the client (the stage models, then `df = model.transform(df)`), and make the 
`PipelineModel` transform through each stage's cached model. `CrossValidator` 
needs the estimator, the evaluator and the param maps. Both change the public 
Rust and PyO3 signatures, so they are a separate PR.
   
   ### Why are the changes needed?
   
   With the native API, `StandardScaler(inputCol="v", 
outputCol="o").fit(df).transform(df)` failed on the server with 
`UnknownException: (java.lang.AssertionError) assertion failed ... 
MLHandler$._transformMLRelation`, for array, vector and double inputs alike. 
The fit did not reach the server, so there was no model to transform. 
`SparkSession._client._get_ml_cache_info()` and `_query_model_size()` raised 
`AttributeError`, and the official 
`pyspark/ml/tests/connect/test_connect_cache.py` and 
`test_connect_model_offloading.py` failed on them (2 and 4 failures).
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Fitting `StandardScaler`, `MaxAbsScaler` and `StringIndexer` of 
`pyspark.ml.connect` now runs on the server, and their transform works. The 
Python client gains `_get_ml_cache_info` and `_query_model_size`.
   
   ### How was this patch tested?
   
   - Offline Rust: `transform_and_fit_build_ml_transform_plans` (the transform 
of a scaler refers to its cached model's object reference) and 
`offline_estimators_fit_and_models_transform` (the same for `MaxAbsScaler` and 
`StringIndexer`, plus `test_all_transformer_types`).
   - Live Rust: `e2e_coverage::ml_estimators_and_transformers` now asserts that 
the fits and transforms succeed on a server, instead of tolerating a rejection.
   - Offline Python, `test_ml_cache_queries_are_those_of_pyspark`: the 
`get_cache_info` and `get_model_size` commands and their results. 
`test_ml_connect_fitted_scalers_transform_the_cached_model`: the fit command of 
each scaler and indexer, and a transform that refers to the fitted object.
   - Live Python, `test_ml_connect_scalers_fit_and_transform_as_pyspark`: the 
values of the three estimators on `array_to_vector` features, which give the 
values of PySpark 4.3.0.
   - The official `pyspark/ml/tests/connect/test_connect_cache.py` now passes 4 
of 4 (was 2 of 4) and `test_connect_model_offloading.py` passes 6 of 6 (was 2 
of 6). The official `test_parity_*.py` files of `pyspark/ml/tests/connect` pass 
140 tests, as on master.
   - The tests of both crates against a local Spark 4.3.0 Connect server (793 
and 186 passed), the build of all tests, also with `wasm-udf`, the offline 
Python suites (2821 passed, 8 skipped), and the live drop-in, Arrow and ML 
suites (234 passed) and live cases (13 passed). rustfmt and ruff pass, and 
clippy reports no diagnostics in the changed lines. The official 
`ml/tests/connect/test_connect_cache.py` (4 passed) and 
`test_connect_model_offloading.py` (6 passed) pass against the drop-in, and the 
11 official `ml/tests/connect/test_parity_*.py` files still pass (140).
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to