Skip to content

Commit

Permalink
Add docs for NelderMeadSampler (#397)
Browse files Browse the repository at this point in the history
* uploaded installation.md

* fixed descriptions wrt Linux and ABCI.

* fixed links.

* added descriptions wrt macOS.

* fixed installation.md

* mkdir installation
mv installation.md installation

* added auto_formatting_guide.md to contribution_guide directory.

* deleted black space

* added Makefile

* added installation_en.md

* fixed installation_en.md

* added auto_formatting_guide_en.md

* fixed error wrt ruff

* fixed errors wrt ruff

* Add docs for nm.(WIP)

* Add docs/source/hpo.

* Add Sphinx documents.

* Delete docs/source/api_reference.

* Add docs/source/api_reference in .gitignore.

* Add publish_pages.yaml.

* Delete japanese in docstring.

* Change html_theme to pydata.

* Delete japanese.

* Remove files.

* Split API reference.

* Add logo.

* Fix for ruff and mypy.

* Update hpo.md

* Fix typo.

* Add --maxdepth option.

* Delete html.

* Remove sphinx_rtd_theme.

* Resize logo.

* Fixed to upper case.

* Change document structure.

* Add contribution_guide(WIP).

* Add api_reference/index.rst.

* Add user_guide(WIP)

* Remove unnecessary sentences.

* Remove docs/source/api_reference/*.md.

* Integrate contribution_guide.

* Split user_guide.

* Adjust html_sidebars.

* Fix for ruff.

* Update installation.md

* Update index.rst

* Update and rename docs/source/installation/installation.md to docs/source/getting_started.md

* Update aiaccel/hpo/algorithms/nelder_mead_algorithm.py

Co-authored-by: Yoshiaki Bando <yoshipon@users.noreply.github.com>

* Add logo_aiaccel_padding.png.

* Integrate neldermeadsampler_en.md.

* Delete unnecessary files.

* Add sphinx.ext.doctest.

* Remove cp index.html .

---------

Co-authored-by: 3bisuoka <ebisuoka-ryoya@aist.go.jp>
Co-authored-by: Yoshiaki Bando <yoshipon@users.noreply.github.com>
  • Loading branch information
3 people authored Dec 6, 2024
1 parent 5561523 commit b6d43f6
Show file tree
Hide file tree
Showing 16 changed files with 302 additions and 67 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/publish_pages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Publish on GitHub Pages

on:
[push, pull_request, workflow_dispatch]

permissions:
contents: write

jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install .[dev,github-actions,test]
- name: Sphinx build
run: |
sphinx-apidoc --maxdepth 2 -f -o ./docs/source/api_reference/ ./aiaccel/
cd docs
make html
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/feature/main' }}
with:
publish_branch: gh-pages
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: docs/build/html
force_orphan: true
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ work_aiaccel/

# document
docs/build/html
docs/source/api_reference/*.rst
!docs/source/api_reference/index.rst

# IntelliJ
.idea
Expand Down
29 changes: 2 additions & 27 deletions aiaccel/hpo/algorithms/nelder_mead_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,48 +33,31 @@ class NelderMeadAlgorism:
Uses a queue to receive results and advance the NelderMead algorithm.
Return parameters within the normalization range by referring only to the number of dimensions.
NelderMead アルゴリズムを管理するクラス
queue を用いて結果を受け取り、NelderMead のアルゴリズムを進める
次元数のみを参照して、正規化範囲でパラメータを返す
Args:
dimensions: int | None = None
The number of dimensions in the search space.
探索範囲の次元数
coeff: NelderMeadCoefficient | None = None
Parameters used in NelderMead.
NelderMead で用いられるパラメータ
rng: np.random.RandomState | None = None
RandomState used for calculating initial points.
初期点計算に用いられる RandomState
block: bool = False
Sets whether to block the queue used internally.
内部で用いられる queue を block するかどうかを設定する
timeout: int | None = None
Time to block the queue.
queue を block する時間
Attributes:
vertices: list[npt.NDArray[np.float64]]
List of simplex parameters.
simplex のパラメータのリスト
values: list[float]
List of simplex calculation results.
simplex の計算結果のリスト
generator: iterator
Generator for NelderMead parameters.
neldermead のパラメータのジェネレータ
lock: threading.Lock
threading.Lock variable used for thread-safe processing.
スレッドセーフ処理に用いる threading.Lock 変数
results: queue.Queue[tuple[npt.NDArray[np.float64], float, bool]]
Queue to receive tuples of parameters, calculation results,
and a boolean indicating whether the parameters were output by NelderMead.
外部からパラメータ・計算結果・nelder mead から出力されたパラメータか否かを示す bool 変数
のタプルを受け取る queue
simplex_size: int
Number of vertices in the simplex.
simplex の頂点の個数
"""

vertices: list[npt.NDArray[np.float64]]
Expand Down Expand Up @@ -107,14 +90,9 @@ def get_vertex(self, dimensions: int | None = None) -> npt.NDArray[np.float64]:
Thread-safe due to parallel processing requirements.
nelder mead の次のパラメータを返すメソッド
並列処理の都合でスレッドセーフとなっている
Returns:
npt.NDArray[np.float64]:
The next parameters for NelderMead.
nelder mead の次のパラメータ
"""

if dimensions is not None:
Expand Down Expand Up @@ -151,14 +129,11 @@ def put_value(
) -> None:
"""Method to pass a pair of parameters and results to NelderMead
nelder mead にパラメータと結果の組を渡すメソッド
Args:
vertex: npt.NDArray[np.float64]: Parameters パラメータ
value: float: Calculation result 計算結果
vertex: npt.NDArray[np.float64]: Parameters
value: float: Calculation result
enqueue: bool = False:
Boolean indicating whether the parameters were output by NelderMead.
nelder mead から出力されたパラメータか否かを示す bool 変数
"""

if self.dimensions is None:
Expand Down
56 changes: 16 additions & 40 deletions aiaccel/hpo/optuna/samplers/nelder_mead_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,48 @@ class NelderMeadSampler(optuna.samplers.BaseSampler):
The enqueued parameters are calculated in parallel
with the parameters determined by NelderMead if parallelisation is enabled.
NelderMead アルゴリズムを用いた Sampler
Example:
パラメータ数-1個の初期点計算と shrink 時のみ並列化可能で、
それ以外は基本的には直列計算になる(optuna.optimize(n_jobs=2)等で設定しても、前述の時以外は直列で計算する)
並列化を有効にする場合は、引数 block = True にする必要がある。
An example of a single-objective optimization is as follows:
optuna.enqueue_trial() 利用時は、NelderMeadSampler が決定するパラメータとは個別に計算され、
良い結果が出れば NelderMead に取り込まれる。(Simplexの再構成を行う)
また、optuna.enqueue_trial() で決定されたパラメータは、
並列化が有効であれば NelderMead の決定するパラメータと並列で計算される。
.. testcode::
import optuna
from aiaccel.hpo.optuna.samplers.nelder_mead_sampler import NelderMeadSampler
def objective(trial):
x = trial.suggest_float("x", -10, 10)
return x**2
search_space = {"x": {"low": -10, "high": 10}}
sampler = NelderMeadSampler(search_space=search_space, seed=42)
study.optimize(objective, n_trials=10)
Args:
search_space: dict[str, tuple[float, float]]
Parameter names and corresponding lower and upper limits.
Must be set separately from suggest_uniform
(as the parameters must be determined at the time of before_trial).
パラメータ名と対応した lower, upper
suggest_uniform 等とは個別に設定する必要がある(before_trial 時点でパラメータを決定する必要があるため)
seed: int | None = None
Random seed used for initial point calculation.
初期点計算をランダムで決定する際に利用されるシード値
rng: np.random.RandomState | None = None
RandomState used for initial point calculation.
If specified with seed, rng takes precedence.
初期点計算に用いられる RandomState
seed と同時に指定された場合、 rng が優先される
coeff: NelderMeadCoefficient | None = None
Parameters used in NelderMead Algorism.
NelderMead で用いられるパラメータ
block: bool = False
Indicates whether the queue used internally is blocked or not.
If parallelisation by optuna.optimize is enabled, it must be set with block = True
内部で用いられる queue を block するかどうかを設定する
optuna.optimize による並列化を有効にする場合は、 block = Trueで設定する必要がある
sub_sampler: optuna.samplers.BaseSampler | None = None
Sampler to output parameters when NelderMead cannot output parameters.
Mainly intended for use on free computation nodes in parallel.
If the sub_sampler function is enabled, it must be set with block = False.
NelderMead がパラメータを出力出来ない時に、代わりにパラメータを出力する Sampler
主に並列化時に空いている計算ノードで利用することを想定している
sub_sampler 機能を有効にする場合は、 block = False で設定する必要がある
Attributes:
nm: NelderMeadAlgorism
Instance of a class that manages the NelderMead algorithm.
NelderMead のアルゴリズムを管理するクラスのインスタンス
"""

Expand Down Expand Up @@ -171,14 +167,6 @@ def before_trial(self, study: Study, trial: FrozenTrial) -> None:
If sub_sampler is specified, sub_sampler.before_trial() is executed,
and trial.user_attr["sub_trial"] is set to True.
trial の前処理
ここで NelderMead のパラメータを決定する
trial.user_attr["params"] に決定したパラメータが格納される
NelderMead のパラメータが出力出来ないかつ、sub_sampler = None の場合は NelderMeadEmptyError を raise する
sub_sampler が指定されている場合は、 sub_sampler.before_trial() を実行し、
trial.user_attr["sub_trial"] = True とする
Args:
study: Study
Target study object.
Expand Down Expand Up @@ -223,13 +211,6 @@ def sample_independent(
If trial.user_attr["sub_trial"] = True,
execute sub_sampler.sample_independent() and return its parameters.
パラメータを sample する
NelderMeadSampler の場合は before_trial でパラメータを決定済みなので、
trial.user_attr["params"] の対応したパラメータを返り値とする
trial.user_attr["sub_trial"] = True の場合は、
sub_sampler.sample_independent() を実行し、そのパラメータを返り値とする
Args:
study: Study
Target study object.
Expand Down Expand Up @@ -299,11 +280,6 @@ def after_trial(
Pass the parameter-result pairs to the NelderMead algorithm.
If trial.user_attr["sub_trial"] = True, execute sub_sampler.after_trial().
trial の後処理
パラメータ、計算結果の組を NelderMead アルゴリズムに渡す
trial.user_attr["sub_trial"] = True の場合は、 sub_sampler.after_trial() を実行する
Args:
study: Study
Target study object.
Expand Down
14 changes: 14 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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
BUILDDIR = build

# 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)
Binary file added docs/image/logo_aiaccel_padding.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=source
set BUILDDIR=build

if "%1" == "" goto help

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
19 changes: 19 additions & 0 deletions docs/source/api_reference/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

*************
API Reference
*************

* :ref:`genindex`
* :ref:`modindex`

----


.. toctree::
:maxdepth: 2

aiaccel.app
aiaccel.hpo
aiaccel.job
aiaccel.torch
aiaccel.utils
88 changes: 88 additions & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# 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.
#
from pathlib import Path
import sys

root_path = Path(__file__).parent.parent.parent
sys.path.insert(0, str(root_path.absolute()))

# -- Project information -----------------------------------------------------

project = "aiaccel"
project_copyright = "2024, AIST"
author = "AIST"

# The full version, including alpha/beta/rc tags
release = "2.0.0"
html_logo = f"{root_path}/docs/image/logo_aiaccel_padding.png"

# -- 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.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.githubpages",
"sphinx.ext.napoleon",
"sphinx.ext.todo",
"sphinx.ext.viewcode",
"sphinx_fontawesome",
"myst_parser",
"pydata_sphinx_theme",
"sphinx.ext.doctest",
]

# 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.
exclude_patterns = [
"_build",
"Thumbs.db",
".DS_Store",
]

# Auto-generated header anchors
# The MyST Parser can automatically generate label “slugs” for header anchors
# so that you can reference them from markdown links. For example, you can use
# header bookmark links, locally; [](#header-anchor), or cross-file
# [](path/to/file.md#header-anchor). To achieve this, use the
# myst_heading_anchors = DEPTH configuration option, where DEPTH is the depth
# of header levels for which you wish to generate links.
# (commentout) myst_heading_anchors = 3

# -- 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 = "pydata_sphinx_theme"
html_show_sourcelink = False
html_show_sphinx = False
# (commentout) html_static_path = ["_static"]
html_sidebars = {"**": ["search-field", "sidebar-nav-bs", "sourcelink.html"]}
source_suffix = {".rst": "restructuredtext", ".txt": "markdown", ".md": "markdown"}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
# (commentout) html_static_path = ['_static']

language = "en"

# -- Extension configuration -------------------------------------------------
# (commentout) todo_include_todos = True
# (commentout) autoclass_content = "both"
3 changes: 3 additions & 0 deletions docs/source/contribution_guide/contribution_guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contribution Guide

(WIP)
Loading

0 comments on commit b6d43f6

Please sign in to comment.