Skip to content

Commit 5765549

Browse files
ezyangfacebook-github-bot
authored andcommitted
codemod -d caffe2 --extensions cc,h CaffeTypeId TypeIdentifier (pytorch#10166)
Summary: Pull Request resolved: pytorch#10166 TypeIdentifier is still easy to codemod away from Reviewed By: smessmer Differential Revision: D9132840 fbshipit-source-id: bc83a8b17b2e7c19c9d2c9cfe5c7ce6ec1d8cec5
1 parent 4a2f3cc commit 5765549

17 files changed

+71
-71
lines changed

caffe2/core/blob_serialization.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ void TensorSerializer::StoreDeviceDetail(
322322
// The actual serialization registry objects.
323323
CAFFE_DEFINE_TYPED_REGISTRY(
324324
BlobSerializerRegistry,
325-
CaffeTypeId,
325+
TypeIdentifier,
326326
BlobSerializerBase,
327327
std::unique_ptr);
328328

caffe2/core/blob_serialization.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ constexpr auto kChunkIdSeparator = "#%";
2626
// The Blob serialization registry and serializer creator functions.
2727
CAFFE_DECLARE_TYPED_REGISTRY(
2828
BlobSerializerRegistry,
29-
CaffeTypeId,
29+
TypeIdentifier,
3030
BlobSerializerBase,
3131
std::unique_ptr);
3232
#define REGISTER_BLOB_SERIALIZER(id, ...) \
3333
CAFFE_REGISTER_TYPED_CLASS(BlobSerializerRegistry, id, __VA_ARGS__)
3434
// Creates an operator with the given operator definition.
35-
inline unique_ptr<BlobSerializerBase> CreateSerializer(CaffeTypeId id) {
35+
inline unique_ptr<BlobSerializerBase> CreateSerializer(TypeIdentifier id) {
3636
return BlobSerializerRegistry()->Create(id);
3737
}
3838

caffe2/core/blob_stats.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace caffe2 {
44

5-
const BlobStatGetter* BlobStatRegistry::get(CaffeTypeId id) {
5+
const BlobStatGetter* BlobStatRegistry::get(TypeIdentifier id) {
66
auto it = map_.find(id);
77
if (it == map_.end()) {
88
return nullptr;
@@ -16,7 +16,7 @@ BlobStatRegistry& BlobStatRegistry::instance() {
1616
}
1717

1818
void BlobStatRegistry::doRegister(
19-
CaffeTypeId id,
19+
TypeIdentifier id,
2020
std::unique_ptr<BlobStatGetter>&& v) {
2121
// don't use CAFFE_ENFORCE_EQ to avoid static initialization order fiasco.
2222
if (map_.count(id) > 0) {

caffe2/core/blob_stats.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ struct BlobStatGetter {
1515

1616
struct BlobStatRegistry {
1717
private:
18-
std::unordered_map<CaffeTypeId, std::unique_ptr<BlobStatGetter>> map_;
19-
void doRegister(CaffeTypeId id, std::unique_ptr<BlobStatGetter>&& v);
18+
std::unordered_map<TypeIdentifier, std::unique_ptr<BlobStatGetter>> map_;
19+
void doRegister(TypeIdentifier id, std::unique_ptr<BlobStatGetter>&& v);
2020

2121
public:
2222
template <typename T, typename Getter>
@@ -27,7 +27,7 @@ struct BlobStatRegistry {
2727
}
2828
};
2929

30-
const BlobStatGetter* get(CaffeTypeId id);
30+
const BlobStatGetter* get(TypeIdentifier id);
3131
static BlobStatRegistry& instance();
3232
};
3333

caffe2/core/dispatch/DispatchKey.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ struct TensorParameterDispatchKey final {
1616
// note: This dispatch key structure is not final yet and will change. Don't rely on it.
1717
DeviceTypeId deviceTypeId;
1818
LayoutId layoutId;
19-
// TODO Move this CaffeTypeId to c10 namespace
20-
caffe2::CaffeTypeId dataType;
19+
// TODO Move this TypeIdentifier to c10 namespace
20+
caffe2::TypeIdentifier dataType;
2121
};
2222
inline constexpr bool operator==(const TensorParameterDispatchKey& lhs, const TensorParameterDispatchKey& rhs) {
2323
return lhs.deviceTypeId == rhs.deviceTypeId && lhs.layoutId == rhs.layoutId && lhs.dataType == rhs.dataType;
@@ -34,7 +34,7 @@ namespace std {
3434
struct hash<c10::details::TensorParameterDispatchKey> {
3535
// TODO constexpr hashing
3636
size_t operator()(const c10::details::TensorParameterDispatchKey& obj) const {
37-
return std::hash<c10::DeviceTypeId>()(obj.deviceTypeId) ^ std::hash<c10::LayoutId>()(obj.layoutId) ^ std::hash<caffe2::CaffeTypeId>()(obj.dataType);
37+
return std::hash<c10::DeviceTypeId>()(obj.deviceTypeId) ^ std::hash<c10::LayoutId>()(obj.layoutId) ^ std::hash<caffe2::TypeIdentifier>()(obj.dataType);
3838
}
3939
};
4040
} // namespace std

caffe2/core/plan_executor.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ std::function<bool(int64_t)> getContinuationTest(
100100

101101
// if the blob doesn't exist or is not initiaized, return false
102102
inline bool getShouldStop(const Blob* b) {
103-
if (!b || b->meta().id() == CaffeTypeId::uninitialized()) { // not exist or uninitialized
103+
if (!b || b->meta().id() == TypeIdentifier::uninitialized()) { // not exist or uninitialized
104104
return false;
105105
}
106106

caffe2/core/tensor.cc

+6-6
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ TypeMeta GetTensorType(const void* c) {
6868
}
6969

7070
// TODO(jerryzh): Remove
71-
static CaffeMap<CaffeTypeId, TypeCall> type_call_registry_{
71+
static CaffeMap<TypeIdentifier, TypeCall> type_call_registry_{
7272
{TypeMeta::Id<Tensor>(), GetTensorType}};
7373

74-
TypeCall GetTypeCallFunction(CaffeTypeId id) {
74+
TypeCall GetTypeCallFunction(TypeIdentifier id) {
7575
auto f = type_call_registry_.find(id);
7676
if (f == type_call_registry_.end()) {
7777
return nullptr;
7878
}
7979
return f->second;
8080
}
8181

82-
void RegisterTypeCallFunction(CaffeTypeId id, TypeCall c) {
82+
void RegisterTypeCallFunction(TypeIdentifier id, TypeCall c) {
8383
type_call_registry_[id] = c;
8484
}
8585

@@ -98,20 +98,20 @@ vector<TIndex> GetTensorInfo(
9898
}
9999

100100
// since we only have one tensor, probably need to remove this at some point?
101-
static CaffeMap<CaffeTypeId, TensorInfoCall> tensor_info_call_registry_{
101+
static CaffeMap<TypeIdentifier, TensorInfoCall> tensor_info_call_registry_{
102102
{TypeMeta::Id<Tensor>(), GetTensorInfo}};
103103

104104
// TODO: Remove this code in a separate diff, since we only have one
105105
// GetTensorInfo function now
106-
TensorInfoCall GetTensorInfoFunction(CaffeTypeId id) {
106+
TensorInfoCall GetTensorInfoFunction(TypeIdentifier id) {
107107
auto f = tensor_info_call_registry_.find(id);
108108
if (f == tensor_info_call_registry_.end()) {
109109
return nullptr;
110110
}
111111
return f->second;
112112
}
113113

114-
void RegisterTensorInfoFunction(CaffeTypeId id, TensorInfoCall c) {
114+
void RegisterTensorInfoFunction(TypeIdentifier id, TensorInfoCall c) {
115115
tensor_info_call_registry_[id] = c;
116116
}
117117

caffe2/core/tensor.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ class Tensor {
473473
Deleter d = nullptr) {
474474
meta_ = meta;
475475
CAFFE_ENFORCE_WITH_CALLER(
476-
meta_.id() != CaffeTypeId::uninitialized(),
476+
meta_.id() != TypeIdentifier::uninitialized(),
477477
"To share with a raw external pointer you need to have meta "
478478
"already set.");
479479
CAFFE_ENFORCE_WITH_CALLER(
@@ -600,7 +600,7 @@ class Tensor {
600600
*/
601601
inline void* raw_mutable_data() {
602602
CAFFE_ENFORCE_WITH_CALLER(
603-
meta_.id() != CaffeTypeId::uninitialized(),
603+
meta_.id() != TypeIdentifier::uninitialized(),
604604
"Calling raw_mutable_data() without meta, but the current meta is "
605605
"of unknown type.");
606606
return raw_mutable_data(meta_);
@@ -829,17 +829,17 @@ constexpr int k_limit_default_ = 1000;
829829

830830
// Type call registry
831831
typedef TypeMeta (*TypeCall)(const void*);
832-
TypeCall GetTypeCallFunction(CaffeTypeId id);
833-
void RegisterTypeCallFunction(CaffeTypeId id, TypeCall c);
832+
TypeCall GetTypeCallFunction(TypeIdentifier id);
833+
void RegisterTypeCallFunction(TypeIdentifier id, TypeCall c);
834834

835835
// Shape call registry
836836
typedef vector<TIndex> (*TensorInfoCall)(
837837
const void*,
838838
bool* shares_data,
839839
size_t* capacity,
840840
DeviceOption* device);
841-
TensorInfoCall GetTensorInfoFunction(CaffeTypeId id);
842-
void RegisterTensorInfoFunction(CaffeTypeId id, TensorInfoCall c);
841+
TensorInfoCall GetTensorInfoFunction(TypeIdentifier id);
842+
void RegisterTensorInfoFunction(TypeIdentifier id, TensorInfoCall c);
843843

844844
// resize helper function
845845
void TensorVectorResize(

caffe2/core/typeid.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ using std::string;
1313

1414
namespace caffe2 {
1515

16-
std::unordered_map<CaffeTypeId, string>& gTypeNames() {
17-
static std::unordered_map<CaffeTypeId, string> g_type_names;
16+
std::unordered_map<TypeIdentifier, string>& gTypeNames() {
17+
static std::unordered_map<TypeIdentifier, string> g_type_names;
1818
return g_type_names;
1919
}
2020

@@ -59,14 +59,14 @@ void TypeMeta::_ThrowRuntimeTypeLogicError(const std::string& msg) {
5959
CAFFE_THROW(msg);
6060
}
6161

62-
CaffeTypeId CaffeTypeId::createTypeId() {
63-
static std::atomic<CaffeTypeId::underlying_type> counter(
62+
TypeIdentifier TypeIdentifier::createTypeId() {
63+
static std::atomic<TypeIdentifier::underlying_type> counter(
6464
TypeMeta::Id<_CaffeHighestPreallocatedTypeId>().underlyingId());
65-
const CaffeTypeId::underlying_type new_value = ++counter;
66-
if (new_value == std::numeric_limits<CaffeTypeId::underlying_type>::max()) {
67-
throw std::logic_error("Ran out of available type ids. If you need more than 2^16 CAFFE_KNOWN_TYPEs, we need to increase CaffeTypeId to use more than 16 bit.");
65+
const TypeIdentifier::underlying_type new_value = ++counter;
66+
if (new_value == std::numeric_limits<TypeIdentifier::underlying_type>::max()) {
67+
throw std::logic_error("Ran out of available type ids. If you need more than 2^16 CAFFE_KNOWN_TYPEs, we need to increase TypeIdentifier to use more than 16 bit.");
6868
}
69-
return CaffeTypeId(new_value);
69+
return TypeIdentifier(new_value);
7070
}
7171

7272
CAFFE_DEFINE_KNOWN_TYPE(Tensor);
@@ -103,7 +103,7 @@ namespace {
103103
// intended to be only instantiated once here.
104104
struct UninitializedTypeNameRegisterer {
105105
UninitializedTypeNameRegisterer() {
106-
gTypeNames()[CaffeTypeId::uninitialized()] = "nullptr (uninitialized)";
106+
gTypeNames()[TypeIdentifier::uninitialized()] = "nullptr (uninitialized)";
107107
}
108108
};
109109
static UninitializedTypeNameRegisterer g_uninitialized_type_name_registerer;

caffe2/core/typeid.h

+28-28
Original file line numberDiff line numberDiff line change
@@ -19,54 +19,54 @@
1919
#include "ATen/core/IdWrapper.h"
2020

2121
namespace caffe2 {
22-
class CaffeTypeId;
22+
class TypeIdentifier;
2323
}
2424

25-
std::ostream& operator<<(std::ostream& stream, caffe2::CaffeTypeId typeId);
25+
std::ostream& operator<<(std::ostream& stream, caffe2::TypeIdentifier typeId);
2626

2727
namespace caffe2 {
2828

2929
class TypeMeta;
3030

3131
/**
3232
* A type id is a unique id for a given C++ type.
33-
* You need to register your types using CAFFE_KNOWN_TYPE(MyType) to be able to use CaffeTypeId with custom types.
33+
* You need to register your types using CAFFE_KNOWN_TYPE(MyType) to be able to use TypeIdentifier with custom types.
3434
* This is for example used to store the dtype of tensors.
3535
*/
36-
class CaffeTypeId final : public at::IdWrapper<CaffeTypeId, uint16_t> {
36+
class TypeIdentifier final : public at::IdWrapper<TypeIdentifier, uint16_t> {
3737
public:
38-
static CaffeTypeId createTypeId();
38+
static TypeIdentifier createTypeId();
3939

40-
friend std::ostream& ::operator<<(std::ostream& stream, CaffeTypeId typeId);
41-
friend bool operator<(CaffeTypeId lhs, CaffeTypeId rhs);
40+
friend std::ostream& ::operator<<(std::ostream& stream, TypeIdentifier typeId);
41+
friend bool operator<(TypeIdentifier lhs, TypeIdentifier rhs);
4242

4343
// This is 8, because 0 is uint8_t (due to ScalarType BC constraint)
44-
static constexpr CaffeTypeId uninitialized() {
45-
return CaffeTypeId(8);
44+
static constexpr TypeIdentifier uninitialized() {
45+
return TypeIdentifier(8);
4646
}
4747

4848
private:
49-
constexpr explicit CaffeTypeId(uint16_t id): IdWrapper(id) {}
49+
constexpr explicit TypeIdentifier(uint16_t id): IdWrapper(id) {}
5050
friend class TypeMeta;
5151
};
5252

5353
// Allow usage in std::map / std::set
5454
// TODO Disallow this and rather use std::unordered_map/set everywhere
55-
inline bool operator<(CaffeTypeId lhs, CaffeTypeId rhs) {
55+
inline bool operator<(TypeIdentifier lhs, TypeIdentifier rhs) {
5656
return lhs.underlyingId() < rhs.underlyingId();
5757
}
5858

5959
}
6060

61-
AT_DEFINE_HASH_FOR_IDWRAPPER(caffe2::CaffeTypeId)
61+
AT_DEFINE_HASH_FOR_IDWRAPPER(caffe2::TypeIdentifier)
6262

63-
inline std::ostream& operator<<(std::ostream& stream, caffe2::CaffeTypeId typeId) {
63+
inline std::ostream& operator<<(std::ostream& stream, caffe2::TypeIdentifier typeId) {
6464
return stream << typeId.underlyingId();
6565
}
6666

6767
namespace caffe2 {
6868

69-
std::unordered_map<CaffeTypeId, std::string>& gTypeNames();
69+
std::unordered_map<TypeIdentifier, std::string>& gTypeNames();
7070
std::unordered_set<std::string>& gRegisteredTypeNames();
7171

7272
// A utility function to demangle a function name.
@@ -95,7 +95,7 @@ std::mutex& gTypeRegistrationMutex();
9595

9696
template <typename T>
9797
struct TypeNameRegisterer {
98-
TypeNameRegisterer(CaffeTypeId id, const std::string& literal_name) {
98+
TypeNameRegisterer(TypeIdentifier id, const std::string& literal_name) {
9999
std::lock_guard<std::mutex> guard(gTypeRegistrationMutex());
100100
#ifdef __GXX_RTTI
101101
(void)literal_name;
@@ -141,7 +141,7 @@ class TypeMeta {
141141
* type, use TypeMeta::Make<T>().
142142
*/
143143
TypeMeta() noexcept
144-
: id_(CaffeTypeId::uninitialized()), itemsize_(0), ctor_(nullptr), copy_(nullptr), dtor_(nullptr) {}
144+
: id_(TypeIdentifier::uninitialized()), itemsize_(0), ctor_(nullptr), copy_(nullptr), dtor_(nullptr) {}
145145

146146
/**
147147
* Copy constructor.
@@ -159,7 +159,7 @@ class TypeMeta {
159159
// TypeMeta can only be created by Make, making sure that we do not
160160
// create incorrectly mixed up TypeMeta objects.
161161
TypeMeta(
162-
CaffeTypeId i,
162+
TypeIdentifier i,
163163
size_t s,
164164
PlacementNew* ctor,
165165
TypedCopy* copy,
@@ -176,7 +176,7 @@ class TypeMeta {
176176
/**
177177
* Returns the type id.
178178
*/
179-
const CaffeTypeId& id() const noexcept {
179+
const TypeIdentifier& id() const noexcept {
180180
return id_;
181181
}
182182
/**
@@ -229,7 +229,7 @@ class TypeMeta {
229229
* is generated during run-time. Do NOT serialize the id for storage.
230230
*/
231231
template <typename T>
232-
CAFFE2_API static CaffeTypeId Id();
232+
CAFFE2_API static TypeIdentifier Id();
233233

234234
/**
235235
* Returns the item size of the type. This is equivalent to sizeof(T).
@@ -356,7 +356,7 @@ class TypeMeta {
356356
}
357357

358358
private:
359-
CaffeTypeId id_;
359+
TypeIdentifier id_;
360360
size_t itemsize_;
361361
PlacementNew* ctor_;
362362
TypedCopy* copy_;
@@ -393,16 +393,16 @@ inline bool operator!=(const TypeMeta& lhs, const TypeMeta& rhs) noexcept {
393393
#ifdef _MSC_VER
394394
#define CAFFE_KNOWN_TYPE(T) \
395395
template <> \
396-
CAFFE2_EXPORT CaffeTypeId TypeMeta::Id<T>() { \
397-
static const CaffeTypeId type_id = CaffeTypeId::createTypeId(); \
396+
CAFFE2_EXPORT TypeIdentifier TypeMeta::Id<T>() { \
397+
static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \
398398
static TypeNameRegisterer<T> registerer(type_id, #T); \
399399
return type_id; \
400400
}
401401
#else // _MSC_VER
402402
#define CAFFE_KNOWN_TYPE(T) \
403403
template <> \
404-
CaffeTypeId TypeMeta::Id<T>() { \
405-
static const CaffeTypeId type_id = CaffeTypeId::createTypeId(); \
404+
TypeIdentifier TypeMeta::Id<T>() { \
405+
static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \
406406
static TypeNameRegisterer<T> registerer(type_id, #T); \
407407
return type_id; \
408408
}
@@ -417,14 +417,14 @@ inline bool operator!=(const TypeMeta& lhs, const TypeMeta& rhs) noexcept {
417417
#ifdef _MSC_VER
418418
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
419419
template <> \
420-
inline CAFFE2_EXPORT CaffeTypeId TypeMeta::Id<T>() { \
421-
return CaffeTypeId(PreallocatedId); \
420+
inline CAFFE2_EXPORT TypeIdentifier TypeMeta::Id<T>() { \
421+
return TypeIdentifier(PreallocatedId); \
422422
}
423423
#else // _MSC_VER
424424
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
425425
template <> \
426-
inline CaffeTypeId TypeMeta::Id<T>() { \
427-
return CaffeTypeId(PreallocatedId); \
426+
inline TypeIdentifier TypeMeta::Id<T>() { \
427+
return TypeIdentifier(PreallocatedId); \
428428
}
429429
#endif
430430

caffe2/core/types.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ CAFFE_KNOWN_TYPE(caffe2::float16);
1313
TensorProto::DataType TypeMetaToDataType(const TypeMeta& meta) {
1414
static_assert(sizeof(int) == 4,
1515
"int in this compiler does not equal to 4 bytes.");
16-
static std::map<CaffeTypeId, TensorProto::DataType> data_type_map {
16+
static std::map<TypeIdentifier, TensorProto::DataType> data_type_map {
1717
{TypeMeta::Id<float>(), TensorProto_DataType_FLOAT},
1818
{TypeMeta::Id<int>(), TensorProto_DataType_INT32},
1919
// BYTE does not have a type meta to proto mapping: we should

caffe2/operators/quant_decode_op.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ inline void DecodeGeneral(
7373
Tensor* outDecoded,
7474
bool resizeOnly) {
7575
const static std::map<
76-
std::pair<CaffeTypeId, CaffeTypeId>,
76+
std::pair<TypeIdentifier, TypeIdentifier>,
7777
std::function<void(
7878
const Tensor& codebook,
7979
const Tensor& codes,

0 commit comments

Comments
 (0)