Skip to content

Commit 8e91da4

Browse files
ArutyunovGfacebook-github-bot
authored andcommitted
Windows shared build (pytorch#13550)
Summary: Hi guys, I'd like to build Caffe2 with more supported options in Windows with Microsoft Visual Studios. This is the first pull request. Running scripts/build_windows_shared.bat is able to build Caffe2 with both CMAKE_BUILD_TYPE=Debug and CMAKE_BUILD_TYPE=Release with Visual Studio 14 2015. CUDA is 9.0, cudnn is 7.0.5, glog, gflags and lmdb are supported on my system. Python is 3.5, Detectron works from python interface as well. It was even possible to debug detectron code and step into caffe2_gpu.dll with pdbs built. What is disappointing, that c10/experimental ops don't build with this Visual Studio generator, I added special option INCLUDE_EXPERIMENTAL_C10_OPS (default ON) to deal with it in build_windows_shared.bat. After this pull request the next step is to add Visual Studio 2017 support in the script. Pull Request resolved: pytorch#13550 Reviewed By: ezyang Differential Revision: D13042597 Pulled By: orionr fbshipit-source-id: f313f909f599cd582a1d000eff766eef3a9fc4fc
1 parent 2c21de2 commit 8e91da4

File tree

73 files changed

+538
-498
lines changed

Some content is hidden

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

73 files changed

+538
-498
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ option(BUILD_DOCS "Build Caffe2 documentation" OFF)
6565
option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" ON)
6666
option(BUILD_PYTHON "Build Python binaries" ON)
6767
option(BUILD_CAFFE2_OPS "Build Caffe2 operators" ON)
68+
option(BUILD_C10_EXPERIMENTAL_OPS "Build c10 experimental operators" ON)
6869
option(BUILD_SHARED_LIBS "Build libcaffe2.so" ON)
6970
cmake_dependent_option(
7071
CAFFE2_LINK_LOCAL_PROTOBUF "If set, build protobuf inside libcaffe2.so." ON

aten/src/ATen/core/TensorTypeId.cpp

Lines changed: 0 additions & 10 deletions
This file was deleted.

aten/src/ATen/core/TensorTypeId.h

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,2 @@
11
#pragma once
2-
3-
#include <iostream>
4-
#include <string>
5-
#include "c10/util/IdWrapper.h"
6-
#include "c10/macros/Macros.h"
7-
8-
namespace at {
9-
10-
namespace details {
11-
using _tensorTypeId_underlyingType = uint8_t;
12-
}
13-
14-
/**
15-
* Dynamic type ID of a Tensor argument. It represents something like
16-
* CPUTensor, etc.
17-
*/
18-
class CAFFE2_API TensorTypeId final
19-
: public at::
20-
IdWrapper<TensorTypeId, details::_tensorTypeId_underlyingType> {
21-
public:
22-
// Don't use this!
23-
// Unfortunately, a default constructor needs to be defined because of
24-
// https://reviews.llvm.org/D41223
25-
constexpr TensorTypeId() noexcept : IdWrapper(0) {}
26-
27-
private:
28-
constexpr explicit TensorTypeId(
29-
details::_tensorTypeId_underlyingType id) noexcept
30-
: IdWrapper(id) {}
31-
32-
friend class TensorTypeIdCreator;
33-
friend CAFFE2_API std::ostream& operator<<(std::ostream&, TensorTypeId);
34-
};
35-
36-
CAFFE2_API std::ostream& operator<<(std::ostream&, at::TensorTypeId);
37-
38-
} // namespace at
39-
40-
C10_DEFINE_HASH_FOR_IDWRAPPER(at::TensorTypeId)
2+
#include <c10/util/TensorTypeId.h>
Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,2 @@
11
#pragma once
2-
3-
/**
4-
* To register your own tensor types, do in a header file:
5-
* AT_DECLARE_TENSOR_TYPE(MY_TENSOR)
6-
* and in one (!) cpp file:
7-
* AT_DEFINE_TENSOR_TYPE(MY_TENSOR)
8-
* Both must be in the same namespace.
9-
*/
10-
11-
#include "ATen/core/TensorTypeId.h"
12-
#include "c10/macros/Macros.h"
13-
14-
#include <atomic>
15-
#include <mutex>
16-
#include <unordered_set>
17-
18-
namespace at {
19-
20-
class CAFFE2_API TensorTypeIdCreator final {
21-
public:
22-
TensorTypeIdCreator();
23-
24-
at::TensorTypeId create();
25-
26-
static constexpr at::TensorTypeId undefined() noexcept {
27-
return TensorTypeId(0);
28-
}
29-
30-
private:
31-
std::atomic<details::_tensorTypeId_underlyingType> last_id_;
32-
33-
C10_DISABLE_COPY_AND_ASSIGN(TensorTypeIdCreator);
34-
};
35-
36-
class CAFFE2_API TensorTypeIdRegistry final {
37-
public:
38-
TensorTypeIdRegistry();
39-
40-
void registerId(at::TensorTypeId id);
41-
void deregisterId(at::TensorTypeId id);
42-
43-
private:
44-
std::unordered_set<at::TensorTypeId> registeredTypeIds_;
45-
std::mutex mutex_;
46-
47-
C10_DISABLE_COPY_AND_ASSIGN(TensorTypeIdRegistry);
48-
};
49-
50-
class CAFFE2_API TensorTypeIds final {
51-
public:
52-
static TensorTypeIds& singleton();
53-
54-
at::TensorTypeId createAndRegister();
55-
void deregister(at::TensorTypeId id);
56-
57-
static constexpr at::TensorTypeId undefined() noexcept;
58-
59-
private:
60-
TensorTypeIds();
61-
62-
TensorTypeIdCreator creator_;
63-
TensorTypeIdRegistry registry_;
64-
65-
C10_DISABLE_COPY_AND_ASSIGN(TensorTypeIds);
66-
};
67-
68-
inline constexpr at::TensorTypeId TensorTypeIds::undefined() noexcept {
69-
return TensorTypeIdCreator::undefined();
70-
}
71-
72-
class CAFFE2_API TensorTypeIdRegistrar final {
73-
public:
74-
TensorTypeIdRegistrar();
75-
~TensorTypeIdRegistrar();
76-
77-
at::TensorTypeId id() const noexcept;
78-
79-
private:
80-
at::TensorTypeId id_;
81-
82-
C10_DISABLE_COPY_AND_ASSIGN(TensorTypeIdRegistrar);
83-
};
84-
85-
inline at::TensorTypeId TensorTypeIdRegistrar::id() const noexcept {
86-
return id_;
87-
}
88-
89-
#define AT_DECLARE_TENSOR_TYPE(TensorName) \
90-
CAFFE2_API at::TensorTypeId TensorName()
91-
92-
#define AT_DEFINE_TENSOR_TYPE(TensorName) \
93-
at::TensorTypeId TensorName() { \
94-
static TensorTypeIdRegistrar registration_raii; \
95-
return registration_raii.id(); \
96-
}
97-
98-
AT_DECLARE_TENSOR_TYPE(UndefinedTensorId);
99-
AT_DECLARE_TENSOR_TYPE(CPUTensorId); // PyTorch/Caffe2 supported
100-
AT_DECLARE_TENSOR_TYPE(CUDATensorId); // PyTorch/Caffe2 supported
101-
AT_DECLARE_TENSOR_TYPE(SparseCPUTensorId); // PyTorch only
102-
AT_DECLARE_TENSOR_TYPE(SparseCUDATensorId); // PyTorch only
103-
AT_DECLARE_TENSOR_TYPE(MKLDNNTensorId); // Caffe2 only
104-
AT_DECLARE_TENSOR_TYPE(OpenGLTensorId); // Caffe2 only
105-
AT_DECLARE_TENSOR_TYPE(OpenCLTensorId); // Caffe2 only
106-
AT_DECLARE_TENSOR_TYPE(IDEEPTensorId); // Caffe2 only
107-
AT_DECLARE_TENSOR_TYPE(HIPTensorId); // Caffe2 only
108-
109-
} // namespace at
2+
#include "c10/util/TensorTypeIdRegistration.h"

aten/src/THC/THCAllocator.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ at::DataPtr THCIpcDeleter::makeDataPtr(void* data, int device) {
1919
auto* context = new THCIpcDeleter(data, device);
2020
return {data, context, &deleteTHCIpcDeleter, at::Device(at::DeviceType::CUDA, cur_device)};
2121
}
22+
23+
THCIpcDeleter::THCIpcDeleter(void* data, int device)
24+
: data_(data), device_(device) {}

aten/src/THC/THCAllocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#ifdef __cplusplus
99
class CAFFE2_API THCIpcDeleter {
1010
public:
11-
THCIpcDeleter(void* data, int device) : data_(data), device_(device) {};
11+
THCIpcDeleter(void* data, int device);
1212
~THCIpcDeleter();
1313
static at::DataPtr makeDataPtr(void* data, int device);
1414
private:

binaries/benchmark_helper.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void loadInput(
141141
vector<string> input_dims_str = caffe2::split(',', input_dims_list[i]);
142142
vector<int> input_dims;
143143
for (const string& s : input_dims_str) {
144-
input_dims.push_back(caffe2::stoi(s));
144+
input_dims.push_back(c10::stoi(s));
145145
}
146146
caffe2::Blob* blob = workspace->GetBlob(input_names[i]);
147147
if (blob == nullptr) {

binaries/convert_image_to_tensor.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ std::vector<float> convertToVector(cv::Mat& img) {
9999
} else if (step == "normalize") {
100100
normalize = {255, 255, 255};
101101
} else if (step == "mean") {
102-
mean = {0.406, 0.456, 0.485};
102+
mean = {0.406f, 0.456f, 0.485f};
103103
} else if (step == "std") {
104-
std = {0.225, 0.224, 0.229};
104+
std = {0.225f, 0.224f, 0.229f};
105105
} else if (step == "bgrtorgb") {
106106
bgrtorgb = true;
107107
} else {
@@ -143,9 +143,14 @@ std::vector<float> convertOneImage(std::string& filename) {
143143
assert(filename[0] != '~');
144144

145145
std::cout << "Converting " << filename << std::endl;
146+
146147
// Load image
147148
cv::Mat img = cv::imread(
149+
#if CV_MAJOR_VERSION <= 3
148150
filename, FLAGS_color ? CV_LOAD_IMAGE_COLOR : CV_LOAD_IMAGE_GRAYSCALE);
151+
#else
152+
filename, FLAGS_color ? cv::IMREAD_COLOR : cv::IMREAD_GRAYSCALE);
153+
#endif
149154

150155
cv::Mat crop = cropToSquare(img);
151156

binaries/speed_benchmark.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ int main(int argc, char** argv) {
127127
vector<string> input_dims_str = caffe2::split(',', input_dims_list[i]);
128128
vector<int> input_dims;
129129
for (const string& s : input_dims_str) {
130-
input_dims.push_back(caffe2::stoi(s));
130+
input_dims.push_back(c10::stoi(s));
131131
}
132132
caffe2::Blob* blob = workspace->GetBlob(input_names[i]);
133133
if (blob == nullptr) {

c10/util/IdWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace c10 {
2323
* for you, given the underlying type supports it.
2424
*/
2525
template <class ConcreteType, class UnderlyingType>
26-
class CAFFE2_API IdWrapper {
26+
class C10_API IdWrapper {
2727
public:
2828
using underlying_type = UnderlyingType;
2929
using concrete_type = ConcreteType;

c10/util/SmallVector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static inline uint64_t NextPowerOf2(uint64_t A) {
5353
} // namespace detail
5454

5555
/// This is all the non-templated stuff common to all SmallVectors.
56-
class CAFFE2_API SmallVectorBase {
56+
class C10_API SmallVectorBase {
5757
protected:
5858
void *BeginX, *EndX, *CapacityX;
5959

c10/util/StringUtil.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define C10_UTIL_STRINGUTIL_H_
33

44
#include <c10/macros/Macros.h>
5+
#include <c10/util/string_utils.h>
56

67
#include <cstddef>
78
#include <ostream>
@@ -73,28 +74,6 @@ struct C10_API SourceLocation {
7374

7475
std::ostream& operator<<(std::ostream& out, const SourceLocation& loc);
7576

76-
/// Portable implementation of std::stoi, which works for Android builds.
77-
///
78-
/// TODO: You won't be able to call this unqualified, because ADL means that it
79-
/// will be ambiguous with std::stoi. Maybe we should fix this by giving
80-
/// our version a different name.
81-
inline int stoi(const std::string& str) {
82-
#if defined(__ANDROID__)
83-
std::stringstream ss;
84-
int n = 0;
85-
ss << str;
86-
ss >> n;
87-
return n;
88-
#else
89-
return std::stoi(str);
90-
#endif // defined(__ANDROID__)
91-
}
92-
9377
} // namespace c10
9478

95-
// TODO: Remove me when namespace unification occurs
96-
namespace at {
97-
using c10::stoi;
98-
}
99-
10079
#endif // C10_UTIL_STRINGUTIL_H_

c10/util/TensorTypeId.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "c10/util/TensorTypeId.h"
2+
#include "c10/util/string_utils.h"
3+
4+
namespace c10 {
5+
6+
std::ostream& operator<<(std::ostream& str, c10::TensorTypeId rhs) {
7+
return str << c10::to_string(rhs.underlyingId());
8+
}
9+
10+
} // namespace c10

c10/util/TensorTypeId.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef TENSOR_TYPE_ID_H_
2+
#define TENSOR_TYPE_ID_H_
3+
4+
#include <iostream>
5+
#include <string>
6+
#include "c10/macros/Macros.h"
7+
#include "c10/util/IdWrapper.h"
8+
9+
namespace c10 {
10+
11+
namespace details {
12+
using _tensorTypeId_underlyingType = uint8_t;
13+
}
14+
15+
/**
16+
* Dynamic type ID of a Tensor argument. It represents something like
17+
* CPUTensor, etc.
18+
*/
19+
class C10_API TensorTypeId final
20+
: public at::
21+
IdWrapper<TensorTypeId, details::_tensorTypeId_underlyingType> {
22+
public:
23+
// Don't use this!
24+
// Unfortunately, a default constructor needs to be defined because of
25+
// https://reviews.llvm.org/D41223
26+
constexpr TensorTypeId() noexcept : IdWrapper(0) {}
27+
28+
private:
29+
constexpr explicit TensorTypeId(
30+
details::_tensorTypeId_underlyingType id) noexcept
31+
: IdWrapper(id) {}
32+
33+
friend class TensorTypeIdCreator;
34+
friend C10_API std::ostream& operator<<(std::ostream&, TensorTypeId);
35+
};
36+
37+
C10_API std::ostream& operator<<(std::ostream&, c10::TensorTypeId);
38+
39+
} // namespace c10
40+
41+
C10_DEFINE_HASH_FOR_IDWRAPPER(c10::TensorTypeId)
42+
43+
#endif // TENSOR_TYPE_ID_H_

0 commit comments

Comments
 (0)