Исходный код СУБД 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include <vector>
#include "orttraining/core/graph/loss_func/mean_squared_error.h"
#include "onnx/defs/attr_proto_util.h"
 
namespace onnxruntime {
namespace training {
 
GraphAugmenter::GraphDefs MeanSquaredError::operator()(const Graph& graph, const LossFunctionInfo& loss_func_info) {
  const std::string& loss_name = loss_func_info.loss_name;
  const VectorString& args = loss_func_info.loss_builder_args;
  ORT_ENFORCE(args.size() == 2, " Invalid loss_func_info for MeanSquaredError.");
  const std::string& prediction_name = args[0];
  const std::string& label_name = args[1];
 
  GraphAugmenter::GraphDefs graph_defs;
 
  graph_defs.AddGraphInputs({label_name});
  graph_defs.AddGraphOutputs({loss_name});
 
  std::vector<NodeDef> new_nodes;
  // Sub
  {
    const NodeArg* prediction_arg = graph.GetNodeArg(prediction_name);
    ORT_ENFORCE(prediction_arg != nullptr,
                "Prediction arg ", prediction_name, " is not found in the graph.");
    TypeProto* label_type_proto = graph_defs.CopyTypeProto(prediction_arg);
 
    new_nodes.emplace_back(NodeDef("Sub",  // Op
                                   {
                                       ArgDef(prediction_name),  // Inputs
                                       ArgDef(label_name, label_type_proto),
                                   },
                                   {
                                       ArgDef("MeanSquaredError_diff"),  // Outputs
                                   },
                                   NodeAttributes(),
                                   "MeanSquaredError_diff"  // name
                                   ));
  }
  // Pow
  {
    onnx::TensorProto tensor_proto;
    tensor_proto.set_data_type(ONNX_NAMESPACE::TensorProto_DataType_FLOAT);
    tensor_proto.add_float_data(2.f);
    tensor_proto.set_name("MeanSquaredError_exponent");
    graph_defs.AddInitializers({tensor_proto});
 
    new_nodes.emplace_back(NodeDef("Pow",  // Op
                                   {
                                       ArgDef("MeanSquaredError_diff"),  // Inputs
                                       ArgDef("MeanSquaredError_exponent"),
                                   },
                                   {
                                       ArgDef("MeanSquaredError_diff_square"),  // Outputs
                                   },
                                   NodeAttributes(),
                                   "MeanSquaredError_pow"  // name
                                   ));
  }
  // ReduceMean
  {
    new_nodes.emplace_back(NodeDef("ReduceMean",  // Op
                                   {
                                       ArgDef("MeanSquaredError_diff_square"),  // Inputs
                                   },
                                   {
                                       ArgDef(loss_name),  // Outputs
                                   },
                                   {ONNX_NAMESPACE::MakeAttribute("keepdims", int64_t(0))},
                                   "MeanSquaredError_reduce_mean"  // name
                                   ));
  }
 
  graph_defs.AddNodeDefs(new_nodes);
 
  return graph_defs;
}
 
}  // namespace training
}  // namespace onnxruntime