Skip to content

[python-package] do not copy column-major numpy arrays when creating Dataset from list of arrays - #6773

Merged
jameslamb merged 4 commits into
masterfrom
no-copy-mats
Jan 26, 2025
Merged

jameslamb merged 4 commits into
masterfrom
no-copy-mats

Conversation

@jmoralez

@jmoralez jmoralez commented Jan 2, 2025 •

Copy link
Copy Markdown
Collaborator

Following the suggestion in #6751 (comment) this changes the signature of the C API LGBM_DatasetCreateFromMats function to take an array of integers as is_row_major, indicating the layout of each matrix. Also updates the lightgbm.Dataset.__init_from_list_np2d method to provide this setting and avoid copying the arrays in the list.

@jmoralez

jmoralez commented Jan 3, 2025

Copy link
Copy Markdown
Collaborator Author

This is a breaking change in the C API, so I can maybe split this into two PRs, one making the change to the C API only and mark it as a breaking change, and then implement the logic in the python package and mark it as efficiency. WDYT @StrikerRUS @jameslamb?

@jmoralez
jmoralez marked this pull request as ready for review January 3, 2025 16:38

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this. I think we should NOT break the ABI just for this purpose. What do you think about introducing a new function in the C API like LGBM_DatasetCreateFromMatsMixedLayouts()?

Most of the implementation of LGBM_DatasetCreateFromMats() could be moved into this function, and then LGBM_DatasetCreateFromMats() could internally construct a vector of is_row_major and call this new function.

You will probably have a better recommendation for the name, but what do you think about this general idea?

In my opinion it's worth growing the C API a little bit in exchange for not breaking other projects that link against lib_lightgbm. I found a few of those on GitHub (with this search):

And we can use the docs on the function to clearly describe what the difference is.

@jameslamb

Copy link
Copy Markdown
Member

This is a breaking change in the C API, so I can maybe split this into two PRs, one making the change to the C API only and mark it as a breaking change, and then implement the logic in the python package and mark it as efficiency

Even if we do decide to keep this as a breaking change, I'd be ok with it being one PR. The changes are so tightly related to each other and the total size of code changes is fairly small. I think it's ok to have a single one under the breaking category. Also the others are under efficiency so people will understand from the release notes that lightgbm makes fewer copies for numpy input... think that's enough.

@StrikerRUS StrikerRUS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you very much, LGTM!

I think that one breaking PR is OK. We already have some breaking changes for next release , so this tiny breakage shouldn't be the only problem for other packages maintainers. They will already have to check Breaking section of changelog carefully, and changing int to int[] shouldn't be a big headache.

Comment thread tests/python_package_test/test_basic.py Resolved
@jameslamb

Copy link
Copy Markdown
Member

They will already have to check Breaking section of changelog carefully, and changing int to int[] shouldn't be a big headache.

Ok, let's leave it as an ABI-breaking change. We are not making ABI stability guarantees here yet anyway. I do think we should consider that at some point... maybe as part of #6774, we could start using the SOVERSION property in CMake to communicate ABI changes (CMake docs).

Co-authored-by: Nikita Titov <nekit94-08@mail.ru>
@jameslamb jameslamb mentioned this pull request Jan 23, 2025
32 of 33 tasks
@StrikerRUS
StrikerRUS requested a review from jameslamb January 26, 2025 16:12
@StrikerRUS

StrikerRUS commented Jan 26, 2025 •

Copy link
Copy Markdown
Collaborator

@jameslamb Can we merge this if CI passed?

@jameslamb jameslamb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes I think it's ok

@jameslamb
jameslamb merged commit 0f0dd9d into master Jan 26, 2025
@jameslamb
jameslamb deleted the no-copy-mats branch January 26, 2025 19:40
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity since it was closed.
To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jan 29, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants