How to generate runtime exceptions conditionally

If there are operators that have undesirable behavior (or crashes) when their input data takes on specific values, it can be desirable to thrown an exception instead of allowing the forward pass to complete. for example, one might want to check that all values of a tensor are between 0 and 1, and throw an exception if not.

it is certainly possible to generate a (1,) tensor that contains a 1 if this condition is true and 0 otherwise, but as far as I know you can’t use that value to then throw an exception, you would have to make that an output of the entire net and check it after the forward pass has completed.

is there a way to check the value of a tensor and conditionally throw an exception, currently?

checking the value requires synchronization somewhere in the network, so I guess a convenient way is to insert a custom operator which assert your desired values

1 Like