blob: 87ee0830e01b2b0367e3dfa5d3737a3e77426c35 [file] [log] [blame]
Pearu Peterson456d96d2022-02-16 18:40:19 -08001"""This script updates the file torch/_masked/_docs.py that contains
2the generated doc-strings for various masked operations. The update
3should be triggered whenever a new masked operation is introduced to
4torch._masked package. Running the script requires that torch package
5is functional.
6"""
7
8import os
9
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040010
Pearu Peterson456d96d2022-02-16 18:40:19 -080011def main() -> None:
12
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040013 target = os.path.join("torch", "_masked", "_docs.py")
Pearu Peterson456d96d2022-02-16 18:40:19 -080014
15 try:
16 import torch
17 except ImportError as msg:
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040018 print(f"Failed to import torch required to build {target}: {msg}")
Pearu Peterson456d96d2022-02-16 18:40:19 -080019 return
20
21 if os.path.isfile(target):
22 with open(target) as _f:
23 current_content = _f.read()
24 else:
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040025 current_content = ""
Pearu Peterson456d96d2022-02-16 18:40:19 -080026
27 _new_content = []
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040028 _new_content.append(
29 """\
Pearu Peterson456d96d2022-02-16 18:40:19 -080030# -*- coding: utf-8 -*-
31# This file is generated, do not modify it!
32#
33# To update this file, run the update masked docs script as follows:
34#
35# python tools/update_masked_docs.py
36#
37# The script must be called from an environment where the development
38# version of torch package can be imported and is functional.
39#
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040040"""
41 )
Pearu Peterson456d96d2022-02-16 18:40:19 -080042
43 for func_name in sorted(torch._masked.__all__):
44 func = getattr(torch._masked, func_name)
45 func_doc = torch._masked._generate_docstring(func)
46 _new_content.append(f'{func_name}_docstring = """{func_doc}"""\n')
47
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040048 new_content = "\n".join(_new_content)
Pearu Peterson456d96d2022-02-16 18:40:19 -080049
50 if new_content == current_content:
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040051 print(f"Nothing to update in {target}")
Pearu Peterson456d96d2022-02-16 18:40:19 -080052 return
53
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040054 with open(target, "w") as _f:
Pearu Peterson456d96d2022-02-16 18:40:19 -080055 _f.write(new_content)
56
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040057 print(f"Successfully updated {target}")
Pearu Peterson456d96d2022-02-16 18:40:19 -080058
59
Edward Z. Yanga11c1bb2022-04-20 08:51:02 -040060if __name__ == "__main__":
Pearu Peterson456d96d2022-02-16 18:40:19 -080061 main()