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

daisytuner / sdfglib / 17621055299

10 Sep 2025 05:00PM UTC coverage: 59.755% (+0.6%) from 59.145%
17621055299

Pull #210

github

web-flow
Merge 6e7cc1401 into b8fdeb232
Pull Request #210: New debug info

777 of 1111 new or added lines in 46 files covered. (69.94%)

11 existing lines in 11 files now uncovered.

9755 of 16325 relevant lines covered (59.75%)

115.06 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/log_softmax.cpp
1
#include "sdfg/data_flow/library_nodes/math/ml/log_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
LogSoftmaxNode::LogSoftmaxNode(
×
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(
×
19
          element_id,
×
20
          debug_info,
×
21
          vertex,
×
22
          parent,
×
23
          LibraryNodeType_LogSoftmax,
24
          {"output"},
×
25
          {"input"},
×
26
          data_flow::ImplementationType_NONE
27
      ),
28
      axis_(axis) {}
×
29

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

32
bool LogSoftmaxNode::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
    int index = parent.index(block);
×
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(
×
NEW
61
        parent, block, transition.assignments(), builder.debug_info().get_region(block.debug_info().indices())
×
62
    );
UNCOV
63
    structured_control_flow::Sequence *last_scope = &new_sequence;
×
64

65
    // Create maps over output subset dims (parallel dims)
66
    data_flow::Subset domain_begin = oedge_output->begin_subset();
×
67
    data_flow::Subset domain_end = oedge_output->end_subset();
×
68

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

92
    // Initialize temp variable to zero
93
    std::string temp_name = builder.find_new_name("_tmp");
×
94
    std::string temp_name2 = builder.find_new_name("_tmp");
×
95
    types::Scalar temp_type(types::PrimitiveType::Float);
×
96
    builder.add_container(temp_name, temp_type);
×
97
    builder.add_container(temp_name2, temp_type);
×
98

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

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

130
    // Create innermost block
131
    auto &code_block = builder.add_block(red_map->root());
×
132

133
    // Create access nodes for input and output
134
    auto &input_access = builder.add_access(code_block, input_name);
×
135
    auto &tmp2_access = builder.add_access(code_block, temp_name2);
×
136
    auto &tmp_access_out = builder.add_access(code_block, temp_name);
×
137
    auto &tmp_access_in = builder.add_access(code_block, temp_name2);
×
138

139
    // Create index expressions for input and output
140
    std::vector<symbolic::Expression> input_subset = loop_syms;
×
141

142
    // Replace the reduction axis index with the reduction variable for input
143
    if (axis_ >= 0 && axis_ < static_cast<int>(input_subset.size())) {
×
144
        input_subset.insert(input_subset.begin() + axis_, red_indvar);
×
145
    } else if (axis_ < 0) {
×
146
        input_subset.push_back(red_indvar);
×
147
    }
×
148

149
    // Compute exponential
150
    auto &exp_tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::expf, "_out", {"_in"});
×
151
    builder
×
152
        .add_computational_memlet(code_block, input_access, exp_tasklet, "_in", input_subset, iedge_input->base_type());
×
153
    builder.add_computational_memlet(code_block, exp_tasklet, "_out", tmp2_access, {}, temp_type);
×
154

155
    // Add to temp (reduction)
156
    auto &add_tasklet = builder.add_tasklet(code_block, data_flow::TaskletCode::add, "_out", {"_in1", "_in2"});
×
157
    builder.add_computational_memlet(code_block, tmp_access_in, add_tasklet, "_in1", {}, temp_type);
×
158
    builder.add_computational_memlet(code_block, tmp2_access, add_tasklet, "_in2", {}, temp_type);
×
159
    builder.add_computational_memlet(code_block, add_tasklet, "_out", tmp_access_out, {}, temp_type);
×
160

161
    // Create writeback - assign the accumulated sum to output
162
    auto &writeback_block = builder.add_block(*last_scope);
×
163
    auto &tmp_access_wb = builder.add_access(writeback_block, temp_name);
×
164
    auto &output_access_wb = builder.add_access(writeback_block, output_name);
×
165
    auto &writeback_tasklet = builder.add_tasklet(writeback_block, data_flow::TaskletCode::logf, "_out", {"_in"});
×
166
    builder.add_computational_memlet(writeback_block, tmp_access_wb, writeback_tasklet, "_in", {}, temp_type);
×
167
    builder.add_computational_memlet(
×
168
        writeback_block, writeback_tasklet, "_out", output_access_wb, loop_syms, oedge_output->base_type()
×
169
    );
170

171
    // Cleanup old block
172
    builder.remove_memlet(block, *iedge_input);
×
173
    builder.remove_memlet(block, *oedge_output);
×
174
    builder.remove_node(block, *this);
×
175
    builder.remove_child(parent, index + 1);
×
176

177
    return true;
×
178
}
×
179

180
std::unique_ptr<data_flow::DataFlowNode> LogSoftmaxNode::
181
    clone(size_t element_id, const graph::Vertex vertex, data_flow::DataFlowGraph &parent) const {
×
182
    return std::unique_ptr<
×
183
        data_flow::DataFlowNode>(new LogSoftmaxNode(element_id, this->debug_info(), vertex, parent, axis_));
×
184
}
×
185

186
nlohmann::json LogSoftmaxNodeSerializer::serialize(const data_flow::LibraryNode &library_node) {
×
187
    const LogSoftmaxNode &node = static_cast<const LogSoftmaxNode &>(library_node);
×
188
    nlohmann::json j;
×
189

190
    j["code"] = node.code().value();
×
191
    j["axis"] = node.axis();
×
192

193
    return j;
×
194
}
×
195

196
data_flow::LibraryNode &LogSoftmaxNodeSerializer::deserialize(
×
197
    const nlohmann::json &j, builder::StructuredSDFGBuilder &builder, structured_control_flow::Block &parent
198
) {
199
    auto code = j["code"].get<std::string>();
×
200
    if (code != LibraryNodeType_LogSoftmax.value()) {
×
201
        throw std::runtime_error("Invalid library node code");
×
202
    }
203

204
    sdfg::serializer::JSONSerializer serializer;
×
NEW
205
    DebugInfoRegion debug_info = serializer.json_to_debug_info_region(j["debug_info"], builder.debug_info());
×
206

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

209
    return builder.add_library_node<LogSoftmaxNode>(parent, debug_info, axis);
×
210
}
×
211

212
} // namespace ml
213
} // namespace math
214
} // 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