Skip to content

[pt1][tensor] caffe2::empty for Resize+mutable_data refactor#12407

Closed
jerryzh168 wants to merge 70 commits intomasterfrom
export-D10207890
Closed

[pt1][tensor] caffe2::empty for Resize+mutable_data refactor#12407
jerryzh168 wants to merge 70 commits intomasterfrom
export-D10207890

Conversation

@jerryzh168
Copy link
Contributor

@jerryzh168 jerryzh168 commented Oct 5, 2018

Stack:
    :black_circle:  #12407 [pt1][tensor] caffe2::empty for Resize+mutable_data refactor  💚

Motivation

We want to use tensor factory to refactor the caffe2's old way of initialize Tensor by Resize and mutable_data
in order to eliminate uninitialized Tensor.

Previously when we want to create a Tensor in caffe2, we'll do the following

Tensor x(CPU); // device type provided
x.Resize({1, 2, 3}); // size provided
x.mutable_data<float>(); // data type provided and memory allocated

This leaves Tensor in not fully initialized state during the process, to eliminate this, we
want to provide all the needed information in the begining. ATen already has its TensorFactories: https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/TensorFactories.cpp, and there is a TensorOption, we want to adopt the same interface to ease future refactoring.

In the callsite, we used to have Output(i) that returns a Blob that contains an uninitialized Tensor and we'll call Resize and mutable_data afterwards to provide dimension and data type,

// uninitialized tensor
auto* Y = Output(0);
// set dimensions
Y->Resize({1, 2, 3});
// actually allocate the data
auto* data = Y->mutable_data<float>();
// After this step, Tensor is fully initialized.

We want to change it to the following:

// provide dimensions and TensorOptions which include device type and data type.
// This will set all the information of Tensor properly and also allocate memory.
auto* Y = Output(0, {1, 2, 3}, at::device({context_.device_type()}).template dtype<T>());
// Tensor is fully initialized after this step

// following `mutable_data` call won't allocate memory.
auto* data = Y->mutable_data<float>();

Perf result

microbenchmarks

============================================================================
caffe2/caffe2/fb/benchmarks/core_overhead_benchmark.ccrelative  time/iter  iters/s
============================================================================
OperatorNewOutputTensorAPI                                   3.27us  306.05K
OperatorOldOutputTensorAPI                                   3.55us  281.54K
============================================================================

Differential Revision: D10207890

Differential Revision: D9779821
Differential Version: 57806436
Differential Revision: D9779821
Differential Version: 58572220
Differential Revision: D9779821
Differential Version: 58709105
Differential Revision: D9779821
Differential Version: 58713403
Differential Revision: D9779821
Differential Version: 58748651
Differential Revision: D9889990
Differential Version: 58748485
Differential Revision: D9889990
Differential Version: 58751934
Differential Revision: D9889990
Differential Version: 58799279
Differential Revision: D10033396
Differential Version: 58815881
Differential Revision: D9889990
Differential Version: 58825947
Differential Revision: D10033396
Differential Version: 58835275
Differential Revision: D9889990
Differential Version: 58856304
Differential Revision: D10033396
Differential Version: 58856963
Differential Revision: D9889990
Differential Version: 58858304
Differential Revision: D9889990
Differential Version: 58875107
Differential Revision: D9889990
Differential Version: 58881242
Differential Revision: D10001033
Differential Version: 58937972
Differential Revision: D10022853
Differential Version: 58947676
Differential Revision: D10022853
Differential Version: 58951172
Differential Revision: D10033396
Differential Version: 59085834
Differential Revision: D10033396
Differential Version: 59088508
Differential Revision: D10033396
Differential Version: 59091126
Differential Revision: D9889990
Differential Version: 59110247
Differential Revision: D10033396
Differential Version: 59227528
Differential Revision: D9889990
Differential Version: 59236356
Differential Revision: D9889990
Differential Version: 59596351
Differential Revision: D9889990
Differential Version: 59619253
Differential Revision: D9889990
Differential Version: 59621759
Differential Revision: D10001033
Differential Version: 59624116
Differential Revision: D9889990
Differential Version: 59625764
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants