Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "orttraining/core/optimizer/gru_replacement.h"
 
#include "core/graph/graph.h"
#include "core/graph/graph_utils.h"
 
namespace onnxruntime {
 
Status GRUReplacement::Apply(Graph& graph, Node& gru_node, RewriteRuleEffect& rule_effect, const logging::Logger&) const {
  const auto& gru_inputs = gru_node.MutableInputDefs();
  auto& gru_outputs = gru_node.MutableOutputDefs();
 
  const auto* type_proto = gru_inputs.front()->TypeAsProto();
  ORT_ENFORCE(type_proto && type_proto->has_tensor_type() && type_proto->tensor_type().has_elem_type(),
              "Could not decipher the type of input to GRU node: ", gru_node.Name());
 
  ONNX_NAMESPACE::TypeProto gru_input_type;
  gru_input_type.mutable_tensor_type()->set_elem_type(type_proto->tensor_type().elem_type());
 
  if (gru_outputs.empty() || !gru_outputs[0]->Exists()) {
    // Add all GRU cell optional outputs for computation even if not required to ensure all outputs are computed
    // so that gradient computation can pick them up.
    NodeArg& all_hidden_states = graph.GetOrCreateNodeArg(graph.GenerateNodeArgName("all_hidden_states"),
                                                          &gru_input_type);
    if (!gru_outputs.empty()) {
      gru_outputs[0] = &all_hidden_states;
    } else {
      gru_outputs.push_back(&all_hidden_states);
    }
  }
 
  if (gru_outputs.size() == 1U) {
    NodeArg& placeholder = graph.GetOrCreateNodeArg("", nullptr);
    gru_outputs.push_back(&placeholder);
  }
 
  NodeArg& zrh = graph.GetOrCreateNodeArg(graph.GenerateNodeArgName("zrh"), &gru_input_type);
  gru_outputs.push_back(&zrh);
 
  // GRUTraining should have 3 outputs
  ORT_ENFORCE(gru_node.OutputDefs().size() == 3U);
 
  auto gru_attributes = gru_node.GetAttributes();
  gru_attributes.erase("layout");
 
  Node& gru_training_node = graph.AddNode(graph.GenerateNodeName(gru_node.Name() + "_training"),
                                          "GRUTraining",
                                          "GRU with extra outputs needed for gradient computation.",
                                          gru_inputs,
                                          gru_outputs,
                                          &gru_attributes,  // AddNode makes a copy, so ok to pass pointer to local var
                                          kMSDomain);
 
  // Assign provider to this new node. Provider should be same as the provider for the node being replaced.
  gru_training_node.SetExecutionProviderType(gru_node.GetExecutionProviderType());
  graph_utils::FinalizeNodeFusion(graph, gru_training_node, gru_node);
  rule_effect = RewriteRuleEffect::kRemovedCurrentNode;
  return Status::OK();
}
 
bool GRUReplacement::SatisfyCondition(const Graph&, const Node&, const logging::Logger&) const {
  return true;
}
 
}  // namespace onnxruntime