HyukjinKwon opened a new pull request, #793:
URL: https://github.com/apache/spark-connect-rust/pull/793
### What changes were proposed in this pull request?
PySpark 4.3.0's `try_remote_fit` logs the fit result's `warning_message`
(ml/util.py:71 and 192-194):
```python
if warning_msg := getattr(model_info, "warning_message", None):
_logger.warning(warning_msg)
```
`_logger` is `logging.getLogger("pyspark.ml.util")`. It logs right after the
Fit's request and before the model is built.
**When the server sets it.** When a tree's training stops early under the ML
cache's memory control (MLHandler.scala:234-247). The messages come from
RandomForest.scala:236-249, which DecisionTree uses too, and
GradientBoostedTrees.scala:524-534. The limit is the session conf
`spark.connect.session.connectML.mlCache.memoryControl.maxModelSize`
(Connect.scala:436).
**The gap.** Such a model is truncated, and PySpark logs, for example:
```
WARNING pyspark.ml.util: The random forest training stops early because the
model size (35016 bytes) exceeds threshold (24000 bytes).
```
The drop-in's native fits, and the Rust API's, read only the model's
reference and uid of the fit result, so nothing told the user that the model
was truncated.
With this PR:
- **The core.**
- `fit_via_command` gives a non-empty `warning_message` to the new
`SparkSession::set_ml_fit_warning_handler`. That is a handler per session,
which clones share and `new_session()` does not, as
`set_observation_metrics_check`. It is called as soon as the Fit's result
arrives, before the model is built. Its error is the fit's error.
- The fitted model keeps the warning, `Model::fit_warning()`. Models of
fetches and loads have none.
- **The binding.**
- Each session sets the handler, which logs the warning through PySpark's
own `pyspark.ml.util._logger.warning`, from PySpark's location, `util.py:194`
in `wrapped`.
- So the record's logger, level, message, location and order among the
requests are PySpark's, and a logging filter or handler that raises makes the
fit raise, as on PySpark.
- Every native fit goes through the core's fit, so this covers:
- the estimators of every family, including the feature, fpm and
recommendation ones;
- QuantileDiscretizer, whose warning comes before its `getSplits` fetch;
- the native OneVsRest's class fits, on their threads;
- Pipelines and the validators, which call their stages' fits.
`docs/compatibility.md` describes both.
### Why are the changes needed?
So that a user learns, as on PySpark, that the server stopped a tree's
training early and the model is smaller than asked.
### Does this PR introduce _any_ user-facing change?
Yes:
- a native fit whose result has a warning logs it, as PySpark does;
- in the Rust API, `SparkSession::set_ml_fit_warning_handler` and
`Model::fit_warning()` are new.
Remaining differences:
- The records of the native OneVsRest's class fits come from Rust worker
threads, so their `threadName` is `Dummy-N`, where PySpark's are `Thread-N
(worker)`.
- The server's threshold is a global of the server, which each ML command
resets. A live test that sets it is safe one at a time, as CI runs them.
### How was this patch tested?
- **A probe of 14 fit scenarios against a Spark 4.3.0 server** gives the
same 21 log records on both clients: logger, level, message, location and order
among the requests. Before this PR the drop-in gave none. The scenarios:
- RandomForest, GBT, DecisionTree and RandomForestClassifier, and
LogisticRegression, which gives no warning;
- a param map, a list of maps and `fitMultiple`;
- a Pipeline, OneVsRest, CrossValidator and TrainValidationSplit;
- the conf unset;
- a filter that raises: both clients raise its `ValueError` from `fit`,
with only the Fit sent.
- **New tests.**
- `ml_golden_replay.rs`: 4 offline tests of a golden fit with a
`warning_message`.
- The warning goes to the handler after the Fit alone, and stays on the
model.
- There is none of a fit without a warning or with an empty one.
- The handler's error is the fit's.
- OneVsRest's class fits hand theirs over in order.
- `e2e_random_forest_regressor.rs`: a live test of `fit_warning()` and the
handler, with and without the conf.
- `test_ml_offline.py`: 4 offline tests of the records and their order,
including OneVsRest's threads, a raising filter, and no warning.
- `test_ml_e2e.py`: a live test of RandomForest, a Pipeline, OneVsRest and
CrossValidator, which passes on the drop-in and on pyspark-client 4.3.0.
- **Python.** The offline suite of `coverage.yml`. The ML offline files on
pandas 3 after the rebase on #791. Live `test_ml_e2e.py` and
`test_tree_api_e2e.py`.
- **Rust.**
- `cargo test -p apache-spark-connect --release`;
- the live tree e2e files;
- `--no-run` with and without `wasm-udf`.
- **Lint.** `cargo fmt --all --check`, `cargo +1.100.0 clippy -p
apache-spark-connect -p pyspark-rs --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]