Repository: pytorch
Issue: test_doc_template is not working correctly
Labels: module: tests,triaged

## 🐛 Bug

Currently the `test_doc_template` is

```python
        def test_doc_template(self) -> None:
            from torch._torch_docs import __file__ as doc_file
            from torch._torch_docs import multi_dim_common, single_dim_common, factory_common_args, factory_like_common_args

            with open(doc_file, "r") as f:
                doc_strs = f.read()

            for doc_str in re.findall(r'add_docstr\((.*?),.*?("""|\'\'\')(.*?)("""|\'\'\')\)', doc_strs, re.MULTILINE | re.DOTALL):
                for common_args in [multi_dim_common, single_dim_common, factory_common_args, factory_like_common_args]:
                    for k, v in common_args.items():
                        self.assertNotIn(v, doc_str[2], 'The argument description "{}" in {} can be '
                                                        'replaced by {{{}}}'.format(v, doc_str[0], k))
```

Unfortunately, the regex used can not split individual docs correctly

## To Reproduce

Add a `self.assertNotIn('add_docstr', doc_str[2])` so you will have

```python
        def test_doc_template(self) -> None:
            from torch._torch_docs import __file__ as doc_file
            from torch._torch_docs import multi_dim_common, single_dim_common, factory_common_args, factory_like_common_args

            with open(doc_file, "r") as f:
                doc_strs = f.read()

            for doc_str in re.findall(r'add_docstr\((.*?),.*?("""|\'\'\')(.*?)("""|\'\'\')\)', doc_strs, re.MULTILINE | re.DOTALL):
                for common_args in [multi_dim_common, single_dim_common, factory_common_args, factory_like_common_args]:
                    for k, v in common_args.items():
                        self.assertNotIn('add_docstr', doc_str[2])   # <-----
                        self.assertNotIn(v, doc_str[2], 'The argument description "{}" in {} can be '
                                                        'replaced by {{{}}}'.format(v, doc_str[0], k))
```

Run the test, you will see failures:

```
_________________________________________________________________________________________________________________________ TestTorch.test_doc_template __________________________________________________________________________________________________________________________

self = <test_torch.TestTorch testMethod=test_doc_template>

    def test_doc_template(self) -> None:
        from torch._torch_docs import __file__ as doc_file
        from torch._torch_docs import multi_dim_common, single_dim_common, factory_common_args, factory_like_common_args
    
        with open(doc_file, "r") as f:
            doc_strs = f.read()
    
        for doc_str in re.findall(r'add_docstr\((.*?),.*?("""|\'\'\')(.*?)("""|\'\'\')\)', doc_strs, re.MULTILINE | re.DOTALL):
            for common_args in [multi_dim_common, single_dim_common, factory_common_args, factory_like_common_args]:
                for k, v in common_args.items():
>                   self.assertNotIn('add_docstr', doc_str[2])
E                   AssertionError: 'add_docstr' unexpectedly found in '\nabs(input, *, out=None) -> Tensor\n\nComputes the absolute value of each element in :attr:`input`.\n\n.. math::\n    \\text{out}_{i} = |\\text{input}_{i}|\n""" + r"""\nArgs:\n    {input}\n\nKeyword args:\n    {out}\n\nExample::\n\n    >>> torch.abs(torch.tensor([-1, -2, 3]))\n    tensor([ 1,  2,  3])\n""".format(**common_args))\n\nadd_docstr(torch.absolute,\n           r"""\nabsolute(input, *, out=None) -> Tensor\n\nAlias for :func:`torch.abs`\n""".format(**common_args))\n\nadd_docstr(torch.acos, r"""\nacos(input, *, out=None) -> Tensor\n\nComputes the inverse cosine of each element in :attr:`input`.\n\n.. math::\n    \\text{out}_{i} = \\cos^{-1}(\\text{input}_{i})\n""" + r"""\nArgs:\n    {input}\n\nKeyword args:\n    {out}\n\nExample::\n\n    >>> a = torch.randn(4)\n    >>> a\n    tensor([ 0.3348, -0.5889,  0.2005, -0.1584])\n    >>> torch.acos(a)\n    tensor([ 1.2294,  2.2004,  1.3690,  1.7298])\n""".format(**common_args))\n\nadd_docstr(torch.arccos, r"""\narccos(input, *, out=None) -> Tensor\n\nAlias for :func:`torch.acos`.\n'

test/test_torch.py:150: AssertionError

```

## Additional context

I suggest using `ast.parse` to handle that file more robustly


cc @mruberry @VitalyFedyunin