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

   ### What changes were proposed in this pull request?
   
   The native `OneVsRest` of the drop-in, and the Rust API's, now fit, load and 
copy as PySpark 4.3.0's (`pyspark/ml/classification.py`). A review found these 
gaps, each probed on both clients against a Spark 4.3.0 server.
   
   **1. The fit follows PySpark's `OneVsRest._fit`.**
   - **Which classifiers fit natively.** The native fit ran for any instance of 
a native classifier, so a subclass's own `fit` or `_fit` never ran. It now runs 
only for the exact public or native class. Any other classifier goes through 
PySpark's own `_fit`, which calls the classifier's `fit`, as PySpark does.
   - **The class count.** It was `max(cast(label AS DOUBLE)) + 1`, over 
backticked columns. It is now PySpark's `int(dataset.agg({labelCol: 
"max"}).head()[0]) + 1`, of the label as given, and the training set is 
`dataset.select(labelCol, featuresCol)`:
   
   | labels | PySpark 4.3.0 (now here too) | before |
   |---|---|---|
   | strings `"10"`, `"9"`, `"0"` | 10 models, predictions `[0, 9, 0]` | 11 
models |
   | strings `"b"`, `"a"` | `ValueError: invalid literal for int() with base 
10: 'b'` | `CAST_INVALID_INPUT` |
   | a column named `my.label` | `UNRESOLVED_COLUMN` at the max query | fits |
   | a max of `-0.5` | 1 model | 1 model |
   | null, NaN, infinity, no rows, a max of `-1` | Python's `int()` errors, or 
"Number of processes must be at least 1" | other errors |
   
   - **The cache.** The dataset is cached by PySpark's `_cache_spark_dataset`, 
as the binding's validators do, so with `SPARKML_TEMP_DFS_PATH` set it is 
written there and removed after, as on PySpark. It was persisted by the drop-in 
itself.
   - **The errors.** A missing classifier, null params and a parallelism below 
1 or None raise PySpark's errors, in PySpark's order: the max query, the 
storage level, the persist, the unpersist, then the error.
   - **The Rust API** follows the same steps. Its `max_label` returns the 
`Value`, and the class count is Python's `int()` of it, emulated with Python's 
messages.
   
   **2. The loads.**
   - The loads refused the JVM's class names in the metadata, such as 
`org.apache.spark.ml.classification.OneVsRestModel`. PySpark's readers check no 
class, so the drop-in and the Rust API now load them too, standalone and as 
Pipeline stages.
   - `OneVsRestModel.load` sent twice the requests of PySpark's, as it loaded 
the submodels twice. It now builds the model from the submodels it has loaded: 
9 ExecutePlans, as PySpark sends, where it sent 18, and a PipelineModel's 11, 
where it sent 20.
   
   **3. The parallelism** was clamped to a long at its save and load. It now 
keeps an int of any size, as PySpark saves it.
   
   **4. The Rust `OneVsRestModel`** gets an `apply_param_map` that applies the 
map to each submodel, so a copy of extra params transforms. Before, the probe 
failed with `FIELD_NOT_FOUND`. It also gets `OneVsRestModel::new(session, 
models)` and `set_classifier`.
   
   `docs/compatibility.md` describes the native OneVsRest's routing, fit, 
cache, params and loads.
   
   ### Why are the changes needed?
   
   So that a `OneVsRest` fits the same models as on PySpark, with PySpark's 
errors, runs the `fit` of a subclass of a classifier, and loads the saves that 
PySpark loads.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above:
   - the class count, the training set and the errors of the fit;
   - the routing of subclasses to PySpark's `_fit`;
   - the cache;
   - the loads of the JVM's class names;
   - the requests of a model's load;
   - the parallelism of any size.
   
   In the Rust API, `OneVsRest::max_label` returns a `Value`, and 
`OneVsRestModel` gets `new`, `set_classifier` and `apply_param_map`.
   
   Remaining differences, documented:
   - The class count is capped at `i32::MAX`. Above it, Python raises 
`MemoryError`, and the Rust API an error, where PySpark would try to allocate 
its list of tasks.
   - The Rust API's `int()` reads ASCII digits only.
   - The Rust API does not read `numClasses` after each class's fit, as its 
reads are lazy, so it sends 3 fewer fetches per fit.
   
   ### How was this patch tested?
   
   - **Probes of both clients** give the same output for the cases above:
     - the class counts and their errors;
     - the subclasses: `MyLR._fit` runs 3 times, `MyLR2.fit` 3 times, and 
`MySVC` raises "LinearSVC only supports binary classification";
     - the cache under `SPARKML_TEMP_DFS_PATH`;
     - the loads of the JVM's class names;
     - the parallelism at `2**64`, `-(2**64)`, `2**63` and `2**31`, through the 
getter, fit, save, load, setter and copy.
   
     Through a recording proxy, each class's Fit plan is identical to PySpark's 
for the plain, weighted, MLP-weighted and persisted cases, in the binding and 
the Rust API, and so is the order of the other requests.
   - **New tests.**
     - Live Rust tests:
       - `e2e_remaining_classifiers.rs`: 
`one_vs_rest_fits_the_classes_of_pythons_int_of_the_max_label_as_pyspark`;
       - `e2e_classification_misc_api.rs`: 
`one_vs_rest_loads_the_metadata_of_the_jvms_class_as_pyspark` and 
`one_vs_rest_model_sets_a_param_map_in_its_models_as_pyspark`.
     - Live Python cases in `_misc_api_cases.py`: the fits, the JVM class 
names, and the parallelism of `2**64`. `tests/golden/misc_api_cases.json` is 
recaptured on PySpark 4.3.0: 37 keys added, none changed, the same in two 
captures.
     - `test_ml_offline.py`: a subclass routed to PySpark's `_fit`, and a 
parallelism of any size.
     - Unit tests of the Rust `int()` against CPython 3.11's outputs.
   - **Python.**
     - Offline: `test_ml_offline.py`, `test_ml_api_offline.py`, 
`test_ml_subclasses_offline.py`, `test_misc_api_offline.py` and 
`test_ml_features_offline.py`, on pandas 2 and 3 after the rebase on #784 (1931 
passed each), and the offline suite of `coverage.yml`.
     - Live: `test_misc_api_e2e.py`, `test_ml_classification_classes_e2e.py`, 
`test_remaining_classifiers_e2e.py`, `test_ml_api_e2e.py` and `test_ml_e2e.py`.
   - **Rust.** `cargo test -p apache-spark-connect --release --lib` (750 
passed), the golden replays and 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