Skip to content

Commit 16763c4

Browse files
committed
[GR-33233] Resize meso benchmarks and add tool for simple analysis of benchmarks.
PullRequest: graalpython/1934
2 parents f677494 + 6082c11 commit 16763c4

14 files changed

+1946
-5
lines changed
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
"""
41+
******************************************************************
42+
* HISTORY
43+
* 15-Oct-94 Jeff Shufelt (js), Carnegie Mellon University
44+
* Prepared for 15-681, Fall 1994.
45+
* Modified by Shuai Che
46+
******************************************************************
47+
"""
48+
49+
import random
50+
import math
51+
52+
# eta value
53+
ETA = 0.3
54+
55+
# momentum value
56+
MOMENTUM = 0.3
57+
58+
# (16, 1 can not be changed)
59+
n_hidden = 16
60+
n_out = 1
61+
62+
63+
def bpnn_layerforward(l1, l2, conn, n1, n2):
64+
## Set up thresholding unit ##
65+
l1[0] = 1.0
66+
## For each unit in second layer ##
67+
r1 = 1 + n2
68+
r2 = 1 + n1
69+
for j in range(1, r1):
70+
## Compute weighted Sum of its inputs ##
71+
Sum = 0.0
72+
for k in range(r2):
73+
Sum += conn[k*r1 + j] * l1[k]
74+
75+
l2[j] = (1.0 / (1.0 + math.exp(-Sum)))
76+
77+
78+
def bpnn_output_error(delta, target, output, nj):
79+
errSum = 0.0
80+
for j in range(1, 1 + nj):
81+
o = output[j]
82+
t = target[j]
83+
v = o * (1.0 - o) * (t - o)
84+
delta[j] = v
85+
errSum += abs(v)
86+
87+
return errSum
88+
89+
90+
def bpnn_hidden_error(delta_h, nh, delta_o, no, who, hidden):
91+
errSum = 0.0
92+
for j in range(1, 1 + nh):
93+
Sum = 0.0
94+
for k in range(1, 1 + no):
95+
Sum += delta_o[k] * who[j * (1 + no) + k]
96+
h = hidden[j]
97+
delta_h[j] = h * (1.0 - h) * Sum
98+
errSum += abs(delta_h[j])
99+
100+
return errSum
101+
102+
103+
def bpnn_adjust_weights(delta, ndelta, ly, nly, w, oldw):
104+
ly[0] = 1.0
105+
for j in range(1, (1 + ndelta)):
106+
for k in range(nly + 1):
107+
val = ETA * delta[j] * ly[k]
108+
val += MOMENTUM * oldw[k*(1 + ndelta) + j]
109+
oldw[k*(1 + ndelta) + j] = val
110+
w[k*(1 + ndelta) + j] += val
111+
112+
113+
def bpnn_train_kernel(_iu_list, _hu_list, _iw_list, _ou_list, _hw_list, _od_list, _t_list, _hd_list, _hw_prev_list, _iw_prev_list, layer_size):
114+
bpnn_layerforward(_iu_list, _hu_list, _iw_list, layer_size, n_hidden)
115+
bpnn_layerforward(_hu_list, _ou_list, _hw_list, n_hidden, n_out)
116+
out_err = bpnn_output_error(_od_list, _t_list, _ou_list, n_out)
117+
hid_err = bpnn_hidden_error(
118+
_hd_list, n_hidden, _od_list, n_out, _hw_list, _hu_list)
119+
bpnn_adjust_weights(_od_list, n_out, _hu_list,
120+
n_hidden, _hw_list, _hw_prev_list)
121+
bpnn_adjust_weights(_hd_list, n_hidden, _iu_list,
122+
layer_size, _iw_list, _iw_prev_list)
123+
124+
return (out_err, hid_err)
125+
126+
127+
class Data:
128+
def __init__(self):
129+
self._iu_list = None
130+
self._hw_list = None
131+
self._hu_list = None
132+
self._ou_list = None
133+
self._hd_list = None
134+
self._od_list = None
135+
self._iw_list = None
136+
self._iw_prev_list = None
137+
self._hw_prev_list = None
138+
self._t_list = None
139+
140+
self.zeros_list = None
141+
self.random_list = None
142+
143+
144+
data = Data()
145+
146+
default_size = 2 ** 16
147+
148+
149+
def measure(layer_size=default_size):
150+
print("Starting training kernel")
151+
bpnn_train_kernel(data._iu_list, data._hu_list, data._iw_list, data._ou_list, data._hw_list,
152+
data._od_list, data._t_list, data._hd_list, data._hw_prev_list, data._iw_prev_list, layer_size)
153+
154+
155+
def __benchmark__(layer_size=default_size):
156+
measure(layer_size)
157+
158+
159+
def __setup__(layer_size=default_size):
160+
random.seed(7)
161+
print("Input layer size : %d" % layer_size)
162+
# Creates a new fully-connected network from scratch,
163+
# with the given numbers of input, hidden, and output units.
164+
# Threshold units are automatically included. All weights are
165+
# randomly initialized.
166+
167+
# Space is also allocated for temporary storage (momentum weights,
168+
# error computations, etc).
169+
170+
## the input units ##
171+
data._iu_list = [random.random() for i in range(layer_size + 1)]
172+
173+
## weights from hidden to output layer ##
174+
data._hw_list = [random.random()
175+
for i in range((n_out + 1) * (n_hidden + 1))]
176+
177+
## the hidden units ##
178+
data._hu_list = [0. for i in range(n_hidden + 1)]
179+
## the output units ##
180+
data._ou_list = [0. for i in range(n_out + 1)]
181+
182+
## storage for hidden unit error ##
183+
data._hd_list = [0. for i in range(n_hidden + 1)]
184+
## storage for output unit error ##
185+
data._od_list = [0. for i in range(n_out + 1)]
186+
187+
## weights from input to hidden layer ##
188+
data._iw_list = [0. for i in range((n_hidden + 1) * (layer_size + 1))]
189+
190+
## The next two are for momentum ##
191+
## previous change on input to hidden wgt ##
192+
data._iw_prev_list = [0. for i in range((n_hidden + 1) * (layer_size + 1))]
193+
## previous change on hidden to output wgt ##
194+
data._hw_prev_list = [0. for i in range((n_out + 1) * (n_hidden + 1))]
195+
196+
## storage for target vector ##
197+
data._t_list = [0.1 for i in range(n_out + 1)]
198+
199+
data.zeros_list = [data._hu_list, data._ou_list, data._hd_list,
200+
data._od_list, data._iw_list, data._iw_prev_list, data._hw_prev_list]
201+
data.random_list = [data._iu_list, data._hw_list]
202+
203+
204+
def __cleanup__(layer_size=default_size):
205+
# clean up written data
206+
for l in data.zeros_list:
207+
for i in range(len(l)):
208+
l[i] = 0.
209+
for l in data.random_list:
210+
for i in range(len(l)):
211+
l[i] = random.random()
212+
for i in range(len(data._t_list)):
213+
data._t_list[i] = 0.1

0 commit comments

Comments
 (0)