Skip to content

Move NIF memory allocation boundary - #7

Merged
iperks merged 31 commits into
mainfrom
ip/dmatrix-interface
Jul 30, 2026
Merged

Move NIF memory allocation boundary#7
iperks merged 31 commits into
mainfrom
ip/dmatrix-interface

Conversation

@iperks

@iperks iperks commented Jul 24, 2026

Copy link
Copy Markdown

See details in CHANGELOG.md and CONTRIBUTING.md

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.ArrayInterface and 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-native does 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.

Comment thread test/exgboost_test.exs Outdated
Comment thread test/nif_test.exs Outdated
Comment thread CHANGELOG.md Outdated
Comment thread c/exgboost/src/utils.c Outdated
Comment thread c/exgboost/src/utils.c Outdated
Comment thread lib/exgboost/array_interface.ex
Comment thread lib/exgboost/dmatrix.ex
Comment thread lib/exgboost/nif.ex
iperks and others added 10 commits July 24, 2026 10:02
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 iperks changed the title Ip/dmatrix interface Move NIF memory allocation boundary Jul 24, 2026
@iperks
iperks marked this pull request as ready for review July 24, 2026 10:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread lib/exgboost/dmatrix.ex
@doc """
Slice the DMatrix and return a new DMatrix that only contains rindex.
"""
@spec slice(any(), Nx.Tensor.t()) :: reference()
Comment thread c/exgboost/src/dmatrix.c
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 thread c/exgboost/src/utils.c
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;
}
Comment thread lib/exgboost/nif.ex
@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
iperks force-pushed the ip/dmatrix-interface branch from 35b7fbb to 54be361 Compare July 29, 2026 15:35
@iperks
iperks merged commit bef912b into main Jul 30, 2026
7 checks passed
@iperks
iperks deleted the ip/dmatrix-interface branch July 30, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants