mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/66742 Modified loops in files under fbsource/fbcode/caffe2/ from the format `for(TYPE var=x0;var<x_max;x++)` to the format `for(const auto var: irange(xmax))` This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand. Test Plan: Sandcastle Reviewed By: malfet Differential Revision: D31705366 fbshipit-source-id: be58222426c192406a7f93c21582c3f6f2082401
93 lines
2.1 KiB
C++
93 lines
2.1 KiB
C++
#ifndef CAFFE2_OPERATORS_ORDER_SWITCH_OPS_H_
|
|
#define CAFFE2_OPERATORS_ORDER_SWITCH_OPS_H_
|
|
|
|
#include "caffe2/core/operator.h"
|
|
#include "caffe2/utils/math.h"
|
|
#include <c10/util/irange.h>
|
|
|
|
#include <vector>
|
|
|
|
namespace caffe2 {
|
|
|
|
// Note(Yangqing): I think it is possible to do a more general swapaxes operator
|
|
// but I am a little afraid of going down that general path. Only implementing
|
|
// the two actually needed ones here.
|
|
|
|
template <typename T, class Context>
|
|
class NHWC2NCHWOp final : public Operator<Context> {
|
|
public:
|
|
USE_OPERATOR_CONTEXT_FUNCTIONS;
|
|
|
|
USE_SIMPLE_CTOR_DTOR(NHWC2NCHWOp);
|
|
|
|
bool RunOnDevice() override {
|
|
const auto& X = Input(0);
|
|
|
|
const int ndim = X.dim();
|
|
CAFFE_ENFORCE_GE(ndim, 3);
|
|
const int N = X.dim32(0);
|
|
const int C = X.dim32(ndim - 1);
|
|
std::vector<int64_t> Y_dims(ndim);
|
|
Y_dims[0] = N;
|
|
Y_dims[1] = C;
|
|
int HxW = 1;
|
|
for (const auto i : c10::irange(2, ndim)) {
|
|
Y_dims[i] = X.dim32(i - 1);
|
|
HxW *= Y_dims[i];
|
|
}
|
|
auto* Y = Output(0, Y_dims, at::dtype<T>());
|
|
if (X.numel() <= 0) {
|
|
return true;
|
|
}
|
|
math::NHWC2NCHW<T, Context>(
|
|
N,
|
|
C,
|
|
HxW,
|
|
X.template data<T>(),
|
|
Y->template mutable_data<T>(),
|
|
&context_);
|
|
return true;
|
|
}
|
|
};
|
|
|
|
template <typename T, class Context>
|
|
class NCHW2NHWCOp final : public Operator<Context> {
|
|
public:
|
|
USE_OPERATOR_CONTEXT_FUNCTIONS;
|
|
|
|
USE_SIMPLE_CTOR_DTOR(NCHW2NHWCOp);
|
|
|
|
bool RunOnDevice() override {
|
|
const auto& X = Input(0);
|
|
|
|
const int ndim = X.dim();
|
|
CAFFE_ENFORCE_GE(ndim, 3);
|
|
const int N = X.dim32(0);
|
|
const int C = X.dim32(1);
|
|
std::vector<int64_t> Y_dims(ndim);
|
|
Y_dims[0] = N;
|
|
Y_dims[ndim - 1] = C;
|
|
int HxW = 1;
|
|
for (int i = 1; i < ndim - 1; ++i) {
|
|
Y_dims[i] = X.dim32(i + 1);
|
|
HxW *= Y_dims[i];
|
|
}
|
|
auto* Y = Output(0, Y_dims, at::dtype<T>());
|
|
if (X.numel() <= 0) {
|
|
return true;
|
|
}
|
|
math::NCHW2NHWC<T, Context>(
|
|
N,
|
|
C,
|
|
HxW,
|
|
X.template data<T>(),
|
|
Y->template mutable_data<T>(),
|
|
&context_);
|
|
return true;
|
|
}
|
|
};
|
|
|
|
} // namespace caffe2
|
|
|
|
#endif // CAFFE2_OPERATORS_ORDER_SWITCH_OPS_H_
|