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

daisytuner / sdfglib / 17300165647

28 Aug 2025 03:14PM UTC coverage: 60.049% (+0.3%) from 59.781%
17300165647

Pull #210

github

web-flow
Merge f6109d03a into 18d34db1e
Pull Request #210: New debug info

377 of 593 new or added lines in 37 files covered. (63.58%)

15 existing lines in 8 files now uncovered.

9588 of 15967 relevant lines covered (60.05%)

114.92 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/softmax.cpp
1
#include "sdfg/data_flow/library_nodes/math/ml/softmax.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
SoftmaxNode::SoftmaxNode(
×
12
    size_t element_id,
13
    const DebugInfoRegion &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_Softmax,
NEW
24
          {"output"},
×
NEW
25
          {"input"},
×
26
          data_flow::ImplementationType_NONE
27
      ),
28
      axis_(axis) {}
×
29

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

32
bool SoftmaxNode::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));
×
38

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

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

57
    // Create new sequence before
58
    auto &new_sequence = builder.add_sequence_before(parent, block, block.debug_info()).first;
×
59
    structured_control_flow::Sequence *last_scope = &new_sequence;
×
60

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

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

88
    // Initialize temp variable to zero
89
    std::string temp_name = builder.find_new_name("_tmp");
×
90
    std::string temp_name2 = builder.find_new_name("_tmp");
×
91
    types::Scalar temp_type(types::PrimitiveType::Float);
×
92
    builder.add_container(temp_name, temp_type);
×
93
    builder.add_container(temp_name2, 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_name2);
×
124
    auto &tmp_access_out = builder.add_access(code_block, temp_name);
×
125
    auto &tmp_access_in = builder.add_access(code_block, temp_name2);
×
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 exponential
138
    auto &exp_tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::expf, "_out", {"_in"});
×
NEW
139
    builder
×
NEW
140
        .add_computational_memlet(code_block, input_access, exp_tasklet, "_in", input_subset, iedge_input->base_type());
×
UNCOV
141
    builder.add_computational_memlet(code_block, exp_tasklet, "_out", tmp2_access, {}, temp_type);
×
142

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

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

159
    // Cleanup old block
160
    builder.remove_memlet(block, *iedge_input);
×
161
    builder.remove_memlet(block, *oedge_output);
×
162
    builder.remove_node(block, *this);
×
163
    builder.remove_child(parent, block);
×
164

165
    return true;
×
166
}
×
167

168
std::unique_ptr<data_flow::DataFlowNode> SoftmaxNode::
169
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
NEW
170
    return std::unique_ptr<data_flow::DataFlowNode>(new SoftmaxNode(element_id, this->debug_info(), vertex, parent, axis_)
×
171
    );
UNCOV
172
}
×
173

174
nlohmann::json SoftmaxNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
175
    const SoftmaxNode &node = static_cast<const SoftmaxNode &>(library_node);
×
176
    nlohmann::json j;
×
177

178
    j["code"] = node.code().value();
×
179
    j["axis"] = node.axis();
×
180

181
    return j;
×
182
}
×
183

184
data_flow::LibraryNode &SoftmaxNodeSerializer::deserialize(
×
185
    const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent
186
) {
187
    auto code = j["code"].get<std::string>();
×
188
    if (code != LibraryNodeType_Softmax.value()) {
×
189
        throw std::runtime_error("Invalid library node code");
×
190
    }
191

192
    sdfg::serializer::JSONSerializer serializer;
×
NEW
193
    DebugInfoRegion debug_info = serializer.json_to_debug_info_region(j["debug_info_region"], builder.debug_info());
×
194

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

197
    return builder.add_library_node<SoftmaxNode>(parent, debug_info, axis);
×
198
}
×
199

200
} // namespace ml
201
} // namespace math
202
} // 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