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

   ### What changes were proposed in this pull request?
   
   Since SPARK-61737 (#718), the public classes of the native ML classes 
(`pyspark.ml.connect.*`) are subclasses of PySpark's classes. A Python subclass 
of one still did not behave like a subclass of PySpark's class. This PR fixes 
construction, dispatch, copies, pickling and uids. Every row was probed on both 
clients, against a Spark 4.3.0 Connect server:
   
   | Case | Before (this client) | PySpark 4.3.0 / after |
   |---|---|---|
   | `class C(LR): __init__(self): super().__init__(maxIter=7)`; 
`C().getMaxIter()` | 100 (keywords dropped) | 7 |
   | `class B(LR): __init__(self, n): ...`; `B(9)` | `TypeError: Method 
__init__ forces keyword arguments.` | constructs |
   | a subclass's `_fit` override, run by `fit` | never called | called, also 
in the copies that a Pipeline or a CrossValidator fits |
   | `A(maxIter=4).copy()` of a subclass; `lr.copy().extra` | the public class; 
attributes lost | `A`; attributes kept (`copy.copy`) |
   | `copy.copy` / `copy.deepcopy` / `pickle` of an estimator, an evaluator, a 
Pipeline, a CrossValidator, a PipelineModel, an unbound model | `TypeError: 
cannot pickle 'LogisticRegression' object` | work |
   | the same, of a fitted model / its summary | the same TypeError | 
`copy.copy` works (it shares the server object); deep copy and pickle raise 
PySpark's errors |
   | uid of `class A(LR)` objects; `x.uid = ...` | `LogisticRegression_...`; 
read-only | `A_...`; assignable |
   | `LR(foo=1)`; `LRModel(None, None)` | PyO3's messages (`__new__`) | 
`LogisticRegression.__init__() got an unexpected keyword argument 'foo'`; 
`JavaModel.__init__() takes from 1 to 2 positional arguments but 3 were given` |
   | `GBTClassificationModel().copy([1])` | `AssertionError` (params walk 
first) | `TypeError: Expecting a dict, ...` |
   
   How each part works:
   - **Construction.** The public class's `__init__` has PySpark's `__init__` 
parameters, wrapped in PySpark's own `keyword_only`, which records 
`_input_kwargs`. It sets its keywords by `setParams`, as PySpark does. Its 
`__new__` binds a call the way that `__init__` would, before the native 
constructor runs (which may read the server's defaults). For a subclass's own 
`__init__`, it leaves the arguments to that `__init__`, as `object.__new__` 
does.
   - **Dispatch.** `fit`, `fitMultiple`, `transform`, `evaluate`, `save` and 
`load` are PySpark's own functions (of `Estimator`, `Transformer`, `Evaluator`, 
`MLWritable` and `MLReadable`), with PySpark's signatures, messages and 
docstrings. They run the object's `_fit`, `_transform`, `_evaluate`, `copy`, 
`write` and `read`. The native `_fit`, `_transform` and `_evaluate` take no 
param map.
   - **Copies.**
     - `copy` keeps the object's class and its attributes.
     - Native `__copy__`, `__getstate__` and `__setstate__` carry the uid, set 
params, own defaults and attributes.
     - `__getnewargs__` makes the meta-models again from their constructor's 
objects.
   - **The class docstring** is PySpark's.
   
   A model made before an exception stays referenced from the frames of the 
traceback until the garbage collector frees it, at some later request; PySpark 
does the same. The offline tests' mock server no longer answers the Delete of 
such a model from the queue of responses of a test's fit.
   
   There are two documented differences from PySpark:
   - A shallow copy of a model keeps the server-side object alive while either 
object lives. PySpark's copy shares its `RemoteModelRef` without taking a 
reference, so garbage-collecting the copy releases the original's object 
(`CONNECT_ML.CACHE_INVALID` at its next use).
   - Assigning `uid` re-parents the object's Params. PySpark keeps the Params 
of the old uid, which then do not belong to the object.
   
   ### Why are the changes needed?
   
   So that a Python subclass of a native ML class, and copies and pickles of 
native objects, behave as they do in PySpark. These are findings G1-G4, F2 and 
F4 of the post-merge review of the last 50 commits.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, in the native `pyspark.ml.connect` classes, which are unreleased: the 
behaviors in the table above.
   
   ### How was this patch tested?
   
   - New `test_ml_subclasses_offline.py` (9 tests) and 
`test_ml_subclasses_e2e.py` (4 tests), added to the CI lists. Official 
pyspark-client 4.3.0 passes them too, against a Spark 4.3.0 Connect server. The 
first version of these tests (before the copy/pickle part) fails without the 
change: 7 offline and 3 e2e.
   - The offline ML suites pass: 1981 passed, 68 skipped. spark-76's subclass 
probe now matches official PySpark on every case except the MRO listing (where 
the public, native and PySpark classes share a name).
   - `cargo fmt --all --check` and `ruff check` pass. The full exact-tree 
suites (the core tests against a server, and the CI offline and live Python 
lists) are running locally, and I'll report them here.
   
   ### 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