Works on #141828
This commit adds to `tools/pyi/gen_pyi.py` to generate more …type hints in `_nn.pyi`. I mainly created the hints by looking at the corresponding .h file in build/atn/src/Aten/ops, but there were often inconsistencies in the header files (such as excluding an out parameter or naming the first input "self" when in python it's "input") so I made tests to validate my hints that I didn't include in the commit. An example of one of these tests is attached at the bottom of the pr.
I made a significant amount of progress, but there's still more to be done. Adding more stubs would be great, and some of the existing type stubs need to be fixed as I tested a few and they were missing an out parameter in their stub or the first input parameter was named "self" in the stub but the actual function's was "input".
I added the hints for cross_entropy_loss, binary_cross_entropy, l1_loss, smooth_l1_loss, max_pool2d_with_indices, max_pool3d_with_indices, huber_loss, mse_loss, multilabel_margin_loss, soft_margin_loss, multi_margin_loss, upsample_nearest1d, upsample_nearest2d, upsample_nearest3d, _upsample_nearest_exact1d, _upsample_nearest_exact2d, and _upsample_nearest_exact3d,
but
max_unpool3d, adaptive_avg_pool2d, adaptive_avg_pool3d, glu, relu6_, relu6, elu, hardsigmoid_, silu_, silu, mish_, mish, hardswish_, hardswish, nll_loss_nd, upsample_linear1d, _upsample_bilinear2d_aa, upsample_bilinear2d, upsample_trilinear3d, _upsample_bicubic2d_aa, upsample_bicubic2d, im2col, col2im
all still need hints.
Here's an example of the tests I didn't include:
```python
def test_cross_entropy_loss():
"""
cross_entropy_loss": [
"def cross_entropy_loss({}) -> Tensor: ...".
format(
", ".join(
[
"input: Tensor",
"target: Tensor",
"weight: Optional[Tensor] = None",
"reduction: int = 1",
"ignore_index: int = -100",
"label_smoothing: float = 0.0",
]
)
)
"""
try:
torch._C._nn.cross_entropy_loss(InvalidType(), InvalidType())
raise Exception # these make sure the above code raises an error
except Exception as e:
assert e.__str__() == "cross_entropy_loss(): argument 'input' (position 1) must be Tensor, not InvalidType", e.__str__()
try:
torch._C._nn.cross_entropy_loss(t, InvalidType())
raise Exception
except Exception as e:
assert e.__str__() == "cross_entropy_loss(): argument 'target' (position 2) must be Tensor, not InvalidType", e.__str__()
try:
torch._C._nn.cross_entropy_loss(t, t, InvalidType())
raise Exception
except Exception as e:
assert e.__str__() == "cross_entropy_loss(): argument 'weight' (position 3) must be Tensor, not InvalidType", e.__str__()
try:
torch._C._nn.cross_entropy_loss(t, t, t, InvalidType())
raise Exception
except Exception as e:
assert e.__str__() == "cross_entropy_loss(): argument 'reduction' (position 4) must be int, not InvalidType", e.__str__()
try:
torch._C._nn.cross_entropy_loss(t, t, t, i, InvalidType())
raise Exception
except Exception as e:
assert e.__str__() == "cross_entropy_loss(): argument 'ignore_index' (position 5) must be int, not InvalidType", e.__str__()
try:
torch._C._nn.cross_entropy_loss(t, t, t, i, i, InvalidType())
raise Exception
except Exception as e:
assert e.__str__() == "cross_entropy_loss(): argument 'label_smoothing' (position 6) must be float, not InvalidType", e.__str__()
try:
torch._C._nn.cross_entropy_loss(t, t, t, i, i, f)
raise Exception
except Exception as e:
assert e.__str__() == "ignore_index is not supported for floating point target", e.__str__() # means it ran the code
try:
torch._C._nn.cross_entropy_loss(t, t, t, i, i, f, out=InvalidType())
raise Exception
except Exception as e:
assert e.__str__() == "cross_entropy_loss() got an unexpected keyword argument 'out'", e.__str__()
```