Validating C++ model number of elements in forward

Hi,

If someone gives me (my C++ code) a model to run, I’d like to validate the input size it requires, and the number of elements in its output. I can get the number of output elements by loading the model and unsigned n_outputs = model.forward({test_input}).toTensor().numel().

However how to size the test_input tensor? Currently I’m just making one with the dimensions suggested by a config, but would like to query the model directly. I’m assuming there is something to be done with model.get_method("forward") as suggested by How to retrieve number of outputs of a model? but instead of “num_inputs()” I’d like “number of elements required in input tensor 0”.

Thanks,
FDS

The issue is that pytorch does not require a models input layer to specify a dimension or size, only amounts of in features and out features (and kernel size maybe). You always have the option of looping over the modules within the script module to see these parameters, you just won’t see dimensions unless they are put in through customs modules. Models will not store this information unless explicitly made to do so

Thanks for the answer Charles. What I suspected. This makes it more difficult for a C++ program to validate a given TorchScript model, like you could in TensorFlow. But the fake-forward pass is better than nothing, at least allows early detection of mismatched dimensions.