Skip to content

Commit

Permalink
Merge pull request #2503 from alibaba/feature/bugfix
Browse files Browse the repository at this point in the history
Feature/bugfix
  • Loading branch information
jxt1234 authored Jul 23, 2023
2 parents 43445b7 + 56f420f commit d8266f9
Show file tree
Hide file tree
Showing 10 changed files with 421 additions and 332 deletions.
1 change: 1 addition & 0 deletions source/backend/cpu/CPUROIAlign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ErrorCode CPUROIAlign::onResize(const std::vector<Tensor*>& inputs, const std::v
auto& roi = inputs[1]->buffer();

mROI.buffer().dimensions = roi.dimensions;
mROI.buffer().type = halide_type_of<int>();// Use int32 instead of float to ensure the backend alloc 4 byte unit
memcpy(mROI.buffer().dim, roi.dim, sizeof(halide_dimension_t) * roi.dimensions);
TensorUtils::getDescribe(&mROI)->dimensionFormat = MNN_DATA_FORMAT_NCHW;
TensorUtils::setLinearLayout(&mROI);
Expand Down
317 changes: 158 additions & 159 deletions source/backend/vulkan/buffer/compiler/AllShader.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void main()
int W = uConst.w;
int H = uConst.h;
int C = uConst.c;
float maxValue = -1000.0;
for(int i = 0; i < H; ++i)
float maxValue = uInput.data[pos.x * H * W + pos.y];
for(int i = 1; i < H; ++i)
{
int index = i * W + pos.x * H * W + pos.y;
maxValue = max(maxValue, uInput.data[index]);
Expand Down
317 changes: 158 additions & 159 deletions source/backend/vulkan/image/compiler/AllShader.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void main()
int W = uConst.w;
int H = uConst.h;
int C = uConst.c;
float maxValue = -1000.0;
for(int i = 0; i < H; ++i)
float maxValue = uInput.data[pos.x * H * W + pos.y];
for(int i = 1; i < H; ++i)
{
int index = i * W + pos.x * H * W + pos.y;
maxValue = max(maxValue, uInput.data[index]);
Expand Down
5 changes: 3 additions & 2 deletions source/geometry/GeometryComputerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,9 @@ int GeometryComputerUtils::buildConstantTensors(std::vector<Schedule::OpCacheInf
if (turnConst) {
for (auto t : info.outputs) {
TensorUtils::getDescribe(t)->usage = Tensor::InsideDescribe::CONSTANT;
TensorUtils::getDescribe(t)->stageMask |= MNN::Tensor::InsideDescribe::StageInfo::GEOMETRY_STAGE;
}
for (auto t : info.inputs) {
TensorUtils::getDescribe(t)->usage = Tensor::InsideDescribe::CONSTANT;
TensorUtils::getDescribe(t)->stageMask |= MNN::Tensor::InsideDescribe::StageInfo::GEOMETRY_STAGE;
}
info.type = Schedule::CONSTANT;
hasConst = true;
Expand All @@ -126,6 +124,9 @@ int GeometryComputerUtils::buildConstantTensors(std::vector<Schedule::OpCacheInf
}
for (auto& info : infos) {
if (info.type == Schedule::CONSTANT) {
for (auto t : info.inputs) {
TensorUtils::getDescribe(t)->stageMask |= MNN::Tensor::InsideDescribe::StageInfo::GEOMETRY_STAGE;
}
for (auto t : info.outputs) {
TensorUtils::getDescribe(t)->usage = Tensor::InsideDescribe::CONSTANT;
}
Expand Down
6 changes: 6 additions & 0 deletions source/geometry/GeometryScatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ class GeometryScatterElements : public GeometryComputer {
if (axis < 0) {
axis = D + axis;
}
if (N == 0) {
auto outputDes = TensorUtils::getDescribe(output);
outputDes->regions = {TensorUtils::makeFullSlice(data)};
outputDes->memoryType = Tensor::InsideDescribe::MEMORY_VIRTUAL;
return true;
}
// flatten indices/update
std::shared_ptr<Tensor> flattenIndice(Tensor::createDevice<int>({N}));
std::shared_ptr<Tensor> flattenUpdate(Tensor::createDevice({N}, updates->getType(), Tensor::TENSORFLOW));
Expand Down
48 changes: 48 additions & 0 deletions test/expr/ModuleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,51 @@ class MemeoryUsageTest : public MNNTestCase {
}
};
MNNTestSuiteRegister(MemeoryUsageTest, "expr/MemeoryUsageTest");

// This test shoule use gpu to test
class ConstMemoryReplaceTest : public MNNTestCase {
public:
virtual bool run(int precision) {
auto x = _Input({1, 4, 1, 1}, NC4HW4);
auto y = _Const(0.3f, {1, 1, 4, 1}, NC4HW4);
auto z = x * y;
auto w0 = _Round(_ReduceSum(_Convert(y, NHWC)));
z = z + _Unsqueeze(w0, {0});
auto w1 = _Scalar<int>(1);
auto shape = _Stack({w1, _Cast<int>(w0), w1, w1}, -1);
auto ones = _Fill(shape, _Scalar<float>(0.3f));
auto res = z + ones;
x->writeMap<float>();
auto ptr = res->readMap<float>();
if (nullptr == ptr) {
FUNC_PRINT(1);
return false;
}
flatbuffers::FlatBufferBuilder builderOutput(1024);
{
std::shared_ptr<MNN::NetT> net(new NetT);
Variable::save({res}, net.get());
y = nullptr;
auto len = MNN::Net::Pack(builderOutput, net.get());
builderOutput.Finish(len);
}
int sizeOutput = builderOutput.GetSize();
auto bufferOutput = builderOutput.GetBufferPointer();
std::shared_ptr<Interpreter> net(Interpreter::createFromBuffer((void*)bufferOutput, sizeOutput), Interpreter::destroy);
ScheduleConfig config;
config.numThread = 4;
config.type = ExecutorScope::Current()->getAttr()->firstType.first;
auto s1 = net->createSession(config);
int resizeCode;
net->getSessionInfo(s1, Interpreter::RESIZE_STATUS, &resizeCode);
if (resizeCode != 0) {
FUNC_PRINT(1);
return false;
}
net->runSession(s1);
net->resizeTensor(net->getSessionInput(s1, nullptr), {1, 1, 1, 1});
net->resizeSession(s1);
return resizeCode == 0;
}
};
MNNTestSuiteRegister(ConstMemoryReplaceTest, "expr/ConstMemoryReplaceTest");
31 changes: 23 additions & 8 deletions test/op/ScatterElementsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,31 @@ class ScatterElementsTest : public MNNTestCase {
const int indicesData[] = {1, 0, 2, 0, 2, 1};
const float updatesData[] = {1.0, 1.1, 1.2, 2.0, 2.1, 2.2};
const float expectedData[] = {2.0, 1.1, 0.0, 1.0, 0.0, 2.2, 0.0, 2.1, 1.2};
{
auto data = _Const(dataData, {3, 3}, NHWC, halide_type_of<float>());
auto indices = _Const(indicesData, {2, 3}, NHWC, halide_type_of<int>());
auto updates = _Const(updatesData, {2, 3}, NHWC, halide_type_of<float>());
auto output = _ScatterElements(data, indices, updates);

auto data = _Const(dataData, {3, 3}, NHWC, halide_type_of<float>());
auto indices = _Const(indicesData, {2, 3}, NHWC, halide_type_of<int>());
auto updates = _Const(updatesData, {2, 3}, NHWC, halide_type_of<float>());
auto output = _ScatterElements(data, indices, updates);
auto outputData = output->readMap<float>();
const int size = output->getInfo()->size;
if (!checkVector<float>(outputData, expectedData, size, 0.001)) {
FUNC_PRINT(1);
return false;
}
}
{
auto data = _Const(dataData, {3, 3}, NHWC, halide_type_of<float>());
auto indices = _Const(nullptr, {0, 3}, NHWC, halide_type_of<int>());
auto updates = _Const(updatesData, {2, 3}, NHWC, halide_type_of<float>());
auto output = _ScatterElements(data, indices, updates);

auto outputData = output->readMap<float>();
const int size = output->getInfo()->size;
if (!checkVector<float>(outputData, expectedData, size, 0.001)) {
return false;
auto outputData = output->readMap<float>();
const int size = output->getInfo()->size;
if (!checkVector<float>(outputData, dataData, size, 0.001)) {
FUNC_PRINT(1);
return false;
}
}
}
{
Expand Down
20 changes: 20 additions & 0 deletions test/op/ScatterNdTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ class ScatterNdTest : public MNNTestCase {
virtual ~ScatterNdTest() = default;

virtual bool run(int precision) {
{
const int indicesData[] = {4, 3, 1, 7};
const float updatesData[] = {9, 10, 11, 12};
const int shapeData[] = {8};
const float dataData[] = {1, 2, 3, 4, 5, 6, 7, 8};
const float expectedResult[] = {1, 11, 3, 10, 9, 6, 7, 12};

auto indices = _Const(indicesData, {0, 1}, NHWC, halide_type_of<int>());
auto updates = _Const(nullptr, {0}, NHWC, halide_type_of<float>());
auto shape = _Const(shapeData, {1}, NHWC, halide_type_of<float>());
auto data = _Const(dataData, {8}, NHWC, halide_type_of<float>());
auto result = _ScatterNd(indices, updates, shape, data);

auto resultData = result->readMap<float>();
const int size = result->getInfo()->size;
if (!checkVector<float>(resultData, dataData, size, 0.001)) {
FUNC_PRINT(1);
return false;
}
}
{
const int indicesData[] = {4, 3, 1, 7};
const float updatesData[] = {9, 10, 11, 12};
Expand Down

0 comments on commit d8266f9

Please sign in to comment.