Исходный код СУБД 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
// Copyright (c) Intel Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <string>
#include "core/optimizer/graph_transformer.h"
#include "core/framework/kernel_registry_manager.h"
#include "core/framework/kernel_registry.h"
 
namespace onnxruntime {
 
/**
 * @class FuseInitializersTransformer
 *
 * A Transformer to fuse cast node that casts from init_type to cvt_type, back to their next/output nodes.
 * Below is the explanation on how this transforms works. It depends on "InsertCastTransforms" to produce the
 * intermediate representation from which it fuses the initializers (which are the cast node with zero input,
 * one initializer, and one output) back to the next/output node. After fusion, the link/edge between such
 * cast node to next/output node will then be removed.
 *
 *
 * ```
 *
 *         "Input Graph"                       "Intermediate Representation"               "Fusion Transforms"
 *
 *           --------                   --------        --------        --------                 --------
 *          | X_Fp16 |                 | X_Fp16 |      | W_Fp16 |      | B_Fp16 |               | X_Fp16 |
 *           --------                   --------        --------        --------                 --------
 *              |                          |               |               |                        |
 *              |                          |               |               |                        |
 *              |                          V               V               V                        V
 *              |                       | Cast |        | Cast |        | Cast |                 | Cast |
 *              |                       | Fp16 |        | Fp16 |        | Fp16 |                 | Fp16 |
 *              |                       |  To  |        |  To  |        |  To  |                 |  To  |
 *              |                       | Fp32 |        | Fp32 |        | Fp32 |                 | Fp32 |
 *              |                          |               |               |                        |
 *              |                          |               |               |                        |
 *              V                          V               V               V                        V
 *  ----------------------------       -----------------------------------------       ----------------------------
 * |        Conv_Fp16           |     |                                         |     |         Conv_Fp32          |
 * |        --W_Fp16--          | ==> |                Conv_Fp32                | ==> |         --W_Fp32--         |
 * |        --B_Fp16--          |     |                                         |     |         --B_Fp32--         |
 *  ----------------------------       -----------------------------------------       ----------------------------
 *              |                                          |                                        |
 *              |                                          |                                        |
 *              |                                          V                                        V
 *              |                                       | Cast |                                 | Cast |
 *              |                                       | Fp32 |                                 | Fp32 |
 *              |                                       |  To  |                                 |  To  |
 *              |                                       | Fp16 |                                 | Fp16 |
 *              |                                          |                                        |
 *              |                                          |                                        |
 *              V                                          V                                        V
 *           --------                                   --------                                 --------
 *          | Y_Fp16 |                                 | Y_Fp16 |                               | Y_Fp16 |
 *           --------                                   --------                                 --------
 *
 * ```
 *
 */
class FuseInitializersTransformer : public GraphTransformer {
 public:
  /**
   * @brief   Fuses Initializers to child node after conversion to child node kernel type
   *          to save compute on Cast Op during inference.
   *
   * This transforms must be applied after InsertCastTransformer. Currently only FP16 Initializers are fused with
   * nodes supporting FP32 Initializers, however, the code is designed to apply for any supported conversion/s.
   *
   * @param name          Name of the transforms, just for logging purpose.
   * @param init_type     The unsupported type for which cast nodes are inserted to convert
   *                      the initializers to supported type initializers expected by next/output nodes.
   * @param cvt_type      The supported type initializers expected by next/output nodes.
   * @param thread_pool   A pointer to thread pool to support conversion from init_type to cvt_type
   *                      with multithreading
   */
  FuseInitializersTransformer(const std::string& name,
                              const onnxruntime::MLDataType init_type,
                              const onnxruntime::MLDataType cvt_type,
                              onnxruntime::concurrency::ThreadPool* thread_pool = nullptr) : GraphTransformer(name, {}),
                                                                                             init_type_(init_type),
                                                                                             cvt_type_(cvt_type),
                                                                                             thread_pool_(thread_pool) {}
 
 private:
  const onnxruntime::MLDataType init_type_;
  const onnxruntime::MLDataType cvt_type_;
  onnxruntime::concurrency::ThreadPool* thread_pool_;
  Status ApplyImpl(
      Graph& graph,
      bool& modified,
      int graph_level,
      const logging::Logger& logger) const override;
};
}  // namespace onnxruntime