BnTraining.cpp 7 KB
Newer Older
yanjl1's avatar
Initial  
yanjl1 committed
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
#include <iostream>

#include <hipdnn_data_sdk/utilities/Tensor.hpp>
#include <hipdnn_frontend.hpp>

#include "hipdnn_data_sdk/utilities/Workspace.hpp"
#include "utils.hpp"

int main()
{
    using InputType = hipdnn_data_sdk::types::half;

    const int64_t n = 16; // Batch size
    // Input
    const int64_t c = 16; // Number of channels
    const int64_t h = 16; // Height
    const int64_t w = 16; // Width

    auto buildBnTrainingGraph = [=](hipdnnHandle_t handle) {
        auto graph = std::make_shared<hipdnn_frontend::graph::Graph>();
        graph->set_name("bn_training_graph")
            .set_io_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
            .set_intermediate_data_type(hipdnn_frontend::getDataTypeEnumFromType<InputType>())
            .set_compute_data_type(hipdnn_frontend::DataType::FLOAT);

        auto x = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("x")
                .set_dim({n, c, h, w})
                .set_stride({c * h * w, 1, c * w, c}));

        auto scale = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("scale")
                .set_dim({1, c, 1, 1})
                .set_stride({c, 1, c, c}));

        auto bias = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("bias")
                .set_dim({1, c, 1, 1})
                .set_stride({c, 1, c, c}));

        auto prevRunningMean = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("prev_running_mean")
                .set_dim({1, c, 1, 1})
                .set_stride({c, 1, c, c}));

        auto prevRunningVar = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("prev_running_variance")
                .set_dim({1, c, 1, 1})
                .set_stride({c, 1, c, c}));

        auto momentum = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("momentum")
                .set_dim({1, 1, 1, 1})
                .set_stride({1, 1, 1, 1}));

        auto epsilon = std::make_shared<hipdnn_frontend::graph::TensorAttributes>(
            hipdnn_frontend::graph::Tensor_attributes()
                .set_name("epsilon")
                .set_dim({1, 1, 1, 1})
                .set_stride({1, 1, 1, 1}));

        epsilon->set_value(1e-5);
        momentum->set_value(0.1);

        auto bnTrainingAttributes
            = hipdnn_frontend::graph::BatchnormAttributes()
                  .set_name("bn_training_node")
                  .set_epsilon(epsilon)
                  .set_previous_running_stats(prevRunningMean, prevRunningVar, momentum);

        auto [y, savedMean, savedInvVariance, nextRunningMean, nextRunningVar]
            = graph->batchnorm(x, scale, bias, bnTrainingAttributes);
        y->set_output(true);
        nextRunningMean->set_output(true);
        nextRunningVar->set_output(true);
        savedMean->set_output(true);
        savedInvVariance->set_output(true);

        // build graph
        HIPDNN_FE_CHECK(graph->build(handle));

        return std::make_tuple(graph,
                               x,
                               scale,
                               bias,
                               prevRunningMean,
                               prevRunningVar,
                               momentum,
                               epsilon,
                               y,
                               savedMean,
                               savedInvVariance,
                               nextRunningMean,
                               nextRunningVar);
    };

    auto backend = hipdnn_frontend::detail::hipdnnBackend();
    if(!backend)
    {
        std::cout << "Creat backend failed. \n";
        return 1;
    }

    hipdnnHandle_t handle;
    HIPDNN_CHECK(backend->create(&handle));

    auto [graph,
          x,
          scale,
          bias,
          prevRunningMean,
          prevRunningVar,
          momentum,
          epsilon,
          y,
          savedMean,
          savedInvVariance,
          nextRunningMean,
          nextRunningVar]
        = buildBnTrainingGraph(handle);

    hipdnn_data_sdk::utilities::Tensor<InputType> xTensor(x->get_dim(), x->get_stride());
    hipdnn_data_sdk::utilities::Tensor<InputType> scaleTensor(scale->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> biasTensor(bias->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> prevMeanTensor(prevRunningMean->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> prevVarTensor(prevRunningVar->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> momentumTensor(momentum->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> epsilonTensor(epsilon->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> yTensor(y->get_dim(), y->get_stride());
    hipdnn_data_sdk::utilities::Tensor<InputType> savedMeanTensor(savedMean->get_dim());
    hipdnn_data_sdk::utilities::Tensor<InputType> savedInvVarTensor(savedInvVariance->get_dim());

    std::unordered_map<int64_t, void*> variantPack;
    variantPack[x->get_uid()] = xTensor.memory().deviceData();
    variantPack[scale->get_uid()] = scaleTensor.memory().deviceData();
    variantPack[bias->get_uid()] = biasTensor.memory().deviceData();
    variantPack[prevRunningMean->get_uid()] = prevMeanTensor.memory().deviceData();
    variantPack[prevRunningVar->get_uid()] = prevVarTensor.memory().deviceData();
    variantPack[momentum->get_uid()] = momentumTensor.memory().deviceData();
    variantPack[epsilon->get_uid()] = epsilonTensor.memory().deviceData();
    variantPack[y->get_uid()] = yTensor.memory().deviceData();

    // hipDNN uses two separate memory blocks to store the statistics before and after updates,
    // whereas MIOpen only uses one memory block to store them.
    // To accommodate this difference, both the prev and next statistics in the hipDNN interface are pointed to the same memory address here,
    // and the plugin layer passes this address to MIOpen.
    variantPack[nextRunningMean->get_uid()] = prevMeanTensor.memory().deviceData();
    variantPack[nextRunningVar->get_uid()] = prevVarTensor.memory().deviceData();
    variantPack[savedMean->get_uid()] = savedMeanTensor.memory().deviceData();
    variantPack[savedInvVariance->get_uid()] = savedInvVarTensor.memory().deviceData();

    int64_t workspaceSize = 0;
    HIPDNN_FE_CHECK(graph->get_workspace_size(workspaceSize));
    const hipdnn_data_sdk::utilities::Workspace workspace(static_cast<size_t>(workspaceSize));

    HIPDNN_FE_CHECK(graph->execute(handle, variantPack, workspace.get()));

    std::cout << "Batch normalization training graph execution complete. ";

    HIPDNN_CHECK(backend->destroy(handle));
    return 0;
}