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

   ### What changes were proposed in this pull request?
   
   A follow-up of #789 (SPARK-61877) and #804 (SPARK-61923), from a round-2 
review of the pickles and `_java_obj` 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 params objects pickle at every protocol.**
   - At protocols 0 and 1, the estimators, transformers, evaluators, Pipelines, 
validators, OneVsRest and PrefixSpan raised `TypeError: Method __init__ forces 
keyword arguments.`.
   - Unbound models raised an `AssertionError`, or a `PicklingError` such as 
`it's not the same object as pyspark.ml.connect.pipeline.PipelineModel`.
   - PySpark's, Python objects, pickle at every protocol. A model of a 
server-side object raises the error of the pickle of the lock of its 
`RemoteModelRef` at the protocol: a `PicklingError` below 2, and the 
`TypeError` `cannot pickle '_thread.lock' object` from 2.
   - `params_api_block!` now has a `__reduce_ex__` (`params_api::reduce_ex`) 
that does the same. Anything else reduces with 
`object.__reduce_ex__(max(protocol, 2))`, which still honors a subclass's 
`__reduce__`.
   
   **2. DataFrames that keep a model are refused as PySpark refuses them.**
   - PySpark Connect pickles a DataFrame's plan before its session. So a 
DataFrame whose plan keeps a model raises the error of the model's lock, at a 
pickle, a cloudpickle and a `copy.deepcopy`. Such DataFrames are:
     - a transform of a fitted model, or a DataFrame that reads one;
     - a DataFrame of a method of a model, or of the summary of an `evaluate`.
   - The drop-in pickled them as any DataFrame: a plain pickle raised the 
`AttributeError` of the session's local function, and cloudpickle and deepcopy 
copied them.
   - A new doc-hidden `spark_connect::ml::pickle_block(df)` walks a plan and 
finds what it keeps:
     - a model (`ModelLock`): the DataFrame's `__reduce_ex__` raises the lock's 
error at the protocol, before the session;
     - a training summary (`SummarySession`): of a DataFrame of a method of a 
training summary, such as `summary.predictions`. Its reduce adds a lock to its 
state, so a plain pickle fails at the session and cloudpickle at the lock, in 
PySpark's order.
   - `__deepcopy__` raises the lock's `TypeError` after PySpark's schema 
requests of the DataFrames that it copies on the way.
   
   **3. `_java_obj`, `_call_java` and the summaries' attributes.**
   - The models of server-side objects had a `_java_obj` of None, and their 
summaries had no object id. So PySpark's own `_call_java(name, *args)`, which 
sends a Fetch over Spark Connect (`try_remote_call`), failed: `AssertionError` 
for a model, and `'NoneType' object has no attribute 'split'` for a summary.
   - Now, as in PySpark:
     - a model's `_java_obj` is the `RemoteModelRef` of its object, after a 
fit, a load, a copy, a fetch or a class method such as 
`StringIndexerModel.from_labels`;
     - a training summary has `"<id>.summary"`, and the `_summary_dataset` and 
`_remote_model_obj` that `try_remote_fit` sets, in its order;
     - the summary of an `evaluate` has the server's id of the summary, and 
`__source_transformer__`, the model;
     - a model with a training summary has `_summary`. It is a property, as 
PySpark's `Params.params` walk reads every attribute in `dir()` except 
properties.
   
   `docs/compatibility.md` describes these, with the remaining differences:
   - The DataFrames of the models of PySpark's own `pyspark.ml` classes, which 
build PySpark's Python plans over the drop-in's DataFrames, still pickle as 
DataFrames that keep no model. `pickle_block` cannot see a Python plan.
   - The `_java_obj` and `_summary` of a fitted or loaded model are set at 
their first read, so `vars(model)` lacks them until then.
   - `_remote_model_obj` is its own `RemoteModelRef` of the same id, where 
PySpark shares the model's.
   - A Pipeline, a OneVsRest, a validator and their models have a `_java_obj` 
of None, which PySpark's do not have. This predates this PR.
   
   ### Why are the changes needed?
   
   So that the ML objects and the DataFrames that keep them pickle, or refuse 
to, as PySpark's do, and PySpark's `_call_java` works on the drop-in's models 
and summaries.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, as above:
   - params objects pickle at protocols 0 and 1;
   - DataFrames that keep a model raise the model lock's error at a pickle, a 
cloudpickle and a deepcopy;
   - models and summaries have PySpark's `_java_obj` and attributes.
   
   ### How was this patch tested?
   
   - **New tests.** Each fails on master.
     - `test_ml_subclasses_offline.py`: 
`test_the_params_objects_pickle_at_every_protocol_as_pysparks`, and 
`test_a_model_of_no_server_side_object_has_no_summary_of_pysparks_params_walk`. 
Both pass on pyspark-client 4.3.0 too.
     - `test_ml_offline.py`:
       - 
`test_the_models_and_summaries_of_fits_have_pysparks_java_obj_of_their_call_java`;
       - 
`test_a_fitted_model_and_the_dataframes_that_keep_it_are_not_pickled_as_pysparks`;
       - `test_a_summary_of_a_fit_or_an_evaluation_is_not_pickled_as_pysparks`, 
extended to every protocol, cloudpickle and the counts of the analyze requests.
     - Live, `test_ml_subclasses_e2e.py`: 
`test_the_models_and_summaries_have_pysparks_java_obj_of_their_call_java`, and 
`test_copy_deepcopy_and_pickle_of_a_model_and_its_summary`, extended.
     - In the core: `the_pickle_blocks_of_plans_are_those_of_pysparks_plans`.
   - **Probes of both clients** give the same lines:
     - the pickles at protocols 0 and 1;
     - `_java_obj` and `_call_java`;
     - the pickles, cloudpickles and deep copies of the summaries, transforms 
and evaluation DataFrames of 10 model families (131 lines).
   - **Python.**
     - The ML offline files on pandas 2 and 3.
     - The offline suite of `coverage.yml`.
     - The live ML files.
   - **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` 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