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

Add deterministic option for LightGBM #7415

Merged
merged 3 commits into from
Mar 14, 2025
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
26 changes: 25 additions & 1 deletion src/Microsoft.ML.LightGbm/LightGbmTrainerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public class OptionsBase : TrainerInputBaseWithGroupId
{nameof(CategoricalSmoothing), "cat_smooth" },
{nameof(L2CategoricalRegularization), "cat_l2" },
{nameof(HandleMissingValue), "use_missing" },
{nameof(UseZeroAsMissingValue), "zero_as_missing" }
{nameof(UseZeroAsMissingValue), "zero_as_missing" },
{nameof(Deterministic), "deterministic"},
{nameof(ForceRowWise), "force_row_wise"},
{nameof(ForceColumnWise), "force_col_wise"},
};

internal string GetOptionName(string name)
Expand Down Expand Up @@ -236,6 +239,24 @@ private protected OptionsBase() { }
[Argument(ArgumentType.AtMostOnce, HelpText = "Sets the random seed for LightGBM to use.")]
public int? Seed;

/// <summary>
/// Setting this to true should ensure the stable results when using the same data and the same parameters and different num_threads.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Whether to use deterministic algorithm.")]
public bool Deterministic = false;
Copy link
Member

Choose a reason for hiding this comment

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

public bool Deterministic = false;

I know this class is exposing fields directly, but I am wondering can the new added stuff be properties?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe we use reflection to instantiate these (like when you run this from ML.NET command line), and if its expecting fields it wouldn't work. I will double check on that before I merge this in.

If it is doing that, its possible that we could change things to either do both or only do properites and update all the options as well.


/// <summary>
/// Whether to force column-wise histogram building.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Whether to force column-wise histogram building.")]
public bool ForceColumnWise = false;

/// <summary>
/// Whether to force row-wise histogram building.
/// </summary>
[Argument(ArgumentType.AtMostOnce, HelpText = "Whether to force row-wise histogram building.")]
public bool ForceRowWise = false;

[Argument(ArgumentType.Multiple, HelpText = "Parallel LightGBM Learning Algorithm", ShortName = "parag")]
internal ISupportParallel ParallelTrainer = new SingleTrainerFactory();

Expand Down Expand Up @@ -279,6 +300,9 @@ internal virtual Dictionary<string, object> ToDictionary(IHost host)
res[GetOptionName(nameof(MaximumCategoricalSplitPointCount))] = MaximumCategoricalSplitPointCount;
res[GetOptionName(nameof(CategoricalSmoothing))] = CategoricalSmoothing;
res[GetOptionName(nameof(L2CategoricalRegularization))] = L2CategoricalRegularization;
res[GetOptionName(nameof(Deterministic))] = Deterministic;
res[GetOptionName(nameof(ForceColumnWise))] = ForceColumnWise;
res[GetOptionName(nameof(ForceRowWise))] = ForceRowWise;

return res;
}
Expand Down
108 changes: 108 additions & 0 deletions test/BaselineOutput/Common/EntryPoints/core_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11891,6 +11891,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down Expand Up @@ -12410,6 +12437,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down Expand Up @@ -12929,6 +12983,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down Expand Up @@ -13409,6 +13490,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down
108 changes: 108 additions & 0 deletions test/BaselineOutput/Common/EntryPoints/netcoreapp/core_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11891,6 +11891,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down Expand Up @@ -12410,6 +12437,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down Expand Up @@ -12929,6 +12983,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down Expand Up @@ -13409,6 +13490,33 @@
"IsNullable": true,
"Default": null
},
{
"Name": "Deterministic",
"Type": "Bool",
"Desc": "Whether to use deterministic algorithm.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceColumnWise",
"Type": "Bool",
"Desc": "Whether to force column-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ForceRowWise",
"Type": "Bool",
"Desc": "Whether to force row-wise histogram building.",
"Required": false,
"SortOrder": 150.0,
"IsNullable": false,
"Default": false
},
{
"Name": "ParallelTrainer",
"Type": {
Expand Down
2 changes: 2 additions & 0 deletions test/Microsoft.ML.Tests/TrainerEstimators/TreeEstimators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public void LightGBMBinaryEstimator()
NumberOfLeaves = 10,
MinimumExampleCountPerLeaf = 2,
UnbalancedSets = false, // default value
Deterministic = true,
ForceRowWise = true
});

var pipeWithTrainer = pipe.Append(trainer);
Expand Down
Loading