-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaterial_layer.py
121 lines (81 loc) · 3.27 KB
/
material_layer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# # Top Matter
import numpy as np
import time
import os
import h5py
import sys
import yaml
from random import random
from material import Material
class PiM_processor(object):
def __init__(self, prm):
"""
Initialise object
"""
self.prm = prm
# # Generate Physical Material Object
self.mobj = Material(prm)
return
#
#
#
def solve_layer(self, lVdata, lVc):
"""
Takes a layers input voltages, re-organises and solves using
material model.
"""
tic = time.time()
if np.max(lVdata) > self.prm['spice']['Vmax']:
raise ValueError("A data input voltage (%.2f) exceeds the max allowed input voltage (%.2f)" % (np.max(lVdata), self.prm['spice']['Vmax']))
elif np.min(lVdata) < self.prm['spice']['Vmin']:
raise ValueError("A data input voltage (%.2f) exceeds the min allowed input voltage (%.2f)" % (np.min(lVdata), self.prm['spice']['Vmin']))
# # Check passed in voltage to calc
if np.shape(lVdata)[1] != self.prm['network']['num_input']*self.prm['network']['hiddenSize']:
raise ValueError("Data voltage array with %d columns cannot be passed into material layer expecting %d cols." % (np.shape(lVdata)[1], self.prm['network']['num_input']*self.prm['network']['hiddenSize']))
# # Check passed in config voltages
if len(lVc) != self.prm['network']['num_config']*self.prm['network']['hiddenSize']:
raise ValueError("Config voltage array of %d cannot be passed into material with %d voltage application nodes." % (len(lVc), self.prm['network']['num_config']*self.prm['network']['hiddenSize']))
#
print(np.shape(lVdata), np.shape(lVc))
# # Calc Output From each material in layer
Vout = []
for m in range(self.prm['network']['hiddenSize']):
# Break up Vin
idxs = m*self.prm['network']['num_input']
idxe = m*self.prm['network']['num_input']+self.prm['network']['num_input']
Vdata = lVdata[:, idxs:idxe]
idxs = m*self.prm['network']['num_config']
idxe = m*self.prm['network']['num_config']+self.prm['network']['num_config']
Vc = lVc[idxs:idxe]
Vc = np.full((len(Vdata), len(Vc)), Vc)
Vin = np.concatenate((Vdata, Vc), axis=1)
Vo = self.mobj.solve_material(Vin)
Vout.append(Vo)
# # Produce a single layer output array
Vout = np.concatenate(np.array(Vout), axis=1)
#
# # Check output voltage dimensions
if np.shape(Vout)[1] != self.prm['network']['num_output']*self.prm['network']['hiddenSize']:
raise ValueError("Output voltage array with %d columns should have %d cols." % (np.shape(Vout)[1], (self.prm['network']['num_output']*self.prm['network']['hiddenSize'])))
print("Time to solve layer = ", time.time()-tic)
#exit()
return Vout
#
def fin(self):
"""
Delete material layer object by shutting down SI and HI+GPIO connections
"""
self.mobj.fin()
del self.mobj
print("Material object delected, re-initialise Material Layer to start up again.")
return
#
#
#
#
#
#
#
#
#
# fin