cuda_rank_objective.hpp 3.96 KB
Newer Older
1
2
3
4
5
6
/*!
 * Copyright (c) 2021 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for
 * license information.
 */

7
8
#ifndef LIGHTGBM_SRC_OBJECTIVE_CUDA_CUDA_RANK_OBJECTIVE_HPP_
#define LIGHTGBM_SRC_OBJECTIVE_CUDA_CUDA_RANK_OBJECTIVE_HPP_
9

10
#ifdef USE_CUDA
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#define NUM_QUERY_PER_BLOCK (10)

#include <LightGBM/cuda/cuda_objective_function.hpp>
#include <LightGBM/utils/threading.h>

#include <fstream>
#include <string>
#include <vector>

#include "../rank_objective.hpp"

namespace LightGBM {

25
26
template <typename HOST_OBJECTIVE>
class CUDALambdaRankObjectiveInterface : public CUDAObjectiveInterface<HOST_OBJECTIVE> {
27
 public:
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  explicit CUDALambdaRankObjectiveInterface(const Config& config): CUDAObjectiveInterface<HOST_OBJECTIVE>(config) {}

  explicit CUDALambdaRankObjectiveInterface(const std::vector<std::string>& strs): CUDAObjectiveInterface<HOST_OBJECTIVE>(strs) {}

  ~CUDALambdaRankObjectiveInterface() {}

  void Init(const Metadata& metadata, data_size_t num_data) override {
    CUDAObjectiveInterface<HOST_OBJECTIVE>::Init(metadata, num_data);

    const int num_threads = OMP_NUM_THREADS();
    std::vector<uint16_t> thread_max_num_items_in_query(num_threads);
    Threading::For<data_size_t>(0, this->num_queries_, 1,
      [this, &thread_max_num_items_in_query] (int thread_index, data_size_t start, data_size_t end) {
        for (data_size_t query_index = start; query_index < end; ++query_index) {
          const data_size_t query_item_count = this->query_boundaries_[query_index + 1] - this->query_boundaries_[query_index];
          if (query_item_count > thread_max_num_items_in_query[thread_index]) {
            thread_max_num_items_in_query[thread_index] = query_item_count;
          }
        }
      });
    data_size_t max_items_in_query = 0;
    for (int thread_index = 0; thread_index < num_threads; ++thread_index) {
      if (thread_max_num_items_in_query[thread_index] > max_items_in_query) {
        max_items_in_query = thread_max_num_items_in_query[thread_index];
      }
    }
    max_items_in_query_aligned_ = 1;
    --max_items_in_query;
    while (max_items_in_query > 0) {
      max_items_in_query >>= 1;
      max_items_in_query_aligned_ <<= 1;
    }
    if (max_items_in_query_aligned_ > 2048) {
      cuda_item_indices_buffer_.Resize(static_cast<size_t>(metadata.query_boundaries()[metadata.num_queries()]));
    }
    this->cuda_labels_ = metadata.cuda_metadata()->cuda_label();
    cuda_query_boundaries_ = metadata.cuda_metadata()->cuda_query_boundaries();
  }
66
67
68
69
70
71
72
73
74
75
76
77

 protected:
  // CUDA memory, held by this object
  CUDAVector<int> cuda_item_indices_buffer_;

  // CUDA memory, held by other objects
  const data_size_t* cuda_query_boundaries_;

  // Host memory
  int max_items_in_query_aligned_;
};

78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class CUDALambdarankNDCG: public CUDALambdaRankObjectiveInterface<LambdarankNDCG> {
 public:
  explicit CUDALambdarankNDCG(const Config& config);

  explicit CUDALambdarankNDCG(const std::vector<std::string>& strs);

  void Init(const Metadata& mdtadata, data_size_t num_data) override;

  ~CUDALambdarankNDCG();

 private:
  void LaunchGetGradientsKernel(const double* score, score_t* gradients, score_t* hessians) const override;

  // CUDA memory, held by this object
  CUDAVector<double> cuda_inverse_max_dcgs_;
  CUDAVector<double> cuda_label_gain_;
};


class CUDARankXENDCG : public CUDALambdaRankObjectiveInterface<RankXENDCG> {
99
100
101
102
103
104
105
106
107
 public:
  explicit CUDARankXENDCG(const Config& config);

  explicit CUDARankXENDCG(const std::vector<std::string>& strs);

  ~CUDARankXENDCG();

  void Init(const Metadata& metadata, data_size_t num_data) override;

108
 protected:
109
110
111
112
113
  void LaunchGetGradientsKernel(const double* score, score_t* gradients, score_t* hessians) const;

  void GenerateItemRands() const;

  mutable std::vector<double> item_rands_;
114
115
  CUDAVector<double> cuda_item_rands_;
  CUDAVector<double> cuda_params_buffer_;
116
117
118
};


119
120
}  // namespace LightGBM

121
#endif  // USE_CUDA
122
#endif  // LIGHTGBM_SRC_OBJECTIVE_CUDA_CUDA_RANK_OBJECTIVE_HPP_