Move NIF memory allocation boundary - #7
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the Elixir↔NIF boundary away from JSON-encoded NumPy Array Interface strings and toward passing Array Interface components directly (binary/typestr/shape/readonly), primarily to improve memory safety and remove address-based APIs.
Changes:
- Replace JSON Array Interface arguments with component-based arguments across DMatrix creation, info-setting, and prediction NIFs.
- Remove pointer/address exposure from
EXGBoost.ArrayInterfaceand adjust tensor reconstruction to use stored binaries. - Add C-side validation helpers (typestr/shape/readonly/binary size) and add/adjust tests + changelog to reflect safety work.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| test/nif_test.exs | Updates NIF tests to use component-based Array Interface calls; adds safety validation tests. |
| test/exgboost_test.exs | Updates ArrayInterface tests for the new struct shape and tensor reconstruction behavior. |
| lib/exgboost/nif.ex | Updates NIF specs/docs to accept Array Interface components instead of JSON strings; deprecates unsafe APIs. |
| lib/exgboost/dmatrix.ex | Updates DMatrix APIs to pass Array Interface components; changes quantile cut handling to consume NIF-returned maps. |
| lib/exgboost/array_interface.ex | Removes address/tensor caching; reconstructs tensors directly from stored binaries. |
| lib/exgboost.ex | Updates prediction paths to pass Array Interface components to NIFs for dense/CSR inputs. |
| CHANGELOG.md | Adds a changelog entry describing safety/interface changes. |
| c/exgboost/src/utils.c | Adds validation helpers + atom caching and array-interface JSON builder; adjusts integer safety/conversions. |
| c/exgboost/src/exgboost.c | Updates NIF function arities and initializes cached atoms on load/upgrade. |
| c/exgboost/src/dmatrix.c | Updates DMatrix NIFs to build Array Interface JSON from components; adds size checks and quantile cut map return. |
| c/exgboost/src/booster.c | Updates prediction NIFs to build Array Interface JSON from components; adds size checks for VLAs. |
| c/exgboost/include/utils.h | Declares new helpers and atom init; documents deprecated unsafe APIs. |
Comments suppressed due to low confidence (1)
test/nif_test.exs:732
- Same endianness issue:
float-32-nativedoes not necessarily match typestr "<f4". Use little-endian encoding here so the test is portable.
small_binary = <<1.0::float-32-native, 2.0::float-32-native>>
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
iperks
marked this pull request as ready for review
July 24, 2026 10:32
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 15 out of 17 changed files in this pull request and generated 5 comments.
Comments suppressed due to low confidence (1)
lib/exgboost/dmatrix.ex:155
- slice/3 passes allow_groups as a boolean, but the underlying NIF expects an int (0 or 1). This will cause allow_groups parsing to fail in the NIF when called via EXGBoost.DMatrix.slice/3.
opts = Keyword.validate!(opts, allow_groups: false)
allow_groups = Keyword.fetch!(opts, :allow_groups)
EXGBoost.NIF.dmatrix_slice(dmat.ref, Nx.to_binary(r_index), allow_groups)
end
| @doc """ | ||
| Slice the DMatrix and return a new DMatrix that only contains rindex. | ||
| """ | ||
| @spec slice(any(), Nx.Tensor.t()) :: reference() |
Comment on lines
826
to
+830
| ret = | ||
| exg_ok(env, enif_make_tuple3( | ||
| env, enif_make_list_from_array(env, indptr, num_rows + 1), | ||
| enif_make_list_from_array(env, indices, num_non_missing), | ||
| enif_make_list_from_array(env, data, num_non_missing))); | ||
| env, enif_make_list_from_array(env, indptr, (size_t)(num_rows + 1)), | ||
| enif_make_list_from_array(env, indices, (size_t)num_non_missing), | ||
| enif_make_list_from_array(env, data, (size_t)num_non_missing))); |
Comment on lines
+371
to
+380
| while (pos + needed + 2 >= capacity) { // +2 for ']' and '\0' | ||
| capacity *= 2; | ||
| char *new_json = enif_realloc(json, capacity); | ||
| if (new_json == NULL) { | ||
| enif_free(json); | ||
| *error_msg = "Failed to reallocate memory for shape JSON"; | ||
| return 0; | ||
| } | ||
| json = new_json; | ||
| } |
| @type dmatrix_reference :: reference() | ||
| @type booster_reference :: reference() | ||
| @type exgboost_return_type(return_type) :: {:ok, return_type} | {:error, String.t()} | ||
| @type feature_score_result :: {[String.t()], tuple(), [float()]} |
| %{data: [4418559984, true], shape: [2, 3], typestr: "<i8", version: 3} | ||
| ## Examples | ||
|
|
||
| iex(1)> tensor = EXGBoost.ArrayInterface.from_tensor(Nx.tensor([[1,2,3],[4,5,6]])) |
iperks
force-pushed
the
ip/dmatrix-interface
branch
from
July 29, 2026 15:35
35b7fbb to
54be361
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See details in
CHANGELOG.mdandCONTRIBUTING.md