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

daisytuner / docc / 26831349290

02 Jun 2026 03:50PM UTC coverage: 61.29% (-0.01%) from 61.302%
26831349290

Pull #725

github

web-flow
Merge a7e2175c0 into 887730e20
Pull Request #725: Tensor node backport

932 of 1642 new or added lines in 52 files covered. (56.76%)

92 existing lines in 33 files now uncovered.

35584 of 58058 relevant lines covered (61.29%)

11020.18 hits per line

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

68.32
/sdfg/src/data_flow/library_nodes/math/tensor/conv_node.cpp
1
#include "sdfg/data_flow/library_nodes/math/tensor/conv_node.h"
2

3
#include <map>
4
#include <sstream>
5
#include <utility>
6

7
#include "sdfg/analysis/analysis.h"
8
#include "sdfg/builder/structured_sdfg_builder.h"
9
#include "sdfg/data_flow/access_node.h"
10
#include "sdfg/data_flow/library_nodes/math/blas/blas_node.h"
11
#include "sdfg/data_flow/library_nodes/stdlib/free.h"
12
#include "sdfg/data_flow/library_nodes/stdlib/malloc.h"
13
#include "sdfg/data_flow/library_nodes/stdlib/memset.h"
14
#include "sdfg/data_flow/memlet.h"
15
#include "sdfg/data_flow/tasklet.h"
16
#include "sdfg/exceptions.h"
17
#include "sdfg/structured_control_flow/block.h"
18
#include "sdfg/structured_control_flow/map.h"
19
#include "sdfg/structured_control_flow/sequence.h"
20
#include "sdfg/symbolic/symbolic.h"
21
#include "sdfg/types/pointer.h"
22
#include "sdfg/types/scalar.h"
23
#include "sdfg/types/tensor.h"
24
#include "sdfg/types/type.h"
25

26
#include "sdfg/analysis/scope_analysis.h"
27
#include "sdfg/data_flow/library_nodes/math/blas/gemm_node.h"
28
#include "symengine/integer.h"
29
#include "symengine/symengine_rcp.h"
30

31
namespace sdfg {
32
namespace math {
33
namespace tensor {
34

35
ConvNode::ConvNode(
36
    size_t element_id,
37
    const DebugInfo& debug_info,
38
    const graph::Vertex vertex,
39
    data_flow::DataFlowGraph& parent,
40
    const std::vector<symbolic::Expression>& shape,
41
    const std::vector<symbolic::Expression>& kernel_shape,
42
    const std::vector<symbolic::Expression>& strides,
43
    const std::vector<symbolic::Expression>& pads,
44
    const std::vector<symbolic::Expression>& dilations,
45
    symbolic::Expression output_channels,
46
    symbolic::Expression group,
47
    QuantizationType quantization,
48
    const data_flow::ImplementationType& impl_type
49
)
50
    : SpatialTensorNode(
37✔
51
          element_id,
37✔
52
          debug_info,
37✔
53
          vertex,
37✔
54
          parent,
37✔
55
          LibraryNodeType_Conv,
37✔
56
          {},
37✔
57
          {"Y", "X", "W", "B"}, // X and W are required, B (bias) is optional
37✔
58
          impl_type,
37✔
59
          quantization,
37✔
60
          shape,
37✔
61
          kernel_shape,
37✔
62
          strides,
37✔
63
          pads,
37✔
64
          dilations
37✔
65
      ),
37✔
66
      output_channels_(std::move(output_channels)), group_(std::move(group)) {}
37✔
67

68
void ConvNode::validate(const Function& function) const {
73✔
69
    TensorNode::validate(function);
73✔
70

71
    auto& graph = this->get_parent();
73✔
72

73
    // Custom validation for ConvNode that handles optional bias input
74
    // We expect X, W as required inputs and optionally B (bias)
75

76
    // Collect all input edges by connector name
77
    std::map<std::string, const data_flow::Memlet*> input_edges;
73✔
78
    for (auto& iedge : graph.in_edges(*this)) {
219✔
79
        input_edges[iedge.dst_conn()] = &iedge;
219✔
80
    }
219✔
81

82
    // Check that required inputs X and W are present
83
    if (input_edges.find("X") == input_edges.end()) {
73✔
84
        throw InvalidSDFGException("ConvNode: Required input 'X' is not connected");
×
85
    }
×
86
    if (input_edges.find("W") == input_edges.end()) {
73✔
87
        throw InvalidSDFGException("ConvNode: Required input 'W' is not connected");
×
88
    }
×
89

90
    // Validate that parameters are not empty
91
    if (shape_.empty()) {
73✔
92
        throw InvalidSDFGException("ConvNode shape cannot be empty");
×
93
    }
×
94
    if (kernel_shape_.empty()) {
73✔
95
        throw InvalidSDFGException("ConvNode kernel_shape cannot be empty");
×
96
    }
×
97
    if (strides_.empty()) {
73✔
98
        throw InvalidSDFGException("ConvNode strides cannot be empty");
×
99
    }
×
100
    if (pads_.empty()) {
73✔
101
        throw InvalidSDFGException("ConvNode pads cannot be empty");
×
102
    }
×
103
    if (dilations_.empty()) {
73✔
104
        throw InvalidSDFGException("ConvNode dilations cannot be empty");
×
105
    }
×
106

107
    // Validate consistent dimensions
108
    size_t spatial_dims = kernel_shape_.size();
73✔
109

110
    if (shape_.size() != spatial_dims + 2) {
73✔
111
        throw InvalidSDFGException("ConvNode shape must match kernel spatial dimensions + 2");
×
112
    }
×
113

114
    if (strides_.size() != spatial_dims) {
73✔
115
        throw InvalidSDFGException("ConvNode strides must match kernel spatial dimensions");
1✔
116
    }
1✔
117

118
    if (pads_.size() != 2 * spatial_dims) {
72✔
119
        throw InvalidSDFGException("ConvNode pads must have 2 * spatial dimensions (start and end for each axis)");
1✔
120
    }
1✔
121

122
    if (dilations_.size() != spatial_dims) {
71✔
123
        throw InvalidSDFGException("ConvNode dilations must match kernel spatial dimensions");
×
124
    }
×
125

126
    // Validate groups
127
    if (SymEngine::is_a<SymEngine::Integer>(*this->group_)) {
71✔
128
        auto group_int = SymEngine::rcp_static_cast<const SymEngine::Integer>(this->group_)->as_int();
71✔
129
        if (SymEngine::is_a<SymEngine::Integer>(*this->shape_[1])) {
71✔
130
            auto input_channels_int = SymEngine::rcp_static_cast<const SymEngine::Integer>(this->shape_[1])->as_int();
71✔
131
            if (input_channels_int % group_int != 0) {
71✔
132
                throw InvalidSDFGException("ConvNode input channels must be divisible by groups");
×
133
            }
×
134
        }
71✔
135
        if (SymEngine::is_a<SymEngine::Integer>(*this->output_channels_)) {
71✔
136
            auto output_channels_int =
71✔
137
                SymEngine::rcp_static_cast<const SymEngine::Integer>(this->output_channels_)->as_int();
71✔
138
            if (output_channels_int % group_int != 0) {
71✔
139
                throw InvalidSDFGException("ConvNode output channels must be divisible by groups");
×
140
            }
×
141
        }
71✔
142
    }
71✔
143
}
71✔
144

145
blas::BLAS_Precision ConvNode::get_blas_precision(types::Scalar base_type) {
7✔
146
    switch (base_type.primitive_type()) {
7✔
NEW
147
        case types::PrimitiveType::Half:
×
NEW
148
            return blas::BLAS_Precision::h;
×
149
        case types::PrimitiveType::Float:
7✔
150
            return blas::BLAS_Precision::s;
7✔
NEW
151
        case types::PrimitiveType::Double:
×
NEW
152
            return blas::BLAS_Precision::d;
×
NEW
153
        default:
×
NEW
154
            return blas::BLAS_Precision::invalid;
×
155
    }
7✔
156
}
7✔
157

158
symbolic::MultiExpression ConvNode::get_out_shape() {
7✔
159
    size_t dims = kernel_shape_.size();
7✔
160
    symbolic::MultiExpression out_shape;
7✔
161
    out_shape.reserve(dims);
7✔
162
    // out_shape[i] = (shape[i + 2] + pads[i] + pads[dims + i] - dilations[i] * (kernel_shape[i] - 1) - 1)
163
    //                 / strides[i] + 1
164
    for (size_t i = 0; i < dims; i++) {
20✔
165
        out_shape.push_back(symbolic::add(
13✔
166
            symbolic::div(
13✔
167
                symbolic::sub(
13✔
168
                    symbolic::
13✔
169
                        sub(symbolic::add(this->shape_[i + 2], symbolic::add(this->pads_[i], this->pads_[dims + i])),
13✔
170
                            symbolic::mul(this->dilations_[i], symbolic::sub(this->kernel_shape_[i], symbolic::one()))),
13✔
171
                    symbolic::one()
13✔
172
                ),
13✔
173
                this->strides_[i]
13✔
174
            ),
13✔
175
            symbolic::one()
13✔
176
        ));
13✔
177
    }
13✔
178
    return out_shape;
7✔
179
}
7✔
180

NEW
181
bool ConvNode::has_bias() const {
×
NEW
182
    auto* bias_edge = get_parent().in_edge_for_connector(*this, "B");
×
NEW
183
    return bias_edge != nullptr;
×
NEW
184
}
×
185

186
bool ConvNode::check_expandable(
187
    data_flow::DataFlowGraph& dfg, analysis::AnalysisManager& analysis_manager, ConvExpandPrerequisits& boundary
188
) const {
9✔
189
    if ((dfg.nodes().size() != 4 || dfg.edges().size() != 3) && (dfg.nodes().size() != 5 || dfg.edges().size() != 4)) {
9✔
190
        return false;
1✔
191
    }
1✔
192

193
    // Get edges
194
    boundary.iedge_X = dfg.in_edge_for_connector(*this, "X");
8✔
195
    boundary.iedge_W = dfg.in_edge_for_connector(*this, "W");
8✔
196
    boundary.iedge_B = dfg.in_edge_for_connector(*this, "B");
8✔
197
    boundary.iedge_Y = dfg.in_edge_for_connector(*this, "Y");
8✔
198
    if (!boundary.iedge_X || !boundary.iedge_W || !boundary.iedge_Y) {
8✔
199
        return false;
×
200
    }
×
201
    boundary.has_bias = boundary.iedge_B != nullptr;
8✔
202

203
    // Get access nodes
204
    boundary.access_X = dynamic_cast<const data_flow::AccessNode*>(&boundary.iedge_X->src());
8✔
205
    boundary.access_W = dynamic_cast<const data_flow::AccessNode*>(&boundary.iedge_W->src());
8✔
206
    boundary.access_B =
8✔
207
        (boundary.has_bias ? dynamic_cast<const data_flow::AccessNode*>(&boundary.iedge_B->src()) : nullptr);
8✔
208
    boundary.access_Y = dynamic_cast<const data_flow::AccessNode*>(&boundary.iedge_Y->src());
8✔
209
    if (!boundary.access_X || !boundary.access_W || (boundary.has_bias && !boundary.access_B) || !boundary.access_Y) {
8✔
210
        return false;
×
211
    }
×
212

213
    // Get block & its parent
214
    boundary.block = dynamic_cast<structured_control_flow::Block*>(dfg.get_parent());
8✔
215
    if (!boundary.block) {
8✔
216
        return false;
×
217
    }
×
218

219
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
8✔
220
    boundary.block_parent = dynamic_cast<structured_control_flow::Sequence*>(scope_analysis.parent_scope(boundary.block)
8✔
221
    );
8✔
222
    if (!boundary.block_parent) {
8✔
NEW
223
        return false;
×
NEW
224
    }
×
225

226
    boundary.block_index = boundary.block_parent->index(*boundary.block);
8✔
227
    if (boundary.block_index >= boundary.block_parent->size()) {
8✔
228
        return false;
×
229
    }
×
230

231
    return true;
8✔
232
}
8✔
233

234
bool ConvNode::expand(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
5✔
235
    // Validate nodes are standalone in the data flow graph
236
    auto& dfg = this->get_parent();
5✔
237
    ConvExpandPrerequisits b;
5✔
238
    if (!check_expandable(dfg, analysis_manager, b)) {
5✔
239
        return false;
×
240
    }
×
241

242
    // Determine BLAS precision
243

244
    types::Scalar base_type(this->primitive_type(dfg));
5✔
245
    blas::BLAS_Precision precision = get_blas_precision(base_type);
5✔
246
    if (precision == blas::BLAS_Precision::invalid) {
5✔
NEW
247
        return false;
×
UNCOV
248
    }
×
249

250
    // Create new sequence for expansion
251
    auto& new_sequence = builder.add_sequence_before(
5✔
252
        *b.block_parent, *b.block, b.block_parent->at(b.block_index).second.assignments(), b.block->debug_info()
5✔
253
    );
5✔
254

255
    // Dimensions, i.e., 1D, 2D, 3D, ...
256
    size_t dims = this->kernel_shape_.size();
5✔
257
    symbolic::MultiExpression out_shape = get_out_shape();
5✔
258
    types::Scalar indvar_type(types::PrimitiveType::Int64);
5✔
259

260
    auto in_channels = symbolic::div(this->shape_[1], this->group_);
5✔
261
    auto out_channels = symbolic::div(this->output_channels_, this->group_);
5✔
262

263
    // Add loop over batch size
264
    auto n_container = builder.find_new_name("_n");
5✔
265
    builder.add_container(n_container, indvar_type);
5✔
266
    auto n = symbolic::symbol(n_container);
5✔
267
    auto& loop_n = builder.add_map(
5✔
268
        new_sequence,
5✔
269
        n,
5✔
270
        symbolic::Lt(n, this->shape_[0]),
5✔
271
        symbolic::zero(),
5✔
272
        symbolic::add(n, symbolic::one()),
5✔
273
        ScheduleType_Sequential::create(),
5✔
274
        {},
5✔
275
        b.block->debug_info()
5✔
276
    );
5✔
277

278
    // Add loop over groups
279
    auto g_container = builder.find_new_name("_g");
5✔
280
    builder.add_container(g_container, indvar_type);
5✔
281
    auto g = symbolic::symbol(g_container);
5✔
282
    auto& loop_g = builder.add_map(
5✔
283
        loop_n.root(),
5✔
284
        g,
5✔
285
        symbolic::Lt(g, this->group_),
5✔
286
        symbolic::zero(),
5✔
287
        symbolic::add(g, symbolic::one()),
5✔
288
        ScheduleType_Sequential::create(),
5✔
289
        {},
5✔
290
        b.block->debug_info()
5✔
291
    );
5✔
292

293
    // Add patches container with malloc
294
    symbolic::Expression patches_size = in_channels;
5✔
295
    for (size_t i = 0; i < dims; i++) {
15✔
296
        patches_size = symbolic::mul(patches_size, symbolic::mul(this->kernel_shape_[i], out_shape[i]));
10✔
297
    }
10✔
298
    types::Pointer patches_type(base_type);
5✔
299
    auto patches_container = builder.find_new_name("_patches");
5✔
300
    builder.add_container(patches_container, patches_type);
5✔
301
    auto [patches_malloc_block, patches_malloc_node] = stdlib::add_malloc_block(
5✔
302
        builder,
5✔
303
        loop_g.root(),
5✔
304
        patches_container,
5✔
305
        symbolic::mul(patches_size, symbolic::size_of_type(base_type)),
5✔
306
        patches_type,
5✔
307
        this->debug_info()
5✔
308
    );
5✔
309

310
    // Add loop over channels
311
    structured_control_flow::Sequence* current_seq = &loop_g.root();
5✔
312
    auto c_container = builder.find_new_name("_c");
5✔
313
    builder.add_container(c_container, indvar_type);
5✔
314
    auto c = symbolic::symbol(c_container);
5✔
315
    auto& loop_c = builder.add_map(
5✔
316
        *current_seq,
5✔
317
        c,
5✔
318
        symbolic::Lt(c, in_channels),
5✔
319
        symbolic::zero(),
5✔
320
        symbolic::add(c, symbolic::one()),
5✔
321
        ScheduleType_Sequential::create(),
5✔
322
        {},
5✔
323
        b.block->debug_info()
5✔
324
    );
5✔
325
    current_seq = &loop_c.root();
5✔
326

327
    // Add loops over kernel shape
328
    symbolic::SymbolVec ks;
5✔
329
    ks.reserve(dims);
5✔
330
    for (size_t i = 0; i < dims; i++) {
15✔
331
        auto k_container = builder.find_new_name("_k");
10✔
332
        builder.add_container(k_container, indvar_type);
10✔
333
        auto k = symbolic::symbol(k_container);
10✔
334
        ks.push_back(k);
10✔
335
        auto& loop_k = builder.add_map(
10✔
336
            *current_seq,
10✔
337
            k,
10✔
338
            symbolic::Lt(k, this->kernel_shape_[i]),
10✔
339
            symbolic::zero(),
10✔
340
            symbolic::add(k, symbolic::one()),
10✔
341
            ScheduleType_Sequential::create(),
10✔
342
            {},
10✔
343
            b.block->debug_info()
10✔
344
        );
10✔
345
        current_seq = &loop_k.root();
10✔
346
    }
10✔
347

348
    // Add loops over output dimensions
349
    symbolic::SymbolVec os;
5✔
350
    os.reserve(dims);
5✔
351
    for (size_t i = 0; i < dims; i++) {
15✔
352
        auto o_container = builder.find_new_name("_o");
10✔
353
        builder.add_container(o_container, indvar_type);
10✔
354
        auto o = symbolic::symbol(o_container);
10✔
355
        os.push_back(o);
10✔
356
        auto& loop_o = builder.add_map(
10✔
357
            *current_seq,
10✔
358
            o,
10✔
359
            symbolic::Lt(o, out_shape[i]),
10✔
360
            symbolic::zero(),
10✔
361
            symbolic::add(o, symbolic::one()),
10✔
362
            ScheduleType_Sequential::create(),
10✔
363
            {},
10✔
364
            b.block->debug_info()
10✔
365
        );
10✔
366
        current_seq = &loop_o.root();
10✔
367
    }
10✔
368

369
    // Add if/else to stay in bounds for copying
370
    symbolic::MultiExpression is;
5✔
371
    is.reserve(dims);
5✔
372
    symbolic::Condition copy_condition = symbolic::__true__();
5✔
373
    symbolic::Condition zero_condition = symbolic::__false__();
5✔
374
    for (size_t i = 0; i < dims; i++) {
15✔
375
        auto i_expr = symbolic::
10✔
376
            add(symbolic::sub(symbolic::mul(os[i], this->strides_[i]), this->pads_[i]),
10✔
377
                symbolic::mul(ks[i], this->dilations_[i]));
10✔
378
        is.push_back(i_expr);
10✔
379
        copy_condition = symbolic::
10✔
380
            And(copy_condition,
10✔
381
                symbolic::And(symbolic::Lt(i_expr, this->shape_[i + 2]), symbolic::Ge(i_expr, symbolic::zero())));
10✔
382
        zero_condition = symbolic::
10✔
383
            Or(zero_condition,
10✔
384
               symbolic::Or(symbolic::Ge(i_expr, this->shape_[i + 2]), symbolic::Lt(i_expr, symbolic::zero())));
10✔
385
    }
10✔
386
    auto& branch = builder.add_if_else(*current_seq, {}, b.block->debug_info());
5✔
387
    auto& copy_case = builder.add_case(branch, copy_condition, b.block->debug_info());
5✔
388
    auto& zero_case = builder.add_case(branch, zero_condition, b.block->debug_info());
5✔
389

390
    // Determine patches subset & tensor type
391
    data_flow::Subset patches_subset;
5✔
392
    patches_subset.push_back(c);
5✔
393
    patches_subset.insert(patches_subset.end(), ks.begin(), ks.end());
5✔
394
    patches_subset.insert(patches_subset.end(), os.begin(), os.end());
5✔
395
    symbolic::MultiExpression patches_shape;
5✔
396
    patches_shape.push_back(in_channels);
5✔
397
    patches_shape.insert(patches_shape.end(), this->kernel_shape_.begin(), this->kernel_shape_.end());
5✔
398
    patches_shape.insert(patches_shape.end(), out_shape.begin(), out_shape.end());
5✔
399
    types::Tensor patches_tensor_type(base_type, patches_shape);
5✔
400

401
    // Determine subset for X
402
    data_flow::Subset subset_X;
5✔
403
    subset_X.push_back(n);
5✔
404
    subset_X.push_back(symbolic::add(symbolic::mul(in_channels, g), c));
5✔
405
    subset_X.insert(subset_X.end(), is.begin(), is.end());
5✔
406

407
    // Add copy from X to patches
408
    auto& copy_block = builder.add_block(copy_case, {}, b.block->debug_info());
5✔
409
    {
5✔
410
        auto& X_access = builder.add_access(copy_block, b.access_X->data(), b.access_X->debug_info());
5✔
411
        auto& patches_access = builder.add_access(copy_block, patches_container, this->debug_info());
5✔
412
        auto& tasklet =
5✔
413
            builder.add_tasklet(copy_block, data_flow::TaskletCode::assign, "_out", {"_in"}, this->debug_info());
5✔
414
        builder.add_computational_memlet(
5✔
415
            copy_block, X_access, tasklet, "_in", subset_X, b.iedge_X->base_type(), b.iedge_X->debug_info()
5✔
416
        );
5✔
417
        builder.add_computational_memlet(
5✔
418
            copy_block, tasklet, "_out", patches_access, patches_subset, patches_tensor_type, this->debug_info()
5✔
419
        );
5✔
420
    }
5✔
421

422
    // Add zero assignment to patches
423
    auto& zero_block = builder.add_block(zero_case, {}, b.block->debug_info());
5✔
424
    {
5✔
425
        auto& constant_zero = builder.add_constant(zero_block, "0.0", base_type, this->debug_info());
5✔
426
        auto& patches_access = builder.add_access(zero_block, patches_container, this->debug_info());
5✔
427
        auto& tasklet =
5✔
428
            builder.add_tasklet(zero_block, data_flow::TaskletCode::assign, "_out", {"_in"}, this->debug_info());
5✔
429
        builder.add_computational_memlet(zero_block, constant_zero, tasklet, "_in", {}, base_type, this->debug_info());
5✔
430
        builder.add_computational_memlet(
5✔
431
            zero_block, tasklet, "_out", patches_access, patches_subset, patches_tensor_type, this->debug_info()
5✔
432
        );
5✔
433
    }
5✔
434

435
    // Add reference to W
436
    auto ref_W_container = builder.find_new_name("_ref_W");
5✔
437
    types::Scalar ref_W_base_type(builder.subject().type(b.access_W->data()).primitive_type());
5✔
438
    types::Pointer ref_W_type(ref_W_base_type);
5✔
439
    builder.add_container(ref_W_container, ref_W_type);
5✔
440
    auto ref_W_subset = symbolic::mul(symbolic::mul(out_channels, g), in_channels);
5✔
441
    for (size_t i = 0; i < dims; i++) {
15✔
442
        ref_W_subset = symbolic::mul(ref_W_subset, this->kernel_shape_[i]);
10✔
443
    }
10✔
444
    auto& ref_W_block = builder.add_block(loop_g.root(), {}, b.block->debug_info());
5✔
445
    {
5✔
446
        auto& W_access = builder.add_access(ref_W_block, b.access_W->data(), b.access_W->debug_info());
5✔
447
        auto& ref_W_access = builder.add_access(ref_W_block, ref_W_container, b.access_W->debug_info());
5✔
448
        builder.add_reference_memlet(ref_W_block, W_access, ref_W_access, {ref_W_subset}, ref_W_type);
5✔
449
    }
5✔
450

451
    // Add reference to Y
452
    auto ref_Y_container = builder.find_new_name("_ref_Y");
5✔
453
    types::Scalar ref_Y_base_type(builder.subject().type(b.access_Y->data()).primitive_type());
5✔
454
    types::Pointer ref_Y_type(ref_Y_base_type);
5✔
455
    builder.add_container(ref_Y_container, ref_Y_type);
5✔
456
    auto ref_Y_subset = symbolic::add(symbolic::mul(this->output_channels_, n), symbolic::mul(out_channels, g));
5✔
457
    for (size_t i = 0; i < dims; i++) {
15✔
458
        ref_Y_subset = symbolic::mul(ref_Y_subset, out_shape[i]);
10✔
459
    }
10✔
460
    auto& ref_Y_block = builder.add_block(loop_g.root(), {}, b.block->debug_info());
5✔
461
    {
5✔
462
        auto& Y_access = builder.add_access(ref_Y_block, b.access_Y->data(), b.access_Y->debug_info());
5✔
463
        auto& ref_Y_access = builder.add_access(ref_Y_block, ref_Y_container, b.access_Y->debug_info());
5✔
464
        builder.add_reference_memlet(ref_Y_block, Y_access, ref_Y_access, {ref_Y_subset}, ref_Y_type);
5✔
465
    }
5✔
466

467
    // Add GEMM node
468
    auto& gemm_block = builder.add_block(loop_g.root(), {}, b.block->debug_info());
5✔
469
    {
5✔
470
        auto& alpha = builder.add_constant(gemm_block, "1.0", base_type, this->debug_info());
5✔
471
        auto& beta = builder.add_constant(gemm_block, "0.0", base_type, this->debug_info());
5✔
472
        auto& ref_W_access = builder.add_access(gemm_block, ref_W_container, b.access_W->debug_info());
5✔
473
        auto& patches_access = builder.add_access(gemm_block, patches_container, this->debug_info());
5✔
474
        auto& ref_Y_access_in = builder.add_access(gemm_block, ref_Y_container, b.access_Y->debug_info());
5✔
475
        symbolic::Expression gemm_m = out_channels;
5✔
476
        symbolic::Expression gemm_n = symbolic::one();
5✔
477
        symbolic::Expression gemm_k = in_channels;
5✔
478
        for (size_t i = 0; i < dims; i++) {
15✔
479
            gemm_n = symbolic::mul(gemm_n, out_shape[i]);
10✔
480
            gemm_k = symbolic::mul(gemm_k, this->kernel_shape_[i]);
10✔
481
        }
10✔
482
        auto& libnode = builder.add_library_node<blas::GEMMNode>(
5✔
483
            gemm_block,
5✔
484
            this->debug_info(),
5✔
485
            blas::ImplementationType_BLAS,
5✔
486
            precision, // precision
5✔
487
            blas::BLAS_Layout::RowMajor, // layout
5✔
488
            blas::BLAS_Transpose::No, // transA
5✔
489
            blas::BLAS_Transpose::No, // transB
5✔
490
            gemm_m, // m
5✔
491
            gemm_n, // n
5✔
492
            gemm_k, // k
5✔
493
            gemm_k, // lda
5✔
494
            gemm_n, // ldb
5✔
495
            gemm_n // ldc
5✔
496
        );
5✔
497
        builder.add_computational_memlet(gemm_block, alpha, libnode, "__alpha", {}, base_type, this->debug_info());
5✔
498
        builder.add_computational_memlet(gemm_block, beta, libnode, "__beta", {}, base_type, this->debug_info());
5✔
499
        builder
5✔
500
            .add_computational_memlet(gemm_block, ref_W_access, libnode, "__A", {}, ref_W_type, b.iedge_W->debug_info());
5✔
501
        builder
5✔
502
            .add_computational_memlet(gemm_block, patches_access, libnode, "__B", {}, patches_type, this->debug_info());
5✔
503
        builder.add_computational_memlet(
5✔
504
            gemm_block, ref_Y_access_in, libnode, "__C", {}, ref_Y_type, b.iedge_Y->debug_info()
5✔
505
        );
5✔
506
    }
5✔
507

508
    // Add bias if available
509
    if (b.has_bias) {
5✔
510
        // Add loop over output channels
511
        auto l_container = builder.find_new_name("_l");
×
512
        builder.add_container(l_container, indvar_type);
×
513
        auto l = symbolic::symbol(l_container);
×
514
        auto& loop_l = builder.add_map(
×
515
            loop_g.root(),
×
516
            l,
×
517
            symbolic::Lt(l, out_channels),
×
518
            symbolic::zero(),
×
519
            symbolic::add(l, symbolic::one()),
×
520
            ScheduleType_Sequential::create(),
×
521
            {},
×
NEW
522
            b.block->debug_info()
×
523
        );
×
524
        current_seq = &loop_l.root();
×
525

526
        // Add loops over output dimensions (again)
527
        for (size_t i = 0; i < dims; i++) {
×
528
            auto o_container = builder.find_new_name("_o");
×
529
            builder.add_container(o_container, indvar_type);
×
530
            auto o = symbolic::symbol(o_container);
×
531
            auto& loop_o = builder.add_map(
×
532
                *current_seq,
×
533
                o,
×
534
                symbolic::Lt(o, out_shape[i]),
×
535
                symbolic::zero(),
×
536
                symbolic::add(o, symbolic::one()),
×
537
                ScheduleType_Sequential::create(),
×
538
                {},
×
NEW
539
                b.block->debug_info()
×
540
            );
×
541
            current_seq = &loop_o.root();
×
542
            os[i] = o;
×
543
        }
×
544

545
        // Add bias to Y
546
        data_flow::Subset Y_subset;
×
547
        Y_subset.push_back(n);
×
548
        Y_subset.push_back(symbolic::add(symbolic::mul(out_channels, g), l));
×
549
        Y_subset.insert(Y_subset.end(), os.begin(), os.end());
×
550
        auto B_subset = symbolic::add(symbolic::mul(out_channels, g), l);
×
NEW
551
        auto& bias_block = builder.add_block(*current_seq, {}, b.block->debug_info());
×
552
        {
×
NEW
553
            auto& B_access = builder.add_access(bias_block, b.access_B->data(), b.access_B->debug_info());
×
NEW
554
            auto& Y_access_in = builder.add_access(bias_block, b.access_Y->data(), b.access_Y->debug_info());
×
NEW
555
            auto& Y_access_out = builder.add_access(bias_block, b.access_Y->data(), b.access_Y->debug_info());
×
556
            auto& tasklet =
×
557
                builder
×
558
                    .add_tasklet(bias_block, data_flow::TaskletCode::fp_add, "_out", {"_in1", "_in2"}, this->debug_info());
×
559
            builder.add_computational_memlet(
×
NEW
560
                bias_block, Y_access_in, tasklet, "_in1", Y_subset, b.iedge_Y->base_type(), this->debug_info()
×
561
            );
×
562
            builder.add_computational_memlet(
×
NEW
563
                bias_block, B_access, tasklet, "_in2", {B_subset}, b.iedge_B->base_type(), b.iedge_B->debug_info()
×
564
            );
×
565
            builder.add_computational_memlet(
×
NEW
566
                bias_block, tasklet, "_out", Y_access_out, Y_subset, b.iedge_Y->base_type(), b.iedge_Y->debug_info()
×
567
            );
×
568
        }
×
569
    }
×
570

571
    // Add free for patches container
572
    auto& patches_free_block = builder.add_block(loop_g.root(), {}, b.block->debug_info());
5✔
573
    {
5✔
574
        auto& patches_access_in = builder.add_access(patches_free_block, patches_container, this->debug_info());
5✔
575
        auto& libnode = builder.add_library_node<stdlib::FreeNode>(patches_free_block, this->debug_info());
5✔
576
        builder.add_computational_memlet(
5✔
577
            patches_free_block, patches_access_in, libnode, "_ptr", {}, patches_type, this->debug_info()
5✔
578
        );
5✔
579
    }
5✔
580

581
    // Clean up the original block
582
    builder.clear_code_node_legacy(*b.block, *this);
5✔
583
    // WARNING: this has been deallocated at this point!!
584
    builder.remove_child(*b.block_parent, b.block_index + 1);
5✔
585

586
    return true;
5✔
587
}
5✔
588

589
symbolic::SymbolSet ConvNode::symbols() const {
13✔
590
    auto syms = SpatialTensorNode::symbols();
13✔
591
    for (auto& atom : symbolic::atoms(output_channels_)) {
13✔
592
        syms.insert(atom);
×
593
    }
×
594
    for (auto& atom : symbolic::atoms(group_)) {
13✔
595
        syms.insert(atom);
×
596
    }
×
597

598
    return syms;
13✔
599
}
13✔
600

601
void ConvNode::replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) {
×
NEW
602
    SpatialTensorNode::replace(old_expression, new_expression);
×
603
    output_channels_ = symbolic::subs(output_channels_, old_expression, new_expression);
×
604
    group_ = symbolic::subs(group_, old_expression, new_expression);
×
605
}
×
606

607
std::unique_ptr<data_flow::DataFlowNode> ConvNode::
608
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph& parent) const {
1✔
609
    return std::unique_ptr<data_flow::DataFlowNode>(new ConvNode(
1✔
610
        element_id,
1✔
611
        this->debug_info(),
1✔
612
        vertex,
1✔
613
        parent,
1✔
614
        shape_,
1✔
615
        kernel_shape_,
1✔
616
        strides_,
1✔
617
        pads_,
1✔
618
        dilations_,
1✔
619
        output_channels_,
1✔
620
        group_,
1✔
621
        fixed_quantization_,
1✔
622
        implementation_type_
1✔
623
    ));
1✔
624
}
1✔
625

626
std::string ConvNode::toStr() const {
×
627
    std::stringstream result;
×
NEW
628
    result << "Conv(";
×
NEW
629
    SpatialTensorNode::operator<<(result);
×
630

NEW
631
    result << ", output_channels=" + output_channels_->__str__();
×
632
    result << ", group=" + group_->__str__() + ")";
×
633
    return result.str();
×
634
}
×
635

NEW
636
symbolic::Expression ConvNode::flop() const {
×
637
    // Total FLOPs = output_elements * K_conv (multiplications)
638
    //             + output_elements * (K_conv - 1) (additions)
NEW
639
    auto output_elems = num_output_elements();
×
NEW
640
    auto k_conv = kernel_iteration_count();
×
641

NEW
642
    auto mul_ops = symbolic::mul(output_elems, k_conv);
×
NEW
643
    auto add_ops = symbolic::mul(output_elems, symbolic::sub(k_conv, symbolic::one()));
×
NEW
644
    return symbolic::add(mul_ops, add_ops);
×
NEW
645
}
×
646

NEW
647
data_flow::PointerAccessType ConvNode::pointer_access_type(int input_idx) const {
×
NEW
648
    if (input_idx == 0) {
×
NEW
649
        return data_flow::PointerAccessMeta::create_full_write_only(symbolic::__nullptr__(), true);
×
NEW
650
    } else if (input_idx >= 1 && input_idx < inputs_.size()) {
×
NEW
651
        return data_flow::PointerAccessMeta::create_read_only(symbolic::__nullptr__(), true);
×
NEW
652
    } else {
×
NEW
653
        return TensorNode::pointer_access_type(input_idx);
×
UNCOV
654
    }
×
NEW
655
}
×
656

NEW
657
symbolic::Expression ConvNode::num_output_elements() const {
×
658
    // N * C_out * prod(output_spatial_dim(i))
NEW
659
    return symbolic::mul(symbolic::mul(shape_[0], output_channels_), output_spatial_volume());
×
NEW
660
}
×
661

NEW
662
symbolic::Expression ConvNode::kernel_iteration_count() const {
×
663
    // (C_in / group) * prod(kernel_shape_[i])
NEW
664
    return symbolic::mul(symbolic::div(shape_[1], group_), kernel_volume());
×
NEW
665
}
×
666

NEW
667
nlohmann::json ConvNodeSerializer::serialize(const data_flow::LibraryNode& library_node) {
×
NEW
668
    const ConvNode& conv_node = static_cast<const ConvNode&>(library_node);
×
NEW
669
    nlohmann::json j;
×
670

NEW
671
    serializer::JSONSerializer serializer;
×
672
    j["output_channels"] = serializer.expression(conv_node.output_channels());
×
673
    j["group"] = serializer.expression(conv_node.group());
×
674

NEW
675
    fill_base_values(conv_node, j);
×
676

677
    return j;
×
678
}
×
679

680
data_flow::LibraryNode& ConvNodeSerializer::deserialize(
681
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
682
) {
×
683
    assert(j.contains("kernel_shape"));
×
684

NEW
685
    auto base = deserialize_base_values(j);
×
686

687
    symbolic::Expression output_channels = symbolic::one();
×
688
    if (j.contains("output_channels")) {
×
689
        output_channels = symbolic::parse(j["output_channels"].get<std::string>());
×
690
    }
×
691

692
    symbolic::Expression group = symbolic::one();
×
693
    if (j.contains("group")) {
×
694
        group = symbolic::parse(j["group"].get<std::string>());
×
695
    }
×
696

NEW
697
    return builder.add_library_node<ConvNode>(
×
NEW
698
        parent,
×
NEW
699
        base.debug_info,
×
NEW
700
        base.shape,
×
NEW
701
        base.kernel_shape,
×
NEW
702
        base.strides,
×
NEW
703
        base.pads,
×
NEW
704
        base.dilations,
×
NEW
705
        output_channels,
×
NEW
706
        group,
×
NEW
707
        base.quantization
×
NEW
708
    );
×
UNCOV
709
}
×
710

711
} // namespace tensor
712
} // namespace math
713
} // namespace sdfg
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc