Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include parameters from reference dataset on subset (fixes #5402) #5416

Merged
merged 3 commits into from
Aug 28, 2022

Conversation

jmoralez
Copy link
Collaborator

Fixes #5402

@StrikerRUS
Copy link
Collaborator

StrikerRUS commented Aug 14, 2022

Should we also add device_type_ and gpu_device_id_?

LightGBM/src/io/dataset.cpp

Lines 789 to 790 in 5ef46f9

device_type_ = dataset->device_type_;
gpu_device_id_ = dataset->gpu_device_id_;

Also, numeric_feature_map_ and num_numeric_features_

LightGBM/src/io/dataset.cpp

Lines 757 to 758 in 5ef46f9

numeric_feature_map_ = dataset->numeric_feature_map_;
num_numeric_features_ = dataset->num_numeric_features_;

@jmoralez
Copy link
Collaborator Author

CopySubrow is called after CopyFeatureMapperFrom when creating the subset

ret->CopySubrow(full_dataset, used_row_indices, num_used_row_indices, true);

and those attributes are set there

LightGBM/src/io/dataset.cpp

Lines 845 to 846 in 5ef46f9

numeric_feature_map_ = fullset->numeric_feature_map_;
num_numeric_features_ = fullset->num_numeric_features_;

LightGBM/src/io/dataset.cpp

Lines 857 to 858 in 5ef46f9

device_type_ = fullset->device_type_;
gpu_device_id_ = fullset->gpu_device_id_;

Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

Thanks for this! Please see my suggestion about making the test stricter.

@@ -243,6 +243,13 @@ def test_chunked_dataset_linear():
valid_data.construct()


def test_save_dataset_subset_and_load_from_file(tmp_path):
data = np.random.rand(100, 2)
ds = lgb.Dataset(data)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please try overriding the defaults (e.g. setting max_bin: 7 or something) and check that those non-default values survive the round trip to disk?

I think that would increase our confidence that this is working as expected. Otherwise, I think a bug of the form "all parameter values are lost when writing to binary Dataset file" could make it through this test.

Copy link
Collaborator Author

@jmoralez jmoralez Aug 16, 2022

Choose a reason for hiding this comment

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

I think that will raise an error like the one in #4904, but I'll try it and confirm here.

Copy link
Collaborator

@jameslamb jameslamb Aug 16, 2022

Choose a reason for hiding this comment

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

Ok I see. I expected a PR called "include parameters" to test that parameters on either side of an operation had the same values.

I think maybe I got confused by the presence of writing and reading a binary file, and thought the issue was specific to storing a Dataset to disk. Is it like "when taking a subset, not all parameters are copied from the reference Dataset to the subset...and this can show up as an error loading the Dataset from file"?

If it is, then it would be great to be able to reach into the Dataset (on the C++ side, not the Python object) and check that attributes like max_bin_ are the same and a non-default value before writing to a file. But I'm not sure how to do that without introducing a new c_api entrypoint.

So if adding the test I suggested does hit the error from #4904, then I think this test in its current state is ok. It is still an improvement that fixes a bug.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I built the dataset with non default parameters and check that loading it with the same ones succeeds in 6a2fd1f.

Copy link
Collaborator

@jameslamb jameslamb Aug 20, 2022

Choose a reason for hiding this comment

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

Ok nice! Thank you for that and for the explanation.

I tried running this PR's test code on latest master, and can see based on the error message that those non-default parameter values are being respected.

import lightgbm as lgb
import numpy as np

data = np.random.rand(100, 2)
params = {'max_bin': 50, 'min_data_in_bin': 10}
ds = lgb.Dataset(data, params=params).construct()
ds.subset([1, 2, 3, 5, 8]).save_binary('subset.bin')
lgb.Dataset('subset.bin', params=params).construct()

[LightGBM] [Fatal] Dataset was constructed with parameter max_bin=32649. It cannot be changed to 50 when loading from binary file.

I'm not sure where that 32649 is coming from, but I think that's not an issue caused by this PR.

@jmoralez jmoralez changed the title include parameters from reference dataset on copy (fixes #5402) include parameters from reference dataset on subset (fixes #5402) Aug 16, 2022
Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

Thanks for this!

I ran the test code on latest master (865c126) and confirmed that this test fails with the error

[LightGBM] [Fatal] Dataset was constructed with parameter max_bin=32649. It cannot be changed to 50 when loading from binary file.

The fix here looks correct to me.

Copy link
Collaborator

@StrikerRUS StrikerRUS left a comment

Choose a reason for hiding this comment

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

Thank you!

@StrikerRUS StrikerRUS merged commit 5079de4 into master Aug 28, 2022
@StrikerRUS StrikerRUS deleted the set-params-on-copy branch August 28, 2022 15:22
@jameslamb jameslamb mentioned this pull request Oct 7, 2022
40 tasks
@github-actions
Copy link

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 Aug 19, 2023
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.

Failed to load Dataset.subset() back after Dataset.save_binary()
3 participants