diff --git a/docs/sciai/docs/Makefile b/docs/sciai/docs/Makefile deleted file mode 100644 index 1eff8952707bdfa503c8d60c1e9a903053170ba2..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/Makefile +++ /dev/null @@ -1,20 +0,0 @@ -# Minimal makefile for Sphinx documentation -# - -# You can set these variables from the command line, and also -# from the environment for the first two. -SPHINXOPTS ?= -SPHINXBUILD ?= sphinx-build -SOURCEDIR = source_zh_cn -BUILDDIR = build_zh_cn - -# Put it first so that "make" without argument is like "make help". -help: - @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) - -.PHONY: help Makefile - -# Catch-all target: route all unknown targets to Sphinx using the new -# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). -%: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/sciai/docs/_ext/myautosummary.py b/docs/sciai/docs/_ext/myautosummary.py deleted file mode 100644 index 3460bcd2f1528a90f3a7bc1c69acfa8f8110f4ad..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/_ext/myautosummary.py +++ /dev/null @@ -1,521 +0,0 @@ -"""Customized autosummary directives for sphinx.""" - -import importlib -import inspect -import os -import re -from typing import List, Tuple -from docutils.nodes import Node -from sphinx.locale import __ -from sphinx.ext.autosummary import Autosummary, posixpath, addnodes, logger, Matcher, autosummary_toc, get_import_prefixes_from_env -from sphinx.ext.autosummary import mock, StringList, ModuleType, get_documenter, ModuleAnalyzer, PycodeError, mangle_signature -from sphinx.ext.autosummary import import_by_name, extract_summary, autosummary_table, nodes, switch_source_input, rst -from sphinx.ext.autodoc.directive import DocumenterBridge, Options - -class MsAutosummary(Autosummary): - """ - Inherited from sphinx's autosummary, add titles and a column for the generated table. - """ - - def init(self): - """ - init method - """ - self.find_doc_name = "" - self.third_title = "" - self.default_doc = "" - - def extract_env_summary(self, doc: List[str]) -> str: - """Extract env summary from docstring.""" - env_sum = self.default_doc - for i, piece in enumerate(doc): - if piece.startswith(self.find_doc_name): - env_sum = doc[i+1][4:] - return env_sum - - def run(self): - """ - run method - """ - self.init() - self.bridge = DocumenterBridge(self.env, self.state.document.reporter, - Options(), self.lineno, self.state) - - names = [x.strip().split()[0] for x in self.content - if x.strip() and re.search(r'^[~a-zA-Z_]', x.strip()[0])] - items = self.get_items(names) - teble_nodes = self.get_table(items) - - if 'toctree' in self.options: - dirname = posixpath.dirname(self.env.docname) - - tree_prefix = self.options['toctree'].strip() - docnames = [] - excluded = Matcher(self.config.exclude_patterns) - for item in items: - docname = posixpath.join(tree_prefix, item[3]) - docname = posixpath.normpath(posixpath.join(dirname, docname)) - if docname not in self.env.found_docs: - location = self.state_machine.get_source_and_line(self.lineno) - if excluded(self.env.doc2path(docname, None)): - msg = __('autosummary references excluded document %r. Ignored.') - else: - msg = __('autosummary: stub file not found %r. ' - 'Check your autosummary_generate setting.') - logger.warning(msg, item[3], location=location) - continue - docnames.append(docname) - - if docnames: - tocnode = addnodes.toctree() - tocnode['includefiles'] = docnames - tocnode['entries'] = [(None, docn) for docn in docnames] - tocnode['maxdepth'] = -1 - tocnode['glob'] = None - teble_nodes.append(autosummary_toc('', '', tocnode)) - return teble_nodes - - def get_items(self, names: List[str]) -> List[Tuple[str, str, str, str, str]]: - """Try to import the given names, and return a list of - ``[(name, signature, summary_string, real_name, env_summary), ...]``. - """ - prefixes = get_import_prefixes_from_env(self.env) - items = [] # type: List[Tuple[str, str, str, str, str]] - max_item_chars = 50 - - for name in names: - display_name = name - if name.startswith('~'): - name = name[1:] - display_name = name.split('.')[-1] - try: - with mock(self.config.autosummary_mock_imports): - real_name, obj, parent, modname = import_by_name(name, prefixes=prefixes) - except ImportError: - logger.warning(__('failed to import %s'), name) - items.append((name, '', '', name, '')) - continue - - self.bridge.result = StringList() # initialize for each documenter - full_name = real_name - if not isinstance(obj, ModuleType): - # give explicitly separated module name, so that members - # of inner classes can be documented - full_name = modname + '::' + full_name[len(modname) + 1:] - # NB. using full_name here is important, since Documenters - # handle module prefixes slightly differently - doccls = get_documenter(self.env.app, obj, parent) - documenter = doccls(self.bridge, full_name) - - if not documenter.parse_name(): - logger.warning(__('failed to parse name %s'), real_name) - items.append((display_name, '', '', real_name, '')) - continue - if not documenter.import_object(): - logger.warning(__('failed to import object %s'), real_name) - items.append((display_name, '', '', real_name, '')) - continue - if documenter.options.members and not documenter.check_module(): - continue - - # try to also get a source code analyzer for attribute docs - try: - documenter.analyzer = ModuleAnalyzer.for_module( - documenter.get_real_modname()) - # parse right now, to get PycodeErrors on parsing (results will - # be cached anyway) - documenter.analyzer.find_attr_docs() - except PycodeError as err: - logger.debug('[autodoc] module analyzer failed: %s', err) - # no source file -- e.g. for builtin and C modules - documenter.analyzer = None - - # -- Grab the signature - - try: - sig = documenter.format_signature(show_annotation=False) - except TypeError: - # the documenter does not support ``show_annotation`` option - sig = documenter.format_signature() - - if not sig: - sig = '' - else: - max_chars = max(10, max_item_chars - len(display_name)) - sig = mangle_signature(sig, max_chars=max_chars) - - # -- Grab the summary - - documenter.add_content(None) - summary = extract_summary(self.bridge.result.data[:], self.state.document) - env_sum = self.extract_env_summary(self.bridge.result.data[:]) - items.append((display_name, sig, summary, real_name, env_sum)) - - return items - - def get_table(self, items: List[Tuple[str, str, str, str, str]]) -> List[Node]: - """Generate a proper list of table nodes for autosummary:: directive. - - *items* is a list produced by :meth:`get_items`. - """ - table_spec = addnodes.tabular_col_spec() - table_spec['spec'] = r'\X{1}{2}\X{1}{2}' - - table = autosummary_table('') - real_table = nodes.table('', classes=['longtable']) - table.append(real_table) - group = nodes.tgroup('', cols=3) - real_table.append(group) - group.append(nodes.colspec('', colwidth=10)) - group.append(nodes.colspec('', colwidth=70)) - group.append(nodes.colspec('', colwidth=30)) - body = nodes.tbody('') - group.append(body) - - def append_row(*column_texts: str) -> None: - row = nodes.row('', color="red") - source, line = self.state_machine.get_source_and_line() - for text in column_texts: - node = nodes.paragraph('') - vl = StringList() - vl.append(text, '%s:%d:' % (source, line)) - with switch_source_input(self.state, vl): - self.state.nested_parse(vl, 0, node) - try: - if isinstance(node[0], nodes.paragraph): - node = node[0] - except IndexError: - pass - row.append(nodes.entry('', node)) - body.append(row) - - # add table's title - append_row("**API Name**", "**Description**", self.third_title) - for name, sig, summary, real_name, env_sum in items: - qualifier = 'obj' - if 'nosignatures' not in self.options: - col1 = ':%s:`%s <%s>`\\ %s' % (qualifier, name, real_name, rst.escape(sig)) - else: - col1 = ':%s:`%s <%s>`' % (qualifier, name, real_name) - col2 = summary - col3 = env_sum - append_row(col1, col2, col3) - - return [table_spec, table] - - -class MsNoteAutoSummary(MsAutosummary): - """ - Inherited from MsAutosummary. Add a third column about `Note` to the table. - """ - - def init(self): - """ - init method - """ - self.find_doc_name = ".. note::" - self.third_title = "**Note**" - self.default_doc = "None" - - def extract_env_summary(self, doc: List[str]) -> str: - """Extract env summary from docstring.""" - env_sum = self.default_doc - for piece in doc: - if piece.startswith(self.find_doc_name): - env_sum = piece[10:] - return env_sum - - -class MsPlatformAutoSummary(MsAutosummary): - """ - Inherited from MsAutosummary. Add a third column about `Supported Platforms` to the table. - """ - def init(self): - """ - init method - """ - self.find_doc_name = "Supported Platforms:" - self.third_title = "**{}**".format(self.find_doc_name[:-1]) - self.default_doc = "To Be Developed" - -class MsCnAutoSummary(Autosummary): - """Overwrite MsPlatformAutosummary for chinese python api.""" - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.table_head = () - self.find_doc_name = "" - self.third_title = "" - self.default_doc = "" - self.third_name_en = "" - - def get_third_column_en(self, doc): - """Get the third column for en.""" - third_column = self.default_doc - for i, piece in enumerate(doc): - if piece.startswith(self.third_name_en): - try: - if "eprecated" in doc[i+1][4:]: - third_column = "弃用" - else: - third_column = doc[i+1][4:] - except IndexError: - third_column = '' - return third_column - - def get_summary_re(self, display_name: str): - return re.compile(rf'\.\. \w+:\w+::\s+{display_name}.*?\n\n\s+(.*?)[。\n]') - - def run(self) -> List[Node]: - self.bridge = DocumenterBridge(self.env, self.state.document.reporter, - Options(), self.lineno, self.state) - - names = [x.strip().split()[0] for x in self.content - if x.strip() and re.search(r'^[~a-zA-Z_]', x.strip()[0])] - items = self.get_items(names) - #pylint: disable=redefined-outer-name - nodes = self.get_table(items) - - dirname = posixpath.dirname(self.env.docname) - - tree_prefix = self.options['toctree'].strip() - docnames = [] - names = [i[0] for i in items] - for name in names: - docname = posixpath.join(tree_prefix, name) - docname = posixpath.normpath(posixpath.join(dirname, docname)) - if docname not in self.env.found_docs: - continue - - docnames.append(docname) - - if docnames: - tocnode = addnodes.toctree() - tocnode['includefiles'] = docnames - tocnode['entries'] = [(None, docn) for docn in docnames] - tocnode['maxdepth'] = -1 - tocnode['glob'] = None - - nodes.append(autosummary_toc('', '', tocnode)) - - return nodes - - def get_items(self, names: List[str]) -> List[Tuple[str, str, str, str]]: - """Try to import the given names, and return a list of - ``[(name, signature, summary_string, real_name), ...]``. - """ - prefixes = get_import_prefixes_from_env(self.env) - doc_path = os.path.dirname(self.state.document.current_source) - items = [] # type: List[Tuple[str, str, str, str]] - max_item_chars = 50 - origin_rst_files = self.env.config.rst_files - all_rst_files = self.env.found_docs - generated_files = all_rst_files.difference(origin_rst_files) - for name in names: - display_name = name - if name.startswith('~'): - name = name[1:] - display_name = name.split('.')[-1] - - dir_name = self.options['toctree'] - spec_path = os.path.join('api_python', dir_name, display_name) - file_path = os.path.join(doc_path, dir_name, display_name+'.rst') - if os.path.exists(file_path) and spec_path not in generated_files: - summary_re_tag = re.compile(rf'\.\. \w+:\w+::\s+{display_name}.*?\n\s+:.*?:\n\n\s+(.*?)[。\n]') - summary_re_line = re.compile(rf'\.\. \w+:\w+::\s+{display_name}(?:.|\n|)+?\n\n\s+(.*?)[。\n]') - summary_re = self.get_summary_re(display_name) - content = '' - with open(file_path, 'r', encoding='utf-8') as f: - content = f.read() - if content: - summary_str = summary_re.findall(content) - summary_str_tag = summary_re_tag.findall(content) - summary_str_line = summary_re_line.findall(content) - if summary_str: - if re.findall("[::,,。.;;]", summary_str[0][-1]): - logger.warning(f"{display_name}接口的概述格式需调整") - summary_str = summary_str[0] + '。' - elif summary_str_tag: - if re.findall("[::,,。.;;]", summary_str_tag[0][-1]): - logger.warning(f"{display_name}接口的概述格式需调整") - summary_str = summary_str_tag[0] + '。' - elif summary_str_line: - if re.findall("[::,,。.;;]", summary_str_line[0][-1]): - logger.warning(f"{display_name}接口的概述格式需调整") - summary_str = summary_str_line[0] + '。' - else: - summary_str = '' - if not self.table_head: - items.append((display_name, summary_str)) - else: - third_str = self.get_third_column(display_name, content) - if third_str: - third_str = third_str[0] - else: - third_str = '' - - items.append((display_name, summary_str, third_str)) - else: - try: - with mock(self.config.autosummary_mock_imports): - real_name, obj, parent, modname = import_by_name(name, prefixes=prefixes) - except ImportError: - logger.warning(__('failed to import %s'), name) - items.append((name, '', '')) - continue - - self.bridge.result = StringList() # initialize for each documenter - full_name = real_name - if not isinstance(obj, ModuleType): - # give explicitly separated module name, so that members - # of inner classes can be documented - full_name = modname + '::' + full_name[len(modname) + 1:] - # NB. using full_name here is important, since Documenters - # handle module prefixes slightly differently - doccls = get_documenter(self.env.app, obj, parent) - documenter = doccls(self.bridge, full_name) - - if not documenter.parse_name(): - logger.warning(__('failed to parse name %s'), real_name) - items.append((display_name, '', '')) - continue - if not documenter.import_object(): - logger.warning(__('failed to import object %s'), real_name) - items.append((display_name, '', '')) - continue - if documenter.options.members and not documenter.check_module(): - continue - - # try to also get a source code analyzer for attribute docs - try: - documenter.analyzer = ModuleAnalyzer.for_module( - documenter.get_real_modname()) - # parse right now, to get PycodeErrors on parsing (results will - # be cached anyway) - documenter.analyzer.find_attr_docs() - except PycodeError as err: - logger.debug('[autodoc] module analyzer failed: %s', err) - # no source file -- e.g. for builtin and C modules - documenter.analyzer = None - - # -- Grab the signature - - try: - sig = documenter.format_signature(show_annotation=False) - except TypeError: - # the documenter does not support ``show_annotation`` option - sig = documenter.format_signature() - - if not sig: - sig = '' - else: - max_chars = max(10, max_item_chars - len(display_name)) - sig = mangle_signature(sig, max_chars=max_chars) - - # -- Grab the summary and third_colum - - documenter.add_content(None) - summary = extract_summary(self.bridge.result.data[:], self.state.document) - if self.table_head: - third_colum = self.get_third_column_en(self.bridge.result.data[:]) - items.append((display_name, summary, third_colum)) - else: - items.append((display_name, summary)) - - - return items - - def get_table(self, items: List[Tuple[str, str, str]]) -> List[Node]: - """Generate a proper list of table nodes for autosummary:: directive. - - *items* is a list produced by :meth:`get_items`. - """ - table_spec = addnodes.tabular_col_spec() - table = autosummary_table('') - real_table = nodes.table('', classes=['longtable']) - table.append(real_table) - - if not self.table_head: - table_spec['spec'] = r'\X{1}{2}\X{1}{2}' - group = nodes.tgroup('', cols=2) - real_table.append(group) - group.append(nodes.colspec('', colwidth=10)) - group.append(nodes.colspec('', colwidth=90)) - else: - table_spec['spec'] = r'\X{1}{2}\X{1}{2}\X{1}{2}' - group = nodes.tgroup('', cols=3) - real_table.append(group) - group.append(nodes.colspec('', colwidth=10)) - group.append(nodes.colspec('', colwidth=60)) - group.append(nodes.colspec('', colwidth=30)) - body = nodes.tbody('') - group.append(body) - - def append_row(*column_texts: str) -> None: - row = nodes.row('') - source, line = self.state_machine.get_source_and_line() - for text in column_texts: - node = nodes.paragraph('') - vl = StringList() - vl.append(text, '%s:%d:' % (source, line)) - with switch_source_input(self.state, vl): - self.state.nested_parse(vl, 0, node) - try: - if isinstance(node[0], nodes.paragraph): - node = node[0] - except IndexError: - pass - row.append(nodes.entry('', node)) - body.append(row) - append_row(*self.table_head) - if not self.table_head: - for name, summary in items: - qualifier = 'obj' - col1 = ':%s:`%s <%s>`' % (qualifier, name, name) - col2 = summary - append_row(col1, col2) - else: - for name, summary, other in items: - qualifier = 'obj' - col1 = ':%s:`%s <%s>`' % (qualifier, name, name) - col2 = summary - col3 = other - append_row(col1, col2, col3) - return [table_spec, table] - -def get_api(fullname): - """Get the api module.""" - try: - module_name, api_name = ".".join(fullname.split('.')[:-1]), fullname.split('.')[-1] - # pylint: disable=unused-variable - module_import = importlib.import_module(module_name) - except ModuleNotFoundError: - module_name, api_name = ".".join(fullname.split('.')[:-2]), ".".join(fullname.split('.')[-2:]) - module_import = importlib.import_module(module_name) - # pylint: disable=eval-used - api = getattr(module_import, api_name) - return api - -class MsCnPlatformAutoSummary(MsCnAutoSummary): - """definition of cnmsplatformautosummary.""" - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.table_head = ('**接口名**', '**概述**', '**支持平台**') - self.third_name_en = "Supported Platforms:" - - def get_third_column(self, name=None, content=None): - """Get the`Supported Platforms`.""" - if not name: - return [] - try: - api_doc = inspect.getdoc(get_api(name)) - platform_str = re.findall(r'Supported Platforms:\n\s+(.*?)\n\n', api_doc) - if ['deprecated'] == platform_str: - return ["弃用"] - if not platform_str: - platform_str_leak = re.findall(r'Supported Platforms:\n\s+(.*)', api_doc) - if platform_str_leak: - return platform_str_leak - return ["``Ascend`` ``GPU`` ``CPU``"] - return platform_str - except: #pylint: disable=bare-except - return [] diff --git a/docs/sciai/docs/_ext/overwriteautosummary_generate.txt b/docs/sciai/docs/_ext/overwriteautosummary_generate.txt deleted file mode 100644 index 4b0a1b1dd2b410ecab971b13da9993c90d65ef0d..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/_ext/overwriteautosummary_generate.txt +++ /dev/null @@ -1,707 +0,0 @@ -""" - sphinx.ext.autosummary.generate - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - Usable as a library or script to generate automatic RST source files for - items referred to in autosummary:: directives. - - Each generated RST file contains a single auto*:: directive which - extracts the docstring of the referred item. - - Example Makefile rule:: - - generate: - sphinx-autogen -o source/generated source/*.rst - - :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS. - :license: BSD, see LICENSE for details. -""" - -import argparse -import importlib -import inspect -import locale -import os -import pkgutil -import pydoc -import re -import sys -import warnings -from gettext import NullTranslations -from os import path -from typing import Any, Dict, List, NamedTuple, Sequence, Set, Tuple, Type, Union - -from jinja2 import TemplateNotFound -from jinja2.sandbox import SandboxedEnvironment - -import sphinx.locale -from sphinx import __display_version__, package_dir -from sphinx.application import Sphinx -from sphinx.builders import Builder -from sphinx.config import Config -from sphinx.deprecation import RemovedInSphinx50Warning -from sphinx.ext.autodoc import Documenter -from sphinx.ext.autodoc.importer import import_module -from sphinx.ext.autosummary import (ImportExceptionGroup, get_documenter, import_by_name, - import_ivar_by_name) -from sphinx.locale import __ -from sphinx.pycode import ModuleAnalyzer, PycodeError -from sphinx.registry import SphinxComponentRegistry -from sphinx.util import logging, rst, split_full_qualified_name, get_full_modname -from sphinx.util.inspect import getall, safe_getattr -from sphinx.util.osutil import ensuredir -from sphinx.util.template import SphinxTemplateLoader - -logger = logging.getLogger(__name__) - - -class DummyApplication: - """Dummy Application class for sphinx-autogen command.""" - - def __init__(self, translator: NullTranslations) -> None: - self.config = Config() - self.registry = SphinxComponentRegistry() - self.messagelog: List[str] = [] - self.srcdir = "/" - self.translator = translator - self.verbosity = 0 - self._warncount = 0 - self.warningiserror = False - - self.config.add('autosummary_context', {}, True, None) - self.config.add('autosummary_filename_map', {}, True, None) - self.config.add('autosummary_ignore_module_all', True, 'env', bool) - self.config.add('docs_branch', '', True, None) - self.config.add('branch', '', True, None) - self.config.add('cst_module_name', '', True, None) - self.config.add('copy_repo', '', True, None) - self.config.add('giturl', '', True, None) - self.config.add('repo_whl', '', True, None) - self.config.init_values() - - def emit_firstresult(self, *args: Any) -> None: - pass - - -class AutosummaryEntry(NamedTuple): - name: str - path: str - template: str - recursive: bool - - -def setup_documenters(app: Any) -> None: - from sphinx.ext.autodoc import (AttributeDocumenter, ClassDocumenter, DataDocumenter, - DecoratorDocumenter, ExceptionDocumenter, - FunctionDocumenter, MethodDocumenter, ModuleDocumenter, - NewTypeAttributeDocumenter, NewTypeDataDocumenter, - PropertyDocumenter) - documenters: List[Type[Documenter]] = [ - ModuleDocumenter, ClassDocumenter, ExceptionDocumenter, DataDocumenter, - FunctionDocumenter, MethodDocumenter, NewTypeAttributeDocumenter, - NewTypeDataDocumenter, AttributeDocumenter, DecoratorDocumenter, PropertyDocumenter, - ] - for documenter in documenters: - app.registry.add_documenter(documenter.objtype, documenter) - - -def _simple_info(msg: str) -> None: - warnings.warn('_simple_info() is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - print(msg) - - -def _simple_warn(msg: str) -> None: - warnings.warn('_simple_warn() is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - print('WARNING: ' + msg, file=sys.stderr) - - -def _underline(title: str, line: str = '=') -> str: - if '\n' in title: - raise ValueError('Can only underline single lines') - return title + '\n' + line * len(title) - - -class AutosummaryRenderer: - """A helper class for rendering.""" - - def __init__(self, app: Union[Builder, Sphinx], template_dir: str = None) -> None: - if isinstance(app, Builder): - warnings.warn('The first argument for AutosummaryRenderer has been ' - 'changed to Sphinx object', - RemovedInSphinx50Warning, stacklevel=2) - if template_dir: - warnings.warn('template_dir argument for AutosummaryRenderer is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - - system_templates_path = [os.path.join(package_dir, 'ext', 'autosummary', 'templates')] - loader = SphinxTemplateLoader(app.srcdir, app.config.templates_path, - system_templates_path) - - self.env = SandboxedEnvironment(loader=loader) - self.env.filters['escape'] = rst.escape - self.env.filters['e'] = rst.escape - self.env.filters['underline'] = _underline - - if isinstance(app, (Sphinx, DummyApplication)): - if app.translator: - self.env.add_extension("jinja2.ext.i18n") - self.env.install_gettext_translations(app.translator) - elif isinstance(app, Builder): - if app.app.translator: - self.env.add_extension("jinja2.ext.i18n") - self.env.install_gettext_translations(app.app.translator) - - def exists(self, template_name: str) -> bool: - """Check if template file exists.""" - warnings.warn('AutosummaryRenderer.exists() is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - try: - self.env.get_template(template_name) - return True - except TemplateNotFound: - return False - - def render(self, template_name: str, context: Dict) -> str: - """Render a template file.""" - try: - template = self.env.get_template(template_name) - except TemplateNotFound: - try: - # objtype is given as template_name - template = self.env.get_template('autosummary/%s.rst' % template_name) - except TemplateNotFound: - # fallback to base.rst - template = self.env.get_template('autosummary/base.rst') - - return template.render(context) - - -# -- Generating output --------------------------------------------------------- - - -class ModuleScanner: - def __init__(self, app: Any, obj: Any) -> None: - self.app = app - self.object = obj - - def get_object_type(self, name: str, value: Any) -> str: - return get_documenter(self.app, value, self.object).objtype - - def is_skipped(self, name: str, value: Any, objtype: str) -> bool: - try: - return self.app.emit_firstresult('autodoc-skip-member', objtype, - name, value, False, {}) - except Exception as exc: - logger.warning(__('autosummary: failed to determine %r to be documented, ' - 'the following exception was raised:\n%s'), - name, exc, type='autosummary') - return False - - def scan(self, imported_members: bool) -> List[str]: - members = [] - for name in members_of(self.object, self.app.config): - try: - value = safe_getattr(self.object, name) - except AttributeError: - value = None - - objtype = self.get_object_type(name, value) - if self.is_skipped(name, value, objtype): - continue - - try: - if inspect.ismodule(value): - imported = True - elif safe_getattr(value, '__module__') != self.object.__name__: - imported = True - else: - imported = False - except AttributeError: - imported = False - - respect_module_all = not self.app.config.autosummary_ignore_module_all - if imported_members: - # list all members up - members.append(name) - elif imported is False: - # list not-imported members - members.append(name) - elif '__all__' in dir(self.object) and respect_module_all: - # list members that have __all__ set - members.append(name) - - return members - - -def members_of(obj: Any, conf: Config) -> Sequence[str]: - """Get the members of ``obj``, possibly ignoring the ``__all__`` module attribute - - Follows the ``conf.autosummary_ignore_module_all`` setting.""" - - if conf.autosummary_ignore_module_all: - return dir(obj) - else: - return getall(obj) or dir(obj) - - -def generate_autosummary_content(name: str, obj: Any, parent: Any, - template: AutosummaryRenderer, template_name: str, - imported_members: bool, app: Any, - recursive: bool, context: Dict, - modname: str = None, qualname: str = None) -> str: - doc = get_documenter(app, obj, parent) - - def skip_member(obj: Any, name: str, objtype: str) -> bool: - try: - return app.emit_firstresult('autodoc-skip-member', objtype, name, - obj, False, {}) - except Exception as exc: - logger.warning(__('autosummary: failed to determine %r to be documented, ' - 'the following exception was raised:\n%s'), - name, exc, type='autosummary') - return False - - def get_class_members(obj: Any) -> Dict[str, Any]: - members = sphinx.ext.autodoc.get_class_members(obj, [qualname], safe_getattr) - return {name: member.object for name, member in members.items()} - - def get_module_members(obj: Any) -> Dict[str, Any]: - members = {} - for name in members_of(obj, app.config): - try: - members[name] = safe_getattr(obj, name) - except AttributeError: - continue - return members - - def get_all_members(obj: Any) -> Dict[str, Any]: - if doc.objtype == "module": - return get_module_members(obj) - elif doc.objtype == "class": - return get_class_members(obj) - return {} - - def get_members(obj: Any, types: Set[str], include_public: List[str] = [], - imported: bool = True) -> Tuple[List[str], List[str]]: - items: List[str] = [] - public: List[str] = [] - - all_members = get_all_members(obj) - for name, value in all_members.items(): - documenter = get_documenter(app, value, obj) - if documenter.objtype in types: - # skip imported members if expected - if imported or getattr(value, '__module__', None) == obj.__name__: - skipped = skip_member(value, name, documenter.objtype) - if skipped is True: - pass - elif skipped is False: - # show the member forcedly - items.append(name) - public.append(name) - else: - items.append(name) - if name in include_public or not name.startswith('_'): - # considers member as public - public.append(name) - return public, items - - def get_module_attrs(members: Any) -> Tuple[List[str], List[str]]: - """Find module attributes with docstrings.""" - attrs, public = [], [] - try: - analyzer = ModuleAnalyzer.for_module(name) - attr_docs = analyzer.find_attr_docs() - for namespace, attr_name in attr_docs: - if namespace == '' and attr_name in members: - attrs.append(attr_name) - if not attr_name.startswith('_'): - public.append(attr_name) - except PycodeError: - pass # give up if ModuleAnalyzer fails to parse code - return public, attrs - - def get_modules(obj: Any) -> Tuple[List[str], List[str]]: - items: List[str] = [] - for _, modname, _ispkg in pkgutil.iter_modules(obj.__path__): - fullname = name + '.' + modname - try: - module = import_module(fullname) - if module and hasattr(module, '__sphinx_mock__'): - continue - except ImportError: - pass - - items.append(fullname) - public = [x for x in items if not x.split('.')[-1].startswith('_')] - return public, items - - ns: Dict[str, Any] = {} - ns.update(context) - - if doc.objtype == 'module': - scanner = ModuleScanner(app, obj) - ns['members'] = scanner.scan(imported_members) - ns['functions'], ns['all_functions'] = \ - get_members(obj, {'function'}, imported=imported_members) - ns['classes'], ns['all_classes'] = \ - get_members(obj, {'class'}, imported=imported_members) - ns['exceptions'], ns['all_exceptions'] = \ - get_members(obj, {'exception'}, imported=imported_members) - ns['attributes'], ns['all_attributes'] = \ - get_module_attrs(ns['members']) - ispackage = hasattr(obj, '__path__') - if ispackage and recursive: - ns['modules'], ns['all_modules'] = get_modules(obj) - elif doc.objtype == 'class': - ns['members'] = dir(obj) - ns['inherited_members'] = \ - set(dir(obj)) - set(obj.__dict__.keys()) - ns['methods'], ns['all_methods'] = \ - get_members(obj, {'method'}, ['__init__']) - ns['attributes'], ns['all_attributes'] = \ - get_members(obj, {'attribute', 'property'}) - - if modname is None or qualname is None: - modname, qualname = split_full_qualified_name(name) - - if doc.objtype in ('method', 'attribute', 'property'): - ns['class'] = qualname.rsplit(".", 1)[0] - - if doc.objtype in ('class',): - shortname = qualname - else: - shortname = qualname.rsplit(".", 1)[-1] - - ns['fullname'] = name - ns['module'] = modname - ns['objname'] = qualname - ns['name'] = shortname - - ns['objtype'] = doc.objtype - ns['underline'] = len(name) * '=' - - if template_name: - return template.render(template_name, ns) - else: - return template.render(doc.objtype, ns) - - -def generate_autosummary_docs(sources: List[str], output_dir: str = None, - suffix: str = '.rst', base_path: str = None, - builder: Builder = None, template_dir: str = None, - imported_members: bool = False, app: Any = None, - overwrite: bool = True, encoding: str = 'utf-8') -> None: - - if builder: - warnings.warn('builder argument for generate_autosummary_docs() is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - - if template_dir: - warnings.warn('template_dir argument for generate_autosummary_docs() is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - - showed_sources = list(sorted(sources)) - if len(showed_sources) > 20: - showed_sources = showed_sources[:10] + ['...'] + showed_sources[-10:] - logger.info(__('[autosummary] generating autosummary for: %s') % - ', '.join(showed_sources)) - - if output_dir: - logger.info(__('[autosummary] writing to %s') % output_dir) - - if base_path is not None: - sources = [os.path.join(base_path, filename) for filename in sources] - - template = AutosummaryRenderer(app) - - # read - items = find_autosummary_in_files(sources) - - # keep track of new files - new_files = [] - - if app: - filename_map = app.config.autosummary_filename_map - else: - filename_map = {} - - # write - for entry in sorted(set(items), key=str): - if entry.path is None: - # The corresponding autosummary:: directive did not have - # a :toctree: option - continue - - path = output_dir or os.path.abspath(entry.path) - ensuredir(path) - - try: - name, obj, parent, modname = import_by_name(entry.name, grouped_exception=True) - qualname = name.replace(modname + ".", "") - except ImportExceptionGroup as exc: - try: - # try to import as an instance attribute - name, obj, parent, modname = import_ivar_by_name(entry.name) - qualname = name.replace(modname + ".", "") - except ImportError as exc2: - if exc2.__cause__: - exceptions: List[BaseException] = exc.exceptions + [exc2.__cause__] - else: - exceptions = exc.exceptions + [exc2] - - errors = list(set("* %s: %s" % (type(e).__name__, e) for e in exceptions)) - logger.warning(__('[autosummary] failed to import %s.\nPossible hints:\n%s'), - entry.name, '\n'.join(errors)) - continue - - context: Dict[str, Any] = {} - if app: - context.update(app.config.autosummary_context) - - content = generate_autosummary_content(name, obj, parent, template, entry.template, - imported_members, app, entry.recursive, context, - modname, qualname) - try: - py_source_rel = get_full_modname(modname, qualname).replace('.', '/') + '.py' - except: - logger.warning(name) - py_source_rel = '' - - re_view = f"\n.. image:: https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/{app.config.docs_branch}/" + \ - f"resource/_static/logo_source_en.svg\n :target: " + app.config.giturl + \ - f"{app.config.copy_repo}/blob/{app.config.branch}/" + app.config.repo_whl + \ - py_source_rel.split(app.config.cst_module_name)[-1] + '\n :alt: View Source On Gitee\n\n' - - if re_view not in content and py_source_rel: - content = re.sub('([=]{5,})\n', r'\1\n' + re_view, content, 1) - filename = os.path.join(path, filename_map.get(name, name) + suffix) - if os.path.isfile(filename): - with open(filename, encoding=encoding) as f: - old_content = f.read() - - if content == old_content: - continue - elif overwrite: # content has changed - with open(filename, 'w', encoding=encoding) as f: - f.write(content) - new_files.append(filename) - else: - with open(filename, 'w', encoding=encoding) as f: - f.write(content) - new_files.append(filename) - - # descend recursively to new files - if new_files: - generate_autosummary_docs(new_files, output_dir=output_dir, - suffix=suffix, base_path=base_path, - builder=builder, template_dir=template_dir, - imported_members=imported_members, app=app, - overwrite=overwrite) - - -# -- Finding documented entries in files --------------------------------------- - -def find_autosummary_in_files(filenames: List[str]) -> List[AutosummaryEntry]: - """Find out what items are documented in source/*.rst. - - See `find_autosummary_in_lines`. - """ - documented: List[AutosummaryEntry] = [] - for filename in filenames: - with open(filename, encoding='utf-8', errors='ignore') as f: - lines = f.read().splitlines() - documented.extend(find_autosummary_in_lines(lines, filename=filename)) - return documented - - -def find_autosummary_in_docstring(name: str, module: str = None, filename: str = None - ) -> List[AutosummaryEntry]: - """Find out what items are documented in the given object's docstring. - - See `find_autosummary_in_lines`. - """ - if module: - warnings.warn('module argument for find_autosummary_in_docstring() is deprecated.', - RemovedInSphinx50Warning, stacklevel=2) - - try: - real_name, obj, parent, modname = import_by_name(name, grouped_exception=True) - lines = pydoc.getdoc(obj).splitlines() - return find_autosummary_in_lines(lines, module=name, filename=filename) - except AttributeError: - pass - except ImportExceptionGroup as exc: - errors = list(set("* %s: %s" % (type(e).__name__, e) for e in exc.exceptions)) - print('Failed to import %s.\nPossible hints:\n%s' % (name, '\n'.join(errors))) - except SystemExit: - print("Failed to import '%s'; the module executes module level " - "statement and it might call sys.exit()." % name) - return [] - - -def find_autosummary_in_lines(lines: List[str], module: str = None, filename: str = None - ) -> List[AutosummaryEntry]: - """Find out what items appear in autosummary:: directives in the - given lines. - - Returns a list of (name, toctree, template) where *name* is a name - of an object and *toctree* the :toctree: path of the corresponding - autosummary directive (relative to the root of the file name), and - *template* the value of the :template: option. *toctree* and - *template* ``None`` if the directive does not have the - corresponding options set. - """ - autosummary_re = re.compile(r'^(\s*)\.\.\s+(ms[a-z]*)?autosummary::\s*') - automodule_re = re.compile( - r'^\s*\.\.\s+automodule::\s*([A-Za-z0-9_.]+)\s*$') - module_re = re.compile( - r'^\s*\.\.\s+(current)?module::\s*([a-zA-Z0-9_.]+)\s*$') - autosummary_item_re = re.compile(r'^\s+(~?[_a-zA-Z][a-zA-Z0-9_.]*)\s*.*?') - recursive_arg_re = re.compile(r'^\s+:recursive:\s*$') - toctree_arg_re = re.compile(r'^\s+:toctree:\s*(.*?)\s*$') - template_arg_re = re.compile(r'^\s+:template:\s*(.*?)\s*$') - - documented: List[AutosummaryEntry] = [] - - recursive = False - toctree: str = None - template = None - current_module = module - in_autosummary = False - base_indent = "" - - for line in lines: - if in_autosummary: - m = recursive_arg_re.match(line) - if m: - recursive = True - continue - - m = toctree_arg_re.match(line) - if m: - toctree = m.group(1) - if filename: - toctree = os.path.join(os.path.dirname(filename), - toctree) - continue - - m = template_arg_re.match(line) - if m: - template = m.group(1).strip() - continue - - if line.strip().startswith(':'): - continue # skip options - - m = autosummary_item_re.match(line) - if m: - name = m.group(1).strip() - if name.startswith('~'): - name = name[1:] - if current_module and \ - not name.startswith(current_module + '.'): - name = "%s.%s" % (current_module, name) - documented.append(AutosummaryEntry(name, toctree, template, recursive)) - continue - - if not line.strip() or line.startswith(base_indent + " "): - continue - - in_autosummary = False - - m = autosummary_re.match(line) - if m: - in_autosummary = True - base_indent = m.group(1) - recursive = False - toctree = None - template = None - continue - - m = automodule_re.search(line) - if m: - current_module = m.group(1).strip() - # recurse into the automodule docstring - documented.extend(find_autosummary_in_docstring( - current_module, filename=filename)) - continue - - m = module_re.match(line) - if m: - current_module = m.group(2) - continue - - return documented - - -def get_parser() -> argparse.ArgumentParser: - parser = argparse.ArgumentParser( - usage='%(prog)s [OPTIONS] ...', - epilog=__('For more information, visit .'), - description=__(""" -Generate ReStructuredText using autosummary directives. - -sphinx-autogen is a frontend to sphinx.ext.autosummary.generate. It generates -the reStructuredText files from the autosummary directives contained in the -given input files. - -The format of the autosummary directive is documented in the -``sphinx.ext.autosummary`` Python module and can be read using:: - - pydoc sphinx.ext.autosummary -""")) - - parser.add_argument('--version', action='version', dest='show_version', - version='%%(prog)s %s' % __display_version__) - - parser.add_argument('source_file', nargs='+', - help=__('source files to generate rST files for')) - - parser.add_argument('-o', '--output-dir', action='store', - dest='output_dir', - help=__('directory to place all output in')) - parser.add_argument('-s', '--suffix', action='store', dest='suffix', - default='rst', - help=__('default suffix for files (default: ' - '%(default)s)')) - parser.add_argument('-t', '--templates', action='store', dest='templates', - default=None, - help=__('custom template directory (default: ' - '%(default)s)')) - parser.add_argument('-i', '--imported-members', action='store_true', - dest='imported_members', default=False, - help=__('document imported members (default: ' - '%(default)s)')) - parser.add_argument('-a', '--respect-module-all', action='store_true', - dest='respect_module_all', default=False, - help=__('document exactly the members in module __all__ attribute. ' - '(default: %(default)s)')) - - return parser - - -def main(argv: List[str] = sys.argv[1:]) -> None: - sphinx.locale.setlocale(locale.LC_ALL, '') - sphinx.locale.init_console(os.path.join(package_dir, 'locale'), 'sphinx') - translator, _ = sphinx.locale.init([], None) - - app = DummyApplication(translator) - logging.setup(app, sys.stdout, sys.stderr) # type: ignore - setup_documenters(app) - args = get_parser().parse_args(argv) - - if args.templates: - app.config.templates_path.append(path.abspath(args.templates)) - app.config.autosummary_ignore_module_all = not args.respect_module_all # type: ignore - - generate_autosummary_docs(args.source_file, args.output_dir, - '.' + args.suffix, - imported_members=args.imported_members, - app=app) - - -if __name__ == '__main__': - main() diff --git a/docs/sciai/docs/_ext/overwriteobjectiondirective.txt b/docs/sciai/docs/_ext/overwriteobjectiondirective.txt deleted file mode 100644 index e7ffdfe09a737771ead4a9c2ce1d0b945bb49947..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/_ext/overwriteobjectiondirective.txt +++ /dev/null @@ -1,374 +0,0 @@ -""" - sphinx.directives - ~~~~~~~~~~~~~~~~~ - - Handlers for additional ReST directives. - - :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS. - :license: BSD, see LICENSE for details. -""" - -import re -import inspect -import importlib -from functools import reduce -from typing import TYPE_CHECKING, Any, Dict, Generic, List, Tuple, TypeVar, cast - -from docutils import nodes -from docutils.nodes import Node -from docutils.parsers.rst import directives, roles - -from sphinx import addnodes -from sphinx.addnodes import desc_signature -from sphinx.deprecation import RemovedInSphinx50Warning, deprecated_alias -from sphinx.util import docutils, logging -from sphinx.util.docfields import DocFieldTransformer, Field, TypedField -from sphinx.util.docutils import SphinxDirective -from sphinx.util.typing import OptionSpec - -if TYPE_CHECKING: - from sphinx.application import Sphinx - - -# RE to strip backslash escapes -nl_escape_re = re.compile(r'\\\n') -strip_backslash_re = re.compile(r'\\(.)') - -T = TypeVar('T') -logger = logging.getLogger(__name__) - -def optional_int(argument: str) -> int: - """ - Check for an integer argument or None value; raise ``ValueError`` if not. - """ - if argument is None: - return None - else: - value = int(argument) - if value < 0: - raise ValueError('negative value; must be positive or zero') - return value - -def get_api(fullname): - """ - 获取接口对象。 - - :param fullname: 接口名全称 - :return: 属性对象或None(如果不存在) - """ - main_module = fullname.split('.')[0] - main_import = importlib.import_module(main_module) - - try: - return reduce(getattr, fullname.split('.')[1:], main_import) - except AttributeError: - return None - -def get_example(name: str): - try: - api_doc = inspect.getdoc(get_api(name)) - example_str = re.findall(r'Examples:\n([\w\W]*?)(\n\n|$)', api_doc) - if not example_str: - return [] - example_str = re.sub(r'\n\s+', r'\n', example_str[0][0]) - example_str = example_str.strip() - example_list = example_str.split('\n') - return ["", "**样例:**", ""] + example_list + [""] - except: - return [] - -def get_platforms(name: str): - try: - api_doc = inspect.getdoc(get_api(name)) - example_str = re.findall(r'Supported Platforms:\n\s+(.*?)\n\n', api_doc) - if not example_str: - example_str_leak = re.findall(r'Supported Platforms:\n\s+(.*)', api_doc) - if example_str_leak: - example_str = example_str_leak[0].strip() - example_list = example_str.split('\n') - example_list = [' ' + example_list[0]] - return ["", "支持平台:"] + example_list + [""] - return [] - example_str = example_str[0].strip() - example_list = example_str.split('\n') - example_list = [' ' + example_list[0]] - return ["", "支持平台:"] + example_list + [""] - except: - return [] - -class ObjectDescription(SphinxDirective, Generic[T]): - """ - Directive to describe a class, function or similar object. Not used - directly, but subclassed (in domain-specific directives) to add custom - behavior. - """ - - has_content = True - required_arguments = 1 - optional_arguments = 0 - final_argument_whitespace = True - option_spec: OptionSpec = { - 'noindex': directives.flag, - } # type: Dict[str, DirectiveOption] - - # types of doc fields that this directive handles, see sphinx.util.docfields - doc_field_types: List[Field] = [] - domain: str = None - objtype: str = None - indexnode: addnodes.index = None - - # Warning: this might be removed in future version. Don't touch this from extensions. - _doc_field_type_map = {} # type: Dict[str, Tuple[Field, bool]] - - def get_field_type_map(self) -> Dict[str, Tuple[Field, bool]]: - if self._doc_field_type_map == {}: - self._doc_field_type_map = {} - for field in self.doc_field_types: - for name in field.names: - self._doc_field_type_map[name] = (field, False) - - if field.is_typed: - typed_field = cast(TypedField, field) - for name in typed_field.typenames: - self._doc_field_type_map[name] = (field, True) - - return self._doc_field_type_map - - def get_signatures(self) -> List[str]: - """ - Retrieve the signatures to document from the directive arguments. By - default, signatures are given as arguments, one per line. - - Backslash-escaping of newlines is supported. - """ - lines = nl_escape_re.sub('', self.arguments[0]).split('\n') - if self.config.strip_signature_backslash: - # remove backslashes to support (dummy) escapes; helps Vim highlighting - return [strip_backslash_re.sub(r'\1', line.strip()) for line in lines] - else: - return [line.strip() for line in lines] - - def handle_signature(self, sig: str, signode: desc_signature) -> Any: - """ - Parse the signature *sig* into individual nodes and append them to - *signode*. If ValueError is raised, parsing is aborted and the whole - *sig* is put into a single desc_name node. - - The return value should be a value that identifies the object. It is - passed to :meth:`add_target_and_index()` unchanged, and otherwise only - used to skip duplicates. - """ - raise ValueError - - def add_target_and_index(self, name: Any, sig: str, signode: desc_signature) -> None: - """ - Add cross-reference IDs and entries to self.indexnode, if applicable. - - *name* is whatever :meth:`handle_signature()` returned. - """ - return # do nothing by default - - def before_content(self) -> None: - """ - Called before parsing content. Used to set information about the current - directive context on the build environment. - """ - pass - - def transform_content(self, contentnode: addnodes.desc_content) -> None: - """ - Called after creating the content through nested parsing, - but before the ``object-description-transform`` event is emitted, - and before the info-fields are transformed. - Can be used to manipulate the content. - """ - pass - - def after_content(self) -> None: - """ - Called after parsing content. Used to reset information about the - current directive context on the build environment. - """ - pass - - def check_class_end(self, content): - for i in content: - if not i.startswith('.. include::') and i != "\n" and i != "": - return False - return True - - def extend_items(self, rst_file, start_num, num): - ls = [] - for i in range(1, num+1): - ls.append((rst_file, start_num+i)) - return ls - - def run(self) -> List[Node]: - """ - Main directive entry function, called by docutils upon encountering the - directive. - - This directive is meant to be quite easily subclassable, so it delegates - to several additional methods. What it does: - - * find out if called as a domain-specific directive, set self.domain - * create a `desc` node to fit all description inside - * parse standard options, currently `noindex` - * create an index node if needed as self.indexnode - * parse all given signatures (as returned by self.get_signatures()) - using self.handle_signature(), which should either return a name - or raise ValueError - * add index entries using self.add_target_and_index() - * parse the content and handle doc fields in it - """ - if ':' in self.name: - self.domain, self.objtype = self.name.split(':', 1) - else: - self.domain, self.objtype = '', self.name - self.indexnode = addnodes.index(entries=[]) - - node = addnodes.desc() - node.document = self.state.document - node['domain'] = self.domain - # 'desctype' is a backwards compatible attribute - node['objtype'] = node['desctype'] = self.objtype - node['noindex'] = noindex = ('noindex' in self.options) - if self.domain: - node['classes'].append(self.domain) - node['classes'].append(node['objtype']) - - self.names: List[T] = [] - signatures = self.get_signatures() - for sig in signatures: - # add a signature node for each signature in the current unit - # and add a reference target for it - signode = addnodes.desc_signature(sig, '') - self.set_source_info(signode) - node.append(signode) - try: - # name can also be a tuple, e.g. (classname, objname); - # this is strictly domain-specific (i.e. no assumptions may - # be made in this base class) - name = self.handle_signature(sig, signode) - except ValueError: - # signature parsing failed - signode.clear() - signode += addnodes.desc_name(sig, sig) - continue # we don't want an index entry here - if name not in self.names: - self.names.append(name) - if not noindex: - # only add target and index entry if this is the first - # description of the object with this name in this desc block - self.add_target_and_index(name, sig, signode) - - contentnode = addnodes.desc_content() - node.append(contentnode) - if self.names: - # needed for association of version{added,changed} directives - self.env.temp_data['object'] = self.names[0] - self.before_content() - try: - example = get_example(self.names[0][0]) - platforms = get_platforms(self.names[0][0]) - except Exception as e: - example = '' - platforms = '' - logger.warning(f'Error API names in {self.arguments[0]}.') - logger.warning(f'{e}') - extra = platforms + example - if extra: - if self.objtype == "method": - self.content.data.extend(extra) - else: - index_num = 0 - for num, i in enumerate(self.content.data): - if i.startswith('.. py:method::') or self.check_class_end(self.content.data[num:]): - index_num = num - break - if index_num: - count = len(self.content.data) - for i in extra: - self.content.data.insert(index_num-count, i) - else: - self.content.data.extend(extra) - try: - self.content.items.extend(self.extend_items(self.content.items[0][0], self.content.items[-1][1], len(extra))) - except Exception as e: - logger.warning(f'{e}') - self.state.nested_parse(self.content, self.content_offset, contentnode) - self.transform_content(contentnode) - self.env.app.emit('object-description-transform', - self.domain, self.objtype, contentnode) - DocFieldTransformer(self).transform_all(contentnode) - self.env.temp_data['object'] = None - self.after_content() - return [self.indexnode, node] - - -class DefaultRole(SphinxDirective): - """ - Set the default interpreted text role. Overridden from docutils. - """ - - optional_arguments = 1 - final_argument_whitespace = False - - def run(self) -> List[Node]: - if not self.arguments: - docutils.unregister_role('') - return [] - role_name = self.arguments[0] - role, messages = roles.role(role_name, self.state_machine.language, - self.lineno, self.state.reporter) - if role: - docutils.register_role('', role) - self.env.temp_data['default_role'] = role_name - else: - literal_block = nodes.literal_block(self.block_text, self.block_text) - reporter = self.state.reporter - error = reporter.error('Unknown interpreted text role "%s".' % role_name, - literal_block, line=self.lineno) - messages += [error] - - return cast(List[nodes.Node], messages) - - -class DefaultDomain(SphinxDirective): - """ - Directive to (re-)set the default domain for this source file. - """ - - has_content = False - required_arguments = 1 - optional_arguments = 0 - final_argument_whitespace = False - option_spec = {} # type: Dict - - def run(self) -> List[Node]: - domain_name = self.arguments[0].lower() - # if domain_name not in env.domains: - # # try searching by label - # for domain in env.domains.values(): - # if domain.label.lower() == domain_name: - # domain_name = domain.name - # break - self.env.temp_data['default_domain'] = self.env.domains.get(domain_name) - return [] - -def setup(app: "Sphinx") -> Dict[str, Any]: - app.add_config_value("strip_signature_backslash", False, 'env') - directives.register_directive('default-role', DefaultRole) - directives.register_directive('default-domain', DefaultDomain) - directives.register_directive('describe', ObjectDescription) - # new, more consistent, name - directives.register_directive('object', ObjectDescription) - - app.add_event('object-description-transform') - - return { - 'version': 'builtin', - 'parallel_read_safe': True, - 'parallel_write_safe': True, - } - diff --git a/docs/sciai/docs/_ext/overwriteviewcode.txt b/docs/sciai/docs/_ext/overwriteviewcode.txt deleted file mode 100644 index 172780ec56b3ed90e7b0add617257a618cf38ee0..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/_ext/overwriteviewcode.txt +++ /dev/null @@ -1,378 +0,0 @@ -""" - sphinx.ext.viewcode - ~~~~~~~~~~~~~~~~~~~ - - Add links to module code in Python object descriptions. - - :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS. - :license: BSD, see LICENSE for details. -""" - -import posixpath -import traceback -import warnings -from os import path -from typing import Any, Dict, Generator, Iterable, Optional, Set, Tuple, cast - -from docutils import nodes -from docutils.nodes import Element, Node - -import sphinx -from sphinx import addnodes -from sphinx.application import Sphinx -from sphinx.builders import Builder -from sphinx.builders.html import StandaloneHTMLBuilder -from sphinx.deprecation import RemovedInSphinx50Warning -from sphinx.environment import BuildEnvironment -from sphinx.locale import _, __ -from sphinx.pycode import ModuleAnalyzer -from sphinx.transforms.post_transforms import SphinxPostTransform -from sphinx.util import get_full_modname, logging, status_iterator -from sphinx.util.nodes import make_refnode - - -logger = logging.getLogger(__name__) - - -OUTPUT_DIRNAME = '_modules' - - -class viewcode_anchor(Element): - """Node for viewcode anchors. - - This node will be processed in the resolving phase. - For viewcode supported builders, they will be all converted to the anchors. - For not supported builders, they will be removed. - """ - - -def _get_full_modname(app: Sphinx, modname: str, attribute: str) -> Optional[str]: - try: - return get_full_modname(modname, attribute) - except AttributeError: - # sphinx.ext.viewcode can't follow class instance attribute - # then AttributeError logging output only verbose mode. - logger.verbose('Didn\'t find %s in %s', attribute, modname) - return None - except Exception as e: - # sphinx.ext.viewcode follow python domain directives. - # because of that, if there are no real modules exists that specified - # by py:function or other directives, viewcode emits a lot of warnings. - # It should be displayed only verbose mode. - logger.verbose(traceback.format_exc().rstrip()) - logger.verbose('viewcode can\'t import %s, failed with error "%s"', modname, e) - return None - - -def is_supported_builder(builder: Builder) -> bool: - if builder.format != 'html': - return False - elif builder.name == 'singlehtml': - return False - elif builder.name.startswith('epub') and not builder.config.viewcode_enable_epub: - return False - else: - return True - - -def doctree_read(app: Sphinx, doctree: Node) -> None: - env = app.builder.env - if not hasattr(env, '_viewcode_modules'): - env._viewcode_modules = {} # type: ignore - - def has_tag(modname: str, fullname: str, docname: str, refname: str) -> bool: - entry = env._viewcode_modules.get(modname, None) # type: ignore - if entry is False: - return False - - code_tags = app.emit_firstresult('viewcode-find-source', modname) - if code_tags is None: - try: - analyzer = ModuleAnalyzer.for_module(modname) - analyzer.find_tags() - except Exception: - env._viewcode_modules[modname] = False # type: ignore - return False - - code = analyzer.code - tags = analyzer.tags - else: - code, tags = code_tags - - if entry is None or entry[0] != code: - entry = code, tags, {}, refname - env._viewcode_modules[modname] = entry # type: ignore - _, tags, used, _ = entry - if fullname in tags: - used[fullname] = docname - return True - - return False - - for objnode in list(doctree.findall(addnodes.desc)): - if objnode.get('domain') != 'py': - continue - names: Set[str] = set() - for signode in objnode: - if not isinstance(signode, addnodes.desc_signature): - continue - modname = signode.get('module') - fullname = signode.get('fullname') - try: - if fullname and modname==None: - if fullname.split('.')[-1].lower() == fullname.split('.')[-1] and fullname.split('.')[-2].lower() != fullname.split('.')[-2]: - modname = '.'.join(fullname.split('.')[:-2]) - fullname = '.'.join(fullname.split('.')[-2:]) - else: - modname = '.'.join(fullname.split('.')[:-1]) - fullname = fullname.split('.')[-1] - fullname_new = fullname - except Exception: - logger.warning(f'error_modename:{modname}') - logger.warning(f'error_fullname:{fullname}') - refname = modname - if env.config.viewcode_follow_imported_members: - new_modname = app.emit_firstresult( - 'viewcode-follow-imported', modname, fullname, - ) - if not new_modname: - new_modname = _get_full_modname(app, modname, fullname) - modname = new_modname - # logger.warning(f'new_modename:{modname}') - if not modname: - continue - # fullname = signode.get('fullname') - # if fullname and modname==None: - fullname = fullname_new - if not has_tag(modname, fullname, env.docname, refname): - continue - if fullname in names: - # only one link per name, please - continue - names.add(fullname) - pagename = posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/')) - signode += viewcode_anchor(reftarget=pagename, refid=fullname, refdoc=env.docname) - - -def env_merge_info(app: Sphinx, env: BuildEnvironment, docnames: Iterable[str], - other: BuildEnvironment) -> None: - if not hasattr(other, '_viewcode_modules'): - return - # create a _viewcode_modules dict on the main environment - if not hasattr(env, '_viewcode_modules'): - env._viewcode_modules = {} # type: ignore - # now merge in the information from the subprocess - for modname, entry in other._viewcode_modules.items(): # type: ignore - if modname not in env._viewcode_modules: # type: ignore - env._viewcode_modules[modname] = entry # type: ignore - else: - if env._viewcode_modules[modname]: # type: ignore - used = env._viewcode_modules[modname][2] # type: ignore - for fullname, docname in entry[2].items(): - if fullname not in used: - used[fullname] = docname - - -def env_purge_doc(app: Sphinx, env: BuildEnvironment, docname: str) -> None: - modules = getattr(env, '_viewcode_modules', {}) - - for modname, entry in list(modules.items()): - if entry is False: - continue - - code, tags, used, refname = entry - for fullname in list(used): - if used[fullname] == docname: - used.pop(fullname) - - if len(used) == 0: - modules.pop(modname) - - -class ViewcodeAnchorTransform(SphinxPostTransform): - """Convert or remove viewcode_anchor nodes depends on builder.""" - default_priority = 100 - - def run(self, **kwargs: Any) -> None: - if is_supported_builder(self.app.builder): - self.convert_viewcode_anchors() - else: - self.remove_viewcode_anchors() - - def convert_viewcode_anchors(self) -> None: - for node in self.document.findall(viewcode_anchor): - anchor = nodes.inline('', _('[源代码]'), classes=['viewcode-link']) - refnode = make_refnode(self.app.builder, node['refdoc'], node['reftarget'], - node['refid'], anchor) - node.replace_self(refnode) - - def remove_viewcode_anchors(self) -> None: - for node in list(self.document.findall(viewcode_anchor)): - node.parent.remove(node) - - -def missing_reference(app: Sphinx, env: BuildEnvironment, node: Element, contnode: Node - ) -> Optional[Node]: - # resolve our "viewcode" reference nodes -- they need special treatment - if node['reftype'] == 'viewcode': - warnings.warn('viewcode extension is no longer use pending_xref node. ' - 'Please update your extension.', RemovedInSphinx50Warning) - return make_refnode(app.builder, node['refdoc'], node['reftarget'], - node['refid'], contnode) - - return None - - -def get_module_filename(app: Sphinx, modname: str) -> Optional[str]: - """Get module filename for *modname*.""" - source_info = app.emit_firstresult('viewcode-find-source', modname) - if source_info: - return None - else: - try: - filename, source = ModuleAnalyzer.get_module_source(modname) - return filename - except Exception: - return None - - -def should_generate_module_page(app: Sphinx, modname: str) -> bool: - """Check generation of module page is needed.""" - module_filename = get_module_filename(app, modname) - if module_filename is None: - # Always (re-)generate module page when module filename is not found. - return True - - builder = cast(StandaloneHTMLBuilder, app.builder) - basename = modname.replace('.', '/') + builder.out_suffix - page_filename = path.join(app.outdir, '_modules/', basename) - - try: - if path.getmtime(module_filename) <= path.getmtime(page_filename): - # generation is not needed if the HTML page is newer than module file. - return False - except IOError: - pass - - return True - - -def collect_pages(app: Sphinx) -> Generator[Tuple[str, Dict[str, Any], str], None, None]: - env = app.builder.env - if not hasattr(env, '_viewcode_modules'): - return - if not is_supported_builder(app.builder): - return - highlighter = app.builder.highlighter # type: ignore - urito = app.builder.get_relative_uri - - modnames = set(env._viewcode_modules) # type: ignore - - for modname, entry in status_iterator( - sorted(env._viewcode_modules.items()), # type: ignore - __('highlighting module code... '), "blue", - len(env._viewcode_modules), # type: ignore - app.verbosity, lambda x: x[0]): - if not entry: - continue - if not should_generate_module_page(app, modname): - continue - - code, tags, used, refname = entry - # construct a page name for the highlighted source - pagename = posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/')) - # highlight the source using the builder's highlighter - if env.config.highlight_language in ('python3', 'default', 'none'): - lexer = env.config.highlight_language - else: - lexer = 'python' - highlighted = highlighter.highlight_block(code, lexer, linenos=False) - # split the code into lines - lines = highlighted.splitlines() - # split off wrap markup from the first line of the actual code - before, after = lines[0].split('
')
-        lines[0:1] = [before + '
', after]
-        # nothing to do for the last line; it always starts with 
anyway - # now that we have code lines (starting at index 1), insert anchors for - # the collected tags (HACK: this only works if the tag boundaries are - # properly nested!) - maxindex = len(lines) - 1 - for name, docname in used.items(): - type, start, end = tags[name] - backlink = urito(pagename, docname) + '#' + refname + '.' + name - lines[start] = ( - '
%s' % (name, backlink, _('[文档]')) + - lines[start]) - lines[min(end, maxindex)] += '
' - # try to find parents (for submodules) - parents = [] - parent = modname - while '.' in parent: - parent = parent.rsplit('.', 1)[0] - if parent in modnames: - parents.append({ - 'link': urito(pagename, - posixpath.join(OUTPUT_DIRNAME, parent.replace('.', '/'))), - 'title': parent}) - parents.append({'link': urito(pagename, posixpath.join(OUTPUT_DIRNAME, 'index')), - 'title': _('Module code')}) - parents.reverse() - # putting it all together - context = { - 'parents': parents, - 'title': modname, - 'body': (_('

Source code for %s

') % modname + - '\n'.join(lines)), - } - yield (pagename, context, 'page.html') - - if not modnames: - return - - html = ['\n'] - # the stack logic is needed for using nested lists for submodules - stack = [''] - for modname in sorted(modnames): - if modname.startswith(stack[-1]): - stack.append(modname + '.') - html.append('
    ') - else: - stack.pop() - while not modname.startswith(stack[-1]): - stack.pop() - html.append('
') - stack.append(modname + '.') - html.append('
  • %s
  • \n' % ( - urito(posixpath.join(OUTPUT_DIRNAME, 'index'), - posixpath.join(OUTPUT_DIRNAME, modname.replace('.', '/'))), - modname)) - html.append('' * (len(stack) - 1)) - context = { - 'title': _('Overview: module code'), - 'body': (_('

    All modules for which code is available

    ') + - ''.join(html)), - } - - yield (posixpath.join(OUTPUT_DIRNAME, 'index'), context, 'page.html') - - -def setup(app: Sphinx) -> Dict[str, Any]: - app.add_config_value('viewcode_import', None, False) - app.add_config_value('viewcode_enable_epub', False, False) - app.add_config_value('viewcode_follow_imported_members', True, False) - app.connect('doctree-read', doctree_read) - app.connect('env-merge-info', env_merge_info) - app.connect('env-purge-doc', env_purge_doc) - app.connect('html-collect-pages', collect_pages) - app.connect('missing-reference', missing_reference) - # app.add_config_value('viewcode_include_modules', [], 'env') - # app.add_config_value('viewcode_exclude_modules', [], 'env') - app.add_event('viewcode-find-source') - app.add_event('viewcode-follow-imported') - app.add_post_transform(ViewcodeAnchorTransform) - return { - 'version': sphinx.__display_version__, - 'env_version': 1, - 'parallel_read_safe': True - } diff --git a/docs/sciai/docs/requirements.txt b/docs/sciai/docs/requirements.txt deleted file mode 100644 index a1b6a69f6dbd9c6f78710f56889e14f0e85b27f4..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -sphinx == 4.4.0 -docutils == 0.17.1 -myst-parser == 0.18.1 -sphinx_rtd_theme == 1.0.0 -numpy -IPython -jieba diff --git a/docs/sciai/docs/source_en/_templates/classtemplate.rst b/docs/sciai/docs/source_en/_templates/classtemplate.rst deleted file mode 100644 index 37a8e95499c8343ad3f8e02d5c9095215fd9010a..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/_templates/classtemplate.rst +++ /dev/null @@ -1,27 +0,0 @@ -.. role:: hidden - :class: hidden-section - -.. currentmodule:: {{ module }} - -{% if objname in [] %} -{{ fullname | underline }} - -.. autofunction:: {{ fullname }} - -{% elif objname[0].istitle() %} -{{ fullname | underline }} - -.. autoclass:: {{ name }} - :exclude-members: construct - :members: - -{% else %} -{{ fullname | underline }} - -.. autofunction:: {{ fullname }} - -{% endif %} - -.. - autogenerated from _templates/classtemplate.rst - note it does not have :inherited-members: diff --git a/docs/sciai/docs/source_en/build_model_with_sciai.md b/docs/sciai/docs/source_en/build_model_with_sciai.md deleted file mode 100644 index b337f9f60d1404eb26a9219e4e1d834510399467..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/build_model_with_sciai.md +++ /dev/null @@ -1,240 +0,0 @@ -# Building Neural Networks with SciAI - -[![View Source On Gitee](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source_en.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_en/build_model_with_sciai.md)   - -SciAI base framework consists of several modules covering network setup, network training, validation and auxiliary functions. - -The following examples indicates the fundamental processes in using SciAI to build a neural network model. - -> You can download the full sample code here: -> - -## Model Building Basics - -The principle of setting up a neural network in ScAI is the same as in [MindSpore](https://www.mindspore.cn/tutorials/en/master/beginner/model.html), but in SciAI it is much easier. - -This chapter takes a Multi-Layer Percetron(MLP) as example, introduces how to train a network to solve the following equation. - -$$ -f(x) = {x_1}^2 + sin(x_2) -$$ - -For the codes of this part, please refer to the [codes](https://gitee.com/mindspore/mindscience/blob/master/SciAI/tutorial/example_net.py). - -### Setup Neural Networks - -The following code segment creates a multi-layer perceptron with 2-D input, 1-D output and two 5-D hidden layers. - -```python -from sciai.architecture import MLP -from sciai.common.initializer import XavierTruncNormal - -net = MLP(layers=[2, 5, 5, 1], weight_init=XavierTruncNormal(), bias_init='zeros', activation="tanh") -``` - -`MLP` will use normal distribution to initialize the weights, and initialize bias with zeros by default. The activation function is `tanh` by default. - -At meantime, `MLP` accepts various initialization method and all [activation functions](https://www.mindspore.cn/docs/en/master/api_python/mindspore.nn.html) provided by MindSpore, as well as those designed for scientific computing. - -### Loss Definition - -We define the loss function as a sub-class of [Cell](https://www.mindspore.cn/docs/en/master/api_python/nn/mindspore.nn.Cell.html), and calculate the loss in method `construct`. - -```python -from mindspore import nn -from sciai.architecture import MSE - -class ExampleLoss(nn.Cell): - def __init__(self, network): - super().__init__() - self.network = network - self.mse = MSE() - - def construct(self, x, y_true): - y_predict = self.network(x) - return self.mse(y_predict - y_true) - -net_loss = ExampleLoss(net) -``` - -The current loss predicted by `net` can be calculated by calling `net_loss` directly and taking the input `x` as the parameter with the true value `y_true`. - -```python -from mindspore import Tensor - -x = Tensor([[0.5, 0.5]]) -y_true = Tensor([0.72942554]) -print("loss value: ", net_loss(x, y_true)) -# expected output -... -loss value: 0.3026065 -``` - -### Model Training and Evaluation - -Then, by creating instance of trainer class provided by SciAI, we can start training with datasets. -In this case, we randomly sample the equation mentioned above to generate dataset `x_train` and `y_true` for training. - -The code segment for training is given as follows, indicating several abilities of SciAI. -The trainer class `TrainCellWithCallBack` is similar to [MindSpore.nn.TrainOneStepCell](https://www.mindspore.cn/docs/en/master/api_python/nn/mindspore.nn.TrainOneStepCell.html), -which needs network `net_loss` and optimizer as parameters and provides callbacks for scientific computation. -Callbacks include printing loss values and time consumption during training and automatic `ckpt` files saving. -The following code wll print the `loss` and time consumption every 100 epochs and save the current model as `ckpt` file every 1000 epochs. -SciAI provides the tool `to_tensor`, which converts multiple numpy data to `Tensor` conveniently. -Use `log_config` to specify the target directory for automatically saving the `TrainCellWithCallBack` callback printouts, as well as those printed by the user using `print_log`. - -```python -import numpy as np -from mindspore import nn -from sciai.common import TrainCellWithCallBack -from sciai.context.context import init_project -from sciai.utils import to_tensor, print_log, log_config - -# Get the correct platform automatically and set to GRAPH_MODE by default. -init_project() -# Auto log saving -log_config("./logs") - -def func(x): - """The function to be learned to""" - return x[:, 0:1] ** 2 + np.sin(x[:, 1:2]) - -optimizer = nn.Adam(net_loss.trainable_params()) -trainer = TrainCellWithCallBack(net_loss, optimizer, loss_interval=100, time_interval=100, ckpt_interval=1000) -x_train = np.random.rand(1000, 2) -# Randomly collect ground truth -y_true = func(x_train) -# Convert to Tensor data type -x_train, y_true = to_tensor((x_train, y_true)) -for _ in range(10001): - trainer(x_train, y_true) -print_log("Finished") -``` - -The expected output is as follows. - -```bash -python ./example_net.py -# expected output -... -step: 0, loss: 0.5189553, interval: 2.7039313316345215s, total: 2.7039313316345215s -step: 100, loss: 0.080132075, interval: 0.11984062194824219s, total: 2.8237719535827637s -step: 200, loss: 0.055663396, interval: 0.09104156494140625s, total: 2.91481351852417s -step: 300, loss: 0.032194577, interval: 0.09095025062561035s, total: 3.0057637691497803s -step: 400, loss: 0.015914217, interval: 0.09099435806274414s, total: 3.0967581272125244s -... -Finished -``` - -When the training of `net` is finished and loss converges, we can use the net to predict the value at `x` by calling `y = net(x)`. -Continue to randomly sample a number of positions `x_val` for validation. - -```python -x_val = np.random.rand(5, 2) -y_true = func(x_val) -y_pred = net(to_tensor(x_val)).asnumpy() -print_log("y_true:") -print_log(y_true) -print_log("y_pred:") -print_log(y_pred) -``` - -The expected output is as follows. After training, the predicted values are close to those obtained by numerical calculation. - -```bash -# expected output -y_true: -[[0.34606973] - [0.70457536] - [0.90531053] - [0.84420218] - [0.48239506]] -y_pred: -[[0.34271246] - [0.70356864] - [0.89893466] - [0.8393946 ] - [0.47805673]] -``` - -## Model Building Extension - -User can solve more complicated problems with SciAI, for example Physics-Informed Neural Network(PINN). This chapter introduces how to use MLP to solve the following Partial Differential Equation(PDE) with SciAI. - -$$ -\frac{\partial{f}}{\partial{x}} - 2 \frac{f}{x} + {x}^2 {y}^2 = 0 -$$ - -The boundary conditions are defined to be - -$$ -f(0) = 0, f(1) = 1. -$$ - -Under those boundary conditions, the analytic solution of this PDE is - -$$ -f(x) = \frac{x^2}{0.2 x^5 + 0.8}. -$$ - -For the codes of this part, please refer to [codes](https://gitee.com/mindspore/mindscience/blob/master/SciAI/tutorial/example_grad_net.py). - -### Loss Definition - -Similar to the loss definition in the last chapter, the loss should be defined as a child class of [Cell](https://www.mindspore.cn/docs/en/master/api_python/nn/mindspore.nn.Cell.html). - -The difference is that in this loss function, the partial derivative of the original function needs to be calculated. -SciAI provides tool `operators.grad` for this situation. Through setting the input and output index, we can calculate the derivative of certain inputs w.r.t. certain output. -In this problem, the dimensions of input and output are 1, therefore, we set `input_index` and `output_index` to 0. - -```python -from mindspore import nn, ops -from sciai.architecture import MSE, MLP -from sciai.operators import grad - -class ExampleLoss(nn.Cell): - """ Loss definition class""" - def __init__(self, network): - super().__init__() - self.network = network - self.dy_dx = grad(net=self.network, output_index=0, input_index=0) # partial differential definition - self.mse = MSE() - - def construct(self, x, x_bc, y_bc_true): - y = self.network(x) - dy_dx = self.dy_dx(x) - domain_res = dy_dx - 2 * ops.div(y, x) + ops.mul(ops.pow(x, 2), ops.pow(y, 2)) # PDE residual error - - y_bc = self.network(x_bc) - bc_res = y_bc_true - y_bc # Boundary conditions residual - return self.mse(domain_res) + 10 * self.mse(bc_res) -``` - -### Model Training and Evaluation - -Executing training and evaluation by launching the script in the terminal, we can get the following expected outputs. The predictions `y_pred` are close to the true values `y_true`. - -```bash -python ./example_grad_net.py -# expected output -... -step: 0, loss: 3.1961572, interval: 3.117840051651001s, total: 3.117840051651001s -step: 100, loss: 1.0862937, interval: 0.23533344268798828s, total: 3.353173494338989s -step: 200, loss: 0.7334847, interval: 0.21307134628295898s, total: 3.566244840621948s -step: 300, loss: 0.5629723, interval: 0.19696831703186035s, total: 3.763213157653809s -step: 400, loss: 0.4133342, interval: 0.20153212547302246s, total: 3.964745283126831s -... -Finished -y_true: -[[0.02245186] - [0.99459697] - [0.04027248] - [0.12594332] - [0.39779923]] -y_pred: -[[0.02293926] - [0.99337316] - [0.03924912] - [0.12166673] - [0.4006738 ]] -``` \ No newline at end of file diff --git a/docs/sciai/docs/source_en/conf.py b/docs/sciai/docs/source_en/conf.py deleted file mode 100644 index 87fd487bd8d54b9d42e0cb138963eec0351b45b6..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/conf.py +++ /dev/null @@ -1,272 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -import os -import shutil -import sys -import IPython -import re -import sphinx -sys.path.append(os.path.abspath('../_ext')) -import sphinx.ext.autosummary.generate as g -from sphinx.ext import autodoc as sphinx_autodoc - -# -- Project information ----------------------------------------------------- - -project = 'MindSpore SciAI' -copyright = 'MindSpore' -author = 'MindSpore' - -# The full version, including alpha/beta/rc tags -release = 'master' - - -# -- General configuration --------------------------------------------------- - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -myst_enable_extensions = ["dollarmath", "amsmath"] - - -myst_heading_anchors = 5 -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.autosummary', - 'sphinx.ext.doctest', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.napoleon', - 'sphinx.ext.viewcode', - 'myst_parser', - 'sphinx.ext.mathjax', - 'IPython.sphinxext.ipython_console_highlighting' -] - -source_suffix = { - '.rst': 'restructuredtext', - '.md': 'markdown', -} - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -mathjax_path = 'https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/mathjax/MathJax-3.2.2/es5/tex-mml-chtml.js' - -mathjax_options = { - 'async':'async' -} - -smartquotes_action = 'De' - -exclude_patterns = [] - -pygments_style = 'sphinx' - -autodoc_inherit_docstrings = False - -autosummary_generate = True - -# -- Options for HTML output ------------------------------------------------- - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# -html_theme = 'sphinx_rtd_theme' - -import sphinx_rtd_theme -layout_target = os.path.join(os.path.dirname(sphinx_rtd_theme.__file__), 'layout.html') -layout_src = '../../../../resource/_static/layout.html' -if os.path.exists(layout_target): - os.remove(layout_target) -shutil.copy(layout_src, layout_target) - -html_search_language = 'en' - -# Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = { - 'python': ('https://docs.python.org/3', '../../../../resource/python_objects.inv'), -} - -# overwriteautosummary_generate add view source for api and more autosummary class availably. -with open('../_ext/overwriteautosummary_generate.txt', 'r', encoding="utf8") as f: - exec(f.read(), g.__dict__) - -# Modify default signatures for autodoc. -autodoc_source_path = os.path.abspath(sphinx_autodoc.__file__) -autodoc_source_re = re.compile(r'stringify_signature\(.*?\)') -get_param_func_str = r"""\ -import re -import inspect as inspect_ - -def remove_typehints_content(text): - # 初始化括号匹配标记,0为无括号包裹 - bracket_count = 0 - start_idx = -1 # 记录第一个":"的位置 - - for i, char in enumerate(text): - # 1. 找到第一个":",记录起始位置 - if start_idx == -1 and char == ":": - start_idx = i - continue - - # 2. 已找到":",开始判断括号状态 - if start_idx != -1: - # 遇到"("或"[",括号计数+1(进入括号内) - if char in ("(", "["): - bracket_count += 1 - # 遇到")"或"]",括号计数-1(离开括号内) - elif char in (")", "]"): - bracket_count = max(0, bracket_count - 1) # 避免负数值 - # 3. 找到不在括号内的第一个",",执行删除 - elif char == "," and bracket_count == 0: - return text[:start_idx] + text[i:] # 拼接删除后的内容 - # 4. 找到不在括号内的第一个"=",执行删除 - elif char == "=" and bracket_count == 0: - return text[:start_idx] + " " + text[i:] # 拼接删除后的内容,"="前需要有一个空格 - - # 若未找到目标",",返回原文本 - return text - -def get_param_func(func): - try: - source_code = inspect_.getsource(func) - if func.__doc__: - source_code = source_code.replace(func.__doc__, '') - all_params_str = re.findall(r"def [\w_\d\-]+\(([\S\s]*?)(\):|\) ->.*?:)", source_code) - all_params = re.sub("(self|cls)(,|, )?", '', all_params_str[0][0].replace("\n", "").replace("'", "\"")) - - if ":" in all_params: - colon_idx = all_params.find(":") - # 处理非最后一个":"以后的内容 - while colon_idx != -1 and "," in all_params[colon_idx+1:]: - all_params = remove_typehints_content(all_params) - # 最后一个":"以后的内容中包含"," - if colon_idx == all_params.find(":"): - break - colon_idx = all_params.find(":") - - # 去掉最后一个":"以后的内容 - colon_idx = all_params.find(":") - if colon_idx != -1: - # 最后一个":"以后的内容中包含"=",需要保留"="及以后的内容 - if "=" in all_params[colon_idx+1:]: - all_params = re.sub(":(.*?)=", ' =', all_params) - # 正常删除最后一个":"以后的内容 - else: - all_params = re.sub(":.*$", '', all_params) - # 目前仅有lambda x出现在最后的情况 - if all_params.endswith("lambda x"): - all_params += ": ..." - - return all_params - except: - return '' - -def get_obj(obj): - if isinstance(obj, type): - return obj.__init__ - - return obj -""" - -with open(autodoc_source_path, "r+", encoding="utf8") as f: - code_str = f.read() - code_str = autodoc_source_re.sub('"(" + get_param_func(get_obj(self.object)) + ")"', code_str, count=0) - exec(get_param_func_str, sphinx_autodoc.__dict__) - exec(code_str, sphinx_autodoc.__dict__) - -# Copy source files of en python api from sciai repository. -import shutil -from sphinx.util import logging -logger = logging.getLogger(__name__) - -src_dir = os.path.join(os.getenv("MSC_PATH"), 'docs/api_python_en/sciai') - -if not os.path.exists(src_dir): - logger.warning("There is no source dir in sciai repository.") -else: - src_files = os.listdir(src_dir) - for i in os.listdir(src_dir): - if os.path.isfile(os.path.join(src_dir,i)): - if os.path.exists('./'+i): - os.remove('./'+i) - shutil.copy(os.path.join(src_dir,i),'./'+i) - else: - if os.path.exists('./'+i): - shutil.rmtree('./'+i) - shutil.copytree(os.path.join(src_dir,i),'./'+i) - -# get params for add view source -import json - -if os.path.exists('../../../../tools/generate_html/version.json'): - with open('../../../../tools/generate_html/version.json', 'r+', encoding='utf-8') as f: - version_inf = json.load(f) -elif os.path.exists('../../../../tools/generate_html/daily_dev.json'): - with open('../../../../tools/generate_html/daily_dev.json', 'r+', encoding='utf-8') as f: - version_inf = json.load(f) -elif os.path.exists('../../../../tools/generate_html/daily.json'): - with open('../../../../tools/generate_html/daily.json', 'r+', encoding='utf-8') as f: - version_inf = json.load(f) - -if os.getenv("MSC_PATH").split('/')[-1]: - copy_repo = os.getenv("MSC_PATH").split('/')[-1] -else: - copy_repo = os.getenv("MSC_PATH").split('/')[-2] - -branch = [version_inf[i]['branch'] for i in range(len(version_inf)) if version_inf[i]['name'] == copy_repo][0] -docs_branch = [version_inf[i]['branch'] for i in range(len(version_inf)) if version_inf[i]['name'] == 'tutorials'][0] -cst_module_name = 'sciai' -repo_whl = 'SciAI/sciai' -giturl = 'https://gitee.com/mindspore/' - -import sciai - -sys.path.append(os.path.abspath('../../../../resource/sphinx_ext')) -# import nbsphinx_mod - -sys.path.append(os.path.abspath('../../../../resource/search')) -import search_code - -sys.path.append(os.path.abspath('../../../../resource/custom_directives')) -from custom_directives import IncludeCodeDirective - -from myautosummary import MsPlatformAutoSummary, MsCnPlatformAutoSummary - -def setup(app): - app.add_directive('msplatformautosummary', MsPlatformAutoSummary) - app.add_directive('mscnplatformautosummary', MsCnPlatformAutoSummary) - app.add_directive('includecode', IncludeCodeDirective) - app.add_config_value('docs_branch', '', True) - app.add_config_value('branch', '', True) - app.add_config_value('cst_module_name', '', True) - app.add_config_value('copy_repo', '', True) - app.add_config_value('giturl', '', True) - app.add_config_value('repo_whl', '', True) - -src_release = os.path.join(os.getenv("MSC_PATH"), 'SciAI/RELEASE.md') -des_release = "./RELEASE.md" -with open(src_release, "r", encoding="utf-8") as f: - data = f.read() -if len(re.findall("\n## (.*?)\n",data)) > 1: - content = re.findall("(## [\s\S\n]*?)\n## ", data) -else: - content = re.findall("(## [\s\S\n]*)", data) -#result = content[0].replace('# MindFlow', '#', 1) -with open(des_release, "w", encoding="utf-8") as p: - p.write("# Release Notes"+"\n\n") - p.write(content[0]) \ No newline at end of file diff --git a/docs/sciai/docs/source_en/images/architecture.png b/docs/sciai/docs/source_en/images/architecture.png deleted file mode 100644 index 17ff9d3972e4e0daedb2989a6d8a144e1cd81384..0000000000000000000000000000000000000000 Binary files a/docs/sciai/docs/source_en/images/architecture.png and /dev/null differ diff --git a/docs/sciai/docs/source_en/index.rst b/docs/sciai/docs/source_en/index.rst deleted file mode 100644 index 2575b7c98817aae9104bbc7912b46369dc9378d4..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/index.rst +++ /dev/null @@ -1,52 +0,0 @@ -Introduction to SciAI -===================== - -Based on MindSpore, SciAI is a model library with 60 built-in most frequently used and cited AI4Sci(AI for Science) models, which cover SOTA models from physics-informed (PINNs, DeepRitz, PFNN, etc.) to neural operators (FNO, DeepONet, PDENet), ranking No.1 in the world in terms of coverage. -MindSpore SciAI provides the developers and users with a high-level API (auto configuration, auto instantiation, training and fine-tuning, etc.), allowing an immediate deployment. -With these features, MindSpore SciAI covers a wide variety of scientific computation fields, including fluid dynamics, electromagnetism, sound, heat, solid and biology, providing developers and users with an efficient and convenient AI4SCI computation platform. - -.. raw:: html - - - -.. toctree:: - :maxdepth: 1 - :caption: Installation - - installation - -.. toctree:: - :maxdepth: 1 - :caption: Launching Instruction - - launch_with_scripts - launch_with_api - -.. toctree:: - :maxdepth: 1 - :caption: Model Library - - model_library - -.. toctree:: - :maxdepth: 1 - :caption: Tutorial - - build_model_with_sciai - -.. toctree:: - :maxdepth: 1 - :caption: API Reference - - sciai.architecture - sciai.common - sciai.context - sciai.operators - sciai.utils - -.. toctree:: - :glob: - :maxdepth: 1 - :caption: RELEASE NOTES - - RELEASE diff --git a/docs/sciai/docs/source_en/installation.md b/docs/sciai/docs/source_en/installation.md deleted file mode 100644 index 8120de79c0b9414f5471389e1b8d618a5abe2ef7..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/installation.md +++ /dev/null @@ -1,74 +0,0 @@ -# MindSpore SciAI Installation - -[![View Source On Gitee](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source_en.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_en/installation.md) -   - -## System Environment Information Confirmation - -- The hardware platform should be Ascend or GPU. -- See [MindSpore Installation Guide](https://www.mindspore.cn/install/en) to install MindSpore. - The versions of MindSpore Elec and MindSpore must be consistent. -- All other dependencies are included - in [requirements.txt](https://gitee.com/mindspore/mindscience/blob/master/SciAI/requirements.txt). - -## Installation - -You can install MindSpore SciAI either by pip or by source code. - -### Method 1: Install With Pip - -This method installs SciAI from .whl package automatically downloaded from MindSpore website, -which does not require the download and compilation of source code. - -```bash -pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/gpu/{arch}/cuda-11.1/sciai-{version}-cp37-cp37m-linux_{arch}.whl -i https://pypi.tuna.tsinghua.edu.cn/simple -``` - -> - When the network is connected, dependencies of the SciAI installation package are automatically downloaded during - the .whl package installation. For details about dependencies, see setup.py. -> - {version} denotes the version of SciAI. For example, when you are installing SciAI 0.1.0, {version} should - be `0.1.0`. -> - {arch} denotes the system architecture. For example, the Linux system you are using is x86 architecture 64-bit, - {arch} should be `x86_64`. If the system is ARM architecture 64-bit, then it should be `aarch64`. - -The following table provides the corresponding installation commands to each architecture and Python version. - -| Device | Architecture | Python | Command | -|--------|--------------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Ascend | x86_64 | Python=3.7 | `pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/gpu/x86_64/cuda-11.1/sciai-0.1.0-cp37-cp37m-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple` | -| | aarch64 | Python=3.7 | `pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/ascend/aarch64/sciai-0.1.0-cp37-cp37m-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple` | -| GPU | x86_64 | Python=3.7 | `pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/gpu/x86_64/cuda-11.1/sciai-0.1.0-cp37-cp37m-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple` | - -Note: If you have other MindScience package(s) installed in your conda or python env, such as `MindElec`, `MindFlow` -, `MindSponge`, please uninstall the MindScience package(s) in the environment first to avoid pip behavior conflicts. - -### Method 2: Install From Source Code - -1. Clone the source code from the Git repository of MindScience. - - ```bash - cd ~ - git clone https://gitee.com/mindspore/mindscience.git - ``` - -2. Build SciAI with script `build.sh`. - - ```bash - cd mindscience/SciAI - bash build.sh -j8 - ``` - -3. After the compilation is complete, install the compiled `.whl` package with the following command. - - ```bash - bash install.sh - ``` - -### Installation Verification - -To verify the installation, run the following commands. If the error message `No module named 'sciai'` is not displayed, -the installation is successful. - -```bash -python -c 'import sciai' -``` diff --git a/docs/sciai/docs/source_en/launch_with_api.md b/docs/sciai/docs/source_en/launch_with_api.md deleted file mode 100644 index 23f1517c518a58774b4f6b8b1ce41c367597cca4..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/launch_with_api.md +++ /dev/null @@ -1,57 +0,0 @@ -# Launching Model with API - -[![View Source On Gitee](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source_en.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_en/launch_with_api.md)   - -MindSpore SciAI provides users with a high order interface `AutoModel`, with which the supported model in the model library can be instantiated with one line code. - -User can launch training and evaluation process with `AutoModel`. - -## Obtaining the Network with AutoModel - -User can use the function `AutoModel.from_pretrained` to get the network models, which are supported in SciAI. - -Here we use the model Conservative Physics-Informed Neural Networks (CPINNs) as example. For the codes of CPINNs model, please refer to the [link](https://gitee.com/mindspore/mindscience/tree/master/SciAI/sciai/model/cpinns). - -The fundamental idea about this model can be found in this [paper](https://www.sciencedirect.com/science/article/abs/pii/S0045782520302127). - -```python -from sciai.model import AutoModel - -# obtain the cpinns model. -model = AutoModel.from_pretrained("cpinns") -``` - -## Training and Fine-tuning with AutoModel - -User can use the function `AutoModel.train` to train the neural networks, and before training, user can use `AutoModel.update_config` to configure the training parameters or finetune the model by loading the `.ckpt` file. - -The acceptable arguments for API `AutoModel.update_config` depends on the model instantiated. - -```python -from sciai.model import AutoModel - -# obtain the cpinns model. -model = AutoModel.from_pretrained("cpinns") -# (optional) load the ckpt file and initialize the model based on the loaded parameters. -model.update_config(load_ckpt=True, load_ckpt_path="./checkpoints/your_file.ckpt", epochs=500) -# train the network with default configuration, -# the figures, data and logs generated will be saved in your execution path. -model.train() -``` - -## Evaluating with AutoModel - -User can evaluate the trained networks with function `AutoModel.evaluate`. - -This function will load the `.ckpt` files provided in SciAI by default. Alternatively, user can load their own `.ckpt` file with interface `AutoModel.update_config`. - -```python -from sciai.model import AutoModel - -# obtain the cpinns model -model = AutoModel.from_pretrained("cpinns") -# (optional) load the ckpt file provided by user -model.update_config(load_ckpt=True, load_ckpt_path="./checkpoints/your_file.ckpt") -# evaluate the model -model.evaluate() -``` diff --git a/docs/sciai/docs/source_en/launch_with_scripts.md b/docs/sciai/docs/source_en/launch_with_scripts.md deleted file mode 100644 index 5fc05c2dd4ad884a1d7b674e70430cd32fd064a9..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/launch_with_scripts.md +++ /dev/null @@ -1,64 +0,0 @@ -# Launching Model with Scripts - -[![View Source On Gitee](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source_en.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_en/launch_with_scripts.md)   - -The models in MindSpore SciAI provides users with scripts for training and evaluation. - -User can train or evaluate any model by running scripts, and the model parameters can be adjusted either through editing the config file or passing parameters in the command line. [This folder](https://gitee.com/mindspore/mindscience/tree/master/SciAI/sciai/model) contains all the models that support launching with scripts. - -The following content introduces the general process of training, evaluating models with scripts, taking Conservative Physics-Informed Neural Networks(CPINNs) as an example. For the codes of CPINNs model, please refer to the [link](https://gitee.com/mindspore/mindscience/tree/master/SciAI/sciai/model/cpinns). - -The fundamental idea about this model can be found in this [paper](https://www.sciencedirect.com/science/article/abs/pii/S0045782520302127). - -## Downloading the Repository - -User can clone the whole repository and initialize the environment variable `PYTHONPATH` with the following commands. - -```bash -git clone https://gitee.com/mindspore/mindscience -source ./mindscience/SciAI/.env -``` - -After a successful clone, user can start training or evaluating according to the `Quick Start` section in the [README.md](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/README.md)(In case of CPINNs). - -```bash -cd ./mindscience/SciAI/sciai/model/cpinns/ -``` - -## Training and Fine-tuning the Model - -User can run script [train.py](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/train.py) in each model directory to train the models. - -```bash -python ./train.py [--parameters] -# expected output -... -step: 0, loss1: 2.1404986, loss2: 8.205103, loss3: 37.23588, loss4: 3.56359, interval: 50.85803508758545s, total: 50.85803508758545s -step: 10, loss1: 2.6560388, loss2: 3.869413, loss3: 9.323585, loss4: 2.1194165, interval: 5.159524917602539s, total: 56.01756000518799s -step: 20, loss1: 1.7885156, loss2: 4.470225, loss3: 3.3072894, loss4: 1.5674783, interval: 1.8615927696228027s, total: 57.87915277481079s -... -``` - -Use the `.ckpt` file to finetune the network: - -```bash -python ./train.py --load_ckpt true --load_ckpt_path {your_file}.ckpt [--parameters] -``` - -Using the optional parameter `[--parameters]`, user can configure the training process of the model, including learning rate, training epochs, data saving and loading paths and so on. - -For details about the configurable parameters in each model, see the `Script Parameters` section in the [README.md](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/README.md). - -## Evaluating the Model - -User can run script `eval.py` in each model directory to evaluate the trained networks. - -```bash -python ./eval.py [--parameters] -# expected output -... -error_u: 0.024803562642018585 -Total time running eval: 20.625872135162354 seconds -``` - -Using the optional parameter `[--parameters]`, user can configure the evaluation process of the model, including the data read and save paths, checkpoints file loading paths, and so on. \ No newline at end of file diff --git a/docs/sciai/docs/source_en/model_library.md b/docs/sciai/docs/source_en/model_library.md deleted file mode 100644 index b6f3ae4c1d41f151c73d508e8c7c6baf64052b66..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_en/model_library.md +++ /dev/null @@ -1,74 +0,0 @@ -# Model Library - -[![View Source On Gitee](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source_en.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_en/model_library.md)   - -SciAI model library provides a wide variety of models that are frequently used and cited in scientific computation. -The following table summarizes the current available neural networks and their corresponding domains. - -| Domain | Network | MindSpore Implementation and Parameters | Ascend | GPU | -|---------------------|-----------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:------:|:---:| -| General Physics | [auq_pinns](https://www.sciencedirect.com/science/article/pii/S0021999119303584) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/auq_pinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [cpinns](https://www.sciencedirect.com/science/article/abs/pii/S0045782520302127) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [deep_hpms](https://www.jmlr.org/papers/volume19/18-046/18-046.pdf) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deep_hpms/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [deep_ritz](https://arxiv.org/abs/1710.00211) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deep_ritz/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [deepbsde](https://www.pnas.org/doi/10.1073/pnas.1718942115) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deepbsde/README.md#script-parameters) | | ✅ | -| General Physics | [deeponet](https://www.nature.com/articles/s42256-021-00302-5) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deeponet/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [dgm](https://arxiv.org/abs/1708.07469) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/dgm/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [fbsnns](https://arxiv.org/abs/1804.07010) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/fbsnns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [fpinns](https://arxiv.org/abs/1811.08967) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/fpinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [gradient_pathologies_pinns](https://arxiv.org/abs/2001.04536) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/gradient_pathologies_pinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [hp_vpinns](https://arxiv.org/abs/2003.05385) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/hp_vpinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [laaf](https://doi.org/10.1016/j.jcp.2019.109136) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/laaf/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [mgnet](https://link.springer.com/article/10.1007/s11425-019-9547-2) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/mgnet/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [multiscale_pinns](https://www.sciencedirect.com/science/article/abs/pii/S0045782521002759) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/multiscale_pinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [pfnn](https://www.sciencedirect.com/science/article/abs/pii/S0021999120308597) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pfnn/README_CN.md#脚本说明) | | ✅ | -| General Physics | [phygeonet](https://www.sciencedirect.com/science/article/abs/pii/S0021999120308536) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/phygeonet/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [pi_deeponet](https://www.sciencedirect.com/science/article/abs/pii/S0021999122009184) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pi_deeponet/README.md#script-parameters) | | ✅ | -| General Physics | [pinns](https://www.sciencedirect.com/science/article/abs/pii/S0021999118307125) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinns/README.md#script-parameters) | | ✅ | -| General Physics | [pinns_ntk](https://www.sciencedirect.com/science/article/pii/S002199912100663X) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinns_ntk/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [ppinns](https://www.sciencedirect.com/science/article/abs/pii/S0045782520304357) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/ppinns/README.md#script-parameters) | ✅ | ✅ | -| General Physics | [xpinns](https://doi.org/10.4208/cicp.OA-2020-0164) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/xpinns/README.md#script-parameters) | ✅ | ✅ | -| Hamiltonian Systems | [sympnets](https://www.sciencedirect.com/science/article/pii/S0893608020303063) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/sympnets/README.md#script-parameters) | ✅ | ✅ | -| Fluid Dynamic | [hfm](https://www.science.org/doi/abs/10.1126/science.aaw4741) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/hfm/README.md#script-parameters) | ✅ | ✅ | -| Fluid Dynamic | [label_free_dnn_surrogate](https://www.sciencedirect.com/science/article/pii/S004578251930622X) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/label_free_dnn_surrogate/README.md#script-parameters) | ✅ | ✅ | -| Fluid Dynamic | [nsf_nets](https://www.sciencedirect.com/science/article/pii/S0021999120307257) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/nsf_nets/README.md#script-parameters) | ✅ | ✅ | -| Fluid Dynamic | [*burgers_fno](https://arxiv.org/abs/2010.08895) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/burgers/fno1d/FNO1D.ipynb) | ✅ | ✅ | -| Fluid Dynamic | [*burgers_kno](https://arxiv.org/abs/2301.10022) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/burgers/kno1d/KNO1D.ipynb) | ✅ | ✅ | -| Fluid Dynamic | [*navier_stokes_fno](https://arxiv.org/abs/2010.08895) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/navier_stokes/fno2d/FNO2D.ipynb) | ✅ | ✅ | -| Fluid Dynamic | [*navier_stokes_kno](https://arxiv.org/abs/2301.10022) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/navier_stokes/kno2d/KNO2D.ipynb) | ✅ | ✅ | -| Fluid Dynamic | [*navier_stokes_3d_fno](https://arxiv.org/abs/2010.08895) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/navier_stokes/fno3d/FNO3D.ipynb) | ✅ | ✅ | -| Fluid Dynamic | [*pde_net](https://arxiv.org/abs/1710.09668) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_mechanism_fusion/pde_net/README.md) | ✅ | ✅ | -| Fluid Dynamic | [*percnn](https://www.nature.com/articles/s42256-023-00685-7) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_mechanism_fusion/percnn/README.md) | ✅ | ✅ | -| Elastodynamics | [pinn_elastodynamics](https://arxiv.org/abs/2006.08472) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinn_elastodynamics/README.md#script-parameters) | ✅ | ✅ | -| Thermodynamics | [pinn_heattransfer](https://arxiv.org/abs/1711.10561) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinn_heattransfer/README.md#script-parameters) | ✅ | ✅ | -| Meteorology | [enso](https://doi.org/10.1038/s41586-019-1559-7) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/enso/README.md#script-parameters) | ✅ | ✅ | -| Geology | [inversion_net](https://ieeexplore.ieee.org/abstract/document/8918045/) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/inversion_net/README.md#script-parameters) | ✅ | ✅ | -| Geology | [pinn_helmholtz](https://academic.oup.com/gji/article-abstract/228/3/1750/6409132) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinn_helmholtz/README.md#script-parameters) | ✅ | ✅ | -| Oceanic Physics | [ocean_model](https://gmd.copernicus.org/articles/12/4729/2019/) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/ocean_model/README.md#Model-Description) | | ✅ | -| Oceanic Physics | [pinns_swe](https://arxiv.org/abs/2104.00615) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinns_swe/README.md#script-parameters) | ✅ | ✅ | -| Electromagnetism | [maxwell_net](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/maxwell_net/README.md#script-parameters) | ✅ | ✅ | -| Electromagnetism | [*AD_FDTD_invert_f](https://www.mindspore.cn/mindelec/docs/en/r0.2/AD_FDTD.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/AD_FDTD/fdtd_forward/README.md#script-parameters) | | ✅ | -| Electromagnetism | [*AD_FDTD_microstrip_filter](https://www.mindspore.cn/mindelec/docs/en/r0.2/AD_FDTD.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/AD_FDTD/fdtd_forward/README.md#script-parameters) | | ✅ | -| Electromagnetism | [*AD_FDTD_inverse](https://www.mindspore.cn/mindelec/docs/en/r0.2/AD_FDTD.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/AD_FDTD/fdtd_inverse/README.md#script-parameters) | | ✅ | -| Electromagnetism | [*frequency_domain_maxwell](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/frequency_domain_maxwell/README.md#script-parameters) | ✅ | ✅ | -| Electromagnetism | [*frequency_domain_maxwell_3D_dielectric_slab](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/frequency_domain_maxwell_3D/dielectric_slab_3d/README.md#脚本参数) | ✅ | ✅ | -| Electromagnetism | [*frequency_domain_maxwell_3D_waveguide_cavity](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/frequency_domain_maxwell_3D/waveguide_cavity_3d/README.md#脚本参数) | ✅ | ✅ | -| Electromagnetism | [*meta_auto_decoder](https://arxiv.org/abs/2111.08823) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/incremental_learning/README.md#script-parameters) | ✅ | ✅ | -| Electromagnetism | [*pinn_fwi](https://agupubs.onlinelibrary.wiley.com/doi/abs/10.1029/2021JB023120) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/pinn_fwi/README.md) | ✅ | ✅ | -| Electromagnetism | [*SED_ANN](https://gitee.com/mindspore/mindscience/tree/master/MindElec/examples/data_driven/sed_ann) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/data_driven/sed_ann/README_CN.md) | ✅ | ✅ | -| Electromagnetism | [*time_domain_maxwell](https://www.ijcai.org/proceedings/2022/533) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/time_domain_maxwell/README.md#script-parameters) | ✅ | ✅ | -| Electromagnetism | [*metasurface_holograms](https://www.researching.cn/articles/OJ44d3746c3db8c1e1) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/metasurface/metasurface_holograms/README.md#parameters) | ✅ | ✅ | -| Biology | [*MEGA-Fold](https://arxiv.org/abs/2206.12240v1) | [link (inference)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) [link (training)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | ✅ | ✅ | -| Biology | [*MEGA-EvoGen](https://arxiv.org/abs/2208.09652) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | ✅ | ✅ | -| Biology | [*MEGA-Assessment](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | [link (inference)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) [link (training)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | ✅ | ✅ | -| Biology | [*ColabDesign](https://www.biorxiv.org/content/10.1101/2021.11.10.468128.abstract) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ColabDesign.md) | ✅ | ✅ | -| Biology | [*DeepFRI](https://www.nature.com/articles/s41467-021-23303-9) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/DeepFri.md) | ✅ | ✅ | -| Biology | [*Multimer](https://www.biorxiv.org/content/10.1101/2021.10.04.463034v1) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/afmultimer.md) | ✅ | ✅ | -| Biology | [*ProteinMPNN](https://www.science.org/doi/abs/10.1126/science.add2187) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ProteinMPNN.MD) | ✅ | ✅ | -| Biology | [*UFold](https://doi.org/10.1093/nar/gkab1074) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/UFold.md) | ✅ | ✅ | -| Biology | [*esm-if1](https://proceedings.mlr.press/v162/hsu22a.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ESM-IF1.md) | ✅ | ✅ | -| Biology | [*esm2](https://www.biorxiv.org/content/10.1101/2022.07.20.500902v1.full.pdf) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ESM-2.md) | ✅ | ✅ | -| Biology | [*grover](https://proceedings.neurips.cc/paper/2020/file/94aef38441efa3380a3bed3faf1f9d5d-Paper.pdf) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/GROVER.MD) | ✅ | ✅ | - -Note: the "*" in the model names indicates that these models have already been released at an earlier time by MindSpore -and MindScience. \ No newline at end of file diff --git a/docs/sciai/docs/source_zh_cn/_templates/classtemplate.rst b/docs/sciai/docs/source_zh_cn/_templates/classtemplate.rst deleted file mode 100644 index 37a8e95499c8343ad3f8e02d5c9095215fd9010a..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/_templates/classtemplate.rst +++ /dev/null @@ -1,27 +0,0 @@ -.. role:: hidden - :class: hidden-section - -.. currentmodule:: {{ module }} - -{% if objname in [] %} -{{ fullname | underline }} - -.. autofunction:: {{ fullname }} - -{% elif objname[0].istitle() %} -{{ fullname | underline }} - -.. autoclass:: {{ name }} - :exclude-members: construct - :members: - -{% else %} -{{ fullname | underline }} - -.. autofunction:: {{ fullname }} - -{% endif %} - -.. - autogenerated from _templates/classtemplate.rst - note it does not have :inherited-members: diff --git a/docs/sciai/docs/source_zh_cn/build_model_with_sciai.md b/docs/sciai/docs/source_zh_cn/build_model_with_sciai.md deleted file mode 100644 index 64430a43424f02444451ce2a462c764ae6b56fe5..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/build_model_with_sciai.md +++ /dev/null @@ -1,240 +0,0 @@ -# 使用SciAI构建神经网络 - -[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_zh_cn/build_model_with_sciai.md)   - -SciAI基础框架由若干基础模块构成,涵盖有神经网络搭建、训练、验证以及其他辅助函数等。 - -如下的示例展示了使用SciAI构建神经网络模型并进行训练的流程。 - -> 你可以在这里下载完整的样例代码: -> - -## 模型构建基础 - -使用SciAI基础框架创建神经网络的原理与[使用MindSpore构建网络](https://www.mindspore.cn/tutorials/zh-CN/master/beginner/model.html)一致,但过程将会十分简便。 - -本章节以一个多层感知器为例,介绍了使用SciAI训练并求解如下方程。 - -$$ -f(x) = {x_1}^2 + sin(x_2) -$$ - -该部分完整代码请参考[代码](https://gitee.com/mindspore/mindscience/blob/master/SciAI/tutorial/example_net.py)。 - -### 模型搭建 - -如下示例代码创建了一个输入维度为2,输出维度为1,包含两层维度为5的中间层的多层感知器。 - -```python -from sciai.architecture import MLP -from sciai.common.initializer import XavierTruncNormal - -net = MLP(layers=[2, 5, 5, 1], weight_init=XavierTruncNormal(), bias_init='zeros', activation="tanh") -``` - -`MLP`将默认使用正态分布随机生成网络权重,偏差`bias`默认为0,激活函数默认为`tanh`。 - -`MLP`同时接受多样化的初始化方式和MindSpore提供的所有[激活函数](https://www.mindspore.cn/docs/zh-CN/master/api_python/mindspore.nn.html),以及专为科学计算设计的激活函数。 - -### 损失函数定义 - -损失函数定义为[Cell](https://www.mindspore.cn/docs/zh-CN/master/api_python/nn/mindspore.nn.Cell.html#mindspore.nn.Cell)的子类,并将损失的计算方法写在方法`construct`中。 - -```python -from mindspore import nn -from sciai.architecture import MSE - -class ExampleLoss(nn.Cell): - def __init__(self, network): - super().__init__() - self.network = network - self.mse = MSE() - - def construct(self, x, y_true): - y_predict = self.network(x) - return self.mse(y_predict - y_true) - -net_loss = ExampleLoss(net) -``` - -此时,通过直接调用`net_loss`,并将输入`x`与真实值`y_true`作为参数,便可计算得到当前`net`预测的损失。 - -```python -from mindspore import Tensor - -x = Tensor([[0.5, 0.5]]) -y_true = Tensor([0.72942554]) -print("loss value: ", net_loss(x, y_true)) -# expected output -... -loss value: 0.3026065 -``` - -### 模型训练与推理 - -得到损失函数后,我们即可使用SciAI框架中已封装好的训练类,使用数据集进行训练。 -在本案例中,我们对方程进行随机采样,生成数据集`x_train`与`y_true`进行训练。 - -模型训练部分代码如下所示,其中主要展示了SciAI若干功能。 -模型训练类`TrainCellWithCallBack`,其与[MindSpore.nn.TrainOneStepCell](https://www.mindspore.cn/docs/zh-CN/master/api_python/nn/mindspore.nn.TrainOneStepCell.html#mindspore.nn.TrainOneStepCell)功能基本一致, -需要提供网络`net_loss`与优化器作为参数,并为科学计算功能增加了回调功能。 -回调包括打印训练`loss`值、训练时间、自动保存`ckpt`文件。 -如下的案例代码将会每100个训练周期打印`loss`值与训练时间,并在每1000个训练周期保存当前模型参数为`ckpt`文件。 -SciAI提供`to_tensor`工具,可以方便地将多个`numpy`数据同时转换为`Tensor`类型。 -使用`log_config`指定目标目录,用于自动保存`TrainCellWithCallBack`的回调打印,以及用户使用`print_log`所打印的内容。 - -```python -import numpy as np -from mindspore import nn -from sciai.common import TrainCellWithCallBack -from sciai.context import init_project -from sciai.utils import to_tensor, print_log, log_config - -# Get the correct platform automatically and set to GRAPH_MODE by default. -init_project() -# Auto log saving -log_config("./logs") - -def func(x): - """The function to be learned to""" - return x[:, 0:1] ** 2 + np.sin(x[:, 1:2]) - -optimizer = nn.Adam(net_loss.trainable_params()) -trainer = TrainCellWithCallBack(net_loss, optimizer, loss_interval=100, time_interval=100, ckpt_interval=1000) -x_train = np.random.rand(1000, 2) -# Randomly collect ground truth -y_true = func(x_train) -# Convert to Tensor data type -x_train, y_true = to_tensor((x_train, y_true)) -for _ in range(10001): - trainer(x_train, y_true) -print_log("Finished") -``` - -预期运行结果如下。 - -```bash -python ./example_net.py -# expected output -... -step: 0, loss: 0.5189553, interval: 2.7039313316345215s, total: 2.7039313316345215s -step: 100, loss: 0.080132075, interval: 0.11984062194824219s, total: 2.8237719535827637s -step: 200, loss: 0.055663396, interval: 0.09104156494140625s, total: 2.91481351852417s -step: 300, loss: 0.032194577, interval: 0.09095025062561035s, total: 3.0057637691497803s -step: 400, loss: 0.015914217, interval: 0.09099435806274414s, total: 3.0967581272125244s -... -Finished -``` - -在训练结束并且损失收敛时,通过调用`y = net(x)`即可得到`x`处的预测值`y`。 -继续随机采样若干位置`x_val`用于验证。 - -```python -x_val = np.random.rand(5, 2) -y_true = func(x_val) -y_pred = net(to_tensor(x_val)).asnumpy() -print_log("y_true:") -print_log(y_true) -print_log("y_pred:") -print_log(y_pred) -``` - -预期运行结果如下。经过训练,模型的预测值接近数值计算结果。 - -```bash -# expected output -y_true: -[[0.34606973] - [0.70457536] - [0.90531053] - [0.84420218] - [0.48239506]] -y_pred: -[[0.34271246] - [0.70356864] - [0.89893466] - [0.8393946 ] - [0.47805673]] -``` - -## 模型构建拓展 - -使用SciAI可以求解更为复杂的问题,例如物理驱动的神经网络(PINN)。该章节继续以一个多层感知器为例,介绍使用SciAI训练并求解如下偏微分方程。 - -$$ -\frac{\partial{f}}{\partial{x}} - 2 \frac{f}{x} + {x}^2 {y}^2 = 0 -$$ - -边界条件定义如下。 - -$$ -f(0) = 0, f(1) = 1 -$$ - -在此边界条件下,函数的解析解为: - -$$ -f(x) = \frac{x^2}{0.2 x^5 + 0.8} -$$ - -该部分完整代码请参考[代码](https://gitee.com/mindspore/mindscience/blob/master/SciAI/tutorial/example_grad_net.py)。 - -### 损失函数定义 - -与上一章中损失函数定义基本一致,需要定义损失为[Cell](https://www.mindspore.cn/docs/zh-CN/master/api_python/nn/mindspore.nn.Cell.html#mindspore.nn.Cell)的子类。 - -不同的是在该损失函数中,需要计算原函数的偏导。 -SciAI为此提供了便捷的工具`operators.grad`,通过设置网络输入与输出的索引,可以计算某个输入对某个输出的偏导值。 -在该问题中,输入输出维度均为1,因此设置`input_index`与`output_index`为0。 - -```python -from mindspore import nn, ops -from sciai.architecture import MSE, MLP -from sciai.operators import grad - -class ExampleLoss(nn.Cell): - """ Loss definition class""" - def __init__(self, network): - super().__init__() - self.network = network - self.dy_dx = grad(net=self.network, output_index=0, input_index=0) # partial differential definition - self.mse = MSE() - - def construct(self, x, x_bc, y_bc_true): - y = self.network(x) - dy_dx = self.dy_dx(x) - domain_res = dy_dx - 2 * ops.div(y, x) + ops.mul(ops.pow(x, 2), ops.pow(y, 2)) # PDE residual error - - y_bc = self.network(x_bc) - bc_res = y_bc_true - y_bc # Boundary conditions residual - return self.mse(domain_res) + 10 * self.mse(bc_res) -``` - -### 模型训练与推理 - -通过终端执行脚本文件,执行训练与推理,得到如下预期结果。最终预测值`y_pred`与真实值`y_true`基本接近。 - -```bash -python ./example_grad_net.py -# expected output -... -step: 0, loss: 3.1961572, interval: 3.117840051651001s, total: 3.117840051651001s -step: 100, loss: 1.0862937, interval: 0.23533344268798828s, total: 3.353173494338989s -step: 200, loss: 0.7334847, interval: 0.21307134628295898s, total: 3.566244840621948s -step: 300, loss: 0.5629723, interval: 0.19696831703186035s, total: 3.763213157653809s -step: 400, loss: 0.4133342, interval: 0.20153212547302246s, total: 3.964745283126831s -... -Finished -y_true: -[[0.02245186] - [0.99459697] - [0.04027248] - [0.12594332] - [0.39779923]] -y_pred: -[[0.02293926] - [0.99337316] - [0.03924912] - [0.12166673] - [0.4006738 ]] -``` \ No newline at end of file diff --git a/docs/sciai/docs/source_zh_cn/conf.py b/docs/sciai/docs/source_zh_cn/conf.py deleted file mode 100644 index bf84e1ca21534bf605fee9632ab35f54575d98db..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/conf.py +++ /dev/null @@ -1,315 +0,0 @@ -# Configuration file for the Sphinx documentation builder. -# -# This file only contains a selection of the most common options. For a full -# list see the documentation: -# https://www.sphinx-doc.org/en/master/usage/configuration.html - -# -- Path setup -------------------------------------------------------------- - -# If extensions (or modules to document with autodoc) are in another directory, -# add these directories to sys.path here. If the directory is relative to the -# documentation root, use os.path.abspath to make it absolute, like shown here. -# -import glob -import os -import shutil -import IPython -import re -import sys -import sphinx -import sphinx.ext.autosummary.generate as g -sys.path.append(os.path.abspath('../_ext')) - -from sphinx import directives -with open('../_ext/overwriteobjectiondirective.txt', 'r') as f: - exec(f.read(), directives.__dict__) - -from sphinx.ext import viewcode -with open('../_ext/overwriteviewcode.txt', 'r', encoding="utf8") as f: - exec(f.read(), viewcode.__dict__) - -from sphinx.ext import autodoc as sphinx_autodoc - -# -- Project information ----------------------------------------------------- - -project = 'MindSpore SciAI' -copyright = 'MindSpore' -author = 'MindSpore' - -# The full version, including alpha/beta/rc tags -release = 'master' - - -# -- General configuration --------------------------------------------------- - -# Add any Sphinx extension module names here, as strings. They can be -# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom -# ones. -myst_enable_extensions = ["dollarmath", "amsmath"] - - -myst_heading_anchors = 5 -extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.autosummary', - 'sphinx.ext.doctest', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.napoleon', - 'sphinx.ext.viewcode', - 'myst_parser', - 'sphinx.ext.mathjax', - 'IPython.sphinxext.ipython_console_highlighting' -] - -source_suffix = { - '.rst': 'restructuredtext', - '.md': 'markdown', -} - -# Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] - -# List of patterns, relative to source directory, that match files and -# directories to ignore when looking for source files. -# This pattern also affects html_static_path and html_extra_path. -mathjax_path = 'https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/mathjax/MathJax-3.2.2/es5/tex-mml-chtml.js' - -mathjax_options = { - 'async':'async' -} - -smartquotes_action = 'De' - -exclude_patterns = [] - -pygments_style = 'sphinx' - -autodoc_inherit_docstrings = False - -autosummary_generate = True - -autosummary_generate_overwrite = False - -# -- Options for HTML output ------------------------------------------------- - -# Reconstruction of sphinx auto generated document translation. -language = 'zh_CN' -locale_dirs = ['../../../../resource/locale/'] -gettext_compact = False - -# The theme to use for HTML and HTML Help pages. See the documentation for -# a list of builtin themes. -# -html_theme = 'sphinx_rtd_theme' - -html_search_language = 'zh' - -html_search_options = {'dict': '../../../resource/jieba.txt'} - -# Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = { - 'python': ('https://docs.python.org/3', '../../../../resource/python_objects.inv'), -} - -# Modify regex for sphinx.ext.autosummary.generate.find_autosummary_in_lines. -gfile_abs_path = os.path.abspath(g.__file__) -autosummary_re_line_old = r"autosummary_re = re.compile(r'^(\s*)\.\.\s+autosummary::\s*')" -autosummary_re_line_new = r"autosummary_re = re.compile(r'^(\s*)\.\.\s+(ms[a-z]*)?autosummary::\s*')" -with open(gfile_abs_path, "r+", encoding="utf8") as f: - data = f.read() - data = data.replace(autosummary_re_line_old, autosummary_re_line_new) - exec(data, g.__dict__) - -sys.path.append(os.path.abspath('../../../../resource/sphinx_ext')) -# import nbsphinx_mod - -# Modify default signatures for autodoc. -autodoc_source_path = os.path.abspath(sphinx_autodoc.__file__) -autodoc_source_re = re.compile(r'stringify_signature\(.*?\)') -get_param_func_str = r"""\ -import re -import inspect as inspect_ - -def remove_typehints_content(text): - # 初始化括号匹配标记,0为无括号包裹 - bracket_count = 0 - start_idx = -1 # 记录第一个":"的位置 - - for i, char in enumerate(text): - # 1. 找到第一个":",记录起始位置 - if start_idx == -1 and char == ":": - start_idx = i - continue - - # 2. 已找到":",开始判断括号状态 - if start_idx != -1: - # 遇到"("或"[",括号计数+1(进入括号内) - if char in ("(", "["): - bracket_count += 1 - # 遇到")"或"]",括号计数-1(离开括号内) - elif char in (")", "]"): - bracket_count = max(0, bracket_count - 1) # 避免负数值 - # 3. 找到不在括号内的第一个",",执行删除 - elif char == "," and bracket_count == 0: - return text[:start_idx] + text[i:] # 拼接删除后的内容 - # 4. 找到不在括号内的第一个"=",执行删除 - elif char == "=" and bracket_count == 0: - return text[:start_idx] + " " + text[i:] # 拼接删除后的内容,"="前需要有一个空格 - - # 若未找到目标",",返回原文本 - return text - -def get_param_func(func): - try: - source_code = inspect_.getsource(func) - if func.__doc__: - source_code = source_code.replace(func.__doc__, '') - all_params_str = re.findall(r"def [\w_\d\-]+\(([\S\s]*?)(\):|\) ->.*?:)", source_code) - all_params = re.sub("(self|cls)(,|, )?", '', all_params_str[0][0].replace("\n", "").replace("'", "\"")) - - if ":" in all_params: - colon_idx = all_params.find(":") - # 处理非最后一个":"以后的内容 - while colon_idx != -1 and "," in all_params[colon_idx+1:]: - all_params = remove_typehints_content(all_params) - # 最后一个":"以后的内容中包含"," - if colon_idx == all_params.find(":"): - break - colon_idx = all_params.find(":") - - # 去掉最后一个":"以后的内容 - colon_idx = all_params.find(":") - if colon_idx != -1: - # 最后一个":"以后的内容中包含"=",需要保留"="及以后的内容 - if "=" in all_params[colon_idx+1:]: - all_params = re.sub(":(.*?)=", ' =', all_params) - # 正常删除最后一个":"以后的内容 - else: - all_params = re.sub(":.*$", '', all_params) - # 目前仅有lambda x出现在最后的情况 - if all_params.endswith("lambda x"): - all_params += ": ..." - - return all_params - except: - return '' - -def get_obj(obj): - if isinstance(obj, type): - return obj.__init__ - - return obj -""" - -with open(autodoc_source_path, "r+", encoding="utf8") as f: - code_str = f.read() - code_str = autodoc_source_re.sub('"(" + get_param_func(get_obj(self.object)) + ")"', code_str, count=0) - exec(get_param_func_str, sphinx_autodoc.__dict__) - exec(code_str, sphinx_autodoc.__dict__) - -sys.path.append(os.path.abspath('../../../../resource/search')) -import search_code - -sys.path.append(os.path.abspath('../../../../resource/custom_directives')) -from custom_directives import IncludeCodeDirective - -# Copy source files of chinese python api from sciai repository. -import shutil -from sphinx.util import logging -logger = logging.getLogger(__name__) - -copy_path = 'docs/api_python/sciai' -src_dir = os.path.join(os.getenv("MSC_PATH"), copy_path) - -copy_list = [] - -present_path = os.path.dirname(__file__) - -for i in os.listdir(src_dir): - if os.path.isfile(os.path.join(src_dir,i)): - if os.path.exists('./'+i): - os.remove('./'+i) - shutil.copy(os.path.join(src_dir,i),'./'+i) - copy_list.append(os.path.join(present_path,i)) - else: - if os.path.exists('./'+i): - shutil.rmtree('./'+i) - shutil.copytree(os.path.join(src_dir,i),'./'+i) - copy_list.append(os.path.join(present_path,i)) - -# add view -import json - -if os.path.exists('../../../../tools/generate_html/version.json'): - with open('../../../../tools/generate_html/version.json', 'r+', encoding='utf-8') as f: - version_inf = json.load(f) -elif os.path.exists('../../../../tools/generate_html/daily_dev.json'): - with open('../../../../tools/generate_html/daily_dev.json', 'r+', encoding='utf-8') as f: - version_inf = json.load(f) -elif os.path.exists('../../../../tools/generate_html/daily.json'): - with open('../../../../tools/generate_html/daily.json', 'r+', encoding='utf-8') as f: - version_inf = json.load(f) - -if os.getenv("MSC_PATH").split('/')[-1]: - copy_repo = os.getenv("MSC_PATH").split('/')[-1] -else: - copy_repo = os.getenv("MSC_PATH").split('/')[-2] - -branch = [version_inf[i]['branch'] for i in range(len(version_inf)) if version_inf[i]['name'] == copy_repo][0] -docs_branch = [version_inf[i]['branch'] for i in range(len(version_inf)) if version_inf[i]['name'] == 'tutorials'][0] - -re_view = f"\n.. image:: https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/{docs_branch}/" + \ - f"resource/_static/logo_source.svg\n :target: https://gitee.com/mindspore/{copy_repo}/blob/{branch}/" - -for cur, _, files in os.walk(present_path): - for i in files: - flag_copy = 0 - if i.endswith('.rst'): - for j in copy_list: - if j in cur: - flag_copy = 1 - break - if os.path.join(cur, i) in copy_list or flag_copy: - try: - with open(os.path.join(cur, i), 'r+', encoding='utf-8') as f: - content = f.read() - new_content = content - if '.. include::' in content and '.. automodule::' in content: - continue - if 'autosummary::' not in content and "\n=====" in content: - re_view_ = re_view + copy_path + cur.split(present_path)[-1] + '/' + i + \ - '\n :alt: 查看源文件\n\n' - new_content = re.sub('([=]{5,})\n', r'\1\n' + re_view_, content, 1) - if new_content != content: - f.seek(0) - f.truncate() - f.write(new_content) - except Exception: - print(f'打开{i}文件失败') - -import sciai -from myautosummary import MsPlatformAutoSummary, MsCnPlatformAutoSummary, MsCnAutoSummary - -rst_files = set([i.replace('.rst', '') for i in glob.glob('./**/*.rst', recursive=True)]) - -def setup(app): - app.add_directive('msplatformautosummary', MsPlatformAutoSummary) - app.add_directive('mscnplatformautosummary', MsCnPlatformAutoSummary) - app.add_directive('mscnautosummary', MsCnAutoSummary) - app.add_directive('includecode', IncludeCodeDirective) - app.add_config_value('rst_files', set(), False) - -src_release = os.path.join(os.getenv("MSC_PATH"), 'SciAI/RELEASE_CN.md') -des_release = "./RELEASE.md" -with open(src_release, "r", encoding="utf-8") as f: - data = f.read() -if len(re.findall("\n## (.*?)\n",data)) > 1: - content = re.findall("(## [\s\S\n]*?)\n## ", data) -else: - content = re.findall("(## [\s\S\n]*)", data) -#result = content[0].replace('# MindFlow', '#', 1) -with open(des_release, "w", encoding="utf-8") as p: - p.write("# Release Notes"+"\n\n") - p.write(content[0]) \ No newline at end of file diff --git a/docs/sciai/docs/source_zh_cn/images/architecture_cn.png b/docs/sciai/docs/source_zh_cn/images/architecture_cn.png deleted file mode 100644 index 10e3de7876b700697de1b80943caabe9773cd896..0000000000000000000000000000000000000000 Binary files a/docs/sciai/docs/source_zh_cn/images/architecture_cn.png and /dev/null differ diff --git a/docs/sciai/docs/source_zh_cn/index.rst b/docs/sciai/docs/source_zh_cn/index.rst deleted file mode 100644 index 6ae8935ee0e4848b501fece84e28995109d10c7b..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/index.rst +++ /dev/null @@ -1,52 +0,0 @@ -MindSpore SciAI介绍 -=================== - -MindSpore SciAI是基于昇思MindSpore打造的AI4SCI高频基础模型套件,内置了60+高频模型,覆盖物理感知(如PINNs、DeepRitz以及PFNN)和神经算子(如FNO、DeepONet、PDENet)等主流模型,覆盖度全球第一; -提供了高阶API(一键环境配置,自动模型加载、极简训练微调等),开发者和用户开箱即用。 -MindSpore SciAI应用领域覆盖到了流体、电磁、声、热、固体、生物等众多学科,为广大开发者和用户提供了高效、易用的AI4Science通用计算平台。 - -.. raw:: html - - - -.. toctree:: - :maxdepth: 1 - :caption: 安装部署 - - installation - -.. toctree:: - :maxdepth: 1 - :caption: 启动指南 - - launch_with_scripts - launch_with_api - -.. toctree:: - :maxdepth: 1 - :caption: 模型列表 - - model_library - -.. toctree:: - :maxdepth: 1 - :caption: 教学示例 - - build_model_with_sciai - -.. toctree:: - :maxdepth: 1 - :caption: API参考 - - sciai.architecture - sciai.common - sciai.context - sciai.operators - sciai.utils - -.. toctree:: - :glob: - :maxdepth: 1 - :caption: RELEASE NOTES - - RELEASE diff --git a/docs/sciai/docs/source_zh_cn/installation.md b/docs/sciai/docs/source_zh_cn/installation.md deleted file mode 100644 index 46e76ba6ede1d523259d72c54d911eacd4d1a96d..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/installation.md +++ /dev/null @@ -1,66 +0,0 @@ -# MindSpore SciAI安装 - -[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_zh_cn/installation.md) -   - -## 确认系统环境信息 - -- 硬件平台为Ascend或GPU。 -- 参考[MindSpore安装指南](https://www.mindspore.cn/install),完成MindSpore的安装。 -- 其余依赖请参见[requirements.txt](https://gitee.com/mindspore/mindscience/blob/master/SciAI/requirements.txt)。 - -## 安装方式 - -可以采用pip安装或者源码编译安装两种方式。 - -### 方式一:pip安装 - -此方式不用克隆源码编译,而是自动下载安装MindSpore官方提供的whl包。 - -```bash -pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/gpu/{arch}/cuda-11.1/sciai-{version}-cp37-cp37m-linux_{arch}.whl -i https://pypi.tuna.tsinghua.edu.cn/simple -``` - -> - 在联网状态下,安装whl包时会自动下载SciAI安装包的依赖项(依赖项详情参见setup.py)。 -> - {version}表示SciAI版本号,例如下载0.1.0版本SciAI时,{version}应写为`0.1.0`。 -> - {arch}表示系统架构,例如使用的Linux系统是x86架构64位时,{arch}应写为`x86_64`。如果系统是ARM架构64位,则写为`aarch64`。 - -下表提供了各架构和Python版本对应的安装命令。 - -| 设备 | 架构 | Python | 安装命令 | -|--------|---------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| Ascend | x86_64 | Python=3.7 | `pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/gpu/x86_64/cuda-11.1/sciai-0.1.0-cp37-cp37m-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple` | -| | aarch64 | Python=3.7 | `pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/ascend/aarch64/sciai-0.1.0-cp37-cp37m-linux_aarch64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple` | -| GPU | x86_64 | Python=3.7 | `pip install https://ms-release.obs.cn-north-4.myhuaweicloud.com/2.2.0/MindScience/sciai/gpu/x86_64/cuda-11.1/sciai-0.1.0-cp37-cp37m-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple` | - -注意:如果您的conda或python env中已经安装了其他MindScience套件,如`MindElec`,`MindFlow`,`MindSponge`,请先卸载环境中的MindScience套件避免pip行为冲突。 - -### 方式二:源码安装 - -1. 克隆MindScience代码Git仓库。 - - ```bash - cd ~ - git clone https://gitee.com/mindspore/mindscience.git - ``` - -2. 使用脚本`build.sh`编译SciAI。 - - ```bash - cd mindscience/SciAI - bash build.sh -j8 - ``` - -3. 编译完成后,通过如下命令安装编译所得`.whl`包。 - - ```bash - bash install.sh - ``` - -### 验证安装 - -执行如下命令,如果没有报错`No module named 'sciai'`,则说明安装成功。 - -```bash -python -c 'import sciai' -``` diff --git a/docs/sciai/docs/source_zh_cn/launch_with_api.md b/docs/sciai/docs/source_zh_cn/launch_with_api.md deleted file mode 100644 index ebb42da0866cf29c00911642ae3e08342d2bcad1..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/launch_with_api.md +++ /dev/null @@ -1,56 +0,0 @@ -# 调用API启动模型 - -[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_zh_cn/launch_with_api.md)   - -MindSpore SciAI为用户提供了高阶API接口`AutoModel`。借助`AutoModel`,用户可以通过一行代码完成模型的实例化。 - -用户可以通过`AutoModel`的接口进行模型参数更新,并启动训练或评估。 - -## 使用AutoModel获取模型 - -用户可以使用`AutoModel.from_pretrained`接口获取已支持的网络模型。 - -这里使用Conservatice Physics-Informed Neural Networks (CPINNs) 作为教学案例。CPINNs模型相关代码请参考[链接](https://gitee.com/mindspore/mindscience/tree/master/SciAI/sciai/model/cpinns)。 - -更多关于该模型的信息,请参考[论文](https://www.sciencedirect.com/science/article/abs/pii/S0045782520302127)。 - -```python -from sciai.model import AutoModel - -# 获取cpinns网络模型 -model = AutoModel.from_pretrained("cpinns") -``` - -## 使用AutoModel训练、微调模型 - -用户可以使用`AutoModel.train`实现模型的训练,并且在执行训练之前, -使用`AutoModel.update_config`调整训练参数,或是加载`.ckpt`文件实现模型微调。 -接口`AutoModel.update_config`所接受的可选参数依赖于模型类型。 - -```python -from sciai.model import AutoModel - -# 获取cpinns网络模型 -model = AutoModel.from_pretrained("cpinns") -# (可选)加载参数ckpt文件,使用已有参数进行模型初始化 -model.update_config(load_ckpt=True, load_ckpt_path="./checkpoints/your_file.ckpt", epochs=500) -# 使用默认参数训练网络,生成的图片、数据与日志将保存至用户的执行目录中 -model.train() -``` - -## 使用AutoModel评估模型 - -用户可以使用`AutoModel.evaluate`评估训练结果。 - -该接口将默认加载SciAI模型库中提供的`.ckpt`文件用于评估,用户也可以调用`model.update_config`接口自定义加载的文件。 - -```python -from sciai.model import AutoModel - -# 获取cpinns网络模型 -model = AutoModel.from_pretrained("cpinns") -# (可选)自定义加载ckpt文件 -model.update_config(load_ckpt=True, load_ckpt_path="./checkpoints/your_file.ckpt") -# 评估网络模型 -model.evaluate() -``` diff --git a/docs/sciai/docs/source_zh_cn/launch_with_scripts.md b/docs/sciai/docs/source_zh_cn/launch_with_scripts.md deleted file mode 100644 index 18e14690515c0fd1ba4532b8b9ce79ce1d8640f0..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/launch_with_scripts.md +++ /dev/null @@ -1,64 +0,0 @@ -# 脚本启动模型 - -[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_zh_cn/launch_with_scripts.md)   - -MindSpore SciAI中的模型为用户提供了训练与评估的脚本文件。 - -通过模型的脚本文件,用户可以直接启动某个模型的训练与评估,并通过修改配置文件或是传入命令行参数的方式调整模型参数。[该目录](https://gitee.com/mindspore/mindscience/tree/master/SciAI/sciai/model)中包含了所有支持脚本启动的模型。 - -下面使用模型Conservative Physics-Informed Neural Networks(CPINNs)介绍使用脚本训练、评估模型的基本通用流程。CPINNs模型相关代码请参考[链接](https://gitee.com/mindspore/mindscience/tree/master/SciAI/sciai/model/cpinns)。 - -更多关于该模型的信息,请参考[论文](https://www.sciencedirect.com/science/article/abs/pii/S0045782520302127)。 - -## 仓库下载 - -使用如下命令直接克隆整个仓库,并初始化环境变量`PYTHONPATH`。 - -```bash -git clone https://gitee.com/mindspore/mindscience -source ./mindscience/SciAI/.env -``` - -克隆完成后,用户可以按照模型[README_CN.md](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/README_CN.md)(以模型CPINNs为例)中的`快速开始`章节,使用脚本进行训练与推理。 - -```bash -cd ./mindscience/SciAI/sciai/model/cpinns/ -``` - -## 训练、微调模型 - -用户可以使用训练脚本[train.py](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/train.py)进行网络模型训练。 - -```bash -python ./train.py [--parameters] -# expected output -... -step: 0, loss1: 2.1404986, loss2: 8.205103, loss3: 37.23588, loss4: 3.56359, interval: 50.85803508758545s, total: 50.85803508758545s -step: 10, loss1: 2.6560388, loss2: 3.869413, loss3: 9.323585, loss4: 2.1194165, interval: 5.159524917602539s, total: 56.01756000518799s -step: 20, loss1: 1.7885156, loss2: 4.470225, loss3: 3.3072894, loss4: 1.5674783, interval: 1.8615927696228027s, total: 57.87915277481079s -... -``` - -使用已有的`.ckpt`文件进行模型微调。 - -```bash -python ./train.py --load_ckpt true --load_ckpt_path {your_file}.ckpt [--parameters] -``` - -使用可选参数`[--parameters]`可以配置模型的训练过程,包括学习率、训练周期、数据读取保存路径等。 - -具体可配置的参数列表请参考[README_CN.md](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/README_CN.md)中`脚本参数`章节。 - -## 评估模型 - -用户可以使用脚本`eval.py`对已完成训练的网络模型进行评估。 - -```bash -python ./eval.py [--parameters] -# expected output -... -error_u: 0.024803562642018585 -Total time running eval: 20.625872135162354 seconds -``` - -使用可选参数`[--parameters]`可以配置模型的评估过程,包括数据读取保存路径、checkpoints文件加载路径等。 diff --git a/docs/sciai/docs/source_zh_cn/model_library.md b/docs/sciai/docs/source_zh_cn/model_library.md deleted file mode 100644 index cb5f53052cd3c3d6332ad66f89d9c1c2458e2c69..0000000000000000000000000000000000000000 --- a/docs/sciai/docs/source_zh_cn/model_library.md +++ /dev/null @@ -1,72 +0,0 @@ -# 网络模型库 - -[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.svg)](https://gitee.com/mindspore/docs/blob/master/docs/sciai/docs/source_zh_cn/model_library.md)   - -SciAI基础模型库提供了丰富的科学计算高频模型,下表中汇总了当前已实现的网络模型及其对应领域。 - -| 领域 | 模型 | MindSpore实现与网络参数 | Ascend | GPU | -|-------|--------------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:------:|:---:| -| 通用物理 | [auq_pinns](https://www.sciencedirect.com/science/article/pii/S0021999119303584) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/auq_pinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [cpinns](https://www.sciencedirect.com/science/article/abs/pii/S0045782520302127) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/cpinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [deep_hpms](https://www.jmlr.org/papers/volume19/18-046/18-046.pdf) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deep_hpms/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [deep_ritz](https://arxiv.org/abs/1710.00211) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deep_ritz/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [deepbsde](https://www.pnas.org/doi/10.1073/pnas.1718942115) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deepbsde/README.md#script-parameters) | | ✅ | -| 通用物理 | [deeponet](https://www.nature.com/articles/s42256-021-00302-5) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/deeponet/README_CN.md#脚本参数) | | ✅ | -| 通用物理 | [dgm](https://arxiv.org/abs/1708.07469) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/dgm/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [fbsnns](https://arxiv.org/abs/1804.07010) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/fbsnns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [fpinns](https://arxiv.org/abs/1811.08967) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/fpinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [gradient_pathologies_pinns](https://arxiv.org/abs/2001.04536) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/gradient_pathologies_pinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [hp_vpinns](https://arxiv.org/abs/2003.05385) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/hp_vpinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [laaf](https://doi.org/10.1016/j.jcp.2019.109136) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/laaf/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [mgnet](https://link.springer.com/article/10.1007/s11425-019-9547-2) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/mgnet/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [multiscale_pinns](https://www.sciencedirect.com/science/article/abs/pii/S0045782521002759) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/multiscale_pinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [pfnn](https://www.sciencedirect.com/science/article/abs/pii/S0021999120308597) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pfnn/README_CN.md#脚本说明) | | ✅ | -| 通用物理 | [phygeonet](https://www.sciencedirect.com/science/article/abs/pii/S0021999120308536) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/phygeonet/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [pi_deeponet](https://www.sciencedirect.com/science/article/abs/pii/S0021999122009184) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pi_deeponet/README_CN.md#脚本参数) | | ✅ | -| 通用物理 | [pinns](https://www.sciencedirect.com/science/article/abs/pii/S0021999118307125) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinns/README_CN.md#脚本参数) | | ✅ | -| 通用物理 | [pinns_ntk](https://www.sciencedirect.com/science/article/pii/S002199912100663X) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinns_ntk/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [ppinns](https://www.sciencedirect.com/science/article/abs/pii/S0045782520304357) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/ppinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 通用物理 | [xpinns](https://doi.org/10.4208/cicp.OA-2020-0164) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/xpinns/README_CN.md#脚本参数) | ✅ | ✅ | -| 哈密顿系统 | [sympnets](https://www.sciencedirect.com/science/article/pii/S0893608020303063) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/sympnets/README_CN.md#脚本参数) | ✅ | ✅ | -| 流体力学 | [hfm](https://www.science.org/doi/abs/10.1126/science.aaw4741) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/hfm/README_CN.md#脚本参数) | ✅ | ✅ | -| 流体力学 | [label_free_dnn_surrogate](https://www.sciencedirect.com/science/article/pii/S004578251930622X) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/label_free_dnn_surrogate/README_CN.md#脚本参数) | ✅ | ✅ | -| 流体力学 | [nsf_nets](https://www.sciencedirect.com/science/article/pii/S0021999120307257) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/nsf_nets/README_CN.md#脚本参数) | ✅ | ✅ | -| 流体力学 | [*burgers_fno](https://arxiv.org/abs/2010.08895) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/burgers/fno1d/FNO1D_CN.ipynb) | ✅ | ✅ | -| 流体力学 | [*burgers_kno](https://arxiv.org/abs/2301.10022) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/burgers/kno1d/KNO1D_CN.ipynb) | ✅ | ✅ | -| 流体力学 | [*navier_stokes_fno](https://arxiv.org/abs/2010.08895) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/navier_stokes/fno2d/FNO2D_CN.ipynb) | ✅ | ✅ | -| 流体力学 | [*navier_stokes_kno](https://arxiv.org/abs/2301.10022) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/navier_stokes/kno2d/KNO2D_CN.ipynb) | ✅ | ✅ | -| 流体力学 | [*navier_stokes_3d_fno](https://arxiv.org/abs/2010.08895) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_driven/navier_stokes/fno3d/FNO3D_CN.ipynb) | ✅ | ✅ | -| 流体力学 | [*pde_net](https://arxiv.org/abs/1710.09668) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_mechanism_fusion/pde_net/README_CN.md) | ✅ | ✅ | -| 流体力学 | [*percnn](https://www.nature.com/articles/s42256-023-00685-7) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindFlow/applications/data_mechanism_fusion/percnn/README_CN.md) | ✅ | ✅ | -| 弹性动力学 | [pinn_elastodynamics](https://arxiv.org/abs/2006.08472) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinn_elastodynamics/README_CN.md#脚本参数) | ✅ | ✅ | -| 热力学 | [pinn_heattransfer](https://arxiv.org/abs/1711.10561) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinn_heattransfer/README_CN.md#脚本参数) | ✅ | ✅ | -| 气象学 | [enso](https://doi.org/10.1038/s41586-019-1559-7) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/enso/README_CN.md#脚本参数) | ✅ | ✅ | -| 地质学 | [inversion_net](https://ieeexplore.ieee.org/abstract/document/8918045/) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/inversion_net/README_CN.md#脚本参数) | ✅ | ✅ | -| 地质学 | [pinn_helmholtz](https://academic.oup.com/gji/article-abstract/228/3/1750/6409132) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinn_helmholtz/README_CN.md#脚本参数) | ✅ | ✅ | -| 海洋物理 | [ocean_model](https://gmd.copernicus.org/articles/12/4729/2019/) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/ocean_model/README_CN.md#模型说明) | | ✅ | -| 海洋物理 | [pinns_swe](https://arxiv.org/abs/2104.00615) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/pinns_swe/README_CN.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [maxwell_net](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/SciAI/sciai/model/maxwell_net/README_CN.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*AD_FDTD_invert_f](https://www.mindspore.cn/mindelec/docs/zh-CN/r0.2/AD_FDTD.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/AD_FDTD/fdtd_forward/README_CN.md#脚本参数) | | ✅ | -| 电磁学 | [*AD_FDTD_microstrip_filter](https://www.mindspore.cn/mindelec/docs/zh-CN/r0.2/AD_FDTD.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/AD_FDTD/fdtd_forward/README_CN.md#脚本参数) | | ✅ | -| 电磁学 | [*AD_FDTD_inverse](https://www.mindspore.cn/mindelec/docs/zh-CN/r0.2/AD_FDTD.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/AD_FDTD/fdtd_inverse/README_CN.md#脚本参数) | | ✅ | -| 电磁学 | [*frequency_domain_maxwell](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/frequency_domain_maxwell/README_CN.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*frequency_domain_maxwell_3D_dielectric_slab](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/frequency_domain_maxwell_3D/dielectric_slab_3d/README.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*frequency_domain_maxwell_3D_waveguide_cavity](https://arxiv.org/abs/2107.06164) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/frequency_domain_maxwell_3D/waveguide_cavity_3d/README.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*meta_auto_decoder](https://arxiv.org/abs/2111.08823) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/incremental_learning/README_CN.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*pinn_fwi](https://agupubs.onlinelibrary.wiley.com/doi/abs/10.1029/2021JB023120) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/pinn_fwi/README.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*SED_ANN](https://gitee.com/mindspore/mindscience/tree/master/MindElec/examples/data_driven/sed_ann) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/data_driven/sed_ann/README_CN.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*time_domain_maxwell](https://www.ijcai.org/proceedings/2022/533) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/physics_driven/time_domain_maxwell/README_CN.md#脚本参数) | ✅ | ✅ | -| 电磁学 | [*metasurface_holograms](https://www.researching.cn/articles/OJ44d3746c3db8c1e1) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindElec/examples/metasurface/metasurface_holograms/README_CN.md#脚本参数) | ✅ | ✅ | -| 生物 | [*MEGA-Fold](https://arxiv.org/abs/2206.12240v1) | [link (推理)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) [link (训练)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | ✅ | ✅ | -| 生物 | [*MEGA-EvoGen](https://arxiv.org/abs/2208.09652) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | ✅ | ✅ | -| 生物 | [*MEGA-Assessment](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | [link (推理)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) [link (训练)](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/MEGAProtein.md) | ✅ | ✅ | -| 生物 | [*ColabDesign](https://www.biorxiv.org/content/10.1101/2021.11.10.468128.abstract) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ColabDesign.md) | ✅ | ✅ | -| 生物 | [*DeepFRI](https://www.nature.com/articles/s41467-021-23303-9) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/DeepFri.md) | ✅ | ✅ | -| 生物 | [*Multimer](https://www.biorxiv.org/content/10.1101/2021.10.04.463034v1) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/afmultimer.md) | ✅ | ✅ | -| 生物 | [*ProteinMPNN](https://www.science.org/doi/abs/10.1126/science.add2187) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ProteinMPNN.MD) | ✅ | ✅ | -| 生物 | [*UFold](https://doi.org/10.1093/nar/gkab1074) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/UFold.md) | ✅ | ✅ | -| 生物 | [*esm-if1](https://proceedings.mlr.press/v162/hsu22a.html) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ESM-IF1.md) | ✅ | ✅ | -| 生物 | [*esm2](https://www.biorxiv.org/content/10.1101/2022.07.20.500902v1.full.pdf) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/ESM-2.md) | ✅ | ✅ | -| 生物 | [*grover](https://proceedings.neurips.cc/paper/2020/file/94aef38441efa3380a3bed3faf1f9d5d-Paper.pdf) | [link](https://gitee.com/mindspore/mindscience/blob/master/MindSPONGE/applications/model_cards/GROVER.MD) | ✅ | ✅ | - -注: 带有“*”的网络模型为MindSpore与MindScience先前已发布网络。 \ No newline at end of file