You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

comments_specification_en.md 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. # MindSpore API Comment Specifications
  2. <!-- TOC -->
  3. - [MindSpore API Comment Specifications](#mindspore-api-comment-specifications)
  4. - [Overview](#overview)
  5. - [Python API Comment Specifications](#python-api-comment-specifications)
  6. - [Comment Format](#comment-format)
  7. - [Precautions](#precautions)
  8. - [Python Example](#python-example)
  9. - [Class](#class)
  10. - [Method](#method)
  11. - [Formula](#formula)
  12. - [Link](#link)
  13. - [C++ API Comment Specifications](#c-api-comment-specifications)
  14. <!-- /TOC -->
  15. ## Overview
  16. - MindSpore Python code comments comply with [Google Python Style Guide](https://google.github.io/styleguide/pyguide.html). An API document is automatically generated by the Sphinx tool. For comment examples and support details, see [Example Google Style Python Docstrings](https://www.sphinx-doc.org/en/master/usage/extensions/example_google.html) and [Support for NumPy and Google style docstrings](https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html).
  17. - MindSpore C++ code needs to be compiled in Markdown files based on the namespace design.
  18. ## Python API Comment Specifications
  19. ### Comment Format
  20. The comments of classes and methods are in the following format:
  21. ```text
  22. Summary.
  23. More elaborate description.
  24. .. warning::
  25. Warning description.
  26. Note:
  27. Note description.
  28. Args:
  29. Arg1 (Type): Description. Default: xxx.
  30. Arg2 (Type): Description.
  31. - Sub-argument1 or Value1 of Arg2: Description.
  32. - Sub-argument2 or Value2 of Arg2: Description.
  33. Returns:
  34. Type, description.
  35. Raises:
  36. Type: Description.
  37. Examples:
  38. >>> Sample Code
  39. ```
  40. The format items are described as follows:
  41. - `Summary`: briefly describes the API function.
  42. - `More elaborate description`: describes the function and usage of an API in detail.
  43. - `warning`: describes warnings for using an API to avoid negative consequences.
  44. - `Note`: describes precautions for using an API. Do not use `Notes`.
  45. - `Args`: API parameter information, including the parameter name, type, value range, and default value.
  46. - `Returns`: return value information, including the return value type.
  47. - `Raises`: exception information, including the exception type and meaning.
  48. - `Examples`: sample code
  49. For comments of operators and cells, add `Inputs`, `Outputs` and `Supported Platforms` before `Examples`.
  50. - `Inputs` and `Outputs`: describes the input and output types and shapes of the operators after instantiation. The input name can be the same as that in the example. It is recommended to provide the corresponding mathematical formula in the comment.
  51. - `Supported Platforms`: describes the hardware platforms supported by the operator. Add `` before and after the platform name, and use space to separate them when there are more than one.
  52. ```text
  53. Inputs:
  54. - **input_name1** (Type) - Description.
  55. - **input_name2** (Type) - Description.
  56. Outputs:
  57. Type and shape, description.
  58. Supported Platforms:
  59. ``Ascend`` ``GPU`` ``CPU``
  60. ```
  61. ### Precautions
  62. - Overall Requirements
  63. - The comment items required for a class or method are as follows: `Summary`, `Args`, `Returns`, and `Raises`. If a function does not contain related information (such as `Args`, `Returns`, and `Raises`), do not write None (for example, `Raises: None`), but directly omit the comment item.
  64. - When an API is generated by directory, the comments in the \_\_init\_\_ file header are displayed at the beginning of the web page. When an API is generated by file, the comments at the beginning of the file are displayed at the beginning of the web page. The comments must contain the overall description of the corresponding modules.
  65. - If a comment contains a backslash (\\), change `"""` in the header to `r"""`.
  66. - Colon requirements: Keywords (such as `Args` and `Returns`) and parameter names (such as `Arg1` or `Arg2`) are followed by colons (:). A colon must be followed by a space. The content of `Summary` and `Returns` cannot contain colons.
  67. - Blank line requirements:
  68. A blank line is required between contents of different types (such as `Args` and `Returns`). A blank line is not required between contents of the same type (for example, `Arg1` and `Arg2`).
  69. ```text
  70. High-Level API for Training or Testing.
  71. Args:
  72. network (Cell): A training or testing network.
  73. loss_fn (Cell): Objective function, if loss_fn is None, the
  74. network should contain the logic of loss and grads calculation, and the logic
  75. of parallel if needed. Default: None.
  76. Returns:
  77. function, original function.
  78. ```
  79. If the content is described in an unordered or ordered list, a blank line must be added between the content in the list and the content above the list.
  80. ```text
  81. Args:
  82. amp_level (str): Option for argument `level` in `mindspore.amp.build_train_network`, level for mixed
  83. precision training. Supports ["O0", "O2", "O3", "auto"]. Default: "O0".
  84. - O0: Do not change.
  85. - O2: Cast network to float16, keep batchnorm run in float32, using dynamic loss scale.
  86. - O3: Cast network to float16, with additional property 'keep_batchnorm_fp32=False'.
  87. - auto: Set to level to recommended level in different devices. Set level to O2 on GPU, Set
  88. level to O3 Ascend. The recommended level is choose by the export experience, cannot
  89. always generalize. User should specify the level for special network.
  90. O2 is recommended on GPU, O3 is recommended on Ascend.
  91. ```
  92. - Space requirements:
  93. The newline characters of `Args` and `Raises` must be indented by four spaces.
  94. ```text
  95. Args:
  96. lr_power (float): Learning rate power controls how the learning rate decreases during training,
  97. must be less than or equal to zero. Use fixed learning rate if `lr_power` is zero.
  98. use_locking (bool): If `True`, the var and accumulation tensors will be protected from being updated.
  99. Default: False.
  100. Raises:
  101. TypeError: If `lr`, `l1`, `l2`, `lr_power` or `use_locking` is not a float.
  102. If `use_locking` is not a bool.
  103. If dtype of `var`, `accum`, `linear` or `grad` is neither float16 nor float32.
  104. If dtype of `indices` is not int32.
  105. ```
  106. The following contents do not need indents and are aligned with the start position of the previous line.
  107. 1. The sub-parameters or values of `Args`
  108. ```text
  109. Args:
  110. parallel_mode (str): There are five kinds of parallel modes, "stand_alone", "data_parallel",
  111. "hybrid_parallel", "semi_auto_parallel" and "auto_parallel". Default: "stand_alone".
  112. - stand_alone: Only one processor is working.
  113. - data_parallel: Distributes the data across different processors.
  114. - hybrid_parallel: Achieves data parallelism and model parallelism
  115. manually.
  116. - semi_auto_parallel: Achieves data parallelism and model parallelism by
  117. setting parallel strategies.
  118. ```
  119. 2. The line breaks for disordered or ordered content of `Inputs`, `Outputs` and `Returns`
  120. ```text
  121. Inputs:
  122. - **var** (Parameter) - The variable to be updated. The data type must be float16 or float32.
  123. - **accum** (Parameter) - The accumulation to be updated, must be same data type and shape as `var`.
  124. - **linear** (Parameter) - the linear coefficient to be updated, must be same data type and shape
  125. as `var`.
  126. - **grad** (Tensor) - A tensor of the same type as `var`, for the gradient.
  127. - **indices** (Tensor) - A vector of indices in the first dimension of `var` and `accum`.
  128. The shape of `indices` must be the same as `grad` in the first dimension. The type must be int32.
  129. Outputs:
  130. Tuple of 3 Tensor, the updated parameters.
  131. - **var** (Tensor) - Tensor, has the same shape
  132. and data type as `var`.
  133. - **accum** (Tensor) - Tensor, has the same shape
  134. and data type as `accum`.
  135. - **linear** (Tensor) - Tensor, has the same shape
  136. and data type as `linear`.
  137. ```
  138. 3. `Note` and `warning`
  139. ```text
  140. .. warning::
  141. This is warning text. Use a warning for information the user must
  142. understand to avoid negative consequences.
  143. If warning text runs over a line, make sure the lines wrap and are indented to
  144. the same level as the warning tag.
  145. ```
  146. In `Args`, there must be a space between the parameter name and the `(` of the type.
  147. ```text
  148. Args:
  149. lr (float): The learning rate value, must be positive.
  150. ```
  151. - `Args` Comment
  152. - Common parameter types are as follows:
  153. - Basic data types: `int`, `float`, `bool`, `str`, `list`, `dict`, `set`, `tuple` and `numpy.ndarray`.
  154. ```text
  155. Args:
  156. arg1 (int): Some description.
  157. arg2 (float): Some description.
  158. arg3 (bool): Some description.
  159. arg4 (str): Some description.
  160. arg5 (list): Some description.
  161. arg6 (dict): Some description.
  162. arg7 (set): Some description.
  163. arg8 (tuple): Some description.
  164. arg9 (numpy.ndarray): Some description.
  165. ```
  166. - dtype: For the value of mindspore.dtype, set this parameter to `mindspore.dtype`. For the value of the numpy type, set this parameter to `numpy.dtype`. Set other parameters based on the actual requirements.
  167. ```text
  168. Args:
  169. arg1 (mindspore.dtype): Some description.
  170. ```
  171. - One parameter with multiple optional types: Union [type 1, type 2], for example, `Union[Tensor, Number]`.
  172. ```text
  173. Args:
  174. arg1 (Union[Tensor, Number]): Some description.
  175. ```
  176. - List type: list[Specific type], for example, `list[str]`.
  177. ```text
  178. Args:
  179. arg1 (list[str]): Some description.
  180. ```
  181. - The format of optional types is as follows: (Type, optional).
  182. ```text
  183. Args:
  184. arg1 (bool, optional): Some description.
  185. ```
  186. - Other types: Tensor, other specific types, or method names.
  187. ```text
  188. Args:
  189. arg1 (Tensor): Some description.
  190. ```
  191. - `Returns` Comment
  192. - If the return value type or dimension changes, describe the relationship between the return value and the input.
  193. - If there are multiple return values, write them in different lines. The line difference is not displayed on the web page. The unordered list supports return values in different lines.
  194. ```text
  195. Returns:
  196. - DatasetNode, the root node of the IR tree.
  197. - Dataset, the root dataset of the IR tree.
  198. ```
  199. - `Examples` Comment
  200. - For the content in `Examples`, ```>>>``` should be added at the beginning of each line of code. For multiple lines (including classes, function definitions or manual switch line) and blank lines, ```...``` is needed at the beginning of these lines. There is no need to add any symbols at the beginning of the output result line.
  201. ```text
  202. Examples:
  203. >>> import mindspore as ms
  204. >>> import mindspore.nn as nn
  205. >>> class Net(nn.Cell):
  206. ... def __init__(self, dense_shape):
  207. ... super(Net, self).__init__()
  208. ... self.dense_shape = dense_shape
  209. ... def construct(self, indices, values):
  210. ... x = SparseTensor(indices, values, self.dense_shape)
  211. ... return x.values, x.indices, x.dense_shape
  212. ...
  213. >>> indices = Tensor([[0, 1], [1, 2]])
  214. >>> values = Tensor([1, 2], dtype=ms.float32)
  215. >>> out = Net((3, 4))(indices, values)
  216. >>> print(out[0])
  217. [1. 2.]
  218. >>> print(out[1])
  219. [[0 1]
  220. [1 2]]
  221. >>> print(out[2])
  222. (3, 4)
  223. ```
  224. - Actual code needs to be provided in `Examples`. If you need to refer to other Examples, use Note.
  225. - The comments of the ops operator are written in PyNative mode. If the operator can be executed, the execution result must be provided.
  226. - Import can be omitted in the case of industry consensus, such as np and nn.
  227. - If the import path is long or a user-defined alias is required, add `from xxx import xxx as something` or `import xxx`. If the import path is short, place it in the code.
  228. - `Inputs` and `Outputs` Comment
  229. - If the data type is Tensor, describe the shape in the format of :math:\`(N, C, X)\`.
  230. - Formula
  231. - Line formula (in the middle of the singly occupied line)
  232. ```text
  233. .. math::
  234. formula
  235. ```
  236. - Line-embedded formula (displayed together with other peer text, not in the middle)
  237. ```text
  238. xxx :math:`formula` xxx
  239. ```
  240. - If the formula contains an underscored variable and the underscore is followed by multiple letters (for example, xxx_yyy) , select one of the following methods based on the site requirements:
  241. 1. Multiple letters are enclosed in braces ({}), for example, xxx_{yyy}. The content following the underscore can be used as the subscript, which is displayed as $xxx_{yyy}$.
  242. 2. If a backslash (\\) is added before an underscore (_), for example, xxx\\_yyy, the complete variable name is displayed as xxx_yyy.
  243. - Parent Class Method Display
  244. - By default, the parent class method is not displayed.
  245. - You can add `:inherited-members:` to the module of the RST file in the Sphinx project to specify the parent class method to be displayed. For details, see <https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html>.
  246. - Link
  247. - Only the title (such as the name in the following example) is displayed. The detailed address is not displayed.
  248. Write a quotation in the following format:
  249. ```text
  250. `name`_
  251. ```
  252. Provide a link in the following format:
  253. ```text
  254. .. _`name`: https://xxx
  255. ```
  256. Note:
  257. - If there is a newline character, indent it. For details, see the following table.
  258. - There must be a space before https.
  259. Alternatively, you can use the following simplified format, that is, write only in the place where the reference is made.
  260. ```text
  261. `name <https://xxx>`_
  262. ```
  263. - Display the detailed address:
  264. ```text
  265. https://xxx
  266. ```
  267. - Table (For details, see section <https://sublime-and-sphinx-guide.readthedocs.io/en/latest/tables.html#list-table-directive>.)
  268. ```text
  269. .. list-table:: Title # Table title
  270. :widths: 25 25 25 # Table column width
  271. :header-rows: 1
  272. * - Heading row 1, column 1 # Table header
  273. - Heading row 1, column 2
  274. - Heading row 1, column 3
  275. * - Row 1, column 1
  276. - #The table is empty.
  277. - Row 1, column 3
  278. * - Row 2, column 1
  279. - Row 2, column 2
  280. - Row 2,
  281. # If a newline is required for the table content, add a blank line in the middle.
  282. column 3
  283. ```
  284. Display effect:
  285. ![image](./resource/list_table.png)
  286. - By default, the detailed description is displayed in one line. If you need to display it in another line, write it in the form of a list or code-block.
  287. - List mode:
  288. ```text
  289. - Content1
  290. - Content2
  291. - Content3
  292. ```
  293. - Code-Block mode:
  294. ```text
  295. .. code-block::
  296. Content1
  297. Content2
  298. Content3
  299. ```
  300. ### Python Example
  301. #### Class
  302. ```python
  303. class Tensor(Tensor_):
  304. """
  305. Tensor is used for data storage.
  306. Tensor inherits tensor object in C++.
  307. Some functions are implemented in C++ and some functions are implemented in Python.
  308. Args:
  309. input_data (Tensor, float, int, bool, tuple, list, numpy.ndarray): Input data of the tensor.
  310. dtype (:class:`mindspore.dtype`): Input data should be None, bool or numeric type defined in `mindspore.dtype`.
  311. The argument is used to define the data type of the output tensor. If it is None, the data type of the
  312. output tensor will be as same as the `input_data`. Default: None.
  313. Outputs:
  314. Tensor, with the same shape as `input_data`.
  315. Examples:
  316. >>> # initialize a tensor with input data
  317. >>> t1 = Tensor(np.zeros([1, 2, 3]), mindspore.float32)
  318. >>> assert isinstance(t1, Tensor)
  319. >>> assert t1.shape == (1, 2, 3)
  320. >>> assert t1.dtype == mindspore.float32
  321. ...
  322. >>> # initialize a tensor with a float scalar
  323. >>> t2 = Tensor(0.1)
  324. >>> assert isinstance(t2, Tensor)
  325. >>> assert t2.dtype == mindspore.float64
  326. """
  327. def __init__(self, input_data, dtype=None):
  328. ...
  329. ```
  330. For details about the display effect, click [here](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.html#mindspore.Tensor).
  331. #### Method
  332. ```python
  333. def ms_function(fn=None, obj=None, input_signature=None):
  334. """
  335. Create a callable MindSpore graph from a python function.
  336. This allows the MindSpore runtime to apply optimizations based on graph.
  337. Args:
  338. fn (Function): The Python function that will be run as a graph. Default: None.
  339. obj (Object): The Python Object that provides the information for identifying the compiled function. Default:
  340. None.
  341. input_signature (MetaTensor): The MetaTensor which describes the input arguments. The MetaTensor specifies
  342. the shape and dtype of the Tensor and they will be supplied to this function. If input_signature
  343. is specified, each input to `fn` must be a `Tensor`. And the input parameters of `fn` cannot accept
  344. `**kwargs`. The shape and dtype of actual inputs should keep the same as input_signature. Otherwise,
  345. TypeError will be raised. Default: None.
  346. Returns:
  347. Function, if `fn` is not None, returns a callable function that will execute the compiled function; If `fn` is
  348. None, returns a decorator and when this decorator invokes with a single `fn` argument, the callable function is
  349. equal to the case when `fn` is not None.
  350. Examples:
  351. >>> def tensor_add(x, y):
  352. ... z = F.tensor_add(x, y)
  353. ... return z
  354. ...
  355. >>> @ms_function
  356. ... def tensor_add_with_dec(x, y):
  357. ... z = F.tensor_add(x, y)
  358. ... return z
  359. ...
  360. >>> @ms_function(input_signature=(MetaTensor(mindspore.float32, (1, 1, 3, 3)),
  361. ... MetaTensor(mindspore.float32, (1, 1, 3, 3))))
  362. ... def tensor_add_with_sig(x, y):
  363. ... z = F.tensor_add(x, y)
  364. ... return z
  365. ...
  366. >>> x = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
  367. >>> y = Tensor(np.ones([1, 1, 3, 3]).astype(np.float32))
  368. ...
  369. >>> tensor_add_graph = ms_function(fn=tensor_add)
  370. >>> out = tensor_add_graph(x, y)
  371. >>> out = tensor_add_with_dec(x, y)
  372. >>> out = tensor_add_with_sig(x, y)
  373. """
  374. ...
  375. ```
  376. For details about the display effect, click [here](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.html#mindspore.ms_function).
  377. #### Formula
  378. ```python
  379. class Conv2d(_Conv):
  380. r"""
  381. 2D convolution layer.
  382. Applies a 2D convolution over an input tensor which is typically of shape :math:`(N, C_{in}, H_{in}, W_{in})`,
  383. where :math:`N` is batch size, :math:`C_{in}` is channel number, and :math:`H_{in}, W_{in})` are height and width.
  384. For each batch of shape :math:`(C_{in}, H_{in}, W_{in})`, the formula is defined as:
  385. .. math::
  386. out_j = \sum_{i=0}^{C_{in} - 1} ccor(W_{ij}, X_i) + b_j,
  387. ...
  388. """
  389. ```
  390. For details about the display effect, click [here](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.nn.html#mindspore.nn.Conv2d).
  391. #### Link
  392. ```python
  393. class BatchNorm(PrimitiveWithInfer):
  394. r"""
  395. Batch Normalization for input data and updated parameters.
  396. Batch Normalization is widely used in convolutional neural networks. This operation
  397. applies Batch Normalization over input to avoid internal covariate shift as described
  398. in the paper `Batch Normalization: Accelerating Deep Network Training by Reducing Internal
  399. Covariate Shift <https://arxiv.org/abs/1502.03167>`_. It rescales and recenters the
  400. features using a mini-batch of data and the learned parameters which can be described
  401. in the following formula,
  402. ...
  403. """
  404. ```
  405. For details about the display effect, click [here](https://www.mindspore.cn/doc/api_python/en/master/mindspore/mindspore.ops.html#mindspore.ops.BatchNorm).
  406. ## C++ API Comment Specifications
  407. - The name of the Markdown file must be the same as that of the namespace.
  408. - The internal format of the Markdown file is as follows. For details, see [Sample](https://www.mindspore.cn/doc/api_cpp/en/master/lite.html).
  409. ```markdown
  410. # The name of namespace
  411. The link of header file.
  412. ## The name of class
  413. The description of class.
  414. The name of attribute or function.
  415. The description of attribute or function.
  416. ```