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

   ### What changes were proposed in this pull request?
   
   A follow-up of #748 and SPARK-61766, from a review of the native ML classes 
and the Rust ML API against PySpark 4.3.0.
   
   **Lists of ints are sent as PySpark's literal of them.** A list of ints of 
an ML param was sent as an `integer` array when every element fits 32 bits, 
else as a `long` array, which the server reads into a JVM `Array[Int]` as the 
low 32 bits of each element. PySpark types the list by its first element 
(`LiteralExpression._infer_type`), and the `LiteralExpression` of each element 
asserts its range. The params are `MultilayerPerceptronClassifier.layers`, 
`VectorSlicer.indices` and `QuantileDiscretizer.numBucketsArray`. For example, 
of `layers=[2, 2**32 + 3, 2]`:
   
   | | PySpark 4.3.0 (now here too) | before |
   |---|---|---|
   | `fit`, `transform` and `save` | bare `AssertionError`, and no request | 
fits a model of layers `[2, 3, 2]` |
   | the getter | the list as given | the list as given |
   | of a first element beyond a long | `VALUE_NOT_BETWEEN` | 
`VALUE_NOT_BETWEEN` |
   
   - **The binding.** A `toListInt` param builds its literal with 
`functions::to_value_column`, as the values of the ML fetches are built. A list 
it cannot build is kept for its getter. The next fit, transform, evaluate or 
save raises the error of its literal, computed from the stored value, after the 
borrow of the object is dropped. That is the bare `AssertionError`, 
`VALUE_NOT_BETWEEN` or `CANNOT_INFER_ARRAY_ELEMENT_TYPE`.
   - **The order of the params.** `sync_params` sets the values in the order of 
PySpark's `_paramMap`, so the error of several such params is of the first of 
them, as PySpark's serialization reads them. For example, 
`MultilayerPerceptronClassifier(maxIter=2**64, layers=[2, 2**33])` raises 
`VALUE_NOT_BETWEEN`.
   - **The Rust API.**
     - `Params::set_param_int_array` types the array by its first element in 
the same way. A list that PySpark cannot send is kept for its getter.
     - The new `Params::sendable()` returns the `AssertionError` before any 
request. Fits, evaluations, both transform paths and the writers send 
`sendable()?`.
     - The new `Params::extend_from` is used by the `fit_multiple` of 
`LogisticRegression` and `LinearSVC`.
     - The param maps of the grids (`meta.rs`) follow the same rule.
   
   **Ints beyond their type in the Rust API.**
   - `PredictionModel::num_features` and `VectorIndexerModel::category_maps` 
return an error of an int out of the range of an `i32`, where they truncated 
it. PySpark reads a fetched int as a Python int of any size.
   - A grid int beyond a long, such as a whole float `1e20` of an int param, is 
PySpark's `VALUE_NOT_BETWEEN`, where it saturated to `i64::MAX`.
   
   **The copy of the validator models.** The `apply_param_map` of 
`CrossValidatorModel` and `TrainValidationSplitModel` applied the map to the 
best model only. It is now PySpark's `copy(extra)` of them (tuning.py:1081-1091 
and 1646-1650):
   - it applies the map to the best model;
   - it fails as PySpark's `assert self.subModels is not None` of a model of no 
sub-models, so the evaluation of a param map of a validator of no sub-models, 
nested in a grid, fails as PySpark's does;
   - it copies the sub-models with an empty map;
   - it sets the model's own params of the map (`numFolds`, `foldCol`, 
`trainRatio` and `seed`).
   
   The evaluation of a param map copies the model only for a map that is not 
empty, as `Transformer.transform` does.
   
   **Others.**
   - `RandomForestClassificationModel::summary()` of no summary gives PySpark's 
message, "No training summary available for this 
RandomForestClassificationModel".
   - The docs of `StringIndexerModel::labels` and `ChiSqSelector` note 
PySpark's deprecations, as doc notes rather than `#[deprecated]`, as for the 
other deprecations of the Rust API: PySpark warns of neither at runtime.
   - `docs/compatibility.md` describes these, and the arithmetic of the 
thresholds of `LogisticRegression`, whose zero divisor is Python's "float 
division by zero".
   
   ### Why are the changes needed?
   
   A list of ints that PySpark refuses was sent, and the server fitted a model 
of other values than those given, with no error. The other cases return values 
or errors that PySpark does not, or apply a param map otherwise.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above:
   - a list of ints of an element out of the range of the type of its first 
element raises PySpark's `AssertionError` at the next fit, transform or save, 
instead of fitting other values;
   - in the Rust API, `num_features` and `category_maps` of an int beyond an 
`i32`, and the grid ints beyond a long, are errors;
   - the `apply_param_map` of the validator models is PySpark's `copy(extra)`;
   - the random forest summary's message is PySpark's.
   
   The new public methods of the Rust API are `Params::sendable` and 
`Params::extend_from`.
   
   Remaining differences, documented in the methods:
   - **Empty lists in the Rust API.** `set_param_int_array([])` sends an empty 
`integer` array, where PySpark raises `CANNOT_INFER_ARRAY_ELEMENT_TYPE`. The 
string and double list setters do the same.
   - **Grid values that PySpark cannot send in the Rust API**, such as `1e20` 
for an int param, are refused at the save of the validator too. PySpark's save 
writes them, and only its fit fails.
   
   ### How was this patch tested?
   
   - **Probes of both clients against a Spark 4.3.0 server** match, apart from 
random uids. They cover the three params through the constructor, setters, 
`setParams`, `copy`, the getters, fit, transform and save, `explainParams` and 
a model's copy. They also cover the order of the errors of two bad params, 
nested validators, and `num_features` of a long through a recording proxy.
   - **New tests.**
     - Python: 
`test_ml_connect_int_lists_are_literals_of_the_type_of_their_first_element_as_pyspark`
 in `test_ml_offline.py`, which fails on master's extension.
     - Rust unit tests in `ml.rs`, `meta.rs`, `validators.rs`, and the tree 
golden tests of `tree_api_cases.json`.
     - A golden replay in `lr_svc_api_golden.rs`, and a live test in 
`e2e_ml_meta.rs`.
   - **Python.** The offline ML files (1921 passed after the rebase on master), 
the offline suite of `coverage.yml`, and the live ML files: misc_api, 
ml_features, feature_api, remaining_classifiers, tree_api, ml and ml_subclasses 
(301 passed).
   - **Rust.**
     - `cargo test -p apache-spark-connect --release --lib` (748 passed);
     - the offline integration tests, and `--no-run` with and without 
`wasm-udf`;
     - the live ML e2e files.
   - **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p 
apache-spark-connect -p pyspark-rs --all-targets -- -D warnings`, ruff 0.14.0, 
`audit_no_stubs.sh` and `scripts/tests`.
   
   ### 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