Throwing run-time errors conditionally

Is it possible to throw a run-time error based on the value of a tensor?

The use case we’re encountering for the WL net framework would be if a tensor is being used to index into another using gather_nd (which is being used to implement a higher-level layer). To prevent a crash, we wish to detect out-of-bounds indices and halt the net evaluation, warning the user by raising a RuntimeError. I don’t see a way of doing this conditionally based on the value of the tensor, currently. Am I missing something? This seems a useful general mechanism to have.

Hi @taliesinb,

Working with imperative NDArray operation in (non-hybridized) Gluon models let you do this quite easily since you can inspect array values (of weights and outputs for example) and just use Python control flow operations. You can inspect values as you run the code, so something like this would work based on the output of a Block;

import mxnet as mx

data = mx.nd.random_normal(shape=(2,))

if data[1].asscalar() > 0:
    print("I'm positive")
    raise Exception("I'm negative")

If you’re just using this for debugging purposes, most IDEs allow you to set conditional break points, so you can set them based on the values in arrays.