HyukjinKwon opened a new pull request, #823:
URL: https://github.com/apache/spark-connect-rust/pull/823
### What changes were proposed in this pull request?
A follow-up of #791 (SPARK-61879), #810 (SPARK-61929) and #812
(SPARK-61932), from a round-3 review of the native ML classes
(`pyspark.ml.connect`) against pyspark-client 4.3.0. Each item was probed on
both clients against a Spark 4.3.0 server.
**1. The writers and readers are PySpark's plain objects.**
- In PySpark, `RemoteMLWriter` and `RemoteMLReader` are plain objects:
- `BaseReadWrite.session(self, sparkSession)`;
- `MLWriter`'s `shouldOverwrite`, `optionMap`, `overwrite()` and
`option(key, value)` (ml/util.py:534, 570-603);
- `RemoteMLWriter.save(self, path)` of `_instance`, and
`RemoteMLReader.load(self, path)` of `_clazz` (ml/connect/readwrite.py:31-39,
202-206).
- The drop-in's kept PyO3's methods and state:
- `w.session(sparkSession=spark)` raised `unexpected keyword argument
'sparkSession'`;
- `w.overwrite(1)` and `w.save(p, 1)` raised PyO3's messages;
- `copy.copy`, `copy.deepcopy` and `pickle` raised `AttributeError: ... no
attribute 'instance'`;
- `w.shouldOverwrite = 0` raised `TypeError`;
- `_sparkSession`, `_instance` and `_clazz` were missing.
- Now:
- **State.** `PyMLWriter` and `PyMLReader` keep PySpark's attributes in
their instance dict, in the order of PySpark's `__init__`.
- **Methods.** They inherit PySpark's `session`, `overwrite` and `option`,
and bind `save` and `load` as PySpark's functions (`_synced`).
- **save.** It asserts an active session first, as PySpark's does; a model
could save without one before. It reads `_instance`, `shouldOverwrite` and
`optionMap` at the call. An object of no native class goes to PySpark's own
`saveInstance`, for example `NotImplementedError: Unsupported write for <class
'NoneType'>`.
- **load.** It reads `_clazz` at the call, and an object of no native
class goes to PySpark's own `loadInstance`.
- **The Write fields** are converted as PySpark's protobuf messages
convert them. A `shouldOverwrite` of None leaves the field unset; the core gets
the doc-hidden `write_operator_opt` and `write_object_opt` for it.
- **Copies and pickles** are those of plain objects. A subclass pickles
through `copyreg.__newobj__`, and the public class through
`_pyspark._new_remote_ml_writer` and `_new_remote_ml_reader`.
**2. `pyspark.ml.stat`.** `SummaryBuilder.summary` is a function of
PySpark's signature and docstring, so a call that does not bind raises
PySpark's message. The static methods are of the module `pyspark.ml.stat`, as
PySpark's.
**3. The defaults of a fitted model are in PySpark's order.** PySpark's
`try_remote_fit` runs `model._resetUid(self.uid)`, which rebuilds the defaults
in name order, then `self._copyValues(model)` (param/__init__.py:531-564). That
appends the estimator's defaults that the model's constructor lacks. So a
fitted ALSModel's `_defaultParamMap` is `['blockSize', 'predictionCol',
'coldStartStrategy', 'itemCol', 'userCol']`, where the drop-in's was in name
order. Of the 38 estimator/model pairs, only ALS was affected.
**4. The native Pipeline's fit iterates its stages as `Pipeline._fit` does**
(pipeline.py:120-142), three times, so `Pipeline(stages=<a generator>)` gives
PySpark's empty PipelineModel.
`docs/compatibility.md` is updated. That includes a load's param order,
which is a deterministic choice of the drop-in, where PySpark's follows upb's
map order, which differs per process.
### Why are the changes needed?
So that the writers, readers, stat functions, fitted models and Pipelines of
the native classes bind, copy, pickle and order as PySpark's.
### Does this PR introduce _any_ user-facing change?
Yes, as above:
- writers and readers have PySpark's attributes and bindings, and copy and
pickle;
- a save without an active session raises PySpark's assertion;
- ALSModel's defaults are in PySpark's order;
- a Pipeline of a one-shot iterator fits as PySpark's.
### How was this patch tested?
- **New tests.** Each fails on master.
- `test_ml_subclasses_offline.py`:
`test_a_writer_and_a_reader_are_pysparks_plain_objects_of_their_attributes` and
`test_a_writer_and_a_reader_copy_and_pickle_as_pysparks_plain_objects`. Both
pass on pyspark-client 4.3.0 too.
- `test_ml_offline.py`:
`test_a_writer_and_a_reader_read_their_attributes_at_the_call_as_pyspark`,
`test_the_maps_of_the_model_of_a_fit_are_of_pysparks_try_remote_fit` and
`test_the_native_pipeline_iterates_its_stages_as_pysparks_fit`.
- `test_statistics_offline.py`:
`test_the_methods_are_of_pysparks_module_and_summary_binds_as_its_function`.
- Live `test_ml_api_e2e.py`:
`test_a_writer_and_a_reader_save_and_load_of_their_attributes_as_pysparks`,
which passes on the official client too.
- **Probes of both clients:**
- the writer probes' differences dropped from 7, 80 and 14 to 0, and the
ALS, Pipeline and stat probes to their documented remainders;
- the Write, Read and handleOverwrite commands of 263 saves and loads are
identical, through a recording proxy;
- the requests of 244 ML phases are identical too.
- **Python.**
- The 15 ML offline files and the offline suite of `coverage.yml`, on
pandas 2 and 3.
- The seven live ML files.
- `test_type_stubs_offline.py` with mypy 2.4.1, and `gen_type_stubs.py
--check`.
- **Rust.**
- `cargo test -p apache-spark-connect -p apache-spark-connect-core
--release`, and `--features wasm-udf`;
- `cargo test -p pyspark-rs --lib`, linked as `rust.yml` links it.
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p pyspark-rs
-p apache-spark-connect --all-targets -- -D warnings`, ruff 0.14.0,
`audit_no_stubs.sh`, `scripts/tests` and `check_vendored_upstream.sh`.
### 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]