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

daisytuner / sdfglib / 17368505925

01 Sep 2025 05:29AM UTC coverage: 58.982% (-0.8%) from 59.781%
17368505925

push

github

web-flow
Merge pull request #216 from daisytuner/transitions-bug

Updates API for Sequence transitions to prevent leakage of assignments

239 of 547 new or added lines in 25 files covered. (43.69%)

75 existing lines in 12 files now uncovered.

9190 of 15581 relevant lines covered (58.98%)

115.89 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/reduce_mean.cpp
1
#include "sdfg/data_flow/library_nodes/math/ml/reduce_mean.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
ReduceMeanNode::ReduceMeanNode(
×
12
    size_t element_id,
13
    const DebugInfo &debug_info,
14
    const graph::Vertex vertex,
15
    data_flow::DataFlowGraph &parent,
16
    int axis
17
)
18
    : MathNode(
×
NEW
19
          element_id,
×
NEW
20
          debug_info,
×
NEW
21
          vertex,
×
NEW
22
          parent,
×
23
          LibraryNodeType_ReduceMean,
NEW
24
          {"output"},
×
NEW
25
          {"input"},
×
26
          data_flow::ImplementationType_NONE
27
      ),
28
      axis_(axis) {}
×
29

30
void ReduceMeanNode::validate(const Function &) const { /* TODO */ }
×
31

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

36
    auto &scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
×
37
    auto &parent = static_cast<structured_control_flow::Sequence &>(*scope_analysis.parent_scope(&block));
×
NEW
38
    int index = parent.index(block);
×
NEW
39
    auto &transition = parent.at(index).second;
×
40

41
    // Locate edges
42
    const data_flow::Memlet *iedge_input = nullptr;
×
43
    const data_flow::Memlet *oedge_output = nullptr;
×
44
    for (const auto &edge : dataflow.in_edges(*this)) {
×
45
        if (edge.dst_conn() == "input") {
×
46
            iedge_input = &edge;
×
47
        }
×
48
    }
49
    for (const auto &edge : dataflow.out_edges(*this)) {
×
50
        if (edge.src_conn() == "output") {
×
51
            oedge_output = &edge;
×
52
        }
×
53
    }
54
    if (!iedge_input || !oedge_output) return false;
×
55

56
    std::string input_name = static_cast<const data_flow::AccessNode &>(iedge_input->src()).data();
×
57
    std::string output_name = static_cast<const data_flow::AccessNode &>(oedge_output->dst()).data();
×
58

59
    // Create new sequence before
NEW
60
    auto &new_sequence = builder.add_sequence_before(parent, block, transition.assignments(), block.debug_info());
×
61
    structured_control_flow::Sequence *last_scope = &new_sequence;
×
62

63
    // Create maps over output subset dims (parallel dims)
64
    data_flow::Subset domain_begin = oedge_output->begin_subset();
×
65
    data_flow::Subset domain_end = oedge_output->end_subset();
×
66

67
    std::vector<symbolic::Expression> loop_syms;
×
68
    structured_control_flow::Map *last_map = nullptr;
×
69
    for (size_t d = 0; d < domain_begin.size(); ++d) {
×
70
        std::string indvar_str = builder.find_new_name("_i");
×
71
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
×
72
        auto indvar = symbolic::symbol(indvar_str);
×
73
        auto init = domain_begin[d];
×
74
        auto update = symbolic::add(indvar, symbolic::one());
×
75
        auto cond = symbolic::Lt(indvar, symbolic::add(domain_end[d], symbolic::one()));
×
76
        last_map = &builder.add_map(
×
77
            *last_scope,
×
78
            indvar,
79
            cond,
80
            init,
81
            update,
82
            structured_control_flow::ScheduleType_Sequential,
83
            {},
×
84
            block.debug_info()
×
85
        );
86
        last_scope = &last_map->root();
×
87
        loop_syms.push_back(indvar);
×
88
    }
×
89

90
    // Initialize temp variable to zero
91
    std::string temp_name = builder.find_new_name("_tmp");
×
92
    types::Scalar temp_type(types::PrimitiveType::Float);
×
93
    builder.add_container(temp_name, temp_type);
×
94

95
    auto &init_block = builder.add_block(*last_scope);
×
96
    auto &init_tasklet = builder.add_tasklet(init_block, data_flow::TaskletCode::assign, "_out", {"0.0f"});
×
97
    auto &tmp_access_init = builder.add_access(init_block, temp_name);
×
98
    builder.add_computational_memlet(init_block, init_tasklet, "_out", tmp_access_init, {}, temp_type);
×
99

100
    // add reduction for loop
101
    symbolic::Expression red_begin;
×
102
    symbolic::Expression red_end;
×
103
    if (axis_ >= 0) {
×
104
        red_begin = iedge_input->begin_subset()[axis_];
×
105
        red_end = iedge_input->end_subset()[axis_];
×
106
    } else {
×
107
        red_begin = iedge_input->begin_subset().back();
×
108
        red_end = iedge_input->end_subset().back();
×
109
    }
110
    std::string red_name = builder.find_new_name("_i");
×
111
    builder.add_container(red_name, types::Scalar(types::PrimitiveType::UInt64));
×
112
    auto red_indvar = symbolic::symbol(red_name);
×
113
    auto red_init = red_begin;
×
114
    auto red_update = symbolic::add(red_indvar, symbolic::one());
×
115
    auto red_cond = symbolic::Lt(red_indvar, symbolic::add(red_end, symbolic::one()));
×
NEW
116
    auto red_map = &builder.add_for(*last_scope, red_indvar, red_cond, red_init, red_update, {}, block.debug_info());
×
117

118
    // Create innermost block
119
    auto &code_block = builder.add_block(red_map->root());
×
120

121
    // Create access nodes for input and output
122
    auto &input_access = builder.add_access(code_block, input_name);
×
123
    auto &tmp2_access = builder.add_access(code_block, temp_name);
×
124
    auto &tmp_access_out = builder.add_access(code_block, temp_name);
×
125
    auto &tmp_access_in = builder.add_access(code_block, temp_name);
×
126

127
    // Create index expressions for input and output
128
    std::vector<symbolic::Expression> input_subset = loop_syms;
×
129

130
    // Replace the reduction axis index with the reduction variable for input
131
    if (axis_ >= 0 && axis_ < static_cast<int>(input_subset.size())) {
×
132
        input_subset.insert(input_subset.begin() + axis_, red_indvar);
×
133
    } else if (axis_ < 0) {
×
134
        input_subset.push_back(red_indvar);
×
135
    }
×
136

137
    // Compute division
138
    auto num_elements = symbolic::add(red_end, symbolic::one());
×
139
    auto num_elements_int = SymEngine::rcp_static_cast<const SymEngine::Integer>(num_elements)->as_int();
×
NEW
140
    auto &div_tasklet = builder.add_tasklet(
×
NEW
141
        code_block, data_flow::TaskletCode::div, "_out", {"_in1", std::to_string(num_elements_int) + ".0f"}
×
142
    );
NEW
143
    builder
×
NEW
144
        .add_computational_memlet(code_block, input_access, div_tasklet, "_in", input_subset, iedge_input->base_type());
×
UNCOV
145
    builder.add_computational_memlet(code_block, div_tasklet, "_out", tmp2_access, {}, temp_type);
×
146

147
    // Add to temp (reduction)
148
    auto &add_tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::add, "_out", {"_in1", "_in2"});
×
149
    builder.add_computational_memlet(code_block, tmp_access_in, add_tasklet, "_in1", {}, temp_type);
×
150
    builder.add_computational_memlet(code_block, tmp2_access, add_tasklet, "_in2", {}, temp_type);
×
151
    builder.add_computational_memlet(code_block, add_tasklet, "_out", tmp_access_out, {}, temp_type);
×
152

153
    // Create writeback - assign the accumulated sum to output
154
    auto &writeback_block = builder.add_block(*last_scope);
×
155
    auto &tmp_access_wb = builder.add_access(writeback_block, temp_name);
×
156
    auto &output_access_wb = builder.add_access(writeback_block, output_name);
×
157
    auto &writeback_tasklet = builder.add_tasklet(writeback_block, data_flow::TaskletCode::assign, "_out", {"_in"});
×
158
    builder.add_computational_memlet(writeback_block, tmp_access_wb, writeback_tasklet, "_in", {}, temp_type);
×
NEW
159
    builder.add_computational_memlet(
×
NEW
160
        writeback_block, writeback_tasklet, "_out", output_access_wb, loop_syms, oedge_output->base_type()
×
161
    );
162

163
    // Cleanup old block
164
    builder.remove_memlet(block, *iedge_input);
×
165
    builder.remove_memlet(block, *oedge_output);
×
166
    builder.remove_node(block, *this);
×
NEW
167
    builder.remove_child(parent, index + 1);
×
168

169
    return true;
×
170
}
×
171

172
std::unique_ptr<data_flow::DataFlowNode> ReduceMeanNode::
173
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
NEW
174
    return std::unique_ptr<
×
NEW
175
        data_flow::DataFlowNode>(new ReduceMeanNode(element_id, this->debug_info(), vertex, parent, axis_));
×
UNCOV
176
}
×
177

178
nlohmann::json ReduceMeanNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
179
    const ReduceMeanNode &node = static_cast<const ReduceMeanNode &>(library_node);
×
180
    nlohmann::json j;
×
181

182
    j["code"] = node.code().value();
×
183
    j["axis"] = node.axis();
×
184

185
    return j;
×
186
}
×
187

188
data_flow::LibraryNode &ReduceMeanNodeSerializer::deserialize(
×
189
    const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent
190
) {
191
    auto code = j["code"].get<std::string>();
×
192
    if (code != LibraryNodeType_ReduceMean.value()) {
×
193
        throw std::runtime_error("Invalid library node code");
×
194
    }
195

196
    sdfg::serializer::JSONSerializer serializer;
×
197
    DebugInfo debug_info = serializer.json_to_debug_info(j["debug_info"]);
×
198

199
    auto axis = j["axis"].get<int>();
×
200

201
    return builder.add_library_node<ReduceMeanNode>(parent, debug_info, axis);
×
202
}
×
203

204
} // namespace ml
205
} // namespace math
206
} // 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