Skip to content

Commit e1037c2

Browse files
Fix chopper bug (IBM#732)
* fix: debug mode, chopper trans bug, memory issue, maximizer trans * fix memeory error with valgrind * changelog * fix UMH test * CUDA arch
1 parent 787b7e7 commit e1037c2

97 files changed

Lines changed: 1095 additions & 591 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog], and this project adheres to
2727

2828
* Fix Hardware-Aware training tutorial notebooks (\#700)
2929
* Fix Post-Training Input Range Calibration notebook (\#716)
30+
* Fix memory issues and bugs in analog training for CUDA (\#732)
3031

3132
## Changed
3233

CMakeLists.txt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ option(RPU_USE_TORCH_BUFFERS "Use torch buffers for RPUCuda" ON)
2323

2424

2525
set(RPU_BLAS "OpenBLAS" CACHE STRING "BLAS backend of choice (OpenBLAS, MKL)")
26-
set(RPU_CUDA_ARCHITECTURES "70;75;80" CACHE STRING "Target CUDA architectures")
26+
set(RPU_CUDA_ARCHITECTURES "75;80;89" CACHE STRING "Target CUDA architectures")
2727

2828
# Internal variables.
2929
set(CUDA_TARGET_PROPERTIES POSITION_INDEPENDENT_CODE ON
@@ -110,12 +110,13 @@ if(USE_CUDA)
110110
if (RPU_USE_TORCH_BUFFERS)
111111
if (BUILD_TEST)
112112
# we could just link torch to the tests in principle
113-
message(FATAL_ERROR "Cannot use torch buffers when BUILD_TEST=ON. Set RPU_USE_TORCH_BUFFERS=OFF")
113+
message(STATUS "Cannot use torch buffers when BUILD_TEST=ON. Set RPU_USE_TORCH_BUFFERS=OFF")
114+
set(RPU_USE_TORCH_BUFFERS OFF)
115+
else (BUILD_TEST)
116+
add_compile_definitions(RPU_TORCH_CUDA_BUFFERS)
117+
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr")
118+
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcudafe --diag_suppress=186")
114119
endif(BUILD_TEST)
115-
116-
add_compile_definitions(RPU_TORCH_CUDA_BUFFERS)
117-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr")
118-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcudafe --diag_suppress=186")
119120
endif(RPU_USE_TORCH_BUFFERS)
120121

121122
if(${CUDAToolkit_VERSION_MAJOR} LESS 11)
@@ -140,7 +141,7 @@ if (BUILD_EXTENSION)
140141

141142
target_link_libraries(AIHWKIT_EXTENSION_OPS torch_python c10 torch_cpu)
142143
target_include_directories(AIHWKIT_EXTENSION_OPS PRIVATE src/aihwkit/extension/extension_src)
143-
144+
144145
if(WIN32)
145146
target_link_libraries(AIHWKIT_EXTENSION_OPS c10.lib torch_cpu.lib)
146147
endif()
@@ -149,7 +150,7 @@ if (BUILD_EXTENSION)
149150
add_library(AIHWKIT_EXTENSION_OPS_GPU ${AIHWKIT_EXTENSION_OPS_GPU_SRCS})
150151
target_link_libraries(AIHWKIT_EXTENSION_OPS_GPU AIHWKIT_EXTENSION_OPS c10_cuda torch_cuda cudart)
151152
target_include_directories(AIHWKIT_EXTENSION_OPS_GPU PRIVATE src/aihwkit/extension/extension_src)
152-
153+
153154
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr")
154155
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcudafe --diag_suppress=186")
155156
set_target_properties(AIHWKIT_EXTENSION_OPS_GPU PROPERTIES ${CUDA_TARGET_PROPERTIES})
@@ -187,20 +188,34 @@ endif(BUILD_EXTENSION)
187188

188189
# Add tests.
189190
if(BUILD_TEST)
191+
190192
enable_testing()
191193

192194
foreach(test_src ${RPU_CPU_TEST_SRCS} ${RPU_GPU_TEST_SRCS})
193195
get_filename_component(test_name ${test_src} NAME_WE)
194196
add_executable(${test_name} ${test_src})
195197
target_link_libraries(${test_name} gtest gmock)
198+
target_link_libraries(${test_name} torch_python c10 torch_cpu)
199+
set_target_properties(${test_name} PROPERTIES CXX_STANDARD 17
200+
POSITION_INDEPENDENT_CODE ON)
201+
202+
if(WIN32)
203+
target_link_libraries(${test_name} c10.lib torch_cpu.lib)
204+
endif()
196205

197206
# Link to main library.
198-
if(${test_src} IN_LIST RPU_CPU_TEST_SRCS)
199-
target_link_libraries(${test_name} RPU_CPU ${RPU_DEPENDENCY_LIBS})
200-
else()
207+
target_link_libraries(${test_name} RPU_CPU ${RPU_DEPENDENCY_LIBS})
208+
209+
if(${test_src} IN_LIST RPU_GPU_TEST_SRCS)
210+
target_link_libraries(${test_name} torch_cuda c10_cuda cudart)
201211
target_link_libraries(${test_name} RPU_GPU RPU_CPU cublas curand ${RPU_DEPENDENCY_LIBS})
202212
set_target_properties(${test_name} PROPERTIES ${CUDA_TARGET_PROPERTIES})
203213
set_property(TARGET ${test_name} PROPERTY CUDA_ARCHITECTURES ${RPU_CUDA_ARCHITECTURES})
214+
215+
if(WIN32)
216+
target_link_libraries(${test_name} c10_cuda.lib torch_cuda.lib)
217+
endif(WIN32)
218+
204219
endif()
205220

206221
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)

cmake/dependencies_test.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ if(BUILD_TEST)
1111
URL_HASH MD5=52943a59cefce0ae0491d4d2412c120b
1212
CMAKE_ARGS "-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI\=0"
1313
INSTALL_COMMAND ""
14+
DOWNLOAD_EXTRACT_TIMESTAMP true
1415
)
1516

1617
ExternalProject_Get_Property(GTest source_dir)
@@ -20,4 +21,5 @@ if(BUILD_TEST)
2021

2122
include_directories(SYSTEM ${GTest_INCLUDE_DIR})
2223
link_directories(SYSTEM ${GTest_LIBRARY_DIR})
24+
2325
endif()

examples/04_lenet5_training.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@
2929
from aihwkit.nn import AnalogConv2d, AnalogLinear, AnalogSequential
3030
from aihwkit.optim import AnalogSGD
3131
from aihwkit.simulator.configs import (
32-
SingleRPUConfig,
3332
FloatingPointRPUConfig,
34-
ConstantStepDevice,
33+
SoftBoundsReferenceDevice,
3534
FloatingPointDevice,
35+
build_config,
3636
)
3737
from aihwkit.simulator.rpu_base import cuda
3838

39-
4039
# Check device
4140
USE_CUDA = 0
4241
if cuda.is_compiled():
@@ -59,13 +58,17 @@
5958
# Select the device model to use in the training.
6059
# * If `SingleRPUConfig(device=ConstantStepDevice())` then analog tiles with
6160
# constant step devices will be used,
61+
# * One can use `build_config` to build a config for different
62+
# specialized analog gradient algorithms
6263
# * If `FloatingPointRPUConfig(device=FloatingPointDevice())` then standard
6364
# floating point devices will be used
64-
USE_ANALOG_TRAINING = False
65+
USE_ANALOG_TRAINING = True
6566
if USE_ANALOG_TRAINING:
66-
RPU_CONFIG = SingleRPUConfig(device=ConstantStepDevice())
67+
algo = "agad" # or e.g. ttv2
68+
RPU_CONFIG = build_config(algo, device=SoftBoundsReferenceDevice(dw_min=0.05))
6769
else:
6870
RPU_CONFIG = FloatingPointRPUConfig(device=FloatingPointDevice())
71+
print(RPU_CONFIG)
6972

7073

7174
def load_images():

examples/23_using_analog_tile_as_matrix.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#
55
# Licensed under the MIT license. See LICENSE file in the project root for details.
66

7-
"""aihwkit example 22: Simple example of how to use an analog tile as a matrix
8-
"""
7+
"""aihwkit example 22: Simple example of how to use an analog tile as a matrix"""
98
# pylint: disable=invalid-name
109
# pylint: disable=too-many-locals
1110

examples/26_correlation_detection.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#
55
# Licensed under the MIT license. See LICENSE file in the project root for details.
66

7-
"""aihwkit example 25: Simple correlation detection with analog optimizers.
8-
"""
7+
"""aihwkit example 25: Simple correlation detection with analog optimizers."""
98
# pylint: disable=invalid-name, too-many-locals, too-many-statements
109

1110
from typing import Union, Tuple, Optional, List, Dict

examples/35_half_precision_training.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,7 @@ def forward(self, x):
7070

7171
pbar = tqdm.tqdm(enumerate(train_loader))
7272
for batch_idx, (data, target) in pbar:
73-
data, target = data.to(device=device, dtype=torch.bfloat16), target.to(
74-
device=device
75-
)
73+
data, target = data.to(device=device, dtype=torch.bfloat16), target.to(device=device)
7674
optimizer.zero_grad()
7775
output = model(data)
7876
loss = F.nll_loss(output.float(), target)

src/aihwkit/extension/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# pylint: disable=import-error, no-name-in-module, invalid-name
88

9-
"""AIHWKIT extension """
9+
"""AIHWKIT extension"""
1010

1111
from importlib.util import find_spec
1212

src/aihwkit/linalg/matrix.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#
55
# Licensed under the MIT license. See LICENSE file in the project root for details.
66

7-
""" Defines an analog matrix
8-
"""
7+
"""Defines an analog matrix"""
98

109
from typing import Any, Union, Tuple
1110
from scipy.sparse.linalg import LinearOperator

src/aihwkit/nn/low_precision_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# Licensed under the MIT license. See LICENSE file in the project root for details.
66

7-
""" Functions to convert a given model to a quantized counterpart """
7+
"""Functions to convert a given model to a quantized counterpart"""
88

99
from copy import deepcopy
1010

0 commit comments

Comments
 (0)