Metadata-Version: 2.4
Name: analogsim
Version: 0.1.0
Summary: Open-source analog computing library for resistive crossbar neural network simulation
Author-email: Aditya Raj Parashar <adityarajparashar@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/Aditya-Raj-Parashar/analoglib
Project-URL: Documentation, https://github.com/Aditya-Raj-Parashar/analoglib/tree/main/docs
Project-URL: Repository, https://github.com/Aditya-Raj-Parashar/analoglib
Project-URL: Issues, https://github.com/Aditya-Raj-Parashar/analoglib/issues
Keywords: analog-computing,reram,crossbar,memristor,neuromorphic,in-memory-computing,neural-network,hardware-simulation,analogsim,analoglib
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.24
Requires-Dist: pycryptodome>=3.19
Requires-Dist: msgpack>=1.0.0
Provides-Extra: torch
Requires-Dist: torch>=2.0; extra == "torch"
Provides-Extra: tensorflow
Requires-Dist: tensorflow>=2.13; extra == "tensorflow"
Provides-Extra: viz
Requires-Dist: matplotlib>=3.7; extra == "viz"
Provides-Extra: cli
Requires-Dist: click>=8.1; extra == "cli"
Provides-Extra: dev
Requires-Dist: pytest>=7.4; extra == "dev"
Requires-Dist: pytest-cov>=4.1; extra == "dev"
Requires-Dist: build>=1.0; extra == "dev"
Requires-Dist: twine>=4.0; extra == "dev"
Provides-Extra: all
Requires-Dist: analogsim[cli,dev,tensorflow,torch,viz]; extra == "all"

# analogsim / analoglib

[![PyPI Version](https://img.shields.io/pypi/v/analogsim.svg)](https://pypi.org/project/analogsim/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)

An open-source Python library for simulating **Analog In-Memory Computing (IMC)** and neural network inference on resistive crossbar architectures (**ReRAM, PCM, Flash**).

---

## ⚡ Key Features

- 🧠 **PyTorch & NumPy Importers**: Convert PyTorch `nn.Module` or NumPy weight matrices directly into physical crossbar arrays.
- 📐 **Analog Intermediate Representation (AIR)**: Decouples high-level model definitions from low-level physical crossbar backends.
- 🧱 **Tiled Crossbar Architecture (`TiledCrossbar`)**: Automatically partitions arbitrary matrix sizes across a 2D grid of physical crossbar tiles with global $w_{\text{max}}$ scale preservation.
- ⚡ **Physical Hardware Non-Idealities (`analoglib.effects`)**:
  - **Parasitic IR Drop**: Wordline/bitline wire resistance simulation ($r_{\text{wire}}$ per cell).
  - **Thermal Scaling**: Arrhenius temperature-dependent conductance shift $G(T) = G_0 \exp(-E_a / k_B T)$.
  - **Retention Drift**: Power-law temporal decay $G(t) = G_0 (t/t_0)^{-\nu}$.
- 📊 **Hardware Profiler & Analytics (`AnalogProfiler`)**: Estimates array power (W), read energy (J), ADC/DAC energy overhead, area ($\mu\text{m}^2$), latency (s), and throughput efficiency ($\text{TOPS/W}$).
- 🔌 **Circuit Exporter**: Export loaded models into **ngspice** and **LTspice** netlists for circuit simulation.
- 📦 **Encrypted `.analog` Binary Format**: Secure serialization using AES-256-GCM + MsgPack.
- 🛠️ **CLI Utility**: Command-line tool `analog` / `analogsim` for info, simulation, profiling, and SPICE export.

---

## 📦 Installation

### Install from PyPI
```bash
pip install analogsim
```

### Install with Optional Dependencies (PyTorch, Matplotlib)
```bash
pip install "analogsim[all]"
```

### Install from Source
```bash
git clone https://github.com/Aditya-Raj-Parashar/analoglib.git
cd analoglib
pip install -e .
```

---

## 🚀 Quick Start

### 1. PyTorch to Analog Simulation Pipeline

```python
import torch.nn as nn
import analogsim as al
import numpy as np

# 1. Define your PyTorch model
torch_model = nn.Sequential(
    nn.Linear(784, 128),
    nn.ReLU(),
    nn.Linear(128, 10),
)

# 2. Convert PyTorch model to AnalogModel via AIR
model = al.AnalogModel.from_torch(torch_model)

# 3. Compile targeting physical ReRAM crossbars + ADC/DAC + Hardware Effects
model.compile(
    device=al.ReRAM(g_min=1e-6, g_max=100e-6, num_states=256, read_noise_sigma=0.01),
    adc_bits=8,
    dac_bits=8,
    r_wire=1.0,    # Parasitic IR drop wire resistance
    E_a=0.1,       # Thermal Arrhenius scaling
    nu=0.05,       # Retention drift exponent
)

# 4. Simulate inference in hardware mode
x_input = np.random.uniform(0, 1, 784)
result = model.simulate(x_input, mode="hardware")

# 5. Generate Hardware Performance & Accuracy Report
result.report()
```

### 2. Direct Crossbar Operations

```python
import analogsim as al
import numpy as np

# Define physical ReRAM device
reram = al.ReRAM(g_min=1e-6, g_max=100e-6, num_states=256)

# Create a differential crossbar (128 rows x 64 columns)
xbar = al.Crossbar(128, 64, device=reram, differential=True)

# Load weights (mapped automatically to G+ and G- conductances)
W = np.random.uniform(-1.0, 1.0, (128, 64))
xbar.load_weights(W, quantize=True)

# Perform VMM
V_in = np.random.uniform(0.0, 1.0, 128)
I_out = xbar.vmm(V_in, mode=al.SimulationMode.HARDWARE)
```

---

## 🛠️ CLI Usage

`analogsim` comes with a command-line tool `analog` (or `analogsim`):

```bash
# View model metadata & layer details
analog info model.analog

# Run hardware simulation
analog simulate model.analog --mode hardware

# Profile power, latency, area, and TOPS/W
analog profile model.analog

# Export SPICE netlist for ngspice
analog export-spice model.analog --out circuit.cir --dialect ngspice
```

---

## 📜 Publishing to PyPI

To build and publish `analogsim` to PyPI:

```bash
# 1. Install build tools
pip install build twine

# 2. Build source distribution and wheel
python -m build

# 3. Upload to PyPI
python -m twine upload dist/*
```

---

## 📄 License

MIT License © 2026 Aditya Raj Parashar
