Skip to content

Commit 83f32ee

Browse files
jerryzh168facebook-github-bot
authored andcommitted
Tensor construction codemod - 2/3 (pytorch#14836)
Summary: Pull Request resolved: pytorch#14836 Codemod generated with clangr shard mode, 25 files per diff, motivation: pytorch#12407 Reviewed By: bddppq Differential Revision: D13335176 fbshipit-source-id: 8d89510670e2cf70559d2f75e68f7181feb0b6d9
1 parent 5222a1b commit 83f32ee

13 files changed

+46
-37
lines changed

caffe2/core/operator.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class CAFFE2_API OperatorBase : public Observable<OperatorBase> {
207207
int idx,
208208
at::TensorOptions options,
209209
const Tensor& src,
210-
BaseContext* context = nullptr) {
210+
bool async = false) {
211211
Tensor* t = Output<Tensor>(idx, options.device().type());
212212
// TODO:
213213
// We plan to use the following:
@@ -216,7 +216,7 @@ class CAFFE2_API OperatorBase : public Observable<OperatorBase> {
216216
CAFFE_ENFORCE(
217217
!t->dtype_initialized() || t->dtype() == src.dtype(),
218218
"We don't allow a change of data type in OutputTensor");
219-
t->CopyFrom(src, context);
219+
t->CopyFrom(src, async);
220220
return t;
221221
}
222222

caffe2/ideep/operators/concat_split_op.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class IDEEPConcatOp final : public IDEEPOperator {
2626

2727
bool RunOnDevice() override {
2828
auto* output = Output(OUTPUT);
29-
TensorCPU* axis_info = OperatorBase::Output<TensorCPU>(AXIS_INFO, CPU);
3029

3130
vector<itensor> inputs;
3231
for (int i = 0; i < InputSize(); ++i) {
@@ -44,7 +43,10 @@ class IDEEPConcatOp final : public IDEEPOperator {
4443
}
4544

4645
auto axis_vdata = ideep::concat::compute(inputs, axis_, add_axis_, *output);
47-
axis_info->Resize(vector<int64_t>(1, InputSize()));
46+
Tensor* axis_info = OutputTensor(
47+
AXIS_INFO,
48+
vector<int64_t>(1, InputSize()),
49+
at::dtype<int>().device(CPU));
4850
int* axis_data = axis_info->template mutable_data<int>();
4951
for (int i = 0; i < axis_vdata.size(); i++) {
5052
axis_data[i] = axis_vdata[i];

caffe2/ideep/operators/utility_ops.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ class CopyIDEEPToCPUOp final : public IDEEPOperator {
4949
if (BlobIsTensorType(input_blob, CPU)) {
5050
VLOG(2) << "Directing sharing of TensorCPU";
5151
const auto& X = OperatorBase::Input<Tensor>(0, CPU);
52-
auto* Y = OperatorBase::Output<Tensor>(0, CPU);
53-
Y->CopyFrom(X);
52+
OutputTensorCopyFrom(0, at::device(CPU), X);
5453
} else {
5554
const auto& X = OperatorBase::Input<itensor>(0);
56-
auto* Y = OperatorBase::Output<Tensor>(0, CPU);
57-
Y->Resize(X.get_dims());
5855
if (X.get_data_type() == itensor::data_type::f32) {
56+
std::vector<int64_t> dims;
57+
for (int i = 0; i < X.get_dims().size(); ++i) {
58+
dims.push_back(X.get_dims()[i]);
59+
}
60+
auto* Y =
61+
OperatorBase::OutputTensor(0, dims, at::dtype<float>().device(CPU));
5962
X.reorder_to(Y->template mutable_data<float>());
6063
} else {
6164
CAFFE_THROW("Unsupported ideep type: ", X.get_data_type());

caffe2/operators/conv_op_cudnn.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,7 @@ bool CudnnConvGradientOp::DoRunWithType() {
13061306

13071307
// Now, actually run the computation.
13081308
if (!no_bias_) {
1309-
auto* dbias = Output(BIAS_OR_INPUT_GRAD);
1310-
dbias->Resize(M);
1309+
auto* dbias = Output(BIAS_OR_INPUT_GRAD, {M}, at::dtype<T_DB>());
13111310
CUDNN_ENFORCE(cudnnConvolutionBackwardBias(
13121311
cudnn_wrapper_.inline_cudnn_handle(),
13131312
cudnnTypeWrapper<T_DY>::kOne(),

caffe2/operators/conv_transpose_op_cudnn.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,7 @@ bool CudnnConvTransposeGradientOp<T>::RunOnDevice() {
615615

616616
// Now, actually run the computation.
617617
if (!no_bias_) {
618-
auto* dbias = Output(BIAS_OR_INPUT_GRAD);
619-
dbias->Resize(C);
618+
auto* dbias = Output(BIAS_OR_INPUT_GRAD, {C}, at::dtype<T>());
620619
CUDNN_ENFORCE(cudnnConvolutionBackwardBias(
621620
cudnn_wrapper_.inline_cudnn_handle(),
622621
cudnnTypeWrapper<T>::kOne(),

caffe2/operators/dropout_op_cudnn.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CuDNNDropoutOp final : public Operator<CUDAContext> {
5555
cudnnTensorDescriptor_t data_desc_;
5656
cudnnDropoutDescriptor_t dropout_desc_;
5757

58-
vector<int64_t> cudnn_input_dims_;
58+
at::IntList cudnn_input_dims_;
5959

6060
float ratio_;
6161
bool is_test_;
@@ -113,7 +113,7 @@ class CuDNNDropoutGradientOp final : public Operator<CUDAContext> {
113113
cudnnTensorDescriptor_t data_desc_;
114114
cudnnDropoutDescriptor_t dropout_desc_;
115115

116-
vector<int64_t> cudnn_input_dims_;
116+
at::IntList cudnn_input_dims_;
117117

118118
Blob* scratch_blob_;
119119

@@ -146,12 +146,11 @@ bool CuDNNDropoutOp::DoRunWithType() {
146146
}
147147
return true;
148148
} else {
149-
auto* mask = Output(1);
150149
// Reshape tensor descriptors if necessary
151-
if (X.sizes() != cudnn_input_dims_ && !is_test_) {
150+
if (X.sizes() != cudnn_input_dims_) {
152151
CAFFE_ENFORCE(scratch_blob_);
153152
Tensor* states = BlobGetMutableTensor(scratch_blob_, CUDA);
154-
cudnn_input_dims_ = X.sizes().vec();
153+
cudnn_input_dims_ = X.sizes();
155154
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
156155
data_desc_,
157156
GetCudnnTensorFormat(StorageOrder::NCHW),
@@ -165,7 +164,6 @@ bool CuDNNDropoutOp::DoRunWithType() {
165164
CUDNN_ENFORCE(cudnnDropoutGetReserveSpaceSize(
166165
data_desc_, &reserve_space_size_in_bytes_));
167166

168-
mask->Resize(reserve_space_size_in_bytes_);
169167
states->Resize(states_size_in_bytes_);
170168

171169
if (!states_initialized_) {
@@ -187,6 +185,10 @@ bool CuDNNDropoutOp::DoRunWithType() {
187185
states_initialized_ = true;
188186
}
189187
}
188+
auto* mask = Output(
189+
1,
190+
{static_cast<int64_t>(reserve_space_size_in_bytes_)},
191+
at::dtype<uint8_t>());
190192
CUDNN_ENFORCE(cudnnDropoutForward(
191193
cudnn_wrapper_.inline_cudnn_handle(),
192194
dropout_desc_,
@@ -244,7 +246,7 @@ bool CuDNNDropoutGradientOp::DoRunWithType() {
244246
}
245247

246248
if (dY.sizes() != cudnn_input_dims_) {
247-
cudnn_input_dims_ = dY.sizes().vec();
249+
cudnn_input_dims_ = dY.sizes();
248250
CUDNN_ENFORCE(cudnnSetTensor4dDescriptor(
249251
data_desc_,
250252
GetCudnnTensorFormat(StorageOrder::NCHW),

caffe2/operators/fully_connected_op.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ class FullyConnectedGradientOp : public Operator<Context> {
208208
CAFFE_ENFORCE(K * N == W.numel(), dimErrorString());
209209

210210
auto* dW = Output(0);
211-
auto* db = Output(1);
211+
212212
dW->ResizeLike(W);
213-
db->Resize(N);
213+
auto* db = Output(1, {N}, at::dtype<T_DB>());
214214

215215
if (X.numel() == 0) {
216216
// generate a zero blob for db and dW when X is empty

caffe2/operators/fused_rowwise_8bit_conversion_ops.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class Fused8BitRowwiseQuantizedToFloatOp : public Operator<Context> {
106106
CAFFE_ENFORCE(IS_LITTLE_ENDIAN, "Unsupported endianness");
107107

108108
const auto& input = Input(DATA_FUSED_SCALE_BIAS_INT8);
109-
auto* output = Output(DATA_FLOAT);
110109

111110
const auto input_rows = input.size(0);
112111
const auto input_columns = input.size(1);
@@ -116,7 +115,7 @@ class Fused8BitRowwiseQuantizedToFloatOp : public Operator<Context> {
116115
// input_columns is the number of values in the original row.
117116
const std::vector<int64_t> output_dimensions = {input_rows,
118117
input_columns - 8};
119-
output->Resize(output_dimensions);
118+
auto* output = Output(DATA_FLOAT, output_dimensions, at::dtype<T>());
120119
const auto output_columns = output->size(1);
121120

122121
const auto* input_data = input.template data<uint8_t>();

caffe2/operators/order_switch_ops_cudnn.cc

+9-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CuDNNOrderSwithOpBase : public Operator<CUDAContext> {
2828
}
2929

3030
protected:
31+
// TODO: std::vector<int> -> std::vector<int64_t>
3132
void SetTensorDescriptor(
3233
const cudnnDataType_t data_type,
3334
const StorageOrder order,
@@ -82,7 +83,7 @@ class CuDNNNHWC2NCHWOp final : public CuDNNOrderSwithOpBase {
8283
template <typename T>
8384
bool DoRunWithType() {
8485
const auto& X = Input(0);
85-
auto* Y = Output(0);
86+
8687
const int ndim = X.dim();
8788
const int N = X.dim32(0);
8889
const int C = X.dim32(ndim - 1);
@@ -91,7 +92,9 @@ class CuDNNNHWC2NCHWOp final : public CuDNNOrderSwithOpBase {
9192
Y_dims[0] = N;
9293
Y_dims[1] = C;
9394
std::copy(X_dims.cbegin() + 1, X_dims.cend() - 1, Y_dims.begin() + 2);
94-
Y->Resize(Y_dims);
95+
std::vector<int64_t> Y_dims_64;
96+
std::copy(Y_dims.cbegin(), Y_dims.cend(), std::back_inserter(Y_dims_64));
97+
auto* Y = Output(0, Y_dims_64, at::dtype<T>());
9598
if (cached_X_dims_ != X_dims) {
9699
cached_X_dims_ = X_dims;
97100
SetTensorDescriptor(
@@ -123,7 +126,7 @@ class CuDNNNCHW2NHWCOp final : public CuDNNOrderSwithOpBase {
123126
template <typename T>
124127
bool DoRunWithType() {
125128
const auto& X = Input(0);
126-
auto* Y = Output(0);
129+
127130
const int ndim = X.dim();
128131
const int N = X.dim32(0);
129132
const int C = X.dim32(1);
@@ -132,7 +135,9 @@ class CuDNNNCHW2NHWCOp final : public CuDNNOrderSwithOpBase {
132135
Y_dims[0] = N;
133136
Y_dims[ndim - 1] = C;
134137
std::copy(X_dims.cbegin() + 2, X_dims.cend(), Y_dims.begin() + 1);
135-
Y->Resize(Y_dims);
138+
std::vector<int64_t> Y_dims_64;
139+
std::copy(Y_dims.cbegin(), Y_dims.cend(), std::back_inserter(Y_dims_64));
140+
auto* Y = Output(0, Y_dims_64, at::dtype<T>());
136141
if (cached_X_dims_ != X_dims) {
137142
cached_X_dims_ = X_dims;
138143
SetTensorDescriptor(

caffe2/quantization/server/fully_connected_fake_lowp_op.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ bool FullyConnectedFakeLowpFPOp<Q, Context, Engine, TransposeWeight>::
3333
const auto& X = Input(0);
3434
const auto& W = Input(1);
3535
const auto& b = Input(2);
36-
auto* Y = Output(0);
36+
3737
CAFFE_ENFORCE(b.dim() == 1, b.dim());
3838
// batch size
3939
const auto canonical_axis = X.canonical_axis_index(axis_);
@@ -79,7 +79,7 @@ bool FullyConnectedFakeLowpFPOp<Q, Context, Engine, TransposeWeight>::
7979
DCHECK_LE(canonical_axis + 1, Y_shape_cache_.size());
8080
Y_shape_cache_.resize(canonical_axis + 1);
8181
Y_shape_cache_[canonical_axis] = N;
82-
Y->Resize(Y_shape_cache_);
82+
auto* Y = Output(0, Y_shape_cache_, at::dtype<T_Y>());
8383
CAFFE_ENFORCE(M * N == Y->size(), dimErrorString());
8484

8585
if (X.size() == 0) {
@@ -180,9 +180,9 @@ bool FullyConnectedGradientFakeLowpFPOp<Q, Context, Engine, TransposeWeight>::
180180
CAFFE_ENFORCE(K * N == W.size());
181181

182182
auto* dW = Output(0);
183-
auto* db = Output(1);
183+
184184
dW->ResizeLike(W);
185-
db->Resize(N);
185+
auto* db = Output(1, {N}, at::dtype<T_DB>());
186186

187187
if (X.size() == 0) {
188188
// generate a zero blob for db and dW when X is empty

caffe2/queue/queue_ops.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ class WeightedSampleDequeueBlobsOp final : public Operator<Context> {
244244
CAFFE_ENFORCE_EQ(OutputSize(), size + 1);
245245
bool status = queue->blockingRead(this->Outputs());
246246
if (table_idx_blob_ >= 0) {
247-
auto* table_idx_blob_out = Output(table_idx_blob_);
248-
table_idx_blob_out->Resize(1);
247+
auto* table_idx_blob_out =
248+
Output(table_idx_blob_, {1}, at::dtype<int32_t>());
249249
int32_t* data = table_idx_blob_out->template mutable_data<int32_t>();
250250
data[0] = idx;
251251
}

caffe2/sgd/iter_op.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ class IterOp final : public Operator<Context> {
4545
"be deprecated soon. More specifically, IterOp now "
4646
"requires an explicit in-place input and output.";
4747

48-
auto* output = OperatorBase::Output<Tensor>(0, CPU);
4948
VLOG(1) << "Initializing iter counter.";
50-
output->Resize(1);
49+
auto* output = OperatorBase::OutputTensor(
50+
0, {1}, at::dtype<int64_t>().device(CPU));
5151
output->template mutable_data<int64_t>()[0] = 0;
5252
}
5353
}

caffe2/sgd/lars_op.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class LarsOp final : public Operator<Context> {
2828
auto& wd = Input(2);
2929
auto& trust = Input(3);
3030
auto& lr_max = Input(4);
31-
auto* lr_rescaled = Output(0);
32-
lr_rescaled->Resize(vector<int64_t>{1});
31+
32+
auto* lr_rescaled = Output(0, vector<int64_t>{1}, at::dtype<T>());
3333

3434
X_norm_tensor_.Resize(1);
3535
T* X_norm_ = X_norm_tensor_.template mutable_data<T>();

0 commit comments

Comments
 (0)