MCPcopy Create free account
hub / github.com/alibaba/MNN / buildScatterND

Function buildScatterND

source/geometry/GeometryScatter.cpp:13–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11#include "core/OpCommonUtils.hpp"
12namespace MNN {
13static bool buildScatterND(const Op* op, Tensor* indices, Tensor* updates, Tensor* data, Tensor* output,
14 int N, int D, int S, int totalSize, int reduction,
15 GeometryComputer::Context& context, CommandBuffer& res) {
16 // get stride
17 std::shared_ptr<Tensor> constStride(Tensor::createDevice<int>({D}));
18 if (!context.allocTensor(constStride.get())) {
19 return false;
20 }
21 int count = output->elementSize();
22 for (int i = 0; i < D; ++i) {
23 count = count / output->length(i);
24 constStride->host<int>()[i] = count;
25 }
26 res.extras.emplace_back(constStride);
27 std::shared_ptr<Tensor> broadcastStride(Tensor::createDevice<int>({N, D}));
28 {
29 // [D] => [N, D]
30 auto des = TensorUtils::getDescribe(broadcastStride.get());
31 des->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL;
32 des->regions.resize(1);
33 des->regions[0].origin = constStride.get();
34 des->regions[0].size[0] = 1;
35 des->regions[0].size[1] = N;
36 des->regions[0].size[2] = D;
37 des->regions[0].dst.stride[0] = N*D;
38 des->regions[0].dst.stride[1] = D;
39 des->regions[0].dst.stride[2] = 1;
40 des->regions[0].src.stride[0] = 0;
41 des->regions[0].src.stride[1] = 0;
42 des->regions[0].src.stride[2] = 1;
43 res.extras.emplace_back(broadcastStride);
44 }
45 // reshape indices: [dims1, D] -> [N, D]
46 std::shared_ptr<Tensor> reshapeIndice(Tensor::createDevice<int>({N, D}));
47 {
48 auto des = TensorUtils::getDescribe(reshapeIndice.get());
49 des->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL;
50 des->regions = {GeometryComputerUtils::makeRawAddressRef(indices, 0, N * D)};
51 res.extras.emplace_back(reshapeIndice);
52 }
53 // get index
54 std::shared_ptr<Tensor> mulIndice(Tensor::createDevice<int>({N, D}));
55 {
56 // [N, D] * [N, D] => [N, D]
57 auto cmd = GeometryComputerUtils::makeBinary(BinaryOpOperation_MUL, reshapeIndice.get(), broadcastStride.get(), mulIndice.get());
58 res.extras.emplace_back(mulIndice);
59 res.command.emplace_back(std::move(cmd));
60 }
61 std::shared_ptr<Tensor> indiceOneLine(Tensor::createDevice<int>({N, 1}));
62 {
63 // [N, D] => [N, 1]
64 auto cmd = GeometryComputerUtils::makeReduce(ReductionType_SUM, mulIndice.get(), indiceOneLine.get());
65 res.extras.emplace_back(indiceOneLine);
66 res.command.emplace_back(std::move(cmd));
67 }
68 auto outputDes = TensorUtils::getDescribe(output);
69 flatbuffers::FlatBufferBuilder builder;
70 {

Callers 2

onComputeMethod · 0.85
onComputeMethod · 0.85

Calls 15

allocTensorMethod · 0.80
add_viewMethod · 0.80
add_iterIndexesMethod · 0.80
add_stepsMethod · 0.80
add_fuseMethod · 0.80
add_initCommandMethod · 0.80
add_commandsMethod · 0.80
add_parallelMethod · 0.80
add_loopNumberMethod · 0.80
getMethod · 0.45
lengthMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected