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

   ### What changes were proposed in this pull request?
   
   Every model class of `pyspark.ml` constructs with no arguments, as 
`JavaModel(java_model=None)`: a model of a new uid of its class and of its 
params, none set, of no server-side object (official pyspark-client 4.3.0, 
`default_instance: true` in `tests/golden/ml_api.json`). Such a model has 
PySpark's contract:
   - its Params API, `copy` and `write()` work;
   - its members of the server, `__repr__`, a transform and a save raise a bare 
`AssertionError`;
   - the `params` walk of a model with `functools.cached_property` members (the 
`trees` of a tree ensemble) raises it too, and so do that model's 
`explainParams` and `copy`;
   - over Spark Connect, another `java_model` is an `AssertionError` (`assert 
isinstance(java_model, RemoteModelRef)`).
   
   No native model class could be constructed: `KMeansModel()`, 
`LinearSVCModel()` and `LDAModel()` raised `TypeError: cannot create ... 
instances`. This PR changes the PyO3 base:
   
   - **`native_model!`** gives each model class, and the base-class form, a 
`#[new]` of `java_model=None`. It makes the model of `ModelCore::unbound`: a 
core of a placeholder model with no server object, of the defaults derived from 
its uid (`params_api::uid_defaults_of`, the `outputCol` of 
`HasOutputCol.__init__`). Another `java_model` raises PySpark's 
`AssertionError`. LDA's two subclasses construct the same way.
   - **An unbound core** raises the bare `AssertionError`:
     - from its `target` (the `_call_java` of every member of the server, and 
`__repr__`);
     - from its `transform`, before the params are serialized, as 
`try_remote_transform_relation` asserts first;
     - from the `save` of its writer: `write()` returns the writer, as 
PySpark's `RemoteMLWriter`.
   - **`ParamsHolder::walk`** is the walk of PySpark's `Params.params`, which 
reads every attribute of the object except its properties. The `params` getter, 
`explainParams` and `copy` (as `_copyValues`) run it. A model's walk reads its 
cached members (its `__prime`, as a fit or a load does). For a fitted or loaded 
model they are already made; an unbound tree ensemble raises the 
`AssertionError` there, as PySpark's does.
   
   ### Why are the changes needed?
   
   The native classes are to follow `pyspark.ml` 4.3.0, whose model classes 
construct without a server-side object.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes: `KMeansModel()` and the other native model classes construct, with 
PySpark's behavior as above, instead of raising `TypeError`. Fitted and loaded 
models are unchanged.
   
   ### How was this patch tested?
   
   - A new golden of official pyspark-client 4.3.0, 
`tests/golden/ml_unbound_models.json` 
(`scripts/capture_ml_unbound_models_golden.py`, cases in 
`python/tests/_ml_unbound_cases.py`), covering the 43 model classes of 
`JavaModel(java_model=None)`. For each it records: the uid; each param's 
`isSet`, `hasDefault` and default (`seed` as the hash of the class name); and 
the outcomes of the `params` walk, `explainParams`, `extractParamMap`, `copy`, 
`repr`, `transform`, `write` and `save`. `test_ml_api_e2e.py` compares the 
native classes with it, and all 43 are identical; the test passes on official 
PySpark too.
   - `test_ml_api_offline.py` constructs each of them (`cls()`, `cls(None)`, 
`cls(java_model=None)`): of its class and a new uid of it, with no param set. 
It passes on ours and on official.
   - 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