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

daisytuner / sdfglib / 17655994367

11 Sep 2025 08:06PM UTC coverage: 60.451% (+1.1%) from 59.335%
17655994367

Pull #219

github

web-flow
Merge 769d2cc0b into 6c1992b40
Pull Request #219: stdlib Library Nodes and ConstantNodes

457 of 1629 new or added lines in 81 files covered. (28.05%)

93 existing lines in 35 files now uncovered.

9382 of 15520 relevant lines covered (60.45%)

107.26 hits per line

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

63.97
/src/data_flow/library_nodes/math/ml/maxpool.cpp
1
#include "sdfg/data_flow/library_nodes/math/ml/maxpool.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
/*************** Constructor ***************/
12
MaxPoolNode::MaxPoolNode(
1✔
13
    size_t element_id,
14
    const DebugInfo &debug_info,
15
    const graph::Vertex vertex,
16
    data_flow::DataFlowGraph &parent,
17
    std::vector<symbolic::Expression> shape,
18
    std::vector<size_t> kernel_shape,
19
    std::vector<size_t> pads,
20
    std::vector<size_t> strides
21
)
22
    : MathNode(
1✔
23
          element_id, debug_info, vertex, parent, LibraryNodeType_MaxPool, {"Y"}, {"X"}, data_flow::ImplementationType_NONE
1✔
24
      ),
25
      shape_(shape), kernel_shape_(std::move(kernel_shape)), pads_(std::move(pads)), strides_(std::move(strides)) {}
1✔
26

27
/*************** Accessors ***************/
NEW
28
const std::vector<symbolic::Expression> &MaxPoolNode::shape() const { return shape_; }
×
29
std::vector<size_t> MaxPoolNode::kernel_shape() const { return kernel_shape_; }
×
30
std::vector<size_t> MaxPoolNode::pads() const { return pads_; }
×
31
std::vector<size_t> MaxPoolNode::strides() const { return strides_; }
×
32

NEW
33
symbolic::SymbolSet MaxPoolNode::symbols() const {
×
NEW
34
    symbolic::SymbolSet syms;
×
NEW
35
    for (const auto &dim : shape_) {
×
NEW
36
        for (auto &atom : symbolic::atoms(dim)) {
×
NEW
37
            syms.insert(atom);
×
38
        }
39
    }
NEW
40
    return syms;
×
NEW
41
}
×
42

NEW
43
void MaxPoolNode::replace(const symbolic::Expression &old_expression, const symbolic::Expression &new_expression) {
×
NEW
44
    for (auto &dim : shape_) {
×
NEW
45
        dim = symbolic::subs(dim, old_expression, new_expression);
×
46
    }
NEW
47
}
×
48

NEW
49
void MaxPoolNode::validate(const Function &) const {}
×
50

51
/*************** Expand ***************/
52
bool MaxPoolNode::expand(builder::StructuredSDFGBuilder &builder, analysis::AnalysisManager &analysis_manager) {
1✔
53
    auto &dataflow = this->get_parent();
1✔
54
    auto &block = static_cast<structured_control_flow::Block &>(*dataflow.get_parent());
1✔
55

56
    auto &scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
1✔
57
    auto &parent = static_cast<structured_control_flow::Sequence &>(*scope_analysis.parent_scope(&block));
1✔
58
    int index = parent.index(block);
1✔
59
    auto &transition = parent.at(index).second;
1✔
60

61
    // Locate edges
62
    const data_flow::Memlet *iedge_X = nullptr;
1✔
63
    const data_flow::Memlet *oedge_Y = nullptr;
1✔
64
    for (const auto &edge : dataflow.in_edges(*this)) {
2✔
65
        if (edge.dst_conn() == "X") {
1✔
66
            iedge_X = &edge;
1✔
67
        }
1✔
68
    }
69
    for (const auto &edge : dataflow.out_edges(*this)) {
2✔
70
        if (edge.src_conn() == "Y") {
1✔
71
            oedge_Y = &edge;
1✔
72
        }
1✔
73
    }
74
    if (!iedge_X || !oedge_Y) return false;
1✔
75

76
    std::string X_name = static_cast<const data_flow::AccessNode &>(iedge_X->src()).data();
1✔
77
    std::string Y_name = static_cast<const data_flow::AccessNode &>(oedge_Y->dst()).data();
1✔
78

79
    // Create new sequence before
80
    auto &new_sequence = builder.add_sequence_before(parent, block, transition.assignments(), block.debug_info());
1✔
81
    structured_control_flow::Sequence *last_scope = &new_sequence;
1✔
82

83
    // Create maps over output subset dims (parallel dims)
84
    data_flow::Subset out_subset;
1✔
85
    std::vector<symbolic::Expression> out_syms;
1✔
86
    structured_control_flow::Map *last_map = nullptr;
1✔
87
    for (size_t d = 0; d < this->shape_.size(); ++d) {
5✔
88
        std::string indvar_str = builder.find_new_name("_i");
4✔
89
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
4✔
90
        auto indvar = symbolic::symbol(indvar_str);
4✔
91
        auto init = symbolic::zero();
4✔
92
        auto update = symbolic::add(indvar, symbolic::one());
4✔
93
        auto cond = symbolic::Lt(indvar, this->shape_[d]);
4✔
94
        last_map = &builder.add_map(
8✔
95
            *last_scope,
4✔
96
            indvar,
97
            cond,
98
            init,
4✔
99
            update,
100
            structured_control_flow::ScheduleType_Sequential::create(),
4✔
101
            {},
4✔
102
            block.debug_info()
4✔
103
        );
104
        last_scope = &last_map->root();
4✔
105
        out_subset.push_back(indvar);
4✔
106
        out_syms.push_back(indvar);
4✔
107
    }
4✔
108

109
    // Kernel reduction loops
110
    structured_control_flow::For *last_for = nullptr;
1✔
111
    std::vector<symbolic::Expression> kernel_syms;
1✔
112
    for (size_t d = 0; d < kernel_shape_.size(); ++d) {
3✔
113
        std::string indvar_str = builder.find_new_name("_k");
2✔
114
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
2✔
115
        auto indvar = symbolic::symbol(indvar_str);
2✔
116
        auto init = symbolic::integer(0);
2✔
117
        auto update = symbolic::add(indvar, symbolic::one());
2✔
118
        auto cond = symbolic::Lt(indvar, symbolic::integer(static_cast<int64_t>(kernel_shape_[d])));
2✔
119
        last_for = &builder.add_for(*last_scope, indvar, cond, init, update, {}, block.debug_info());
2✔
120
        last_scope = &last_for->root();
2✔
121
        kernel_syms.push_back(indvar);
2✔
122
    }
2✔
123

124
    // Build subsets
125
    // infer: X dims N,C,H,W => assume same dims as Y except spatial dims with stride/pad
126

127
    auto int_expr = [](size_t v) { return symbolic::integer(static_cast<int64_t>(v)); };
5✔
128
    auto get_stride = [&](size_t idx) -> symbolic::Expression {
3✔
129
        return idx < strides_.size() ? int_expr(strides_[idx]) : symbolic::one();
2✔
130
    };
131
    auto get_pad = [&](size_t idx) -> symbolic::Expression {
3✔
132
        return idx < pads_.size() ? int_expr(pads_[idx]) : symbolic::zero();
2✔
133
    };
134

135
    // Create innermost block
136
    auto &code_block = builder.add_block(*last_scope, {}, block.debug_info());
1✔
137

138
    // Access nodes
139
    const DebugInfo dbg = block.debug_info();
1✔
140
    auto &X_acc = builder.add_access(code_block, X_name, dbg);
1✔
141
    auto &Y_acc_in = builder.add_access(code_block, Y_name, dbg);
1✔
142
    auto &Y_acc_out = builder.add_access(code_block, Y_name, dbg);
1✔
143

144
    // Build X subset using output coords * stride - pad + kernel_idx
145
    data_flow::Subset subset_X;
1✔
146
    // Assume dims: N, C, spatial...
147
    subset_X.push_back(out_syms[0]); // N
1✔
148
    subset_X.push_back(out_syms[1]); // C
1✔
149
    for (size_t d = 0; d < kernel_syms.size(); ++d) {
3✔
150
        auto expr =
151
            symbolic::sub(symbolic::add(symbolic::mul(get_stride(d), out_syms[2 + d]), kernel_syms[d]), get_pad(d));
2✔
152
        subset_X.push_back(expr);
2✔
153
    }
2✔
154

155
    // Y subset is just out_subset
156
    data_flow::Subset subset_Y = out_subset;
1✔
157

158
    // Tasklet max
159
    auto &tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::max, "_out", {"_in1", "_in2"}, dbg);
1✔
160

161
    builder.add_computational_memlet(code_block, Y_acc_in, tasklet, "_in1", subset_Y, oedge_Y->base_type(), dbg);
1✔
162
    builder.add_computational_memlet(code_block, X_acc, tasklet, "_in2", subset_X, iedge_X->base_type(), dbg);
1✔
163
    builder.add_computational_memlet(code_block, tasklet, "_out", Y_acc_out, subset_Y, oedge_Y->base_type(), dbg);
1✔
164

165
    // Cleanup old block
166
    builder.remove_memlet(block, *iedge_X);
1✔
167
    builder.remove_memlet(block, *oedge_Y);
1✔
168
    builder.remove_node(block, *this);
1✔
169
    builder.remove_child(parent, index + 1);
1✔
170

171
    return true;
1✔
172
}
1✔
173

174
/*************** Clone ***************/
175
std::unique_ptr<data_flow::DataFlowNode> MaxPoolNode::
176
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
177
    return std::unique_ptr<data_flow::DataFlowNode>(new MaxPoolNode(
×
NEW
178
        element_id, this->debug_info(), vertex, parent, this->shape_, this->kernel_shape_, this->pads_, this->strides_
×
179
    ));
180
}
×
181

182
/*************** Serializer ***************/
183
nlohmann::json MaxPoolNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
184
    const MaxPoolNode &node = static_cast<const MaxPoolNode &>(library_node);
×
185
    nlohmann::json j;
×
186
    j["code"] = node.code().value();
×
187
    j["outputs"] = node.outputs();
×
188
    j["inputs"] = node.inputs();
×
189
    j["kernel_shape"] = node.kernel_shape();
×
190
    j["pads"] = node.pads();
×
191
    j["strides"] = node.strides();
×
192

NEW
193
    serializer::JSONSerializer serializer;
×
NEW
194
    j["shape"] = nlohmann::json::array();
×
NEW
195
    for (auto &dim : node.shape()) {
×
NEW
196
        j["shape"].push_back(serializer.expression(dim));
×
197
    }
198

199
    return j;
×
200
}
×
201

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

212
    auto kernel_shape = j["kernel_shape"].get<std::vector<size_t>>();
×
213
    auto pads = j["pads"].get<std::vector<size_t>>();
×
214
    auto strides = j["strides"].get<std::vector<size_t>>();
×
215

NEW
216
    std::vector<symbolic::Expression> shape;
×
NEW
217
    for (const auto &dim : j["shape"]) {
×
NEW
218
        shape.push_back(SymEngine::Expression(dim.get<std::string>()));
×
219
    }
220

NEW
221
    return builder.add_library_node<MaxPoolNode>(parent, debug_info, shape, kernel_shape, pads, strides);
×
UNCOV
222
}
×
223

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