From 5be32481b1e3f7449a0606109ef6eaba7a5c42fc Mon Sep 17 00:00:00 2001 From: Yifan Xiong Date: Thu, 28 Jan 2021 21:01:28 +0800 Subject: [PATCH] Setup: Init - Initialize setup.py and basic configs (#4) Initialize setup.py and basic configurations for this project. Major revisions: - initialize setup.py for Python package - add gitignore and dockerignore - add editorconfig for editors - configure yapf for auto formating - configure mypy for type hint - configure flake8 for lint, including quotes and docstrings - add pre-commit check for `git commit` - add spelling check in GitHub Actions - format existing files according to configured rules Example usage: # install dependencies $ python3 -m pip install -e .[dev,test] $ pre-commit install # format code automatically $ python3 setup.py format # lint code $ python3 setup.py lint # test code $ python3 setup.py test --- .dockerignore | 13 ++++ .editorconfig | 19 +++++ .flake8 | 6 ++ .gitattributes | 5 ++ .github/workflows/lint.yml | 21 +++++ .gitignore | 144 ++++++++++++++++++++++++++++++++++ .mypy.ini | 7 ++ .pre-commit-config.yaml | 33 ++++++++ .style.yapf | 4 + MANIFEST.in | 1 + README.md | 4 +- SECURITY.md | 4 +- SUPPORT.md | 50 ++++++------ setup.py | 153 +++++++++++++++++++++++++++++++++++++ superbench/__init__.py | 10 +++ test.dockerfile | 18 ++--- tests/test_example.py | 16 +++- 17 files changed, 468 insertions(+), 40 deletions(-) create mode 100644 .dockerignore create mode 100644 .editorconfig create mode 100644 .flake8 create mode 100644 .gitattributes create mode 100644 .github/workflows/lint.yml create mode 100644 .gitignore create mode 100644 .mypy.ini create mode 100644 .pre-commit-config.yaml create mode 100644 .style.yapf create mode 100644 MANIFEST.in create mode 100644 setup.py create mode 100644 superbench/__init__.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..5826788b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +# SuperBench +outputs +*.tar.gz + +# Python +__pycache__ +*.pyc +*.pyo +*.pyd + +# Git +.git +.github diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..f4f3fe4d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,19 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +charset = utf-8 +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true + +[*.py] +max_line_length = 79 + +[*.yaml] +indent_size = 2 + +[*.json] +indent_size = 2 +insert_final_newline = false diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..9db71b29 --- /dev/null +++ b/.flake8 @@ -0,0 +1,6 @@ +[flake8] +inline-quotes = single +multiline-quotes = double +docstring-quotes = double +docstring-convention = google +max-complexity = 10 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..07e9d3bf --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +* text=auto eol=lf + +# Source files +bin/* text +*.py text diff=python diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..bcfb13c3 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,21 @@ +name: Lint + +on: + pull_request: + branches: + - main + - dev + +jobs: + spelling: + name: Spelling check + runs-on: ubuntu-16.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install dependencies + run: | + curl -L https://git.io/misspell | sudo bash -s -- -b /bin + - name: Check spelling + run: | + misspell -error . diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..17679fe7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,144 @@ +# Created by https://github.com/github/gitignore + +# SuperBench tmp files +outputs/ +*.tar.gz + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ diff --git a/.mypy.ini b/.mypy.ini new file mode 100644 index 00000000..a225426a --- /dev/null +++ b/.mypy.ini @@ -0,0 +1,7 @@ +[mypy] +ignore_missing_imports = True +scripts_are_modules = True + +[superbench] +warn_return_any = True +disallow_untyped_defs = True diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..43f2be1a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,33 @@ +# Run `pre-commit install` to install +# See https://pre-commit.com for more information + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: check-added-large-files + - id: check-docstring-first + - id: check-json + - id: check-yaml + - id: detect-private-key + - id: double-quote-string-fixer + - id: end-of-file-fixer + - id: trailing-whitespace + - repo: https://github.com/pre-commit/mirrors-yapf + rev: v0.30.0 + hooks: + - id: yapf + name: Format code using yapf + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.800 + hooks: + - id: mypy + name: Check type hint using mypy + - repo: https://gitlab.com/pycqa/flake8 + rev: 3.8.4 + hooks: + - id: flake8 + name: Lint code using flake8 + additional_dependencies: + - flake8-quotes + - flake8-docstrings diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 00000000..04b172bc --- /dev/null +++ b/.style.yapf @@ -0,0 +1,4 @@ +[style] +based_on_style = pep8 +spaces_before_comment = 4 +blank_line_before_module_docstring = True diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 00000000..72ca36c0 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include LICENSE README.md diff --git a/README.md b/README.md index 5cd7cecf..7f66d767 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additio ## Trademarks -This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft -trademarks or logos is subject to and must follow +This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft +trademarks or logos is subject to and must follow [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies. diff --git a/SECURITY.md b/SECURITY.md index f7b89984..a050f362 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -14,7 +14,7 @@ Instead, please report them to the Microsoft Security Response Center (MSRC) at If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc). -You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc). +You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc). Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: @@ -38,4 +38,4 @@ We prefer all communications to be in English. Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd). - \ No newline at end of file + diff --git a/SUPPORT.md b/SUPPORT.md index dc72f0e5..ecbe64c0 100644 --- a/SUPPORT.md +++ b/SUPPORT.md @@ -1,25 +1,25 @@ -# TODO: The maintainer of this repo has not yet edited this file - -**REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project? - -- **No CSS support:** Fill out this template with information about how to file issues and get help. -- **Yes CSS support:** Fill out an intake form at [aka.ms/spot](https://aka.ms/spot). CSS will work with/help you to determine next steps. More details also available at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). -- **Not sure?** Fill out a SPOT intake as though the answer were "Yes". CSS will help you decide. - -*Then remove this first heading from this SUPPORT.MD file before publishing your repo.* - -# Support - -## How to file issues and get help - -This project uses GitHub Issues to track bugs and feature requests. Please search the existing -issues before filing new issues to avoid duplicates. For new issues, file your bug or -feature request as a new Issue. - -For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE -FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER -CHANNEL. WHERE WILL YOU HELP PEOPLE?**. - -## Microsoft Support Policy - -Support for this **PROJECT or PRODUCT** is limited to the resources listed above. +# TODO: The maintainer of this repo has not yet edited this file + +**REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project? + +- **No CSS support:** Fill out this template with information about how to file issues and get help. +- **Yes CSS support:** Fill out an intake form at [aka.ms/spot](https://aka.ms/spot). CSS will work with/help you to determine next steps. More details also available at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). +- **Not sure?** Fill out a SPOT intake as though the answer were "Yes". CSS will help you decide. + +*Then remove this first heading from this SUPPORT.MD file before publishing your repo.* + +# Support + +## How to file issues and get help + +This project uses GitHub Issues to track bugs and feature requests. Please search the existing +issues before filing new issues to avoid duplicates. For new issues, file your bug or +feature request as a new Issue. + +For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE +FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER +CHANNEL. WHERE WILL YOU HELP PEOPLE?**. + +## Microsoft Support Policy + +Support for this **PROJECT or PRODUCT** is limited to the resources listed above. diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..32529d3d --- /dev/null +++ b/setup.py @@ -0,0 +1,153 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +"""The setuptools based setup module. + +Reference: + https://packaging.python.org/guides/distributing-packages-using-setuptools/ +""" + +import os +import sys +import pathlib +from typing import List, Tuple + +from setuptools import setup, find_packages, Command + +import superbench + +here = pathlib.Path(__file__).parent.resolve() +long_description = (here / 'README.md').read_text(encoding='utf-8') + + +class Formatter(Command): + """Cmdclass for `python setup.py format`. + + Args: + Command (distutils.cmd.Command): + Abstract base class for defining command classes. + """ + + description = 'format the code using yapf' + user_options: List[Tuple[str, str, str]] = [] + + def initialize_options(self): + """Set default values for options that this command supports.""" + pass + + def finalize_options(self): + """Set final values for options that this command supports.""" + pass + + def run(self): + """Fromat the code using yapf.""" + errno = os.system( + 'python3 -m yapf --in-place --recursive --exclude .git .') + sys.exit(errno) + + +class Linter(Command): + """Cmdclass for `python setup.py lint`. + + Args: + Command (distutils.cmd.Command): + Abstract base class for defining command classes. + """ + + description = 'lint the code using flake8' + user_options: List[Tuple[str, str, str]] = [] + + def initialize_options(self): + """Set default values for options that this command supports.""" + pass + + def finalize_options(self): + """Set final values for options that this command supports.""" + pass + + def run(self): + """Lint the code with yapf, mypy, and flake8.""" + errno = os.system(' && '.join([ + 'python3 -m yapf --diff --recursive --exclude .git .', + 'python3 -m mypy .', + 'python3 -m flake8', + ])) + sys.exit(errno) + + +class Tester(Command): + """Cmdclass for `python setup.py test`. + + Args: + Command (distutils.cmd.Command): + Abstract base class for defining command classes. + """ + + description = 'test the code using pytest' + user_options: List[Tuple[str, str, str]] = [] + + def initialize_options(self): + """Set default values for options that this command supports.""" + pass + + def finalize_options(self): + """Set final values for options that this command supports.""" + pass + + def run(self): + """Run pytest.""" + errno = os.system('python3 -m pytest -v') + sys.exit(errno) + + +setup( + name='superbench', + version=superbench.__version__, + description='Provide hardware and software benchmarks for AI systems.', + long_description=long_description, + long_description_content_type='text/markdown', + url='https://github.com/microsoft/superbenchmark', + author=superbench.__author__, + author_email='superbench@microsoft.com', + license='MIT', + classifiers=[ + 'Development Status :: 2 - Pre-Alpha', + 'Environment :: GPU', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: MIT License', + 'Operating System :: POSIX', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3 :: Only', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', + 'Topic :: System :: Benchmark', + 'Topic :: System :: Clustering', + 'Topic :: System :: Hardware', + ], + keywords='benchmark, AI systems', + packages=find_packages(exclude=['tests']), + python_requires='>=3.6, <4', + install_requires=[], + extras_require={ + 'dev': ['pre-commit'], + 'test': [ + 'yapf', + 'mypy', + 'flake8', + 'flake8-quotes', + 'flake8-docstrings', + 'pytest', + ], + }, + package_data={}, + entry_points={ + 'console_scripts': [], + }, + cmdclass={ + 'format': Formatter, + 'lint': Linter, + 'test': Tester, + }, +) diff --git a/superbench/__init__.py b/superbench/__init__.py new file mode 100644 index 00000000..bdeaf861 --- /dev/null +++ b/superbench/__init__.py @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +"""SuperBench Python module. + +Provide hardware and software benchmarks for AI systems. +""" + +__version__ = '0.0.0' +__author__ = 'Microsoft' diff --git a/test.dockerfile b/test.dockerfile index 6b12fd3b..99da3b78 100644 --- a/test.dockerfile +++ b/test.dockerfile @@ -12,16 +12,16 @@ RUN apt-get update && apt-get install -y \ RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1 && \ update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 10 -# Upgrade pip and instll flake8 and pytest -RUN python3 -m pip install --upgrade pip && \ - python3 -m pip install flake8 pytest - # Create workspace WORKDIR /superbench COPY . /superbench -# Check code format using flake8 -RUN python3 -m flake8 - -# Install library and run pytest -RUN python3 -m pytest -v +# Upgrade pip and install dependencies +RUN python3 -m pip install --upgrade pip setuptools && \ + python3 -m pip install .[test] + +# Lint code +RUN python3 setup.py lint + +# Test code +RUN python3 setup.py test diff --git a/tests/test_example.py b/tests/test_example.py index dbd432ba..48c6a763 100644 --- a/tests/test_example.py +++ b/tests/test_example.py @@ -1,12 +1,24 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT license. -# content of test_sample.py -# get it from https://docs.pytest.org/en/stable/ +"""Test example. + +Get it from https://docs.pytest.org/en/stable/. +""" + def inc(x): + """Increase an integer. + + Args: + x (int): Input value. + + Returns: + int: Increased value. + """ return x + 1 def test_answer(): + """Test inc function.""" assert inc(3) == 4