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

   ### What changes were proposed in this pull request?
   
   **Stacked on the validators PR (#698), itself on #690**: please review the 
last commit.
   
   Base D3 of completing the native ML classes and the Rust API to the full 
`pyspark.ml` 4.3.0 surface. The Rust API's `Pipeline` held stage names and its 
`CrossValidator` numeric params only, and both fits raised `NOT_IMPLEMENTED`. 
`spark_connect::ml` now has PySpark's meta-algorithms, of any estimators, 
transformers, models and evaluators of the crate (or the user's), as PySpark 
4.3.0 runs them over Spark Connect, on the client 
(`crates/spark-connect/src/ml/{meta,validators}.rs`):
   
   - **Stages and param maps**:
     - `PipelineStage` is an estimator, a transformer or a fitted model, of any 
type that clones (`EstimatorStage`, `TransformerStage`, `EvaluatorStage`, with 
blanket impls).
     - `ParamMap` holds the values of the params of stages, by the uid of a 
stage and the name of its param, as PySpark's `Param.parent` and `Param.name`. 
`ParamGridBuilder` is PySpark's (`add_grid`, `base_on`, `build`, the last param 
varying fastest).
     - `apply_param_map`, a new default method of `Estimator`, `Transformer` 
and `Evaluator`, sets the values of a map, as PySpark's `copy(extra)`. A 
pipeline sets them in its stages.
   - **`Pipeline` and `PipelineModel`**: `fit` (`fit_pipeline`, of the typed 
model) is `Pipeline._fit`, and `PipelineModel` transforms by each stage in 
order.
   - **`CrossValidator`, `TrainValidationSplit` and their models**:
     - Their fit (`fit_validator`, of the typed model) splits the dataset with 
`k_fold` or `train_validation_split` (#698) and caches each split, as 
`_cache_spark_dataset` does (`SPARKML_TEMP_DFS_PATH` too).
     - It fits a copy of the estimator of each param map in `parallelism` 
scoped threads and evaluates each, with numpy's metrics (the mean and 
population deviation over the folds) and numpy's `argmax`/`argmin`, of the 
first NaN.
     - It then fits the best map on the whole dataset. The models have 
`best_model`, the metrics and `sub_models`.
     - A validator of no seed has a random one, as PySpark's default seed (the 
hash of the class name) differs between processes.
   - **Persistence** of PySpark's Python format:
     - `write()` returns an `MlWriter` of a new kind, a `MetaSave`, whose 
overwrite goes through the ML helper first, as `RemoteMLWriter` does. It writes 
PySpark's metadata in PySpark's order of its keys and each stage by its own 
writer: the validators' param maps, estimator, evaluator, best model and 
sub-models (`persistSubModels`).
     - `load` reads the metadata, of the `NaN` and `Infinity` floats that 
Python writes. `load_stage` loads any saved stage by the class of its metadata, 
through a table of the kinds of the JVM classes of `pyspark.ml`. The table is 
generated from the API golden by `scripts/gen_ml_stage_kinds.py`, as the 
server's `Read` needs a class's operator type.
     - Loaded JVM stages are `OperatorEstimator`, `OperatorTransformer` or 
`ObjectModel`, and evaluators are the evaluator types of the crate.
   - `serde_json` parses floats exactly (`float_roundtrip`, a feature of the 
same crate): the default parse was one ulp off on the saved metrics.
   - The params table test (`ml_params_table`) moves the meta-algorithms from 
"pending (Base D)" to converted, with their `PARAM_NAMES`.
   
   ### Why are the changes needed?
   
   The Rust API is to follow `pyspark.ml` too, and a pipeline and a validator 
are how the estimators are used together and tuned. They run on the client in 
PySpark, so the server cannot run them.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the Rust API:
   - `Pipeline::set_stages` takes `PipelineStage`s instead of names. 
`Pipeline::fit` and `CrossValidator::fit` fit instead of raising 
`NOT_IMPLEMENTED`.
   - `CrossValidator::seed()` is an `i64`, a random one by default.
   - New: `PipelineModel::stages`/`load`, `TrainValidationSplit(+Model)`, 
`CrossValidatorModel`'s members, `ParamMap`, `ParamGridBuilder`, 
`PipelineStage`, `load_stage`, `MlWriter::meta`, and `apply_param_map`.
   - `docs/compatibility.md` describes them.
   
   ### How was this patch tested?
   
   - **Unit tests** (offline): param maps and grids (PySpark's product order, 
`base_on`, an empty grid), `apply_param_map` of an estimator and of a 
pipeline's stages, the Python JSON of NaN and Infinity, the JSON of the values 
of params, the kinds of the JVM classes, numpy's `argmax`/`argmin` of NaN, the 
metrics of the folds, the `persistSubModels` option, the metadata of the param 
maps, the uid maps of the nested stages and the refusal of a nested validator, 
and the fits without a request (a pipeline of no estimator, a validator of no 
estimator or no param maps).
   - **Live** (`e2e_ml_object`): a pipeline's fit, transform, save and load, 
and its model's; a cross validation of two folds in two threads, with its 
sub-models, repeatable of its seed, and its save and load (metadata keys, 
sub-model directories, `persistSubModels=false`); a train validation split of a 
pipeline estimator, saved and loaded.
   - **Cross-loading**, by hand, with official pyspark-client 4.3.0:
     - Official PySpark loads the pipeline, pipeline model, cross validator and 
model, and the train validation split model that this API saved, transforms 
with them, and refits the loaded cross validator.
     - This API loads the cross validator, its model and a train validation 
split model with sub-models that official PySpark saved.
     - A refit of the loaded cross validator gives official's metrics on the 
same 40 rows and seed: `[0.3944444444444445, 0.3944444444444445]`.
   - **Existing suites**: every test of `apache-spark-connect` against a server 
(1158 passed), all integration tests compiled with `wasm-udf`, fmt, and 
workspace clippy (`-D warnings`; one pre-existing lint of `spark-connect-core` 
allowed). The unit tests of the old Pipeline's and CrossValidator's names, 
params and `NOT_IMPLEMENTED` fits now cover the new API.
   
   ### 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