[C++] Correctly handle scalar inputs in reduction ops, enforce Transpose perm attribute matches input rank. (#17041)
### Description
This PR addresses the following issues related to the use of the
functions in ORT.
- https://github.com/microsoft/onnxruntime/issues/16492
- https://github.com/microsoft/onnxruntime/issues/16997
- https://github.com/microsoft/onnxruntime/issues/14678
- Partially addresses
https://github.com/microsoft/onnxruntime/issues/16813
The optimization case for a scalar input did not correctly recognize it
as such.
Transpose kernel assumed that `perm` attribute would always match input
tensor rank.
### Motivation and Context
The issues causes crashes and erratic behavior.