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

[python-package] Expose ObjectiveFunction class #6586

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add an error when not using CPU
  • Loading branch information
Atanas Dimitrov committed Aug 1, 2024
commit c1485d3f15e943ec05d597391019913654d07046
6 changes: 5 additions & 1 deletion src/c_api.cpp
Original file line number Diff line number Diff line change
@@ -2754,7 +2754,11 @@ LIGHTGBM_C_EXPORT int LGBM_ObjectiveFunctionCreate(const char *typ,
API_BEGIN();
auto param = Config::Str2Map(parameter);
Config config(param);
*out = ObjectiveFunction::CreateObjectiveFunction(std::string(typ), config);
if (config.device_type != std::string("cpu")) {
Log::Fatal("Currently the ObjectiveFunction class is only exposed for CPU devices.");
} else {
*out = ObjectiveFunction::CreateObjectiveFunction(std::string(typ), config);
}
API_END();
}

6 changes: 6 additions & 0 deletions tests/python_package_test/test_engine.py
Original file line number Diff line number Diff line change
@@ -4424,8 +4424,14 @@ def test_objective_function_class(use_weight, test_data, num_boost_round):

params["objective"] = builtin_loss
booster_exposed = lgb.train(params, lgb_train, num_boost_round=num_boost_round)

if getenv("TASK", "") != "cpu":
with pytest.raises(lgb.basic.LightGBMError):
builtin_loss(y, lgb_train)
return
params["objective"] = test_data["objective_name"]
booster = lgb.train(params, lgb_train, num_boost_round=num_boost_round)

params["objective"] = test_data["custom_objective"]
booster_custom = lgb.train(params, lgb_train, num_boost_round=num_boost_round)

Loading
Oops, something went wrong.