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

8
#if defined(USE_CUDA) || defined(USE_ROCM)
9
10
11

#include <LightGBM/cuda/cuda_objective_function.hpp>

12
#include <memory>
13
14
15
#include <string>
#include <vector>

16
17
#include "cuda_binary_objective.hpp"

18
19
20
21
22
23
#include "../multiclass_objective.hpp"

#define GET_GRADIENTS_BLOCK_SIZE_MULTICLASS (1024)

namespace LightGBM {

24
class CUDAMulticlassSoftmax: public CUDAObjectiveInterface<MulticlassSoftmax> {
25
26
27
28
29
30
31
32
33
34
35
36
 public:
  explicit CUDAMulticlassSoftmax(const Config& config);

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

  ~CUDAMulticlassSoftmax();

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

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

37
  const double* LaunchConvertOutputCUDAKernel(const data_size_t num_data, const double* input, double* output) const;
38
39
40
41
42
43

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


44
class CUDAMulticlassOVA: public CUDAObjectiveInterface<MulticlassOVA> {
45
46
47
48
49
50
51
52
53
 public:
  explicit CUDAMulticlassOVA(const Config& config);

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

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

  void GetGradients(const double* score, score_t* gradients, score_t* hessians) const override;

54
  const double* ConvertOutputCUDA(const data_size_t num_data, const double* input, double* output) const override;
55
56
57
58
59
60
61
62
63
64
65
66
67
68

  double BoostFromScore(int class_id) const override {
    return cuda_binary_loss_[class_id]->BoostFromScore(0);
  }

  bool ClassNeedTrain(int class_id) const override {
    return cuda_binary_loss_[class_id]->ClassNeedTrain(0);
  }

  ~CUDAMulticlassOVA();

  bool IsCUDAObjective() const override { return true; }

 private:
69
70
  void LaunchGetGradientsKernel(const double* /*scores*/, score_t* /*gradients*/, score_t* /*hessians*/) const {}

71
72
73
74
  std::vector<std::unique_ptr<CUDABinaryLogloss>> cuda_binary_loss_;
};


75
76
}  // namespace LightGBM

77
#endif  // USE_CUDA || USE_ROCM
78
#endif  // LIGHTGBM_OBJECTIVE_CUDA_CUDA_MULTICLASS_OBJECTIVE_HPP_