Skip to content

Commit

Permalink
verified versions
Browse files Browse the repository at this point in the history
  • Loading branch information
alibillalhammoud committed Aug 25, 2024
1 parent 3971f55 commit 877ff01
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 51 deletions.
32 changes: 22 additions & 10 deletions openfasoc/MLoptimization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Code for reinforcement learning loop with openfasoc generators for optimizing metrics

## Supported Versions
Please note that this program has only been tested with python3.11
Please note that this program has only been tested with python3.10 and ngspice v40s

## Quick Start
run `bash quickstart.bash` to get an example RL run optimizing opamps.
Expand All @@ -21,13 +21,13 @@ Make sure that you have OpenAI Gym and Ray installed. To do this, run the follow

To generate the design specifications that the agent trains on, run:
```
python3.11 gen_specs.py
python3.10 gen_specs.py
```
The result is a yaml file dumped to the ../generators/gdsfactory-gen/.

To train the agent, open ipython from the top level directory and then:
```
python3.11 model.py
python3.10 model.py
```
The training checkpoints will be saved in your home directory under ray\_results. Tensorboard can be used to load reward and loss plots using the command:

Expand All @@ -39,16 +39,28 @@ tensorboard --logdir path/to/checkpoint
The evaluation script takes the trained agent and gives it new specs that the agent has never seen before. To generate new design specs, run the gen_specs.py file again with your desired number of specs to validate on. To run validation:

```
python3.11 eval.py
```
python3.10 eval.py
```

The evaluation result will be saved to the ../generators/gdsfactory-gen/.
The evaluation result will be saved to the ../generators/glayout/.

## Results
Please note that results vary greatly based on random seed and spec generation (both for testing and validation). An example spec file is provided that was used to generate the results below.
example resulting opamps (parameter arrays and names provided)

```
# transistor parameters provided as (width, length, fingers, multipliers)
# all parameters are listed in floating point format (even integers) for processing purposes
<p float="left">
<img src="mean_reward_versus_step.png" width="400" />
</p>
153 MegHz UGB, 91db DC Gain, 166uW power
[3.0, 0.5, 6.0, 7.0, 1.5, 10.0, 5.0, 0.7, 8.0, 3.0, 5.0, 1.2, 12.0, 3.0, 5.0, 1.0, 16.0, 6.0, 2.0, 4.0, 4.0, 0.5, 5.0, 12.0, 12.0, 3.0, 2.0]
{'diffpair': (3.0, 0.5, 6.0), 'diffpair_bias': (7.0, 1.5, 10.0), 'secondstage': (5.0, 0.7, 8.0, 3.0), 'secondstage_bias': (5.0, 1.2, 12.0, 3.0), 'output_stage_params': (5.0, 1.0, 16.0), 'output_stage_bias': (6.0, 2.0, 4.0), 'firststage': (4.0, 0.5, 5.0), 'mim_cap_size': (12.0, 12.0), 'mim_cap_rows': 3, 'rmult': 2}
143 MegHz UGB, 95db DC Gain, 179uW power
[2.0, 0.6, 4.0, 4.0, 1.0, 7.0, 4.0, 0.7, 13.0, 3.0, 4.0, 1.1, 12.0, 3.0, 5.0, 1.0, 16.0, 6.0, 2.0, 4.0, 3.0, 0.5, 5.0, 12.0, 12.0, 3.0, 2.0]
{'diffpair': (2.0, 0.6, 4.0), 'diffpair_bias': (4.0, 1.0, 7.0), 'secondstage': (4.0, 0.7, 13.0, 3.0), 'secondstage_bias': (4.0, 1.1, 12.0, 3.0), 'output_stage_params': (5.0, 1.0, 16.0), 'output_stage_bias': (6.0, 2.0, 4.0), 'firststage': (3.0, 0.5, 5.0), 'mim_cap_size': (12.0, 12.0), 'mim_cap_rows': 3, 'rmult': 2}
137 MegHz UGB, 95db DC Gain, 41uW power
[3.0, 0.5, 4.0, 4.0, 1.0, 7.0, 4.0, 0.8, 14.0, 3.0, 4.0, 1.2, 11.0, 3.0, 5.0, 1.0, 16.0, 6.0, 2.0, 4.0, 3.0, 0.5, 5.0, 12.0, 12.0, 3.0, 2.0]
{'diffpair': (3.0, 0.5, 4.0), 'diffpair_bias': (4.0, 1.0, 7.0), 'secondstage': (4.0, 0.8, 14.0, 3.0), 'secondstage_bias': (4.0, 1.2, 11.0, 3.0), 'output_stage_params': (5.0, 1.0, 16.0), 'output_stage_bias': (6.0, 2.0, 4.0), 'firststage': (3.0, 0.5, 5.0), 'mim_cap_size': (12.0, 12.0), 'mim_cap_rows': 3, 'rmult': 2}
```

3 changes: 2 additions & 1 deletion openfasoc/MLoptimization/gen_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def generate_random_specs(env, num_specs):
specs_range = {
"gain_min" : [float(10003380.0), float(35003380.0)],
"gain_min" : [float(10003380.0), float(130003380.0)],
"FOM" : [float(5e11), float(5e11)]
}
specs_range_vals = list(specs_range.values())
Expand Down Expand Up @@ -37,3 +37,4 @@ def main():

if __name__=="__main__":
main()

66 changes: 30 additions & 36 deletions openfasoc/MLoptimization/quickstart.bash
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
#!/bin/bash

# the script produces an example RL optimization run
echo "This script has been verified to run with python3.11 and package versions provided"


# this script produces an example RL optimization run

# =====================================================================
#
# find most recent version of python
#
# Find all installed Python 3 versions and sort them in descending order
#PYTHON_VERSIONS=$(compgen -c | grep -E '^python3\.[0-9]+$' | sort -V | tail -n 1)
# Extract the most recent version
#MOST_RECENT_PYTHON=$(echo "$PYTHON_VERSIONS" | tail -n 1)
# Check if a Python version was found
#if [ -z "$MOST_RECENT_PYTHON" ]; then
# echo "No Python 3 versions found."
# exit 1
#fi
# Print the most recent Python version
#echo
#echo "Currently using Python version: $MOST_RECENT_PYTHON"
#echo
# Check if the most recent version is at least 3.10
#MINIMUM_VERSION="3.10"
#if [[ "$(echo $MOST_RECENT_PYTHON | cut -d '.' -f2)" -lt "$(echo $MINIMUM_VERSION | cut -d '.' -f2)" ]]; then
# echo "The most recent Python version ($MOST_RECENT_PYTHON) is less than $MINIMUM_VERSION. Please update your Python installation."
# echo
# exit 1
#fi
# Save the command to run the most recent Python version into a variable
#PY_RUN=$MOST_RECENT_PYTHON
PY_RUN="python3.11"

# Check if Python 3.10 is installed
if command -v python3.10 &>/dev/null; then
echo "Python 3.10 is installed."
else
echo "Error: Python 3.10 is not installed."
exit 1
fi
PY_RUN="python3.10"

# =====================================================================
# Check if ngspice is installed
if command -v ngspice &>/dev/null; then
echo "ngspice is installed."
else
echo "Error: ngspice is not installed."
exit 1
fi

# =====================================================================
# check that ngspice 40 is installed
ngspice --version > test_ngspice_version.txt
version_line=$(sed -n '2p' test_ngspice_version.txt)
expected_version="ngspice-40"
if [[ $version_line == *"$expected_version"* ]]; then
echo "Correct ngspice version ($expected_version) is installed."
else
echo "Error: Incorrect ngspice version. Expected $expected_version but found:"
echo "$version_line"
exit 1
fi

# =====================================================================
#
# ensure all python depedencies are installed
#

# File containing the list of python dependencies
requirements_file="requirements.txt"
#requirements_file="donotdothischeck.txt"

# Function to check if a Python package is installed
is_installed() {
Expand Down Expand Up @@ -80,7 +74,6 @@ echo "Dependency check and package installations complete."


# =====================================================================
#
# setup and run the RL code
#

Expand All @@ -100,3 +93,4 @@ $PY_RUN model.py
$PY_RUN gen_spec.py
$PY_RUN eval.py
# eval.py creates eval*.txt which shows how many specifications are reached

7 changes: 5 additions & 2 deletions openfasoc/MLoptimization/run_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(self, env_config):
"Diffpair_bias2" : [3, 13, 1],
"pamp_hparams0" : [1, 9, 1],
"pamp_hparams1" : [0.5, 2.1, 0.1],
"pamp_hparams2" : [2, 13, 1],
"pamp_hparams2" : [4, 17, 1],
"bias0" : [1, 8, 1],
"bias1" : [0.5, 2.1, 0.1],
"bias2" : [3, 18, 1],
Expand Down Expand Up @@ -137,7 +137,7 @@ def reset(self, *, seed=None, options=None):
self.specs_ideal_norm = self.lookup(self.specs_ideal, self.global_g)

#initialize current parameters
self.cur_params_idx = np.array([6, 2, 7, 6, 0, 5, 7, 0, 10, 6, 5, 13, 0, 6, 4, 2])
self.cur_params_idx = np.array([6, 0, 7, 6, 0, 5, 7, 0, 12, 6, 5, 9, 1, 6, 2, 2])
# param array
self.cur_specs = self.update(self.cur_params_idx)
cur_spec_norm = self.lookup(self.cur_specs, self.global_g)
Expand Down Expand Up @@ -253,6 +253,8 @@ def update(self, params_idx):
inputparam[22] = params[14]
inputparam[25] = params[15]
result = safe_single_build_and_simulation(inputparam,temp=-269)
with open("quick_unorganized.txt", "a") as filerec:
filerec.write(str(inputparam)+"\n"+str(result)+"\n\n\n")
specs = np.array([0.0 , 0.0])
specs[0] = result["ugb"]
specs[1] = result["ugb"]/(result["Ibias_diffpair"]+result["Ibias_commonsource"])
Expand All @@ -270,3 +272,4 @@ def main():

if __name__ == "__main__":
main()

Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,42 @@ while bias_cs le bias_cs_Max
** Measure phase margin
let phase = (180/PI)*vp(vo)
meas ac pm find phase when vdb(vo)=0
let pm_FOM_factor = pm > 45 ? 1 : 0.0000001
*let pm_FOM_factor = pm > 45 ? 1 : 0.0000001
let pm_FOM_factor = 0.0000001
if ( pm ge 45 )
let pm_FOM_factor = 1
end
** Measure DC(ish) gain
meas ac dcg find vdb(vo) at=10
** Measure 3db BW
let threedbabsgain = dcg - 3
meas ac threedb when vdb(vo)=threedbabsgain FALL=1
** if FOM is better than previous max save results
let FOM = pm_FOM_factor * ugb_f / (bias_cs + bias_dp)
* let FOM = pm_FOM_factor * ugb_f / (bias_cs + bias_dp)
let FOM = pm_FOM_factor * ugb_f
*
* debug outputs
echo "finished computations, and calculated the following"
echo "phase margin"
print pm
echo "pm factor:"
print pm_FOM_factor
echo "FOM"
print FOM
echo "max FOM"
print maxFOM
* end debug outputs
*
if ( FOM ge maxFOM )
echo "now setting a new maxFOM"
let maxFOM = FOM
let maxUGB = ugb_f
let maxBics = bias_cs
let maxBidp = bias_dp
let maxBio = bias_o
let savedPhaseMargin = pm % 360
echo "saved Phase Margin"
print savedPhaseMargin
let savedDCGain = dcg
let savedthreedbBW = threedb
end
Expand All @@ -153,6 +174,8 @@ while bias_cs le bias_cs_Max
end
end
** Export global maxima
echo "the final saved phase margin is:"
print savedPhaseMargin
wrdata result_ac.txt maxUGB maxBidp maxBics maxBio savedPhaseMargin savedDCGain savedthreedbBW

** Export power usage of correctly biased opamp
Expand Down Expand Up @@ -180,3 +203,4 @@ quit
.GLOBAL GND
.GLOBAL VDD
.end

0 comments on commit 877ff01

Please sign in to comment.