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

   ### What changes were proposed in this pull request?
   
   The objects of PySpark's ML classes are instances of every class of their 
MRO:
   - `MLWritable`, `MLReadable`, `Identifiable`, `JavaMLWritable` and 
`JavaMLReadable`;
   - `HasTrainingSummary` and the summary bases;
   - the param mixins (`HasMaxIter`, `HasFeaturesCol`, ...), `PredictionModel` 
and `ClassificationModel`;
   - the Java wrappers, and the class itself.
   
   The native ML classes were PyO3 classes, registered as virtual subclasses of 
PySpark's ABC bases only (`Estimator`, `Model`, `Params`, `Evaluator`). So 
`isinstance(model, MLWritable)` was False for each of them (`MLWritable` is a 
plain class that cannot be registered with), and PySpark's own 
`pyspark.ml.Pipeline` and validators refused to save native stages ("... is not 
MLWritable"). Each of the 140 classes of the full API had a class of PySpark's 
MRO that it was not a subclass of.
   
   - **The public classes** (`python/pyspark/ml/_native_classes.py`). The class 
of `pyspark.ml.connect.<module>` (and of `pyspark.ml.stat`) is a subclass of 
its native class and of PySpark's class of the same name of 
`pyspark.ml.<module>`, so its MRO has every class of PySpark's, after the 
native class, whose members come first. Its namespace:
     - a no-op `__init__`: the native `__new__` constructs, and PySpark's 
`__init__`s would write `uid` and the param maps;
     - a no-op `__del__`, since the native class releases its server objects 
itself;
     - the native `__signature__`;
     - `_java_obj = None`.
   
     A class of the same module that is a base of PySpark's class, whose native 
class is not a base (the summary of a training summary), takes the public class 
as a virtual subclass (`ABCMeta`). Every native module publishes its classes at 
import, and the package `pyspark.ml.connect` publishes all of them eagerly, so 
an object of a module that the user has not imported is of its public class 
too: the trees of a GBT classifier are regression models. The native feature 
stages that the package installs into the vendored deprecated `feature` module 
are published there.
   - **The objects that the client makes**:
     - every native ML pyclass is `subclass`, and `object::new_object` makes an 
object of a native class of its registered public class 
(`_register_public_class`), through PyO3's `tp_new_impl`, as the constructor of 
the public class makes it;
     - every creation site goes through it: fits (`ModelObject::into_object`, 
through the model's `make`, so an LDA fit is of its subclass), copies, loads of 
models and of estimators (`from_read`), summaries, trees, OneVsRest models, 
validators and pipelines, and the writers, readers and `fitMultiple` iterators 
that methods returned by value;
     - `Fitted` keeps its generic conversion, and `fit_native_model` returns 
the object of the model's class.
   - PySpark's dispatch over these classes stays safe (audited in vendored 
`pyspark.ml`):
     - over Spark Connect, `try_remote_write` sends every vendored `write()` to 
`RemoteMLWriter`, whose Pipeline/validator/OneVsRest branches validate 
`MLWritable` (which native stages now are) and save each stage by its own 
`write()`, the native writer;
     - the Java branches (`isinstance(x, JavaModel)` and then `x._java_obj`) 
are reached only through a class's own `write` or `_call_java`, which the 
native classes define first in their MRO.
   
   ### Why are the changes needed?
   
   The native classes are to follow `pyspark.ml` 4.3.0, whose objects are 
instances of every class of their MRO. Code that checks `isinstance(stage, 
MLWritable)`, PySpark's own `Pipeline.write` among it, refused the native 
classes.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes:
   - the classes of `pyspark.ml.connect.<module>` are subclasses of PySpark's 
classes, so `isinstance(model, MLWritable)`, `isinstance(model, 
ClassificationModel)` and `isinstance(model, 
pyspark.ml.classification.LinearSVCModel)` hold;
   - `pyspark.ml.Pipeline` and the validators of `pyspark.ml.tuning` fit, save 
and load native stages;
   - the classes of `pyspark._pyspark` are their native bases.
   
   ### How was this patch tested?
   
   - `test_ml_api_offline.py::test_classes_are_of_every_base_of_pyspark_mls`: 
each of the 140 classes of the full API is a subclass of every class of the MRO 
of PySpark's class, and of this client's class of each base of PySpark's module 
that is a class of the full API. It passes on ours and on official 
pyspark-client 4.3.0.
   - 
`test_ml_api_e2e.py::test_objects_of_this_client_are_of_the_classes_of_pyspark_mls_modules`,
 also passing on official. The objects that the client makes are of the class 
of their module:
     - fits, also of a param map and of `fitMultiple`, copies, and loads of a 
model and of an estimator;
     - summaries and evaluations, the trees of a GBT (regression models) and of 
a random forest, and the models of a OneVsRest;
     - an LDA `toLocal`, and a CrossValidator's best model;
     - the stages of a fitted and of a loaded Pipeline.
   
     It also checks that a Pipeline of `pyspark.ml` over a native stage saves 
and loads.
   - The tests that asserted that an exported class is its native class 
(`_pyspark.X`) now assert that it is a subclass of it.
   - The class-hierarchy diff against official (every class of the official 
MRO, for the 140 classes) went from 140 classes with a gap to 0. The import 
time of `pyspark.ml.connect` is unchanged within noise.
   - CI's offline and live Python lists; `cargo fmt`, `cargo clippy -p 
pyspark-rs --all-targets -D warnings`, `ruff check` and `ruff format --check`.
   
   ### 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