Skip to content

Commit

Permalink
Examples: turn examples folder into brevitas_examples package
Browse files Browse the repository at this point in the history
  • Loading branch information
volcacius authored Apr 8, 2020
1 parent 9b57b61 commit 2425a9e
Show file tree
Hide file tree
Showing 76 changed files with 616 additions and 229 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/base.yml.template
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ jobs:
mkdir $CONDA_PKGS_DIRS
mkdir $PYTORCH_PKG_CACHE_PATH

- name: Install libsndfile on Ubuntu
shell: bash
run: sudo apt-get install -y libsndfile-dev
if: startsWith(runner.os, 'Linux') == true

- name: Setup Conda
uses: s-weigand/setup-conda@v1

Expand Down
21 changes: 19 additions & 2 deletions .github/workflows/develop_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@ jobs:
pytorch_version: ['1.1.0', '1.2.0', '1.3.0', '1.3.1', '1.4.0']
platform: ['windows-latest', 'ubuntu-latest', 'macos-latest']


exclude:
- platform: 'windows-latest'
conda_python_version: '3.6'

- platform: 'macos-latest'
pytorch_version: '1.1.0'

- pytorch_version: '1.1.0'
conda_python_version: '3.8'

- pytorch_version: '1.2.0'
conda_python_version: '3.8'

- pytorch_version: '1.3.0'
conda_python_version: '3.8'

- pytorch_version: '1.3.1'
conda_python_version: '3.8'



steps:

- name: Checkout repo
Expand Down Expand Up @@ -72,6 +79,11 @@ jobs:
mkdir $CONDA_PKGS_DIRS
mkdir $PYTORCH_PKG_CACHE_PATH
- name: Install libsndfile on Ubuntu
shell: bash
run: sudo apt-get install -y libsndfile-dev
if: startsWith(runner.os, 'Linux') == true

- name: Setup Conda
uses: s-weigand/setup-conda@v1

Expand Down Expand Up @@ -115,9 +127,14 @@ jobs:
rm -R ${{ steps.cache-dirs.outputs.pytorch_pkg_cache_path }}/pytorch*
if: steps.pytorch-cache.outputs.cache-hit == 'true'

- name: Run Nox session for testing develop install and imports
- name: Run Nox session for testing brevitas develop install and imports
shell: bash
run: nox --verbose --session tests_install_develop-${{ matrix.conda_python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)
run: nox -v -s tests_brevitas_install_dev-${{ matrix.conda_python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)

- name: Run Nox session for testing brevitas_examples develop install and imports
shell: bash
run: nox -v -s tests_brevitas_examples_install_dev-${{ matrix.conda_python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)



- name: Remove tarballs before caching Pytorch deps pkgs
Expand Down
29 changes: 19 additions & 10 deletions .github/workflows/gen_github_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@

PYTEST_MATRIX_EXTRA = od([('jit_status', list(JIT_STATUSES))])

PYTEST_STEP_LIST = [od([
('name', 'Run Nox session for pytest'),
('shell', 'bash'),
('run', 'nox --verbose --session tests_cpu-${{ matrix.conda_python_version }}\(${{ matrix.jit_status }}\,\ pytorch_${{ matrix.pytorch_version }}\)')])]

TEST_INSTALL_DEVELOP_STEP_LIST = [od([
('name', 'Run Nox session for testing develop install and imports'),
('shell', 'bash'),
('run', 'nox --verbose --session tests_install_develop-${{ matrix.conda_python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)')])]
PYTEST_STEP_LIST = [
od([
('name', 'Run Nox session for pytest'),
('shell', 'bash'),
('run', 'nox -v -s tests_brevitas_cpu-${{ matrix.conda_python_version }}\(${{ matrix.jit_status }}\,\ pytorch_${{ matrix.pytorch_version }}\)')
])]

TEST_INSTALL_DEV_STEP_LIST = [
od([
('name', 'Run Nox session for testing brevitas develop install and imports'),
('shell', 'bash'),
('run', 'nox -v -s tests_brevitas_install_dev-${{ matrix.conda_python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)')]),
od([
('name', 'Run Nox session for testing brevitas_examples develop install and imports'),
('shell', 'bash'),
('run', 'nox -v -s tests_brevitas_examples_install_dev-${{ matrix.conda_python_version }}\(\pytorch_${{ matrix.pytorch_version }}\)')
])]


# whitespaces to indent generated portions of output yaml
Expand Down Expand Up @@ -90,6 +98,7 @@ def dict_str(d, quote_val, indent_first):
repr += f"{name}: {val}\n"
if indent_first:
repr = indent(repr, RELATIVE_INDENT*' ', predicate=lambda line: not first_line_prefix in line)
repr += '\n'
return repr

def gen_yaml(self, output_path):
Expand Down Expand Up @@ -122,7 +131,7 @@ def gen_test_develop_install_yml():
'Test develop install',
EXCLUDE_LIST,
MATRIX,
TEST_INSTALL_DEVELOP_STEP_LIST)
TEST_INSTALL_DEV_STEP_LIST)
test_develop_install.gen_yaml(DEVELOP_INSTALL_YML)


Expand Down
16 changes: 15 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,31 @@ jobs:
platform: ['windows-latest', 'ubuntu-latest', 'macos-latest']
jit_status: ['jit_enabled', 'jit_disabled']


exclude:
- platform: 'windows-latest'
conda_python_version: '3.6'

- platform: 'macos-latest'
pytorch_version: '1.1.0'

- pytorch_version: '1.1.0'
conda_python_version: '3.8'

- pytorch_version: '1.2.0'
conda_python_version: '3.8'

- pytorch_version: '1.3.0'
conda_python_version: '3.8'

- pytorch_version: '1.3.1'
conda_python_version: '3.8'

- pytorch_version: '1.1.0'
jit_status: 'jit_disabled'



steps:

- name: Checkout repo
Expand Down Expand Up @@ -75,6 +83,11 @@ jobs:
mkdir $CONDA_PKGS_DIRS
mkdir $PYTORCH_PKG_CACHE_PATH
- name: Install libsndfile on Ubuntu
shell: bash
run: sudo apt-get install -y libsndfile-dev
if: startsWith(runner.os, 'Linux') == true

- name: Setup Conda
uses: s-weigand/setup-conda@v1

Expand Down Expand Up @@ -120,7 +133,8 @@ jobs:

- name: Run Nox session for pytest
shell: bash
run: nox --verbose --session tests_cpu-${{ matrix.conda_python_version }}\(${{ matrix.jit_status }}\,\ pytorch_${{ matrix.pytorch_version }}\)
run: nox -v -s tests_brevitas_cpu-${{ matrix.conda_python_version }}\(${{ matrix.jit_status }}\,\ pytorch_${{ matrix.pytorch_version }}\)



- name: Remove tarballs before caching Pytorch deps pkgs
Expand Down
Empty file added brevitas_examples/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ Below in the table is a list of example pretrained models made available for ref
To evaluate a pretrained quantized model on ImageNet:

- Make sure you have Brevitas installed and the ImageNet dataset in a Pytorch friendly format (following this [script](https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh)).
- Pass the corresponding cfg .ini file as an input to the evaluation script. The required checkpoint will be downloaded automatically.
- Pass the name of the model as an input to the evaluation script. The required checkpoint will be downloaded automatically.

For example, for *quant_mobilenet_v1_4b* evaluated on GPU 0:

```
python imagenet_val.py --imagenet-dir /path/to/imagenet --model-cfg ./cfg/quant_mobilenet_v1_4b.ini --gpu 0
brevitas_imagenet_val --imagenet-dir /path/to/imagenet --model quant_mobilenet_v1_4b --gpu 0 --pretrained
```

## MobileNet V1
Expand Down
1 change: 1 addition & 0 deletions brevitas_examples/imagenet_classification/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .models import *
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@
import torchvision.transforms as transforms
import torchvision.datasets as datasets

from models import *
from .models import model_with_cfg

SEED = 123456

models = {'quant_mobilenet_v1': quant_mobilenet_v1,
'quant_proxylessnas_mobile14': quant_proxylessnas_mobile14}


parser = argparse.ArgumentParser(description='PyTorch ImageNet Validation')
parser.add_argument('--imagenet-dir', help='path to folder containing Imagenet val folder')
parser.add_argument('--model-cfg', type=str, help='Path to pretrained model .ini configuration file')
parser.add_argument('--model', type=str, default='quant_mobilenet_v1_4b', help='Name of the model')
parser.add_argument('--pretrained', action='store_true', help='Load pretrained checkpoint')
parser.add_argument('--workers', default=4, type=int, help='number of data loading workers')
parser.add_argument('--batch-size', default=256, type=int, help='Minibatch size')
parser.add_argument('--gpu', default=None, type=int, help='GPU id to use.')
Expand All @@ -33,28 +31,13 @@ def main():
random.seed(SEED)
torch.manual_seed(SEED)

assert os.path.exists(args.model_cfg)
cfg = configparser.ConfigParser()
cfg.read(args.model_cfg)
arch = cfg.get('MODEL', 'ARCH')

model = models[arch](cfg)
model, cfg = model_with_cfg(args.model, args.pretrained)

if args.gpu is not None:
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
cudnn.benchmark = True

pretrained_url = cfg.get('MODEL', 'PRETRAINED_URL')
print("=> Loading checkpoint from:'{}'".format(pretrained_url))
if args.gpu is None:
checkpoint = torch.hub.load_state_dict_from_url(pretrained_url)
else:
# Map model to be loaded to specified single gpu.
loc = 'cuda:{}'.format(args.gpu)
checkpoint = torch.hub.load_state_dict_from_url(pretrained_url, map_location=loc)
model.load_state_dict(checkpoint, strict=True)

valdir = os.path.join(args.imagenet_dir, 'val')
mean = [float(cfg.get('PREPROCESS', 'MEAN_0')), float(cfg.get('PREPROCESS', 'MEAN_1')),
float(cfg.get('PREPROCESS', 'MEAN_2'))]
Expand Down
48 changes: 48 additions & 0 deletions brevitas_examples/imagenet_classification/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os
from configparser import ConfigParser

from torch import hub

from .mobilenetv1 import *
from .vgg import *
from .proxylessnas import *

model_impl = {
'quant_mobilenet_v1': quant_mobilenet_v1,
'quant_proxylessnas_mobile14': quant_proxylessnas_mobile14
}


def model_with_cfg(name, pretrained):
cfg = ConfigParser()
current_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(current_dir, '..', 'cfg', name + '.ini')
assert os.path.exists(config_path)
cfg.read(config_path)
arch = cfg.get('MODEL', 'ARCH')
model = model_impl[arch](cfg)
if pretrained:
checkpoint = cfg.get('MODEL', 'PRETRAINED_URL')
state_dict = hub.load_state_dict_from_url(checkpoint, progress=True, map_location='cpu')
model.load_state_dict(state_dict, strict=True)
return model, cfg


def quant_mobilenet_v1_4b(pretrained=True):
model, _ = model_with_cfg('quant_mobilenet_v1_4b', pretrained)
return model


def quant_proxylessnas_mobile14_4b(pretrained=True):
model, _ = model_with_cfg('quant_proxylessnas_mobile14_4b', pretrained)
return model


def quant_proxylessnas_mobile14_4b5b(pretrained=True):
model, _ = model_with_cfg('quant_proxylessnas_mobile14_4b5b', pretrained)
return model


def quant_proxylessnas_mobile14_hadamard_4b(pretrained=True):
model, _ = model_with_cfg('quant_proxylessnas_mobile14_hadamard_4b', pretrained)
return model
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from brevitas.core.scaling import ScalingImplType
from brevitas.core.stats import StatsOp


QUANT_TYPE = QuantType.INT
SCALING_MIN_VAL = 2e-16

Expand Down Expand Up @@ -151,3 +152,6 @@ def make_hadamard_classifier(in_channels,
return qnn.HadamardClassifier(in_channels=in_channels,
out_channels=out_channels,
fixed_scale=fixed_scale)



Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from brevitas.quant_tensor import pack_quant_tensor

from .common import make_quant_conv2d, make_quant_linear, make_quant_relu, make_quant_avg_pool
from .common import *


FIRST_LAYER_BIT_WIDTH = 8
Expand Down Expand Up @@ -170,3 +170,6 @@ def quant_mobilenet_v1(cfg):
first_stage_stride=first_stage_stride,
bit_width=bit_width)
return net



Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ It is highly recommended to setup a virtual environment.

Download and pre-process the LibriSpeech dataset with the following command:
```
python utilities/get_librispeech_data.py --data_root=/path/to/validation/folder --data_set=DEV_OTHER
brevitas_quartznet_preprocess --data_root=/path/to/validation/folder --data_set=DEV_OTHER
```

To evaluate a pretrained quantized model on LibriSpeech:

- Install pytorch from the [Pytorch Website](https://pytorch.org/), and Cython with the following command:
`python install --upgrade cython`
- Install the Quartznet requirements with `pip install requirements.txt`
- Make sure you have Brevitas installed
- Pass the corresponding cfg .ini file as an input to the evaluation script. The required checkpoint will be downloaded automatically.
- Install SoX (this [guide](https://at.projects.genivi.org/wiki/display/PROJ/Installation+of+SoX+on+different+Platforms)
may be helpful)
- After cloning the repository, install Brevitas and QuartzNet requirements with `pip install .[stt]`
- Pass the name of the model as an input to the evaluation script. The required checkpoint will be downloaded automatically.

For example, for the evaluation on GPU 0:

```
python quartznet_val.py --input-folder /path/to/validation/folder --model-cfg cfg/quant_quartznet_pertensorscaling_8b.ini --gpu 0
brevitas_quartznet_val --input-folder /path/to/validation/folder --model quant_quartznet_pertensorscaling_8b --gpu 0 --pretrained
```
1 change: 1 addition & 0 deletions brevitas_examples/speech_to_text/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .quartznet import *
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[MODEL]
ARCH: quartznet
TOPOLOGY_FILE: cfg/topology/quartznet15x5.yaml
TOPOLOGY_FILE: quartznet15x5.yaml
PRETRAINED_ENCODER_URL: https://github.com/Xilinx/brevitas/releases/download/quant_quartznet_4b-r0/quant_quartznet_encoder_4b-0a46a232.pth
PRETRAINED_DECODER_URL: https://github.com/Xilinx/brevitas/releases/download/quant_quartznet_4b-r0/quant_quartznet_decoder_4b-bcbf8c7b.pth

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[MODEL]
ARCH: quartznet
TOPOLOGY_FILE: cfg/topology/quartznet15x5.yaml
TOPOLOGY_FILE: quartznet15x5.yaml
PRETRAINED_ENCODER_URL: https://github.com/Xilinx/brevitas/releases/download/quant_quartznet_8b-r0/quant_quartznet_encoder_8b-50f12b4b.pth
PRETRAINED_DECODER_URL: https://github.com/Xilinx/brevitas/releases/download/quant_quartznet_8b-r0/quant_quartznet_decoder_8b-af09651c.pth

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[MODEL]
ARCH: quartznet
TOPOLOGY_FILE: cfg/topology/quartznet15x5.yaml
TOPOLOGY_FILE: quartznet15x5.yaml
PRETRAINED_ENCODER_URL: https://github.com/Xilinx/brevitas/releases/download/quant_quartznet_8b-r0/quant_quartznet_encoder_8b-50f12b4b.pth
PRETRAINED_DECODER_URL: https://github.com/Xilinx/brevitas/releases/download/quant_quartznet_8b-r0/quant_quartznet_decoder_8b-af09651c.pth

Expand Down
Empty file.
Loading

0 comments on commit 2425a9e

Please sign in to comment.