Skip to content

Commit

Permalink
Updated memtorch.mn.Module.patch_model for 1.1.0 Release (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreylammie committed Feb 13, 2021
1 parent 5c20fa2 commit 27b1880
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 26 deletions.
192 changes: 182 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,197 @@
language: python
matrix:
include:
# pytest, codecov
- os: linux
sudo: required
python: 3.6
dist: xenial
dist: focal
env :
- CIBW_BUILD="cp36-manylinux_x86_64"
- CIBW_MANYLINUX_X86_64_IMAGE="manylinux2014"
- CIBW_ENVIRONMENT="LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/host/home/travis/virtualenv/python3.6.12/lib/python3.6/site-packages/torch/lib/:/usr/local/lib"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: osx
osx_image: xcode11
language: shell
python: 3.6
env :
- CIBW_BUILD="cp36-macosx*"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: linux
sudo: required
python: 3.7
dist: xenial
dist: focal
env :
- CIBW_BUILD="cp37-manylinux_x86_64"
- CIBW_MANYLINUX_X86_64_IMAGE="manylinux2014"
- CIBW_ENVIRONMENT="LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/host/home/travis/virtualenv/python3.7.7/lib/python3.7/site-packages/torch/lib/:/usr/local/lib"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: osx
osx_image: xcode11
language: shell
python: 3.7
env :
- CIBW_BUILD="cp37-macosx*"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: linux
sudo: required
python: 3.8
dist: focal
env :
- CIBW_BUILD="cp38-manylinux_x86_64"
- CIBW_MANYLINUX_X86_64_IMAGE="manylinux2014"
- CIBW_ENVIRONMENT="LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/host/home/travis/virtualenv/python3.8.3/lib/python3.8/site-packages/torch/lib/:/usr/local/lib"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: osx
osx_image: xcode11
language: shell
python: 3.8
env :
- CIBW_BUILD="cp38-macosx*"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
# cibuildhweel
- os: linux
sudo: required
python: 3.6
dist: focal
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp36-manylinux_x86_64"
- CIBW_MANYLINUX_X86_64_IMAGE="manylinux2014"
- CIBW_ENVIRONMENT="LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/host/home/travis/virtualenv/python3.6.12/lib/python3.6/site-packages/torch/lib/:/usr/local/lib"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: osx
osx_image: xcode11
language: shell
python: 3.6
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp36-macosx*"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: windows
language: shell
before_install:
- choco install ninja
- choco install visualstudio2019buildtools --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
- choco install python --version 3.6.8
- export PATH="/c/Python36:/c/Python36/Scripts:$PATH"
- ln -s /c/Python36/python.exe /c/Python36/python3.exe
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp36-win_amd64"
- CIBW_BEFORE_BUILD="/c/cibw/python/python.3.6.8/tools/python.exe -m pip install -r requirements_windows_cpu.txt"
- os: linux
sudo: required
python: 3.7
dist: focal
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp37-manylinux_x86_64"
- CIBW_MANYLINUX_X86_64_IMAGE="manylinux2014"
- CIBW_ENVIRONMENT="LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/host/home/travis/virtualenv/python3.7.7/lib/python3.7/site-packages/torch/lib/:/usr/local/lib"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: osx
osx_image: xcode11
language: shell
python: 3.7
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp37-macosx*"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: windows
language: shell
before_install:
- choco install ninja
- choco install visualstudio2019buildtools --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
- choco install python --version 3.7.9
- export PATH="/c/Python37:/c/Python37/Scripts:$PATH"
- ln -s /c/Python37/python.exe /c/Python37/python3.exe
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp37-win_amd64"
- CIBW_BEFORE_BUILD="/c/cibw/python/python.3.7.9/tools/python.exe -m pip install -r requirements_windows_cpu.txt"
- os: linux
sudo: required
python: 3.8
dist: focal
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp38-manylinux_x86_64"
- CIBW_MANYLINUX_X86_64_IMAGE="manylinux2014"
- CIBW_ENVIRONMENT="LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/host/home/travis/virtualenv/python3.8.3/lib/python3.8/site-packages/torch/lib/:/usr/local/lib"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: osx
osx_image: xcode11
language: shell
python: 3.8
dist: xenial
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp38-macosx*"
- CIBW_BEFORE_BUILD="python3 -m pip install -r requirements_linux_osx.txt"
- os: windows
language: shell
before_install:
- choco install ninja
- choco install visualstudio2019buildtools --package-parameters "--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64"
- choco install python --version 3.8.7
- export PATH="/c/Python38:/c/Python38/Scripts:$PATH"
- ln -s /c/Python38/python.exe /c/Python38/python3.exe
env :
- CIBUILDWHEEL="True"
- CIBW_BUILD="cp38-win_amd64"
- CIBW_BEFORE_BUILD="/c/cibw/python/python.3.8.7/tools/python.exe -m pip install -r requirements_windows_cpu.txt"
install:
- sudo apt-get install -y ninja-build
- python -m pip install --upgrade pip
- python -m pip install -U pytest
- python -m pip install numpy pandas torch torchvision matplotlib seaborn sklearn codecov pytest-cov travispls ipython
- python setup.py install
- |
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
sudo apt-get install -y ninja-build
sudo apt-get install libc6
fi
- python3 -m pip install --upgrade pip
- python3 -m pip install -U pytest
- |
if [[ "$TRAVIS_OS_NAME" == "linux" ]] || [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
python3 -m pip install -r requirements_linux_osx.txt
python3 setup.py install
else
export PATH=$MSBUILD_PATH:$PATH
cmd.exe //C 'python3 -m pip install -r requirements_windows_cpu.txt'
cmd.exe //C 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat' amd64 '&&' set DISTUTILS_USE_SDK=1 '&&' python3 setup.py install
fi
script:
- travis-pls -m 5000 pytest -s --cov=memtorch
- |
if [[ "$TRAVIS_OS_NAME" == "linux" ]] || [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
if [[ -z "$CIBUILDWHEEL" ]]; then
python3 -m pip install codecov pytest-cov travis-wait-improved
travis-wait-improved --timeout 60m pytest -s --cov=memtorch
else
python3 -m pip install cibuildwheel
python3 -m cibuildwheel --output-dir wheelhouse
fi
else
if [[ -z "$CIBUILDWHEEL" ]]; then
cmd.exe //C 'python3 -m pip install codecov pytest-cov travis-wait-improved'
cmd.exe //C 'travis-wait-improved --timeout 60m pytest -s --cov=memtorch'
else
cmd.exe //C 'python3 -m pip install cibuildwheel'
cmd.exe //C 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat' amd64 '&&' set DISTUTILS_USE_SDK=1 '&&' python3 -m cibuildwheel --output-dir wheelhouse
fi
fi
deploy:
provider: releases
api_key: $TOKEN
file_glob: true
file:
- "/home/travis/build/coreylammie/MemTorch/wheelhouse/*"
- "/Users/travis/build/coreylammie/MemTorch/wheelhouse/*"
- "/c/Users/travis/build/coreylammie/MemTorch/wheelhouse/*"
skip_cleanup: true
draft: true
on:
tags: true
all_branches: true
after_success:
- codecov
- |
if [[ -z "$CIBUILDWHEEL" ]]; then
codecov
fi
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ coverage:
threshold: 0.5%
patch:
default:
target: 85%
target: 50%
threshold: 0.5%
2 changes: 1 addition & 1 deletion memtorch/bh/memristor/Stanford_PKU.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def simulate(self, voltage_signal, return_current=False):
def set_conductance(self, conductance):
conductance = clip(conductance, 1 / self.r_off, 1 / self.r_on)
gap = self.g_0 * np.log(self.I_0 * np.sinh(self.read_voltage / self.V_0) / (self.read_voltage / (1 / conductance)))
self.gap = max(min(gap, gap_max), gap_min)
self.gap = max(min(gap, self.gap_max), self.gap_min)

def plot_hysteresis_loop(self, duration=0.5, voltage_signal_amplitude=1.5, voltage_signal_frequency=10, log_scale=False, return_result=False):
return super().plot_hysteresis_loop(self, duration=duration, voltage_signal_amplitude=voltage_signal_amplitude, voltage_signal_frequency=voltage_signal_frequency, log_scale=log_scale, return_result=return_result)
Expand Down
49 changes: 35 additions & 14 deletions memtorch/mn/Module.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
'<class \'torch.nn.modules.conv.Conv3d\'>': Conv3d
}

def patch_model(model, memristor_model, memristor_model_params, module_parameters_to_patch={}, mapping_routine=naive_map, p_l=None, transistor=True,
programming_routine=None, programming_routine_params={'rel_tol': 0.1}, scheme=memtorch.bh.Scheme.DoubleColumn, verbose=True,
def patch_model(model, memristor_model, memristor_model_params, module_parameters_to_patch={}, mapping_routine=naive_map, transistor=True,
programming_routine=None, programming_routine_params={'rel_tol': 0.1}, p_l=None, scheme=memtorch.bh.Scheme.DoubleColumn,
tile_shape=None, max_input_voltage=None, ADC_resolution=None, ADC_overflow_rate=0., quant_method=None, verbose=True,
**kwargs):
"""Method to convert a torch.nn model to a memristive model.
Expand All @@ -33,16 +34,26 @@ def patch_model(model, memristor_model, memristor_model_params, module_parameter
Model parameters to patch.
mapping_routine : function
Mapping routine to use.
p_l: float
If not None, the proportion of weights to retain.
transistor : bool
Used to determine if a 1T1R (True) or 1R arrangement (False) is simulated.
programming_routine : function
Programming routine to use.
programming_routine_params : **kwargs
Programming routine keyword arguments.
p_l: float
If not None, the proportion of weights to retain.
scheme : memtorch.bh.Scheme
Weight representation scheme.
tile_shape : (int, int)
Tile shape to use to store weights. If None, modular tiles are not used.
max_input_voltage : float
Maximum input voltage used to encode inputs. If None, inputs are unbounded.
ADC_resolution : int
ADC resolution (bit width). If None, quantization noise is not accounted for.
ADC_overflow_rate : float
Overflow rate threshold for linear quanitzation (if ADC_resolution is not None).
quant_method:
Quantization method. Must be in ['linear', 'log', 'log_minmax', 'minmax', 'tanh'], or None.
verbose : bool
Used to determine if verbose output is enabled (True) or disabled (False).
Expand Down Expand Up @@ -71,20 +82,30 @@ def patch_model(model, memristor_model, memristor_model_params, module_parameter
programming_routine_params=programming_routine_params,
p_l=p_l,
scheme=scheme,
tile_shape=tile_shape,
max_input_voltage=max_input_voltage,
ADC_resolution=ADC_resolution,
ADC_overflow_rate=ADC_overflow_rate,
quant_method=quant_method,
verbose=verbose,
**kwargs))
else:
setattr(model, name, patch(m,
memristor_model=memristor_model,
memristor_model_params=memristor_model_params,
mapping_routine=mapping_routine,
transistor=transistor,
programming_routine=programming_routine,
programming_routine_params=programming_routine_params,
p_l=p_l,
scheme=scheme,
verbose=verbose,
**kwargs))
memristor_model=memristor_model,
memristor_model_params=memristor_model_params,
mapping_routine=mapping_routine,
transistor=transistor,
programming_routine=programming_routine,
programming_routine_params=programming_routine_params,
p_l=p_l,
scheme=scheme,
tile_shape=tile_shape,
max_input_voltage=max_input_voltage,
ADC_resolution=ADC_resolution,
ADC_overflow_rate=ADC_overflow_rate,
quant_method=quant_method,
verbose=verbose,
**kwargs))

def tune_(self, tune_kwargs=None):
"""Method to tune a memristive layer.
Expand Down
8 changes: 8 additions & 0 deletions requirements_linux_osx.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
numpy
pandas
matplotlib
seaborn
sklearn
ipython
torch
torchvision
10 changes: 10 additions & 0 deletions requirements_windows_cpu.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
numpy
pandas
matplotlib
seaborn
sklearn
ipython
-f https://download.pytorch.org/whl/torch_stable.html
torch==1.7.1+cpu
-f https://download.pytorch.org/whl/torch_stable.html
torchvision==0.8.2+cpu
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def create_version_py(version, CUDA):
'scipy',
'sklearn',
'torch>=1.2.0',
'torchvision',
'matplotlib',
'seaborn',
'ipython'
Expand Down

0 comments on commit 27b1880

Please sign in to comment.