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

Initial implementation of the ordinal recoder. #11098

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/common/device_helpers.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,15 @@ xgboost::common::Span<T> ToSpan(VectorT &vec, IndexT offset = 0,
}

template <typename T>
xgboost::common::Span<T> ToSpan(thrust::device_vector<T> &vec, size_t offset, size_t size) {
xgboost::common::Span<T> ToSpan(device_vector<T> &vec, size_t offset, size_t size) {
return ToSpan(vec, offset, size);
}

template <typename T>
xgboost::common::Span<std::add_const_t<T>> ToSpan(device_vector<T> const &vec) {
return {thrust::raw_pointer_cast(vec.data()), vec.size()};
}

template <typename T>
xgboost::common::Span<T> ToSpan(DeviceUVector<T> &vec) {
return {vec.data(), vec.size()};
Expand Down
51 changes: 51 additions & 0 deletions src/data/cat_container.cuh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2024, XGBoost Contributors
*/
#pragma once
#include "../common/device_helpers.cuh" // for ToSpan
#include "../common/device_vector.cuh" // for device_vector, XGBDeviceAllocator
#include "../encoder/ordinal.h" // for CatCharT

namespace xgboost::cuda_impl {
struct CatStrArray {
dh::device_vector<std::int32_t> offsets;
dh::device_vector<enc::CatCharT> values;

CatStrArray() = default;
CatStrArray(CatStrArray const& that) = delete;
CatStrArray& operator=(CatStrArray const& that) = delete;

CatStrArray(CatStrArray&& that) = default;
CatStrArray& operator=(CatStrArray&& that) = default;

[[nodiscard]] explicit operator enc::CatStrArrayView() const {
return {dh::ToSpan(offsets), dh::ToSpan(values)};
}
[[nodiscard]] std::size_t size() const { // NOLINT
return enc::CatStrArrayView(*this).size();
}
};

template <typename T>
struct ViewToStorageImpl;

template <>
struct ViewToStorageImpl<enc::CatStrArrayView> {
using Type = CatStrArray;
};

template <typename T>
struct ViewToStorageImpl<common::Span<T const>> {
using Type = dh::device_vector<T>;
};

template <typename... Ts>
struct ViewToStorage;

template <typename... Ts>
struct ViewToStorage<std::tuple<Ts...>> {
using Type = std::tuple<typename ViewToStorageImpl<Ts>::Type...>;
};

using CatIndexTypes = ViewToStorage<enc::CatIndexViewTypes>::Type;
} // namespace xgboost::cuda_impl
48 changes: 48 additions & 0 deletions src/data/cat_container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2025, XGBoost Contributors
*/
#pragma once

#include <cstdint> // for int32_t, int8_t
#include <tuple> // for tuple
#include <vector> // for vector

#include "../encoder/ordinal.h" // for DictionaryView

namespace xgboost {
namespace cpu_impl {
struct CatStrArray {
std::vector<std::int32_t> offsets;
std::vector<enc::CatCharT> values;

[[nodiscard]] explicit operator enc::CatStrArrayView() const { return {offsets, values}; }
[[nodiscard]] std::size_t size() const { // NOLINT
return enc::CatStrArrayView(*this).size();
}
};

template <typename T>
struct ViewToStorageImpl;

template <>
struct ViewToStorageImpl<enc::CatStrArrayView> {
using Type = CatStrArray;
};

template <typename T>
struct ViewToStorageImpl<common::Span<T const>> {
using Type = std::vector<T>;
};

template <typename... Ts>
struct ViewToStorage;

template <typename... Ts>
struct ViewToStorage<std::tuple<Ts...>> {
using Type = std::tuple<typename ViewToStorageImpl<Ts>::Type...>;
};

using CatIndexTypes = ViewToStorage<enc::CatIndexViewTypes>::Type;
using ColumnType = enc::cpu_impl::TupToVarT<CatIndexTypes>;
} // namespace cpu_impl
} // namespace xgboost
Loading
Loading