Sycl implementation for objective functions (#9846)
--------- Co-authored-by: Dmitry Razdoburdin <>
This commit is contained in:
committed by
GitHub
parent
ddab49a8be
commit
43897b8296
28
tests/cpp/plugin/test_sycl_multiclass_obj.cc
Normal file
28
tests/cpp/plugin/test_sycl_multiclass_obj.cc
Normal file
@@ -0,0 +1,28 @@
|
||||
/*!
|
||||
* Copyright 2018-2023 XGBoost contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/context.h>
|
||||
|
||||
#include "../objective/test_multiclass_obj.h"
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
TEST(SyclObjective, SoftmaxMultiClassObjGPair) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestSoftmaxMultiClassObjGPair(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, SoftmaxMultiClassBasic) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestSoftmaxMultiClassObjGPair(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, SoftprobMultiClassBasic) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestSoftprobMultiClassBasic(&ctx);
|
||||
}
|
||||
} // namespace xgboost
|
||||
99
tests/cpp/plugin/test_sycl_regression_obj.cc
Normal file
99
tests/cpp/plugin/test_sycl_regression_obj.cc
Normal file
@@ -0,0 +1,99 @@
|
||||
/*!
|
||||
* Copyright 2017-2019 XGBoost contributors
|
||||
*/
|
||||
#include <gtest/gtest.h>
|
||||
#include <xgboost/objective.h>
|
||||
#include <xgboost/context.h>
|
||||
|
||||
#include "../helpers.h"
|
||||
#include "../objective/test_regression_obj.h"
|
||||
|
||||
namespace xgboost {
|
||||
|
||||
TEST(SyclObjective, LinearRegressionGPair) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestLinearRegressionGPair(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, SquaredLog) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestSquaredLog(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, LogisticRegressionGPair) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestLogisticRegressionGPair(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, LogisticRegressionBasic) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
|
||||
TestLogisticRegressionBasic(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, LogisticRawGPair) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
TestsLogisticRawGPair(&ctx);
|
||||
}
|
||||
|
||||
TEST(SyclObjective, CPUvsSycl) {
|
||||
Context ctx;
|
||||
ctx.UpdateAllowUnknown(Args{{"device", "sycl"}});
|
||||
ObjFunction * obj_sycl =
|
||||
ObjFunction::Create("reg:squarederror_sycl", &ctx);
|
||||
|
||||
ctx = ctx.MakeCPU();
|
||||
ObjFunction * obj_cpu =
|
||||
ObjFunction::Create("reg:squarederror", &ctx);
|
||||
|
||||
linalg::Matrix<GradientPair> cpu_out_preds;
|
||||
linalg::Matrix<GradientPair> sycl_out_preds;
|
||||
|
||||
constexpr size_t kRows = 400;
|
||||
constexpr size_t kCols = 100;
|
||||
auto pdmat = RandomDataGenerator(kRows, kCols, 0).Seed(0).GenerateDMatrix();
|
||||
HostDeviceVector<float> preds;
|
||||
preds.Resize(kRows);
|
||||
auto& h_preds = preds.HostVector();
|
||||
for (size_t i = 0; i < h_preds.size(); ++i) {
|
||||
h_preds[i] = static_cast<float>(i);
|
||||
}
|
||||
auto& info = pdmat->Info();
|
||||
|
||||
info.labels.Reshape(kRows, 1);
|
||||
auto& h_labels = info.labels.Data()->HostVector();
|
||||
for (size_t i = 0; i < h_labels.size(); ++i) {
|
||||
h_labels[i] = 1 / static_cast<float>(i+1);
|
||||
}
|
||||
|
||||
{
|
||||
// CPU
|
||||
obj_cpu->GetGradient(preds, info, 0, &cpu_out_preds);
|
||||
}
|
||||
{
|
||||
// sycl
|
||||
obj_sycl->GetGradient(preds, info, 0, &sycl_out_preds);
|
||||
}
|
||||
|
||||
auto h_cpu_out = cpu_out_preds.HostView();
|
||||
auto h_sycl_out = sycl_out_preds.HostView();
|
||||
|
||||
float sgrad = 0;
|
||||
float shess = 0;
|
||||
for (size_t i = 0; i < kRows; ++i) {
|
||||
sgrad += std::pow(h_cpu_out(i).GetGrad() - h_sycl_out(i).GetGrad(), 2);
|
||||
shess += std::pow(h_cpu_out(i).GetHess() - h_sycl_out(i).GetHess(), 2);
|
||||
}
|
||||
ASSERT_NEAR(sgrad, 0.0f, kRtEps);
|
||||
ASSERT_NEAR(shess, 0.0f, kRtEps);
|
||||
|
||||
delete obj_cpu;
|
||||
delete obj_sycl;
|
||||
}
|
||||
|
||||
} // namespace xgboost
|
||||
Reference in New Issue
Block a user