Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion npu/audio_exec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,33 @@ bool run_graph_impl(const OnnxGraphInfo& g, std::map<std::string, Tensor> t, con
}
};

// engine#2399: a tensor with exactly one use can be MOVED into its consumer instead of copied. Reshape
// was deep-copying 4.6 MB per node (216 nodes, ~130 ms per encoder pass) to change a shape and no bytes.
// A move changes no bits, so this cannot alter any result; the count makes it safe (a twice-used input,
// e.g. Add(x, x), keeps its copy).
std::map<std::string, int> use_count;
for (const auto& n : g.nodes) {
for (const auto& in : n.ins) {
if (!in.empty()) ++use_count[in];
}
}
// engine#2437: a graph OUTPUT is a use too. The fetch after the loop reads the requested tensor out of `t`
// (`out_name`, else g.outputs[0]), so a tensor that is an output can never be "last used by a node" even
// when it has exactly one node consumer -- moving it there leaves the fetch copying a moved-from (empty)
// vector, and the run still returns true. Counting outputs keeps the invariant in one place: the count
// answers "how many more users of `name` are there", and the fetch is one of them.
for (const auto& o : g.outputs) {
if (!o.empty()) ++use_count[o];
}
if (!out_name.empty()) ++use_count[out_name];
auto take_if_last = [&](const std::string& name) -> Tensor* {
const auto c = use_count.find(name);
const auto it = t.find(name);
if (c == use_count.end() || c->second != 1 || it == t.end()) return nullptr;
use_count[name] = 0;
return &it->second;
};

int node_index = 0;
for (const auto& n : g.nodes) {
++node_index;
Expand Down Expand Up @@ -547,7 +574,11 @@ bool run_graph_impl(const OnnxGraphInfo& g, std::map<std::string, Tensor> t, con
if (infer >= 0) dims[size_t(infer)] = int64_t(a.data.size() / std::max<size_t>(known, 1));
if (nelem(dims) != a.data.size()) fail(where() + ": reshape changes the element count");
r.dims = dims;
r.data = a.data;
if (Tensor* src = take_if_last(n.ins[0])) {
r.data = std::move(src->data); // engine#2399: same bits, no copy
} else {
r.data = a.data;
}
} else if (n.op == "Conv") {
const Tensor& a = need(n.ins[0]);
const Tensor& w = init_f32(n.ins[1]);
Expand Down
46 changes: 46 additions & 0 deletions tests/audio_parallel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,53 @@ void test_a_throwing_unit() {

} // namespace

// engine#2437: a tensor that is BOTH the graph's requested output and the input of exactly one node. The move
// optimisation counts uses, and before this case existed it counted node inputs only -- so `y` looked last-used by
// the Reshape, was MOVED into it, and the fetch of "y" afterwards copied a moved-from (empty) vector while the run
// still returned true. That is a silent empty output, which is why the arm asserts the DATA and not just the rc.
void output_also_feeds_one_node() {
OnnxGraphInfo g;
g.dir = ".";
g.inputs = {"x"};
g.outputs = {"y"}; // y is the requested output ...
const std::vector<float> x = {1.f, 2.f, 3.f, 4.f};
OnnxInit shp;
shp.name = "shape22";
shp.dtype = 7; // int64
static const int64_t d22[2] = {2, 2};
shp.dims = {2}; // a 1-D shape tensor of two values
shp.raw = reinterpret_cast<const uint8_t*>(d22);
shp.raw_len = sizeof(d22);
OnnxInit shp4;
shp4.name = "shape4";
shp4.dtype = 7;
static const int64_t d4[1] = {4};
shp4.dims = {1};
shp4.raw = reinterpret_cast<const uint8_t*>(d4);
shp4.raw_len = sizeof(d4);
g.initializers = {"shape22", "shape4"};
g.init_tensors.push_back(shp);
g.init_tensors.push_back(shp4);
// node 1 produces y from the input ...
OnnxNodeInfo r1;
r1.op = "Reshape"; r1.name = "r1"; r1.ins = {"x", "shape22"}; r1.outs = {"y"};
g.nodes.push_back(r1);
// ... and y then feeds exactly ONE node, whose output nobody fetches. take_if_last used to read that as
// "last use of y" and move it, emptying the tensor the fetch below then copies.
OnnxNodeInfo r2;
r2.op = "Reshape"; r2.name = "r2"; r2.ins = {"y", "shape4"}; r2.outs = {"z"};
g.nodes.push_back(r2);
AudioRunResult out;
std::string why;
const bool ok = run_audio_graph(g, x.data(), 2, 2, "y", &out, &why);
if (!ok) std::printf(" (why: %s)\n", why.c_str());
check(ok, "y-as-output-and-reshape-input: ran");
check(out.data.size() == x.size(), "y-as-output-and-reshape-input: output is not empty");
check(out.data == x, "y-as-output-and-reshape-input: output bytes survive the Reshape move");
}

int main() {
output_also_feeds_one_node();
test_bit_identity();
test_row_group_edges();
test_elementwise_bit_identity();
Expand Down
Loading