Исходный код СУБД 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <atomic>
#include <istream>
#include <mutex>
#include <string>
#include <string_view>
#include <utility>
 
#include <gsl/gsl>
#include "core/common/common.h"
#include "core/common/inlined_containers.h"
#include "core/framework/kernel_pilot.h"
 
namespace onnxruntime {
 
class OpKernel;
namespace logging {
class Logger;
}
 
// Host-side state only. CUDA allocations and placement policy do not belong here.
class KernelPilotMoeExpertState {
 public:
  // Counter-update logging
  // ----------------------
 
  KernelPilotMoeExpertState() = default;
  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(KernelPilotMoeExpertState);
 
  static constexpr size_t kMaxCounterLogRecordsPerRun = 1024;
 
  // Starts one Run and optionally enables counter-update logging for it.
  // A second Run is rejected because selections and counters are session-owned state.
  Status BeginRun(std::string request_id, const logging::Logger* logger);
 
  // Releases the active Run and its optional logging state.
  Status EndRun();
 
  // Expert counters
  // ---------------
 
  // Configures the exponential counter update:
  //   counter = alpha * counter + beta * selected
  // Must be called before registering any MoE kernels.
  Status SetCounterParameters(double alpha, double beta);
 
  // Registers one resolved MoE kernel and allocates its contiguous counter range and
  // provider-independent KernelPilot. Registration closes after initialization.
  Status RegisterNode(const OpKernel* kernel, std::string_view graph_scope, size_t node_index,
                      std::string_view node_type, size_t expert_count);
 
  // Loads initial counters after node registration and before FinalizeInitialization().
  // UTF-8 text format: the first line must be exactly "moe_expert_state 1", followed by
  // one whitespace-separated record per line:
  //   "graph_scope" node_index node_type expert_id counter_value
  // Example:
  //   moe_expert_state 1
  //   "main" 0 MoE 2 12
  //   "main/4/11:then_branch" 0 QMoE 1 3.5
  //
  // graph_scope uses std::quoted escaping for quotes/backslashes; unquoted scopes without
  // whitespace are also accepted. The root scope is "main"; subgraphs append
  // /<parent-node-index>/<attribute-name-length>:<attribute-name> (length in bytes).
  // node_index identifies the registered node in the resolved, optimized graph.
  // node_type must match its registered type ("MoE" or "QMoE"). expert_id is a zero-based
  // index local to that node, not a global expert index or kernel pointer.
  // counter_value is parsed in the classic locale and must be finite and non-negative.
  //
  // Omitted experts retain their current values (zero after registration). Blank lines,
  // comments, extra fields, duplicate expert records, unknown nodes, and invalid IDs or
  // values are rejected. A malformed record or stream read error leaves all counters unchanged.
  Status Load(std::istream& input);
 
  // Closes registration and loading. Run-time counter updates require finalized state.
  Status FinalizeInitialization();
 
  // Returns the pilot owned by a registered kernel, or nullptr for any other kernel.
  KernelPilot* GetKernelPilot(const OpKernel* kernel);
 
  // During a Run, only this kernel may read its counters.
  Status GetCounters(const OpKernel* kernel, InlinedVector<double>& counters) const;
 
  // Maps a kernel-local expert ID to its index in the session-wide counter array.
  Status GetExpertId(const OpKernel* kernel, int expert_id, size_t& global_expert_id) const;
 
  // One entry per registered (kernel, local expert index). expert_id is local to that kernel,
  // matching RegisterNode/GetExpertId, not a global counter index. Order is unspecified.
  // Call only while no Run is active.
  struct ExpertStat {
    const OpKernel* kernel;
    size_t expert_id;
    double popularity;
  };
  InlinedVector<ExpertStat> GetExpertStats() const;
 
  // Returns the total number of experts registered across all MoE kernels.
  size_t TotalExpertCount() const noexcept { return counters_.size(); }
 
 private:
  friend class KernelPilot;
  Status RecordUsage(const OpKernel* kernel);
 
  using Key = std::pair<std::string, size_t>;
  struct ExpertRange {
    size_t begin;
    size_t count;
  };
  struct KernelState {
    KernelState(Key key, ExpertRange range, KernelPilotMoeExpertState& owner, const OpKernel* kernel)
        : key(std::move(key)), experts(range), pilot(owner, kernel) {}
    Key key;
    ExpertRange experts;
    KernelPilot pilot;
  };
  NodeHashMap<const OpKernel*, KernelState> kernels_;
  InlinedHashMap<std::pair<const OpKernel*, int>, size_t> expert_ids_;
  InlinedVector<double> counters_;
  mutable std::mutex run_mutex_;
  bool run_active_{false};
  std::string logging_request_id_;
  const logging::Logger* logging_logger_{nullptr};
  std::atomic<size_t> logging_record_count_{0};
  double alpha_{0.9};
  double beta_{0.1};
  bool initialized_{false};
};
 
}  // namespace onnxruntime