• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

daisytuner / sdfglib / 17316805645

29 Aug 2025 06:46AM UTC coverage: 60.01% (+0.2%) from 59.781%
17316805645

Pull #210

github

web-flow
Merge cd9cb386d into 18d34db1e
Pull Request #210: New debug info

351 of 562 new or added lines in 37 files covered. (62.46%)

15 existing lines in 8 files now uncovered.

9574 of 15954 relevant lines covered (60.01%)

115.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/data_flow/library_nodes/math/ml/batch_normalization.cpp
1
#include "sdfg/data_flow/library_nodes/math/ml/batch_normalization.h"
2

3
#include "sdfg/analysis/analysis.h"
4
#include "sdfg/analysis/scope_analysis.h"
5
#include "sdfg/builder/structured_sdfg_builder.h"
6

7
namespace sdfg {
8
namespace math {
9
namespace ml {
10

11
BatchNormalizationNode::BatchNormalizationNode(
×
12
    size_t element_id,
13
    const DebugInfoRegion &debug_info,
14
    const graph::Vertex vertex,
15
    data_flow::DataFlowGraph &parent,
16
    int axis,
17
    const std::string &epsilon
18
)
19
    : MathNode(
×
NEW
20
          element_id,
×
NEW
21
          debug_info,
×
NEW
22
          vertex,
×
NEW
23
          parent,
×
24
          LibraryNodeType_BatchNormalization,
NEW
25
          {"Y"},
×
NEW
26
          {"X", "Scale", "B", "input_mean", "input_var"},
×
27
          data_flow::ImplementationType_NONE
28
      ),
29
      axis_(axis), epsilon_(epsilon) {}
×
30

31
void BatchNormalizationNode::validate(const Function &) const { /* TODO */ }
×
32

33
bool BatchNormalizationNode::expand(builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager) {
×
34
    auto &dataflow = this->get_parent();
×
35
    auto &block = static_cast<structured_control_flow::Block &>(*dataflow.get_parent());
×
36

37
    auto &scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
×
38
    auto &parent = static_cast<structured_control_flow::Sequence &>(*scope_analysis.parent_scope(&block));
×
39

40
    // Locate edges
41
    const data_flow::Memlet *iedge_input = nullptr;
×
42
    const data_flow::Memlet *iedge_scale = nullptr;
×
43
    const data_flow::Memlet *iedge_bias = nullptr;
×
44
    const data_flow::Memlet *iedge_mean = nullptr;
×
45
    const data_flow::Memlet *iedge_var = nullptr;
×
46
    const data_flow::Memlet *oedge_output = nullptr;
×
47
    for (const auto &edge : dataflow.in_edges(*this)) {
×
48
        if (edge.dst_conn() == "X") {
×
49
            iedge_input = &edge;
×
50
        } else if (edge.dst_conn() == "Scale") {
×
51
            iedge_scale = &edge;
×
52
        } else if (edge.dst_conn() == "B") {
×
53
            iedge_bias = &edge;
×
54
        } else if (edge.dst_conn() == "input_mean") {
×
55
            iedge_mean = &edge;
×
56
        } else if (edge.dst_conn() == "input_var") {
×
57
            iedge_var = &edge;
×
58
        }
×
59
    }
60
    for (const auto &edge : dataflow.out_edges(*this)) {
×
61
        if (edge.src_conn() == "Y") {
×
62
            oedge_output = &edge;
×
63
        }
×
64
    }
65
    if (!iedge_input || !iedge_scale || !iedge_bias || !iedge_mean || !iedge_var || !oedge_output) return false;
×
66

67
    std::string input_name = static_cast<const data_flow::AccessNode &>(iedge_input->src()).data();
×
68
    std::string scale_name = static_cast<const data_flow::AccessNode &>(iedge_scale->src()).data();
×
69
    std::string bias_name = static_cast<const data_flow::AccessNode &>(iedge_bias->src()).data();
×
70
    std::string mean_name = static_cast<const data_flow::AccessNode &>(iedge_mean->src()).data();
×
71
    std::string var_name = static_cast<const data_flow::AccessNode &>(iedge_var->src()).data();
×
72
    std::string output_name = static_cast<const data_flow::AccessNode &>(oedge_output->dst()).data();
×
73

74
    // Create new sequence before
75
    auto &new_sequence = builder.add_sequence_before(parent, block, block.debug_info()).first;
×
76
    structured_control_flow::Sequence *last_scope = &new_sequence;
×
77

78
    // Create maps over output subset dims (parallel dims)
79
    data_flow::Subset domain_begin = oedge_output->begin_subset();
×
80
    data_flow::Subset domain_end = oedge_output->end_subset();
×
81

82
    std::vector<symbolic::Expression> loop_syms;
×
83
    structured_control_flow::Map *last_map = nullptr;
×
84
    for (size_t d = 0; d < domain_begin.size(); ++d) {
×
85
        std::string indvar_str = builder.find_new_name("_i");
×
86
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
×
87
        auto indvar = symbolic::symbol(indvar_str);
×
88
        auto init = domain_begin[d];
×
89
        auto update = symbolic::add(indvar, symbolic::one());
×
90
        auto cond = symbolic::Lt(indvar, symbolic::add(domain_end[d], symbolic::one()));
×
91
        last_map = &builder.add_map(
×
92
            *last_scope,
×
93
            indvar,
94
            cond,
95
            init,
96
            update,
97
            structured_control_flow::ScheduleType_Sequential,
98
            {},
×
99
            block.debug_info()
×
100
        );
101
        last_scope = &last_map->root();
×
102
        loop_syms.push_back(indvar);
×
103
    }
×
104

105
    // Create normalization block
106
    auto &norm_block = builder.add_block(*last_scope);
×
107

108
    // Create access nodes for normalization
109
    auto &input_access_norm = builder.add_access(norm_block, input_name);
×
110
    auto &scale_access_norm = builder.add_access(norm_block, scale_name);
×
111
    auto &bias_access_norm = builder.add_access(norm_block, bias_name);
×
112
    auto &mean_access_norm = builder.add_access(norm_block, mean_name);
×
113
    auto &var_access_norm = builder.add_access(norm_block, var_name);
×
114
    auto &output_access_norm = builder.add_access(norm_block, output_name);
×
115

116
    // Add epsilon to variance and compute standard deviation
NEW
117
    auto &add_epsilon_tasklet =
×
NEW
118
        builder.add_tasklet(norm_block, data_flow::TaskletCode::add, "_out", {"_in1", epsilon_});
×
119
    auto &var_eps_access = builder.add_access(norm_block, builder.find_new_name("_var_eps"));
×
NEW
120
    builder.add_computational_memlet(
×
NEW
121
        norm_block, var_access_norm, add_epsilon_tasklet, "_in1", loop_syms, iedge_var->base_type()
×
122
    );
NEW
123
    builder
×
NEW
124
        .add_computational_memlet(norm_block, add_epsilon_tasklet, "_out", var_eps_access, {}, iedge_var->base_type());
×
125

126
    auto &sqrt_tasklet = builder.add_tasklet(norm_block, data_flow::TaskletCode::sqrt, "_out", {"_in"});
×
127
    auto &std_dev_access = builder.add_access(norm_block, builder.find_new_name("_std_dev"));
×
128
    builder.add_computational_memlet(norm_block, var_eps_access, sqrt_tasklet, "_in", {}, iedge_var->base_type());
×
129
    builder.add_computational_memlet(norm_block, sqrt_tasklet, "_out", std_dev_access, {}, iedge_var->base_type());
×
130

131
    // Normalize: (x - mean) / std_dev
132
    auto &sub_norm_tasklet = builder.add_tasklet(norm_block, data_flow::TaskletCode::sub, "_out", {"_in1", "_in2"});
×
133
    auto &centered_access = builder.add_access(norm_block, builder.find_new_name("_centered"));
×
NEW
134
    builder.add_computational_memlet(
×
NEW
135
        norm_block, input_access_norm, sub_norm_tasklet, "_in1", loop_syms, iedge_input->base_type()
×
136
    );
NEW
137
    builder.add_computational_memlet(
×
NEW
138
        norm_block, mean_access_norm, sub_norm_tasklet, "_in2", loop_syms, iedge_mean->base_type()
×
139
    );
NEW
140
    builder
×
NEW
141
        .add_computational_memlet(norm_block, sub_norm_tasklet, "_out", centered_access, {}, iedge_input->base_type());
×
142

143
    auto &div_norm_tasklet = builder.add_tasklet(norm_block, data_flow::TaskletCode::div, "_out", {"_in1", "_in2"});
×
144
    auto &normalized_access = builder.add_access(norm_block, builder.find_new_name("_normalized"));
×
NEW
145
    builder
×
NEW
146
        .add_computational_memlet(norm_block, centered_access, div_norm_tasklet, "_in1", {}, iedge_input->base_type());
×
NEW
147
    builder
×
NEW
148
        .add_computational_memlet(norm_block, std_dev_access, div_norm_tasklet, "_in2", loop_syms, iedge_var->base_type());
×
NEW
149
    builder
×
NEW
150
        .add_computational_memlet(norm_block, div_norm_tasklet, "_out", normalized_access, {}, iedge_input->base_type());
×
151

152
    // Apply scale and bias: scale * normalized + bias
153
    auto &mul_scale_tasklet = builder.add_tasklet(norm_block, data_flow::TaskletCode::mul, "_out", {"_in1", "_in2"});
×
154
    auto &scaled_access = builder.add_access(norm_block, builder.find_new_name("_scaled"));
×
NEW
155
    builder
×
NEW
156
        .add_computational_memlet(norm_block, normalized_access, mul_scale_tasklet, "_in1", {}, iedge_input->base_type());
×
NEW
157
    builder.add_computational_memlet(
×
NEW
158
        norm_block, scale_access_norm, mul_scale_tasklet, "_in2", loop_syms, iedge_scale->base_type()
×
159
    );
UNCOV
160
    builder.add_computational_memlet(norm_block, mul_scale_tasklet, "_out", scaled_access, {}, iedge_input->base_type());
×
161

162
    auto &add_bias_tasklet = builder.add_tasklet(norm_block, data_flow::TaskletCode::add, "_out", {"_in1", "_in2"});
×
163
    builder.add_computational_memlet(norm_block, scaled_access, add_bias_tasklet, "_in1", {}, iedge_input->base_type());
×
NEW
164
    builder.add_computational_memlet(
×
NEW
165
        norm_block, bias_access_norm, add_bias_tasklet, "_in2", loop_syms, iedge_bias->base_type()
×
166
    );
NEW
167
    builder.add_computational_memlet(
×
NEW
168
        norm_block, add_bias_tasklet, "_out", output_access_norm, loop_syms, oedge_output->base_type()
×
169
    );
170

171
    // Cleanup old block
172
    builder.remove_memlet(block, *iedge_input);
×
173
    builder.remove_memlet(block, *iedge_scale);
×
174
    if (iedge_bias) {
×
175
        builder.remove_memlet(block, *iedge_bias);
×
176
    }
×
177
    builder.remove_memlet(block, *iedge_mean);
×
178
    builder.remove_memlet(block, *iedge_var);
×
179
    builder.remove_memlet(block, *oedge_output);
×
180
    builder.remove_node(block, *this);
×
181
    builder.remove_child(parent, block);
×
182

183
    return true;
×
184
}
×
185

186
std::unique_ptr<data_flow::DataFlowNode> BatchNormalizationNode::
187
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
NEW
188
    return std::unique_ptr<data_flow::DataFlowNode>(
×
NEW
189
        new BatchNormalizationNode(element_id, this->debug_info(), vertex, parent, axis_, epsilon_)
×
190
    );
UNCOV
191
}
×
192

193
nlohmann::json BatchNormalizationNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
194
    const BatchNormalizationNode &node = static_cast<const BatchNormalizationNode &>(library_node);
×
195
    nlohmann::json j;
×
196

197
    j["code"] = node.code().value();
×
198
    j["axis"] = node.axis();
×
199
    j["epsilon"] = node.epsilon();
×
200

201
    return j;
×
202
}
×
203

204
data_flow::LibraryNode &BatchNormalizationNodeSerializer::deserialize(
×
205
    const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent
206
) {
207
    auto code = j["code"].get<std::string>();
×
208
    if (code != LibraryNodeType_BatchNormalization.value()) {
×
209
        throw std::runtime_error("Invalid library node code");
×
210
    }
211

212
    sdfg::serializer::JSONSerializer serializer;
×
NEW
213
    DebugInfoRegion debug_info = serializer.json_to_debug_info_region(j["debug_info"], builder.debug_info());
×
214

215
    auto axis = j["axis"].get<int>();
×
216
    auto epsilon = j["epsilon"].get<std::string>();
×
217

218
    return builder.add_library_node<BatchNormalizationNode>(parent, debug_info, axis, epsilon);
×
219
}
×
220

221
} // namespace ml
222
} // namespace math
223
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc