Skip to content

Commit 4f34cd6

Browse files
wconstabpytorchmergebot
authored andcommitted
Replace all CHECK_ and DCHECK_ with TORCH_* macros (pytorch#82032)
Avoid exposing defines that conflict with google logging, since this blocks external usage of libtorch in certain cases. All the 'interesting' changes should be in these two files, and the rest should just be mechanical changes via sed. c10/util/logging_is_not_google_glog.h c10/util/logging_is_google_glog.h Fixes pytorch#81415 cc @miladm @malfet Pull Request resolved: pytorch#82032 Approved by: https://github.com/soumith, https://github.com/miladm
1 parent cab8192 commit 4f34cd6

File tree

150 files changed

+680
-612
lines changed

Some content is hidden

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

150 files changed

+680
-612
lines changed

aten/src/ATen/native/cpu/layer_norm_kernel.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void LayerNormKernelImplInternal(
3535
Tensor* rstd) {
3636
using T_ACC = vec::vec_scalar_t<T>;
3737
using Vec = vec::Vectorized<T_ACC>;
38-
DCHECK_EQ(X.numel(), M * N);
38+
TORCH_DCHECK_EQ(X.numel(), M * N);
3939
DCHECK(!gamma.defined() || gamma.numel() == N);
4040
DCHECK(!beta.defined() || beta.numel() == N);
4141
const T* X_data = X.data_ptr<T>();
@@ -117,10 +117,10 @@ void LayerNormBackwardKernelImplInternal(
117117
Tensor* dbeta) {
118118
using T_ACC = vec::vec_scalar_t<T>;
119119
using Vec = vec::Vectorized<T_ACC>;
120-
DCHECK_EQ(dY.numel(), M * N);
121-
DCHECK_EQ(X.numel(), M * N);
122-
DCHECK_EQ(mean.numel(), M);
123-
DCHECK_EQ(rstd.numel(), M);
120+
TORCH_DCHECK_EQ(dY.numel(), M * N);
121+
TORCH_DCHECK_EQ(X.numel(), M * N);
122+
TORCH_DCHECK_EQ(mean.numel(), M);
123+
TORCH_DCHECK_EQ(rstd.numel(), M);
124124
DCHECK(!gamma.defined() || gamma.numel() == N);
125125
const T* dY_data = dY.template data_ptr<T>();
126126
const T* X_data = X.template data_ptr<T>();

aten/src/ATen/native/quantized/cpu/qsoftmax.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Tensor qsoftmax_qnnpack(const Tensor& qx, const int64_t dim) {
9494
TORCH_CHECK(
9595
status == pytorch_qnnp_status_success,
9696
"failed to create QNNPACK Softmax operator");
97-
CHECK_NOTNULL(softargmax);
97+
TORCH_CHECK_NOTNULL(softargmax);
9898

9999
status = pytorch_qnnp_setup_softargmax_nc_q8(
100100
softargmax, batch_size, input, input_stride, output, output_stride);

benchmarks/static_runtime/deep_wide_pt_bench.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void BM_deep_wide_jit_graph_executor(benchmark::State& state) {
4747

4848
std::vector<IValue> inputs({ad_emb_packed, user_emb, wide});
4949

50-
CHECK_EQ(setenv("TORCH_JIT_DISABLE_NEW_EXECUTOR", "1", 1), 0);
50+
TORCH_CHECK_EQ(setenv("TORCH_JIT_DISABLE_NEW_EXECUTOR", "1", 1), 0);
5151

5252
mod.forward(inputs);
5353
for (auto _ : state) {
@@ -65,7 +65,7 @@ static void BM_deep_wide_jit_profiling_executor(benchmark::State& state) {
6565

6666
std::vector<IValue> inputs({ad_emb_packed, user_emb, wide});
6767

68-
CHECK_EQ(unsetenv("TORCH_JIT_DISABLE_NEW_EXECUTOR"), 0);
68+
TORCH_CHECK_EQ(unsetenv("TORCH_JIT_DISABLE_NEW_EXECUTOR"), 0);
6969

7070
mod.forward(inputs);
7171
for (auto _ : state) {

binaries/benchmark_helper.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int loadInput(
173173
LOG(INFO) << "Running on GPU.";
174174
#ifdef __CUDA_ARCH__
175175
caffe2::TensorCUDA* tensor = blob->GetMutable<caffe2::TensorCUDA>();
176-
CHECK_NOTNULL(tensor);
176+
TORCH_CHECK_NOTNULL(tensor);
177177
tensor->Resize(input_dims);
178178
if (input_type_list[i] == "uint8_t") {
179179
tensor->mutable_data<uint8_t>();
@@ -189,17 +189,17 @@ int loadInput(
189189
if (input_type_list[i] == "uint8_t") {
190190
caffe2::int8::Int8TensorCPU* tensor =
191191
blob->GetMutable<caffe2::int8::Int8TensorCPU>();
192-
CHECK_NOTNULL(tensor);
192+
TORCH_CHECK_NOTNULL(tensor);
193193
tensor->t.Resize(input_dims);
194194
tensor->t.mutable_data<uint8_t>();
195195
} else if (input_type_list[i] == "float") {
196196
caffe2::TensorCPU* tensor = BlobGetMutableTensor(blob, caffe2::CPU);
197-
CHECK_NOTNULL(tensor);
197+
TORCH_CHECK_NOTNULL(tensor);
198198
tensor->Resize(input_dims);
199199
tensor->mutable_data<float>();
200200
} else if (input_type_list[i] == "int") {
201201
caffe2::TensorCPU* tensor = BlobGetMutableTensor(blob, caffe2::CPU);
202-
CHECK_NOTNULL(tensor);
202+
TORCH_CHECK_NOTNULL(tensor);
203203
tensor->Resize(input_dims);
204204
tensor->mutable_data<int>();
205205
} else {
@@ -495,7 +495,7 @@ int benchmark(
495495
net_def.set_name("benchmark");
496496
}
497497
caffe2::NetBase* net = workspace->CreateNet(net_def);
498-
CHECK_NOTNULL(net);
498+
TORCH_CHECK_NOTNULL(net);
499499
runNetwork(
500500
workspace,
501501
net,

binaries/convert_and_benchmark.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ void runNetwork(
591591
}
592592

593593
caffe2::NetBase* net = workspace->CreateNet(net_def);
594-
CHECK_NOTNULL(net);
594+
TORCH_CHECK_NOTNULL(net);
595595

596596
LOG(INFO) << "Starting benchmark.";
597597
caffe2::ObserverConfig::initSampleRate(1, 1, 1, run_individual, warmup);

binaries/make_image_db.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void ConvertImageDataset(
251251
// Synthesize key for this entry
252252
auto key_len = snprintf(
253253
key_cstr, sizeof(key_cstr), "%08d_%s", i, lines[i].first.c_str());
254-
DCHECK_LE(key_len, sizeof(key_cstr));
254+
TORCH_DCHECK_LE(key_len, sizeof(key_cstr));
255255

256256
// Put in db
257257
transaction->Put(string(key_cstr), std::move(value));

binaries/speed_benchmark.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@ int main(int argc, char** argv) {
136136
if (input_type_list[i] == "uint8_t") {
137137
caffe2::int8::Int8TensorCPU* tensor =
138138
blob->GetMutable<caffe2::int8::Int8TensorCPU>();
139-
CHECK_NOTNULL(tensor);
139+
TORCH_CHECK_NOTNULL(tensor);
140140
tensor->t.Resize(input_dims);
141141
tensor->t.mutable_data<uint8_t>();
142142
} else if (input_type_list[i] == "float") {
143143
caffe2::TensorCPU* tensor = BlobGetMutableTensor(blob, caffe2::CPU);
144-
CHECK_NOTNULL(tensor);
144+
TORCH_CHECK_NOTNULL(tensor);
145145
tensor->Resize(input_dims);
146146
tensor->mutable_data<float>();
147147
} else {
@@ -184,7 +184,7 @@ int main(int argc, char** argv) {
184184
}
185185

186186
caffe2::NetBase* net = workspace->CreateNet(net_def);
187-
CHECK_NOTNULL(net);
187+
TORCH_CHECK_NOTNULL(net);
188188
CAFFE_ENFORCE(net->Run());
189189
net->TEST_Benchmark(FLAGS_warmup, FLAGS_iter, FLAGS_run_individual);
190190

c10/test/util/logging_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ TEST(LoggingTest, Join) {
141141

142142
TEST(LoggingTest, TestDanglingElse) {
143143
if (true)
144-
DCHECK_EQ(1, 1);
144+
TORCH_DCHECK_EQ(1, 1);
145145
else
146146
GTEST_FAIL();
147147
}

c10/util/Logging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ using EnforceNotMet = ::c10::Error;
180180
* With further usages like `CAFFE_ENFORCE_THAT(IsVector(Input(0).dims()))`
181181
*
182182
* Convenient wrappers for binary operations like CAFFE_ENFORCE_EQ are provided
183-
* too. Please use them instead of CHECK_EQ and friends for failures in
183+
* too. Please use them instead of TORCH_CHECK_EQ and friends for failures in
184184
* user-provided input.
185185
*/
186186

c10/util/Registry.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ class Registry {
6464
const RegistryPriority priority = REGISTRY_DEFAULT) {
6565
std::lock_guard<std::mutex> lock(register_mutex_);
6666
// The if statement below is essentially the same as the following line:
67-
// CHECK_EQ(registry_.count(key), 0) << "Key " << key
67+
// TORCH_CHECK_EQ(registry_.count(key), 0) << "Key " << key
6868
// << " registered twice.";
69-
// However, CHECK_EQ depends on google logging, and since registration is
70-
// carried out at static initialization time, we do not want to have an
69+
// However, TORCH_CHECK_EQ depends on google logging, and since registration
70+
// is carried out at static initialization time, we do not want to have an
7171
// explicit dependency on glog's initialization function.
7272
if (registry_.count(key) != 0) {
7373
auto cur_priority = priority_[key];

c10/util/logging_is_google_glog.h

+65
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,71 @@ INSTANTIATE_FOR_CONTAINER(set)
5050
#include <glog/logging.h>
5151

5252
// Additional macros on top of glog
53+
#ifndef NDEBUG
54+
#define TORCH_CHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
55+
#define TORCH_CHECK_NE(val1, val2) CHECK_NE(val1, val2)
56+
#define TORCH_CHECK_LE(val1, val2) CHECK_LE(val1, val2)
57+
#define TORCH_CHECK_LT(val1, val2) CHECK_LT(val1, val2)
58+
#define TORCH_CHECK_GE(val1, val2) CHECK_GE(val1, val2)
59+
#define TORCH_CHECK_GT(val1, val2) CHECK_GT(val1, val2)
60+
#define TORCH_DCHECK_EQ(val1, val2) DCHECK_EQ(val1, val2)
61+
#define TORCH_DCHECK_NE(val1, val2) DCHECK_NE(val1, val2)
62+
#define TORCH_DCHECK_LE(val1, val2) DCHECK_LE(val1, val2)
63+
#define TORCH_DCHECK_LT(val1, val2) DCHECK_LT(val1, val2)
64+
#define TORCH_DCHECK_GE(val1, val2) DCHECK_GE(val1, val2)
65+
#define TORCH_DCHECK_GT(val1, val2) DCHECK_GT(val1, val2)
66+
#else // !NDEBUG
67+
// These versions generate no code in optimized mode.
68+
#define TORCH_CHECK_EQ(val1, val2) \
69+
while (false) \
70+
CHECK_EQ(val1, val2)
71+
#define TORCH_CHECK_NE(val1, val2) \
72+
while (false) \
73+
CHECK_NE(val1, val2)
74+
#define TORCH_CHECK_LE(val1, val2) \
75+
while (false) \
76+
CHECK_LE(val1, val2)
77+
#define TORCH_CHECK_LT(val1, val2) \
78+
while (false) \
79+
CHECK_LT(val1, val2)
80+
#define TORCH_CHECK_GE(val1, val2) \
81+
while (false) \
82+
CHECK_GE(val1, val2)
83+
#define TORCH_CHECK_GT(val1, val2) \
84+
while (false) \
85+
CHECK_GT(val1, val2)
86+
#define TORCH_DCHECK_EQ(val1, val2) \
87+
while (false) \
88+
DCHECK_EQ(val1, val2)
89+
#define TORCH_DCHECK_NE(val1, val2) \
90+
while (false) \
91+
DCHECK_NE(val1, val2)
92+
#define TORCH_DCHECK_LE(val1, val2) \
93+
while (false) \
94+
DCHECK_LE(val1, val2)
95+
#define TORCH_DCHECK_LT(val1, val2) \
96+
while (false) \
97+
DCHECK_LT(val1, val2)
98+
#define TORCH_DCHECK_GE(val1, val2) \
99+
while (false) \
100+
DCHECK_GE(val1, val2)
101+
#define TORCH_DCHECK_GT(val1, val2) \
102+
while (false) \
103+
DCHECK_GT(val1, val2)
104+
#endif // NDEBUG
105+
106+
// Check that a pointer is not null.
107+
#define TORCH_CHECK_NOTNULL(val) CHECK_NOTNULL(val)
108+
109+
#ifndef NDEBUG
110+
// Debug only version of TORCH_CHECK_NOTNULL
111+
#define TORCH_DCHECK_NOTNULL(val) DCHECK_NOTNULL(val)
112+
#else // !NDEBUG
113+
// Optimized version - generates no code.
114+
#define TORCH_DCHECK_NOTNULL(val) \
115+
while (false) \
116+
DCHECK_NOTNULL(val)
117+
#endif // NDEBUG
53118

54119
// Log with source location information override (to be used in generic
55120
// warning/error handlers implemented as functions, not macros)

c10/util/logging_is_not_google_glog.h

+43-43
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ void LogMessageFatal(const char* file, int line, const T& message) {
6161
MessageLogger(file, line, GLOG_FATAL).stream() << message;
6262
}
6363

64-
// Helpers for CHECK_NOTNULL(). Two are necessary to support both raw pointers
65-
// and smart pointers.
64+
// Helpers for TORCH_CHECK_NOTNULL(). Two are necessary to support both raw
65+
// pointers and smart pointers.
6666
template <typename T>
6767
T& CheckNotNullCommon(const char* file, int line, const char* names, T& t) {
6868
if (t == nullptr) {
@@ -136,63 +136,63 @@ static_assert(
136136
::c10::MessageLogger(__FILE__, __LINE__, ::c10::GLOG_##n).stream()
137137
#endif // NDEBUG
138138

139-
#define CHECK_OP(val1, val2, op) \
139+
#define TORCH_CHECK_OP(val1, val2, op) \
140140
FATAL_IF(((val1)op(val2))) << "Check failed: " #val1 " " #op " " #val2 " (" \
141141
<< (val1) << " vs. " << (val2) << ") "
142142

143-
// Check_op macro definitions
144-
#define CHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
145-
#define CHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
146-
#define CHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
147-
#define CHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
148-
#define CHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
149-
#define CHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
143+
// TORCH_CHECK_OP macro definitions
144+
#define TORCH_CHECK_EQ(val1, val2) TORCH_CHECK_OP(val1, val2, ==)
145+
#define TORCH_CHECK_NE(val1, val2) TORCH_CHECK_OP(val1, val2, !=)
146+
#define TORCH_CHECK_LE(val1, val2) TORCH_CHECK_OP(val1, val2, <=)
147+
#define TORCH_CHECK_LT(val1, val2) TORCH_CHECK_OP(val1, val2, <)
148+
#define TORCH_CHECK_GE(val1, val2) TORCH_CHECK_OP(val1, val2, >=)
149+
#define TORCH_CHECK_GT(val1, val2) TORCH_CHECK_OP(val1, val2, >)
150150

151151
#ifndef NDEBUG
152-
// Debug only versions of CHECK_OP macros.
153-
#define DCHECK_EQ(val1, val2) CHECK_OP(val1, val2, ==)
154-
#define DCHECK_NE(val1, val2) CHECK_OP(val1, val2, !=)
155-
#define DCHECK_LE(val1, val2) CHECK_OP(val1, val2, <=)
156-
#define DCHECK_LT(val1, val2) CHECK_OP(val1, val2, <)
157-
#define DCHECK_GE(val1, val2) CHECK_OP(val1, val2, >=)
158-
#define DCHECK_GT(val1, val2) CHECK_OP(val1, val2, >)
152+
// Debug only versions of TORCH_CHECK_OP macros.
153+
#define TORCH_DCHECK_EQ(val1, val2) TORCH_CHECK_OP(val1, val2, ==)
154+
#define TORCH_DCHECK_NE(val1, val2) TORCH_CHECK_OP(val1, val2, !=)
155+
#define TORCH_DCHECK_LE(val1, val2) TORCH_CHECK_OP(val1, val2, <=)
156+
#define TORCH_DCHECK_LT(val1, val2) TORCH_CHECK_OP(val1, val2, <)
157+
#define TORCH_DCHECK_GE(val1, val2) TORCH_CHECK_OP(val1, val2, >=)
158+
#define TORCH_DCHECK_GT(val1, val2) TORCH_CHECK_OP(val1, val2, >)
159159
#else // !NDEBUG
160160
// These versions generate no code in optimized mode.
161-
#define DCHECK_EQ(val1, val2) \
162-
while (false) \
163-
CHECK_OP(val1, val2, ==)
164-
#define DCHECK_NE(val1, val2) \
165-
while (false) \
166-
CHECK_OP(val1, val2, !=)
167-
#define DCHECK_LE(val1, val2) \
168-
while (false) \
169-
CHECK_OP(val1, val2, <=)
170-
#define DCHECK_LT(val1, val2) \
171-
while (false) \
172-
CHECK_OP(val1, val2, <)
173-
#define DCHECK_GE(val1, val2) \
174-
while (false) \
175-
CHECK_OP(val1, val2, >=)
176-
#define DCHECK_GT(val1, val2) \
177-
while (false) \
178-
CHECK_OP(val1, val2, >)
161+
#define TORCH_DCHECK_EQ(val1, val2) \
162+
while (false) \
163+
TORCH_CHECK_OP(val1, val2, ==)
164+
#define TORCH_DCHECK_NE(val1, val2) \
165+
while (false) \
166+
TORCH_CHECK_OP(val1, val2, !=)
167+
#define TORCH_DCHECK_LE(val1, val2) \
168+
while (false) \
169+
TORCH_CHECK_OP(val1, val2, <=)
170+
#define TORCH_DCHECK_LT(val1, val2) \
171+
while (false) \
172+
TORCH_CHECK_OP(val1, val2, <)
173+
#define TORCH_DCHECK_GE(val1, val2) \
174+
while (false) \
175+
TORCH_CHECK_OP(val1, val2, >=)
176+
#define TORCH_DCHECK_GT(val1, val2) \
177+
while (false) \
178+
TORCH_CHECK_OP(val1, val2, >)
179179
#endif // NDEBUG
180180

181181
// Check that a pointer is not null.
182-
#define CHECK_NOTNULL(val) \
183-
::c10::CheckNotNull( \
182+
#define TORCH_CHECK_NOTNULL(val) \
183+
::c10::CheckNotNull( \
184184
__FILE__, __LINE__, "Check failed: '" #val "' Must be non NULL", (val))
185185

186186
#ifndef NDEBUG
187-
// Debug only version of CHECK_NOTNULL
188-
#define DCHECK_NOTNULL(val) \
189-
::c10::CheckNotNull( \
187+
// Debug only version of TORCH_CHECK_NOTNULL
188+
#define TORCH_DCHECK_NOTNULL(val) \
189+
::c10::CheckNotNull( \
190190
__FILE__, __LINE__, "Check failed: '" #val "' Must be non NULL", (val))
191191
#else // !NDEBUG
192192
// Optimized version - generates no code.
193-
#define DCHECK_NOTNULL(val) \
194-
while (false) \
195-
CHECK_NOTNULL(val)
193+
#define TORCH_DCHECK_NOTNULL(val) \
194+
while (false) \
195+
TORCH_CHECK_NOTNULL(val)
196196
#endif // NDEBUG
197197

198198
// ---------------------- Support for std objects --------------------------

caffe2/contrib/fakelowp/fp16_fc_acc_op.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Fp16FCAccOp final : public Operator<Context> {
7878

7979
Y_shape_cache_ = X.sizes().vec();
8080
// This is an invariant of canonical_axis, so we can DCHECK.
81-
DCHECK_LE(canonical_axis + 1, Y_shape_cache_.size());
81+
TORCH_DCHECK_LE(canonical_axis + 1, Y_shape_cache_.size());
8282
Y_shape_cache_.resize(canonical_axis + 1);
8383
Y_shape_cache_[canonical_axis] = N;
8484
Y->Resize(Y_shape_cache_);

caffe2/contrib/nccl/cuda_nccl_gpu.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ NCCLContext* getNCCLContext(const NCCLExecution& ex) {
9191
LOG(INFO) << "Creating NCCLContext for key: " << key;
9292
contexts[key].reset(new NCCLContext(ex));
9393
}
94-
return CHECK_NOTNULL(contexts[key].get());
94+
return TORCH_CHECK_NOTNULL(contexts[key].get());
9595
}
9696

9797
template <typename T>
@@ -153,7 +153,7 @@ void runNCCL(const NCCLExecution& ex, InitF&& init_f, F&& f) {
153153
auto& comm = comms[i];
154154
auto& stream = streams[i];
155155

156-
DCHECK_EQ(ctx.device, GetGPUIDForPointer(ctx.src->raw_data()));
156+
TORCH_DCHECK_EQ(ctx.device, GetGPUIDForPointer(ctx.src->raw_data()));
157157
CUDA_ENFORCE(cudaStreamWaitEvent(stream, context->master_event_, 0));
158158
f(ctx, comm, stream);
159159
}

caffe2/contrib/opencl/context.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class OpenCLContext final {
3636
public:
3737
explicit OpenCLContext();
3838
explicit OpenCLContext(const DeviceOption& option) {
39-
DCHECK_EQ(option.device_type(), PROTO_OPENCL);
39+
TORCH_DCHECK_EQ(option.device_type(), PROTO_OPENCL);
4040
OpenCLContext();
4141
}
4242
~OpenCLContext() {}

caffe2/core/common_cudnn.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ CuDNNWrapper::PerGPUCuDNNStates& CuDNNWrapper::cudnn_states() {
99
// New it (never delete) to avoid calling the destructors on process
1010
// exit and racing against the CUDA shutdown sequence.
1111
static auto* p = new CuDNNWrapper::PerGPUCuDNNStates();
12-
CHECK_NOTNULL(p);
12+
TORCH_CHECK_NOTNULL(p);
1313
return *p;
1414
}
1515

0 commit comments

Comments
 (0)