Well, you cannot raise in CUDA device code and expect that to propagate to host C/Python before synchronization takes place.
That said, in cuda kernels you can use assert(condition);. On the host AT_CHECK(cond, "error message with ", mytensor.dims(), " dynamic content"); works.
rgrep in aten/src/ will find examples of either.
Best regards
Thomas