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

   ### What changes were proposed in this pull request?
   
   An audit of `docs/compatibility.md` found that the native ML classes of the 
Python client differ from PySpark 4.3.0's in four ways, and that they report 
none of their Python objects to the garbage collector. This PR makes them 
behave as PySpark's:
   - **The getters of the native properties.** A native property had PySpark's 
docstring, but its getter was the client's helper: 
`LinearSVCModel.coefficients.fget` was `_synced_property.<locals>.fget`. Every 
native property's getter, across all the native classes, now has PySpark's 
module, qualified name, signature, annotations and docstring.
   - **Models of a `RemoteModelRef`.** `LinearSVCModel(RemoteModelRef(id))` 
raised a bare `AssertionError`. It now makes the model of that server-side 
object for every native model class, as PySpark does over Spark Connect:
     - The model holds one reference of the object and releases it once when it 
is freed, also from a thread with no active session, so the object is deleted 
from the ML cache when PySpark deletes it.
     - `Params.copy` takes a reference of its own, and `copy.copy` shares the 
reference as PySpark's does. A transform keeps the object while it runs.
     - The release runs through `deliver::run_apart`, which keeps an exception 
that propagates meanwhile.
   - **Pickles that reach a model.** `pickle.dumps(t.exists())`, where `t` is a 
model's transform, succeeded. PySpark fails with the `TypeError` of a lock. A 
hidden core API, `PickleReach`, walks the functions, summary datasets and plans 
of a value in PySpark's order. The binding's `reach_reduce` makes `pickle` at 
every protocol, `cloudpickle` and `copy.deepcopy` fail at PySpark's first 
object with its error. This covers DataFrames, Columns, table arguments, 
grouped data and writers, through subqueries, `asTable`, lateral joins and UDTF 
table arguments.
   - **The extra members of `dir()`.** These are removed:
     - `__del__` of Pipeline, PipelineModel, OneVsRest, OneVsRestModel and the 
validators and their models;
     - `__signature__` of every class;
     - `_setParams` of feature stages;
     - `__deepcopy__` of summaries.
   
     The members that must stay are listed in the compat with their reason. 
Examples are the class-level lazy attributes and `__setstate__`, which reads 
the pickles of earlier versions.
   - **Garbage collection.** 13 native ML classes get a `__traverse__` and a 
`__clear__`, written in the terms of #921 (SPARK-62235): models, OneVsRest and 
its model, the feature stages, Pipeline and PipelineModel, the validators and 
their models, `_ParamAttribute` and the `fitMultiple` iterator.
     - They report their summaries, cached members, parameter values and 
`RemoteModelRef`s, so `model.summary.model = model` is collected, and the 
model's object is deleted from the ML cache.
     - A model's transform runs without the GIL under `gc::detach_of_values`, 
over the parts of its DataFrame and of its summary.
   - **Docs.** The compat paragraphs of each part are rewritten, and the KD's 
"Garbage collection" row states what remains. The DataFrames that a model keeps 
in Rust from its fit, its summary dataset and a OneVsRestModel's datasets, are 
not reported. So a cycle through a UDF of the training DataFrame that keeps the 
model is not collected yet. That is tracked as a follow-up.
   
   ### Why are the changes needed?
   
   The Python client is a drop-in for PySpark 4.3.0's Spark Connect client, so 
its ML classes should behave as PySpark's do, also in introspection, pickling 
and object lifetime.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for these behaviors of the native ML classes of `pyspark.ml`:
   - their property getters' metadata;
   - models built from a `RemoteModelRef`, which now work;
   - the errors of pickles and deep copies that reach a model;
   - the members that `dir()` lists;
   - the collection of reference cycles through a model.
   
   Each is now PySpark 4.3.0's.
   
   ### How was this patch tested?
   
   - **New tests that fail without the change:**
     - offline tests in `test_ml_offline.py` and 
`test_ml_subclasses_offline.py`;
     - a live test in `test_ml_api_e2e.py` for models of a `RemoteModelRef`, 
which also passes on PySpark 4.3.0;
     - core tests of `PickleReach`;
     - native ML calls in the clone check over calls without the GIL 
(`test_no_part_that_the_collector_sees_is_cloned_without_the_gil_by_a_call`);
     - the creation-time traverse test for the new `__traverse__`s.
   - **Mutation check.** Running the transform without its parts' busy marks 
fails the clone check in 3 of 3 runs.
   - **Probes against PySpark 4.3.0:**
     - members, `RemoteModelRef` models, copies, threads, exit and fork, and 
pickles of every shape match PySpark's, except the differences that the compat 
documents;
     - GC shapes at N=200: summary cycles and OneVsRest are collected as 
PySpark collects them;
     - a stress of 4 threads with concurrent `gc.collect` gives no errors.
   - **Review.** Independent reviews of each round, and a final review of the 
rebased change: safe to merge.
   - **Official ML tests through the drop-in runner (29 modules).** 155 passed. 
1 failed, `test_multinomial_logistic_regression_with_bound`, because the test 
server has no `data/mllib`.
   - **Performance.** The import of `pyspark.ml` is unchanged (763 ms vs 766 
ms). The pickle round trip of an estimator goes from 214 µs to 37 µs, and 
`copy.copy` of an estimator from 2.2 µs to 6.6 µs.
   - **The usual checks:**
     - the core tests in release and debug (1886);
     - the binding's own tests, clippy, rustdoc, rustfmt, ruff and the type 
stubs;
     - the vendored-files check, the no-stub audit and the tooling tests;
     - the offline suite under coverage (93.91%);
     - the e2e tests with protobuf (454) and without it (428);
     - the live ML tests.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code
   
   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