• 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/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 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_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));
×
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
    std::string temp_name2 = builder.find_new_name("_tmp");
×
93
    types::Scalar temp_type(types::PrimitiveType::Float);
×
94
    builder.add_container(temp_name, temp_type);
×
95
    builder.add_container(temp_name2, temp_type);
×
96

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

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

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

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

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

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

139
    // Compute exponential
140
    auto &exp_tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::expf, "_out", {"_in"});
×
NEW
141
    builder
×
NEW
142
        .add_computational_memlet(code_block, input_access, exp_tasklet, "_in", input_subset, iedge_input->base_type());
×
UNCOV
143
    builder.add_computational_memlet(code_block, exp_tasklet, "_out", tmp2_access, {}, temp_type);
×
144

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

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

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

167
    return true;
×
168
}
×
169

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

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

180
    j["code"] = node.code().value();
×
181
    j["axis"] = node.axis();
×
182

183
    return j;
×
184
}
×
185

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

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

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

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

202
} // namespace ml
203
} // namespace math
204
} // 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