├── src ├── aihwkit │ ├── VERSION.txt │ ├── simulator │ │ ├── __init__.pyi │ │ ├── digital_low_precision │ │ │ ├── __init__.py │ │ │ ├── utils.py │ │ │ └── LICENSE │ │ ├── __init__.py │ │ ├── tiles │ │ │ ├── __init__.py │ │ │ └── torch_tile_irdrop_t.py │ │ ├── noise_models.py │ │ ├── CMakeLists.txt │ │ ├── configs │ │ │ ├── utils.py │ │ │ └── __init__.py │ │ ├── parameters │ │ │ ├── __init__.py │ │ │ ├── runtime.py │ │ │ └── base.py │ │ ├── presets │ │ │ ├── compounds.py │ │ │ └── __init__.py │ │ └── rpu_base_src │ │ │ └── rpu_base.h │ ├── nn │ │ ├── low_precision_modules │ │ │ ├── __init__.py │ │ │ └── quantized_input_module.py │ │ ├── modules │ │ │ ├── __init__.py │ │ │ └── rnn │ │ │ │ └── __init__.py │ │ └── __init__.py │ ├── utils │ │ └── __init__.py │ ├── experiments │ │ ├── experiments │ │ │ └── __init__.py │ │ ├── __init__.py │ │ └── runners │ │ │ ├── base.py │ │ │ ├── __init__.py │ │ │ └── i_metrics.py │ ├── cloud │ │ ├── client │ │ │ ├── v1 │ │ │ │ └── __init__.py │ │ │ ├── __init__.py │ │ │ ├── exceptions.py │ │ │ └── utils.py │ │ ├── __init__.py │ │ └── converter │ │ │ ├── v1 │ │ │ ├── __init__.py │ │ │ └── analog_info.py │ │ │ ├── definitions │ │ │ ├── __init__.py │ │ │ ├── output_file_pb2.py │ │ │ ├── input_file_pb2.py │ │ │ └── i_output_file_pb2.py │ │ │ ├── __init__.py │ │ │ └── exceptions.py │ ├── __init__.py │ ├── inference │ │ ├── noise │ │ │ └── __init__.py │ │ ├── compensation │ │ │ ├── __init__.py │ │ │ ├── base.py │ │ │ └── drift.py │ │ ├── converter │ │ │ ├── __init__.py │ │ │ └── base.py │ │ ├── calibration │ │ │ └── __init__.py │ │ ├── __init__.py │ │ └── utils.py │ ├── linalg │ │ └── __init__.py │ ├── optim │ │ └── __init__.py │ ├── extension │ │ ├── extension_src │ │ │ ├── aihwkit_extension.h │ │ │ ├── ops │ │ │ │ ├── float_prec_common_gpu.h │ │ │ │ ├── float_prec_op.h │ │ │ │ └── thevenin_equiv_op.h │ │ │ └── aihwkit_extension.cpp │ │ ├── __init__.py │ │ ├── CMakeLists.txt │ │ └── functions.py │ ├── version.py │ └── exceptions.py └── rpucuda │ ├── cuda │ ├── CMakeLists.txt │ ├── rpu_cub.h │ ├── weight_clipper_cuda.h │ ├── cuda_fp16_util.h │ ├── test_helper.h │ ├── weight_modifier_cuda.h │ ├── cuda_buffer.h │ ├── rpucuda_constantstep_device.h │ ├── weight_drifter_cuda.h │ ├── weight_remapper_cuda.h │ ├── maximizer.h │ ├── rpucuda_buffered_transfer_device.h │ └── rpucuda_expstep_device.h │ ├── CMakeLists.txt │ ├── rpu_constantstep_device.h │ ├── weight_clipper.h │ └── math_util.h ├── .black ├── docs ├── source │ ├── glossary.rst │ ├── changelog.rst │ ├── api │ │ ├── modules.rst │ │ ├── aihwkit.version.rst │ │ ├── aihwkit.exceptions.rst │ │ ├── aihwkit.utils.export.rst │ │ ├── aihwkit.utils.legacy.rst │ │ ├── aihwkit.linalg.matrix.rst │ │ ├── aihwkit.nn.conversion.rst │ │ ├── aihwkit.optim.context.rst │ │ ├── aihwkit.utils.fitting.rst │ │ ├── aihwkit.inference.utils.rst │ │ ├── aihwkit.nn.modules.base.rst │ │ ├── aihwkit.nn.modules.conv.rst │ │ ├── aihwkit.nn.modules.linear.rst │ │ ├── aihwkit.utils.analog_info.rst │ │ ├── aihwkit.cloud.client.utils.rst │ │ ├── aihwkit.nn.modules.rnn.rnn.rst │ │ ├── aihwkit.cloud.client.session.rst │ │ ├── aihwkit.extension.functions.rst │ │ ├── aihwkit.inference.noise.base.rst │ │ ├── aihwkit.inference.noise.pcm.rst │ │ ├── aihwkit.nn.modules.container.rst │ │ ├── aihwkit.nn.modules.rnn.cells.rst │ │ ├── aihwkit.simulator.tiles.base.rst │ │ ├── aihwkit.utils.visualization.rst │ │ ├── aihwkit.cloud.client.entities.rst │ │ ├── aihwkit.cloud.client.v1.stubs.rst │ │ ├── aihwkit.inference.noise.reram.rst │ │ ├── aihwkit.nn.modules.rnn.layers.rst │ │ ├── aihwkit.simulator.presets.web.rst │ │ ├── aihwkit.simulator.rpu_base.tiles.rst │ │ ├── aihwkit.simulator.tiles.array.rst │ │ ├── aihwkit.simulator.tiles.utils.rst │ │ ├── aihwkit.inference.noise.custom.rst │ │ ├── aihwkit.inference.noise.fusion.rst │ │ ├── aihwkit.nn.modules.conv_mapped.rst │ │ ├── aihwkit.optim.analog_optimizer.rst │ │ ├── aihwkit.simulator.noise_models.rst │ │ ├── aihwkit.simulator.tiles.analog.rst │ │ ├── aihwkit.simulator.tiles.custom.rst │ │ ├── aihwkit.simulator.tiles.module.rst │ │ ├── aihwkit.cloud.client.exceptions.rst │ │ ├── aihwkit.cloud.client.v1.parsers.rst │ │ ├── aihwkit.simulator.configs.utils.rst │ │ ├── aihwkit.simulator.parameters.io.rst │ │ ├── aihwkit.simulator.presets.utils.rst │ │ ├── aihwkit.simulator.tiles.rpucuda.rst │ │ ├── aihwkit.utils.visualization_web.rst │ │ ├── aihwkit.experiments.runners.base.rst │ │ ├── aihwkit.experiments.runners.cloud.rst │ │ ├── aihwkit.experiments.runners.local.rst │ │ ├── aihwkit.inference.converter.base.rst │ │ ├── aihwkit.nn.modules.linear_mapped.rst │ │ ├── aihwkit.simulator.configs.configs.rst │ │ ├── aihwkit.simulator.configs.devices.rst │ │ ├── aihwkit.simulator.configs.helpers.rst │ │ ├── aihwkit.simulator.parameters.base.rst │ │ ├── aihwkit.simulator.presets.configs.rst │ │ ├── aihwkit.simulator.presets.devices.rst │ │ ├── aihwkit.simulator.tiles.functions.rst │ │ ├── aihwkit.simulator.tiles.inference.rst │ │ ├── aihwkit.simulator.tiles.periphery.rst │ │ ├── aihwkit.simulator.tiles.transfer.rst │ │ ├── aihwkit.cloud.converter.exceptions.rst │ │ ├── aihwkit.inference.converter.fusion.rst │ │ ├── aihwkit.simulator.parameters.enums.rst │ │ ├── aihwkit.simulator.parameters.utils.rst │ │ ├── aihwkit.cloud.client.v1.api_client.rst │ │ ├── aihwkit.cloud.converter.definitions.rst │ │ ├── aihwkit.experiments.runners.i_cloud.rst │ │ ├── aihwkit.experiments.runners.i_local.rst │ │ ├── aihwkit.experiments.runners.metrics.rst │ │ ├── aihwkit.inference.compensation.base.rst │ │ ├── aihwkit.simulator.configs.compounds.rst │ │ ├── aihwkit.simulator.presets.compounds.rst │ │ ├── aihwkit.simulator.presets.inference.rst │ │ ├── aihwkit.simulator.tiles.analog_mvm.rst │ │ ├── aihwkit.simulator.tiles.torch_tile.rst │ │ ├── aihwkit.experiments.experiments.base.rst │ │ ├── aihwkit.inference.compensation.drift.rst │ │ ├── aihwkit.simulator.parameters.helpers.rst │ │ ├── aihwkit.simulator.parameters.mapping.rst │ │ ├── aihwkit.simulator.parameters.runtime.rst │ │ ├── aihwkit.cloud.client.v1.i_api_client.rst │ │ ├── aihwkit.cloud.converter.v1.i_mappings.rst │ │ ├── aihwkit.cloud.converter.v1.inferencing.rst │ │ ├── aihwkit.experiments.runners.i_metrics.rst │ │ ├── aihwkit.simulator.parameters.inference.rst │ │ ├── aihwkit.simulator.parameters.pre_post.rst │ │ ├── aihwkit.simulator.parameters.training.rst │ │ ├── aihwkit.cloud.converter.v1.analog_info.rst │ │ ├── aihwkit.inference.converter.conductance.rst │ │ ├── aihwkit.simulator.tiles.floating_point.rst │ │ ├── aihwkit.experiments.experiments.training.rst │ │ ├── aihwkit.simulator.tiles.inference_torch.rst │ │ ├── aihwkit.inference.calibration.calibration.rst │ │ ├── aihwkit.cloud.converter.v1.rpu_config_info.rst │ │ ├── aihwkit.experiments.experiments.inferencing.rst │ │ ├── aihwkit.cloud.converter.v1.noise_model_info.rst │ │ ├── aihwkit.simulator.tiles.analog_mvm_irdrop_t.rst │ │ ├── aihwkit.simulator.tiles.torch_tile_irdrop_t.rst │ │ ├── aihwkit.linalg.rst │ │ ├── aihwkit.extension.rst │ │ ├── aihwkit.cloud.rst │ │ ├── aihwkit.optim.rst │ │ ├── aihwkit.simulator.rpu_base.rst │ │ ├── aihwkit.experiments.rst │ │ ├── aihwkit.inference.calibration.rst │ │ ├── aihwkit.nn.rst │ │ ├── aihwkit.nn.modules.rnn.rst │ │ ├── aihwkit.inference.compensation.rst │ │ ├── aihwkit.inference.converter.rst │ │ ├── aihwkit.utils.rst │ │ ├── aihwkit.cloud.client.v1.rst │ │ ├── aihwkit.experiments.experiments.rst │ │ ├── aihwkit.inference.noise.rst │ │ ├── aihwkit.cloud.converter.rst │ │ ├── aihwkit.simulator.configs.rst │ │ ├── aihwkit.inference.rst │ │ ├── aihwkit.cloud.converter.v1.rst │ │ ├── aihwkit.cloud.client.rst │ │ ├── aihwkit.simulator.presets.rst │ │ ├── aihwkit.rst │ │ ├── aihwkit.simulator.rst │ │ ├── aihwkit.experiments.runners.rst │ │ ├── aihwkit.nn.modules.rst │ │ ├── aihwkit.simulator.parameters.rst │ │ └── aihwkit.simulator.tiles.rst │ ├── roadmap.rst │ ├── pros_cons.rst │ └── analog_ai_hw.rst ├── img │ ├── hwa.jpg │ ├── svhn.png │ ├── EcRam.png │ ├── ReRAM-ES.png │ ├── ReRAM-SB.png │ ├── pcm_model.png │ ├── tikitaka.png │ ├── strapi_data.png │ ├── aihwkit-design.png │ ├── analog_ai_hw.png │ ├── architecture.png │ ├── capacitorCell.png │ ├── fashion_mnist.png │ ├── momentum_sgd.png │ ├── pcm_drift_plot.png │ ├── pcm_prog_plot.png │ ├── pcm_resistance.png │ ├── pulse_trains.png │ ├── analog_mac_time.png │ ├── hwa_added_noise.png │ ├── parallel_update.png │ ├── pcm_compensation.png │ ├── pcm_drift_model.png │ ├── reram_drift_plot.png │ ├── reram_prog_plot.png │ ├── reram_read_plot.png │ ├── training_example.png │ ├── analog_dnn_training.png │ ├── filamentary_reram.png │ ├── inference_version.png │ ├── mixedprecision_sgd.png │ ├── reram_measurements.png │ ├── analog_ai_comparison.png │ ├── analog_non_idealities.png │ ├── composer_architecture.png │ ├── toolkit_quantization.png │ ├── analog_chip_description.png │ ├── google_provider_details.png │ └── strapi_training_details.png ├── requirements.txt └── Makefile ├── notebooks ├── img │ ├── hwa.jpg │ ├── tt.png │ ├── xbar.png │ ├── LeNet.png │ ├── reram.png │ ├── fusion-ui.png │ ├── pcm-array.png │ ├── analog_Dnn.png │ ├── fusion-chip.png │ ├── MnistExamples.png │ ├── pcm_rpu_unit.png │ ├── LeNet5_animation.png │ ├── idealizedDevice.png │ ├── processing-unit-and-computional-memory.png │ └── processing-unit-and-conventional-memory.png └── iscas_tutorial │ └── README.md ├── .whitesource ├── examples ├── img │ ├── test_error.png │ ├── test_losses.png │ └── replay_fake_images_gan.gif ├── csv │ └── selfdefine.csv ├── 10_plot_presets.py ├── 19_analog_summary_lenet.py ├── 29_linalg_krylov.py ├── 23_using_analog_tile_as_matrix.py ├── 17_resnet34_imagenet_conversion_to_analog.py ├── 02_multiple_layer.py ├── 01_simple_layer.py ├── 09_simple_layer_deterministic_pulses.py ├── 07_simple_layer_with_other_devices.py └── 13_experiment_3fc.py ├── requirements-examples.txt ├── requirements-dev.txt ├── tests ├── __init__.py ├── helpers │ ├── __init__.py │ └── testcases.py └── README.md ├── requirements.txt ├── .github ├── SECURITY.md ├── pull_request_template.md └── ISSUE_TEMPLATE │ ├── feature_request.md │ └── bug_report.md ├── pyproject.toml ├── MANIFEST.in ├── cmake ├── Modules │ ├── FindCUB.cmake │ ├── FindAVX.cmake │ └── FindTorch.cmake ├── dependencies_test.cmake └── dependencies_cuda.cmake ├── setup.cfg ├── .readthedocs.yaml ├── LICENSE.txt └── Makefile /src/aihwkit/VERSION.txt: -------------------------------------------------------------------------------- 1 | 1.0.0 2 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/__init__.pyi: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.black: -------------------------------------------------------------------------------- 1 | [tool.black] 2 | line-length = 100 3 | -------------------------------------------------------------------------------- /docs/source/glossary.rst: -------------------------------------------------------------------------------- 1 | Glossary 2 | ======== -------------------------------------------------------------------------------- /docs/img/hwa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/hwa.jpg -------------------------------------------------------------------------------- /docs/img/svhn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/svhn.png -------------------------------------------------------------------------------- /docs/img/EcRam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/EcRam.png -------------------------------------------------------------------------------- /docs/img/ReRAM-ES.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/ReRAM-ES.png -------------------------------------------------------------------------------- /docs/img/ReRAM-SB.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/ReRAM-SB.png -------------------------------------------------------------------------------- /docs/img/pcm_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pcm_model.png -------------------------------------------------------------------------------- /docs/img/tikitaka.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/tikitaka.png -------------------------------------------------------------------------------- /docs/source/changelog.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../../CHANGELOG.md 2 | :parser: myst_parser.sphinx_ -------------------------------------------------------------------------------- /notebooks/img/hwa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/hwa.jpg -------------------------------------------------------------------------------- /notebooks/img/tt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/tt.png -------------------------------------------------------------------------------- /notebooks/img/xbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/xbar.png -------------------------------------------------------------------------------- /.whitesource: -------------------------------------------------------------------------------- 1 | { 2 | "settingsInheritedFrom": "whitesource-config/whitesource-config@master" 3 | } -------------------------------------------------------------------------------- /docs/img/strapi_data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/strapi_data.png -------------------------------------------------------------------------------- /notebooks/img/LeNet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/LeNet.png -------------------------------------------------------------------------------- /notebooks/img/reram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/reram.png -------------------------------------------------------------------------------- /docs/img/aihwkit-design.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/aihwkit-design.png -------------------------------------------------------------------------------- /docs/img/analog_ai_hw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/analog_ai_hw.png -------------------------------------------------------------------------------- /docs/img/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/architecture.png -------------------------------------------------------------------------------- /docs/img/capacitorCell.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/capacitorCell.png -------------------------------------------------------------------------------- /docs/img/fashion_mnist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/fashion_mnist.png -------------------------------------------------------------------------------- /docs/img/momentum_sgd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/momentum_sgd.png -------------------------------------------------------------------------------- /docs/img/pcm_drift_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pcm_drift_plot.png -------------------------------------------------------------------------------- /docs/img/pcm_prog_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pcm_prog_plot.png -------------------------------------------------------------------------------- /docs/img/pcm_resistance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pcm_resistance.png -------------------------------------------------------------------------------- /docs/img/pulse_trains.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pulse_trains.png -------------------------------------------------------------------------------- /examples/img/test_error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/examples/img/test_error.png -------------------------------------------------------------------------------- /notebooks/img/fusion-ui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/fusion-ui.png -------------------------------------------------------------------------------- /notebooks/img/pcm-array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/pcm-array.png -------------------------------------------------------------------------------- /docs/img/analog_mac_time.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/analog_mac_time.png -------------------------------------------------------------------------------- /docs/img/hwa_added_noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/hwa_added_noise.png -------------------------------------------------------------------------------- /docs/img/parallel_update.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/parallel_update.png -------------------------------------------------------------------------------- /docs/img/pcm_compensation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pcm_compensation.png -------------------------------------------------------------------------------- /docs/img/pcm_drift_model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/pcm_drift_model.png -------------------------------------------------------------------------------- /docs/img/reram_drift_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/reram_drift_plot.png -------------------------------------------------------------------------------- /docs/img/reram_prog_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/reram_prog_plot.png -------------------------------------------------------------------------------- /docs/img/reram_read_plot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/reram_read_plot.png -------------------------------------------------------------------------------- /docs/img/training_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/training_example.png -------------------------------------------------------------------------------- /examples/img/test_losses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/examples/img/test_losses.png -------------------------------------------------------------------------------- /notebooks/img/analog_Dnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/analog_Dnn.png -------------------------------------------------------------------------------- /notebooks/img/fusion-chip.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/fusion-chip.png -------------------------------------------------------------------------------- /docs/img/analog_dnn_training.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/analog_dnn_training.png -------------------------------------------------------------------------------- /docs/img/filamentary_reram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/filamentary_reram.png -------------------------------------------------------------------------------- /docs/img/inference_version.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/inference_version.png -------------------------------------------------------------------------------- /docs/img/mixedprecision_sgd.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/mixedprecision_sgd.png -------------------------------------------------------------------------------- /docs/img/reram_measurements.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/reram_measurements.png -------------------------------------------------------------------------------- /docs/source/api/modules.rst: -------------------------------------------------------------------------------- 1 | aihwkit 2 | ======= 3 | 4 | .. toctree:: 5 | :maxdepth: 4 6 | 7 | aihwkit 8 | -------------------------------------------------------------------------------- /notebooks/img/MnistExamples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/MnistExamples.png -------------------------------------------------------------------------------- /notebooks/img/pcm_rpu_unit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/pcm_rpu_unit.png -------------------------------------------------------------------------------- /docs/img/analog_ai_comparison.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/analog_ai_comparison.png -------------------------------------------------------------------------------- /docs/img/analog_non_idealities.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/analog_non_idealities.png -------------------------------------------------------------------------------- /docs/img/composer_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/composer_architecture.png -------------------------------------------------------------------------------- /docs/img/toolkit_quantization.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/toolkit_quantization.png -------------------------------------------------------------------------------- /notebooks/img/LeNet5_animation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/LeNet5_animation.png -------------------------------------------------------------------------------- /notebooks/img/idealizedDevice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/idealizedDevice.png -------------------------------------------------------------------------------- /docs/img/analog_chip_description.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/analog_chip_description.png -------------------------------------------------------------------------------- /docs/img/google_provider_details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/google_provider_details.png -------------------------------------------------------------------------------- /docs/img/strapi_training_details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/docs/img/strapi_training_details.png -------------------------------------------------------------------------------- /examples/img/replay_fake_images_gan.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/examples/img/replay_fake_images_gan.gif -------------------------------------------------------------------------------- /src/aihwkit/simulator/digital_low_precision/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 Qualcomm Technologies, Inc. 2 | # All Rights Reserved. 3 | -------------------------------------------------------------------------------- /examples/csv/selfdefine.csv: -------------------------------------------------------------------------------- 1 | 0.001,0.004 2 | 0.001,0.004 3 | 0.005,0.006 4 | 0.005,0.006 5 | 0.001,0.002 6 | 0.001,0.002 7 | 0.001,0.002 8 | -------------------------------------------------------------------------------- /requirements-examples.txt: -------------------------------------------------------------------------------- 1 | torchvision>=0.7.0 2 | matplotlib>=3.0 3 | pyamg 4 | jupyter 5 | transformers 6 | evaluate 7 | accelerate 8 | lmfit -------------------------------------------------------------------------------- /notebooks/img/processing-unit-and-computional-memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/processing-unit-and-computional-memory.png -------------------------------------------------------------------------------- /notebooks/img/processing-unit-and-conventional-memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/aihwkit/master/notebooks/img/processing-unit-and-conventional-memory.png -------------------------------------------------------------------------------- /requirements-dev.txt: -------------------------------------------------------------------------------- 1 | mypy==0.991 2 | types-dataclasses 3 | types-requests==2.26.3 4 | pycodestyle==2.10.0 5 | pylint==2.15.7 6 | pytest==6.2.4 7 | parameterized==0.8.1 8 | black==24.3.0 9 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.version.rst: -------------------------------------------------------------------------------- 1 | aihwkit.version module 2 | ====================== 3 | 4 | .. automodule:: aihwkit.version 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.exceptions.rst: -------------------------------------------------------------------------------- 1 | aihwkit.exceptions module 2 | ========================= 3 | 4 | .. automodule:: aihwkit.exceptions 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.export.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils.export module 2 | =========================== 3 | 4 | .. automodule:: aihwkit.utils.export 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.legacy.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils.legacy module 2 | =========================== 3 | 4 | .. automodule:: aihwkit.utils.legacy 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.linalg.matrix.rst: -------------------------------------------------------------------------------- 1 | aihwkit.linalg.matrix module 2 | ============================ 3 | 4 | .. automodule:: aihwkit.linalg.matrix 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.conversion.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.conversion module 2 | ============================ 3 | 4 | .. automodule:: aihwkit.nn.conversion 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.optim.context.rst: -------------------------------------------------------------------------------- 1 | aihwkit.optim.context module 2 | ============================ 3 | 4 | .. automodule:: aihwkit.optim.context 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.fitting.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils.fitting module 2 | ============================ 3 | 4 | .. automodule:: aihwkit.utils.fitting 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.utils module 2 | ============================== 3 | 4 | .. automodule:: aihwkit.inference.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.base module 2 | ============================== 3 | 4 | .. automodule:: aihwkit.nn.modules.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.conv.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.conv module 2 | ============================== 3 | 4 | .. automodule:: aihwkit.nn.modules.conv 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.linear.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.linear module 2 | ================================ 3 | 4 | .. automodule:: aihwkit.nn.modules.linear 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.analog_info.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils.analog\_info module 2 | ================================= 3 | 4 | .. automodule:: aihwkit.utils.analog_info 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.utils module 2 | ================================= 3 | 4 | .. automodule:: aihwkit.cloud.client.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.rnn.rnn.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.rnn.rnn module 2 | ================================= 3 | 4 | .. automodule:: aihwkit.nn.modules.rnn.rnn 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.session.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.session module 2 | =================================== 3 | 4 | .. automodule:: aihwkit.cloud.client.session 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.extension.functions.rst: -------------------------------------------------------------------------------- 1 | aihwkit.extension.functions module 2 | ================================== 3 | 4 | .. automodule:: aihwkit.extension.functions 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.noise.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.noise.base module 2 | =================================== 3 | 4 | .. automodule:: aihwkit.inference.noise.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.noise.pcm.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.noise.pcm module 2 | ================================== 3 | 4 | .. automodule:: aihwkit.inference.noise.pcm 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.container.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.container module 2 | =================================== 3 | 4 | .. automodule:: aihwkit.nn.modules.container 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.rnn.cells.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.rnn.cells module 2 | =================================== 3 | 4 | .. automodule:: aihwkit.nn.modules.rnn.cells 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.base module 2 | =================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.visualization.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils.visualization module 2 | ================================== 3 | 4 | .. automodule:: aihwkit.utils.visualization 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.entities.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.entities module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.cloud.client.entities 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.v1.stubs.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.v1.stubs module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.cloud.client.v1.stubs 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.noise.reram.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.noise.reram module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.inference.noise.reram 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.rnn.layers.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.rnn.layers module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.nn.modules.rnn.layers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.web.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets.web module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.simulator.presets.web 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.rpu_base.tiles.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.rpu\_base.tiles 2 | ================================= 3 | 4 | .. automodule:: aihwkit.simulator.rpu_base.tiles 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.array.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.array module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.array 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.utils module 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/nn/low_precision_modules/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit tests.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.noise.custom.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.noise.custom module 2 | ===================================== 3 | 4 | .. automodule:: aihwkit.inference.noise.custom 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.noise.fusion.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.noise.fusion module 2 | ===================================== 3 | 4 | .. automodule:: aihwkit.inference.noise.fusion 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.conv_mapped.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.conv\_mapped module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.nn.modules.conv_mapped 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.optim.analog_optimizer.rst: -------------------------------------------------------------------------------- 1 | aihwkit.optim.analog\_optimizer module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.optim.analog_optimizer 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.noise_models.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.noise\_models module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.simulator.noise_models 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.analog.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.analog module 2 | ===================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.analog 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.custom.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.custom module 2 | ===================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.custom 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.module.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.module module 2 | ===================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.module 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.exceptions.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.exceptions module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.cloud.client.exceptions 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.v1.parsers.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.v1.parsers module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.cloud.client.v1.parsers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.configs.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.configs.utils module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.simulator.configs.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.io.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.io module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.simulator.parameters.io 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets.utils module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.simulator.presets.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.rpucuda.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.rpucuda module 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.rpucuda 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.visualization_web.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils.visualization\_web module 2 | ======================================= 3 | 4 | .. automodule:: aihwkit.utils.visualization_web 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.base module 2 | ======================================= 3 | 4 | .. automodule:: aihwkit.experiments.runners.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.cloud.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.cloud module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.experiments.runners.cloud 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.local.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.local module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.experiments.runners.local 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.converter.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.converter.base module 2 | ======================================= 3 | 4 | .. automodule:: aihwkit.inference.converter.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.linear_mapped.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.linear\_mapped module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.nn.modules.linear_mapped 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.configs.configs.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.configs.configs module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.configs.configs 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.configs.devices.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.configs.devices module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.configs.devices 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.configs.helpers.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.configs.helpers module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.configs.helpers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.base module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.parameters.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.configs.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets.configs module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.presets.configs 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.devices.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets.devices module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.presets.devices 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.functions.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.functions module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.functions 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.inference.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.inference module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.inference 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.periphery.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.periphery module 2 | ======================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.periphery 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.transfer.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.transfer module 2 | ======================================= 3 | 4 | .. automodule:: aihwkit.simulator.tiles.transfer 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.exceptions.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.exceptions module 2 | ========================================= 3 | 4 | .. automodule:: aihwkit.cloud.converter.exceptions 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.converter.fusion.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.converter.fusion module 2 | ========================================= 3 | 4 | .. automodule:: aihwkit.inference.converter.fusion 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.enums.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.enums module 2 | ========================================= 3 | 4 | .. automodule:: aihwkit.simulator.parameters.enums 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.utils module 2 | ========================================= 3 | 4 | .. automodule:: aihwkit.simulator.parameters.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /tests/helpers/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Helpers for aihwkit tests.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.v1.api_client.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.v1.api\_client module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.cloud.client.v1.api_client 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.definitions.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.definitions package 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.cloud.converter.definitions 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.i_cloud.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.i\_cloud module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.experiments.runners.i_cloud 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.i_local.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.i\_local module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.experiments.runners.i_local 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.metrics.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.metrics module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.experiments.runners.metrics 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.compensation.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.compensation.base module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.inference.compensation.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.configs.compounds.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.configs.compounds module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.simulator.configs.compounds 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.compounds.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets.compounds module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.simulator.presets.compounds 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.inference.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets.inference module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.simulator.presets.inference 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.analog_mvm.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.analog\_mvm module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.analog_mvm 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.torch_tile.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.torch\_tile module 2 | ========================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.torch_tile 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/nn/modules/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Neural network modules.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.experiments.base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.experiments.base module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.experiments.experiments.base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.compensation.drift.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.compensation.drift module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.inference.compensation.drift 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.helpers.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.helpers module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.simulator.parameters.helpers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.mapping.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.mapping module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.simulator.parameters.mapping 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.runtime.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.runtime module 2 | =========================================== 3 | 4 | .. automodule:: aihwkit.simulator.parameters.runtime 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/nn/modules/rnn/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Analog RNN related modules.""" 8 | -------------------------------------------------------------------------------- /src/aihwkit/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Utilities and helpers for aihwkit.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.v1.i_api_client.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.v1.i\_api\_client module 2 | ============================================= 3 | 4 | .. automodule:: aihwkit.cloud.client.v1.i_api_client 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.v1.i_mappings.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.v1.i\_mappings module 2 | ============================================= 3 | 4 | .. automodule:: aihwkit.cloud.converter.v1.i_mappings 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.v1.inferencing.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.v1.inferencing module 2 | ============================================= 3 | 4 | .. automodule:: aihwkit.cloud.converter.v1.inferencing 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.i_metrics.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners.i\_metrics module 2 | ============================================= 3 | 4 | .. automodule:: aihwkit.experiments.runners.i_metrics 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.inference.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.inference module 2 | ============================================= 3 | 4 | .. automodule:: aihwkit.simulator.parameters.inference 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.pre_post.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.pre\_post module 2 | ============================================= 3 | 4 | .. automodule:: aihwkit.simulator.parameters.pre_post 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.training.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters.training module 2 | ============================================ 3 | 4 | .. automodule:: aihwkit.simulator.parameters.training 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | Sphinx==6.2.1 2 | sphinx-rtd-theme==1.2.2 3 | recommonmark==0.6.0 4 | myst-parser==2.0.0 5 | # Dependencies for extras in `setup.py` needed for documentation. 6 | matplotlib>=3.0 7 | lmfit 8 | # Dependencies needed for building the package 9 | mypy==0.991 10 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.v1.analog_info.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.v1.analog\_info module 2 | ============================================== 3 | 4 | .. automodule:: aihwkit.cloud.converter.v1.analog_info 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.converter.conductance.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.converter.conductance module 2 | ============================================== 3 | 4 | .. automodule:: aihwkit.inference.converter.conductance 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.floating_point.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.floating\_point module 2 | ============================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.floating_point 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # Build dependencies. 2 | cmake>=3.18 3 | scikit-build>=0.11.1 4 | scikit-learn 5 | pybind11>=2.6.2 6 | # Runtime dependencies. 7 | torch>=1.9 8 | torchvision 9 | scipy 10 | requests>=2.25,<3 11 | numpy>=1.22,<2 12 | protobuf>=4.21.6 13 | tqdm 14 | mypy==0.991 15 | -------------------------------------------------------------------------------- /src/aihwkit/experiments/experiments/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Experiments for aihwkit.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.experiments.training.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.experiments.training module 2 | =============================================== 3 | 4 | .. automodule:: aihwkit.experiments.experiments.training 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.inference_torch.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.inference\_torch module 2 | =============================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.inference_torch 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.calibration.calibration.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.calibration.calibration module 2 | ================================================ 3 | 4 | .. automodule:: aihwkit.inference.calibration.calibration 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/client/v1/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Helpers for the AIHW Composer API (version 1).""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.v1.rpu_config_info.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.v1.rpu\_config\_info module 2 | =================================================== 3 | 4 | .. automodule:: aihwkit.cloud.converter.v1.rpu_config_info 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.experiments.inferencing.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.experiments.inferencing module 2 | ================================================== 3 | 4 | .. automodule:: aihwkit.experiments.experiments.inferencing 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/client/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Client for connecting to the the AIHW Composer API.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.v1.noise_model_info.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.v1.noise\_model\_info module 2 | ==================================================== 3 | 4 | .. automodule:: aihwkit.cloud.converter.v1.noise_model_info 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Functionality related to the cloud client for AIHW Composer API.""" 8 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/v1/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Helpers for version 1 of the AIHW Composer format.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.analog_mvm_irdrop_t.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.analog\_mvm\_irdrop\_t module 2 | ===================================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.analog_mvm_irdrop_t 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.torch_tile_irdrop_t.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles.torch\_tile\_irdrop\_t module 2 | ===================================================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles.torch_tile_irdrop_t 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/definitions/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Protobuf definitions for the AIHW Composer API.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.linalg.rst: -------------------------------------------------------------------------------- 1 | aihwkit.linalg package 2 | ====================== 3 | 4 | .. automodule:: aihwkit.linalg 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.linalg.matrix 16 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Conversion utilities for interacting with the AIHW Composer API.""" 8 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | If you believe you have found a vulnerability in `aihwkit`, we encourage to 6 | let us know as soon as possible. 7 | 8 | Please contact us at the ``aihwkit@us.ibm.com`` email address with details 9 | about the vulnerability. 10 | -------------------------------------------------------------------------------- /src/aihwkit/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Analog hardware library for PyTorch.""" 8 | 9 | from .version import __version__ 10 | -------------------------------------------------------------------------------- /src/aihwkit/inference/noise/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Noise models to apply to converted weight values during analog inference.""" 8 | -------------------------------------------------------------------------------- /src/aihwkit/linalg/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Linear Algebra functionality.""" 8 | 9 | from .matrix import AnalogMatrix 10 | -------------------------------------------------------------------------------- /src/aihwkit/inference/compensation/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Compensation methods such as drift compensation during analog inference.""" 8 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Related issues 2 | 3 | 4 | 5 | ## Description 6 | 7 | 8 | 9 | ## Details 10 | 11 | 12 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.extension.rst: -------------------------------------------------------------------------------- 1 | aihwkit.extension package 2 | ========================= 3 | 4 | .. automodule:: aihwkit.extension 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.extension.functions 16 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud package 2 | ===================== 3 | 4 | .. automodule:: aihwkit.cloud 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.cloud.client 16 | aihwkit.cloud.converter 17 | -------------------------------------------------------------------------------- /src/aihwkit/inference/converter/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Converter of weight matrix values into conductance values and back for analog inference.""" 8 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.optim.rst: -------------------------------------------------------------------------------- 1 | aihwkit.optim package 2 | ===================== 3 | 4 | .. automodule:: aihwkit.optim 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.optim.analog_optimizer 16 | aihwkit.optim.context 17 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.rpu_base.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.rpu\_base module 2 | ================================== 3 | 4 | .. automodule:: aihwkit.simulator.rpu_base 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.simulator.rpu_base.tiles 16 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments package 2 | =========================== 3 | 4 | .. automodule:: aihwkit.experiments 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.experiments.experiments 16 | aihwkit.experiments.runners 17 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.calibration.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.calibration package 2 | ===================================== 3 | 4 | .. automodule:: aihwkit.inference.calibration 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.inference.calibration.calibration 16 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn package 2 | ================== 3 | 4 | .. automodule:: aihwkit.nn 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.nn.modules 16 | 17 | Submodules 18 | ---------- 19 | 20 | .. toctree:: 21 | :maxdepth: 4 22 | 23 | aihwkit.nn.conversion 24 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.rnn.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules.rnn package 2 | ============================== 3 | 4 | .. automodule:: aihwkit.nn.modules.rnn 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.nn.modules.rnn.cells 16 | aihwkit.nn.modules.rnn.layers 17 | aihwkit.nn.modules.rnn.rnn 18 | -------------------------------------------------------------------------------- /src/aihwkit/optim/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Analog Optimizers.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | 11 | from aihwkit.optim.analog_optimizer import AnalogOptimizer, AnalogSGD, AnalogAdam 12 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = [ 3 | "setuptools>=66.0.0", 4 | "wheel", 5 | "ninja", 6 | "cmake>=3.18", 7 | "scikit-build>=0.11.1", 8 | "scikit-learn", 9 | "pybind11>=2.6.2", 10 | "torch>=1.9", 11 | "torchvision", 12 | "scipy", 13 | "requests>=2.25,<3", 14 | "numpy>=1.22,<2", 15 | "protobuf>=4.21.6", 16 | "tqdm", 17 | "mypy==0.991" 18 | ] 19 | build-backend = "setuptools.build_meta" 20 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.compensation.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.compensation package 2 | ====================================== 3 | 4 | .. automodule:: aihwkit.inference.compensation 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.inference.compensation.base 16 | aihwkit.inference.compensation.drift 17 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """RPU simulator bindings.""" 8 | 9 | # This import is required in order to load the `torch` shared libraries, which 10 | # the simulator shared library is linked against. 11 | import torch 12 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | # Simulator main files. 6 | file(GLOB RPU_GPU_SRCS *.cu) 7 | set(RPU_GPU_SRCS ${RPU_GPU_SRCS} PARENT_SCOPE) 8 | 9 | # Simulator test files. 10 | file(GLOB RPU_GPU_TEST_SRCS *_test.cpp) 11 | set(RPU_GPU_TEST_SRCS ${RPU_GPU_TEST_SRCS} PARENT_SCOPE) 12 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/exceptions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Conversion-related Exceptions.""" 8 | 9 | from aihwkit.exceptions import AihwkitException 10 | 11 | 12 | class ConversionError(AihwkitException): 13 | """Errors related to Experiment conversion.""" 14 | -------------------------------------------------------------------------------- /src/aihwkit/extension/extension_src/aihwkit_extension.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | #pragma once 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | namespace py = pybind11; 16 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.converter.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.converter package 2 | =================================== 3 | 4 | .. automodule:: aihwkit.inference.converter 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.inference.converter.base 16 | aihwkit.inference.converter.conductance 17 | aihwkit.inference.converter.fusion 18 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.utils.rst: -------------------------------------------------------------------------------- 1 | aihwkit.utils package 2 | ===================== 3 | 4 | .. automodule:: aihwkit.utils 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.utils.analog_info 16 | aihwkit.utils.export 17 | aihwkit.utils.fitting 18 | aihwkit.utils.legacy 19 | aihwkit.utils.visualization 20 | aihwkit.utils.visualization_web 21 | -------------------------------------------------------------------------------- /src/aihwkit/version.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Package version string.""" 8 | 9 | import os 10 | 11 | VERSION_FILE = os.path.join(os.path.dirname(__file__), "VERSION.txt") 12 | with open(VERSION_FILE, encoding="utf-8") as version_file: 13 | __version__ = version_file.read().strip() 14 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE.txt 2 | include README.md 3 | include src/aihwkit/VERSION.txt 4 | global-include CMakeLists.txt 5 | include cmake/*.cmake 6 | include cmake/Modules/*.cmake 7 | include src/aihwkit/simulator/rpu_base_src/*.cpp 8 | include src/aihwkit/simulator/rpu_base_src/*.h 9 | include src/rpucuda/*.h 10 | include src/rpucuda/*.cpp 11 | include src/rpucuda/cuda/*.h 12 | include src/rpucuda/cuda/*.cpp 13 | include src/rpucuda/cuda/*.cu 14 | include tests/*.py 15 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.v1.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client.v1 package 2 | =============================== 3 | 4 | .. automodule:: aihwkit.cloud.client.v1 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.cloud.client.v1.api_client 16 | aihwkit.cloud.client.v1.i_api_client 17 | aihwkit.cloud.client.v1.parsers 18 | aihwkit.cloud.client.v1.stubs 19 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.experiments.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.experiments package 2 | ======================================= 3 | 4 | .. automodule:: aihwkit.experiments.experiments 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.experiments.experiments.base 16 | aihwkit.experiments.experiments.inferencing 17 | aihwkit.experiments.experiments.training 18 | -------------------------------------------------------------------------------- /src/aihwkit/inference/calibration/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """High level inference tools.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | from .calibration import ( 11 | InputRangeCalibrationType, 12 | calibrate_input_ranges, 13 | calibrate_quantization_ranges, 14 | ) 15 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.noise.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference.noise package 2 | =============================== 3 | 4 | .. automodule:: aihwkit.inference.noise 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.inference.noise.base 16 | aihwkit.inference.noise.custom 17 | aihwkit.inference.noise.fusion 18 | aihwkit.inference.noise.pcm 19 | aihwkit.inference.noise.reram 20 | -------------------------------------------------------------------------------- /src/rpucuda/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | # Simulator main files. 6 | file(GLOB RPU_CPU_SRCS *.cpp) 7 | list(FILTER RPU_CPU_SRCS EXCLUDE REGEX ".*_test.cpp$") 8 | set(RPU_CPU_SRCS ${RPU_CPU_SRCS} PARENT_SCOPE) 9 | 10 | # Simulator test files. 11 | file(GLOB RPU_CPU_TEST_SRCS *_test.cpp) 12 | set(RPU_CPU_TEST_SRCS ${RPU_CPU_TEST_SRCS} PARENT_SCOPE) 13 | -------------------------------------------------------------------------------- /src/aihwkit/experiments/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """High-level interface for executing Experiments.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | 11 | from aihwkit.experiments.experiments.training import BasicTraining 12 | from aihwkit.experiments.experiments.inferencing import BasicInferencing 13 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter package 2 | =============================== 3 | 4 | .. automodule:: aihwkit.cloud.converter 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.cloud.converter.definitions 16 | aihwkit.cloud.converter.v1 17 | 18 | Submodules 19 | ---------- 20 | 21 | .. toctree:: 22 | :maxdepth: 4 23 | 24 | aihwkit.cloud.converter.exceptions 25 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.configs.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.configs package 2 | ================================= 3 | 4 | .. automodule:: aihwkit.simulator.configs 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.simulator.configs.compounds 16 | aihwkit.simulator.configs.configs 17 | aihwkit.simulator.configs.devices 18 | aihwkit.simulator.configs.helpers 19 | aihwkit.simulator.configs.utils 20 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.inference.rst: -------------------------------------------------------------------------------- 1 | aihwkit.inference package 2 | ========================= 3 | 4 | .. automodule:: aihwkit.inference 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.inference.calibration 16 | aihwkit.inference.compensation 17 | aihwkit.inference.converter 18 | aihwkit.inference.noise 19 | 20 | Submodules 21 | ---------- 22 | 23 | .. toctree:: 24 | :maxdepth: 4 25 | 26 | aihwkit.inference.utils 27 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.converter.v1.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.converter.v1 package 2 | ================================== 3 | 4 | .. automodule:: aihwkit.cloud.converter.v1 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.cloud.converter.v1.analog_info 16 | aihwkit.cloud.converter.v1.i_mappings 17 | aihwkit.cloud.converter.v1.inferencing 18 | aihwkit.cloud.converter.v1.noise_model_info 19 | aihwkit.cloud.converter.v1.rpu_config_info 20 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.cloud.client.rst: -------------------------------------------------------------------------------- 1 | aihwkit.cloud.client package 2 | ============================ 3 | 4 | .. automodule:: aihwkit.cloud.client 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.cloud.client.v1 16 | 17 | Submodules 18 | ---------- 19 | 20 | .. toctree:: 21 | :maxdepth: 4 22 | 23 | aihwkit.cloud.client.entities 24 | aihwkit.cloud.client.exceptions 25 | aihwkit.cloud.client.session 26 | aihwkit.cloud.client.utils 27 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.presets.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.presets package 2 | ================================= 3 | 4 | .. automodule:: aihwkit.simulator.presets 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.simulator.presets.compounds 16 | aihwkit.simulator.presets.configs 17 | aihwkit.simulator.presets.devices 18 | aihwkit.simulator.presets.inference 19 | aihwkit.simulator.presets.utils 20 | aihwkit.simulator.presets.web 21 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.rst: -------------------------------------------------------------------------------- 1 | aihwkit package 2 | =============== 3 | 4 | .. automodule:: aihwkit 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.cloud 16 | aihwkit.experiments 17 | aihwkit.extension 18 | aihwkit.inference 19 | aihwkit.nn 20 | aihwkit.optim 21 | aihwkit.simulator 22 | aihwkit.utils 23 | 24 | Submodules 25 | ---------- 26 | 27 | .. toctree:: 28 | :maxdepth: 4 29 | 30 | aihwkit.exceptions 31 | aihwkit.version 32 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator package 2 | ========================= 3 | 4 | .. automodule:: aihwkit.simulator 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.simulator.configs 16 | aihwkit.simulator.parameters 17 | aihwkit.simulator.presets 18 | aihwkit.simulator.tiles 19 | 20 | Submodules 21 | ---------- 22 | 23 | .. toctree:: 24 | :maxdepth: 4 25 | 26 | aihwkit.simulator.noise_models 27 | aihwkit.simulator.rpu_base 28 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | --- 8 | 9 | ## Description and motivation 10 | 11 | 12 | 13 | ## Proposed solution 14 | 15 | 16 | 17 | ## Alternatives and other information 18 | 19 | 20 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.experiments.runners.rst: -------------------------------------------------------------------------------- 1 | aihwkit.experiments.runners package 2 | =================================== 3 | 4 | .. automodule:: aihwkit.experiments.runners 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.experiments.runners.base 16 | aihwkit.experiments.runners.cloud 17 | aihwkit.experiments.runners.i_cloud 18 | aihwkit.experiments.runners.i_local 19 | aihwkit.experiments.runners.i_metrics 20 | aihwkit.experiments.runners.local 21 | aihwkit.experiments.runners.metrics 22 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.nn.modules.rst: -------------------------------------------------------------------------------- 1 | aihwkit.nn.modules package 2 | ========================== 3 | 4 | .. automodule:: aihwkit.nn.modules 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Subpackages 10 | ----------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.nn.modules.rnn 16 | 17 | Submodules 18 | ---------- 19 | 20 | .. toctree:: 21 | :maxdepth: 4 22 | 23 | aihwkit.nn.modules.base 24 | aihwkit.nn.modules.container 25 | aihwkit.nn.modules.conv 26 | aihwkit.nn.modules.conv_mapped 27 | aihwkit.nn.modules.linear 28 | aihwkit.nn.modules.linear_mapped 29 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/rpu_cub.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #ifndef RPU_CUB_NS_QUALIFIER 10 | #ifndef CUB_NS_QUALIFIER 11 | #undef CUB_NS_PREFIX 12 | #undef CUB_NS_POSTFIX 13 | #define CUB_NS_PREFIX namespace RPU { 14 | #define CUB_NS_POSTFIX } 15 | #define CUB_NS_QUALIFIER ::RPU::cub 16 | #define RPU_CUB_NS_QUALIFIER RPU::cub:: 17 | #else 18 | #define RPU_CUB_NS_QUALIFIER CUB_NS_QUALIFIER:: 19 | #endif 20 | #endif 21 | 22 | #include 23 | -------------------------------------------------------------------------------- /src/aihwkit/extension/extension_src/ops/float_prec_common_gpu.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "float_prec_common.h" 3 | 4 | namespace aihwkit { 5 | namespace detail { 6 | 7 | template 8 | __device__ __forceinline__ float cudaFPrecCast(const float x) { 9 | FLOATPREC_INIT(EL, ML); 10 | FLOATPREC_BODY(x, y, saturate_to_inf, rounding); 11 | return y; 12 | } 13 | 14 | template <> __device__ __forceinline__ float cudaFPrecCast<6, 9, false, false>(const float x) { 15 | FLOATPREC_EL6_ML9_0_0(x, y); 16 | return y; 17 | } 18 | 19 | } // namespace detail 20 | } // namespace aihwkit 21 | -------------------------------------------------------------------------------- /cmake/Modules/FindCUB.cmake: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | # Try to find the CUB library and headers. 6 | # CUB_FOUND - system has CUB 7 | # CUB_INCLUDE_DIRS - the CUB include directory 8 | 9 | find_path(CUB_INCLUDE_DIR 10 | NAMES cub/cub.cuh 11 | DOC "The directory where CUB includes reside" 12 | ) 13 | 14 | set(CUB_INCLUDE_DIRS ${CUB_INCLUDE_DIR}) 15 | 16 | find_package_handle_standard_args(CUB 17 | FOUND_VAR CUB_FOUND 18 | REQUIRED_VARS CUB_INCLUDE_DIR 19 | ) 20 | 21 | mark_as_advanced(CUB_FOUND) 22 | -------------------------------------------------------------------------------- /src/aihwkit/experiments/runners/base.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Base class for an Experiment Runner.""" 8 | 9 | # pylint: disable=too-few-public-methods 10 | 11 | from typing import Any 12 | 13 | from aihwkit.experiments.experiments.base import Experiment 14 | 15 | 16 | class Runner: 17 | """Base class for an Experiment Runner.""" 18 | 19 | def run(self, experiment: Experiment, **kwargs: Any) -> None: 20 | """Run a single Experiment.""" 21 | raise NotImplementedError 22 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.parameters.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.parameters package 2 | ==================================== 3 | 4 | .. automodule:: aihwkit.simulator.parameters 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.simulator.parameters.base 16 | aihwkit.simulator.parameters.enums 17 | aihwkit.simulator.parameters.helpers 18 | aihwkit.simulator.parameters.inference 19 | aihwkit.simulator.parameters.mapping 20 | aihwkit.simulator.parameters.pre_post 21 | aihwkit.simulator.parameters.runtime 22 | aihwkit.simulator.parameters.training 23 | -------------------------------------------------------------------------------- /src/aihwkit/extension/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | # pylint: disable=import-error, no-name-in-module, invalid-name 8 | 9 | """AIHWKIT extension""" 10 | 11 | from importlib.util import find_spec 12 | 13 | EXTENSION_COMPILED = find_spec(".aihwkit_extension", package="aihwkit.extension") is not None 14 | 15 | if EXTENSION_COMPILED: 16 | from .functions import FloatPrecisionCast 17 | from aihwkit.extension.aihwkit_extension import ops as extension_ops 18 | else: 19 | extension_ops = None 20 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/tiles/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """High level analog tiles.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | 11 | from aihwkit.simulator.tiles.analog import AnalogTile 12 | from aihwkit.simulator.tiles.floating_point import FloatingPointTile 13 | from aihwkit.simulator.tiles.inference import InferenceTile 14 | from aihwkit.simulator.tiles.inference_torch import TorchInferenceTile 15 | from aihwkit.simulator.tiles.quantized_inference_torch import QuantizedTorchInferenceTile 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: 'bug' 6 | assignees: '' 7 | --- 8 | 9 | ## Description 10 | 11 | 12 | 13 | ## How to reproduce 14 | 15 | 16 | 17 | ## Expected behavior 18 | 19 | 20 | 21 | ## Other information 22 | 23 | 24 | 25 | * Pytorch version: 26 | * Package version: 27 | * OS: 28 | * Python version: 29 | * Conda version (or N/A): 30 | -------------------------------------------------------------------------------- /src/aihwkit/extension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | # CPU ops files 6 | file(GLOB AIHWKIT_EXTENSION_OPS_CPU_SRCS extension_src/ops/*.cpp) 7 | set(AIHWKIT_EXTENSION_OPS_CPU_SRCS ${AIHWKIT_EXTENSION_OPS_CPU_SRCS} PARENT_SCOPE) 8 | 9 | # GPU ops files 10 | file(GLOB AIHWKIT_EXTENSION_OPS_GPU_SRCS extension_src/ops/*.cu) 11 | set(AIHWKIT_EXTENSION_OPS_GPU_SRCS ${AIHWKIT_EXTENSION_OPS_GPU_SRCS} PARENT_SCOPE) 12 | 13 | # CPU extension files 14 | file(GLOB EXTENSION_BINDINGS_SRCS extension_src/*.cpp) 15 | set(EXTENSION_BINDINGS_SRCS ${EXTENSION_BINDINGS_SRCS} PARENT_SCOPE) 16 | 17 | 18 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | # IBM Analog Hardware Acceleration Kit: Unit Tests 2 | 3 | 4 | This project contains a unittest compatible test suite, that can be executed by any Python test runner. The recommended runner is pytest, which can be installed along with a number of other development tools used in aihwkit via: 5 | ``` 6 | pip install -r requirements-dev.txt 7 | ``` 8 | 9 | To run the full test suite simply do (from the command line): 10 | ``` 11 | make pytest 12 | ``` 13 | 14 | To run individual test files, you can use, e.g. 15 | ``` 16 | pytest -v -s tests/test_presets.py 17 | ``` 18 | 19 | Individual tests can be run by giving the name, e.g.: 20 | ``` 21 | pytest -v -s tests/test_presets.py::PresetTest_TTv2IdealizedPreset::test_tile_preset 22 | ``` 23 | -------------------------------------------------------------------------------- /src/aihwkit/experiments/runners/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Experiment Runners for aihwkit.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | 11 | from aihwkit.experiments.runners.cloud import CloudRunner 12 | from aihwkit.experiments.runners.i_cloud import InferenceCloudRunner 13 | from aihwkit.experiments.runners.local import LocalRunner 14 | from aihwkit.experiments.runners.i_local import InferenceLocalRunner 15 | from aihwkit.experiments.runners.metrics import LocalMetric 16 | from aihwkit.experiments.runners.i_metrics import InferenceLocalMetric 17 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/noise_models.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Legacy class for import the noise models for inference""" 8 | 9 | import warnings 10 | from aihwkit.inference import * # pylint: disable=unused-wildcard-import, wildcard-import 11 | 12 | warnings.warn( 13 | FutureWarning( 14 | "\n\nThe `aihwkit.simulator.noise_models` module has been superseded " 15 | "by the `aihwkit.inference` module. " 16 | "Please replace `from aihwkit.simulator.noise_models import ...` " 17 | "with `from aihwkit.inference import ...` in your import statement.\n" 18 | ) 19 | ) 20 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [pycodestyle] 2 | max-line-length = 100 3 | exclude = *_pb2.py 4 | ignore = E203, W503 5 | 6 | [pydocstyle] 7 | convention = google 8 | add_ignore = D105,D107,D205,D400,D415 9 | add_select = D204,D215,D401,D404 10 | match-dir = ^(?!helpers|definitions).* 11 | 12 | [mypy] 13 | python_version = 3.8 14 | namespace_packages = True 15 | ignore_missing_imports = True 16 | warn_redundant_casts = True 17 | warn_unreachable = True 18 | strict_equality = True 19 | disallow_untyped_calls = True 20 | disallow_untyped_defs = True 21 | disallow_incomplete_defs = True 22 | strict_optional = True 23 | 24 | [mypy-torch.*] 25 | follow_imports = skip 26 | follow_imports_for_stubs = True 27 | 28 | [mypy-aihwkit.cloud.converter.definitions.*] 29 | ignore_errors = True 30 | 31 | [flake8] 32 | max-line-length = 100 33 | ignore = E203, W503 34 | -------------------------------------------------------------------------------- /docs/source/api/aihwkit.simulator.tiles.rst: -------------------------------------------------------------------------------- 1 | aihwkit.simulator.tiles package 2 | =============================== 3 | 4 | .. automodule:: aihwkit.simulator.tiles 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | Submodules 10 | ---------- 11 | 12 | .. toctree:: 13 | :maxdepth: 4 14 | 15 | aihwkit.simulator.tiles.analog 16 | aihwkit.simulator.tiles.array 17 | aihwkit.simulator.tiles.base 18 | aihwkit.simulator.tiles.custom 19 | aihwkit.simulator.tiles.floating_point 20 | aihwkit.simulator.tiles.functions 21 | aihwkit.simulator.tiles.inference 22 | aihwkit.simulator.tiles.inference_torch 23 | aihwkit.simulator.tiles.module 24 | aihwkit.simulator.tiles.periphery 25 | aihwkit.simulator.tiles.rpucuda 26 | aihwkit.simulator.tiles.torch_tile 27 | aihwkit.simulator.tiles.transfer 28 | aihwkit.simulator.tiles.utils 29 | -------------------------------------------------------------------------------- /src/aihwkit/extension/extension_src/ops/float_prec_op.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #include 8 | 9 | namespace aihwkit { 10 | namespace detail { 11 | 12 | template 13 | void floatPrecisionCastCUDA(const int N, float *Y, const float *X, const bool saturate_to_inf) 14 | #ifndef RPU_USE_CUDA 15 | { 16 | TORCH_CHECK(false, "CUDA is not available."); 17 | } 18 | #endif 19 | ; 20 | 21 | template 22 | void floatPrecisionCastCPU(const int N, float *Y, const float *X, const bool saturate_to_inf); 23 | 24 | } // namespace detail 25 | 26 | at::Tensor 27 | floatPrecisionCast(at::Tensor &x_input, int exponent, int mantissa, bool saturate_to_inf); 28 | 29 | } // namespace aihwkit 30 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | set(python_module_name rpu_base) 6 | 7 | file(GLOB RPU_BINDINGS_SRCS rpu_base_src/*.cpp) 8 | pybind11_add_module(${python_module_name} MODULE ${RPU_BINDINGS_SRCS}) 9 | target_link_libraries(${python_module_name} PRIVATE torch_python) 10 | set_target_properties(${python_module_name} PROPERTIES CXX_STANDARD 17) 11 | 12 | if (USE_CUDA) 13 | target_link_libraries(${python_module_name} PRIVATE RPU_GPU) 14 | else() 15 | target_link_libraries(${python_module_name} PRIVATE RPU_CPU) 16 | endif() 17 | 18 | install(TARGETS ${python_module_name} DESTINATION "src/aihwkit/simulator") 19 | 20 | add_custom_command(TARGET ${python_module_name} 21 | COMMAND stubgen --module "src/aihwkit/simulator" --output . 22 | WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} 23 | ) 24 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # Read the Docs configuration file for Sphinx projects 2 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 3 | 4 | # Required 5 | version: 2 6 | 7 | # Set the OS, Python version and other tools you might need 8 | build: 9 | os: ubuntu-22.04 10 | tools: 11 | python: "3.10" 12 | apt_packages: 13 | - libopenblas-dev 14 | - libpython3-dev 15 | - gcc 16 | # Build documentation in the "docs/" directory with Sphinx 17 | sphinx: 18 | configuration: docs/source/conf.py 19 | 20 | # Optional but recommended, declare the Python requirements required 21 | # to build your documentation 22 | # See https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html 23 | python: 24 | install: 25 | - requirements: docs/requirements.txt 26 | - requirements: requirements.txt 27 | - method: pip 28 | path: . 29 | extra_requirements: 30 | - visualization 31 | - fitting 32 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/digital_low_precision/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2021 Qualcomm Technologies, Inc. 2 | # All Rights Reserved. 3 | 4 | """Util conversion function for the quant library""" 5 | 6 | import numpy as np 7 | from numpy.typing import NDArray 8 | from torch import Tensor 9 | 10 | 11 | def to_numpy(tensor: Tensor) -> NDArray: 12 | """ 13 | Helper function that turns the given tensor into a numpy array. 14 | 15 | Parameters 16 | ---------- 17 | tensor : torch.Tensor 18 | 19 | Returns 20 | ------- 21 | tensor : float or np.array 22 | """ 23 | if isinstance(tensor, np.ndarray): 24 | return tensor 25 | if hasattr(tensor, "is_cuda"): 26 | if tensor.is_cuda: 27 | return tensor.cpu().detach().numpy() 28 | if hasattr(tensor, "detach"): 29 | return tensor.detach().numpy() 30 | if hasattr(tensor, "numpy"): 31 | return tensor.numpy() 32 | 33 | return np.array(tensor) 34 | -------------------------------------------------------------------------------- /src/aihwkit/nn/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Neural network modules.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | 11 | from aihwkit.nn.modules.container import AnalogSequential, AnalogWrapper 12 | from aihwkit.nn.modules.conv import AnalogConv1d, AnalogConv2d, AnalogConv3d 13 | from aihwkit.nn.modules.linear import AnalogLinear 14 | from aihwkit.nn.modules.rnn.rnn import AnalogRNN 15 | from aihwkit.nn.modules.rnn.cells import ( 16 | AnalogGRUCell, 17 | AnalogLSTMCell, 18 | AnalogVanillaRNNCell, 19 | AnalogLSTMCellCombinedWeight, 20 | ) 21 | from aihwkit.nn.modules.linear_mapped import AnalogLinearMapped 22 | from aihwkit.nn.modules.conv_mapped import ( 23 | AnalogConv1dMapped, 24 | AnalogConv2dMapped, 25 | AnalogConv3dMapped, 26 | ) 27 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line, and also 5 | # from the environment for the first two. 6 | SPHINXOPTS ?= 7 | SPHINXBUILD ?= sphinx-build 8 | SOURCEDIR = source 9 | BUILDDIR = build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile apidoc 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 21 | 22 | apidoc: 23 | sphinx-apidoc --separate --module-first --output-dir source/api ../src/aihwkit \ 24 | ../src/aihwkit/cloud/converter/definitions/*_pb2.py ../src/aihwkit/cloud/converter/v1/mappings.py \ 25 | ../src/aihwkit/cloud/converter/v1/training.py 26 | -------------------------------------------------------------------------------- /cmake/dependencies_test.cmake: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) 6 | 7 | if(BUILD_TEST) 8 | include(ExternalProject) 9 | ExternalProject_Add(GTest 10 | URL https://github.com/google/googletest/archive/release-1.11.0.zip 11 | URL_HASH MD5=52943a59cefce0ae0491d4d2412c120b 12 | CMAKE_ARGS "-DCMAKE_CXX_FLAGS=-D_GLIBCXX_USE_CXX11_ABI\=0" 13 | INSTALL_COMMAND "" 14 | DOWNLOAD_EXTRACT_TIMESTAMP true 15 | ) 16 | 17 | ExternalProject_Get_Property(GTest source_dir) 18 | ExternalProject_Get_Property(GTest binary_dir) 19 | set(GTest_INCLUDE_DIR ${source_dir}/googletest/include) 20 | set(GTest_LIBRARY_DIR ${binary_dir}/lib) 21 | 22 | include_directories(SYSTEM ${GTest_INCLUDE_DIR}) 23 | link_directories(SYSTEM ${GTest_LIBRARY_DIR}) 24 | 25 | endif() 26 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/configs/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Legacy location of the utils module. Please import from parameter.utils / enum in future.""" 8 | 9 | # pylint: disable=unused-import 10 | 11 | from aihwkit.simulator.parameters import ( 12 | IOParameters, 13 | UpdateParameters, 14 | WeightModifierParameter, 15 | WeightClipParameter, 16 | WeightRemapParameter, 17 | SimpleDriftParameter, 18 | DriftParameter, 19 | MappingParameter, 20 | InputRangeParameter, 21 | PrePostProcessingParameter, 22 | ) 23 | 24 | from aihwkit.simulator.parameters.enums import ( 25 | BoundManagementType, 26 | NoiseManagementType, 27 | WeightNoiseType, 28 | PulseType, 29 | WeightModifierType, 30 | WeightClipType, 31 | WeightRemapType, 32 | VectorUnitCellUpdatePolicy, 33 | AnalogMVType, 34 | ) 35 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/v1/analog_info.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Helper class for adding rpu_config to neural network model""" 8 | # pylint: disable=no-name-in-module,import-error 9 | 10 | from aihwkit.cloud.converter.definitions.i_input_file_pb2 import ( # type: ignore[attr-defined] 11 | AnalogProto, 12 | ) 13 | 14 | 15 | # pylint: disable=too-few-public-methods) 16 | class AnalogInfo: 17 | """Data class for fields from protobuf AnalogProto message""" 18 | 19 | def __init__(self, a_info: AnalogProto): # type: ignore[valid-type] 20 | """Constructor for this class""" 21 | 22 | # all three fields are required 23 | self.output_noise_strength = a_info.output_noise_strength # type: ignore[attr-defined] 24 | self.adc = a_info.adc # type: ignore[attr-defined] 25 | self.dac = a_info.dac # type: ignore[attr-defined] 26 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/aihwkit/inference/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """High level inference tools.""" 8 | 9 | # Convenience imports for easier access to the classes. 10 | from aihwkit.inference.converter.base import BaseConductanceConverter 11 | from aihwkit.inference.converter.conductance import ( 12 | SinglePairConductanceConverter, 13 | SingleDeviceConductanceConverter, 14 | DualPairConductanceConverter, 15 | NPairConductanceConverter, 16 | CustomPairConductanceConverter, 17 | ) 18 | from aihwkit.inference.noise.base import BaseNoiseModel 19 | from aihwkit.inference.noise.pcm import PCMLikeNoiseModel, CustomDriftPCMLikeNoiseModel 20 | from aihwkit.inference.noise.reram import ReRamWan2022NoiseModel, ReRamCMONoiseModel 21 | from aihwkit.inference.noise.custom import StateIndependentNoiseModel 22 | from aihwkit.inference.compensation.base import BaseDriftCompensation 23 | from aihwkit.inference.compensation.drift import GlobalDriftCompensation 24 | from aihwkit.inference.utils import drift_analog_weights, program_analog_weights 25 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/parameters/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """RPU simulator bindings.""" 8 | 9 | # This import is required in order to load the `torch` shared libraries, which 10 | # the simulator shared library is linked against. 11 | 12 | from .enums import ( 13 | RPUDataType, 14 | BoundManagementType, 15 | NoiseManagementType, 16 | WeightNoiseType, 17 | PulseType, 18 | WeightModifierType, 19 | WeightClipType, 20 | WeightRemapType, 21 | VectorUnitCellUpdatePolicy, 22 | AnalogMVType, 23 | ) 24 | 25 | from .training import UpdateParameters 26 | 27 | from .io import IOParameters, IOParametersIRDropT 28 | 29 | from .mapping import MappingParameter 30 | 31 | from .pre_post import ( 32 | InputRangeParameter, 33 | PrePostProcessingParameter, 34 | PrePostProcessingParameterQuant, 35 | ) 36 | 37 | from .inference import ( 38 | WeightModifierParameter, 39 | WeightClipParameter, 40 | WeightRemapParameter, 41 | SimpleDriftParameter, 42 | DriftParameter, 43 | ) 44 | -------------------------------------------------------------------------------- /src/aihwkit/extension/extension_src/ops/thevenin_equiv_op.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #include 8 | 9 | namespace aihwkit { 10 | namespace detail { 11 | 12 | template 13 | void theveninEquivCUDA( 14 | const int B, 15 | const int M, 16 | const int N, 17 | const int U, 18 | T *vth_3d, 19 | T *rth_3d, 20 | const T *X, 21 | const T *Gp, 22 | const T *Gm, 23 | T tmax, 24 | T r_s) 25 | #ifndef RPU_USE_CUDA 26 | { 27 | TORCH_CHECK(false, "CUDA is not available."); 28 | } 29 | #endif 30 | ; 31 | 32 | template 33 | void theveninEquivCPU( 34 | const int B, 35 | const int M, 36 | const int N, 37 | const int U, 38 | T *vth_3d, 39 | T *rth_3d, 40 | const T *X, 41 | const T *Gp, 42 | const T *Gm, 43 | T tmax, 44 | T r_s); 45 | 46 | } // namespace detail 47 | 48 | at::Tensor theveninEquiv( 49 | at::Tensor &x_input, 50 | at::Tensor &gp_values, 51 | at::Tensor &gm_values, 52 | float r_s, 53 | float t_max, 54 | int time_steps); 55 | } // namespace aihwkit 56 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/weight_clipper_cuda.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "cuda_util.h" 10 | #include "maximizer.h" 11 | #include "weight_clipper.h" 12 | 13 | namespace RPU { 14 | 15 | template class WeightClipperCuda { 16 | 17 | public: 18 | explicit WeightClipperCuda(CudaContextPtr context, int x_size, int d_size); 19 | WeightClipperCuda() {}; 20 | 21 | void apply(T *weights, const WeightClipParameter &wclpar); 22 | 23 | void dumpExtra(RPU::state_t &extra, const std::string prefix) {}; 24 | void loadExtra(const RPU::state_t &extra, const std::string prefix, bool strict) {}; 25 | 26 | private: 27 | CudaContextPtr context_ = nullptr; 28 | int x_size_ = 0; 29 | int d_size_ = 0; 30 | int size_ = 0; 31 | size_t temp_storage_bytes_ = 0; 32 | 33 | // no need to copy 34 | std::unique_ptr> row_amaximizer_ = nullptr; 35 | std::unique_ptr> dev_std_value_ = nullptr; 36 | std::unique_ptr> dev_sum_value_ = nullptr; 37 | std::unique_ptr> dev_temp_storage_ = nullptr; 38 | }; 39 | 40 | } // namespace RPU 41 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/cuda_fp16_util.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | #include "utility_functions.h" 9 | 10 | #ifdef RPU_DEFINE_CUDA_HALF_ARRAY 11 | #include "cuda_fp16.h" 12 | 13 | #ifdef RPU_BFLOAT_AS_FP16 14 | #include "cuda_bf16.h" 15 | #endif 16 | 17 | #ifndef __RPU_CUDA_HALF_DEFINED 18 | #define __RPU_CUDA_HALF_DEFINED 19 | #define RPU_DEFINE_HALF_FUN(NAME) \ 20 | __device__ __forceinline__ half_t NAME(half_t value) { return h##NAME(value); } 21 | RPU_DEFINE_HALF_FUN(rint); 22 | RPU_DEFINE_HALF_FUN(trunc); 23 | RPU_DEFINE_HALF_FUN(exp); 24 | RPU_DEFINE_HALF_FUN(log2); 25 | RPU_DEFINE_HALF_FUN(log); 26 | RPU_DEFINE_HALF_FUN(floor); 27 | RPU_DEFINE_HALF_FUN(ceil); 28 | RPU_DEFINE_HALF_FUN(rsqrt); 29 | RPU_DEFINE_HALF_FUN(sqrt); 30 | 31 | __device__ __forceinline__ half_t fabs(half_t value) { return __habs(value); } 32 | __device__ __forceinline__ half_t isinf(half_t value) { return __isinf(value); } 33 | __device__ __forceinline__ half_t round(half_t value) { return hrint(value); } 34 | 35 | #undef RPU_DEFINE_HALF_FUN 36 | #endif 37 | #endif 38 | -------------------------------------------------------------------------------- /cmake/dependencies_cuda.cmake: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) 6 | 7 | # CUDA 8 | if(USE_CUDA) 9 | enable_language(CUDA) 10 | 11 | # CUDA Toolkit 12 | find_package(CUDAToolkit) 13 | 14 | # CUB (Only for CUDA v. less than 11) 15 | if(${CUDAToolkit_VERSION_MAJOR} LESS 11) 16 | find_package(CUB QUIET) 17 | if(CUB_FOUND) 18 | include_directories(SYSTEM ${CUB_INCLUDE_DIRS}) 19 | else() 20 | include(ExternalProject) 21 | ExternalProject_Add(cub 22 | URL https://github.com/NVlabs/cub/archive/1.8.0.zip 23 | URL_HASH MD5=a821b9dffbc9d1bacf1c8db2a59094bf 24 | GIT_TAG origin/release/1.2.3 25 | CONFIGURE_COMMAND "" 26 | BUILD_COMMAND "" 27 | INSTALL_COMMAND "" 28 | ) 29 | 30 | ExternalProject_Get_Property(cub source_dir) 31 | set(CUB_INCLUDE_DIR ${source_dir}) 32 | include_directories(SYSTEM ${CUB_INCLUDE_DIR}) 33 | endif(CUB_FOUND) 34 | endif() 35 | 36 | include_directories(SYSTEM ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}) 37 | 38 | add_compile_definitions(RPU_USE_CUDA) 39 | 40 | endif() 41 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/test_helper.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | namespace RPU { 10 | 11 | namespace test_helper { 12 | 13 | template 14 | int debugKernelUpdateW( 15 | T *weights, 16 | uint32_t *x_counts, 17 | int x_size, 18 | uint32_t *d_counts, 19 | int d_size, 20 | int nK32, 21 | T dw_min, 22 | T dw_min_std, 23 | T bound, 24 | T *timings); 25 | 26 | template 27 | int debugKernelUpdateWBatch( 28 | T *weights, 29 | uint32_t *x_counts, 30 | int x_size, 31 | uint32_t *d_counts, 32 | int d_size, 33 | int nK32, 34 | int m_batch, 35 | bool trans, 36 | T dw_min, 37 | T dw_min_std, 38 | T bound, 39 | int kernel_type, 40 | T *timings); 41 | 42 | template 43 | int debugKernelUpdateWBatchShared( 44 | T *weights, 45 | uint32_t *x_counts, 46 | int x_size, 47 | uint32_t *d_counts, 48 | int d_size, 49 | int K, 50 | int m_batch, 51 | bool trans, 52 | T dw_min, 53 | T dw_min_std, 54 | T bound, 55 | int kernel_type, 56 | T *timings); 57 | } // namespace test_helper 58 | } // namespace RPU 59 | // namespace RPU 60 | -------------------------------------------------------------------------------- /notebooks/iscas_tutorial/README.md: -------------------------------------------------------------------------------- 1 | ## Interactive Task 2 | Deployment of a HWA Trained Network to Fusion: 3 | 1. Go to https://aihw-composer.draco.res.ibm.com and create an account. 4 | 2. Create a New experiment -> Inference experiment -> Real Chip with Program your own Neural Network Weights. 5 | 3. Download the ResNet-9 pretrained network checkpoint: https://aihwkit-tutorial.s3.us-east.cloud-object-storage.appdomain.cloud/resnet9s.th. 6 | 4. Load the example Jupyter Notebook from the website https://github.com/IBM/aihwkit/blob/master/notebooks/analog_fusion.ipynb. 7 | 5. Modify cell [5] to return the ResNet-9 network model definition from example 30: https://github.com/IBM/aihwkit/blob/master/examples/30_external_hardware_aware_model.py. 8 | 6. Modify cell [8] to load CIFAR-10 test dataset from torchvision datasets (code from example 30). 9 | 7. Modify cell [11] to prepare the ResNet-9 model by loading the checkpoint state dictionaries and convert it to analog (code from example 30). 10 | 8. Create the 'resnet9s_target_weights.csv' file of conductance values to be uploaded on the composer website. 11 | 9. Program the conductance values on Fusion chip using the analog composer, run the experiment, and download the resulting csv file of programmed conductance values 12 | 10. Finally, use the fusion_import utility to set the model weights to the conductance values read from the chip and evaluate the model. 13 | -------------------------------------------------------------------------------- /examples/10_plot_presets.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 10: plotting of presets. 8 | 9 | Plot the step response of different preset devices and preset configurations. 10 | """ 11 | # pylint: disable=invalid-name 12 | 13 | import matplotlib.pyplot as plt 14 | 15 | from aihwkit.utils.visualization import plot_device 16 | 17 | from aihwkit.simulator.presets import ( 18 | ReRamSBPresetDevice, 19 | ReRamESPresetDevice, 20 | CapacitorPresetDevice, 21 | EcRamPresetDevice, 22 | IdealizedPresetDevice, 23 | EcRamMOPresetDevice, 24 | PCMPresetUnitCell, 25 | ) 26 | 27 | 28 | plt.ion() 29 | 30 | # Note alternatively one can use plot_device_compact for a more compact 31 | # plot. 32 | 33 | # Idealized 34 | plot_device(IdealizedPresetDevice(), n_steps=10000) 35 | 36 | # ReRam based on ExpStep 37 | plot_device(ReRamESPresetDevice(), n_steps=1000) 38 | 39 | # ReRam based on SoftBounds 40 | plot_device(ReRamSBPresetDevice(), n_steps=1000) 41 | 42 | # Capacitor 43 | plot_device(CapacitorPresetDevice(), n_steps=400) 44 | 45 | # ECRAM 46 | plot_device(EcRamPresetDevice(), n_steps=1000) 47 | 48 | # Mo-ECRAM 49 | plot_device(EcRamMOPresetDevice(), n_steps=8000) 50 | 51 | # PCM 52 | plot_device(PCMPresetUnitCell(), n_steps=80) 53 | 54 | plt.show() 55 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/weight_modifier_cuda.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "cuda_util.h" 10 | #include "maximizer.h" 11 | #include "weight_modifier.h" 12 | 13 | namespace RPU { 14 | 15 | template class WeightModifierCuda { 16 | 17 | public: 18 | explicit WeightModifierCuda(CudaContextPtr context, int x_size, int d_size); 19 | WeightModifierCuda() {}; 20 | 21 | void apply(T *new_weights, const T *weights, const WeightModifierParameter &wmpar); 22 | 23 | inline bool enableDuringTest() { return enable_during_test_; }; 24 | 25 | void dumpExtra(RPU::state_t &extra, const std::string prefix); 26 | void loadExtra(const RPU::state_t &extra, const std::string prefix, bool strict); 27 | 28 | private: 29 | CudaContextPtr context_ = nullptr; 30 | int x_size_ = 0; 31 | int d_size_ = 0; 32 | int size_ = 0; 33 | bool enable_during_test_ = false; 34 | // no need to copy 35 | std::unique_ptr> amaximizer_ = nullptr; 36 | std::unique_ptr> row_amaximizer_ = nullptr; 37 | std::unique_ptr> row_maximizer_ = nullptr; 38 | std::unique_ptr> row_minimizer_ = nullptr; 39 | std::vector coeffs_; 40 | std::unique_ptr> dev_coeffs_ = nullptr; 41 | }; 42 | 43 | } // namespace RPU 44 | -------------------------------------------------------------------------------- /src/aihwkit/exceptions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Custom Exceptions for aihwkit.""" 8 | 9 | 10 | class AihwkitException(Exception): 11 | """Base class for exceptions related to aihwkit.""" 12 | 13 | 14 | class ModuleError(AihwkitException): 15 | """Exceptions related to analog neural network modules.""" 16 | 17 | 18 | class TileError(AihwkitException): 19 | """Exceptions related to analog tiles.""" 20 | 21 | 22 | class TileModuleError(TileError): 23 | """Exceptions related to analog tile modules.""" 24 | 25 | 26 | class ArgumentError(AihwkitException): 27 | """Exceptions related to wrong arguments.""" 28 | 29 | 30 | class CudaError(AihwkitException): 31 | """Exceptions related to CUDA.""" 32 | 33 | 34 | class ConfigError(AihwkitException): 35 | """Exceptions related to tile configuration.""" 36 | 37 | 38 | class AnalogBiasConfigError(ConfigError): 39 | """Exception that analog bias is wrongly set.""" 40 | 41 | 42 | class TorchTileConfigError(ConfigError): 43 | """Exceptions related to torch tile configuration.""" 44 | 45 | 46 | class CloudError(AihwkitException): 47 | """Exceptions related to the cloud functionality.""" 48 | 49 | 50 | class FusionExportError(CloudError): 51 | """Exceptions related to the fusion export functionality.""" 52 | -------------------------------------------------------------------------------- /docs/source/roadmap.rst: -------------------------------------------------------------------------------- 1 | Project roadmap 2 | =============== 3 | 4 | You are one of the early users of the *IBM Analog Hardware Acceleration Kit*. 5 | The initial releases have been focused on releasing a basic PyTorch 6 | integration for exploring selected features of the analog devices simulator, 7 | and set the basis that will be extended and improved upon: 8 | 9 | * integration of more simulator features in the PyTorch interface 10 | * tools to improve inference accuracy by converting pre-trained models 11 | with hardware-aware training 12 | * algorithmic tools to improve training accuracy by compensating for material 13 | short-comings 14 | * additional analog neural network layers 15 | * additional analog optimizers 16 | * custom network architectures and dataset/model zoos 17 | * integration with the cloud 18 | * hardware demonstrators 19 | 20 | This document will be updated with more details as the roadmap for the project 21 | evolves. As a companion, please refer to the `Issues`_ tab in the repository 22 | for more in-depth details about the status of the implementation of the 23 | different features and a sneak peek into the next release. 24 | 25 | We have an ambitious plan to incrementally bring new simulation and hardware 26 | features to our users, but we are eager to hear your feedback on the features 27 | of value for your work. Please contact us at ``aihwkit@us.ibm.com`` for any 28 | feedback or information. 29 | 30 | .. _Issues: https://github.com/IBM/aihwkit/issues 31 | -------------------------------------------------------------------------------- /src/rpucuda/rpu_constantstep_device.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "rng.h" 10 | #include "rpu_pulsed_device.h" 11 | #include "utility_functions.h" 12 | 13 | namespace RPU { 14 | 15 | template class ConstantStepRPUDevice; 16 | 17 | BUILD_PULSED_DEVICE_META_PARAMETER( 18 | ConstantStep, 19 | /*implements*/ 20 | DeviceUpdateType::ConstantStep, 21 | /*parameter def*/ 22 | , 23 | /*print body*/ 24 | , 25 | /* calc weight granularity body */ 26 | return this->dw_min; 27 | , 28 | /*add*/ 29 | ); 30 | 31 | template class ConstantStepRPUDevice : public PulsedRPUDevice { 32 | 33 | BUILD_PULSED_DEVICE_CONSTRUCTORS( 34 | ConstantStepRPUDevice, 35 | /* ctor*/ 36 | , 37 | /* dtor*/ 38 | , 39 | /* copy */ 40 | , 41 | /* move assignment */ 42 | , 43 | /* swap*/ 44 | , 45 | /* dp names*/ 46 | , 47 | /* dp2vec body*/ 48 | , 49 | /* vec2dp body*/ 50 | , 51 | /*invert copy DP */ 52 | ); 53 | 54 | void doSparseUpdate( 55 | T **weights, int i, const int *x_signed_indices, int x_count, int d_sign, RNG *rng) 56 | override; 57 | void doDenseUpdate(T **weights, int *coincidences, RNG *rng) override; 58 | }; 59 | } // namespace RPU 60 | -------------------------------------------------------------------------------- /examples/19_analog_summary_lenet.py: -------------------------------------------------------------------------------- 1 | """aihwkit example 19: Analog summary on LeNet. 2 | 3 | Extracts analog information in the form of a printed table. 4 | The information can also be accessed via the returned AnalogInfo object. 5 | """ 6 | 7 | # pylint: disable=invalid-name 8 | 9 | # Imports from PyTorch. 10 | from torch import nn 11 | 12 | # Imports from aihwkit. 13 | from aihwkit.nn.conversion import convert_to_analog_mapped 14 | from aihwkit.simulator.configs import SingleRPUConfig, ConstantStepDevice 15 | from aihwkit.utils.analog_info import analog_summary 16 | 17 | # Define a single-layer network, using a constant step device type. 18 | rpu_config = SingleRPUConfig(device=ConstantStepDevice()) 19 | rpu_config.mapping.max_input_size = 256 20 | rpu_config.mapping.max_output_size = 256 21 | 22 | 23 | channel = [16, 32, 512, 128] 24 | model = nn.Sequential( 25 | nn.Conv2d(in_channels=1, out_channels=channel[0], kernel_size=5, stride=1), 26 | nn.Tanh(), 27 | nn.MaxPool2d(kernel_size=2), 28 | nn.Conv2d(in_channels=channel[0], out_channels=channel[1], kernel_size=5, stride=1), 29 | nn.Tanh(), 30 | nn.MaxPool2d(kernel_size=2), 31 | nn.Tanh(), 32 | nn.Flatten(), 33 | nn.Linear(in_features=channel[2], out_features=channel[3]), 34 | nn.Tanh(), 35 | nn.Linear(in_features=channel[3], out_features=10), 36 | nn.LogSoftmax(dim=1), 37 | ) 38 | 39 | analog_model = convert_to_analog_mapped(model, rpu_config=rpu_config) 40 | 41 | analog_summary(analog_model, (1, 1, 28, 28)) 42 | -------------------------------------------------------------------------------- /examples/29_linalg_krylov.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | # pylint: disable=invalid-name 8 | 9 | """Krylov with analog preconditioner""" 10 | 11 | import numpy as np 12 | import matplotlib.pyplot as plt 13 | from torch import device as torch_device 14 | 15 | from pyamg.gallery import poisson 16 | from pyamg.krylov import fgmres 17 | from pyamg.util.linalg import norm 18 | from pyamg import smoothed_aggregation_solver 19 | 20 | from aihwkit.linalg import AnalogMatrix 21 | from aihwkit.simulator.presets import ReRamSBPreset 22 | from aihwkit.simulator.rpu_base import cuda 23 | 24 | 25 | # Check GPU device 26 | DEVICE = torch_device("cuda" if cuda.is_compiled() else "cpu") 27 | 28 | # config the hardware properties 29 | rpu_config = ReRamSBPreset() 30 | 31 | A = poisson((10, 10)).astype("float32") 32 | n = A.shape[0] 33 | b = np.ones((n,)).astype("float32") 34 | E = np.eye(n).astype("float32") 35 | 36 | ml = smoothed_aggregation_solver(A) 37 | M_fp = ml.aspreconditioner() * E 38 | 39 | M = AnalogMatrix(M_fp, rpu_config=rpu_config, realistic=False, device=DEVICE) 40 | 41 | (x, flag) = fgmres(A, b, M=M, maxiter=6, tol=1e-8) 42 | print(f"{norm(b - A*x):.6}") 43 | 44 | plt.clf() 45 | 46 | plt.subplot(1, 2, 1) 47 | plt.imshow(M @ E) 48 | plt.title("Analog pre-conditioner") 49 | 50 | plt.subplot(1, 2, 2) 51 | plt.imshow(M_fp) 52 | plt.title("Digital pre-conditioner") 53 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/cuda_buffer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | #include 9 | #include 10 | 11 | #if defined RPU_TORCH_CUDA_BUFFERS 12 | #include 13 | #define RPU_CUB_NS_QUALIFIER ::cub:: 14 | #endif 15 | 16 | namespace RPU { 17 | 18 | class CudaContext; 19 | typedef CudaContext *CudaContextPtr; 20 | template class CudaArray; 21 | 22 | template class CudaBuffer { 23 | public: 24 | CudaBuffer() {}; 25 | CudaBuffer(const CudaBuffer &); 26 | CudaBuffer &operator=(const CudaBuffer &); 27 | CudaBuffer(CudaBuffer &&); 28 | CudaBuffer &operator=(CudaBuffer &&); 29 | ~CudaBuffer() = default; 30 | 31 | friend void swap(CudaBuffer &a, CudaBuffer &b) noexcept { 32 | using std::swap; 33 | const std::lock_guard locka(a.mutex_); 34 | const std::lock_guard lockb(b.mutex_); 35 | swap(a.buffer_, b.buffer_); 36 | #if defined RPU_TORCH_CUDA_BUFFERS 37 | swap(a.tmp_context_, b.tmp_context_); 38 | #endif 39 | } 40 | 41 | T *get(CudaContextPtr context, size_t size); 42 | void release(); 43 | 44 | void print(size_t size) const; 45 | 46 | private: 47 | #if defined RPU_TORCH_CUDA_BUFFERS 48 | at::Tensor buffer_; 49 | CudaContextPtr tmp_context_ = nullptr; 50 | #else 51 | std::unique_ptr> buffer_ = nullptr; 52 | #endif 53 | std::recursive_mutex mutex_; 54 | }; 55 | } // namespace RPU 56 | -------------------------------------------------------------------------------- /examples/23_using_analog_tile_as_matrix.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 22: Simple example of how to use an analog tile as a matrix""" 8 | # pylint: disable=invalid-name 9 | # pylint: disable=too-many-locals 10 | 11 | # Imports from PyTorch. 12 | from torch import randn 13 | from torch import device as torch_device 14 | 15 | # Imports from aihwkit. 16 | from aihwkit.linalg.matrix import AnalogMatrix 17 | from aihwkit.simulator.presets import ReRamSBPreset 18 | from aihwkit.simulator.rpu_base import cuda 19 | 20 | # Check GPU device 21 | DEVICE = torch_device("cuda" if cuda.is_compiled() else "cpu") 22 | 23 | # config the hardware properties 24 | rpu_config = ReRamSBPreset() 25 | # rpu_config.forward.out_noise = 0.1 # set some properties 26 | 27 | # size of matrix 28 | n = 50 29 | m = 200 30 | t = 10 31 | matrix = 0.1 * randn(n, m) # matrix A 32 | 33 | x_values = randn(t, m) 34 | d_values = randn(t, n) 35 | 36 | # create analog tile (a single crossbar array) 37 | analog_matrix = AnalogMatrix(matrix, rpu_config, device=DEVICE) 38 | 39 | if DEVICE.type == "cuda": 40 | x_values = x_values.cuda(DEVICE) 41 | d_values = d_values.cuda(DEVICE) 42 | 43 | # compute matrix vector product (A * x) 44 | y1 = analog_matrix @ x_values 45 | 46 | # compute transposed matrix vector product (A' * d) 47 | y2 = d_values @ analog_matrix 48 | 49 | # compute rank update A += - lr * x * d' 50 | analog_matrix.ger(x_values, d_values, 0.01) 51 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/presets/compounds.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Compound configurations presets for resistive processing units.""" 8 | 9 | # pylint: disable=too-many-instance-attributes 10 | from typing import List 11 | from dataclasses import dataclass, field 12 | 13 | from aihwkit.simulator.configs.compounds import OneSidedUnitCell 14 | from aihwkit.simulator.parameters.training import UpdateParameters 15 | from aihwkit.simulator.parameters.io import IOParameters 16 | from aihwkit.simulator.presets.devices import PCMPresetDevice 17 | from aihwkit.simulator.presets.utils import PresetIOParameters, PresetUpdateParameters 18 | 19 | 20 | @dataclass 21 | class PCMPresetUnitCell(OneSidedUnitCell): 22 | """A unit cell that is comprised of two uni-directional PCM devices of 23 | opposite sign (see :class:`~PCMPresetDevice`). 24 | 25 | Check for refresh is performed after each mini-batch update. See 26 | :class:`~aihwkit.simulator.configs.device.OneSidedUnitCell` for 27 | details on the refresh implementation. 28 | """ 29 | 30 | unit_cell_devices: List = field(default_factory=lambda: [PCMPresetDevice(), PCMPresetDevice()]) 31 | 32 | refresh_every: int = 1 33 | units_in_mbatch: bool = True 34 | refresh_forward: IOParameters = field(default_factory=PresetIOParameters) 35 | refresh_update: UpdateParameters = field( 36 | default_factory=lambda: PresetUpdateParameters(desired_bl=31) 37 | ) 38 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | .PHONY: build_inplace clean clean-doc clang-format mypy pycodestyle pylint pytest build_inplace_mkl build_inplace_cuda build_cuda 8 | 9 | build_inplace: 10 | python setup.py build_ext -j8 -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE --inplace ${flags} 11 | 12 | build_inplace_mkl: 13 | make build_inplace flags="-DRPU_BLAS=MKL -DINTEL_MKL_DIR=${MKLROOT} ${flags}" 14 | 15 | build: 16 | python setup.py install --user -j8 -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE ${flags} 17 | 18 | build_mkl: 19 | make build flags="-DRPU_BLAS=MKL -DINTEL_MKL_DIR=${MKLROOT} ${flags}" 20 | 21 | build_cuda: 22 | make build_mkl flags="-DUSE_CUDA=ON ${flags}" 23 | 24 | build_inplace_cuda: 25 | make build_inplace_mkl flags="-DUSE_CUDA=ON ${flags}" 26 | 27 | clean: 28 | python setup.py clean 29 | rm -rf _skbuild 30 | rm -f src/aihwkit/simulator/rpu_base*.so 31 | rm -f src/aihwkit/extension/*.so 32 | 33 | clean-doc: 34 | cd docs && make clean 35 | 36 | clang-format: 37 | git ls-files | grep -E ".*\.(cu|h|cpp)$$" | xargs clang-format -i 38 | 39 | doc: 40 | cd docs && make html 41 | 42 | mypy: 43 | mypy --show-error-codes src/ 44 | 45 | pycodestyle: 46 | pycodestyle src/ tests/ examples/ 47 | 48 | pylint: 49 | PYTHONPATH=src/ git ls-files | grep -E ".*\.py$$" | grep -v "pb2\.py$$" | xargs pylint -rn 50 | 51 | pytest: 52 | PYTHONPATH=src/ pytest -v -s tests/ 53 | 54 | black: 55 | git ls-files | grep \.py$$ | xargs black -t py310 -C --config .black 56 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/definitions/output_file_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: output_file.proto 4 | """Generated protocol buffer code.""" 5 | from google.protobuf.internal import builder as _builder 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | from . import onnx_common_pb2 as onnx__common__pb2 16 | from . import common_pb2 as common__pb2 17 | 18 | 19 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( 20 | b'\n\x11output_file.proto\x12\x05\x61ihwx\x1a\x11onnx_common.proto\x1a\x0c\x63ommon.proto"I\n\x10\x45pochInformation\x12\r\n\x05\x65poch\x18\x01 \x02(\x05\x12&\n\x07metrics\x18\x02 \x03(\x0b\x32\x15.aihwx.AttributeProto"\xa3\x01\n\x0eTrainingOutput\x12\x1f\n\x07version\x18\x01 \x02(\x0b\x32\x0e.aihwx.Version\x12\x1f\n\x07network\x18\x02 \x02(\x0b\x32\x0e.aihwx.Network\x12\'\n\x06\x65pochs\x18\x03 \x03(\x0b\x32\x17.aihwx.EpochInformation\x12&\n\x07metrics\x18\x04 \x03(\x0b\x32\x15.aihwx.AttributeProto' 21 | ) 22 | 23 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) 24 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, "output_file_pb2", globals()) 25 | if _descriptor._USE_C_DESCRIPTORS == False: 26 | DESCRIPTOR._options = None 27 | _EPOCHINFORMATION._serialized_start = 61 28 | _EPOCHINFORMATION._serialized_end = 134 29 | _TRAININGOUTPUT._serialized_start = 137 30 | _TRAININGOUTPUT._serialized_end = 300 31 | # @@protoc_insertion_point(module_scope) 32 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/client/exceptions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Exceptions related to the cloud client.""" 8 | 9 | from urllib.parse import urlparse 10 | 11 | from requests import Response 12 | 13 | from aihwkit.exceptions import CloudError 14 | 15 | 16 | class ResponseError(CloudError): 17 | """Error retrieving a response.""" 18 | 19 | def __init__(self, response: Response): 20 | self.response = response 21 | self.url = self._sanitize_url(response.url) 22 | 23 | super().__init__(str(self)) 24 | 25 | def __str__(self) -> str: 26 | return "{} {} for url: {} {}".format( 27 | self.response.status_code, self.response.reason, self.response.request.method, self.url 28 | ) 29 | 30 | @staticmethod 31 | def _sanitize_url(url: str) -> str: 32 | """Remove sensitive parts from an url.""" 33 | return url 34 | 35 | 36 | class ApiResponseError(ResponseError): 37 | """Error retrieving a response (object storage).""" 38 | 39 | @staticmethod 40 | def _sanitize_url(url: str) -> str: 41 | """Remove sensitive parts from an url.""" 42 | parts = urlparse(url) 43 | 44 | return "{}{}".format(parts.path, "?..." if parts.query else "") 45 | 46 | 47 | class InvalidResponseFieldError(CloudError): 48 | """Invalid or unsupported field in the response.""" 49 | 50 | 51 | class ExperimentStatusError(CloudError): 52 | """Error dependent on to the Experiment status.""" 53 | 54 | 55 | class CredentialsError(CloudError): 56 | """Errors related to cloud credentials.""" 57 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/digital_low_precision/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 Qualcomm Technologies, Inc. 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted 6 | (subject to the limitations in the disclaimer below) provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions 9 | and the following disclaimer: 10 | 11 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions 12 | and the following disclaimer in the documentation and/or other materials provided with the 13 | istribution. 14 | 15 | * Neither the name of Qualcomm Technologies, Inc. nor the names of its contributors may be used to 16 | endorse or promote products derived from this software without specific prior written permission. 17 | 18 | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS 19 | SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 20 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 26 | THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /examples/17_resnet34_imagenet_conversion_to_analog.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 17: resnet34 CNN conversion to analog. 8 | 9 | resnet34 inspired network based on the paper: 10 | https://arxiv.org/abs/1512.03385 11 | """ 12 | # pylint: disable=invalid-name 13 | 14 | # Imports from PyTorch. 15 | from torchvision.models import resnet34 16 | 17 | # Imports from aihwkit. 18 | from aihwkit.nn.conversion import convert_to_analog 19 | from aihwkit.simulator.presets import TikiTakaReRamSBPreset 20 | from aihwkit.simulator.configs import MappingParameter 21 | 22 | # Example: Load a predefined model from pytorch library and convert to 23 | # its analog version. 24 | 25 | # Load a pytorch model. 26 | model = resnet34() 27 | print(model) 28 | 29 | # Define device and chip configuration used in the RPU tile 30 | mapping = MappingParameter( 31 | max_input_size=512, # analog tile size 32 | max_output_size=512, 33 | digital_bias=True, 34 | weight_scaling_omega=0.6, 35 | ) # whether to use analog or digital bias 36 | # Choose any preset or RPU configuration here 37 | rpu_config = TikiTakaReRamSBPreset(mapping=mapping) 38 | 39 | # Convert the model to its analog version. This will replace 40 | # ``Conv2d`` layers with ``AnalogConv2d`` (using simply unfolding for 41 | # convolutions) 42 | model = convert_to_analog(model, rpu_config) 43 | 44 | # Note: One can also use ``convert_to_analog_mapped`` instead to 45 | # convert e.g. ``Conv2d`` to ``AnalogConv2dMapped`` (using a special way to 46 | # unfold over multiple tiles in a more memory efficient way 47 | # for some analog tiles on GPU) 48 | 49 | print(model) 50 | -------------------------------------------------------------------------------- /examples/02_multiple_layer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details.. 6 | 7 | """aihwkit example 2: network with multiple layers. 8 | 9 | Network that consists of multiple analog layers. It aims to learn to sum all 10 | the elements from one array. 11 | """ 12 | # pylint: disable=invalid-name 13 | 14 | # Imports from PyTorch. 15 | from torch import Tensor 16 | from torch.nn.functional import mse_loss 17 | from torch.nn import Sequential 18 | 19 | # Imports from aihwkit. 20 | from aihwkit.nn import AnalogLinear 21 | from aihwkit.optim import AnalogSGD 22 | from aihwkit.simulator.configs import SingleRPUConfig, ConstantStepDevice 23 | 24 | # Prepare the datasets (input and expected output). 25 | x_b = Tensor([[0.1, 0.2, 0.0, 0.0], [0.2, 0.4, 0.0, 0.0]]) 26 | y_b = Tensor([[0.3], [0.6]]) 27 | 28 | # Define a multiple-layer network, using a constant step device type. 29 | model = Sequential( 30 | AnalogLinear(4, 2, rpu_config=SingleRPUConfig(device=ConstantStepDevice())), 31 | AnalogLinear(2, 2, rpu_config=SingleRPUConfig(device=ConstantStepDevice())), 32 | AnalogLinear(2, 1, rpu_config=SingleRPUConfig(device=ConstantStepDevice())), 33 | ) 34 | 35 | # Define an analog-aware optimizer, preparing it for using the layers. 36 | opt = AnalogSGD(model.parameters(), lr=0.5) 37 | opt.regroup_param_groups(model) 38 | 39 | for epoch in range(100): 40 | opt.zero_grad() 41 | 42 | # Add the training Tensor to the model (input). 43 | pred = model(x_b) 44 | # Add the expected output Tensor. 45 | loss = mse_loss(pred, y_b) 46 | # Run training (backward propagation). 47 | loss.backward() 48 | 49 | opt.step() 50 | 51 | print("Loss error: {:.16f}".format(loss)) 52 | -------------------------------------------------------------------------------- /src/aihwkit/inference/converter/base.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Base conductance converter for the phenomenological noise models for inference.""" 8 | 9 | from typing import Dict, List, Tuple 10 | 11 | from torch import Tensor 12 | from torch.autograd import no_grad 13 | 14 | 15 | class BaseConductanceConverter: 16 | """Base class for converting DNN weights into conductances.""" 17 | 18 | @no_grad() 19 | def convert_to_conductances(self, weights: Tensor) -> Tuple[List[Tensor], Dict]: 20 | """Convert a weight matrix into conductances. 21 | 22 | Caution: 23 | The conversion is assumed deterministic and repeatable. 24 | 25 | Args: 26 | weights: weight matrix tensor. 27 | 28 | Returns: 29 | Tuple of the list of conductance tensors and a params 30 | dictionary that is used for the reverse conversion. 31 | """ 32 | raise NotImplementedError 33 | 34 | @no_grad() 35 | def convert_back_to_weights(self, conductances: List[Tensor], params: Dict) -> Tensor: 36 | """Convert a matrix of conductances into weights. 37 | 38 | Caution: 39 | The conversion is assumed deterministic and repeatable. 40 | 41 | Args: 42 | conductances: list of conductance tensors representing a weight matrix 43 | params: param dictionary that was returned from the conversion 44 | 45 | Returns: 46 | weight matrix 47 | """ 48 | raise NotImplementedError 49 | 50 | def __eq__(self, other: object) -> bool: 51 | return self.__class__ == other.__class__ and self.__dict__ == other.__dict__ 52 | -------------------------------------------------------------------------------- /src/aihwkit/inference/compensation/base.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Base drift compensation for inference.""" 8 | 9 | from typing import Tuple 10 | 11 | from torch import Tensor 12 | from torch.autograd import no_grad 13 | 14 | 15 | class BaseDriftCompensation: 16 | """Base class for drift compensations.""" 17 | 18 | def __init__(self) -> None: 19 | pass 20 | 21 | @no_grad() 22 | def init_baseline(self, forward_output: Tensor) -> Tuple[Tensor, Tensor]: 23 | """Initialize the base line for applying the compensation. 24 | 25 | Uses a all one tensor for read_out. 26 | 27 | Args: 28 | forward_output: forward output of the read out vector to compensate 29 | 30 | Returns: 31 | reference tensor readout 32 | """ 33 | ref_value = self.readout(forward_output) 34 | 35 | return ref_value 36 | 37 | @no_grad() 38 | def get_readout_tensor(self, in_size: int) -> Tensor: 39 | """Return the read-out tensor. 40 | 41 | Called once during :meth:`~init_baseline`. 42 | """ 43 | raise NotImplementedError 44 | 45 | @no_grad() 46 | def readout(self, out_tensor: Tensor) -> Tensor: 47 | """Implement the read out math.""" 48 | raise NotImplementedError 49 | 50 | @no_grad() 51 | def apply(self, forward_output: Tensor, ref_value: Tensor) -> Tensor: 52 | """Read out the current value from the output of the forward 53 | pass and returns the drift compensation alpha scale.""" 54 | current_value = self.readout(forward_output) 55 | ratio = ref_value / current_value 56 | 57 | return ratio 58 | -------------------------------------------------------------------------------- /examples/01_simple_layer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 1: simple network with one layer. 8 | 9 | Simple network that consist of one analog layer. The network aims to learn 10 | to sum all the elements from one array. 11 | """ 12 | # pylint: disable=invalid-name 13 | 14 | # Imports from PyTorch. 15 | from torch import Tensor 16 | from torch.nn.functional import mse_loss 17 | 18 | # Imports from aihwkit. 19 | from aihwkit.nn import AnalogLinear 20 | from aihwkit.optim import AnalogSGD 21 | from aihwkit.simulator.configs import SingleRPUConfig, ConstantStepDevice 22 | from aihwkit.simulator.rpu_base import cuda 23 | 24 | # Prepare the datasets (input and expected output). 25 | x = Tensor([[0.1, 0.2, 0.4, 0.3], [0.2, 0.1, 0.1, 0.3]]) 26 | y = Tensor([[1.0, 0.5], [0.7, 0.3]]) 27 | 28 | # Define a single-layer network, using a constant step device type. 29 | rpu_config = SingleRPUConfig(device=ConstantStepDevice()) 30 | 31 | model = AnalogLinear(4, 2, bias=True, rpu_config=rpu_config) 32 | 33 | # Move the model and tensors to cuda if it is available. 34 | if cuda.is_compiled(): 35 | x = x.cuda() 36 | y = y.cuda() 37 | model = model.cuda() 38 | 39 | # Define an analog-aware optimizer, preparing it for using the layers. 40 | opt = AnalogSGD(model.parameters(), lr=0.1) 41 | opt.regroup_param_groups(model) 42 | 43 | for epoch in range(100): 44 | # Delete old gradient 45 | opt.zero_grad() 46 | # Add the training Tensor to the model (input). 47 | pred = model(x) 48 | # Add the expected output Tensor. 49 | loss = mse_loss(pred, y) 50 | # Run training (backward propagation). 51 | loss.backward() 52 | 53 | opt.step() 54 | 55 | print("Loss error: {:.16f}".format(loss)) 56 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/rpucuda_constantstep_device.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "pwu_kernel_parameter_base.h" 10 | #include "rpu_constantstep_device.h" 11 | #include "rpucuda_pulsed_device.h" 12 | #include 13 | 14 | namespace RPU { 15 | 16 | /* ConstantStepRPUDeviceCuda 17 | 18 | Implements the ConstantStep update, that is the step-size per 19 | pulse (dw) does not depend on the actual weight value (w). 20 | 21 | For smaller noise, we assume that the cycle-to-cycle 22 | noise times the weight update minimal step (dw) remains 23 | consistent with the direction of the update. E.g. if the update 24 | is supposed to be positive, dw + noise is assumed to be positive 25 | as well. 26 | 27 | If noise setting is too large, so that dw+noise signs changes 28 | become likely during multiple steps in the same direction, we use 29 | a slightly slower implementation, where the bounds are checked 30 | for each weight change. 31 | 32 | */ 33 | template class ConstantStepRPUDeviceCuda : public PulsedRPUDeviceCuda { 34 | 35 | public: 36 | BUILD_PULSED_DEVICE_CONSTRUCTORS_CUDA( 37 | ConstantStepRPUDeviceCuda, 38 | ConstantStepRPUDevice, 39 | /*ctor body*/ 40 | , 41 | /*dtor body*/ 42 | , 43 | /*copy body*/ 44 | , 45 | /*move assigment body*/ 46 | , 47 | /*swap body*/ 48 | , 49 | /*host copy from cpu (rpu_device). Parent device params are copyied automatically*/ 50 | ) 51 | 52 | pwukpvec_t getUpdateKernels( 53 | int m_batch, 54 | int nK32, 55 | int use_bo64, 56 | bool out_trans, 57 | const PulsedUpdateMetaParameter &up) override; 58 | }; 59 | 60 | } // namespace RPU 61 | -------------------------------------------------------------------------------- /src/aihwkit/inference/compensation/drift.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Global drift compensation for inference.""" 8 | 9 | from torch.autograd import no_grad 10 | from torch import abs as torch_abs 11 | from torch import clamp, Tensor, eye 12 | 13 | from aihwkit.inference.compensation.base import BaseDriftCompensation 14 | 15 | 16 | class GlobalDriftCompensation(BaseDriftCompensation): 17 | """Global drift compensation. 18 | 19 | Uses a constant factor for compensating the drift. 20 | """ 21 | 22 | @no_grad() 23 | def readout(self, out_tensor: Tensor) -> Tensor: 24 | """Read outs the mean abs.""" 25 | return clamp(torch_abs(out_tensor).mean(), min=0.0001) 26 | 27 | @no_grad() 28 | def get_readout_tensor(self, in_size: int) -> Tensor: 29 | """Return the read-out tensor. 30 | 31 | Uses the set of one-hot vectors (eye). 32 | """ 33 | return eye(in_size) 34 | 35 | def __str__(self) -> str: 36 | return "{}()".format(self.__class__.__name__) 37 | 38 | 39 | class PerColumnDriftCompensation(BaseDriftCompensation): 40 | """Per column drift compensation. 41 | Uses a vector for compensating the drift. 42 | """ 43 | 44 | @no_grad() 45 | def readout(self, out_tensor: Tensor) -> Tensor: 46 | """Read outs the per-column mean abs.""" 47 | return clamp(torch_abs(out_tensor).mean(dim=0), min=0.0001) 48 | 49 | @no_grad() 50 | def get_readout_tensor(self, in_size: int) -> Tensor: 51 | """Return the read-out tensor. 52 | Uses the set of one-hot vectors (eye). 53 | """ 54 | return eye(in_size) 55 | 56 | def __str__(self) -> str: 57 | return "{}()".format(self.__class__.__name__) 58 | -------------------------------------------------------------------------------- /docs/source/pros_cons.rst: -------------------------------------------------------------------------------- 1 | Advantages and Challenges 2 | ========================= 3 | Why is computation in analog interesting for AI acceleration. There are several advantages and challenges. 4 | First, due to the parallel in-memory compute, the matrix vector product is very power efficient as the weights data does not need to 5 | be fetched from memory for each operation. Second, because the analog matrix vector product is performed using the laws of electro-physics, 6 | it is computed in constant time. This implies that a large weight matrix needs in first approximations the same time to compute the matrix 7 | vector product as when using a smaller matrix. This is not the case for conventional digital MAC operations, where the time to compute 8 | the matrix vector product typically scales with the number of matrix elements as illustrated in the figure below. 9 | 10 | 11 | 12 | .. image:: ../img/analog_mac_time.png 13 | :alt: MAC Operations in Analog vs. Digital 14 | 15 | With all these great advantages, comes also key challenges for analog AI accelerators. Most importantly, the analog vector product is not exact. 16 | There are many possible noise sources and non-idealities. For instance, as shown in the graph below, repeating the same matrix vector product twice, 17 | will result in a slightly different outcome. This is the main motivation behind developing a flexible simulation tool for Analog accelerators as there 18 | is a need to investigate whether we can achieve acceptable accuracies when we accelerate important AI workloads with these future Analog chips. 19 | Additionally, a simulation tool that simulates the expected noise sources of the analog compute, will also help to develop new algorithms or error 20 | compensation methods to improve the accuracy and reduce any potential impact of the expected non-idealities of Analog compute. 21 | 22 | .. image:: ../img/analog_non_idealities.png 23 | :alt: MAC Operations in Analog vs. Digital -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/definitions/input_file_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: input_file.proto 4 | """Generated protocol buffer code.""" 5 | from google.protobuf.internal import builder as _builder 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | from . import common_pb2 as common__pb2 16 | 17 | 18 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( 19 | b'\n\x10input_file.proto\x12\x05\x61ihwx\x1a\x0c\x63ommon.proto"\\\n\x07\x44\x61taset\x12\x12\n\ndataset_id\x18\x01 \x02(\t\x12)\n\ntransforms\x18\x02 \x03(\x0b\x32\x15.aihwx.TransformProto\x12\x12\n\nbatch_size\x18\x03 \x01(\x05"\x9f\x01\n\x08Training\x12(\n\toptimizer\x18\x01 \x02(\x0b\x32\x15.aihwx.OptimizerProto\x12(\n\tscheduler\x18\x02 \x01(\x0b\x32\x15.aihwx.SchedulerProto\x12/\n\rloss_function\x18\x03 \x01(\x0b\x32\x18.aihwx.LossFunctionProto\x12\x0e\n\x06\x65pochs\x18\x04 \x01(\x05"\x95\x01\n\rTrainingInput\x12\x1f\n\x07version\x18\x01 \x02(\x0b\x32\x0e.aihwx.Version\x12\x1f\n\x07\x64\x61taset\x18\x02 \x02(\x0b\x32\x0e.aihwx.Dataset\x12\x1f\n\x07network\x18\x03 \x02(\x0b\x32\x0e.aihwx.Network\x12!\n\x08training\x18\x04 \x02(\x0b\x32\x0f.aihwx.Training' 20 | ) 21 | 22 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) 23 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, "input_file_pb2", globals()) 24 | if _descriptor._USE_C_DESCRIPTORS == False: 25 | DESCRIPTOR._options = None 26 | _DATASET._serialized_start = 41 27 | _DATASET._serialized_end = 133 28 | _TRAINING._serialized_start = 136 29 | _TRAINING._serialized_end = 295 30 | _TRAININGINPUT._serialized_start = 298 31 | _TRAININGINPUT._serialized_end = 447 32 | # @@protoc_insertion_point(module_scope) 33 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/configs/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Configurations for resistive processing units.""" 8 | 9 | from aihwkit.simulator.parameters import ( 10 | IOParameters, 11 | IOParametersIRDropT, 12 | UpdateParameters, 13 | WeightModifierParameter, 14 | WeightClipParameter, 15 | WeightRemapParameter, 16 | SimpleDriftParameter, 17 | DriftParameter, 18 | MappingParameter, 19 | InputRangeParameter, 20 | PrePostProcessingParameter, 21 | ) 22 | from aihwkit.simulator.parameters.enums import ( 23 | RPUDataType, 24 | BoundManagementType, 25 | NoiseManagementType, 26 | WeightNoiseType, 27 | PulseType, 28 | WeightModifierType, 29 | WeightClipType, 30 | WeightRemapType, 31 | VectorUnitCellUpdatePolicy, 32 | AnalogMVType, 33 | CountLRFeedbackPolicy, 34 | ) 35 | from .devices import ( 36 | FloatingPointDevice, 37 | IdealDevice, 38 | ConstantStepDevice, 39 | LinearStepDevice, 40 | SoftBoundsDevice, 41 | SoftBoundsPmaxDevice, 42 | SoftBoundsReferenceDevice, 43 | ExpStepDevice, 44 | PowStepDevice, 45 | PowStepReferenceDevice, 46 | PiecewiseStepDevice, 47 | ) 48 | from .compounds import ( 49 | VectorUnitCell, 50 | ReferenceUnitCell, 51 | OneSidedUnitCell, 52 | DifferenceUnitCell, 53 | TransferCompound, 54 | BufferedTransferCompound, 55 | ChoppedTransferCompound, 56 | DynamicTransferCompound, 57 | MixedPrecisionCompound, 58 | ) 59 | from .configs import ( 60 | FloatingPointRPUConfig, 61 | InferenceRPUConfig, 62 | SingleRPUConfig, 63 | UnitCellRPUConfig, 64 | DigitalRankUpdateRPUConfig, 65 | TorchInferenceRPUConfig, 66 | TorchInferenceRPUConfigIRDropT, 67 | QuantizedTorchInferenceRPUConfig, 68 | ) 69 | 70 | from .helpers import build_config 71 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/weight_drifter_cuda.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "cuda_util.h" 10 | #include "weight_drifter.h" 11 | 12 | namespace RPU { 13 | 14 | template class WeightDrifterCuda { 15 | 16 | public: 17 | explicit WeightDrifterCuda(CudaContextPtr context, int size); 18 | explicit WeightDrifterCuda(CudaContextPtr, const WeightDrifter &wd, int x_size, int d_size); 19 | WeightDrifterCuda() {}; 20 | virtual ~WeightDrifterCuda() = default; 21 | 22 | WeightDrifterCuda(const WeightDrifterCuda &); // = default; 23 | WeightDrifterCuda &operator=(const WeightDrifterCuda &) = default; 24 | WeightDrifterCuda(WeightDrifterCuda &&) = default; 25 | WeightDrifterCuda &operator=(WeightDrifterCuda &&) = default; 26 | 27 | void apply(T *weights, T time_since_last_call); 28 | 29 | inline bool isActive() { return active_; }; 30 | 31 | void saturate(T *weights, param_t *dev_4params); 32 | const T *getNu() const { return dev_nu_ == nullptr ? nullptr : dev_nu_->getDataConst(); }; 33 | 34 | void dumpExtra(RPU::state_t &extra, const std::string prefix); 35 | void loadExtra(const RPU::state_t &extra, const std::string prefix, bool strict); 36 | 37 | protected: 38 | CudaContextPtr context_ = nullptr; 39 | int size_ = 0; 40 | int max_size_ = 0; 41 | bool active_ = false; 42 | T current_t_ = 0.0; 43 | 44 | DriftParameter par_; 45 | 46 | std::unique_ptr> dev_previous_weights_ = nullptr; 47 | std::unique_ptr> dev_w0_ = nullptr; 48 | std::unique_ptr> dev_t_ = nullptr; 49 | std::unique_ptr> dev_nu_ = nullptr; 50 | 51 | private: 52 | void initialize(const T *weights); 53 | void 54 | populateFrom(const WeightDrifter &wd, int x_size, int d_size); // called during construction 55 | }; 56 | 57 | } // namespace RPU 58 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/weight_remapper_cuda.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "cuda_util.h" 10 | #include "maximizer.h" 11 | #include "weight_remapper.h" 12 | 13 | namespace RPU { 14 | 15 | template class WeightRemapperCuda { 16 | 17 | public: 18 | explicit WeightRemapperCuda(CudaContextPtr context, int x_size, int d_size); 19 | WeightRemapperCuda() {}; 20 | 21 | void apply( 22 | T *weights, 23 | T current_lr, 24 | const WeightRemapParameter &wrmpar, 25 | T *scales, 26 | T *biases = nullptr, 27 | int *channel_exceded = nullptr); 28 | 29 | bool applySWA( 30 | T *swa_weights, 31 | T *weights, 32 | uint64_t iter, 33 | const WeightRemapParameter &wmpar, 34 | T current_lr, 35 | T *scales = nullptr, 36 | T *biases = nullptr, 37 | int *channel_exceded = nullptr); 38 | 39 | void dumpExtra(RPU::state_t &extra, const std::string prefix) {}; 40 | void loadExtra(const RPU::state_t &extra, const std::string prefix, bool strict) {}; 41 | 42 | private: 43 | CudaContextPtr context_ = nullptr; 44 | int x_size_ = 0; 45 | int d_size_ = 0; 46 | int size_ = 0; 47 | int max_size_ = 0; 48 | 49 | // no need to copy 50 | std::unique_ptr> amaximizer_ = nullptr; 51 | std::unique_ptr> row_amaximizer_ = nullptr; 52 | std::unique_ptr> row_maximizer_ = nullptr; 53 | std::unique_ptr> row_minimizer_ = nullptr; 54 | std::unique_ptr> scale_minimizer_ = nullptr; 55 | std::unique_ptr> bias_buffer_ = nullptr; 56 | std::unique_ptr> scale_buffer_ = nullptr; 57 | std::unique_ptr> msqr_values_ = nullptr; 58 | std::unique_ptr> dev_temp_storage_ = nullptr; 59 | 60 | int getNBlocks(int nthreads); 61 | }; 62 | 63 | } // namespace RPU 64 | -------------------------------------------------------------------------------- /src/aihwkit/extension/extension_src/aihwkit_extension.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #include "aihwkit_extension.h" 8 | #include "ops/float_prec_op.h" 9 | #include "ops/thevenin_equiv_op.h" 10 | 11 | namespace py = pybind11; 12 | 13 | PYBIND11_MODULE(aihwkit_extension, m) { 14 | m.doc() = "Bindings for the AIHWKIT extensions."; 15 | auto m_ops = m.def_submodule("ops"); 16 | 17 | m_ops.def( 18 | "float_precision_cast", &aihwkit::floatPrecisionCast, py::arg("x_input"), py::arg("exponent"), 19 | py::arg("mantissa"), py::arg("saturate_to_inf"), 20 | R"pbdoc( 21 | fake-casts FP32 number to variable mantissa / exponent. The underlying number format remains FP32 22 | 23 | Args: 24 | x_input: input tensor 25 | exponent: number of bits used for the exponent 26 | mantissa: number of bits used for the mantissa 27 | saturate_to_inf: whether to set it to infinity if saturated or to perform clipping 28 | 29 | Returns: 30 | y_output: fake casts tensor of the same size 31 | )pbdoc"); 32 | m_ops.def( 33 | "thevenin_equiv", &aihwkit::theveninEquiv, py::arg("x_input"), py::arg("gp_values"), 34 | py::arg("gm_values"), py::arg("r_s"), py::arg("t_max"), py::arg("time_steps"), 35 | R"pbdoc( 36 | Helper function for computing the thevenin equivalent for time based IR drop 37 | 38 | Args: 39 | x_input: input tensor 40 | gp_values: Positive conductance values (in muS) 41 | gm_values: Negative conductance values (in muS) 42 | r_s: Series resistance 43 | t_max: max time (in x units) 44 | time_steps: number of time steps 45 | 46 | Returns: 47 | y_output: [vth_rd, rht_3d] 48 | )pbdoc"); 49 | } 50 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/converter/definitions/i_output_file_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: i_output_file.proto 4 | """Generated protocol buffer code.""" 5 | from google.protobuf.internal import builder as _builder 6 | from google.protobuf import descriptor as _descriptor 7 | from google.protobuf import descriptor_pool as _descriptor_pool 8 | from google.protobuf import symbol_database as _symbol_database 9 | 10 | # @@protoc_insertion_point(imports) 11 | 12 | _sym_db = _symbol_database.Default() 13 | 14 | 15 | from . import i_common_pb2 as i__common__pb2 16 | 17 | 18 | DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile( 19 | b'\n\x13i_output_file.proto\x12\x11\x61ihwx.inferencing\x1a\x0ei_common.proto"}\n\x15InferenceResultsProto\x12\x13\n\x0bt_inference\x18\x01 \x02(\x02\x12\x14\n\x0c\x61vg_accuracy\x18\x02 \x02(\x02\x12\x14\n\x0cstd_accuracy\x18\x03 \x02(\x02\x12\x11\n\tavg_error\x18\x04 \x02(\x02\x12\x10\n\x08\x61vg_loss\x18\x05 \x02(\x02"\x9d\x01\n\x12InferenceRunsProto\x12\x18\n\x10inference_repeat\x18\x01 \x02(\x03\x12\x12\n\nis_partial\x18\x02 \x02(\x08\x12\x14\n\x0ctime_elapsed\x18\x03 \x02(\x02\x12\x43\n\x11inference_results\x18\x04 \x03(\x0b\x32(.aihwx.inferencing.InferenceResultsProto"\x7f\n\x11InferencingOutput\x12+\n\x07version\x18\x01 \x02(\x0b\x32\x1a.aihwx.inferencing.Version\x12=\n\x0einference_runs\x18\x02 \x02(\x0b\x32%.aihwx.inferencing.InferenceRunsProto' 20 | ) 21 | 22 | _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) 23 | _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, "i_output_file_pb2", globals()) 24 | if _descriptor._USE_C_DESCRIPTORS == False: 25 | DESCRIPTOR._options = None 26 | _INFERENCERESULTSPROTO._serialized_start = 58 27 | _INFERENCERESULTSPROTO._serialized_end = 183 28 | _INFERENCERUNSPROTO._serialized_start = 186 29 | _INFERENCERUNSPROTO._serialized_end = 343 30 | _INFERENCINGOUTPUT._serialized_start = 345 31 | _INFERENCINGOUTPUT._serialized_end = 472 32 | # @@protoc_insertion_point(module_scope) 33 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/maximizer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "cuda_util.h" 10 | 11 | namespace RPU { 12 | 13 | struct CustomMaxAbs { 14 | template __device__ __forceinline__ T operator()(const T &a, const T &b) const { 15 | T bval = b >= (T)0.0 ? b : -b; 16 | T aval = a >= (T)0.0 ? a : -a; 17 | return (bval > aval) ? bval : aval; 18 | } 19 | }; 20 | 21 | template class Maximizer { 22 | 23 | public: 24 | explicit Maximizer(CudaContextPtr c, int size, bool abs_if = true); 25 | 26 | /* computes the max/amax values */ 27 | template 28 | void compute(InputIteratorT dev_input, int m_batch = 1, bool trans = false); 29 | 30 | /* sets the computed max values to zero below thres. Caution: This 31 | is in-place. does not check whether compute was called. */ 32 | void setZeroBelow(T thres); 33 | void saturateAbove(T thres); 34 | 35 | inline T *getMaxValues() { return dev_max_values_->getData(); }; 36 | inline void copyMaxValuesToHost(T *dest) { dev_max_values_->copyTo(dest); }; 37 | 38 | void printMaxValues() { dev_max_values_->printValues(); }; 39 | 40 | private: 41 | void initializeBatchBuffer(int m_batch); 42 | 43 | int buffer_m_batch_ = 0; 44 | int size_ = 0; 45 | CudaContextPtr context_ = nullptr; 46 | 47 | CustomMaxAbs max_abs_op_; 48 | bool abs_if_ = true; 49 | 50 | std::unique_ptr> dev_max_values_ = nullptr; 51 | std::unique_ptr> dev_max_values0_ = nullptr; 52 | std::unique_ptr> dev_offsets_ = nullptr; 53 | 54 | std::unique_ptr> dev_v_temp_storage_ = nullptr; 55 | std::unique_ptr> dev_m_temp_storage_ = nullptr; 56 | }; 57 | 58 | namespace test_helper { 59 | template 60 | void debugMaxBatched(const T *indata, int size, int m_batch, bool trans, T *max_values); 61 | 62 | } 63 | 64 | } // namespace RPU 65 | -------------------------------------------------------------------------------- /src/aihwkit/nn/low_precision_modules/quantized_input_module.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | # mypy: disable-error-code=attr-defined 8 | 9 | """Module wrapper to quantize its input""" 10 | 11 | from typing import Any 12 | 13 | from torch import Tensor 14 | from torch.nn import Module 15 | 16 | from aihwkit.simulator.digital_low_precision.base_quantized_classes import QuantizedActivation 17 | from aihwkit.simulator.digital_low_precision.config_utils import convert_act_config_to_kwargs_dict 18 | from aihwkit.simulator.parameters.quantization import ActivationQuantConfig 19 | 20 | 21 | class QuantizedInputModule(Module): 22 | """Wraps a module with an activation quantizer on the inputs, this offering 23 | the capability to quantize both the inputs and the outputs of a module. 24 | (NOTE: Output quantization is considered to be taken care of in the activation 25 | quantization of the module that is being wrapped) 26 | 27 | This is useful when a module is a first layer and consumes directly from the dataloader 28 | as well as when a layer follows a functional operation (e.g., activation function or addition) 29 | which did not quantize its own output down to the required size. 30 | """ 31 | 32 | def __init__(self, module: Module, act_quant_config: ActivationQuantConfig): 33 | super().__init__() 34 | # The original module 35 | self.module = module 36 | # Input quantizer 37 | self.input_quantizer = QuantizedActivation( 38 | **convert_act_config_to_kwargs_dict(act_quant_config) 39 | ) 40 | 41 | def forward(self, inp: Tensor, *args: Any, **kwargs: Any) -> Tensor: 42 | """Perform the forward of the original module after quantizing its input""" 43 | # Quantize inputs 44 | input_q = self.input_quantizer(inp) 45 | # Feed the quantized inputs to the original module 46 | return self.module(input_q, *args, **kwargs) 47 | -------------------------------------------------------------------------------- /src/aihwkit/extension/functions.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | # pylint: disable=abstract-method, no-name-in-module 8 | 9 | """AIHWKIT Extension functions""" 10 | 11 | from typing import Any 12 | 13 | from torch import Tensor 14 | from torch.autograd import Function 15 | from torch.nn import Module 16 | from aihwkit.extension.aihwkit_extension import ops # pylint: disable=import-error 17 | 18 | 19 | class FloatPrecisionCastFunction(Function): 20 | """Function for the float precision cast.""" 21 | 22 | @staticmethod 23 | def forward( 24 | _: Any, input_: Tensor, exponent: int = 8, mantissa: int = 7, saturate_to_inf: bool = True 25 | ) -> Tensor: 26 | # pylint: disable=unused-argument, arguments-differ 27 | return ops.float_precision_cast(input_, exponent, mantissa, saturate_to_inf) 28 | 29 | @staticmethod 30 | def backward(ctx: Any, grad_in: Tensor) -> Tensor: 31 | # pylint: disable=unused-argument, arguments-differ 32 | return grad_in 33 | 34 | 35 | class FloatPrecisionCast(Module): 36 | """Fake cast of FP32 numbers with variable exponent and mantissa. 37 | Backward pass is pass-trough 38 | 39 | Args: 40 | exponent: number of bits used for exponent 41 | mantissa: number of bits used for mantissa 42 | saturate_to_inf: whether to set it to infinity if saturated or to perform clipping 43 | 44 | Returns: 45 | Cast tensor. 46 | """ 47 | 48 | def __init__(self, exponent: int = 8, mantissa: int = 7, saturate_to_inf: bool = True): 49 | super().__init__() 50 | self.exponent = exponent 51 | self.mantissa = mantissa 52 | self.saturate_to_inf = saturate_to_inf 53 | 54 | def forward(self, input_: Tensor) -> Tensor: 55 | """Fake cast with FP32 input.""" 56 | return FloatPrecisionCastFunction.apply( 57 | input_, self.exponent, self.mantissa, self.saturate_to_inf 58 | ) 59 | -------------------------------------------------------------------------------- /cmake/Modules/FindAVX.cmake: -------------------------------------------------------------------------------- 1 | # This file has been adapted from pytorch. 2 | # https://github.com/pytorch/pytorch 3 | 4 | # Copyright (c) 2016-, Facebook Inc. All rights reserved. 5 | # 6 | # Licensed under the MIT license. See LICENSE file in the project root for details. 7 | 8 | INCLUDE(CheckCSourceCompiles) 9 | INCLUDE(CheckCXXSourceCompiles) 10 | 11 | SET(AVX_CODE " 12 | #include 13 | 14 | int main() 15 | { 16 | __m256 a; 17 | a = _mm256_set1_ps(0); 18 | return 0; 19 | } 20 | ") 21 | 22 | SET(AVX2_CODE " 23 | #include 24 | 25 | int main() 26 | { 27 | __m256i a = {0}; 28 | a = _mm256_abs_epi16(a); 29 | __m256i x; 30 | _mm256_extract_epi64(x, 0); // we rely on this in our AVX2 code 31 | return 0; 32 | } 33 | ") 34 | 35 | MACRO(CHECK_SSE lang type flags) 36 | SET(__FLAG_I 1) 37 | SET(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS}) 38 | FOREACH(__FLAG ${flags}) 39 | IF(NOT ${lang}_${type}_FOUND) 40 | SET(CMAKE_REQUIRED_FLAGS ${__FLAG}) 41 | IF(lang STREQUAL "CXX") 42 | CHECK_CXX_SOURCE_COMPILES("${${type}_CODE}" ${lang}_HAS_${type}_${__FLAG_I}) 43 | ELSE() 44 | CHECK_C_SOURCE_COMPILES("${${type}_CODE}" ${lang}_HAS_${type}_${__FLAG_I}) 45 | ENDIF() 46 | IF(${lang}_HAS_${type}_${__FLAG_I}) 47 | SET(${lang}_${type}_FOUND TRUE CACHE BOOL "${lang} ${type} support") 48 | SET(${lang}_${type}_FLAGS "${__FLAG}" CACHE STRING "${lang} ${type} flags") 49 | ENDIF() 50 | MATH(EXPR __FLAG_I "${__FLAG_I}+1") 51 | ENDIF() 52 | ENDFOREACH() 53 | SET(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE}) 54 | 55 | IF(NOT ${lang}_${type}_FOUND) 56 | SET(${lang}_${type}_FOUND FALSE CACHE BOOL "${lang} ${type} support") 57 | SET(${lang}_${type}_FLAGS "" CACHE STRING "${lang} ${type} flags") 58 | ENDIF() 59 | 60 | MARK_AS_ADVANCED(${lang}_${type}_FOUND ${lang}_${type}_FLAGS) 61 | 62 | ENDMACRO() 63 | 64 | CHECK_SSE(C "AVX" " ;-mavx;/arch:AVX") 65 | CHECK_SSE(C "AVX2" " ;-mavx2 -mfma;/arch:AVX2") 66 | 67 | CHECK_SSE(CXX "AVX" " ;-mavx;/arch:AVX") 68 | CHECK_SSE(CXX "AVX2" " ;-mavx2 -mfma;/arch:AVX2") 69 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/parameters/runtime.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | # pylint: disable=too-many-instance-attributes 8 | # pylint: disable=too-many-lines 9 | 10 | """Runtime parameters for settings at simulation time.""" 11 | 12 | from dataclasses import dataclass 13 | from .helpers import _PrintableMixin 14 | from .enums import RPUDataType 15 | 16 | 17 | @dataclass 18 | class RuntimeParameter(_PrintableMixin): 19 | """Parameter that define the analog-matvec (forward / backward) and 20 | peripheral digital input-output behavior. 21 | 22 | Here one can enable analog-digital conversion, dynamic input 23 | scaling, and define the properties of the analog-matvec 24 | computations, such as noise and non-idealities (e.g. IR-drop). 25 | """ 26 | 27 | data_type: RPUDataType = RPUDataType.FLOAT 28 | """Data type to use for the C++ bindings and simulations. 29 | """ 30 | 31 | on_the_fly_bindings: bool = False 32 | """Enable on the fly generation of some parameter bindings. 33 | 34 | For some post update steps (e.g. for inference tile), parameter 35 | bindings are generated on the fly. This is only useful if these 36 | parameters are vary during training and is turned off by default 37 | for performance reason (which typically is not supported as the 38 | `RPUConfig` is considered fixed once the analog tile is 39 | instantiated). 40 | 41 | If indeed parameters (e.g. ``WeightClipParameter``, 42 | ``WeightModifierParameter``, and ``WeightRemapParameter``) vary 43 | during training, this options as to be turned off to re-generate 44 | the bindings on the fly (that is each mini-batch). 45 | 46 | """ 47 | 48 | offload_input: bool = False 49 | """Whether to offload the stored input for the update pass on 50 | CPU to save GPU memory. 51 | """ 52 | 53 | offload_gradient: bool = False 54 | """Whether to offload the stored gradient for the update pass on 55 | CPU to save GPU memory. 56 | 57 | Note: 58 | Only for in case tiles are simulated with RPUCuda library. 59 | """ 60 | -------------------------------------------------------------------------------- /src/rpucuda/weight_clipper.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "rng.h" 10 | #include 11 | 12 | namespace RPU { 13 | 14 | enum class WeightClipType { 15 | None, 16 | FixedValue, 17 | LayerGaussian, 18 | AverageChannelMax, 19 | }; 20 | 21 | // no template. Just double 22 | struct WeightClipParameter { 23 | 24 | WeightClipType type = WeightClipType::None; 25 | double fixed_value = -1.0; // will always be applied if larger >0 26 | double sigma = 2.5; 27 | 28 | void print() const { 29 | std::stringstream ss; 30 | printToStream(ss); 31 | std::cout << ss.str(); 32 | }; 33 | 34 | void printToStream(std::stringstream &ss) const { 35 | 36 | ss << "\t weight clipper type:\t" << getTypeName() << std::endl; 37 | if (type == WeightClipType::None) { 38 | return; 39 | } 40 | ss << "\t fixed value:\t\t" << fixed_value << std::endl; 41 | if (type == WeightClipType::LayerGaussian) { 42 | ss << "\t sigma:\t\t" << sigma << std::endl; 43 | } 44 | 45 | ss << std::endl; 46 | }; 47 | 48 | inline std::string getTypeName() const { 49 | switch (type) { 50 | case WeightClipType::None: 51 | return "None"; 52 | case WeightClipType::FixedValue: 53 | return "FixedValue"; 54 | case WeightClipType::AverageChannelMax: 55 | return "AverageChannelMax"; 56 | case WeightClipType::LayerGaussian: 57 | return "LayerGaussian"; 58 | default: 59 | return "Unknown"; 60 | } 61 | }; 62 | }; 63 | 64 | template class WeightClipper { 65 | 66 | public: 67 | explicit WeightClipper(int x_size, int d_size); 68 | WeightClipper() {}; 69 | 70 | /* in-place clipping of weights */ 71 | void apply(T *weights, const WeightClipParameter &wclpar); 72 | 73 | void dumpExtra(RPU::state_t &extra, const std::string prefix) {}; 74 | void loadExtra(const RPU::state_t &extra, const std::string prefix, bool strict) {}; 75 | 76 | private: 77 | void clip(T *weights, T clip_value); 78 | std::vector amax_values_; 79 | 80 | int x_size_ = 0; 81 | int d_size_ = 0; 82 | int size_ = 0; 83 | }; 84 | 85 | } // namespace RPU 86 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/rpucuda_buffered_transfer_device.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "io_manager.h" 10 | #include "pulsed_weight_updater.h" 11 | #include "rpu_buffered_transfer_device.h" 12 | #include "rpucuda_transfer_device.h" 13 | 14 | namespace RPU { 15 | 16 | template class BufferedTransferRPUDeviceCuda : public TransferRPUDeviceCuda { 17 | 18 | public: 19 | explicit BufferedTransferRPUDeviceCuda() {}; 20 | explicit BufferedTransferRPUDeviceCuda( 21 | CudaContextPtr c, const BufferedTransferRPUDevice &other); 22 | 23 | ~BufferedTransferRPUDeviceCuda() {}; 24 | BufferedTransferRPUDeviceCuda(const BufferedTransferRPUDeviceCuda &other); 25 | BufferedTransferRPUDeviceCuda &operator=(const BufferedTransferRPUDeviceCuda &other); 26 | BufferedTransferRPUDeviceCuda(BufferedTransferRPUDeviceCuda &&other); 27 | BufferedTransferRPUDeviceCuda &operator=(BufferedTransferRPUDeviceCuda &&other); 28 | 29 | friend void 30 | swap(BufferedTransferRPUDeviceCuda &a, BufferedTransferRPUDeviceCuda &b) noexcept { 31 | using std::swap; 32 | swap(static_cast &>(a), static_cast &>(b)); 33 | swap(a.transfer_buffer_vec_, b.transfer_buffer_vec_); 34 | }; 35 | 36 | void populateFrom(const AbstractRPUDevice &rpu_device) override; 37 | BufferedTransferRPUDeviceMetaParameter &getPar() const { 38 | return static_cast &>( 39 | SimpleRPUDeviceCuda::getPar()); 40 | }; 41 | BufferedTransferRPUDeviceCuda *clone() const override { 42 | return new BufferedTransferRPUDeviceCuda(*this); 43 | }; 44 | 45 | void readAndUpdate( 46 | int to_device_idx, 47 | int from_device_idx, 48 | int i_col_start, 49 | const T lr, 50 | const T count_lr, 51 | const T *x_input, 52 | const int n_vec, 53 | const PulsedUpdateMetaParameter &up) override; 54 | 55 | std::vector getHiddenWeights() const override; 56 | 57 | protected: 58 | std::vector>> transfer_buffer_vec_; 59 | }; 60 | 61 | } // namespace RPU 62 | -------------------------------------------------------------------------------- /cmake/Modules/FindTorch.cmake: -------------------------------------------------------------------------------- 1 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 2 | # 3 | # Licensed under the MIT license. See LICENSE file in the project root for details. 4 | 5 | # Try to find the Torch library and headers. 6 | # TORCH_INCLUDE_DIRS - the torch include directory 7 | # TORCH_LIB_DIR - the torch shared library 8 | 9 | # Require Python in order to find the site-packages location. 10 | find_package(Python REQUIRED) 11 | 12 | # Attempt to locate torch/extension.h. 13 | find_path(TORCH_EXTENSION_H_INCLUDE_DIR 14 | NAMES torch/extension.h 15 | DOC "The directory where Torch includes (extension.h) reside" 16 | PATHS "${Python_SITELIB}/torch/include" 17 | "${Python_STDARCH}/torch/include" 18 | "${Python_SITEARCH}/torch/include" 19 | ) 20 | 21 | if(TORCH_EXTENSION_H_INCLUDE_DIR STREQUAL "TORCH_EXTENSION_H_INCLUDE_DIR-NOTFOUND") 22 | # Attempt to locate the path by invoking python. 23 | execute_process(COMMAND python -c "import torch; print(torch.__path__[0])" 24 | OUTPUT_VARIABLE CUSTOM_PYTHON_TORCH_PATH 25 | OUTPUT_STRIP_TRAILING_WHITESPACE) 26 | else() 27 | set(CUSTOM_PYTHON_TORCH_PATH "") 28 | endif() 29 | 30 | # extension.h 31 | find_path(TORCH_EXTENSION_H_INCLUDE_DIR REQUIRED 32 | NAMES torch/extension.h 33 | DOC "The directory where Torch includes (extension.h) reside" 34 | PATHS "${Python_SITELIB}/torch/include" 35 | "${Python_STDARCH}/torch/include" 36 | "${Python_SITEARCH}/torch/include" 37 | "${CUSTOM_PYTHON_TORCH_PATH}/include" 38 | ) 39 | 40 | # torch.h 41 | find_path(TORCH_H_INCLUDE_DIR REQUIRED 42 | NAMES torch/torch.h 43 | DOC "The directory where Torch includes (torch.h) reside" 44 | PATHS ${TORCH_EXTENSION_H_INCLUDE_DIR}/torch/csrc/api/include 45 | ) 46 | 47 | # libtorch_python.so 48 | find_library(TORCH_PYTHON_SO torch_python REQUIRED 49 | PATHS "${Python_SITELIB}/torch/lib" 50 | "${Python_STDARCH}/torch/lib" 51 | "${Python_SITEARCH}/torch/lib" 52 | "${CUSTOM_PYTHON_TORCH_PATH}/lib" 53 | ) 54 | 55 | # Set variables. 56 | set(TORCH_INCLUDE_DIRS ${TORCH_EXTENSION_H_INCLUDE_DIR} ${TORCH_H_INCLUDE_DIR}) 57 | get_filename_component(TORCH_LIB_DIR ${TORCH_PYTHON_SO} DIRECTORY) 58 | 59 | find_package_handle_standard_args(Torch DEFAULT_MSG 60 | TORCH_INCLUDE_DIRS 61 | TORCH_LIB_DIR 62 | ) 63 | -------------------------------------------------------------------------------- /examples/09_simple_layer_deterministic_pulses.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 9: simple network with one layer using 8 | deterministic pulse trains for update. 9 | 10 | Simple network that consist of one analog layer. The network aims to learn 11 | to sum all the elements from one array. 12 | """ 13 | # pylint: disable=invalid-name 14 | 15 | # Imports from PyTorch. 16 | from torch import Tensor 17 | from torch.nn.functional import mse_loss 18 | 19 | # Imports from aihwkit. 20 | from aihwkit.nn import AnalogLinear 21 | from aihwkit.optim import AnalogSGD 22 | from aihwkit.simulator.configs import SingleRPUConfig, ConstantStepDevice, PulseType 23 | from aihwkit.simulator.rpu_base import cuda 24 | 25 | # Prepare the datasets (input and expected output). 26 | x = Tensor([[0.1, 0.2, 0.4, 0.3], [0.2, 0.1, 0.1, 0.3]]) 27 | y = Tensor([[1.0, 0.5], [0.7, 0.3]]) 28 | 29 | # Define a single-layer network, using a constant step device type. 30 | rpu_config = SingleRPUConfig(device=ConstantStepDevice()) 31 | rpu_config.update.pulse_type = PulseType.DETERMINISTIC_IMPLICIT 32 | rpu_config.update.desired_bl = 10 # max number in this case 33 | rpu_config.update.update_bl_management = True # will vary up to 10 on demand 34 | rpu_config.update.d_res_implicit = 0.1 # effective resolution of x bit lines 35 | rpu_config.update.x_res_implicit = 0.1 # effective resolution of d bit lines 36 | 37 | model = AnalogLinear(4, 2, bias=True, rpu_config=rpu_config) 38 | print(model) 39 | # Move the model and tensors to cuda if it is available. 40 | if cuda.is_compiled(): 41 | x = x.cuda() 42 | y = y.cuda() 43 | model.cuda() 44 | 45 | # Define an analog-aware optimizer, preparing it for using the layers. 46 | opt = AnalogSGD(model.parameters(), lr=0.1) 47 | opt.regroup_param_groups(model) 48 | 49 | for epoch in range(100): 50 | # Delete old gradients 51 | opt.zero_grad() 52 | # Add the training Tensor to the model (input). 53 | pred = model(x) 54 | # Add the expected output Tensor. 55 | loss = mse_loss(pred, y) 56 | # Run training (backward propagation). 57 | loss.backward() 58 | 59 | opt.step() 60 | print("Loss error: {:.16f}".format(loss)) 61 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/rpu_base_src/rpu_base.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | #pragma once 7 | 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace py = pybind11; 17 | 18 | #ifdef RPU_USE_FP16 19 | namespace pybind11 { 20 | namespace detail { 21 | template <> struct type_caster : public type_caster_base { 22 | 23 | public: 24 | bool load(handle src, bool convert) { 25 | if (py::isinstance(src) || py::isinstance(src)) { 26 | this->value = new half_t(py::cast(src)); 27 | // std::cerr << "converted " << py::cast(src) << " to " << (float)*((half_t *) 28 | // this->value) << std::endl; 29 | return true; 30 | } 31 | 32 | return false; 33 | } 34 | static handle cast(half_t src, return_value_policy policy, handle parent) { 35 | return type_caster::cast(float(src), policy, parent); 36 | } 37 | }; 38 | } // namespace detail 39 | } // namespace pybind11 40 | #endif 41 | 42 | #if RPU_USE_FP16 43 | #define DEFAULT_DTYPE \ 44 | ((std::is_same::value) \ 45 | ? torch::kFloat16 \ 46 | : ((std::is_same::value) ? torch::kFloat64 : torch::kFloat32)) 47 | #else 48 | #define DEFAULT_DTYPE ((std::is_same::value) ? torch::kFloat64 : torch::kFloat32) 49 | #endif 50 | 51 | #define DEFAULT_TENSOR_OPTIONS auto default_options = torch::TensorOptions().dtype(DEFAULT_DTYPE) 52 | 53 | void declare_utils(py::module &m_devices, py::module &m_tiles); 54 | 55 | template 56 | void declare_rpu_tiles(py::module &m, std::string type_name_add); 57 | 58 | template void declare_rpu_devices(py::module &m, std::string type_name_add); 59 | 60 | #ifdef RPU_USE_CUDA 61 | template 62 | void declare_rpu_tiles_cuda(py::module &m, std::string type_name_add, bool add_utilities = false); 63 | #endif 64 | -------------------------------------------------------------------------------- /src/aihwkit/cloud/client/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Utilities for the AIHW Composer API.""" 8 | 9 | from os import getenv, path 10 | from typing import Dict 11 | from configparser import ConfigParser 12 | 13 | DEFAULT_URL = "https://api-aihw-composer.draco.res.ibm.com" 14 | 15 | 16 | class ClientConfiguration: 17 | """Helper for retrieving the user configuration. 18 | 19 | Utility for retrieving the user configuration. The API token will be 20 | retrieved from, in order of preference: 21 | 22 | 1. the ``AIHW_API_TOKEN`` environment variable. 23 | 2. the ``aihwkit.conf`` configuration file. 24 | 3. if the API token could not be found, it will return ``None``. 25 | 26 | The API URL will be retrieved from, in order of preference: 27 | 28 | 1. the ``aihwkit.conf`` configuration file. 29 | 2. the ``DEFAULT_URL`` module variable. 30 | 31 | The ``aihwkit.conf`` file is read from: 32 | 33 | * the current working directory. 34 | * ``XDG_CONFIG_HOME`` ``/aihwkit`` (by default, ``~/.config/aihwkit``). 35 | """ 36 | 37 | def __init__(self) -> None: 38 | self.stored_config = self.parse_config() 39 | 40 | def parse_config(self) -> Dict: 41 | """Read the configuration from a config file. 42 | 43 | Returns: 44 | A dictionary with the contents of the ``aihwkit.conf`` 45 | configuration file. 46 | """ 47 | parser = ConfigParser() 48 | parser.read( 49 | [ 50 | "aihwkit.conf", 51 | path.expanduser( 52 | "{}/aihwkit/aihwkit.conf".format(getenv("XDG_CONFIG_HOME", "~/.config")) 53 | ), 54 | ] 55 | ) 56 | 57 | # Check that the expected section is present. 58 | if "cloud" in parser: 59 | return dict(parser["cloud"]) 60 | 61 | return {} 62 | 63 | @property 64 | def token(self) -> str: 65 | """Return the user token.""" 66 | return getenv("AIHW_API_TOKEN", self.stored_config.get("api_token", None)) 67 | 68 | @property 69 | def url(self) -> str: 70 | """Return the API URL.""" 71 | return self.stored_config.get("api_url", DEFAULT_URL) 72 | -------------------------------------------------------------------------------- /src/aihwkit/experiments/runners/i_metrics.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Helper for retrieving Metrics of an Experiment.""" 8 | 9 | from datetime import datetime 10 | import json 11 | from typing import Dict 12 | 13 | 14 | class InferenceLocalMetric: 15 | """Metric used by the InferenceWorker Runner.""" 16 | 17 | def __init__(self, stdout: bool = False) -> None: 18 | self.current_repeat: Dict = {} 19 | self.time_init = datetime.utcnow() 20 | self.stdout = stdout 21 | 22 | def receive_repeat_start(self, repeat: int) -> None: 23 | """Hook for `INFERENCE_REPEAT_START`.""" 24 | self.current_repeat = {"number": repeat, "inference_results": []} 25 | 26 | def receive_repeat_end( 27 | self, 28 | t_inference_array: list, 29 | avg_acc_arr: list, 30 | std_acc_arr: list, 31 | avg_err_arr: list, 32 | avg_loss_arr: list, 33 | inference_repeats: int, 34 | ) -> Dict: 35 | """Hook for `INFERENCE_REPEAT_END`.""" 36 | 37 | inf_results = [] 38 | n_inference = len(t_inference_array) 39 | 40 | # The input are the arrays of avg accuracy, avg error and avg loss. 41 | # Create the dict entry for the items in the arrays. 42 | for i in range(n_inference): 43 | new_dict = { 44 | "t_inference": t_inference_array[i], 45 | "avg_accuracy": avg_acc_arr[i], 46 | "std_accuracy": std_acc_arr[i], 47 | "avg_error": avg_err_arr[i], 48 | "avg_loss": avg_loss_arr[i], 49 | } 50 | inf_results.append(new_dict) 51 | 52 | repeat = self.current_repeat["number"] + 1 53 | time_elapsed = (datetime.utcnow() - self.time_init).total_seconds() 54 | is_partial = bool(repeat < inference_repeats) 55 | partial = { 56 | "inference_runs": { 57 | "inference_repeat": repeat, 58 | "is_partial": is_partial, 59 | "time_elapsed": time_elapsed, 60 | "inference_results": inf_results, 61 | } 62 | } 63 | 64 | if self.stdout: 65 | print("{}".format(json.dumps(partial))) 66 | 67 | # Return the partial. 68 | return partial 69 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/tiles/torch_tile_irdrop_t.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | # pylint: disable=too-many-locals, too-many-arguments 8 | 9 | """Low level implementation of torch-based tile.""" 10 | 11 | from typing import TYPE_CHECKING 12 | 13 | from torch import Tensor 14 | 15 | from aihwkit.exceptions import TorchTileConfigError 16 | from aihwkit.simulator.tiles.torch_tile import TorchSimulatorTile 17 | from aihwkit.simulator.tiles.analog_mvm_irdrop_t import AnalogMVMIRDropT 18 | 19 | if TYPE_CHECKING: 20 | from aihwkit.simulator.configs.configs import TorchInferenceRPUConfigIRDropT 21 | 22 | 23 | class TorchSimulatorTileIRDropT(TorchSimulatorTile): 24 | """Torch tile class including time-dependent IR drop calculation. 25 | 26 | Args: 27 | x_size: input size 28 | d_size: output size 29 | rpu_config: resistive processing unit configuration. 30 | """ 31 | 32 | # pylint: disable=abstract-method 33 | 34 | def __init__( 35 | self, 36 | x_size: int, 37 | d_size: int, 38 | rpu_config: "TorchInferenceRPUConfigIRDropT", 39 | bias: bool = False, 40 | ): 41 | self._phys_input_size = rpu_config.mapping.max_input_size or x_size 42 | self._g_converter = rpu_config.noise_model.g_converter 43 | 44 | super().__init__(x_size, d_size, rpu_config, bias, analog_mvm=AnalogMVMIRDropT) 45 | 46 | def forward( 47 | self, 48 | x_input: Tensor, 49 | bias: bool = False, 50 | in_trans: bool = False, 51 | out_trans: bool = False, 52 | is_test: bool = False, 53 | non_blocking: bool = False, 54 | ) -> Tensor: 55 | if in_trans or out_trans: 56 | raise TorchTileConfigError("Non-trans MVMs supported only.") 57 | 58 | if not is_test: 59 | noisy_weights = self.modify_weight(self.weight, self._modifier, x_input.shape[0]) 60 | else: 61 | noisy_weights = self.weight 62 | 63 | return self._analog_mvm.matmul( 64 | noisy_weights, 65 | x_input, 66 | self._f_io, 67 | False, 68 | is_test, 69 | phys_input_size=self._phys_input_size, 70 | g_converter=self._g_converter, 71 | out_noise_values=self.out_noise_values, 72 | ) 73 | -------------------------------------------------------------------------------- /src/aihwkit/inference/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Utilities for inference.""" 8 | 9 | from typing import Optional, TYPE_CHECKING 10 | from torch.nn import Module 11 | from aihwkit.exceptions import ModuleError 12 | 13 | if TYPE_CHECKING: 14 | from aihwkit.inference.noise.base import BaseNoiseModel 15 | 16 | 17 | def drift_analog_weights(model: Module, t_inference: float = 0.0) -> None: 18 | """(Program) and drift all analog inference layers of a given model. 19 | 20 | Args: 21 | model: torch model with analog layers 22 | t_inference: assumed time of inference (in sec) 23 | 24 | Raises: 25 | ModuleError: if the layer is not in evaluation mode. 26 | """ 27 | # avoid circular import 28 | # pylint: disable=import-outside-toplevel 29 | from aihwkit.nn.modules.base import AnalogLayerBase 30 | 31 | if model.training: 32 | raise ModuleError("drift_analog_weights can only be applied in evaluation mode") 33 | 34 | for module in model.modules(): 35 | if not isinstance(module, AnalogLayerBase): 36 | continue 37 | module.drift_analog_weights(t_inference) 38 | 39 | 40 | def program_analog_weights(model: Module, noise_model: Optional["BaseNoiseModel"]) -> None: 41 | """Program all analog inference layers of a given model. 42 | 43 | Args: 44 | model: torch model with analog layers 45 | noise_model: Optional defining the noise model to be 46 | used. If not given, it will use the noise model 47 | defined in the `RPUConfig`. 48 | 49 | Caution: 50 | 51 | If given a noise model here it will overwrite the 52 | stored `rpu_config.noise_model` definition in the 53 | tiles. 54 | 55 | Raises: 56 | ModuleError: if the layer is not in evaluation mode. 57 | """ 58 | # avoid circular import 59 | # pylint: disable=import-outside-toplevel 60 | from aihwkit.nn.modules.base import AnalogLayerBase 61 | 62 | if model.training: 63 | raise ModuleError("program_analog_weights can only be applied in evaluation mode") 64 | 65 | for module in model.modules(): 66 | if not isinstance(module, AnalogLayerBase): 67 | continue 68 | module.program_analog_weights(noise_model=noise_model) 69 | -------------------------------------------------------------------------------- /src/rpucuda/cuda/rpucuda_expstep_device.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "pwu_kernel_parameter_base.h" 10 | #include "rpu_expstep_device.h" 11 | #include "rpucuda_pulsed_device.h" 12 | #include 13 | 14 | namespace RPU { 15 | 16 | template class ExpStepRPUDeviceCuda : public PulsedRPUDeviceCuda { 17 | 18 | public: 19 | BUILD_PULSED_DEVICE_CONSTRUCTORS_CUDA( 20 | ExpStepRPUDeviceCuda, 21 | ExpStepRPUDevice, 22 | /*ctor body*/ 23 | dev_es_par_ = std::unique_ptr>(new CudaArray(this->context_, 9)); 24 | , 25 | /*dtor body*/ 26 | , 27 | /*copy body*/ 28 | dev_es_par_->assign(*other.dev_es_par_); 29 | , 30 | /*move assigment body*/ 31 | dev_es_par_ = std::move(other.dev_es_par_); 32 | , 33 | /*swap body*/ 34 | swap(a.dev_es_par_, b.dev_es_par_); 35 | , 36 | /*host copy from cpu (rpu_device). Parent device params are copyied automatically*/ 37 | T es_par_arr[9]; 38 | auto &par = getPar(); 39 | es_par_arr[0] = par.es_A_down; 40 | es_par_arr[1] = par.es_A_up; 41 | es_par_arr[2] = par.es_gamma_down; 42 | es_par_arr[3] = par.es_gamma_up; 43 | es_par_arr[4] = par.es_a; 44 | es_par_arr[5] = par.es_b; 45 | es_par_arr[6] = par.getScaledWriteNoise(); 46 | es_par_arr[7] = par.dw_min_std_add; 47 | es_par_arr[8] = par.dw_min_std_slope; 48 | dev_es_par_->assign(es_par_arr); 49 | this->context_->synchronize();) 50 | 51 | T *getGlobalParamsData() override { return dev_es_par_->getData(); }; 52 | pwukpvec_t getUpdateKernels( 53 | int m_batch, 54 | int nK32, 55 | int use_bo64, 56 | bool out_trans, 57 | const PulsedUpdateMetaParameter &up) override; 58 | T *get1ParamsData() override { 59 | return getPar().usesPersistentWeight() ? this->dev_persistent_weights_->getData() : nullptr; 60 | }; 61 | T getWeightGranularityNoise() const override { 62 | // need to make sure that random states are enabled 63 | return getPar().usesPersistentWeight() 64 | ? PulsedRPUDeviceCuda::getWeightGranularityNoise() + (T)1e-6 65 | : PulsedRPUDeviceCuda::getWeightGranularityNoise(); 66 | } 67 | 68 | private: 69 | std::unique_ptr> dev_es_par_ = nullptr; 70 | }; 71 | 72 | } // namespace RPU 73 | -------------------------------------------------------------------------------- /src/rpucuda/math_util.h: -------------------------------------------------------------------------------- 1 | /** 2 | * (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 3 | * 4 | * Licensed under the MIT license. See LICENSE file in the project root for details. 5 | */ 6 | 7 | #pragma once 8 | 9 | #include "math.h" 10 | #include "utility_functions.h" 11 | 12 | #ifdef RPU_USE_MKL 13 | #include "mkl.h" 14 | #else 15 | #ifdef RPU_USE_OPENBLAS 16 | extern "C" { 17 | #include "cblas.h" 18 | } 19 | #endif 20 | #endif 21 | 22 | #ifndef MIN 23 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 24 | #endif 25 | #ifndef MAX 26 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 27 | #endif 28 | 29 | namespace RPU { 30 | namespace math { 31 | 32 | template 33 | void gemm( 34 | const CBLAS_ORDER Order, 35 | const CBLAS_TRANSPOSE TransA, 36 | const CBLAS_TRANSPOSE TransB, 37 | const int M, 38 | const int N, 39 | const int K, 40 | const T alpha, 41 | const T *A, 42 | const int lda, 43 | const T *B, 44 | const int ldb, 45 | const T beta, 46 | T *C, 47 | const int ldc); 48 | 49 | template int iamax(const int N, const T *X, const int incX); 50 | 51 | template T max(const int N, const T *X, const int incX); 52 | 53 | template void scal(const int N, const T alpha, T *X, const int incX); 54 | 55 | template T nrm2(const int N, const T *X, const int incX); 56 | 57 | template void copy(const int N, const T *X, const int incX, T *Y, const int incY); 58 | 59 | template 60 | void gemv( 61 | const CBLAS_ORDER Order, 62 | const CBLAS_TRANSPOSE TransA, 63 | const int M, 64 | const int N, 65 | const T alpha, 66 | const T *A, 67 | const int lda, 68 | const T *X, 69 | const int incX, 70 | const T beta, 71 | T *Y, 72 | const int incY); 73 | 74 | template 75 | void ger( 76 | const CBLAS_ORDER Order, 77 | const int M, 78 | const int N, 79 | const T alpha, 80 | const T *X, 81 | const int incX, 82 | const T *Y, 83 | const int incY, 84 | T *A, 85 | const int lda); 86 | 87 | template void permute132(T *X_out, const T *X_in, int d1, int d2, int d3, bool bias); 88 | 89 | template 90 | void makeBias( 91 | T *x_with_bias, const T *x_without_bias, const int size, const int m_batch, const bool trans); 92 | 93 | template 94 | void copyWithoutBias( 95 | T *x_without_bias, const T *x_with_bias, const int size, const int m_batch, const bool trans); 96 | 97 | } // namespace math 98 | } // namespace RPU 99 | -------------------------------------------------------------------------------- /docs/source/analog_ai_hw.rst: -------------------------------------------------------------------------------- 1 | Analog AI Hardware 2 | ================== 3 | 4 | There are many promising candidates for the resistive element in analog in-memory computing, including Phase Change Memory (PCM), 5 | Resistive Randam Access memory (RRAM), Electro chemical Random-access memory (ECRAM), Complementary Metal Oxide Semiconductor (CMOS), 6 | Magnetic RAM (MRAM), Ferroelectric RAM (FERAM or FEFET), and photonics:ref:`[1] `. 7 | 8 | Shown on the figure below is not a complete list of possible resistive elements, but examples of how analog resistance level are achieved 9 | with various material and circuit implementation. One set of resistive RAM switches based on the formation and dissolution of a filament in a 10 | non-conductive dielectric material. Intermediate conductance is achieve either by modulating the width of the filament or by modulating the composition 11 | of the conductive path. Electro-chemical RAM modulates the conductance between source and channel using the gate reservoir voltage that drives ions 12 | into the channel.CMOS-based capacitive cells can also be used as resistive elements for analog computing, as long as leakage is controlled 13 | and that the compute and read operations can be completed quickly. Magnetic RAM is a very popular emerging non-volatile memory device with 14 | usually limited dynamic range that forces its use as binary digital memory. However, using a racetrack memory type implementation, 15 | precise control of domain wall movement can be used to modulate conductance in an analog fashion. Similar domain wall motion implementation can also be 16 | applied to ferro-electric devices, though analog in-memory implementation with binary conductance levels but analog accumulation is also actively explored 17 | using FE-RAM or FE-FETs. Last but not the least, vector-matrix multiplication with photonic devices, where the transmission of light is modulated 18 | in an analog fashion has also been demonstrated. 19 | 20 | Clearly there is still no ideal Analog AI device as each one of the available options has its strenghts and weaknesses as illustrated in the figure. For instance, 21 | PCM devices are considered the most mature among storage-class memory types, however they suffer from assymetruc, drift, and abrupt reset. 22 | 23 | .. image:: ../img/analog_ai_hw.png 24 | :alt: Various Candidates for Resistive Memory Devices 25 | 26 | This is one of the key motivations behind building a simulator like the aihwkit to allow the exploration of various device and a multitude of charateristics on the 27 | performance of AI models. 28 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/presets/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Configurations presets for resistive processing units.""" 8 | 9 | from .configs import ( 10 | # Single device configs. 11 | ReRamESPreset, 12 | ReRamSBPreset, 13 | CapacitorPreset, 14 | EcRamPreset, 15 | EcRamMOPreset, 16 | IdealizedPreset, 17 | GokmenVlasovPreset, 18 | PCMPreset, 19 | # 2-device configs. 20 | ReRamES2Preset, 21 | ReRamSB2Preset, 22 | Capacitor2Preset, 23 | EcRam2Preset, 24 | EcRamMO2Preset, 25 | Idealized2Preset, 26 | # 4-device configs. 27 | ReRamES4Preset, 28 | ReRamSB4Preset, 29 | Capacitor4Preset, 30 | EcRam4Preset, 31 | EcRamMO4Preset, 32 | Idealized4Preset, 33 | # Tiki-taka configs. 34 | TikiTakaReRamESPreset, 35 | TikiTakaReRamSBPreset, 36 | TikiTakaCapacitorPreset, 37 | TikiTakaEcRamPreset, 38 | TikiTakaEcRamMOPreset, 39 | TikiTakaIdealizedPreset, 40 | # TTv2 configs. 41 | TTv2ReRamESPreset, 42 | TTv2ReRamSBPreset, 43 | TTv2CapacitorPreset, 44 | TTv2EcRamPreset, 45 | TTv2EcRamMOPreset, 46 | TTv2IdealizedPreset, 47 | # c-TTv2 configs. 48 | ChoppedTTv2ReRamESPreset, 49 | ChoppedTTv2ReRamSBPreset, 50 | ChoppedTTv2CapacitorPreset, 51 | ChoppedTTv2EcRamPreset, 52 | ChoppedTTv2EcRamMOPreset, 53 | ChoppedTTv2IdealizedPreset, 54 | # AGAD configs. 55 | AGADReRamESPreset, 56 | AGADReRamSBPreset, 57 | AGADCapacitorPreset, 58 | AGADEcRamPreset, 59 | AGADEcRamMOPreset, 60 | AGADIdealizedPreset, 61 | # MixedPrecision configs. 62 | MixedPrecisionReRamESPreset, 63 | MixedPrecisionReRamSBPreset, 64 | MixedPrecisionCapacitorPreset, 65 | MixedPrecisionEcRamPreset, 66 | MixedPrecisionEcRamMOPreset, 67 | MixedPrecisionIdealizedPreset, 68 | MixedPrecisionGokmenVlasovPreset, 69 | MixedPrecisionPCMPreset, 70 | ) 71 | from .inference import StandardHWATrainingPreset, FloatingPointPreset 72 | from .devices import ( 73 | ReRamESPresetDevice, 74 | ReRamSBPresetDevice, 75 | CapacitorPresetDevice, 76 | EcRamPresetDevice, 77 | EcRamMOPresetDevice, 78 | IdealizedPresetDevice, 79 | GokmenVlasovPresetDevice, 80 | PCMPresetDevice, 81 | ReRamArrayOMPresetDevice, 82 | ReRamArrayHfO2PresetDevice, 83 | ) 84 | from .compounds import PCMPresetUnitCell 85 | from .utils import PresetIOParameters, StandardIOParameters, PresetUpdateParameters 86 | -------------------------------------------------------------------------------- /examples/07_simple_layer_with_other_devices.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 7: simple network with one layer using other devices. 8 | 9 | Simple network that consist of one analog layer. The network aims to learn 10 | to sum all the elements from one array. 11 | """ 12 | # pylint: disable=invalid-name 13 | 14 | # Imports from PyTorch. 15 | from torch import Tensor 16 | from torch.nn.functional import mse_loss 17 | 18 | # Imports from aihwkit. 19 | from aihwkit.nn import AnalogLinear 20 | from aihwkit.optim import AnalogSGD 21 | from aihwkit.simulator.configs import ( 22 | UnitCellRPUConfig, 23 | ConstantStepDevice, 24 | VectorUnitCell, 25 | VectorUnitCellUpdatePolicy, 26 | ) 27 | from aihwkit.simulator.rpu_base import cuda 28 | 29 | # Prepare the datasets (input and expected output). 30 | x = Tensor([[0.1, 0.2, 0.4, 0.3], [0.2, 0.1, 0.1, 0.3]]) 31 | y = Tensor([[1.0, 0.5], [0.7, 0.3]]) 32 | 33 | # Define a single-layer network, using a vector device having multiple 34 | # devices per crosspoint. Each device can be arbitrarily defined 35 | 36 | rpu_config = UnitCellRPUConfig() 37 | # 3 arbitrary single unit cell devices (of the same type) per cross-point. 38 | rpu_config.device = VectorUnitCell( 39 | unit_cell_devices=[ 40 | ConstantStepDevice(w_max=0.3), 41 | ConstantStepDevice(w_max_dtod=0.4), 42 | ConstantStepDevice(up_down_dtod=0.1), 43 | ] 44 | ) 45 | 46 | # Only one of the devices should receive a single update. 47 | # That is selected randomly, the effective weights is the sum of all 48 | # weights. 49 | rpu_config.device.update_policy = VectorUnitCellUpdatePolicy.SINGLE_RANDOM 50 | 51 | 52 | model = AnalogLinear(4, 2, bias=True, rpu_config=rpu_config) 53 | 54 | print(rpu_config) 55 | 56 | # Move the model and tensors to cuda if it is available. 57 | if cuda.is_compiled(): 58 | x = x.cuda() 59 | y = y.cuda() 60 | model.cuda() 61 | 62 | # Define an analog-aware optimizer, preparing it for using the layers. 63 | opt = AnalogSGD(model.parameters(), lr=0.1) 64 | opt.regroup_param_groups(model) 65 | 66 | for epoch in range(100): 67 | # Delete old gradient 68 | opt.zero_grad() 69 | # Add the training Tensor to the model (input). 70 | pred = model(x) 71 | # Add the expected output Tensor. 72 | loss = mse_loss(pred, y) 73 | # Run training (backward propagation). 74 | loss.backward() 75 | 76 | opt.step() 77 | print("Loss error: {:.16f}".format(loss)) 78 | -------------------------------------------------------------------------------- /tests/helpers/testcases.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """TestCases for aihwkit tests.""" 8 | 9 | import os 10 | from typing import Type 11 | from unittest import SkipTest, TestCase 12 | 13 | from numpy.testing import assert_array_almost_equal, assert_raises 14 | from aihwkit.simulator.rpu_base import cuda 15 | 16 | SKIP_CUDA_TESTS = os.getenv("SKIP_CUDA_TESTS") or not cuda.is_compiled() 17 | 18 | 19 | class AihwkitTestCase(TestCase): 20 | """Test case that contains common asserts and functions for aihwkit.""" 21 | 22 | def assertTensorAlmostEqual(self, tensor_a, tensor_b, decimal=4): 23 | """Assert that two tensors are almost equal.""" 24 | # pylint: disable=invalid-name 25 | array_a = tensor_a.detach().cpu().numpy() 26 | array_b = tensor_b.detach().cpu().numpy() 27 | assert_array_almost_equal(array_a, array_b, decimal=decimal) 28 | 29 | def assertNotAlmostEqualTensor(self, tensor_a, tensor_b, decimal=4): 30 | """Assert that two tensors are not equal.""" 31 | # pylint: disable=invalid-name 32 | assert_raises( 33 | AssertionError, self.assertTensorAlmostEqual, tensor_a, tensor_b, decimal=decimal 34 | ) 35 | 36 | 37 | class ParametrizedTestCase(AihwkitTestCase): 38 | """Test case that is parametrized using aihwkit test decorators. 39 | 40 | Base class for aihwkit parametrized tests. This base class should be used 41 | in combination with the decorators in the ``.decorators`` package 42 | (``@parametrize_over_tiles``, ``@parametrize_over_layers``). 43 | 44 | The decorators will set the class attributes and methods accordingly at 45 | runtime for each parametrized test. 46 | """ 47 | 48 | use_cuda: bool = False 49 | """Determines if the test case requires CUDA support.""" 50 | 51 | simulator_tile_class: Type = None 52 | """rpu_base tile class that is expected to be used internally in this test.""" 53 | 54 | first_hidden_field: str = "" 55 | """First of the hidden fields to check during hidden parameters.""" 56 | 57 | bias: bool = False 58 | """If True, the tiles and layer in this test will use bias.""" 59 | 60 | digital_bias: bool = False 61 | """If True, the tiles and layer in this test will use bias.""" 62 | 63 | parameter: str = "" 64 | """Friendly name of the parameters used in this test.""" 65 | 66 | def setUp(self) -> None: 67 | if self.use_cuda and SKIP_CUDA_TESTS: 68 | raise SkipTest("not compiled with CUDA support") 69 | 70 | super().setUp() 71 | -------------------------------------------------------------------------------- /src/aihwkit/simulator/parameters/base.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """Base classes for the RPUConfig.""" 8 | 9 | from typing import Type, Union, Any, TYPE_CHECKING 10 | from dataclasses import dataclass, field 11 | 12 | from .runtime import RuntimeParameter 13 | 14 | 15 | if TYPE_CHECKING: 16 | from aihwkit.simulator.configs.configs import ( 17 | InferenceRPUConfig, 18 | SingleRPUConfig, 19 | UnitCellRPUConfig, 20 | TorchInferenceRPUConfig, 21 | QuantizedTorchInferenceRPUConfig, 22 | DigitalRankUpdateRPUConfig, 23 | ) 24 | from aihwkit.simulator.tiles.base import BaseTile 25 | 26 | 27 | RPUConfigGeneric = Union[ 28 | "InferenceRPUConfig", 29 | "SingleRPUConfig", 30 | "UnitCellRPUConfig", 31 | "TorchInferenceRPUConfig", 32 | "DigitalRankUpdateRPUConfig", 33 | "QuantizedTorchInferenceRPUConfig", 34 | ] 35 | 36 | 37 | @dataclass 38 | class RPUConfigBase: 39 | """Base class of all RPUConfigs.""" 40 | 41 | tile_class: Type 42 | """Tile class that correspond to the RPUConfig. Needs to be 43 | defined in the derived class.""" 44 | 45 | runtime: RuntimeParameter = field(default_factory=RuntimeParameter, compare=False) 46 | """Parameters setting simulation runtime options, such as 47 | gradient memory management. 48 | """ 49 | 50 | def as_bindings(self) -> Any: 51 | """Return a representation of this instance as a simulator bindings object.""" 52 | return self 53 | 54 | def compatible_with(self, tile_class_name: str) -> bool: 55 | """Tests whether the RPUConfig is compatile with a given ``TileModule`` class. 56 | 57 | Args: 58 | tile_class_name: name of the TileModule class 59 | 60 | Returns: 61 | 62 | Whether the class is compatible. By default only the 63 | class that is defined in the ``tile_class`` property of 64 | the RPUConfig 65 | """ 66 | return tile_class_name == self.tile_class.__name__ 67 | 68 | def get_default_tile_module_class(self, out_size: int = 0, in_size: int = 0) -> Type: 69 | """Returns the default TileModule class.""" 70 | # pylint: disable=unused-argument 71 | return self.tile_class 72 | 73 | def create_tile(self, *args: Any, **kwargs: Any) -> "BaseTile": 74 | """Created a tile with this configuration. 75 | 76 | Short-cut for instantiating ``self.tile_class`` with given parameters. 77 | """ 78 | return self.tile_class(*args, rpu_config=self, **kwargs) 79 | -------------------------------------------------------------------------------- /examples/13_experiment_3fc.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # (C) Copyright 2020, 2021, 2022, 2023, 2024 IBM. All Rights Reserved. 4 | # 5 | # Licensed under the MIT license. See LICENSE file in the project root for details. 6 | 7 | """aihwkit example 13: Experiment training (3 fully connected layers). 8 | 9 | Creation and execution of a Basic Training experiment, where the model uses 10 | 3 fully connected analog layers. 11 | """ 12 | # pylint: disable=invalid-name 13 | 14 | import os 15 | 16 | # Imports from PyTorch. 17 | import torch 18 | from torch.nn import Flatten, Sigmoid, LogSoftmax 19 | 20 | # Imports from aihwkit. 21 | from torchvision.datasets import FashionMNIST 22 | 23 | from aihwkit.experiments import BasicTraining 24 | from aihwkit.experiments.runners import LocalRunner 25 | from aihwkit.nn import AnalogLinear, AnalogSequential 26 | from aihwkit.simulator.configs import SingleRPUConfig, ConstantStepDevice 27 | from aihwkit.simulator.rpu_base import cuda 28 | 29 | # Check device 30 | USE_CUDA = 0 31 | if cuda.is_compiled(): 32 | USE_CUDA = 1 33 | DEVICE = torch.device("cuda" if USE_CUDA else "cpu") 34 | 35 | # Path where the datasets will be stored. 36 | PATH_DATASET = os.path.join("data", "DATASET") 37 | 38 | # Network definition. 39 | INPUT_SIZE = 784 40 | HIDDEN_SIZES = [256, 128] 41 | OUTPUT_SIZE = 10 42 | 43 | # Training parameters. 44 | EPOCHS = 30 45 | BATCH_SIZE = 64 46 | LEARNING_RATE = 0.05 47 | 48 | 49 | def main(): 50 | """Create and execute an experiment.""" 51 | model = AnalogSequential( 52 | Flatten(), 53 | AnalogLinear( 54 | INPUT_SIZE, 55 | HIDDEN_SIZES[0], 56 | True, 57 | rpu_config=SingleRPUConfig(device=ConstantStepDevice()), 58 | ), 59 | Sigmoid(), 60 | AnalogLinear( 61 | HIDDEN_SIZES[0], 62 | HIDDEN_SIZES[1], 63 | True, 64 | rpu_config=SingleRPUConfig(device=ConstantStepDevice()), 65 | ), 66 | Sigmoid(), 67 | AnalogLinear( 68 | HIDDEN_SIZES[1], 69 | OUTPUT_SIZE, 70 | True, 71 | rpu_config=SingleRPUConfig(device=ConstantStepDevice()), 72 | ), 73 | LogSoftmax(dim=1), 74 | ) 75 | 76 | # Create the training Experiment. 77 | experiment = BasicTraining( 78 | dataset=FashionMNIST, model=model, epochs=EPOCHS, batch_size=BATCH_SIZE 79 | ) 80 | 81 | # Create the runner and execute the experiment. 82 | runner = LocalRunner(device=DEVICE) 83 | results = runner.run(experiment, dataset_root=PATH_DATASET) 84 | print(results) 85 | 86 | 87 | if __name__ == "__main__": 88 | # Execute only if run as the entry point into the program. 89 | main() 90 | --------------------------------------------------------------------------------