Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxf99 committed Aug 4, 2021
1 parent acfeb92 commit 9538f06
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Benchmarks/DL/mnist-omp/dlp.py
@@ -0,0 +1,69 @@
'''Trains a simple deep NN on the MNIST dataset.
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
2 seconds per epoch on a K520 GPU.
'''

from __future__ import print_function

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop

import os
import tensorflow as tf
from keras import backend as K

batch_size = #P0
num_classes = 10
epochs = #P1

#change the parallelism threads and OpenMP settings
os.environ["OMP_NUM_THREADS"] ="#P4"
os.environ["OMP_PLACES"] = "#P6"
os.environ["KMP_BLOCKTIME"] = "0"
os.environ["KMP_SETTINGS"] = "1"
os.environ["KMP_AFFINITY"]= "granularity=fine,verbose,#P5,1,0"
config = tf.ConfigProto(intra_op_parallelism_threads=int(os.getenv('OMP_NUM_THREADS', 64)), inter_op_parallelism_threads=1, allow_soft_placement=True)
K.set_session(tf.Session(config=config))

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(#P2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(#P2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
optimizer='#P3',
metrics=['accuracy'])

history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
25 changes: 25 additions & 0 deletions Benchmarks/DL/mnist-omp/exe.pl
@@ -0,0 +1,25 @@
#!/usr/bin/env perl

#Author: Xingfu Wu
#MCS, ANL
# exe.pl: average the execution time in 5 runs
#
use Time::HiRes qw(gettimeofday);

$A_FILE = "tmpfile.txt";
foreach $filename (@ARGV) {
# print "Start to preprocess ", $filename, "...\n";
system("python $filename > tmpfile.txt");
open (TEMFILE, '<', $A_FILE);
while (<TEMFILE>) {
$line = $_;
chomp ($line);

if ($line =~ /Test accuracy/) {
($v1, $v2) = split(': ', $line);
printf("%.3f", 1/$v2)
}
}
close(TEMFILE);
system("unlink tmpfile.txt");
}
18 changes: 18 additions & 0 deletions Benchmarks/DL/mnist-omp/findMin.py
@@ -0,0 +1,18 @@
import pandas
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression

dataframe = pandas.read_csv("results.csv")
array = dataframe.values
x = array[:,7]

print("Performance (accuracy) summary based on", len(array), "evaluations:")
print("Min: ", 1/x.max())
print("Max: ", 1/x.min())
print("Mean: ", 1/x.mean())
print("The best configurations (for the smallest 1/accuracy) of P0, P1, P2, P3, P4, P5 and P6 is:\n")
print("P0 P1 P2 P3 P4 P5 P6 1/accuracy elapsed time\n")
mn = x.min()
for i in range(len(array)):
if x[i] == mn:
print (array[i,:])
72 changes: 72 additions & 0 deletions Benchmarks/DL/mnist-omp/mlp-omp.py
@@ -0,0 +1,72 @@
'''Trains a simple deep NN on the MNIST dataset.
A Multilayer Perceptron (Neural Network) implementation example using
TensorFlow library. This example is using the MNIST database of handwritten
digits (http://yann.lecun.com/exdb/mnist/).
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
'''

from __future__ import print_function

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop

import os
import tensorflow as tf
from keras import backend as K

batch_size = 128
num_classes = 10
epochs = 20

#change the parallelism threads and OpenMP settings
os.environ["OMP_NUM_THREADS"] ="4"
os.environ["OMP_PLACES"] = "cores"
os.environ["KMP_BLOCKTIME"] = "0"
os.environ["KMP_SETTINGS"] = "1"
os.environ["KMP_AFFINITY"]= "granularity=fine,verbose,compact,1,0"
config = tf.ConfigProto(intra_op_parallelism_threads=int(os.getenv('OMP_NUM_THREADS', 64)), inter_op_parallelism_threads=1, allow_soft_placement=True)
K.set_session(tf.Session(config=config))


# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])

history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
58 changes: 58 additions & 0 deletions Benchmarks/DL/mnist-omp/mnist_mlp.py
@@ -0,0 +1,58 @@
'''Trains a simple deep NN on the MNIST dataset.
A Multilayer Perceptron (Neural Network) implementation example using
TensorFlow library. This example is using the MNIST database of handwritten
digits (http://yann.lecun.com/exdb/mnist/).
Gets to 98.40% test accuracy after 20 epochs
(there is *a lot* of margin for parameter tuning).
'''

from __future__ import print_function

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop

batch_size = 128
num_classes = 10
epochs = 20

# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])

history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
77 changes: 77 additions & 0 deletions Benchmarks/DL/mnist-omp/problem.py
@@ -0,0 +1,77 @@
import numpy as np
from numpy import abs, cos, exp, mean, pi, prod, sin, sqrt, sum
from autotune import TuningProblem
from autotune.space import *
import os
import sys
import time
import json
import math

import ConfigSpace as CS
import ConfigSpace.hyperparameters as CSH
from skopt.space import Real, Integer, Categorical

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(1, os.path.dirname(HERE)+ '/plopper')
from plopper import Plopper

cs = CS.ConfigurationSpace(seed=1234)
#batch_size
p0= CSH.OrdinalHyperparameter(name='p0', sequence=['16','32','64','100','128','200','256','300','400','512'], default_value='128')
#epochs
p1= CSH.OrdinalHyperparameter(name='p1', sequence=['1','2','4','8','12','16','20','22','24','30'], default_value='20')
#dropout rate
p2= CSH.OrdinalHyperparameter(name='p2', sequence=['0.1', '0.15', '0.2', '0.25','0.4'], default_value='0.2')
#optimizer
p3= CSH.CategoricalHyperparameter(name='p3', choices=['rmsprop','adam','sgd','adamax','adadelta','adagrad','nadam'], default_value='rmsprop')
#number of threads
p4= CSH.OrdinalHyperparameter(name='p4', sequence=['4','5','6','7','8'], default_value='8')
#thread affinity type
p5= CSH.CategoricalHyperparameter(name='p5', choices=['compact','scatter','balanced','none','disabled', 'explicit'], default_value='none')
# omp placement
p6= CSH.CategoricalHyperparameter(name='p6', choices=['cores','threads','sockets'], default_value='cores')

cs.add_hyperparameters([p0, p1, p2, p3, p4, p5, p6])

# problem space
task_space = None

input_space = cs

output_space = Space([
Real(0.0, inf, name="time")
])

dir_path = os.path.dirname(os.path.realpath(__file__))
kernel_idx = dir_path.rfind('/')
kernel = dir_path[kernel_idx+1:]
obj = Plopper(dir_path+'/dlp.py',dir_path)

x1=['p0','p1','p2','p3','p4','p5', 'p6']

def myobj(point: dict):

def plopper_func(x):
x = np.asarray_chkfinite(x) # ValueError if any NaN or Inf
value = [point[x1[0]],point[x1[1]],point[x1[2]],point[x1[3]],point[x1[4]],point[x1[5]],point[x1[6]]]
print('VALUES:',point[x1[4]])
params = ["P0", "P1","P2","P3","P4","P5","P6"]

result = obj.findRuntime(value, params)
return result

x = np.array([point[f'p{i}'] for i in range(len(point))])
results = plopper_func(x)
print('OUTPUT: ',results)

return results

Problem = TuningProblem(
task_space=None,
input_space=input_space,
output_space=output_space,
objective=myobj,
constraints=None,
model=None
)
2 changes: 2 additions & 0 deletions Benchmarks/DL/mnist-omp/run.bat
@@ -0,0 +1,2 @@
python -m ytopt.search.ambs --evaluator ray --problem problem.Problem --max-evals=10 --learner RF
python findMin.py

0 comments on commit 9538f06

Please sign in to comment.