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

   ### What changes were proposed in this pull request?
   
   Base C of completing the native ML classes to the full `pyspark.ml` 4.3.0 
surface, after Base B (#667) and the API golden (#669). It adds the PyO3 base 
that each native class of `pyspark.ml.connect` uses to follow PySpark's API. 
`LinearSVC` (with `LinearSVCModel`, `LinearSVCSummary`, 
`LinearSVCTrainingSummary`) and `KMeans` (with `KMeansModel`, `KMeansSummary`) 
follow it in full, as the worked examples for the areas.
   
   **PySpark's Params API** (`crates/pyspark-rs/src/ml/params_api.rs`), as 
`pyspark.ml.param.Params` has it:
   - `params`, `getParam`, `hasParam`, `isSet`, `isDefined`, `hasDefault`, 
`getOrDefault`, `explainParam(s)`, `extractParamMap`, `set`, `clear` (a no-op 
over Spark Connect, as `JavaParams.clear`) and `copy`;
   - PySpark's errors (`_resolveParam`, `_shouldOwn`, `_copyValues`) and 
keywords (`getParam(paramName=...)`, `fitMultiple(dataset, paramMaps)`).
   
   The Param objects are PySpark's own `pyspark.ml.param.Param`, of the uid of 
the instance. Their docs, converters and defaults come from `ml_params.json`, 
the params of `tests/golden/ml_api.json` (#669), which the extension embeds.
   
   **PySpark's defaults of each instance**:
   - the default `seed` is the `hash` of the name of the class, and the default 
`outputCol` is the uid and `__output`;
   - a fitted model gets the defaults of its estimator, as `_copyValues` copies 
them. Its seed is the hash of the estimator's class name, and ALS's 
`coldStartStrategy` becomes a default of ALSModel;
   - a loaded object keeps the defaults of the uid that it was made of, as 
`_resetUid` keeps them. A loaded transformer's `outputCol` default is not its 
uid and `__output`.
   
   **Macros** (`crates/pyspark-rs/src/ml/macros.rs`). PyO3 takes one 
`#[pymethods]` block per class without `multiple-pymethods`, so each macro 
emits the whole block:
   - `native_pymethods!` wraps an estimator, transformer or evaluator. It adds 
the Params API, a getter and a Param per param, `copy`, `__repr__` (the uid), 
and `write`, `save`, `read` and `load`.
   - `native_model!` defines a typed model of a `ModelCore`. It has the same 
Params API and persistence, the setters of PySpark's model, and its properties, 
methods and DataFrames, each a fetch of the server's object as PySpark's 
`_call_java` and attribute relations do. It also has `transform` (of a param 
map too) and `__repr__` (the server's `toString`).
   - `native_summary!` defines a summary class of an `MlTarget`.
   
   **Objects** (`crates/pyspark-rs/src/ml/object.rs`):
   - `MLWriter` (`overwrite`, `option`, `optionMap`, `session`, `save`; `sc` 
raises as PySpark does) and `MLReader`, as `RemoteMLWriter` and 
`RemoteMLReader`;
   - the conversion of `MlValue`s to Python (vectors and matrices of 
`pyspark.ml.linalg`);
   - the training summary of a fit. It is the same object on every call, as 
PySpark's `_summary`, and is of the summary dataset of the class: the training 
dataset for the classification models, the transform of it for the clustering 
models.
   
   **Fits of param maps**: `fit(dataset, paramMap)` fits a copy with the map, 
and a list or tuple of maps fits once per map. `fitMultiple` returns PySpark's 
`_FitMultipleIterator`. `transform(dataset, paramMap)` transforms with a copy 
of the model.
   
   In the Rust API (`spark_connect::ml`):
   - `ObjectModel` is a model of a server-side object, of its operator and 
params, such as one that `read_model` loads.
   - `training_summary`, `summary_transform` and `evaluated_summary` give the 
summary targets of a fit and of an `evaluate`.
   
   Also:
   - `scripts/gen_native_params_table.py <Class>` prints the `params:` rows of 
a class from the golden.
   - The live and offline test lists of `build_python_connect.yml` and 
`coverage.yml` are one file per line, so the PRs that add test files do not 
conflict.
   
   ### Why are the changes needed?
   
   The native classes had no Params API, no typed models, summaries or 
persistence, and they refused param maps. Code written for `pyspark.ml` did not 
run on them: `model.coefficients`, `model.summary`, `est.explainParams()`, 
`model.save(path)`, `CrossValidator`'s param maps. Each area's PR now converts 
its classes with these macros.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   - `LinearSVC`/`LinearSVCModel`/`LinearSVCSummary`/`LinearSVCTrainingSummary` 
and `KMeans`/`KMeansModel`/`KMeansSummary` of 
`pyspark.ml.connect.classification` and `pyspark.ml.connect.clustering` have 
the full API of `pyspark.ml` 4.3.0.
   - `KMeans` gets `setIntermediateStorageLevel`.
   - A fit of a non-empty param map applies the map, as PySpark does, instead 
of raising the native `TypeError`.
   - The Rust API gets `ObjectModel`, `training_summary`, `summary_transform` 
and `evaluated_summary`.
   - `docs/compatibility.md` describes them.
   
   ### How was this patch tested?
   
   - **`python/tests/test_ml_api_offline.py`** compares each class that follows 
`pyspark.ml` with `tests/golden/ml_api.json`: the member names and Params, the 
member kinds, the parameters (names, kinds and defaults) of each method, and 
the keyword-only constructor. The signature check found that 
`fitMultiple(param_maps)`, `getParam(param_name)` and the `beta=1.0` defaults 
of the summaries did not match PySpark, and this PR fixes them. It also runs 
the Params-API cases of `_ml_api_cases.py` against 
`tests/golden/ml_api_cases.json` (from `scripts/capture_ml_api_cases_golden.py` 
on official pyspark-client 4.3.0). There are 49 cases for LinearSVC and 56 for 
KMeans, including the seed default and a copy.
   - **`python/tests/test_ml_api_e2e.py`** runs the live cases against a 
server: 65 for LinearSVC and 46 for KMeans. They cover the models' members, the 
summaries (their DataFrames, the identity of `summary`, `evaluate`), transforms 
of param maps, save and load of models and estimators (the loaded seed and 
defaults too), `fitMultiple`, and fits of a map and of a list. All match 
official PySpark, errors included.
   - Both test files pass on official PySpark too, as the reference 
(`_native_classes.native_class`).
   - Rust: a unit test of `training_summary`, `ObjectModel` and 
`evaluated_summary`, and a live `e2e_ml_object` test of `summary_transform` 
with KMeans, `evaluated_summary` and an `ObjectModel` of a loaded model.
   - The existing suites pass locally against Spark 4.3.0:
     - the offline list of the coverage job: 4679 passed. The one failure is 
`test_a_deep_column_unpickles_and_is_used_on_a_thread_of_512_kib` on macOS 
x86_64, which also fails on master since #667 there and is fixed by #677;
     - the whole live list of `build_python_connect.yml` (832 tests);
     - the core lib tests (692) and `e2e_ml_object`. Two existing tests 
expected the native `TypeError` of a param map, or listed KMeans's setters; 
they now follow a converted class (`_no_param_map`).
   - All integration tests compile, with and without `wasm-udf`. Workspace 
clippy is clean (`-D warnings`, Rust 1.100.0), and so are `cargo fmt --all 
--check` and ruff.
   
   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