• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daisytuner / sdfglib / 17637380013

11 Sep 2025 07:29AM UTC coverage: 59.755% (+0.6%) from 59.145%
17637380013

push

github

web-flow
New debug info (#210)

* initial draft

* update data structure and construction logic

* finalize DebugInfo draft

* fix tests

* Update serializer and fix tests

* fix append bug

* update data structure

* sdfg builder update

* const ref vectors

* update implementation and partial tests

* compiling state

* update serializer interface

* update dot test

* reset interface to debug_info in json to maintain compatibility with tools

* first review batch

* second batch of changes

* merge fixes

777 of 1111 new or added lines in 46 files covered. (69.94%)

11 existing lines in 11 files now uncovered.

9755 of 16325 relevant lines covered (59.75%)

115.06 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(
×
20
          element_id,
×
21
          debug_info,
×
22
          vertex,
×
23
          parent,
×
24
          LibraryNodeType_BatchNormalization,
25
          {"Y"},
×
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
    int index = parent.index(block);
×
40
    auto &transition = parent.at(index).second;
×
41

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

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

76
    // Create new sequence before
NEW
77
    auto &new_sequence = builder.add_sequence_before(
×
NEW
78
        parent, block, transition.assignments(), builder.debug_info().get_region(block.debug_info().indices())
×
79
    );
UNCOV
80
    structured_control_flow::Sequence *last_scope = &new_sequence;
×
81

82
    // Create maps over output subset dims (parallel dims)
83
    data_flow::Subset domain_begin = oedge_output->begin_subset();
×
84
    data_flow::Subset domain_end = oedge_output->end_subset();
×
85

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

109
    // Create normalization block
110
    auto &norm_block = builder.add_block(*last_scope);
×
111

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

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

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

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

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

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

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

175
    // Cleanup old block
176
    builder.remove_memlet(block, *iedge_input);
×
177
    builder.remove_memlet(block, *iedge_scale);
×
178
    if (iedge_bias) {
×
179
        builder.remove_memlet(block, *iedge_bias);
×
180
    }
×
181
    builder.remove_memlet(block, *iedge_mean);
×
182
    builder.remove_memlet(block, *iedge_var);
×
183
    builder.remove_memlet(block, *oedge_output);
×
184
    builder.remove_node(block, *this);
×
185
    builder.remove_child(parent, index + 1);
×
186

187
    return true;
×
188
}
×
189

190
std::unique_ptr<data_flow::DataFlowNode> BatchNormalizationNode::
191
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
192
    return std::unique_ptr<data_flow::DataFlowNode>(
×
193
        new BatchNormalizationNode(element_id, this->debug_info(), vertex, parent, axis_, epsilon_)
×
194
    );
195
}
×
196

197
nlohmann::json BatchNormalizationNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
198
    const BatchNormalizationNode &node = static_cast<const BatchNormalizationNode &>(library_node);
×
199
    nlohmann::json j;
×
200

201
    j["code"] = node.code().value();
×
202
    j["axis"] = node.axis();
×
203
    j["epsilon"] = node.epsilon();
×
204

205
    return j;
×
206
}
×
207

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

216
    sdfg::serializer::JSONSerializer serializer;
×
NEW
217
    DebugInfoRegion debug_info = serializer.json_to_debug_info_region(j["debug_info"], builder.debug_info());
×
218

219
    auto axis = j["axis"].get<int>();
×
220
    auto epsilon = j["epsilon"].get<std::string>();
×
221

222
    return builder.add_library_node<BatchNormalizationNode>(parent, debug_info, axis, epsilon);
×
223
}
×
224

225
} // namespace ml
226
} // namespace math
227
} // 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