Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
#include <memory>
#include <string>
#include "core/common/common.h"
#include "core/framework/op_kernel.h"
#include "core/providers/cpu/controlflow/utils.h"
#include "contrib_ops/cpu/transformers/subgraph_gpt.h"
#include "contrib_ops/cpu/transformers/generation_device_helper.h"
#include "contrib_ops/cpu/transformers/sampling_parameters.h"
 
namespace onnxruntime {
class FeedsFetchesManager;
 
namespace contrib {
namespace transformers {
 
using namespace onnxruntime::controlflow;  // namespace of IControlFlowKernel
 
class Sampling : public IControlFlowKernel {
 public:
  explicit Sampling(const OpKernelInfo& info)
      : IControlFlowKernel(info),
        decoder_feeds_fetches_manager_(nullptr),
        dumper_(nullptr) {
    Init(info);
  }
 
  void Init(const OpKernelInfo& info);
 
  Status Compute(OpKernelContext* ctx) const override;
 
  Status SetupSubgraphExecutionInfo(const SessionState& session_state,
                                    const std::string& attribute_name,
                                    const SessionState& subgraph_session_state) override;
 
 protected:
  void SetConsoleDumper(IConsoleDumper* dumper) { dumper_ = dumper; }
 
  // device helpers that is same for both GPT and encoder-decoder models.
  void SetDeviceHelpers(
      const GenerationDeviceHelper::AddToFeedsFunc& add_to_feeds_func,
      const GenerationDeviceHelper::TopkFunc& topk_func,
      const GenerationDeviceHelper::DeviceCopyFunc<float>& device_copy_func,
      const GenerationDeviceHelper::GreedySearchProcessLogitsFunc<float>& process_logits_func,
      const GenerationDeviceHelper::GreedySearchProcessLogitsFunc<MLFloat16>& process_logits_fp16_func,
      const GenerationDeviceHelper::InitGreedyStateFunc<float>& init_greedy_state_func,
      const GenerationDeviceHelper::InitGreedyStateFunc<MLFloat16>& init_greedy_state_fp16_func) {
    add_to_feeds_func_ = add_to_feeds_func;
    topk_func_ = topk_func;
    device_copy_func_ = device_copy_func;
    process_logits_func_ = process_logits_func;
    process_logits_fp16_func_ = process_logits_fp16_func;
    init_greedy_state_func_ = init_greedy_state_func;
    init_greedy_state_fp16_func_ = init_greedy_state_fp16_func;
  }
 
#ifdef USE_CUDA
  void SetDeviceHelpers_Cuda(const GenerationDeviceHelper::ReorderPastStateFunc& reorder_past_state_func) {
    reorder_past_state_func_ = reorder_past_state_func;
  }
#endif
 
  void SetDeviceHelpers_Gpt(
      const GenerationDeviceHelper::UpdateGptFeedsFunc<float>& update_gpt_feeds_func,
      const GenerationDeviceHelper::UpdateGptFeedsFunc<MLFloat16>& update_gpt_feeds_fp16_func) {
    update_gpt_feeds_func_ = update_gpt_feeds_func;
    update_gpt_feeds_fp16_func_ = update_gpt_feeds_fp16_func;
  }
 
#ifdef USE_CUDA
  const void* gpu_device_prop_ = nullptr;
  int gpu_device_arch_ = 0;
#endif
 
 private:
  // Device specific functions
  GenerationDeviceHelper::AddToFeedsFunc add_to_feeds_func_;
  GenerationDeviceHelper::TopkFunc topk_func_;
  GenerationDeviceHelper::DeviceCopyFunc<float> device_copy_func_;
 
  GenerationDeviceHelper::GreedySearchProcessLogitsFunc<float> process_logits_func_;
  GenerationDeviceHelper::GreedySearchProcessLogitsFunc<MLFloat16> process_logits_fp16_func_;
 
  GenerationDeviceHelper::InitGreedyStateFunc<float> init_greedy_state_func_;
  GenerationDeviceHelper::InitGreedyStateFunc<MLFloat16> init_greedy_state_fp16_func_;
 
#ifdef USE_CUDA
  GenerationDeviceHelper::ReorderPastStateFunc reorder_past_state_func_;
#endif
 
  //------------------------------------------------------------
  // Device specific functions for GPT
  //------------------------------------------------------------
  GenerationDeviceHelper::UpdateGptFeedsFunc<float> update_gpt_feeds_func_;
  GenerationDeviceHelper::UpdateGptFeedsFunc<MLFloat16> update_gpt_feeds_fp16_func_;
 
  //------------------------------------------------------------
  // Subgraph and FeedsFetchesManager re-used for each subgraph execution.
  //------------------------------------------------------------
  std::unique_ptr<GptSubgraph> init_run_gpt_subgraph_;
  std::unique_ptr<GptSubgraph> gpt_subgraph_;
 
  FeedsFetchesManager* decoder_feeds_fetches_manager_;
  FeedsFetchesManager* init_run_decoder_feeds_fetches_manager_;
 
  IConsoleDumper* dumper_;
 
  SamplingParameters parameters_;
 
  bool has_init_decoder_ = false;
};
 
}  // namespace transformers
}  // namespace contrib
}  // namespace onnxruntime