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

daisytuner / docc / 26809042868

02 Jun 2026 08:49AM UTC coverage: 60.831% (-0.04%) from 60.869%
26809042868

Pull #725

github

web-flow
Merge eceff48b9 into cd25c9278
Pull Request #725: Tensor node backport

599 of 1255 new or added lines in 51 files covered. (47.73%)

538 existing lines in 45 files now uncovered.

35099 of 57699 relevant lines covered (60.83%)

11080.96 hits per line

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

46.39
/sdfg/src/data_flow/library_nodes/math/tensor/matmul_node.cpp
1
#include "sdfg/data_flow/library_nodes/math/tensor/matmul_node.h"
2
#include <cstddef>
3
#include <string>
4

5
#include "sdfg/analysis/scope_analysis.h"
6
#include "sdfg/builder/structured_sdfg_builder.h"
7
#include "sdfg/data_flow/library_nodes/math/blas/blas_node.h"
8
#include "sdfg/data_flow/library_nodes/math/blas/gemm_node.h"
9
#include "sdfg/data_flow/library_nodes/stdlib/free.h"
10
#include "sdfg/data_flow/tasklet.h"
11
#include "sdfg/element.h"
12
#include "sdfg/exceptions.h"
13
#include "sdfg/structured_control_flow/control_flow_node.h"
14
#include "sdfg/structured_control_flow/map.h"
15
#include "sdfg/structured_control_flow/sequence.h"
16
#include "sdfg/symbolic/symbolic.h"
17
#include "sdfg/types/pointer.h"
18
#include "sdfg/types/scalar.h"
19
#include "sdfg/types/tensor.h"
20
#include "sdfg/types/type.h"
21
#include "sdfg/types/utils.h"
22

23
namespace sdfg {
24
namespace math {
25
namespace tensor {
26

27
bool MatMulNode::has_basic_strides(symbolic::MultiExpression shape, symbolic::MultiExpression strides) {
×
28
    auto basic_strides = types::Tensor::strides_from_shape(shape);
×
29
    if (basic_strides.size() != strides.size()) {
×
30
        return false;
×
31
    }
×
32
    for (size_t i = 0; i < strides.size(); i++) {
×
33
        if (!symbolic::eq(basic_strides[i], strides[i])) {
×
34
            return false;
×
35
        }
×
36
    }
×
37
    return true;
×
38
}
×
39

40
bool MatMulNode::has_transposed_strides(symbolic::MultiExpression shape, symbolic::MultiExpression strides) {
×
41
    if (shape.size() < 2) {
×
42
        return false;
×
43
    }
×
44
    symbolic::MultiExpression new_shape;
×
45
    new_shape.reserve(shape.size());
×
46
    for (size_t i = 0; i < shape.size() - 2; i++) {
×
47
        new_shape.push_back(shape[i]);
×
48
    }
×
49
    new_shape.push_back(shape[shape.size() - 1]);
×
50
    new_shape.push_back(shape[shape.size() - 2]);
×
51
    symbolic::MultiExpression transposed_strides(strides);
×
52
    transposed_strides[strides.size() - 2] = strides[strides.size() - 1];
×
53
    transposed_strides[strides.size() - 1] = strides[strides.size() - 2];
×
54
    return MatMulNode::has_basic_strides(new_shape, transposed_strides);
×
55
}
×
56

57
MatMulNode::MatMulNode(
58
    size_t element_id,
59
    const DebugInfo& debug_info,
60
    const graph::Vertex vertex,
61
    data_flow::DataFlowGraph& parent,
62
    const TensorLayout& layout_a,
63
    const TensorLayout& layout_b,
64
    QuantizationType quantization,
65
    const data_flow::ImplementationType& impl_type
66
)
67
    : TensorNode(element_id, debug_info, vertex, parent, LibraryNodeType_MatMul, {}, {"Y", "A", "B"}, impl_type),
5✔
68
      fixed_quantization_(quantization), layout_a_(layout_a), layout_b_(layout_b) {
5✔
69
    if (layout_a.dims() < 2) {
5✔
70
        throw std::invalid_argument("MatMulNode: Input A must have at least 2 dimensions");
×
71
    }
×
72
    if (layout_b.dims() < 2) {
5✔
73
        throw std::invalid_argument("MatMulNode: Input B must have at least 2 dimensions");
×
74
    }
×
75
}
5✔
76

77
symbolic::Expression MatMulNode::m() const {
8✔
78
    // M is the second-to-last dimension of A
79
    return layout_a_.get_dim_innermost(1);
8✔
80
}
8✔
81

82
symbolic::Expression MatMulNode::n() const {
12✔
83
    // N is the last dimension of B
84
    return layout_b_.get_dim_innermost(0);
12✔
85
}
12✔
86

87
symbolic::Expression MatMulNode::k() const {
7✔
88
    // K is the last dimension of A (and second-to-last of B)
89
    return layout_a_.get_dim_innermost(0);
7✔
90
}
7✔
91

92
const TensorLayout& MatMulNode::layout_a() const { return layout_a_; }
×
93

94
const TensorLayout& MatMulNode::layout_b() const { return layout_b_; }
×
95

96
void MatMulNode::validate(const Function& function) const {
5✔
97
    TensorNode::validate(function);
5✔
98

99
    auto& graph = this->get_parent();
5✔
100

101
    // Check that we have exactly 2 inputs and 1 output
102
    if (graph.in_degree(*this) != 3) {
5✔
103
        throw InvalidSDFGException("MatMulNode: Expected exactly 3 inputs (Y, A, B)");
×
104
    }
×
105
    if (graph.out_degree(*this) != 0) {
5✔
106
        throw InvalidSDFGException("MatMulNode: Expected no outputs");
×
107
    }
×
108

109
    // Validate K dimension matches between A and B
110
    auto k_a = layout_a_.get_dim_innermost(0);
5✔
111
    auto k_b = layout_b_.get_dim_innermost(1);
5✔
112
    if (!symbolic::eq(k_a, k_b)) {
5✔
113
        throw InvalidSDFGException(
×
114
            "MatMulNode: K dimension mismatch. A has K=" + k_a->__str__() + ", B has K=" + k_b->__str__()
×
115
        );
×
116
    }
×
117
}
5✔
118

119
symbolic::SymbolSet MatMulNode::symbols() const {
1✔
120
    symbolic::SymbolSet syms;
1✔
121
    layout_a_.collect_symbols(syms);
1✔
122
    layout_b_.collect_symbols(syms);
1✔
123
    return syms;
1✔
124
}
1✔
125

126
void MatMulNode::replace(const symbolic::Expression old_expression, const symbolic::Expression new_expression) {
×
127
    layout_a_.replace_symbols(old_expression, new_expression);
×
128
    layout_b_.replace_symbols(old_expression, new_expression);
×
129
}
×
130

131
std::unique_ptr<data_flow::DataFlowNode> MatMulNode::
132
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph& parent) const {
×
NEW
133
    return std::unique_ptr<data_flow::DataFlowNode>(new MatMulNode(
×
NEW
134
        element_id, debug_info(), vertex, parent, layout_a_, layout_b_, fixed_quantization_, implementation_type_
×
NEW
135
    ));
×
UNCOV
136
}
×
137

138
types::PrimitiveType MatMulNode::fixed_quantization() const { return fixed_quantization_; }
×
139

140
types::PrimitiveType MatMulNode::quantization(const data_flow::DataFlowGraph& data_flow_graph) const {
×
141
    if (fixed_quantization_ != QUANTIZATION_MATCH_INPUTS) {
×
142
        return fixed_quantization_;
×
143
    } else {
×
144
        return this->primitive_type(data_flow_graph);
×
145
    }
×
146
}
×
147

148
std::optional<types::PrimitiveType> MatMulNode::uniform_quantization(const data_flow::DataFlowGraph& data_flow_graph
149
) const {
5✔
150
    if (fixed_quantization_ != QUANTIZATION_MATCH_INPUTS) {
5✔
151
        auto inferred = this->primitive_type(data_flow_graph);
×
152
        if (inferred == fixed_quantization_) {
×
153
            return fixed_quantization_;
×
154
        } else {
×
155
            return std::nullopt;
×
156
        }
×
157
    } else {
5✔
158
        return this->primitive_type(data_flow_graph);
5✔
159
    }
5✔
160
}
5✔
161

162
std::string MatMulNode::toStr() const {
×
163
    std::stringstream ss;
×
164
    ss << "MatMul(";
×
165
    ss << types::primitive_type_to_string(fixed_quantization_) << ", ";
×
166
    ss << "A: " << layout_a_;
×
167
    ss << ", B: " << layout_b_;
×
168
    ss << ")";
×
169
    return ss.str();
×
170
}
×
171

172
symbolic::Expression MatMulNode::flop() const {
×
173
    auto res_elems = symbolic::mul(this->m(), this->n());
×
174
    auto k = this->k();
×
175

176
    auto mm_mul_ops = symbolic::mul(res_elems, k);
×
177
    auto mm_sum_ops = symbolic::mul(res_elems, symbolic::sub(k, symbolic::one()));
×
178

179
    auto mul_ops = mm_mul_ops;
×
180
    auto add_ops = mm_sum_ops;
×
181
    auto per_mat = symbolic::add(mul_ops, add_ops);
×
182
    int a_dims = layout_a_.dims();
×
183
    int b_dims = layout_b_.dims();
×
184
    if (a_dims > 2 || b_dims > 2) {
×
185
        std::vector<symbolic::Expression> factors{per_mat};
×
186
        auto max_dims = std::max(a_dims, b_dims);
×
187
        for (int i = 2; i < max_dims; ++i) {
×
188
            symbolic::Expression dim_a, dim_b;
×
189
            if (i < a_dims) {
×
190
                dim_a = layout_a_.get_dim_innermost(i);
×
191
            }
×
192
            if (i < b_dims) {
×
193
                dim_b = layout_b_.get_dim_innermost(i);
×
194
            }
×
195
            if (dim_a.is_null() & !dim_b.is_null()) {
×
196
                factors.push_back(dim_b);
×
197
            } else if (!dim_a.is_null() & dim_b.is_null()) {
×
198
                factors.push_back(dim_a);
×
199
            } else if (!dim_a.is_null() & !dim_b.is_null()) {
×
200
                if (!symbolic::eq(dim_a, dim_b)) {
×
201
                    throw InvalidSDFGException(
×
202
                        "Batch dimension " + std::to_string(i) + " mismatch between A and B. A has " +
×
203
                        dim_a->__str__() + ", B has " + dim_b->__str__()
×
204
                    );
×
205
                } else {
×
206
                    factors.push_back(dim_a);
×
207
                }
×
208
            } else {
×
209
                return SymEngine::null;
×
210
            }
×
211
        }
×
212
        return SymEngine::mul(factors);
×
213
    } else {
×
214
        return per_mat;
×
215
    }
×
216
}
×
217

218
void free_after_copy(
219
    const std::string& copy_name, builder::StructuredSDFGBuilder& builder, structured_control_flow::Sequence& parent
220
) {
×
221
    auto& block = builder.add_block(parent, {}, DebugInfo());
×
222
    auto& access_in = builder.add_access(block, copy_name);
×
223
    auto& free_node = builder.add_library_node<stdlib::FreeNode>(block, DebugInfo());
×
224
    builder.add_computational_memlet(
×
225
        block, access_in, free_node, "_ptr", {}, types::Pointer(types::Scalar(types::PrimitiveType::Void))
×
226
    );
×
227
}
×
228

229
bool MatMulNode::expand(builder::StructuredSDFGBuilder& builder, analysis::AnalysisManager& analysis_manager) {
5✔
230
    auto& dataflow = this->get_parent();
5✔
231
    auto& block = static_cast<structured_control_flow::Block&>(*dataflow.get_parent());
5✔
232

233
    if (dataflow.in_degree(*this) != 3 || dataflow.out_degree(*this) != 0) {
5✔
234
        return false;
×
235
    }
×
236

237
    auto& scope_analysis = analysis_manager.get<analysis::ScopeAnalysis>();
5✔
238
    auto& parent = static_cast<structured_control_flow::Sequence&>(*scope_analysis.parent_scope(&block));
5✔
239
    int index = parent.index(block);
5✔
240
    auto& transition = parent.at(index).second;
5✔
241

242
    // Get input and output edges
243
    auto iedges = dataflow.in_edges_by_connector(*this);
5✔
244
    if (iedges.size() != 3) {
5✔
245
        return false;
×
246
    }
×
247
    auto* iedge_y = iedges.at(Y_INPUT_IDX);
5✔
248
    auto* iedge_a = iedges.at(A_INPUT_IDX);
5✔
249
    auto* iedge_b = iedges.at(B_INPUT_IDX);
5✔
250

251
    // Check if legal - access nodes must not have other connections
252
    auto& input_node_a = static_cast<data_flow::AccessNode&>(iedge_a->src());
5✔
253
    auto& input_node_b = static_cast<data_flow::AccessNode&>(iedge_b->src());
5✔
254
    auto& output_ptr = static_cast<data_flow::AccessNode&>(iedge_y->src());
5✔
255

256
    if (dataflow.in_degree(input_node_a) != 0 || dataflow.in_degree(input_node_b) != 0 ||
5✔
257
        dataflow.in_degree(output_ptr) != 0) {
5✔
258
        return false;
×
259
    }
×
260

261
    // Determine BLAS precision from primitive type
262
    auto prim_type = this->uniform_quantization(dataflow);
5✔
263
    if (!prim_type) {
5✔
264
        return false;
×
265
    }
×
266
    blas::BLAS_Precision precision;
5✔
267
    switch (prim_type.value()) {
5✔
268
        case types::PrimitiveType::Half:
×
269
            precision = blas::BLAS_Precision::h;
×
270
            break;
×
271
        case types::PrimitiveType::Float:
3✔
272
            precision = blas::BLAS_Precision::s;
3✔
273
            break;
3✔
274
        case types::PrimitiveType::Double:
1✔
275
            precision = blas::BLAS_Precision::d;
1✔
276
            break;
1✔
277
        default:
1✔
278
            // GEMM only supports floating point types, fall back to naive expansion
279
            return false;
1✔
280
    };
5✔
281

282
    // Add new graph after the current block
283
    auto& new_sequence = builder.add_sequence_before(parent, block, transition.assignments(), block.debug_info());
4✔
284

285
    auto copy_name_a = input_node_a.data();
4✔
286
    auto copy_name_b = input_node_b.data();
4✔
287

288
    // Check if A and B have basic strides and whether they are transposed in the last dimension
289
    blas::BLAS_Transpose trans_a, trans_b;
4✔
290
    if (layout_a_.has_linear_accesses_no_padding()) {
4✔
291
        trans_a = blas::BLAS_Transpose::No;
4✔
292
    } else if (layout_a_.has_transposed_strides_no_padding()) {
4✔
293
        trans_a = blas::BLAS_Transpose::Trans;
×
294
    } else {
×
295
        trans_a = blas::BLAS_Transpose::No;
×
296
        throw InvalidSDFGException("A must be in c-order");
×
297
    }
×
298
    if (layout_b_.has_linear_accesses_no_padding()) {
4✔
299
        trans_b = blas::BLAS_Transpose::No;
4✔
300
    } else if (layout_b_.has_transposed_strides_no_padding()) {
4✔
301
        trans_b = blas::BLAS_Transpose::Trans;
×
302
    } else {
×
303
        trans_b = blas::BLAS_Transpose::No;
×
304
        throw InvalidSDFGException("B must be in c-order");
×
305
    }
×
306

307
    // Create maps for batch dimensions and M, N dimensions
308
    structured_control_flow::Sequence* last_scope = &new_sequence;
4✔
309
    structured_control_flow::Map* last_map = nullptr;
4✔
310
    symbolic::MultiExpression batch_vars;
4✔
311

312
    // Compute batch dimensions (all except last 2)
313
    size_t batch_dims_a = layout_a_.dims() - 2;
4✔
314
    size_t batch_dims_b = layout_b_.dims() - 2;
4✔
315
    size_t max_batch_dims = std::max(batch_dims_a, batch_dims_b);
4✔
316

317
    // Create maps for batch dimensions (using broadcasting)
318
    for (size_t i = 0; i < max_batch_dims; ++i) {
5✔
319
        std::string indvar_str = builder.find_new_name("_b");
1✔
320
        builder.add_container(indvar_str, types::Scalar(types::PrimitiveType::UInt64));
1✔
321

322
        auto indvar = symbolic::symbol(indvar_str);
1✔
323
        auto init = symbolic::zero();
1✔
324
        auto update = symbolic::add(indvar, symbolic::one());
1✔
325

326
        // Determine the bound for this batch dimension (max of A and B for broadcasting)
327
        symbolic::Expression bound;
1✔
328
        size_t a_idx = batch_dims_a >= (max_batch_dims - i) ? i - (max_batch_dims - batch_dims_a) : SIZE_MAX;
1✔
329
        size_t b_idx = batch_dims_b >= (max_batch_dims - i) ? i - (max_batch_dims - batch_dims_b) : SIZE_MAX;
1✔
330

331
        if (a_idx != SIZE_MAX && b_idx != SIZE_MAX) {
1✔
332
            // Both have this dimension - they should be equal or one should be 1 (broadcasting)
333
            bound = layout_a_.get_dim(a_idx); // Assume they match or broadcasting is handled
1✔
334
        } else if (a_idx != SIZE_MAX) {
1✔
335
            bound = layout_a_.get_dim(a_idx);
×
336
        } else {
×
337
            bound = layout_b_.get_dim(b_idx);
×
338
        }
×
339

340
        auto condition = symbolic::Lt(indvar, bound);
1✔
341
        last_map = &builder.add_map(
1✔
342
            *last_scope,
1✔
343
            indvar,
1✔
344
            condition,
1✔
345
            init,
1✔
346
            update,
1✔
347
            structured_control_flow::ScheduleType_Sequential::create(),
1✔
348
            {},
1✔
349
            block.debug_info()
1✔
350
        );
1✔
351
        last_scope = &last_map->root();
1✔
352
        batch_vars.push_back(indvar);
1✔
353
    }
1✔
354

355
    auto& ref_block = builder.add_block(*last_scope, {}, block.debug_info());
4✔
356

357
    auto scalar_type = types::Scalar(prim_type.value());
4✔
358

359
    // Compute offsets for this batch iteration
360
    // For A: base_offset_a = offset_a + sum_i(batch_idx_i * batch_stride_a_i)
361
    symbolic::Expression a_batch_offset = layout_a_.offset();
4✔
362
    for (size_t i = 0; i < batch_dims_a; ++i) {
5✔
363
        size_t batch_idx = max_batch_dims - batch_dims_a + i;
1✔
364
        a_batch_offset = symbolic::add(a_batch_offset, symbolic::mul(batch_vars[batch_idx], layout_a_.get_stride(i)));
1✔
365
    }
1✔
366

367
    // For B: base_offset_b = offset_b + sum_i(batch_idx_i * batch_stride_b_i)
368
    symbolic::Expression b_batch_offset = layout_b_.offset();
4✔
369
    for (size_t i = 0; i < batch_dims_b; ++i) {
5✔
370
        size_t batch_idx = max_batch_dims - batch_dims_b + i;
1✔
371
        b_batch_offset = symbolic::add(b_batch_offset, symbolic::mul(batch_vars[batch_idx], layout_b_.get_stride(i)));
1✔
372
    }
1✔
373

374
    // Compute output batch offset (same as batch_vars pattern for Y)
375
    symbolic::Expression c_batch_offset = symbolic::integer(0);
4✔
376
    for (size_t i = 0; i < batch_vars.size(); ++i) {
5✔
377
        // Output has shape [batch..., M, N] with row-major strides
378
        // Stride for batch dim i is: M * N * product of remaining batch dims
379
        symbolic::Expression c_stride = symbolic::mul(this->m(), this->n());
1✔
380
        for (size_t j = i + 1; j < batch_vars.size(); ++j) {
1✔
381
            // Multiply by subsequent batch dimensions
382
            if (j < batch_dims_a) {
×
383
                c_stride = symbolic::mul(c_stride, layout_a_.get_dim(j));
×
384
            } else if (j - batch_dims_a < batch_dims_b) {
×
385
                c_stride = symbolic::mul(c_stride, layout_b_.get_dim(j - batch_dims_a));
×
386
            }
×
387
        }
×
388
        c_batch_offset = symbolic::add(c_batch_offset, symbolic::mul(batch_vars[i], c_stride));
1✔
389
    }
1✔
390

391
    // Create access nodes
392
    auto& a_access = builder.add_access(ref_block, copy_name_a, debug_info());
4✔
393
    auto& b_access = builder.add_access(ref_block, copy_name_b, debug_info());
4✔
394
    auto& c_access_in = builder.add_access(ref_block, output_ptr.data(), debug_info());
4✔
395

396
    std::string ref_name_a = builder.find_new_name(copy_name_a + "_ref");
4✔
397
    builder.add_container(ref_name_a, types::Pointer(types::Scalar(types::PrimitiveType::Void)));
4✔
398
    auto& a_access_ref = builder.add_access(ref_block, ref_name_a, debug_info());
4✔
399
    std::string ref_name_b = builder.find_new_name(copy_name_b + "_ref");
4✔
400
    builder.add_container(ref_name_b, types::Pointer(types::Scalar(types::PrimitiveType::Void)));
4✔
401
    auto& b_access_ref = builder.add_access(ref_block, ref_name_b, debug_info());
4✔
402
    std::string ref_name_c = builder.find_new_name(output_ptr.data() + "_ref");
4✔
403
    builder.add_container(ref_name_c, types::Pointer(types::Scalar(types::PrimitiveType::Void)));
4✔
404
    auto& c_access_ref_in = builder.add_access(ref_block, ref_name_c, debug_info());
4✔
405

406
    builder.add_reference_memlet(
4✔
407
        ref_block, a_access, a_access_ref, {a_batch_offset}, ::sdfg::types::Pointer(scalar_type), debug_info()
4✔
408
    );
4✔
409
    builder.add_reference_memlet(
4✔
410
        ref_block, b_access, b_access_ref, {b_batch_offset}, ::sdfg::types::Pointer(scalar_type), debug_info()
4✔
411
    );
4✔
412
    builder.add_reference_memlet(
4✔
413
        ref_block, c_access_in, c_access_ref_in, {c_batch_offset}, ::sdfg::types::Pointer(scalar_type), debug_info()
4✔
414
    );
4✔
415

416
    // Create block with GEMM library node
417
    auto& gemm_block = builder.add_block(*last_scope, {}, block.debug_info());
4✔
418

419
    // Leading dimensions: stride of the row dimension (second-to-last dim)
420
    symbolic::Expression lda, ldb;
4✔
421
    if (trans_a == blas::BLAS_Transpose::No) {
4✔
422
        // For row-major A [m * k] -> lda = k
423
        lda = layout_a_.get_stride_innermost(1);
4✔
424
    } else {
4✔
425
        // For row-major A [m * k] -> lda = m
426
        lda = layout_a_.get_stride_innermost(0);
×
427
    }
×
428
    if (trans_b == blas::BLAS_Transpose::No) {
4✔
429
        // For row-major B [k * n] -> ldb = n
430
        ldb = layout_b_.get_stride_innermost(1);
4✔
431
    } else {
4✔
432
        // For row-major B [k * n] -> ldb = k
433
        ldb = layout_b_.get_stride_innermost(0);
×
434
    }
×
435
    // For row-major C [m * n] -> ldc = n
436
    auto ldc = this->n();
4✔
437

438
    // Add GEMM node: C = alpha * A * B + beta * C
439
    // With alpha = 1.0, beta = 0.0: C = A * B
440
    auto& gemm_node = builder.add_library_node<blas::GEMMNode>(
4✔
441
        gemm_block,
4✔
442
        debug_info(),
4✔
443
        blas::ImplementationType_BLAS,
4✔
444
        precision,
4✔
445
        blas::BLAS_Layout::RowMajor,
4✔
446
        trans_a,
4✔
447
        trans_b,
4✔
448
        this->m(),
4✔
449
        this->n(),
4✔
450
        this->k(),
4✔
451
        lda,
4✔
452
        ldb,
4✔
453
        ldc
4✔
454
    );
4✔
455

456
    auto& a_access_ref_in_gemm = builder.add_access(gemm_block, ref_name_a, debug_info());
4✔
457
    auto& b_access_ref_in_gemm = builder.add_access(gemm_block, ref_name_b, debug_info());
4✔
458
    auto& c_access_ref_in_gemm = builder.add_access(gemm_block, ref_name_c, debug_info());
4✔
459

460
    // Create alpha and beta constants
461
    auto& alpha_const = builder.add_constant(gemm_block, "1.0", scalar_type, debug_info());
4✔
462
    auto& beta_const = builder.add_constant(gemm_block, "0.0", scalar_type, debug_info());
4✔
463

464
    // Connect memlets with batch offsets
465
    // Input A with offset
466
    builder.add_computational_memlet(
4✔
467
        gemm_block, a_access_ref_in_gemm, gemm_node, "__A", {}, ::sdfg::types::Pointer(scalar_type), debug_info()
4✔
468
    );
4✔
469
    // Input B with offset
470
    builder.add_computational_memlet(
4✔
471
        gemm_block, b_access_ref_in_gemm, gemm_node, "__B", {}, ::sdfg::types::Pointer(scalar_type), debug_info()
4✔
472
    );
4✔
473
    // Input C (for beta * C, but beta=0 so just needs to be connected)
474
    builder.add_computational_memlet(
4✔
475
        gemm_block, c_access_ref_in_gemm, gemm_node, "__C", {}, ::sdfg::types::Pointer(scalar_type), debug_info()
4✔
476
    );
4✔
477
    // Alpha constant
478
    builder.add_computational_memlet(gemm_block, alpha_const, gemm_node, "__alpha", {}, scalar_type, debug_info());
4✔
479
    // Beta constant
480
    builder.add_computational_memlet(gemm_block, beta_const, gemm_node, "__beta", {}, scalar_type, debug_info());
4✔
481

482
    // Free copies if we made them
483
    if (copy_name_a != input_node_a.data()) {
4✔
484
        free_after_copy(copy_name_a, builder, new_sequence);
×
485
    }
×
486
    if (copy_name_b != input_node_b.data()) {
4✔
487
        free_after_copy(copy_name_b, builder, new_sequence);
×
488
    }
×
489

490

491
    builder.clear_code_node_legacy(block, *this);
4✔
492
    // WARNING: this has been deallocated at this point!!
493
    builder.remove_child(parent, index + 1);
4✔
494

495
    return true;
4✔
496
}
4✔
497

498
nlohmann::json MatMulNodeSerializer::serialize(const data_flow::LibraryNode& library_node) {
×
499
    const MatMulNode& matmul_node = static_cast<const MatMulNode&>(library_node);
×
500
    nlohmann::json j;
×
501

502
    j["code"] = matmul_node.code().value();
×
503

504
    serializer::JSONSerializer serializer;
×
505

506
    matmul_node.layout_a().serialize_to_json(j["layout_a"]);
×
507
    matmul_node.layout_b().serialize_to_json(j["layout_b"]);
×
508

509
    j["result_quant"] = matmul_node.fixed_quantization();
×
510

511
    return j;
×
512
}
×
513

514
data_flow::LibraryNode& MatMulNodeSerializer::deserialize(
515
    const nlohmann::json& j, builder::StructuredSDFGBuilder& builder, structured_control_flow::Block& parent
516
) {
×
517
    assert(j.contains("element_id"));
×
518
    assert(j.contains("code"));
×
519
    assert(j.contains("debug_info"));
×
520

521
    std::optional<TensorLayout> layout_a;
×
522
    std::optional<TensorLayout> layout_b;
×
523

UNCOV
524
    auto layout_a_it = j.find("layout_a");
×
525
    if (layout_a_it != j.end()) {
×
526
        layout_a = TensorLayout::deserialize_from_json(*layout_a_it);
×
527
        layout_b = TensorLayout::deserialize_from_json(j.at("layout_b"));
×
528

529
    } else {
×
530
        assert(j.contains("shape_a"));
×
531
        assert(j.contains("shape_b"));
×
532

533
        symbolic::MultiExpression shape_a;
×
534
        for (const auto& dim : j["shape_a"]) {
×
535
            shape_a.push_back(symbolic::parse(dim.get<std::string>()));
×
536
        }
×
537

538
        symbolic::MultiExpression shape_b;
×
539
        for (const auto& dim : j["shape_b"]) {
×
540
            shape_b.push_back(symbolic::parse(dim.get<std::string>()));
×
541
        }
×
542

543
        symbolic::MultiExpression strides_a;
×
544
        if (j.contains("strides_a")) {
×
545
            for (const auto& stride : j["strides_a"]) {
×
546
                strides_a.push_back(symbolic::parse(stride.get<std::string>()));
×
547
            }
×
548
        }
×
549

550
        symbolic::MultiExpression strides_b;
×
551
        if (j.contains("strides_b")) {
×
552
            for (const auto& stride : j["strides_b"]) {
×
553
                strides_b.push_back(symbolic::parse(stride.get<std::string>()));
×
554
            }
×
555
        }
×
556

557
        symbolic::Expression offset_a = symbolic::integer(0);
×
558
        if (j.contains("offset_a")) {
×
559
            offset_a = symbolic::parse(j["offset_a"].get<std::string>());
×
560
        }
×
561

562
        symbolic::Expression offset_b = symbolic::integer(0);
×
563
        if (j.contains("offset_b")) {
×
564
            offset_b = symbolic::parse(j["offset_b"].get<std::string>());
×
565
        }
×
566

567
        layout_a = TensorLayout(shape_a, strides_a, offset_a);
×
568
        layout_b = TensorLayout(shape_b, strides_b, offset_b);
×
569
    }
×
570

NEW
571
    auto quantization = deserialize_quantization(j, "result_quant", QUANTIZATION_MATCH_INPUTS);
×
572

573
    sdfg::serializer::JSONSerializer serializer;
×
574
    DebugInfo debug_info = serializer.json_to_debug_info(j["debug_info"]);
×
575

576
    return builder.add_library_node<MatMulNode>(parent, debug_info, layout_a.value(), layout_b.value(), quantization);
×
577
}
×
578

579
} // namespace tensor
580
} // namespace math
581
} // 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