Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Add model MobileNetV1, ResNet50 and SqueezeNet #441

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

haozech
Copy link
Collaborator

@haozech haozech commented Sep 22, 2021

This pr supports 2 new Models: MobileNetV1 and ResNet50. And their corresponding tests are added, too.
To achieve this, a new op reshape2 is added and some bugs are fixed.

@paddle-bot-old
Copy link

Thanks for your contribution!

@haozech haozech changed the title Add model mobilenetV1 and resnet50 Add model MobileNetV1 and ResNet50 Sep 22, 2021
@haozech haozech changed the title Add model MobileNetV1 and ResNet50 Add model MobileNetV1, ResNet50 and SqueezeNet Sep 24, 2021
}

void PaddleModelToProgram::AddOpMapper_reshape2() {
op_mappers_["reshape2"] = [&](const paddle::cpp::OpDesc& op_desc) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reshape2?改下名字,下同

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks

@@ -1252,6 +1262,12 @@ std::vector<std::vector<int>> InferShapeForPool2d(const std::vector<std::vector<
(inputs_shape[0][width_axis] - kernel_size[1] + padding_size[1] + padding_size[3]) / stride_size[1] + 1;
}

if (adaptive) {
kernel_size = std::get<std::vector<int>>(attr_store["kernel_size"]);
if (kernel_size.size() == 1) kernel_size.push_back(kernel_size[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check kernel_size.size(),防止数组越界

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
if (axis < 0) axis += inputs_shape[0].size();
std::vector<int> output_shape = inputs_shape[0];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对inputs_shape进行校验,两者size需要相同以及axis不能越界

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/**
* Reshape a tensor.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释具体点?下同

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

.set_attr<cinn::hlir::framework::StrategyFunction>("CINNStrategy", cinn::hlir::op::StrategyForReshape2)
.set_attr("infershape", std::function(cinn::hlir::op::InferShapeForReshape2))
.set_attr("inferdtype", std::function(cinn::hlir::op::InferDtypeForReshape2))
.set_attr<cinn::hlir::framework::OpPatternKind>("OpPattern", cinn::hlir::framework::OpPatternKind::kOpaque)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有新增op需要添加inferlayout函数

std::vector<int> output_shape;
for (auto &iter : attrs.attr_store) {
if (iter.first == "shape") {
output_shape = std::get<std::vector<int>>(iter.second);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

output_shape = std::get<std::vector<int>>(iter.second);
}
}
int tensor_size = 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增op添加单测

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

int flag_index = -1;
for (int i = 0; i < output_shape.size(); i++) {
if (output_shape[i] > 0) {
CHECK_EQ(tensor_size % output_shape[i], 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paddle里output_shape[i]可能为0吧?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for (auto &iter : attrs.attr_store) {
if (iter.first == "axis") {
axis = std::get<int>(iter.second);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

std::vector<Expr> new_expr_shape;
std::vector<Expr> A_expr_shape = A->shape;
for (auto& i : new_shape) {
new_expr_shape.push_back(Expr(i));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

增强shape参数的校验

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

for (auto& i : new_shape) {
new_expr_shape.push_back(Expr(i));
}
auto res = Compute(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的new_shape[i]是不是也有可能是-1和0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不会,这里的shape是通过infershape得到的

class TestLoadPaddleModel_FC(unittest.TestCase):
def setUp(self):
if enable_gpu == "ON":
self.target = DefaultNVGPUTarget()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些为啥删了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

误删,已恢复

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants