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

   ### What changes were proposed in this pull request?
   
   **Stacked on #690** (the native Pipeline): the commits before the last are 
#690's, so please review the last commit, and merge after #690.
   
   Base D2 of completing the native ML classes to the full `pyspark.ml` 4.3.0 
surface. The native `CrossValidator` of `pyspark.ml.connect` held its numeric 
params only, and its `fit` raised `NOT_IMPLEMENTED`; there was no 
`TrainValidationSplit`. `pyspark.ml.connect.tuning` now has PySpark's 
validators of `pyspark.ml.tuning`, natively, as PySpark 4.3.0 has them over 
Spark Connect (`crates/pyspark-rs/src/ml/tuning_api.rs`): `CrossValidator`, 
`CrossValidatorModel`, `TrainValidationSplit` and `TrainValidationSplitModel`. 
Its `ParamGridBuilder` is PySpark's own class.
   
   - **Estimators and evaluators**: any estimator and evaluator, of the native 
classes, of `pyspark.ml`, or of the user's. A Pipeline is an estimator too.
   - **Fit**: `fit` is the `_fit` of PySpark's class, on the client:
     - The splits are PySpark's, built by the core 
(`spark_connect::ml::{k_fold, train_validation_split}`): the folds of a column 
`<uid>_rand` of `rand(seed)`, or of the numbers of `foldCol`, whose 
out-of-range numbers fail by `raise_error` and whose empty folds raise 
PySpark's `ValueError`; the rows of `rand(seed)` below `trainRatio`.
     - Each split is cached while its models are fitted, by PySpark's 
`_cache_spark_dataset`.
     - The fits and the metrics of the param maps are those of PySpark's 
`_parallelFitTasks` (the estimator's `fitMultiple`), in a `ThreadPool` of 
`parallelism` threads, with the tags of the session 
(`inheritable_thread_target`).
     - `collectSubModels` keeps the model of each fold and param map.
     - The metrics are PySpark's, of numpy (`avgMetrics` and `stdMetrics` of 
`_gen_avg_and_std_metrics`, `validationMetrics`), and the best is 
`argmax`/`argmin` by `isLargerBetter`.
     - The best param map is fitted on the whole dataset, in a model of a new 
uid that gets the params and the defaults of the validator, as `_copyValues` 
gives them.
   - **Models**: `bestModel`, the metrics and `subModels` are attributes of the 
instance, as PySpark's. `transform` is the best model's. `copy` copies the best 
model, with the extra params, and the sub-models; a model of no sub-models 
fails, as PySpark's `assert` does.
   - **Copy**: the validators' `copy` keeps the uid and copies the estimator 
and the evaluator with the extra params; the param maps stay the same.
   - **Params**: PySpark's Params API, and `clear`. The values are PySpark's 
converted Python values, in the orders of PySpark's `_paramMap` and 
`_defaultParamMap` (the constructor's, or of the names after a copy or a load), 
which the metadata writes.
   - **Persistence**: `write`, `save`, `read` and `load` are of PySpark's 
Python format (`_ValidatorSharedReadWrite`):
     - the metadata of `pyspark.ml.tuning.CrossValidator` (or the others) and 
of the params, in PySpark's order of its keys. The param maps are of the JSON 
of each value, or of the path of a value that is a stage (`epm_<name><n>`).
     - the estimator and the evaluator at `estimator` and `evaluator`, a 
model's best model at `bestModel`, and its sub-models at `subModels`, of the 
writer's option `persistSubModels` (by default, of whether it has them).
     - the checks of `validateParams` (writable stages, the params of the param 
maps of the estimator's stages, no nested validator) and their errors.
     - A load reads the metadata as Python's `json.loads`, sets the params as 
`getAndSetParams` does (all of them set, the defaults of the save), and repeats 
one list of the sub-models for each fold, as PySpark's reader does.
   
     PySpark loads what this client saves, and this client loads what PySpark 
saves.
   
   Also:
   - The native Pipeline, PipelineModel and validators are registered as 
virtual subclasses of PySpark's classes of them, so that `isinstance` checks of 
`pyspark.ml.Pipeline` and `pyspark.ml.tuning.CrossValidator` hold of them, and 
PySpark's `MetaAlgorithmReadWrite` reads the nested stages of a native Pipeline 
(a validator's uid map).
   - The Pipeline and the validators take as writable an object of a native 
class (or a subclass of one), and any other only if it is PySpark's 
`MLWritable`, as PySpark's checks do.
   - `scripts/capture_ml_api_golden.py` captures the defaults of the models of 
the meta-algorithms (`CrossValidatorModel(None)`), which the API golden now has.
   - `docs/compatibility.md`: the summary of the native classes that follow 
`pyspark.ml` points to the section of each class, so that the area PRs do not 
each edit one sentence.
   
   ### Why are the changes needed?
   
   The native classes are to follow `pyspark.ml`, and the validators are how a 
model is tuned. PySpark's validators fit on the client, of any estimator and 
evaluator, so the server cannot fit one.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   - `pyspark.ml.connect.CrossValidator` fits, transforms, copies, and saves 
and loads, as PySpark's, and `pyspark.ml.connect.CrossValidatorModel` is the 
class of its model, not the generic native model.
   - `pyspark.ml.connect.TrainValidationSplit` and `TrainValidationSplitModel` 
are new, and `pyspark.ml.connect.tuning` has PySpark's `ParamGridBuilder`.
   - The Rust API gets `k_fold` and `train_validation_split`.
   - `docs/compatibility.md` describes them.
   
   ### How was this patch tested?
   
   - **Cases** of `python/tests/_ml_api_cases.py` against the golden of 
official pyspark-client 4.3.0 (`tests/golden/ml_api_cases.json`), for each of 
CrossValidator and TrainValidationSplit:
     - offline (65 and 61): the Params API, PySpark's binding errors, the 
setters, copies of the validators and of their models, the isinstance checks, 
and the errors of the fits before any request (unset params, no param maps, 
`numFolds=0`, a dataset that is not one);
     - live (46 and 42): fits, their metrics and their types, sub-models, maps 
and lists of maps, `fitMultiple`, a Pipeline estimator, the fold column (and 
its out-of-range and empty folds), copies and transforms of maps; save and load 
of both classes, the metadata, which matches what PySpark writes (key order 
included), the directories, `persistSubModels` and its errors, nested 
validators, and params of a foreign stage.
   - **Members**: `test_ml_api_offline.py` compares the members, kinds, method 
parameters and Params of the four classes with the API golden.
   - **Reference**: both test files pass on official PySpark too.
   - **Cross-loading**, by hand: official PySpark loads the validators and 
models that this client saved, and this client loads PySpark's; their metrics 
and transforms are the same.
   - **Rust**: unit tests of the plans of the splits, and a live 
`e2e_ml_object` test of the folds of a seed and of a fold column, the 
out-of-range and empty folds, and the train validation split.
   - **Existing suites**: the offline list of the coverage job (4697 passed), 
the live list of `build_python_connect.yml` (835 passed), the core lib tests 
(699), all integration tests compiled with and without `wasm-udf`, fmt, clippy 
(`-D warnings`) and ruff. The tests of the old CrossValidator's not-implemented 
fit are removed, and so is its row of the setter tables, as the Pipeline's in 
#690.
   
   ### 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