Skip to content

Commit

Permalink
Revert D13007246: [codemod][caffe2] Tensor construction: combine Resi…
Browse files Browse the repository at this point in the history
…ze+mutable_data - 1/4

Differential Revision:
D13007246

Original commit changeset: 230de42a3843

fbshipit-source-id: 40ce266826f00d320f7215169188ef4ead232660
  • Loading branch information
bddppq authored and facebook-github-bot committed Nov 14, 2018
1 parent 30676bd commit fbd50bb
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 140 deletions.
4 changes: 2 additions & 2 deletions caffe2/operators/accuracy_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ template <>
bool AccuracyOp<float, CPUContext>::RunOnDevice() {
auto& X = Input(PREDICTION);
auto& label = Input(LABEL);

auto* Y = Output(0);
CAFFE_ENFORCE_EQ(X.dim(), 2);
int N = X.dim32(0);
int D = X.dim32(1);
CAFFE_ENFORCE_EQ(label.dim(), 1);
CAFFE_ENFORCE_EQ(label.dim32(0), N);
auto* Y = Output(0, vector<int64_t>(), at::dtype<float>());
Y->Resize(vector<int64_t>());
const auto* Xdata = X.data<float>();
const auto* labelData = label.data<int>();
const int top_k = top_k_;
Expand Down
4 changes: 2 additions & 2 deletions caffe2/operators/apmeter_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ template <>
bool APMeterOp<float, CPUContext>::RunOnDevice() {
auto& X = Input(PREDICTION);
auto& label = Input(LABEL);

auto* Y = Output(0);
// Check dimensions
DCHECK_EQ(X.dim(), 2);
int N = X.dim32(0);
int D = X.dim32(1);
DCHECK_EQ(label.dim(), 2);
DCHECK_EQ(label.dim32(0), N);
DCHECK_EQ(label.dim32(1), D);
auto* Y = Output(0, {D}, at::dtype<float>());
Y->Resize(D);

const auto* Xdata = X.data<float>();
const auto* labelData = label.data<int>();
Expand Down
6 changes: 3 additions & 3 deletions caffe2/operators/arg_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class ArgOp final : public Operator<Context> {
template <typename T>
bool DoRunWithType() {
const auto& X = Input(0);

auto* Y = Output(0);
const int ndim = X.dim();
if (axis_ == -1) {
axis_ = ndim - 1;
}
CAFFE_ENFORCE_GE(axis_, 0);
CAFFE_ENFORCE_LT(axis_, ndim);
const std::vector<int> X_dims(X.sizes().cbegin(), X.sizes().cend());
std::vector<int64_t> Y_dims;
std::vector<int> Y_dims;
Y_dims.reserve(ndim);
int prev_size = 1;
int next_size = 1;
Expand All @@ -53,7 +53,7 @@ class ArgOp final : public Operator<Context> {
Y_dims.push_back(X_dims[i]);
next_size *= X_dims[i];
}
auto* Y = Output(0, Y_dims, at::dtype<int64_t>());
Y->Resize(Y_dims);
const int n = X_dims[axis_];
return reducer_(
prev_size,
Expand Down
4 changes: 2 additions & 2 deletions caffe2/operators/batch_bucketize_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bool BatchBucketizeOp<CPUContext>::RunOnDevice() {
auto& indices = Input(INDICES);
auto& boundaries = Input(BOUNDARIES);
auto& lengths = Input(LENGTHS);

auto* output = Output(O);
CAFFE_ENFORCE_EQ(lengths.dim(), 1);
CAFFE_ENFORCE_EQ(indices.dim(), 1);
CAFFE_ENFORCE_EQ(boundaries.dim(), 1);
Expand All @@ -34,7 +34,7 @@ bool BatchBucketizeOp<CPUContext>::RunOnDevice() {
CAFFE_ENFORCE_EQ(length_sum, boundaries.numel());

int64_t lower_bound = 0;
auto* output = Output(O, {batch_size, output_dim}, at::dtype<int32_t>());
output->Resize(batch_size, output_dim);
auto* output_data = output->template mutable_data<int32_t>();

for (int64_t i = 0; i < batch_size; i++) {
Expand Down
3 changes: 2 additions & 1 deletion caffe2/operators/batch_gather_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ class BatchGatherOp final : public Operator<Context> {
bool DoRunWithType() {
auto& data = Input(DATA);
auto& indices = Input(INDICES);
auto* output = Output(0);

CAFFE_ENFORCE_GE(data.dim(), 2, "DATA should be at least 2-D");

vector<int64_t> shape;
shape.push_back(data.size(0));
shape.insert(shape.end(), indices.sizes().begin(), indices.sizes().end());
shape.insert(shape.end(), data.sizes().begin() + 2, data.sizes().end());
auto* output = Output(0, shape, at::dtype(data.dtype()));
output->Resize(shape);

auto block_size = data.size_from_dim(2);
auto block_bytesize = block_size * data.dtype().itemsize();
Expand Down
5 changes: 3 additions & 2 deletions caffe2/operators/batch_matmul_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class BatchMatMulOp final : public Operator<Context> {
bool DoRunWithType() {
const auto& A = Input(0);
const auto& B = Input(1);
auto* Y = Output(0);

auto ndims_A = A.dim();
auto dims_A = A.sizes().vec();
Expand Down Expand Up @@ -85,7 +86,7 @@ class BatchMatMulOp final : public Operator<Context> {
dims_B[0],
"Vector-vector product requires each of the vectors to "
"be the same size.");
auto* Y = Output(0, {1}, at::dtype<T>());
Y->Resize(1);
math::Dot<T, Context>(
dims_A[0], data_A, data_B, Y->template mutable_data<T>(), &context_);
} else {
Expand Down Expand Up @@ -239,7 +240,7 @@ class BatchMatMulOp final : public Operator<Context> {
}

// Allocate output tensor
auto* Y = Output(0, new_dims, at::dtype<T>());
Y->Resize(new_dims);
auto* Y_data = Y->template mutable_data<T>();

// Zero batch dimension indicates no elements
Expand Down
7 changes: 4 additions & 3 deletions caffe2/operators/batch_moments_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ class BatchMomentsOp final : public Operator<Context> {

bool RunOnDevice() override {
const auto& X = Input(0);

auto* mu = Output(0);
auto* var = Output(1);
const int ndim = X.dim();
const int N = X.dim32(0);
const int C = order_ == StorageOrder::NCHW ? X.dim32(1) : X.dim32(ndim - 1);
const int HxW = X.numel() / (N * C);
auto* mu = Output(0, {C}, at::dtype<T>());
auto* var = Output(1, {C}, at::dtype<T>());
mu->Resize(C);
var->Resize(C);
const T* X_data = X.template data<T>();
T* mu_data = mu->template mutable_data<T>();
T* var_data = var->template mutable_data<T>();
Expand Down
8 changes: 4 additions & 4 deletions caffe2/operators/batch_sparse_to_dense_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bool BatchSparseToDenseOp<T, Context>::RunOnDevice() {
auto& lengths = Input(LENGTHS);
auto& indices = Input(INDICES);
auto& values = Input(VALUES);

auto* output = Output(0);
CAFFE_ENFORCE_EQ(indices.numel(), values.numel());
CAFFE_ENFORCE_EQ(lengths.dim(), 1);
CAFFE_ENFORCE_EQ(indices.dim(), 1);
Expand Down Expand Up @@ -37,7 +37,7 @@ bool BatchSparseToDenseOp<T, Context>::RunOnDevice() {
CAFFE_ENFORCE(dense_last_dim_ >= 1, "The last dim of dense must be >= 1");
}
output_shape.push_back(dense_last_dim_);
auto* output = Output(0, output_shape, at::dtype<T>());
output->Resize(output_shape);
T* output_data = output->template mutable_data<T>();
math::Set(
output->numel(), static_cast<T>(default_value_), output_data, &context_);
Expand Down Expand Up @@ -65,7 +65,7 @@ bool BatchDenseToSparseOp<T, Context>::RunOnDevice() {
auto& lengths = Input(LENGTHS);
auto& indices = Input(INDICES);
auto& dense = Input(DENSE);

auto* output = Output(0);
CAFFE_ENFORCE_EQ(lengths.dim(), 1);
CAFFE_ENFORCE_EQ(indices.dim(), 1);
CAFFE_ENFORCE_EQ(dense.dim(), 2);
Expand All @@ -81,7 +81,7 @@ bool BatchDenseToSparseOp<T, Context>::RunOnDevice() {
CAFFE_ENFORCE_EQ(batch_size, dense.size(0));
dense_last_dim_ = dense.size(1);
vector<int64_t> output_shape = indices.sizes().vec();
auto* output = Output(0, output_shape, at::dtype<T>());
output->Resize(output_shape);
T* output_data = output->template mutable_data<T>();

int64_t k = 0;
Expand Down
3 changes: 2 additions & 1 deletion caffe2/operators/bbox_transform_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ bool BBoxTransformOp<float, CPUContext>::RunOnDevice() {
}

if (OutputSize() > 1) {
auto* roi_batch_splits = Output(1, {batch_size}, at::dtype<float>());
auto* roi_batch_splits = Output(1);
roi_batch_splits->Resize(batch_size);
Eigen::Map<EArrXf> roi_batch_splits_map(
roi_batch_splits->template mutable_data<float>(), batch_size);
roi_batch_splits_map =
Expand Down
3 changes: 2 additions & 1 deletion caffe2/operators/boolean_mask_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ bool BooleanMaskOp<CPUContext>::RunOnDevice() {

int64_t* out_vec = nullptr;
if (OutputSize() == 2) {
auto* indicesOut = Output(1, {numOutputs}, at::dtype<int64_t>());
auto* indicesOut = Output(1);
indicesOut->Resize(numOutputs);
out_vec = indicesOut->template mutable_data<int64_t>();
}

Expand Down
12 changes: 8 additions & 4 deletions caffe2/operators/box_with_nms_limit_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ template <>
bool BoxWithNMSLimitOp<CPUContext>::RunOnDevice() {
const auto& tscores = Input(0);
const auto& tboxes = Input(1);
auto* out_scores = Output(0);
auto* out_boxes = Output(1);
auto* out_classes = Output(2);

const int box_dim = rotated_ ? 5 : 4;

Expand Down Expand Up @@ -48,9 +51,9 @@ bool BoxWithNMSLimitOp<CPUContext>::RunOnDevice() {
Eigen::Map<const EArrXf> batch_splits(batch_splits_data, batch_size);
CAFFE_ENFORCE_EQ(batch_splits.sum(), N);

auto* out_scores = Output(0, {0}, at::dtype<float>());
auto* out_boxes = Output(1, {0, box_dim}, at::dtype<float>());
auto* out_classes = Output(2, {0}, at::dtype<float>());
out_scores->Resize(0);
out_boxes->Resize(0, box_dim);
out_classes->Resize(0);

Tensor* out_keeps = nullptr;
Tensor* out_keeps_size = nullptr;
Expand Down Expand Up @@ -224,7 +227,8 @@ bool BoxWithNMSLimitOp<CPUContext>::RunOnDevice() {
}

if (OutputSize() > 3) {
auto* batch_splits_out = Output(3, {batch_size}, at::dtype<float>());
auto* batch_splits_out = Output(3);
batch_splits_out->Resize(batch_size);
Eigen::Map<EArrXf> batch_splits_out_map(
batch_splits_out->template mutable_data<float>(), batch_size);
batch_splits_out_map =
Expand Down
8 changes: 4 additions & 4 deletions caffe2/operators/byte_weight_dequant_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class ByteWeightDequantOp : public Operator<Context> {
: Operator<Context>(operator_def, ws),
min_(this->template GetSingleArgument<float>("min", -3)),
max_(this->template GetSingleArgument<float>("max", 3)),
shape_(this->template GetRepeatedArgument<int64_t>("shape")) {}
shape_(this->template GetRepeatedArgument<int>("shape")) {}

USE_OPERATOR_FUNCTIONS(Context);
using Operator<Context>::Operator;

bool RunOnDevice() override {
const auto& WI = Input(0);

auto* Y = Output(0, shape_, at::dtype<float>());
auto* Y = Output(0);
Y->Resize(shape_);
float bin_interval = (max_ - min_) / 255.0;
int total = 1;
for (int i = 0; i < shape_.size(); i++) {
Expand All @@ -47,7 +47,7 @@ class ByteWeightDequantOp : public Operator<Context> {
private:
float min_;
float max_;
std::vector<int64_t> shape_;
std::vector<int> shape_;
};

} // namespace caffe2
Expand Down
16 changes: 6 additions & 10 deletions caffe2/operators/collect_and_distribute_fpn_rpn_proposals_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ bool CollectAndDistributeFpnRpnProposalsOp<CPUContext>::RunOnDevice() {
// equivalent to python code
// outputs[0].reshape(rois.shape)
// outputs[0].data[...] = rois

auto* rois_out = Output(0, {rois.rows(), rois.cols()}, at::dtype<float>());
auto* rois_out = Output(0);
rois_out->Resize(rois.rows(), rois.cols());
Eigen::Map<ERArrXXf> rois_out_mat(
rois_out->template mutable_data<float>(), rois.rows(), rois.cols());
rois_out_mat = rois;
Expand All @@ -201,11 +201,8 @@ bool CollectAndDistributeFpnRpnProposalsOp<CPUContext>::RunOnDevice() {
utils::RowsWhereRoILevelEquals(rois, lvls, lvl, &blob_roi_level, &idx_lvl);

// Output blob_roi_level

auto* roi_out = Output(
i + 1,
{blob_roi_level.rows(), blob_roi_level.cols()},
at::dtype<float>());
auto* roi_out = Output(i + 1);
roi_out->Resize(blob_roi_level.rows(), blob_roi_level.cols());
Eigen::Map<ERArrXXf> roi_out_mat(
roi_out->template mutable_data<float>(),
blob_roi_level.rows(),
Expand All @@ -217,9 +214,8 @@ bool CollectAndDistributeFpnRpnProposalsOp<CPUContext>::RunOnDevice() {
rois_idx_restore.tail(idx_lvl.size()) = idx_lvl;
}
utils::ArgSort(rois_idx_restore);

auto* rois_idx_restore_out =
Output(OutputSize() - 1, {rois_idx_restore.size()}, at::dtype<int>());
auto* rois_idx_restore_out = Output(OutputSize() - 1);
rois_idx_restore_out->Resize(rois_idx_restore.size());
Eigen::Map<EArrXi> rois_idx_restore_out_mat(
rois_idx_restore_out->template mutable_data<int>(),
rois_idx_restore.size());
Expand Down
7 changes: 2 additions & 5 deletions caffe2/operators/concat_split_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,8 @@ bool SplitByLengthsOp<Context>::RunOnDevice() {
template <class Context>
bool ConcatOp<Context>::RunOnDevice() {
auto* output = Output(0);

// We can override default options(Context::GetDeviceType())
// by explictly passing in device type we want
Tensor* split = Output(
1, std::vector<int64_t>(1, InputSize()), at::dtype<int>().device(CPU));
Tensor* split = this->template Output<Tensor>(1, CPU);
split->Resize(vector<int64_t>(1, InputSize()));
int* axis_data = split->template mutable_data<int>();
auto& input_zero = Input(0);
int adj_size = input_zero.dim() + (add_axis_ ? 1 : 0);
Expand Down
6 changes: 4 additions & 2 deletions caffe2/operators/conv_op_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ bool ConvGradientOp<T, Context>::RunOnDeviceWithOrderNCHW() {

T* dbias_data = nullptr;
if (!no_bias_) {
auto* dbias = Output(BIAS_OR_INPUT_GRAD, {M}, at::dtype<T>());
auto* dbias = Output(BIAS_OR_INPUT_GRAD);
dbias->Resize(M);
if (bias_multiplier_.numel() != output_image_size) {
// If the helper bias multiplier is not M, reshape and fill it with one.
bias_multiplier_.Resize(vector<int64_t>(1, output_image_size));
Expand Down Expand Up @@ -743,7 +744,8 @@ bool ConvGradientOp<T, Context>::RunOnDeviceWithOrderNHWC() {

T* dbias_data = nullptr;
if (!no_bias_) {
auto* dbias = Output(BIAS_OR_INPUT_GRAD, {M}, at::dtype<T>());
auto* dbias = Output(BIAS_OR_INPUT_GRAD);
dbias->Resize(M);
dbias_data = dbias->template mutable_data<T>();
math::Set<T, Context>(dbias->numel(), 0, dbias_data, &context_);
if (bias_multiplier_.numel() != output_image_size) {
Expand Down
4 changes: 2 additions & 2 deletions caffe2/operators/create_scope_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ by Do operator to store local blobs
template <>
bool HasScopeOp<CPUContext>::RunOnDevice() {
const auto& ws_stack = OperatorBase::Input<detail::WorkspaceStack>(0);

auto* output = Output(0, {1}, at::dtype<bool>());
auto* output = Output(0);
output->Resize(1);
bool* output_value = output->template mutable_data<bool>();
*output_value = !ws_stack.empty();
return true;
Expand Down
3 changes: 2 additions & 1 deletion caffe2/operators/crf_viterbi_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ViterbiPathOp : public Operator<CPUContext> {
bool RunOnDevice() override {
auto& predictions = Input(0);
auto& transitions = Input(1);
auto* viterbiPath = Output(0);

CAFFE_ENFORCE(
predictions.dim() == 2 && transitions.dim() == 2,
Expand All @@ -89,7 +90,7 @@ class ViterbiPathOp : public Operator<CPUContext> {

auto seqLen = predictions.dim32(0);

auto* viterbiPath = Output(0, {seqLen}, at::dtype<int32_t>());
viterbiPath->Resize(seqLen);
auto block_size = predictions.numel() / predictions.size(0);
auto block_bytesize =
predictions.size_from_dim(1) * predictions.dtype().itemsize();
Expand Down
Loading

0 comments on commit fbd50bb

Please sign in to comment.