Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add im2sequence op #1239

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
81 changes: 81 additions & 0 deletions paddle2onnx/mapper/nn/im2sequence.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle2onnx/mapper/nn/im2sequence.h"

#include <string>
#include <vector>

namespace paddle2onnx
{
REGISTER_MAPPER(im2sequence, Im2SequenceMapper)

int32_t Im2SequenceMapper::GetMinOpset(bool verbose)
{
return 7;
}

std::vector<int64_t> CalculateNewShape(const std::vector<int64_t> &input_shape,
const std::vector<int64_t> &kernels,
const std::vector<int64_t> &strides,
const std::vector<int64_t> &paddings)
{
int64_t N = input_shape[0];
int64_t C = input_shape[1];
int64_t H = input_shape[2];
int64_t W = input_shape[3];

int64_t kernel_h = kernels[0];
int64_t kernel_w = kernels[1];
int64_t stride_h = strides[0];
int64_t stride_w = strides[1];
int64_t pad_top = paddings[0];
int64_t pad_bottom = paddings[1];
int64_t pad_left = paddings[2];
int64_t pad_right = paddings[3];

int64_t out_h = (H + pad_top + pad_bottom - kernel_h) / stride_h + 1;
int64_t out_w = (W + pad_left + pad_right - kernel_w) / stride_w + 1;

std::vector<int64_t> new_shape = {N, C, out_h * kernel_h, out_w * kernel_w};

return new_shape;
}

void Im2SequenceMapper::Opset7()
{
auto input_info = GetInput("X");
auto output_info = GetOutput("Out");

std::vector<int64_t> kernels = kernels_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

这些变量似乎都没有用到,可以移除?

std::vector<int64_t> strides = strides_;
std::vector<int64_t> paddings = paddings_;

std::vector<int64_t> new_shape = CalculateNewShape(input_info[0].shape, kernels_, strides_, paddings_);
std::vector<int64_t> starts = {0, 0};
std::vector<int64_t> ends = {-1, -1};
std::vector<int64_t> axes = {2, 3};
std::vector<int64_t> steps = {1, 1};

std::string reshaped_input = helper_->Reshape(input_info[0].name, new_shape);

std::vector<int64_t> starts_i64(starts.begin(), starts.end());
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里重新copy构造的原因是什么?

std::vector<int64_t> ends_i64(ends.begin(), ends.end());
std::vector<int64_t> axes_i64(axes.begin(), axes.end());
std::vector<int64_t> steps_i64(steps.begin(), steps.end());
std::string sliced_input = helper_->Slice(reshaped_input, starts_i64, ends_i64, axes_i64);
helper_->MakeNode("Identity", {sliced_input}, {output_info[0].name});
}

}
44 changes: 44 additions & 0 deletions paddle2onnx/mapper/nn/im2sequence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <string>
#include <vector>

#include "paddle2onnx/mapper/mapper.h"

namespace paddle2onnx
{

class Im2SequenceMapper : public Mapper
{
public:
Im2SequenceMapper(const PaddleParser &p, OnnxHelper *helper, int64_t block_id,
int64_t op_id) : Mapper(p, helper, block_id, op_id)
{
GetAttr("kernels", &kernels_);
GetAttr("out_stride", &out_stride_);
GetAttr("paddings", &paddings_);
GetAttr("strides", &strides_);
}
int32_t GetMinOpset(bool verbose = false);
void Opset7();

private:
std::vector<int64_t> kernels_;
std::vector<int64_t> out_stride_;
std::vector<int64_t> paddings_;
std::vector<int64_t> strides_;
};
}
1 change: 1 addition & 0 deletions paddle2onnx/mappers_registry.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ USE_P2O_MAPPER(layer_norm, LayerNormMapper)
USE_P2O_MAPPER(rnn, RnnMapper)
USE_P2O_MAPPER(softmax_with_cross_entropy, SoftmaxCrossEntropyLossMapper)
USE_P2O_MAPPER(affine_channel, AffineChannelMapper)
USE_P2O_MAPPER(im2sequence, RnnMapper)
USE_P2O_MAPPER(conv3d, Conv3dMapper)
USE_P2O_MAPPER(conv2d_transpose, Conv2dTransposeMapper)
USE_P2O_MAPPER(depthwise_conv2d_transpose, Conv2dTransposeMapper)
Expand Down