Skip to content

Commit 4cdb423

Browse files
xianjiecorionr
authored andcommitted
[easy] improve error messages
as desc. #accept2ship
1 parent 05a009a commit 4cdb423

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

caffe2/operators/segment_reduction_op.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,21 +1488,23 @@ class AbstractLengthsOp : public Operator<Context> {
14881488
idx = indices[dataIndex];
14891489
CAFFE_ENFORCE(
14901490
0 <= idx && idx < dataSize,
1491-
"Index ",
1491+
"The ",
14921492
dataIndex,
1493-
" is out of bounds: ",
1493+
"th index from the input indices is out of bounds: ",
14941494
idx,
1495-
", range 0 to ",
1495+
" vs. valid range 0 to ",
14961496
dataSize);
14971497
} else {
14981498
idx = dataIndex;
14991499
CAFFE_ENFORCE(
1500-
idx < dataSize,
1501-
"Range ",
1500+
0 <= idx && idx < dataSize,
1501+
"When calculating the ",
15021502
rangeIndex,
1503-
" of length ",
1503+
"th output with length=",
15041504
lengths[rangeIndex],
1505-
" is out of bound ",
1505+
", the index is out of bounds: ",
1506+
idx,
1507+
" vs. valid range 0 to ",
15061508
dataSize);
15071509
}
15081510

caffe2/python/optimizer.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def __init__(self):
5555
'''
5656
def __call__(self, net, param_init_net, param, grad=None):
5757
if grad is None:
58-
assert isinstance(param, parameter_info.ParameterInfo)
58+
assert isinstance(param, parameter_info.ParameterInfo), (
59+
"Expected parameter to be of type ParameterInfo, got {}".format(
60+
param
61+
))
5962
assert param.grad is not None
6063
else:
6164
if isinstance(param, basestring):
@@ -155,7 +158,8 @@ def add_lr_multiplier(self, lr_multiplier, is_gpu_blob=False):
155158

156159
@staticmethod
157160
def dedup(net, sparse_dedup_aggregator, grad):
158-
assert (isinstance(grad, core.GradientSlice))
161+
assert isinstance(grad, core.GradientSlice), (
162+
"Dedup only works for sparse gradient, got {}".format(grad))
159163
if sparse_dedup_aggregator:
160164
return net.DeduplicateGradientSlices(
161165
grad, aggregator=sparse_dedup_aggregator)
@@ -213,11 +217,14 @@ def _run(self, net, param_init_net, param_info):
213217
grad = param_info.grad
214218
if self.base_learning_rate == 0:
215219
return
216-
assert self.base_learning_rate > 0
220+
assert self.base_learning_rate > 0, (
221+
"Expect positive base learning rate, got {}".format(
222+
self.base_learning_rate))
217223

218224
# TODO(zqq): support LARS for sparse parameters
219225
if self.lars is not None and not isinstance(grad, core.GradientSlice):
220-
assert self.lars >= 0, 'Lars offset must be nonnegative.'
226+
assert self.lars >= 0, (
227+
'Lars offset must be nonnegative, got {}'.format(self.lars))
221228
lr_lars_multiplier = net.Lars(
222229
[param, grad],
223230
self.make_unique_blob_name(str(param) + "_lars"),
@@ -317,7 +324,9 @@ def _run(self, net, param_init_net, param_info):
317324
grad = param_info.grad
318325
if self.base_learning_rate == 0:
319326
return
320-
assert self.base_learning_rate > 0
327+
assert self.base_learning_rate > 0, (
328+
"Expect positive base learning rate, got {}".format(
329+
self.base_learning_rate))
321330

322331
lr, _ = self.build_lr(
323332
net, param_init_net,
@@ -330,8 +339,8 @@ def _run(self, net, param_init_net, param_info):
330339
param_fp32, str(param) + "_momentum", value=0.)
331340
self._aux_params.local.append(momentum_data)
332341

333-
assert not isinstance(grad, core.GradientSlice), \
334-
"Doesn't support sparse gradients"
342+
assert not isinstance(grad, core.GradientSlice), (
343+
"MultiPrecisionSgd does not support sparse gradients")
335344

336345
# Copy gradient to fp32
337346
grad_fp32 = net.HalfToFloat(grad, grad + "_fp32")
@@ -404,7 +413,9 @@ def _run(self, net, param_init_net, param_info, fp32_update=False):
404413

405414
if self.base_learning_rate == 0:
406415
return
407-
assert self.base_learning_rate > 0
416+
assert self.base_learning_rate > 0, (
417+
"Expect positive base learning rate, got {}".format(
418+
self.base_learning_rate))
408419

409420
lr, _ = self.build_lr(
410421
net, param_init_net,
@@ -421,8 +432,8 @@ def _run(self, net, param_init_net, param_info, fp32_update=False):
421432

422433
self._aux_params.local.append(momentum_data)
423434

424-
assert not isinstance(grad, core.GradientSlice), \
425-
"Doesn't support sparse gradients"
435+
assert not isinstance(grad, core.GradientSlice), (
436+
"FP16Sgd does not support sparse gradients")
426437

427438
if fp32_update_flag == 0:
428439
net.FP16MomentumSGDUpdate(
@@ -462,7 +473,8 @@ def _run(self, net, param_init_net, param_info):
462473
)
463474

464475
if isinstance(param_info.grad, core.GradientSlice):
465-
assert "Weight decay does not yet support sparse gradients"
476+
raise ValueError(
477+
"Weight decay does not yet support sparse gradients")
466478
else:
467479
net.WeightedSum(
468480
[param_info.grad, ONE, param_info.blob, WD],
@@ -493,7 +505,8 @@ def _run(self, net, param_init_net, param_info):
493505
return
494506

495507
if self.lars is not None and not isinstance(grad, core.GradientSlice):
496-
assert self.lars >= 0, 'Lars offset must be nonnegative.'
508+
assert self.lars >= 0, (
509+
'Lars offset must be nonnegative, got {}'.format(self.lars))
497510
lr_lars_multiplier = net.Lars(
498511
[param, grad],
499512
self.make_unique_blob_name(str(param) + "_lars"),
@@ -796,7 +809,7 @@ def _run(self, net, param_init_net, param_info):
796809

797810
assert self.alpha > 0
798811
assert not isinstance(grad, core.GradientSlice), \
799-
"Doesn't support sparse gradients"
812+
"YellowFin does not support sparse gradients"
800813

801814
if not param_init_net.BlobIsDefined(_OPTIMIZER_ITERATION_NAME):
802815
# Add training operators.

0 commit comments

Comments
 (0)