Skip to content

Commit 6a40657

Browse files
authored
Merge pull request #133 from iotamudelta/master
Merge from upstream
2 parents a487cf2 + e26bb17 commit 6a40657

File tree

119 files changed

+1709
-1161
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1709
-1161
lines changed

.jenkins/pytorch/build.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ if [[ "$BUILD_ENVIRONMENT" == *rocm* ]]; then
4242
# This environment variable enabled HCC Optimizations that speed up the linking stage.
4343
# https://github.com/RadeonOpenCompute/hcc#hcc-with-thinlto-linking
4444
export KMTHINLTO=1
45-
45+
4646
# Need the libc++1 and libc++abi1 libraries to allow torch._C to load at runtime
4747
sudo apt-get install libc++1
4848
sudo apt-get install libc++abi1
49-
49+
5050
python tools/amd_build/build_pytorch_amd.py
5151
USE_ROCM=1 python setup.py install --user
5252
exit 0
@@ -118,5 +118,9 @@ if [[ "$BUILD_TEST_LIBTORCH" == "1" ]]; then
118118
echo "Building libtorch"
119119
# NB: Install outside of source directory (at the same level as the root
120120
# pytorch folder) so that it doesn't get cleaned away prior to docker push.
121-
WERROR=1 VERBOSE=1 tools/cpp_build/build_caffe2.sh "$PWD/../cpp-build"
121+
BUILD_LIBTORCH_PY=$PWD/tools/build_libtorch.py
122+
mkdir -p ../cpp-build/caffe2
123+
pushd ../cpp-build/caffe2
124+
WERROR=1 VERBOSE=1 DEBUG=1 python $BUILD_LIBTORCH_PY
125+
popd
122126
fi

.jenkins/pytorch/macos-test.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,12 @@ test_cpp_api() {
5656
#
5757
CPP_BUILD="$PWD/../cpp-build"
5858
rm -rf $CPP_BUILD
59-
mkdir -p $CPP_BUILD
60-
WERROR=1 VERBOSE=1 tools/cpp_build/build_caffe2.sh "$CPP_BUILD"
59+
mkdir -p $CPP_BUILD/caffe2
60+
61+
BUILD_LIBTORCH_PY=$PWD/tools/build_libtorch.py
62+
pushd $CPP_BUILD/caffe2
63+
WERROR=1 VERBOSE=1 DEBUG=1 python $BUILD_LIBTORCH_PY
64+
popd
6165

6266
python tools/download_mnist.py --quiet -d test/cpp/api/mnist
6367

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ if(NOT MSVC)
230230
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments")
231231
endif()
232232
if ((APPLE AND (NOT ("${CLANG_VERSION_STRING}" VERSION_LESS "9.0")))
233-
OR (CMAKE_COMPILER_IS_GNUCXX
233+
OR (CMAKE_COMPILER_IS_GNUCXX
234234
AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 AND NOT APPLE)))
235235
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -faligned-new")
236236
endif()

aten/src/ATen/Declarations.cwrap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@
23942394
if (self_->dim() == 0) {
23952395
throw std::runtime_error("Input must be 1-d or 2-d");
23962396
}
2397-
${THTensor}_diag(${state,}result_->tensor, self_->tensor, diagonal);
2397+
${THTensor}_diag(${state,}result_, self_, diagonal);
23982398
result_->maybe_zero_dim(self_->dim() == 0);
23992399
]]
24002400
[[
@@ -2986,7 +2986,7 @@
29862986
- arg: real tol
29872987
default: -1
29882988
aten_custom_call: |
2989-
${THTensor}_pstrf(res1_->tensor, res2_->tensor, self_->tensor, (upper) ? "U" : "L", tol_);
2989+
${THTensor}_pstrf(res1_, res2_, self_, (upper) ? "U" : "L", tol_);
29902990
res2 -= 1; // LAPACK returns 1-indexed pivots
29912991
]]
29922992
[[

aten/src/ATen/Registry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class AT_API Registry {
114114
};
115115

116116
template <class SrcType, class ObjectPtrType, class... Args>
117-
class Registerer {
117+
class AT_API Registerer {
118118
public:
119119
Registerer(
120120
const SrcType& key,

aten/src/ATen/SparseTensorImpl.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace {
3131
// we don't currently support zero-size dimensions, so we can't actually
3232
// do this; so we just allocate zero-size tensors for everything.
3333
SparseTensorImpl::SparseTensorImpl(at::TensorTypeId type_id, at::ScalarType scalar_type)
34-
: TensorImpl(type_id, scalar_type, nullptr, false)
34+
: TensorImpl(type_id, scalar_type, false)
3535
, size_{0}
3636
, sparseDims_(1)
3737
, denseDims_(0)
@@ -44,6 +44,14 @@ IntList SparseTensorImpl::sizes() const {
4444
IntList SparseTensorImpl::strides() const {
4545
AT_ERROR("sparse tensors do not have strides");
4646
}
47+
int64_t SparseTensorImpl::size(int64_t d) const {
48+
d = at::maybe_wrap_dim(d, dim(), false);
49+
return size_[d];
50+
}
51+
int64_t SparseTensorImpl::stride(int64_t d) const {
52+
AT_ERROR("sparse tensors do not have strides");
53+
}
54+
4755
int64_t SparseTensorImpl::dim() const {
4856
return sparseDims_ + denseDims_;
4957
}
@@ -54,13 +62,15 @@ TensorImpl* SparseTensorImpl::maybe_zero_dim(bool condition_when_zero_dim) {
5462
" changing dimensionality via maybe_zero_dim");
5563
return this;
5664
}
57-
void * SparseTensorImpl::unsafeGetTH(bool retain) {
58-
AT_ERROR("unsafeGetTH not supported for new style TensorImpl");
59-
}
6065
std::unique_ptr<Storage> SparseTensorImpl::storage() {
6166
AT_ERROR("sparse tensors do not have storage");
6267
}
63-
68+
at::StorageImpl* SparseTensorImpl::storageImpl() const {
69+
AT_ERROR("sparse tensors do not have storage");
70+
}
71+
int64_t SparseTensorImpl::storage_offset() const {
72+
AT_ERROR("sparse tensors do not have storage");
73+
}
6474
void SparseTensorImpl::set_indices_and_values(const Tensor& indices, const Tensor& values) {
6575
// TODO: Explicit empty test is needed because we don't handle size zero
6676
// dimensions at the moment

aten/src/ATen/SparseTensorImpl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ struct AT_API SparseTensorImpl : public TensorImpl {
5959

6060
IntList sizes() const override;
6161
IntList strides() const override;
62+
int64_t size(int64_t d) const override;
63+
int64_t stride(int64_t d) const override;
64+
6265
int64_t dim() const override;
6366
TensorImpl* maybe_zero_dim(bool condition_when_zero_dim) override;
64-
void * unsafeGetTH(bool retain) override;
6567
std::unique_ptr<Storage> storage() override;
68+
at::StorageImpl* storageImpl() const override;
69+
int64_t storage_offset() const override;
6670

6771
// Some ops do some manual size fiddling.
6872
// TODO: Figure out a more safe way to provide this functionality

aten/src/ATen/StorageImpl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#pragma once
22

3-
#include <ATen/Scalar.h>
4-
53
#include <ATen/Allocator.h>
64
#include <ATen/ScalarType.h>
75
#include <ATen/ScalarTypeUtils.h>

aten/src/ATen/TensorImpl.cpp

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,59 +59,77 @@ void Tensor::backward(
5959
pImpl->backward(std::move(gradient), keep_graph, create_graph);
6060
}
6161

62-
TensorImpl::TensorImpl(TensorTypeId type_id, ScalarType scalar_type)
63-
: type_id_(type_id), scalar_type_(scalar_type) {
64-
auto type = &globalContext().getType(tensorTypeIdToBackend(type_id), scalar_type);
65-
auto storage = type->storage(true);
66-
StorageImpl* storage_impl = storage->pImpl();
67-
storage_impl->retain();
68-
tensor = new THTensor(storage_impl);
62+
TensorImpl::TensorImpl(TensorTypeId type_id, ScalarType scalar_type, bool is_variable)
63+
: TensorImpl(nullptr, type_id, scalar_type, is_variable) {
64+
// UndefinedTensors and SparseTensors don't have storages.
65+
if (type_id != UndefinedTensorId() && scalar_type != ScalarType::Undefined
66+
&& type_id != SparseCPUTensorId() && type_id != SparseCUDATensorId()) {
67+
auto type = &globalContext().getType(tensorTypeIdToBackend(type_id), scalar_type);
68+
auto storage = type->storage(true);
69+
storage_ = storage->pImpl();
70+
storage_->retain();
71+
}
6972
}
7073

74+
TensorImpl::TensorImpl(StorageImpl* storage, TensorTypeId type_id, bool is_variable)
75+
: TensorImpl(storage, type_id, storage->scalar_type(), is_variable) {}
76+
77+
TensorImpl::TensorImpl(StorageImpl* storage, TensorTypeId type_id, ScalarType scalar_type, bool is_variable)
78+
: storage_(storage),
79+
storage_offset_(0),
80+
sizes_{0},
81+
strides_{1},
82+
type_id_(type_id),
83+
scalar_type_(scalar_type),
84+
is_variable_(is_variable) {}
85+
7186
TensorImpl::~TensorImpl() {
72-
if (tensor) tensor->release();
87+
if (storage_) {
88+
storage_->release();
89+
storage_ = nullptr;
90+
}
7391
}
7492

7593
IntList TensorImpl::sizes() const {
76-
// NB: dim in tensor is not synchronized with THTensor, so it's
77-
// important to apply dim here
78-
return IntList(THTensor_getSizePtr(tensor), dim());
94+
return sizes_;
7995
}
8096

8197
IntList TensorImpl::strides() const {
82-
// NB: dim in tensor is not synchronized with THTensor, so it's
83-
// important to apply dim here
84-
return IntList(THTensor_getStridePtr(tensor), dim());
98+
return strides_;
8599
}
86100

87101
void TensorImpl::release_resources() {
88-
if (tensor) {
89-
tensor->release();
90-
tensor = nullptr;
102+
if (storage_) {
103+
storage_->release();
104+
storage_ = nullptr;
91105
}
92106
}
93107

94108
int64_t TensorImpl::dim() const {
95-
return tensor->dim();
109+
return sizes_.size();
96110
}
97111

98-
TensorImpl* TensorImpl::maybe_zero_dim(bool condition_when_zero_dim) {
99-
AT_CHECK(tensor, "TensorImpl without THTensor in maybe_zero_dim");
100-
THTensor_maybe_zero_dim(tensor, condition_when_zero_dim);
101-
return this;
112+
int64_t TensorImpl::size(int64_t d) const {
113+
d = at::maybe_wrap_dim(d, dim(), false);
114+
return sizes_[d];
115+
}
116+
117+
int64_t TensorImpl::stride(int64_t d) const {
118+
d = at::maybe_wrap_dim(d, dim(), false);
119+
return strides_[d];
102120
}
103121

104-
void * TensorImpl::unsafeGetTH(bool retain) {
105-
if (retain) {
106-
tensor->retain();
122+
TensorImpl* TensorImpl::maybe_zero_dim(bool condition_when_zero_dim) {
123+
bool set_zero_dim = condition_when_zero_dim && this->sizes().size() == 1 && this->size(0) == 1;
124+
if (set_zero_dim) {
125+
THTensor_resizeDim(this, 0);
107126
}
108-
return tensor;
127+
return this;
109128
}
110129

111130
std::unique_ptr<Storage> TensorImpl::storage() {
112-
StorageImpl* storage = tensor->storage_;
113-
storage->retain();
114-
return std::unique_ptr<Storage>(new Storage(storage));
131+
storage_->retain();
132+
return std::unique_ptr<Storage>(new Storage(storage_));
115133
}
116134

117135
} // namespace at

aten/src/ATen/TensorImpl.h

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <atomic>
44
#include <memory>
55

6-
#include "ATen/Retainable.h"
7-
#include "ATen/ScalarType.h"
6+
#include "ATen/StorageImpl.h"
87
#include "ATen/core/optional.h"
98
#include "ATen/core/TensorTypeId.h"
109
#include "ATen/core/TensorTypeIdRegistration.h"
@@ -20,9 +19,8 @@ struct Tensor;
2019

2120
namespace at {
2221
struct AT_API TensorImpl : public Retainable {
23-
explicit TensorImpl(TensorTypeId type_id, ScalarType scalar_type, THTensor * tensor, bool is_variable)
24-
: type_id_(type_id), scalar_type_(scalar_type), is_variable_(is_variable), tensor(tensor) {}
25-
TensorImpl(TensorTypeId type_id, ScalarType scalar_type);
22+
TensorImpl(TensorTypeId type_id, ScalarType scalar_type, bool is_variable);
23+
TensorImpl(StorageImpl* storage, TensorTypeId type_id, bool is_variable);
2624

2725
virtual ~TensorImpl();
2826

@@ -37,7 +35,6 @@ struct AT_API TensorImpl : public Retainable {
3735
virtual IntList sizes() const;
3836
virtual IntList strides() const;
3937
virtual int64_t dim() const;
40-
virtual void * unsafeGetTH(bool retain);
4138
virtual std::unique_ptr<Storage> storage();
4239
friend struct Type;
4340

@@ -95,14 +92,58 @@ struct AT_API TensorImpl : public Retainable {
9592

9693
virtual void set_data(Tensor new_data);
9794

95+
// TODO: make these protected
96+
// Note: storage->size() may be greater than the recorded size
97+
// of a tensor
98+
at::StorageImpl* storage_;
99+
int64_t storage_offset_;
100+
101+
std::vector<int64_t> sizes_;
102+
std::vector<int64_t> strides_;
103+
104+
template <typename T>
105+
inline T * data() const {
106+
return storageImpl()->data<T>() + storage_offset_;
107+
}
108+
109+
template <typename T>
110+
inline T * unsafe_data() const {
111+
return storageImpl()->unsafe_data<T>() + storage_offset_;
112+
}
113+
114+
inline at::ScalarType scalar_type() const {
115+
return scalar_type_;
116+
}
117+
118+
virtual int64_t storage_offset() const {
119+
return storage_offset_;
120+
}
121+
122+
// represents that numel() == 0.
123+
inline bool is_empty() const {
124+
for (int64_t i = 0; i < dim(); ++i) {
125+
if (sizes()[i] == 0) {
126+
return true;
127+
}
128+
}
129+
return false;
130+
}
131+
132+
virtual int64_t size(int64_t d) const;
133+
virtual int64_t stride(int64_t d) const;
134+
135+
// TODO: get rid of this.
136+
virtual at::StorageImpl* storageImpl() const { return storage_; }
137+
98138
protected:
99139
TensorTypeId type_id_;
100140
// INVARIANT: When storage is non-null, this scalar type must
101141
// agree with the scalar type in storage
102142
ScalarType scalar_type_;
103143
bool is_variable_ = false;
104144
bool is_wrapped_number_ = false;
105-
public:
106-
THTensor * tensor;
145+
146+
private:
147+
TensorImpl(StorageImpl* storage, TensorTypeId type_id, ScalarType scalar_type, bool is_variable);
107148
};
108149
} // namespace at

aten/src/ATen/UndefinedTensor.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,37 @@ namespace at {
66

77
// should this use the globalContext? Can it get a context passed in somehow?
88
UndefinedTensor::UndefinedTensor()
9-
: TensorImpl(UndefinedTensorId(), ScalarType::Undefined, nullptr, /* is variable */ false) {
9+
: TensorImpl(UndefinedTensorId(), ScalarType::Undefined, /* is variable */ false) {
1010
}
1111

1212
IntList UndefinedTensor::sizes() const {
1313
AT_ERROR("sizes() called on undefined Tensor");
1414
}
1515

16+
int64_t UndefinedTensor::size(int64_t d) const {
17+
AT_ERROR("size(dim) called on an undefined Tensor");
18+
}
19+
20+
int64_t UndefinedTensor::stride(int64_t d) const {
21+
AT_ERROR("stride(dim) called on an undefined Tensor");
22+
}
23+
1624
int64_t UndefinedTensor::dim() const {
1725
AT_ERROR("dim() called on undefined Tensor");
1826
}
1927

20-
void * UndefinedTensor::unsafeGetTH(bool retain) {
21-
AT_ERROR("unsafeGetTH(bool retain) called on undefined Tensor");
22-
}
2328
std::unique_ptr<Storage> UndefinedTensor::storage() {
2429
AT_ERROR("storage() called on undefined Tensor");
2530
}
2631

32+
at::StorageImpl* UndefinedTensor::storageImpl() const {
33+
AT_ERROR("storageImpl() called on an undefined Tensor");
34+
}
35+
36+
int64_t UndefinedTensor::storage_offset() const {
37+
AT_ERROR("storage_offset() called on an undefined Tensor");
38+
}
39+
2740
IntList UndefinedTensor::strides() const {
2841
AT_ERROR("strides() called on undefined Tensor");
2942
}

aten/src/ATen/UndefinedTensor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ struct AT_API UndefinedTensor final : public TensorImpl {
1111
}
1212
IntList sizes() const override;
1313
IntList strides() const override;
14+
int64_t size(int64_t d) const override;
15+
int64_t stride(int64_t d) const override;
1416
int64_t dim() const override;
15-
void * unsafeGetTH(bool retain) override;
1617
std::unique_ptr<Storage> storage() override;
18+
at::StorageImpl* storageImpl() const override;
19+
int64_t storage_offset() const override;
1720
private:
1821
UndefinedTensor();
1922
static UndefinedTensor _singleton;

0 commit comments

Comments
 (0)