Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 48 additions & 58 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,58 @@
`latu_ml` follows [Semantic Versioning](https://semver.org). Before 1.0, a minor version may
rename or remove; each such change is listed here with the migration in one line.

## Unreleased

**The `latu` requirement is now `~> 0.4`**, tightened from `~> 0.3`. Nothing here needs a
Latu 0.4 feature; the old constraint resolved any Latu below 1.0, and Latu's own changelog
says a minor version may rename or remove, so it permitted a Latu release that broke this
package silently. Bump `latu` alongside `latu_ml`.

## 0.1.0 — 2026-09-07

First release, against Spark **4.2.0**, and a companion to `latu ~> 0.3`.

Spark MLlib from Elixir over Spark Connect: `fit`, `transform`, `evaluate`, model and summary
attributes, `save`/`load` in Spark's own on-disk format, the model cache and its bracket, the
`ConnectHelper` route, pipelines and grid search. The operator surface is **generated** — 111
`ConnectHelper` route, pipelines and grid search. The operator surface is **generated**: 111
rows carrying 991 params, extracted from PySpark's own classes and the server's own
`ALLOWED_ATTRIBUTES` rather than transcribed — giving 68 constructors and 623 accessors across
64 model and summary modules. All 66 runnable operators have been run against a live server.

Every place the API departs from `pyspark.ml` is in `docs/deviations.md`, with why. What
follows is what each milestone added, oldest last.

`Latu.ML.Functions.vector_to_array/2` and `array_to_vector/1`: the way to read a `Vector`
column, which `Latu.collect/2` refuses. Spark keeps both in an *internal* function registry
rather than the builtin one, so they are unreachable from SQL and reachable over Connect — the
wire form is a plain `UnresolvedFunction` with `is_internal` left unset. `docs/deviations.md`
has the whole of it. `Summarizer` uses the same route and is not wrapped yet: reading a metric
out of the struct `aggregate_metrics` answers with needs field access Latu does not build.

`Latu.ML.evaluate/2` runs an evaluator; `Latu.ML.save/3` and `Latu.ML.load/3` write a model or
an unfitted operator to Spark's own on-disk format and read one back. All six evaluators are
`:probed` as a result, and `dev/README.md` has the interop dance that shows PySpark reading what
this wrote and the reverse.

The `ConnectHelper` route: `Latu.ML.Stat`'s three statistical tests, `Latu.ML.assign_clusters/2`
and `find_frequent_sequential_patterns/2` for the two operators that are neither fitted nor
applied, `from_labels/3` and its siblings generated onto their model modules,
`Latu.ML.Feature.load_default_stop_words/2`, and `Latu.ML.helper/3` under all of them. The
registry grew a fifth operator kind and a helpers section, both extracted; `Latu.ML.Plan` grew
nested lists, which also closes `Bucketizer.splitsArray`.

Pipelines: `Latu.ML.pipeline/1` builds one, `Latu.ML.fit/2` folds a frame through its stages,
and `save/3` and `load/3` carry both a fitted and an unfitted one in Spark's own directory
layout. There is no wire form for a pipeline in any client, so the fold is this package's own —
`dev/probe_ml.exs`'s pipelines section checks it lands where fitting the stages by hand does,
and `--interop` has PySpark reading a pipeline Latu wrote and the reverse. The layout is
PySpark's, which Scala cannot read; `docs/deviations.md` says so plainly and `docs/decisions.md`
has the one key it turns on.

Tuning: `Latu.ML.param_grid/1,2` builds a grid as data, `cross_validator/1` and
`train_validation_split/1` search one, and `fit/2` runs the search — folds cut with
`rand(seed)` and a range as Spark does it, one fit at a time, the winner refit on the whole
frame. A sub-model is released as its metric is read, so a five-by-six search holds one cache
entry rather than thirty; `collect_sub_models: true` keeps them and `delete/1` gives them back.
`larger_better?/1` and `best_index/1` are public, read off the six client-side `isLargerBetter`
overrides the registry now extracts. `dev/pyspark_oracle.py --interop` prints PySpark's metrics
for the same grid, seed and rows, which is the only check either side has.

`save/3` and `load/4` carry a search too, in Spark's own layout: `metadata` with the
validator's params and its encoded grid, `estimator/` and `evaluator/`, and for a fitted one
`bestModel/` and the metrics. `sub_models: true` writes the fold models and asks for them back
— **a plain load leaves them on disk**, which is where this deviates from PySpark and why is in
`docs/deviations.md`. PySpark reads a search this wrote and the reverse; `dev/README.md` has
the dance.

Documentation, to Latu's standard: a [quick start](docs/guides/quick-start.md) and a
[cookbook](docs/guides/cookbook.md) — a recipe per model family, plus pipelines, grid search and
cleaning up — both executed by `test/integration/guides_test.exs` like the two "coming from"
guides. `usage-rules.md` ships for agents in the `usage_rules` convention, and
`docs/cheatsheet.cheatmd` puts every hand-written verb on one page with
`test/latu/cheatsheet_test.exs` failing on either kind of drift. The README gained an install
section, a server to point at and links to all of it; `CONTRIBUTING.md` carries the extractors,
the oracle, the probe and the release checklist.
`ALLOWED_ATTRIBUTES` rather than transcribed. That gives 68 constructors and 623 accessors
across 64 model and summary modules. All 66 runnable operators have been run against a
live server.

Every place the API departs from `pyspark.ml` is in `docs/deviations.md`, with why.

- **Operators and models.** `Latu.ML.fit/2` and `transform/2`, `attribute/2` and
`attribute_frame/2` for what a fitted model knows, and a generated accessor module per model
and summary class. A model is a handle into a server-side cache, not a value. `with_model/3`
brackets one and `delete/1` gives it back.
- **Reading a `Vector` column.** `Latu.ML.Functions.vector_to_array/2` and `array_to_vector/1`,
which is what `Latu.collect/2` refuses. Spark keeps both in an internal function registry
rather than the builtin one, so they are unreachable from SQL and reachable over Connect. The
wire form is a plain `UnresolvedFunction` with `is_internal` left unset. `Summarizer` takes
the same route and is not wrapped: reading one metric out of the struct it answers with needs
field access Latu does not build.
- **Evaluation and persistence.** `Latu.ML.evaluate/2` runs an evaluator, and `Latu.ML.save/3`
and `Latu.ML.load/3` carry a model or an unfitted operator through Spark's own on-disk
format. All six evaluators are `:probed` as a result. PySpark reads what this writes and the
reverse; `dev/README.md` has the interop dance.
- **The `ConnectHelper` route.** `Latu.ML.Stat`'s three statistical tests,
`Latu.ML.assign_clusters/2` and `find_frequent_sequential_patterns/2` for the two operators
that are neither fitted nor applied, `from_labels/3` and its siblings generated onto their
model modules, `Latu.ML.Feature.load_default_stop_words/2`, and `Latu.ML.helper/3` under all
of them.
- **Pipelines and tuning.** `Latu.ML.pipeline/1` builds a pipeline and `Latu.ML.param_grid/1,2`
builds a grid as data; `cross_validator/1` and `train_validation_split/1` search one. None of
them has a wire form in any client, so the folds are this package's own, cut with `rand(seed)`
and a range as Spark does it. A sub-model is released as its metric is read, so a five-by-six
search holds one cache entry rather than thirty. `collect_sub_models: true` keeps them.
`larger_better?/1` and `best_index/1` are public. A saved search carries its encoded grid, and
`sub_models: true` writes the fold models and asks for them back. **A plain load leaves them
on disk**, which is where this deviates from PySpark.
- **Documentation.** A [quick start](docs/guides/quick-start.md) and a
[cookbook](docs/guides/cookbook.md), a recipe per model family, both executed by
`test/integration/guides_test.exs` like the two "coming from" guides. `usage-rules.md` ships
for agents in the `usage_rules` convention, and `docs/cheatsheet.cheatmd` puts every
hand-written verb on one page with `test/latu/cheatsheet_test.exs` failing on either kind of
drift. `CONTRIBUTING.md` carries the extractors, the oracle, the probe and the release
checklist.
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ load-bearing enough to restate.

## What this package may assume of Latu

Latu **0.3.0** — the pin is `~> 0.3` — exposes exactly five things for this package, and no
Latu **0.4.0**, pinned `~> 0.4`, exposes exactly five things for this package, and no
more:

- `Latu.Plan.new/1` wraps a `Relation` or a `Command` as a `Plan`.
Expand Down
115 changes: 49 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ package to [Latu](https://hexdocs.pm/latu).

The algorithms run on the cluster, where the data is. The Elixir side holds plans and handles.

MLlib is DataFrame in, DataFrame out. There is no `fit(x, y)` on tensors: features go into one
`Vector` column, and a fitted model scores a frame.

## Install

```elixir
def deps do
[
{:latu, "~> 0.3"},
{:latu, "~> 0.4"},
{:latu_ml, "~> 0.1"}
]
end
```

Requires Elixir ~> 1.20 and a Spark **4.2.0** Connect server — the same one Latu targets, and
the only one either package claims.
Requires Elixir ~> 1.20 and a Spark **4.2.0** Connect server, the same one Latu targets.

## A server to talk to

Expand All @@ -36,8 +38,8 @@ docker run -p 15002:15002 apache/spark:4.2.0 \

## The lines at the top of your file

`Latu.ML` is called qualified, the way `Latu` is. The operator groups are aliased because you
name one every time you build something, and they are PySpark's own grouping:
`Latu.ML` is called qualified, the way `Latu` is. There are seven operator groups, PySpark's own
grouping, and you name one every time you build something. Alias the ones you need:

```elixir
alias Latu.ML
Expand Down Expand Up @@ -65,34 +67,29 @@ features = ML.transform(assembler, training)
end)
```

Four things are happening there, and three of them are the whole package. `ML.transform/2` is a
**lazy builder** — it hands back a `Latu.DataFrame` and reaches no server. `ML.fit/2`, which
`ML.with_model/3` calls, is an **action**, and what it hands back is a reference into a
server-side cache rather than a value. And `ML.with_model/3` deletes that reference in an
`after`, because the BEAM has no finalizer to do it for you.
Three things there are the whole package. `ML.transform/2` is a **lazy builder**. It hands back a
`Latu.DataFrame` and reaches no server. `ML.fit/2` is an **action**, and what it hands back is a
reference into a server-side cache rather than a value. `ML.with_model/3` deletes that reference
in an `after`, because the BEAM has no finalizer to do it for you.

This page shows the shape of the API. The
[quick start](https://hexdocs.pm/latu_ml/quick-start.html) shows results, and every line of it
is executed by the test suite — which is why the numbers there can be trusted and the ones in a
README cannot.
The [quick start](https://hexdocs.pm/latu_ml/quick-start.html) is the same walk with the results
in it, executed against a live server.

## What is in it

**Every operator the 4.2.0 server exposes**, generated rather than transcribed: 111 rows
carrying 991 params, extracted from PySpark's own classes and the server's own Scala. That is
68 constructors — 42 estimators, 18 transformers, 6 evaluators and the 2 operators that are
neither — and 623 accessors across 64 model and summary modules.
**Every operator the 4.2.0 server exposes**, generated rather than transcribed: 111 rows carrying
991 params, read off PySpark's own classes and the server's own Scala. That is 68 constructors,
and 623 accessors across 64 model and summary modules.

**The verbs.** `fit`, `transform`, `evaluate`, `attribute`, `attribute_frame`, `summary`,
`save`, `load`, `delete`, `with_model`, `model_size`, `cache_info`, `clean_cache`. Actions
return `{:ok, _} | {:error, %Latu.Error{}}` with `!` twins; builders return a struct or a
`Latu.DataFrame`.

**Attributes off a fitted model**, as `Nx` tensors, `Latu.ML.SparseVector`s or lazy frames —
`coefficients`, `feature_importances`, cluster centres, a training summary's ROC curve. What
you may ask for is the server's own allowlist, and the generated accessor per class is the
discovery surface: `h Latu.ML.Regression.LinearRegressionTrainingSummary.r2` prints Spark's
own prose.
**Attributes off a fitted model**, as `Nx` tensors, `Latu.ML.SparseVector`s or lazy frames.
`coefficients`, `feature_importances`, cluster centres, a training summary's ROC curve. What you
may ask for is the server's own allowlist, and the generated accessor per class is the discovery
surface: `h Latu.ML.Regression.LinearRegressionTrainingSummary.r2` prints Spark's own prose.

**Pipelines and tuning**, client-side on both sides of the wire because Spark exposes no `Fit`
for either: `Latu.ML.pipeline/1`, `Latu.ML.param_grid/2`, `Latu.ML.cross_validator/1` and
Expand All @@ -103,16 +100,12 @@ for either: `Latu.ML.pipeline/1`, `Latu.ML.param_grid/2`, `Latu.ML.cross_validat
`Latu.ML.find_frequent_sequential_patterns/2`, and the model constructors that build a
`StringIndexerModel` from labels or a `CountVectorizerModel` from a vocabulary.

**Two column functions**, `Latu.ML.Functions.vector_to_array/2` and
`Latu.ML.Functions.array_to_vector/1` — the way to read a `Vector` column, which
`Latu.collect/2` refuses because Spark describes it as a UDT and declines to say what it
serialises as.
**Two column functions**: `Latu.ML.Functions.vector_to_array/2` and
`Latu.ML.Functions.array_to_vector/1`. They are the way to read a `Vector` column, which
`Latu.collect/2` refuses: Spark describes it as a UDT and declines to say what it serialises as.

**Not here.** Anything closure-shaped — `predict_batch_udf`, and `xgboost.spark`, whose
training is Python code shipped to Python workers even on Connect. The RDD-era `spark.mllib`.
Building a model out of parameters you already hold: a model exists only by a fit or a read,
and there is no wire path for the other direction. `OneVsRest`. And the deprecated torch-based
`pyspark.ml.connect` package from 3.4 and 3.5, which is not what any of this mirrors.
**Not here.** Anything closure-shaped, the RDD-era `spark.mllib`, `OneVsRest`, and building a
model out of parameters you already hold.

## What this is

Expand All @@ -125,47 +118,42 @@ Remote MLlib, not a local one. Two things are load-bearing:

2. **The surface is generated from two extractors and checked by a probe.** Constructors,
params, accessors and their docs come from `priv/ml_operators.exs` and
`priv/ml_attributes.exs`, which are read off PySpark's classes and the server's `MLUtils`
rather than typed by hand. Every constructor's `@doc` says whether a live server has
actually run it — `:probed` or `:built` — so `h` tells you what is verified rather than what
is claimed.
`priv/ml_attributes.exs`. Those are read off PySpark's classes and the server's `MLUtils`,
not typed by hand. Every constructor's `@doc` says whether a live server has run it.

`Scholar` runs classical ML on one node, in memory, and hands you a model you can inspect and
ship. This runs MLlib on a cluster and hands you a reference. Reach for Scholar when the
training set fits a node; reach for this when it does not, or when the features already live in
a lakehouse. [With Nx and Scholar](https://hexdocs.pm/latu_ml/with-scholar-and-nx.html) is
that seam, measured.
training set fits a node; reach for this when it does not, or when the features already live in a
lakehouse. [With Nx and Scholar](https://hexdocs.pm/latu_ml/with-scholar-and-nx.html) is that
seam.

## Status

**0.1.0, and pre-1.0 in the way that matters**: a minor version may rename or remove, and
`CHANGELOG.md` carries the migration in one line when it does.

All 66 runnable operators are `:probed` — fitted, applied or evaluated against a live 4.2.0
server, with every allowlisted attribute answered — and so are the eleven `ConnectHelper`
methods. No operator is `:missing`. Six saved directories cross over between PySpark and this
package in both directions.
Every runnable operator has been fitted, applied or evaluated against a live 4.2.0 server, with
every allowlisted attribute answered.

## Where to go next

* [Quick start](https://hexdocs.pm/latu_ml/quick-start.html)
— connect, fit, read, score, release; every line of it executed
* [Cookbook](https://hexdocs.pm/latu_ml/cookbook.html)
— a recipe per model family, and the ones for pipelines, grid search and cleaning up
* [Coming from PySpark ML](https://hexdocs.pm/latu_ml/from-pyspark-ml.html)
— the five differences that matter, and what a model *is* here
* [With Nx and Scholar](https://hexdocs.pm/latu_ml/with-scholar-and-nx.html)
— getting numbers back onto the BEAM, where that is worth it, and where it stops working
* [`docs/cheatsheet.cheatmd`](https://hexdocs.pm/latu_ml/cheatsheet.html)
— every verb on one page
* [`usage-rules.md`](https://hexdocs.pm/latu_ml/usage-rules.html)
— the short set of rules that are not guessable from the function names, in the
* [Quick start](https://hexdocs.pm/latu_ml/quick-start.html). Connect, fit, read, score,
release. Every line of it is executed.
* [Cookbook](https://hexdocs.pm/latu_ml/cookbook.html). A recipe per model family, and the ones
for pipelines, grid search and cleaning up.
* [Coming from PySpark ML](https://hexdocs.pm/latu_ml/from-pyspark-ml.html). The five
differences, and what a model *is* here.
* [With Nx and Scholar](https://hexdocs.pm/latu_ml/with-scholar-and-nx.html). Getting numbers
back onto the BEAM, and where that stops working.
* [Cheatsheet](https://hexdocs.pm/latu_ml/cheatsheet.html). Every verb on one page.
* [`usage-rules.md`](https://hexdocs.pm/latu_ml/usage-rules.html). The short set of rules that
are not guessable from the function names, in the
[`usage_rules`](https://github.com/ash-project/usage_rules) convention, so an agent can sync
it into its context
* [`docs/deviations.md`](https://hexdocs.pm/latu_ml/deviations.html)
— every place the API departs from `pyspark.ml`, and why
* [`CONTRIBUTING.md`](https://hexdocs.pm/latu_ml/contributing.html)
— the servers, the two extractors, the oracle and the probe
it into its context.
* [`docs/deviations.md`](https://hexdocs.pm/latu_ml/deviations.html). Every place the API
departs from `pyspark.ml`, and why.
* [`CONTRIBUTING.md`](https://hexdocs.pm/latu_ml/contributing.html). The servers, the two
extractors, the oracle and the probe.

Latu's own [docs](https://hexdocs.pm/latu) cover the DataFrame API this is built on; its
[usage rules](https://hexdocs.pm/latu/usage-rules.html) are worth having in context alongside
Expand All @@ -185,12 +173,7 @@ mix check.all # the above plus integration, against sc://localhost:15003

**Claude (Anthropic) wrote the overwhelming majority of the code, the tests and the
documentation.** The maintainers set the scope, made the design calls, ran every gate and
reviewed the result — the same division as Latu's, stated for the same reason.

What holds it up is the apparatus rather than the review: two extractors that reproduce the
committed registries byte for byte, a PySpark oracle that captures the protobuf PySpark *would*
have sent, a live probe that has run every operator, and `docs/decisions.md`, which records the
reasoning behind every non-obvious choice.
reviewed the result. The same division as Latu's, stated for the same reason.

## License

Expand Down
Loading