@@ -35,10 +35,10 @@ class TensorCheck {
35
35
device_index_(v.device().index()),
36
36
requires_grad_(v.requires_grad()),
37
37
sizes_(std::move(dynamic_dims_sizes)),
38
- strides_(std::move(dynamic_dims_strides)) {
38
+ strides_(std::move(dynamic_dims_strides)),
39
+ dim_(static_cast <int64_t >(sizes_.size())) {
39
40
// TODO(voz): In cases where sizes_ and strides_ are fully dynamic, should
40
41
// we just treat this as optional?
41
- dim_ = sizes_.size ();
42
42
}
43
43
44
44
// See note in guards.py [Note - On Export Tensor Guards]
@@ -157,9 +157,9 @@ typedef struct {
157
157
} TensorGuards;
158
158
159
159
static void TensorGuards_dealloc (TensorGuards* self) {
160
- if (self->checks != NULL ) {
160
+ if (self->checks != nullptr ) {
161
161
delete self->checks ;
162
- self->checks = NULL ;
162
+ self->checks = nullptr ;
163
163
}
164
164
Py_TYPE (self)->tp_free ((PyObject*)self);
165
165
}
@@ -169,7 +169,7 @@ static PyObject* TensorGuards_new(
169
169
PyObject* args,
170
170
PyObject* kwds) {
171
171
TensorGuards* self = (TensorGuards*)type->tp_alloc (type, 0 );
172
- if (self != NULL ) {
172
+ if (self != nullptr ) {
173
173
self->checks = new ChecksList ();
174
174
}
175
175
return (PyObject*)self;
@@ -191,7 +191,7 @@ static std::vector<std::optional<int64_t>> pyListToVecOptInt(PyObject* pyList) {
191
191
for (Py_ssize_t i = 0 ; i < size; i++) {
192
192
PyObject* item = PyList_GetItem (pyList, i);
193
193
if (item == Py_None) {
194
- vec.push_back (std::nullopt);
194
+ vec.emplace_back (std::nullopt);
195
195
} else {
196
196
int64_t value = PyLong_AsLongLong (item);
197
197
if (value == -1 && PyErr_Occurred ()) {
@@ -200,7 +200,7 @@ static std::vector<std::optional<int64_t>> pyListToVecOptInt(PyObject* pyList) {
200
200
" Size or stride list item is not a valid integer." );
201
201
TORCH_CHECK (false , " Size or stride list item is not a valid integer." );
202
202
}
203
- vec.push_back (value);
203
+ vec.emplace_back (value);
204
204
}
205
205
}
206
206
return vec;
@@ -231,13 +231,13 @@ static int TensorGuards_init(
231
231
// Top level structure is List[List[Union[int, None]]]
232
232
PyObject* dynamic_dims_sizes_py =
233
233
PyDict_GetItemString (kwds, " dynamic_dims_sizes" );
234
- if (dynamic_dims_sizes_py == NULL ) {
234
+ if (dynamic_dims_sizes_py == nullptr ) {
235
235
PyErr_SetString (PyExc_TypeError, " missing dynamic_dims_sizes=..." );
236
236
return -1 ;
237
237
}
238
238
PyObject* dynamic_dims_strides_py =
239
239
PyDict_GetItemString (kwds, " dynamic_dims_strides" );
240
- if (dynamic_dims_strides_py == NULL ) {
240
+ if (dynamic_dims_strides_py == nullptr ) {
241
241
PyErr_SetString (PyExc_TypeError, " missing dynamic_dims_strides=..." );
242
242
return -1 ;
243
243
}
@@ -263,11 +263,11 @@ static int TensorGuards_init(
263
263
}
264
264
auto tensor = THPVariable_Unpack (item);
265
265
std::vector<std::optional<int64_t >> tensor_dims_size =
266
- per_tensor_dynamic_dims_sizes.size () == 0
266
+ per_tensor_dynamic_dims_sizes.empty ()
267
267
? wrapIntegersInOptional (tensor.sizes ())
268
268
: per_tensor_dynamic_dims_sizes[i];
269
269
std::vector<std::optional<int64_t >> tensor_dims_stride =
270
- per_tensor_dynamic_dims_strides.size () == 0
270
+ per_tensor_dynamic_dims_strides.empty ()
271
271
? wrapIntegersInOptional (tensor.strides ())
272
272
: per_tensor_dynamic_dims_strides[i];
273
273
checks.emplace_back (
@@ -286,7 +286,7 @@ PyObject* TensorGuards_check(
286
286
PyObject* kwargs) {
287
287
if (!PyTuple_CheckExact (args)) {
288
288
PyErr_SetString (PyExc_TypeError, " expected tuple()" );
289
- return NULL ;
289
+ return nullptr ;
290
290
}
291
291
auto & checks = *self->checks ;
292
292
auto len = PyTuple_GET_SIZE (args);
@@ -295,7 +295,7 @@ PyObject* TensorGuards_check(
295
295
296
296
if (static_cast <decltype (len)>(checks.size ()) != len) {
297
297
PyErr_SetString (PyExc_TypeError, " wrong length" );
298
- return NULL ;
298
+ return nullptr ;
299
299
}
300
300
301
301
LocalState state;
@@ -330,34 +330,34 @@ PyObject* TensorGuards_check_verbose(
330
330
PyObject* kwargs) {
331
331
if (!PyTuple_CheckExact (args)) {
332
332
PyErr_SetString (PyExc_TypeError, " expected tuple()" );
333
- return NULL ;
333
+ return nullptr ;
334
334
}
335
335
auto & checks = *self->checks ;
336
336
auto len = PyTuple_GET_SIZE (args);
337
337
338
338
if (static_cast <decltype (len)>(checks.size ()) != len) {
339
339
PyErr_SetString (PyExc_TypeError, " wrong length" );
340
- return NULL ;
340
+ return nullptr ;
341
341
}
342
342
343
343
PyObject* tensor_check_names_py =
344
344
PyDict_GetItemString (kwargs, " tensor_check_names" );
345
- if (tensor_check_names_py == NULL ) {
345
+ if (tensor_check_names_py == nullptr ) {
346
346
PyErr_SetString (PyExc_TypeError, " missing tensor_check_names kwarg" );
347
- return NULL ;
347
+ return nullptr ;
348
348
}
349
349
350
350
if (!PyList_Check (tensor_check_names_py)) {
351
351
PyErr_SetString (PyExc_TypeError, " tensor_check_names kwarg must be a list" );
352
- return NULL ;
352
+ return nullptr ;
353
353
}
354
354
355
355
auto names_size = PyList_Size (tensor_check_names_py);
356
356
if (names_size != static_cast <decltype (names_size)>(checks.size ())) {
357
357
PyErr_SetString (
358
358
PyExc_TypeError,
359
359
" tensor_check_names should be the same size as # tensors" );
360
- return NULL ;
360
+ return nullptr ;
361
361
}
362
362
363
363
std::vector<std::string> tensor_check_names;
@@ -367,7 +367,7 @@ PyObject* TensorGuards_check_verbose(
367
367
if (!PyUnicode_Check (value)) {
368
368
PyErr_SetString (
369
369
PyExc_TypeError, " tensor_check_names must only contain strings" );
370
- return NULL ;
370
+ return nullptr ;
371
371
}
372
372
tensor_check_names.emplace_back (PyUnicode_AsUTF8 (value));
373
373
}
@@ -407,6 +407,7 @@ PyObject* TensorGuards_check_verbose(
407
407
Py_RETURN_TRUE;
408
408
}
409
409
410
+ // NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
410
411
static PyMethodDef TensorGuards_methods[] = {
411
412
{" check" ,
412
413
(PyCFunction)(void *)TensorGuards_check,
@@ -416,20 +417,19 @@ static PyMethodDef TensorGuards_methods[] = {
416
417
(PyCFunction)(void *)TensorGuards_check_verbose,
417
418
METH_VARARGS | METH_KEYWORDS,
418
419
" verbose fail reasons for failed checks" },
419
- {NULL } /* Sentinel */
420
+ {nullptr } /* Sentinel */
420
421
};
421
422
422
- static PyTypeObject TensorGuardsType = {
423
- // NOLINTNEXTLINE
424
- PyVarObject_HEAD_INIT (NULL , 0 )};
423
+ static PyTypeObject TensorGuardsType = {PyVarObject_HEAD_INIT (nullptr , 0 )};
425
424
426
425
static PyObject* check_type_id (PyObject* dummy, PyObject* args) {
427
426
// faster `lambda obj, expected: id(type(obj)) == expected`
428
- PyObject* obj;
429
- unsigned long long expected;
427
+ PyObject* obj = nullptr ;
428
+ unsigned long long expected = 0 ;
430
429
if (!PyArg_ParseTuple (args, " OK" , &obj, &expected)) {
431
- return NULL ;
430
+ return nullptr ;
432
431
}
432
+ // NOLINTNEXTLINE(performance-no-int-to-ptr)
433
433
if (Py_TYPE (obj) == (void *)expected) {
434
434
Py_RETURN_TRUE;
435
435
} else {
@@ -439,11 +439,12 @@ static PyObject* check_type_id(PyObject* dummy, PyObject* args) {
439
439
440
440
static PyObject* check_obj_id (PyObject* dummy, PyObject* args) {
441
441
// faster `lambda obj, expected: id(obj) == expected`
442
- PyObject* obj;
443
- unsigned long long expected;
442
+ PyObject* obj = nullptr ;
443
+ unsigned long long expected = 0 ;
444
444
if (!PyArg_ParseTuple (args, " OK" , &obj, &expected)) {
445
- return NULL ;
445
+ return nullptr ;
446
446
}
447
+ // NOLINTNEXTLINE(performance-no-int-to-ptr)
447
448
if (obj == (void *)expected) {
448
449
Py_RETURN_TRUE;
449
450
} else {
@@ -456,25 +457,25 @@ static PyObject* assert_size_stride(PyObject* dummy, PyObject* args) {
456
457
Assert that a given tensor has a given size/stride, but ignore strides
457
458
of size==1 dimensions. Implemented in C++ as this is on the hot path.
458
459
*/
459
- PyObject* item;
460
- PyObject* size;
461
- PyObject* stride;
460
+ PyObject* item = nullptr ;
461
+ PyObject* size = nullptr ;
462
+ PyObject* stride = nullptr ;
462
463
if (!PyArg_ParseTuple (args, " OOO" , &item, &size, &stride)) {
463
- return NULL ;
464
+ return nullptr ;
464
465
}
465
466
if (!THPVariable_CheckExact (item) && !THPVariable_Check (item)) {
466
467
PyErr_SetString (PyExc_TypeError, " expected Tensor()" );
467
- return NULL ;
468
+ return nullptr ;
468
469
}
469
470
if (!PyTuple_CheckExact (size) || !PyTuple_CheckExact (stride)) {
470
471
PyErr_SetString (PyExc_TypeError, " expected tuple()" );
471
- return NULL ;
472
+ return nullptr ;
472
473
}
473
474
at::Tensor tensor = THPVariable_Unpack (item);
474
475
int64_t ndim = tensor.ndimension ();
475
476
if (PyTuple_GET_SIZE (size) != ndim || PyTuple_GET_SIZE (stride) != ndim) {
476
477
PyErr_SetString (PyExc_AssertionError, " wrong number of dimensions" );
477
- return NULL ;
478
+ return nullptr ;
478
479
}
479
480
for (auto i : c10::irange (ndim)) {
480
481
int64_t want_size = THPUtils_unpackLong (PyTuple_GET_ITEM (size, i));
@@ -488,17 +489,18 @@ static PyObject* assert_size_stride(PyObject* dummy, PyObject* args) {
488
489
msg << " expected size " << actual_size << " ==" << want_size << " , stride "
489
490
<< actual_stride << " ==" << want_stride << " at dim=" << i;
490
491
PyErr_SetString (PyExc_AssertionError, msg.str ().c_str ());
491
- return NULL ;
492
+ return nullptr ;
492
493
}
493
494
}
494
495
Py_RETURN_TRUE;
495
496
}
496
497
498
+ // NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
497
499
static PyMethodDef _methods[] = {
498
- {" check_type_id" , check_type_id, METH_VARARGS, NULL },
499
- {" check_obj_id" , check_obj_id, METH_VARARGS, NULL },
500
- {" assert_size_stride" , assert_size_stride, METH_VARARGS, NULL },
501
- {NULL , NULL , 0 , NULL }};
500
+ {" check_type_id" , check_type_id, METH_VARARGS, nullptr },
501
+ {" check_obj_id" , check_obj_id, METH_VARARGS, nullptr },
502
+ {" assert_size_stride" , assert_size_stride, METH_VARARGS, nullptr },
503
+ {nullptr , nullptr , 0 , nullptr }};
502
504
503
505
static struct PyModuleDef _module = {
504
506
PyModuleDef_HEAD_INIT,
@@ -521,19 +523,18 @@ PyObject* torch_c_dynamo_guards_init() {
521
523
TensorGuardsType.tp_init = (initproc)TensorGuards_init;
522
524
TensorGuardsType.tp_new = TensorGuards_new;
523
525
524
- PyObject* m;
525
526
if (PyType_Ready (&TensorGuardsType) < 0 )
526
- return NULL ;
527
+ return nullptr ;
527
528
528
- m = PyModule_Create (&_module);
529
- if (m == NULL )
530
- return NULL ;
529
+ auto m = PyModule_Create (&_module);
530
+ if (m == nullptr )
531
+ return nullptr ;
531
532
532
533
Py_INCREF (&TensorGuardsType);
533
534
if (PyModule_AddObject (m, " TensorGuards" , (PyObject*)&TensorGuardsType) < 0 ) {
534
535
Py_DECREF (&TensorGuardsType);
535
536
Py_DECREF (m);
536
- return NULL ;
537
+ return nullptr ;
537
538
}
538
539
539
540
return m;
0 commit comments