mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 00:20:18 +01:00
Summary: *Context:* https://github.com/pytorch/pytorch/issues/53406 added a lint for trailing whitespace at the ends of lines. However, in order to pass FB-internal lints, that PR also had to normalize the trailing newlines in four of the files it touched. This PR adds an OSS lint to normalize trailing newlines. The changes to the following files (made in 54847d0adb9be71be4979cead3d9d4c02160e4cd) are the only manually-written parts of this PR: - `.github/workflows/lint.yml` - `mypy-strict.ini` - `tools/README.md` - `tools/test/test_trailing_newlines.py` - `tools/trailing_newlines.py` I would have liked to make this just a shell one-liner like the other three similar lints, but nothing I could find quite fit the bill. Specifically, all the answers I tried from the following Stack Overflow questions were far too slow (at least a minute and a half to run on this entire repository): - [How to detect file ends in newline?](https://stackoverflow.com/q/38746) - [How do I find files that do not end with a newline/linefeed?](https://stackoverflow.com/q/4631068) - [How to list all files in the Git index without newline at end of file](https://stackoverflow.com/q/27624800) - [Linux - check if there is an empty line at the end of a file [duplicate]](https://stackoverflow.com/q/34943632) - [git ensure newline at end of each file](https://stackoverflow.com/q/57770972) To avoid giving false positives during the few days after this PR is merged, we should probably only merge it after https://github.com/pytorch/pytorch/issues/54967. Pull Request resolved: https://github.com/pytorch/pytorch/pull/54737 Test Plan: Running the shell script from the "Ensure correct trailing newlines" step in the `quick-checks` job of `.github/workflows/lint.yml` should print no output and exit in a fraction of a second with a status of 0. That was not the case prior to this PR, as shown by this failing GHA workflow run on an earlier draft of this PR: - https://github.com/pytorch/pytorch/runs/2197446987?check_suite_focus=true In contrast, this run (after correcting the trailing newlines in this PR) succeeded: - https://github.com/pytorch/pytorch/pull/54737/checks?check_run_id=2197553241 To unit-test `tools/trailing_newlines.py` itself (this is run as part of our "Test tools" GitHub Actions workflow): ``` python tools/test/test_trailing_newlines.py ``` Reviewed By: malfet Differential Revision: D27409736 Pulled By: samestep fbshipit-source-id: 46f565227046b39f68349bbd5633105b2d2e9b19
315 lines
10 KiB
C++
315 lines
10 KiB
C++
#include "caffe2/operators/roi_align_op.h"
|
|
|
|
#include <vector>
|
|
|
|
#include "caffe2/utils/eigen_utils.h"
|
|
#include "caffe2/utils/math.h"
|
|
|
|
namespace caffe2 {
|
|
|
|
namespace {
|
|
|
|
template <typename T>
|
|
struct BilinearInterpolationParam {
|
|
int64_t p1;
|
|
int64_t p2;
|
|
int64_t p3;
|
|
int64_t p4;
|
|
T w1;
|
|
T w2;
|
|
T w3;
|
|
T w4;
|
|
};
|
|
|
|
template <typename T>
|
|
std::vector<BilinearInterpolationParam<T>> MakeBilinearInterpolationParams(
|
|
int64_t H,
|
|
int64_t W,
|
|
int64_t pooled_h,
|
|
int64_t pooled_w,
|
|
T bin_size_h,
|
|
T bin_size_w,
|
|
int64_t bin_grid_h,
|
|
int64_t bin_grid_w,
|
|
T roi_start_h,
|
|
T roi_start_w) {
|
|
std::vector<BilinearInterpolationParam<T>> params(
|
|
pooled_h * pooled_w * bin_grid_h * bin_grid_w);
|
|
const T ch = bin_size_h / static_cast<T>(bin_grid_h);
|
|
const T cw = bin_size_w / static_cast<T>(bin_grid_w);
|
|
int64_t cnt = 0;
|
|
for (int64_t ph = 0; ph < pooled_h; ++ph) {
|
|
for (int64_t pw = 0; pw < pooled_w; ++pw) {
|
|
for (int64_t iy = 0; iy < bin_grid_h; ++iy) {
|
|
const T yy = roi_start_h + static_cast<T>(ph) * bin_size_h +
|
|
(static_cast<T>(iy) + T(0.5)) * ch;
|
|
if (yy < T(-1) || yy > static_cast<T>(H)) {
|
|
std::memset(params.data() + cnt, 0, bin_grid_w * sizeof(params[0]));
|
|
cnt += bin_grid_w;
|
|
continue;
|
|
}
|
|
for (int64_t ix = 0; ix < bin_grid_w; ++ix) {
|
|
const T xx = roi_start_w + pw * bin_size_w +
|
|
(static_cast<T>(ix) + T(0.5f)) * cw;
|
|
BilinearInterpolationParam<T>& param = params[cnt++];
|
|
if (xx < T(-1) || xx > static_cast<T>(W)) {
|
|
std::memset(¶m, 0, sizeof(param));
|
|
continue;
|
|
}
|
|
const T y = std::min(std::max(yy, T(0)), static_cast<T>(H - 1));
|
|
const T x = std::min(std::max(xx, T(0)), static_cast<T>(W - 1));
|
|
const int64_t yl = static_cast<int64_t>(std::floor(y));
|
|
const int64_t xl = static_cast<int64_t>(std::floor(x));
|
|
const int64_t yh = std::min(yl + 1, H - 1);
|
|
const int64_t xh = std::min(xl + 1, W - 1);
|
|
const T py = y - static_cast<T>(yl);
|
|
const T px = x - static_cast<T>(xl);
|
|
const T qy = T(1) - py;
|
|
const T qx = T(1) - px;
|
|
param.p1 = yl * W + xl;
|
|
param.p2 = yl * W + xh;
|
|
param.p3 = yh * W + xl;
|
|
param.p4 = yh * W + xh;
|
|
param.w1 = qy * qx;
|
|
param.w2 = qy * px;
|
|
param.w3 = py * qx;
|
|
param.w4 = py * px;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return params;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
template <>
|
|
C10_EXPORT bool RoIAlignOp<float, CPUContext>::RunOnDeviceWithOrderNCHW(
|
|
int64_t N,
|
|
int64_t C,
|
|
int64_t H,
|
|
int64_t W,
|
|
int64_t roi_cols,
|
|
const float* X,
|
|
const float* R,
|
|
float* Y) {
|
|
DCHECK(roi_cols == 4 || roi_cols == 5);
|
|
const float roi_offset = aligned_ ? 0.5f : 0.0f;
|
|
|
|
#ifdef _OPENMP
|
|
#pragma omp parallel for
|
|
#endif
|
|
for (int64_t n = 0; n < N; ++n) {
|
|
const int64_t roi_batch_idx = roi_cols == 4 ? 0 : R[n * roi_cols];
|
|
const float* X_ptr = X + roi_batch_idx * C * H * W;
|
|
const float* R_ptr = R + n * roi_cols + (roi_cols == 5);
|
|
float* Y_ptr = Y + n * C * pooled_h_ * pooled_w_;
|
|
|
|
// Do not using rounding; this implementation detail is critical
|
|
const float roi_w1 = R_ptr[0] * spatial_scale_ - roi_offset;
|
|
const float roi_h1 = R_ptr[1] * spatial_scale_ - roi_offset;
|
|
const float roi_w2 = R_ptr[2] * spatial_scale_ - roi_offset;
|
|
const float roi_h2 = R_ptr[3] * spatial_scale_ - roi_offset;
|
|
float roi_w = roi_w2 - roi_w1;
|
|
float roi_h = roi_h2 - roi_h1;
|
|
if (aligned_) {
|
|
CAFFE_ENFORCE(
|
|
roi_w >= 0.0f && roi_h >= 0.0f,
|
|
"ROIs in ROIAlign do not have non-negative size!");
|
|
} else { // backward compatibility
|
|
// Force malformed ROIs to be 1x1
|
|
roi_w = std::max(roi_w, 1.0f);
|
|
roi_h = std::max(roi_h, 1.0f);
|
|
}
|
|
const float bin_size_h = roi_h / static_cast<float>(pooled_h_);
|
|
const float bin_size_w = roi_w / static_cast<float>(pooled_w_);
|
|
|
|
// We use roi_bin_grid to sample the grid and mimic integral
|
|
const int64_t bin_grid_h = (sampling_ratio_ > 0)
|
|
? sampling_ratio_
|
|
: static_cast<int64_t>(ceil(roi_h / static_cast<float>(pooled_h_)));
|
|
const int64_t bin_grid_w = (sampling_ratio_ > 0)
|
|
? sampling_ratio_
|
|
: static_cast<int64_t>(ceil(roi_w / static_cast<float>(pooled_w_)));
|
|
|
|
const std::vector<BilinearInterpolationParam<float>> params =
|
|
MakeBilinearInterpolationParams(
|
|
H,
|
|
W,
|
|
pooled_h_,
|
|
pooled_w_,
|
|
bin_size_h,
|
|
bin_size_w,
|
|
bin_grid_h,
|
|
bin_grid_w,
|
|
roi_h1,
|
|
roi_w1);
|
|
|
|
const float scale = 1.0f / static_cast<float>(bin_grid_h * bin_grid_w);
|
|
for (int64_t c = 0; c < C; ++c) {
|
|
int64_t cnt = 0;
|
|
for (int64_t ph = 0; ph < pooled_h_; ++ph) {
|
|
for (int64_t pw = 0; pw < pooled_w_; ++pw) {
|
|
float sum = 0.0f;
|
|
for (int64_t iy = 0; iy < bin_grid_h; ++iy) {
|
|
for (int64_t ix = 0; ix < bin_grid_w; ++ix) {
|
|
const BilinearInterpolationParam<float>& param = params[cnt++];
|
|
sum += param.w1 * X_ptr[param.p1] + param.w2 * X_ptr[param.p2] +
|
|
param.w3 * X_ptr[param.p3] + param.w4 * X_ptr[param.p4];
|
|
}
|
|
}
|
|
Y_ptr[ph * pooled_w_ + pw] = sum * scale;
|
|
}
|
|
}
|
|
X_ptr += H * W;
|
|
Y_ptr += pooled_h_ * pooled_w_;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
template <>
|
|
C10_EXPORT bool RoIAlignOp<float, CPUContext>::RunOnDeviceWithOrderNHWC(
|
|
int64_t N,
|
|
int64_t C,
|
|
int64_t H,
|
|
int64_t W,
|
|
int64_t roi_cols,
|
|
const float* X,
|
|
const float* R,
|
|
float* Y) {
|
|
DCHECK(roi_cols == 4 || roi_cols == 5);
|
|
const float roi_offset = aligned_ ? 0.5f : 0.0f;
|
|
|
|
#ifdef _OPENMP
|
|
#pragma omp parallel for
|
|
#endif
|
|
for (int64_t n = 0; n < N; ++n) {
|
|
const int64_t roi_batch_idx = roi_cols == 4 ? 0 : R[n * roi_cols];
|
|
const float* X_ptr = X + roi_batch_idx * C * H * W;
|
|
const float* R_ptr = R + n * roi_cols + (roi_cols == 5);
|
|
float* Y_ptr = Y + n * C * pooled_h_ * pooled_w_;
|
|
|
|
// Do not using rounding; this implementation detail is critical
|
|
const float roi_w1 = R_ptr[0] * spatial_scale_ - roi_offset;
|
|
const float roi_h1 = R_ptr[1] * spatial_scale_ - roi_offset;
|
|
const float roi_w2 = R_ptr[2] * spatial_scale_ - roi_offset;
|
|
const float roi_h2 = R_ptr[3] * spatial_scale_ - roi_offset;
|
|
float roi_w = roi_w2 - roi_w1;
|
|
float roi_h = roi_h2 - roi_h1;
|
|
if (aligned_) {
|
|
CAFFE_ENFORCE(
|
|
roi_w >= 0.0f && roi_h >= 0.0f,
|
|
"ROIs in ROIAlign do not have non-negative size!");
|
|
} else { // backward compatibility
|
|
// Force malformed ROIs to be 1x1
|
|
roi_w = std::max(roi_w, 1.0f);
|
|
roi_h = std::max(roi_h, 1.0f);
|
|
}
|
|
const float bin_size_h = roi_h / static_cast<float>(pooled_h_);
|
|
const float bin_size_w = roi_w / static_cast<float>(pooled_w_);
|
|
|
|
// We use roi_bin_grid to sample the grid and mimic integral
|
|
const int64_t bin_grid_h = (sampling_ratio_ > 0)
|
|
? sampling_ratio_
|
|
: static_cast<int64_t>(ceil(roi_h / static_cast<float>(pooled_h_)));
|
|
const int64_t bin_grid_w = (sampling_ratio_ > 0)
|
|
? sampling_ratio_
|
|
: static_cast<int64_t>(ceil(roi_w / static_cast<float>(pooled_w_)));
|
|
|
|
const std::vector<BilinearInterpolationParam<float>> params =
|
|
MakeBilinearInterpolationParams(
|
|
H,
|
|
W,
|
|
pooled_h_,
|
|
pooled_w_,
|
|
bin_size_h,
|
|
bin_size_w,
|
|
bin_grid_h,
|
|
bin_grid_w,
|
|
roi_h1,
|
|
roi_w1);
|
|
|
|
const float scale = 1.0f / static_cast<float>(bin_grid_h * bin_grid_w);
|
|
int64_t cnt = 0;
|
|
for (int64_t ph = 0; ph < pooled_h_; ++ph) {
|
|
for (int64_t pw = 0; pw < pooled_w_; ++pw) {
|
|
EigenVectorArrayMap<float> Y_arr(Y_ptr + (ph * pooled_w_ + pw) * C, C);
|
|
Y_arr.setZero();
|
|
for (int64_t iy = 0; iy < bin_grid_h; ++iy) {
|
|
for (int64_t ix = 0; ix < bin_grid_w; ++ix) {
|
|
const BilinearInterpolationParam<float>& param = params[cnt++];
|
|
ConstEigenVectorArrayMap<float> x1_arr(X_ptr + param.p1 * C, C);
|
|
ConstEigenVectorArrayMap<float> x2_arr(X_ptr + param.p2 * C, C);
|
|
ConstEigenVectorArrayMap<float> x3_arr(X_ptr + param.p3 * C, C);
|
|
ConstEigenVectorArrayMap<float> x4_arr(X_ptr + param.p4 * C, C);
|
|
Y_arr += param.w1 * x1_arr + param.w2 * x2_arr + param.w3 * x3_arr +
|
|
param.w4 * x4_arr;
|
|
}
|
|
}
|
|
Y_arr *= scale;
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
REGISTER_CPU_OPERATOR(RoIAlign, RoIAlignOp<float, CPUContext>);
|
|
|
|
// Input: X, rois; Output: Y
|
|
OPERATOR_SCHEMA(RoIAlign)
|
|
.NumInputs(2)
|
|
.NumOutputs(1)
|
|
.SetDoc(R"DOC(
|
|
Region of Interest (RoI) align operation as used in Mask R-CNN.
|
|
)DOC")
|
|
.Arg(
|
|
"spatial_scale",
|
|
"(float) default 1.0; Spatial scale of the input feature map X "
|
|
"relative to the input image. E.g., 0.0625 if X has a stride of 16 "
|
|
"w.r.t. the input image.")
|
|
.Arg("pooled_h", "(int) default 1; Pooled output Y's height.")
|
|
.Arg("pooled_w", "(int) default 1; Pooled output Y's width.")
|
|
.Arg(
|
|
"sampling_ratio",
|
|
"(int) default -1; number of sampling points in the interpolation grid "
|
|
"used to compute the output value of each pooled output bin. If > 0, "
|
|
"then exactly sampling_ratio x sampling_ratio grid points are used. If "
|
|
"<= 0, then an adaptive number of grid points are used (computed as "
|
|
"ceil(roi_width / pooled_w), and likewise for height).")
|
|
.Input(0, "X", "4D feature map input of shape (N, C, H, W).")
|
|
.Input(
|
|
1,
|
|
"RoIs",
|
|
"2D input of shape (R, 4 or 5) specifying R RoIs "
|
|
"representing: batch index in [0, N - 1], x1, y1, x2, y2. The RoI "
|
|
"coordinates are in the coordinate system of the input image. For "
|
|
"inputs corresponding to a single image, batch index can be excluded "
|
|
"to have just 4 columns.")
|
|
.Output(
|
|
0,
|
|
"Y",
|
|
"4D output of shape (R, C, pooled_h, pooled_w). The r-th batch element "
|
|
"is a pooled feature map cooresponding to the r-th RoI.");
|
|
|
|
template <typename T>
|
|
using RoIAlignCPUOp = caffe2::RoIAlignOp<T, CPUContext>;
|
|
|
|
} // namespace caffe2
|
|
|
|
C10_EXPORT_CAFFE2_OP_TO_C10_CPU(
|
|
RoIAlign,
|
|
"_caffe2::RoIAlign("
|
|
" Tensor features,"
|
|
" Tensor rois,"
|
|
" str order,"
|
|
" float spatial_scale,"
|
|
" int pooled_h,"
|
|
" int pooled_w,"
|
|
" int sampling_ratio,"
|
|
" bool aligned"
|
|
") -> Tensor",
|
|
caffe2::RoIAlignCPUOp<float>);
|