Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <functional>
#include "core/framework/op_kernel.h"
#include "core/framework/session_state.h"
#include "core/session/onnxruntime_c_api.h"
 
// onnxruntime internal OpKernelContext derived class to provide additional
// APIs that aren't desirable to add to the public OpKernelContext API
 
namespace onnxruntime {
class SessionState;
class ExecutionFrame;
 
class OpKernelContextInternal : public OpKernelContext {
 public:
  explicit OpKernelContextInternal(const SessionState& session_state,
                                   IExecutionFrame& frame,
                                   const OpKernel& kernel,
                                   const logging::Logger& logger,
                                   const bool& terminate_flag,
                                   Stream* stream,
                                   profiling::Profiler* run_profiler = nullptr)
      : OpKernelContext(&frame, &kernel, stream, session_state.GetThreadPool(), logger),
        session_state_(session_state),
        terminate_flag_(terminate_flag),
        run_profiler_(run_profiler) {
    const auto& implicit_inputs = kernel.Node().ImplicitInputDefs();
    int num_implicit_inputs = static_cast<int>(implicit_inputs.size());
    implicit_input_values_.reserve(num_implicit_inputs);
 
    for (int i = 0; i < num_implicit_inputs; ++i) {
      const auto* entry = GetImplicitInputMLValue(i);
      ORT_ENFORCE(entry != nullptr, "All implicit inputs should have OrtValue instances by now. ",
                  implicit_inputs[i]->Name(), " does not.");
      implicit_input_values_.push_back(entry);
    }
 
#if !defined(ORT_MINIMAL_BUILD)
    if (session_state_.GetNodeStatsRecorder() != nullptr) {
      auto alloc = OpKernelContext::GetAllocator(kernel.GetDevice(OrtMemTypeDefault));
      if (alloc != nullptr) {
        accounting_allocator_ = std::make_shared<AccountingAllocator>(std::move(alloc));
      }
    }
#endif
  }
 
  bool GetUseDeterministicCompute() const override {
    return session_state_.GetUseDeterministicCompute();
  }
 
#if !defined(ORT_MINIMAL_BUILD)
  KernelPilot* GetKernelPilot() const override {
    kernel_pilot_ = session_state_.GetKernelPilot(GetKernel());
    return kernel_pilot_;
  }
 
  // Called by the executor only after successful Compute(). To commit usage, Compute() must obtain
  // the pilot via GetKernelPilot(), then BeginInvocation() and collect its selection before returning.
  // Without a lookup this is a no-op; KernelPilot::RecordUsage() also checks for a pending invocation.
  Status RecordKernelUsage() const {
    if (kernel_pilot_ == nullptr) {
      return Status::OK();
    }
    return kernel_pilot_->RecordUsage();
  }
#endif
 
  const SessionState* SubgraphSessionState(const std::string& attribute_name) {
    return session_state_.GetSubgraphSessionState(GetNodeIndex(), attribute_name);
  }
 
  const OrtValue* GetInputMLValue(int index) const override {
    return OpKernelContext::GetInputMLValue(index);
  }
 
  OrtValue* GetOutputMLValue(int index) {
    return OpKernelContext::GetOutputMLValue(index);
  }
 
  OrtValue* GetPreallocatedOutputMLValue(int index) const {
    return OpKernelContext::GetPreallocatedOutputMLValue(index);
  }
 
#ifdef ENABLE_ATEN
  Status SetOutputMLValue(int index, const OrtValue& ort_value) {
    return OpKernelContext::SetOutputMLValue(index, ort_value);
  }
#endif
 
  OrtValue* OutputMLValue(int index, const TensorShape& shape) override {
    return OpKernelContext::OutputMLValue(index, shape);
  }
 
  // Get the OrtValue's for all implicit inputs. Order is same as Node::ImplicitInputDefs(). No nullptr entries.
  const std::vector<const OrtValue*>& GetImplicitInputs() const {
    return implicit_input_values_;
  }
 
  int GetOrtValueIndexForOutput(int output_index) const override {
    return OpKernelContext::GetOrtValueIndexForOutput(output_index);
  }
 
#if !defined(ORT_MINIMAL_BUILD)
  Status GetTempSpaceAllocator(AllocatorPtr* output) const override {
    if (accounting_allocator_) {
      *output = accounting_allocator_;
      return Status::OK();
    }
    return OpKernelContext::GetTempSpaceAllocator(output);
  }
#endif
 
#if !defined(ORT_MINIMAL_BUILD)
  bool GetAllocatorStats(AllocatorStats& stats) {
    if (accounting_allocator_ == nullptr) {
      return false;
    }
    accounting_allocator_->GetStats(&stats);
    return true;
  }
#endif
 
  const bool& GetTerminateFlag() const noexcept { return terminate_flag_; }
 
  profiling::Profiler* GetRunProfiler() const noexcept { return run_profiler_; }
 
 private:
#if !defined(ORT_MINIMAL_BUILD)
  mutable KernelPilot* kernel_pilot_{nullptr};
 
  class AccountingAllocator : public IAllocator {
   public:
    AccountingAllocator(AllocatorPtr alloc) : IAllocator(alloc->Info()), allocator_(std::move(alloc)) {
    }
 
    void* Alloc(size_t size) override {
      void* p = allocator_->Alloc(size);
      if (p != nullptr) {
        stats_.total_allocated_bytes += size;
      }
      return p;
    }
 
    void Free(void* p) override {
      allocator_->Free(p);
    }
 
    void GetStats(AllocatorStats* stats) override {
      *stats = stats_;
    }
 
   private:
    AllocatorPtr allocator_;
    AllocatorStats stats_;
  };
 
  AllocatorPtr accounting_allocator_;
#endif
 
  const SessionState& session_state_;
  const bool& terminate_flag_;
  profiling::Profiler* run_profiler_;
  std::vector<const OrtValue*> implicit_input_values_;
};
 
}  // namespace onnxruntime