diff --git a/npu/audio_exec.cpp b/npu/audio_exec.cpp index f662bf468..a05e82e25 100644 --- a/npu/audio_exec.cpp +++ b/npu/audio_exec.cpp @@ -348,6 +348,33 @@ bool run_graph_impl(const OnnxGraphInfo& g, std::map 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 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; @@ -547,7 +574,11 @@ bool run_graph_impl(const OnnxGraphInfo& g, std::map t, con if (infer >= 0) dims[size_t(infer)] = int64_t(a.data.size() / std::max(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]); diff --git a/tests/audio_parallel_test.cpp b/tests/audio_parallel_test.cpp index c8fab16c8..6cb2b195e 100644 --- a/tests/audio_parallel_test.cpp +++ b/tests/audio_parallel_test.cpp @@ -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 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(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(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();