Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 45c0515

Browse files
authored
Merge branch 'master' into invalid-argument
2 parents 72b0744 + e20d519 commit 45c0515

File tree

10 files changed

+365
-17
lines changed

10 files changed

+365
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ docs/dev
306306
# Protobuf files
307307
*.pb.cc
308308
*.pb.h
309+
*.pb
309310

310311
# Third party
311312
third_party/gflags/

cmake/Dependencies.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ include(cmake/CapnProtoMacros.cmake)
3131
# ---[ Google-protobuf
3232
include(cmake/ProtoBuf.cmake)
3333

34+
# --[ tensorflow
35+
find_library(TFlowC
36+
NAMES tensorflow
37+
PATHS "/usr/local/lib")
38+
list(APPEND Peloton_LINKER_LIBS ${TFlowC})
39+
3440
# ---[ Libevent
3541
find_package(Libevent REQUIRED)
3642
include_directories(SYSTEM ${LIBEVENT_INCLUDE_DIRS})

cmake/ProtoBuf.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Finds Google Protocol Buffers library and compilers and extends
22
# the standard cmake script with version and python generation support
3-
4-
find_package( Protobuf REQUIRED )
3+
find_package( Protobuf REQUIRED)
54
include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIR})
65
list(APPEND Peloton_LINKER_LIBS ${PROTOBUF_LIBRARIES})
76

script/installation/packages.sh

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
## * OSX
1818
## =================================================================
1919

20+
2021
# Determine OS platform
2122
UNAME=$(uname | tr "[:upper:]" "[:lower:]")
2223
# If Linux, try to determine specific distribution
@@ -36,6 +37,50 @@ fi
3637
unset UNAME
3738
DISTRO=$(echo $DISTRO | tr "[:lower:]" "[:upper:]")
3839
TMPDIR=/tmp
40+
TF_VERSION="1.4.0"
41+
TF_TYPE="cpu"
42+
43+
44+
function install_protobuf3.4.0() {
45+
# Install Relevant tooling
46+
# Remove any old versions of protobuf
47+
DISTRIB=$1 # ubuntu/fedora
48+
if [ "$DISTRIB" == "ubuntu" ]
49+
then
50+
sudo apt-get --yes --force-yes remove --purge libprotobuf-dev protobuf-compiler
51+
elif [ "$DISTRIB" == "fedora" ]
52+
then
53+
sudo dnf -q remove -y protobuf protobuf-devel protobuf-compiler
54+
else
55+
echo "Only Ubuntu and Fedora is supported currently!"
56+
return 0
57+
fi
58+
CWD=`pwd`
59+
cd $TMPDIR
60+
wget -O protobuf-cpp-3.4.0.tar.gz https://github.com/google/protobuf/releases/download/v3.4.0/protobuf-cpp-3.4.0.tar.gz
61+
tar -xzf protobuf-cpp-3.4.0.tar.gz
62+
cd protobuf-3.4.0
63+
./autogen.sh && ./configure && make -j4 && sudo make install && sudo ldconfig
64+
# Do cleanup
65+
cd $CWD
66+
}
67+
68+
# Utility function for installing tensorflow components of python/C++
69+
function install_tf() {
70+
TFCApiFile=$1
71+
TFBinaryURL=$2
72+
LinkerConfigCmd=$3
73+
TARGET_DIRECTORY="/usr/local"
74+
# Install Tensorflow Python Binary
75+
sudo pip3 install --upgrade ${TFBinaryURL}
76+
77+
# Install C-API
78+
TFCApiURL="https://storage.googleapis.com/tensorflow/libtensorflow/${TFCApiFile}"
79+
wget -O $TFCApiFile $TFCApiURL
80+
sudo tar -C $TARGET_DIRECTORY -xzf $TFCApiFile
81+
# Configure the Linker
82+
eval $LinkerConfigCmd
83+
}
3984

4085
## ------------------------------------------------
4186
## UBUNTU
@@ -75,13 +120,19 @@ if [ "$DISTRO" = "UBUNTU" ]; then
75120
if [ "$MAJOR_VER" == "14" ]; then
76121
PKG_CMAKE="cmake3"
77122
FORCE_Y="--force-yes"
123+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp34-cp34m-linux_x86_64.whl"
124+
fi
125+
if [ "$MAJOR_VER" == "16" ]; then
126+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp35-cp35m-linux_x86_64.whl"
78127
fi
79128
# Fix for llvm on Ubuntu 17.x
80129
if [ "$MAJOR_VER" == "17" ]; then
81130
PKG_LLVM="llvm-3.9"
82131
PKG_CLANG="clang-3.8"
132+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp35-cp35m-linux_x86_64.whl"
83133
fi
84-
134+
TFCApiFile="libtensorflow-${TF_TYPE}-linux-x86_64-${TF_VERSION}.tar.gz"
135+
LinkerConfigCmd="sudo ldconfig"
85136
sudo apt-get -qq $FORCE_Y --ignore-missing -y install \
86137
$PKG_CMAKE \
87138
$PKG_LLVM \
@@ -92,9 +143,7 @@ if [ "$DISTRO" = "UBUNTU" ]; then
92143
flex \
93144
valgrind \
94145
lcov \
95-
protobuf-compiler \
96146
libgflags-dev \
97-
libprotobuf-dev \
98147
libevent-dev \
99148
libboost-dev \
100149
libboost-thread-dev \
@@ -103,7 +152,19 @@ if [ "$DISTRO" = "UBUNTU" ]; then
103152
libpqxx-dev \
104153
libedit-dev \
105154
libssl-dev \
106-
postgresql-client
155+
postgresql-client \
156+
python3-pip \
157+
curl \
158+
autoconf \
159+
automake \
160+
libtool \
161+
make \
162+
g++ \
163+
unzip
164+
# Install version of protobuf needed by C-API
165+
install_protobuf3.4.0 "ubuntu"
166+
# Install tensorflow
167+
install_tf "$TFCApiFile" "$TFBinaryURL" "$LinkerConfigCmd"
107168

108169
## ------------------------------------------------
109170
## DEBIAN
@@ -140,14 +201,15 @@ elif [[ "$DISTRO" == *"FEDORA"* ]]; then
140201
26) LLVM="llvm";;
141202
*) LLVM="llvm4.0";;
142203
esac
143-
204+
TFCApiFile="libtensorflow-${TF_TYPE}-linux-x86_64-${TF_VERSION}.tar.gz"
205+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp36-cp36m-linux_x86_64.whl"
206+
LinkerConfigCmd="sudo ldconfig"
144207
sudo dnf -q install -y \
145208
git \
146209
gcc-c++ \
147210
make \
148211
cmake \
149212
gflags-devel \
150-
protobuf-devel \
151213
bison \
152214
flex \
153215
libevent-devel \
@@ -166,7 +228,16 @@ elif [[ "$DISTRO" == *"FEDORA"* ]]; then
166228
libasan \
167229
libtsan \
168230
libubsan \
169-
libatomic
231+
libatomic \
232+
python3-pip \
233+
curl \
234+
autoconf \
235+
automake \
236+
libtool
237+
# Install version of protobuf needed by C-API
238+
install_protobuf3.4.0 "fedora"
239+
# Install tensorflow
240+
install_tf "$TFCApiFile" "$TFBinaryURL" "$LinkerConfigCmd"
170241

171242
## ------------------------------------------------
172243
## REDHAT
@@ -194,18 +265,17 @@ elif [[ "$DISTRO" == *"REDHAT"* ]] && [[ "${DISTRO_VER%.*}" == "7" ]]; then
194265
fi
195266
popd; popd
196267
return 0
197-
}
268+
}
198269

199-
# Package download paths
270+
#Package download paths
200271
PKGS=(
201272
"https://github.com/schuhschuh/gflags/archive/v2.0.tar.gz"
202273
)
203-
204-
# Add EPEL repository first
274+
#Add EPEL repository first
205275
sudo yum -q -y install epel-release
206276
sudo yum -q -y upgrade epel-release
207277

208-
# Simple installations via yum
278+
#Simple installations via yum
209279
sudo yum -q -y install \
210280
git \
211281
gcc-c++ \
@@ -245,7 +315,9 @@ elif [ "$DISTRO" = "DARWIN" ]; then
245315
echo "Installing homebrew..."
246316
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
247317
fi
248-
318+
TFBinaryURL="https://storage.googleapis.com/tensorflow/mac/${TF_TYPE}/tensorflow-${TF_VERSION}-py3-none-any.whl"
319+
TFCApiFile="libtensorflow-${TF_TYPE}-darwin-x86_64-${TF_VERSION}.tar.gz"
320+
LinkerConfigCmd="sudo update_dyld_shared_cache"
249321
brew install git
250322
brew install cmake
251323
brew install gflags
@@ -260,6 +332,12 @@ elif [ "$DISTRO" = "DARWIN" ]; then
260332
brew install libedit
261333
brew install [email protected]
262334
brew install postgresql
335+
brew install curl
336+
brew install python
337+
brew upgrade python
338+
# Brew installs correct version of Protobuf(3.5.1 >= 3.4.0)
339+
# So we can directly install tensorflow
340+
install_tf "$TFCApiFile" "$TFBinaryURL" "$LinkerConfigCmd"
263341

264342
## ------------------------------------------------
265343
## UNKNOWN

src/CMakeLists.txt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
##################################################################################
2-
# SRC CMAKELISTS
2+
#SRC CMAKELISTS
33
##################################################################################
44

5-
# --[ Cap'nProto library
5+
#--[Cap'nProto library
66

77
file(GLOB capnp_files include/capnp/*.capnp)
88
if (capnp_files)
@@ -34,6 +34,22 @@ peloton_default_properties(peloton-proto)
3434

3535
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/libpg_query/ libpg_query.a)
3636

37+
##################################################################################
38+
39+
# --[ tensorflow
40+
# Generate essential model files
41+
set(MODEL_GEN_DIR ${CMAKE_CURRENT_LIST_DIR}/brain/modelgen)
42+
set(MODEL_GEN_PY ${MODEL_GEN_DIR}/model_generator.py)
43+
set(MODEL_GEN_SETTINGS ${MODEL_GEN_DIR}/settings.json)
44+
set(MODEL_GEN_COMMAND python3 ${MODEL_GEN_PY} ${MODEL_GEN_SETTINGS} ${MODEL_GEN_DIR})
45+
46+
message(STATUS "Tensorflow models being generated")
47+
execute_process(COMMAND ${MODEL_GEN_COMMAND}
48+
RESULT_VARIABLE RETURN_VALUE)
49+
50+
if (NOT RETURN_VALUE EQUAL 0)
51+
message(FATAL_ERROR "Failed to generate tensorflow models.")
52+
endif()
3753

3854
##################################################################################
3955

src/brain/modelgen/LSTM_Model.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#===----------------------------------------------------------------------===#
2+
#
3+
# Peloton
4+
#
5+
# LSTM_Model.py
6+
#
7+
# Identification: src/brain/modelgen/LSTM_Model.py
8+
#
9+
# Copyright (c) 2015-2018, Carnegie Mellon University Database Group
10+
#
11+
#===----------------------------------------------------------------------===#
12+
13+
import tensorflow as tf
14+
import functools
15+
import os
16+
import argparse
17+
18+
def lazy_property(function):
19+
attribute = '_cache_' + function.__name__
20+
21+
@property
22+
@functools.wraps(function)
23+
def decorator(self):
24+
if not hasattr(self, attribute):
25+
setattr(self, attribute, function(self))
26+
return getattr(self, attribute)
27+
28+
return decorator
29+
30+
class LSTM_Model:
31+
"""Container module with an encoder, a recurrent module, and a decoder.
32+
Encoder-decoder LSTM is useful for Seq2Seq prediction problems
33+
i.e. where we have a sequence as input(previous workload info) and want to predict a sequence
34+
as output(workload prediction 10 days later for 1 entire day)
35+
Learning rate used: https:#www.tensorflow.org/tutorials/seq2seq - Check Gradient computation & optimization
36+
Its provided: The value of learning_rate can is usually in the range 0.0001 to 0.001;
37+
and can be set to decrease as training progresses
38+
"""
39+
40+
def __init__(self, ntoken, ninp, nhid, nlayers, lr=0.001,
41+
dropout_ratio=0.5, clip_norm = 0.5, **kwargs):
42+
"""
43+
:param ntoken: #features(input to encoder)
44+
:param ninp: input_size to LSTM(output of encoder)
45+
:param nhid: hidden layers in LSTM
46+
:param nlayers:
47+
:param dropout:
48+
:param tie_weights:
49+
"""
50+
tf.reset_default_graph()
51+
self.data = tf.placeholder(tf.float32, [None, None, ntoken], name="data_")
52+
self.target = tf.placeholder(tf.float32, [None, None, ntoken], name="target_")
53+
self._ntoken = ntoken
54+
self._ninp = ninp
55+
self._nhid = nhid
56+
self._nlayers = nlayers
57+
# Setting to defaults known to work well
58+
self._lr = tf.placeholder_with_default(lr, shape=None,
59+
name="learn_rate_")
60+
self._dropout_ratio = tf.placeholder_with_default(dropout_ratio, shape=None,
61+
name="dropout_ratio_")
62+
self._clip_norm = tf.placeholder_with_default(clip_norm, shape=None,
63+
name="clip_norm_")
64+
self.tf_init = tf.global_variables_initializer
65+
self.prediction
66+
self.loss
67+
self.optimize
68+
69+
70+
@lazy_property
71+
def prediction(self):
72+
# Recurrent network.
73+
# Define weights
74+
weights = {
75+
'encoder': tf.Variable(tf.random_normal([self._ntoken, self._ninp]), name="enc_wt"),
76+
'decoder': tf.Variable(tf.random_normal([self._nhid, self._ntoken]), name="dec_wt")
77+
}
78+
biases = {
79+
'encoder': tf.Variable(tf.random_normal([self._ninp]), name="enc_bias"),
80+
'decoder': tf.Variable(tf.random_normal([self._ntoken]), name="dec_bias")
81+
}
82+
# Reshape inputs to feed to encoder
83+
bptt = tf.shape(self.data)[0]
84+
bsz = tf.shape(self.data)[1]
85+
input = tf.reshape(self.data, [bptt * bsz, self._ntoken])
86+
# Apply encoder to get workload-embeddings
87+
emb = tf.matmul(input, weights["encoder"]) + biases["encoder"]
88+
# Reshape embeddings to feed to Stacked LSTM
89+
emb = tf.reshape(emb, [bptt, bsz, self._ninp])
90+
stacked_lstm_cell = tf.nn.rnn_cell.\
91+
MultiRNNCell([tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.LSTMCell(self._nhid),
92+
output_keep_prob=self._dropout_ratio)
93+
for _ in range(self._nlayers)])
94+
# time_major true => time steps x batch size x features.
95+
# time_major false => batch_size x time_steps x features
96+
output, _ = tf.nn.dynamic_rnn(stacked_lstm_cell,
97+
emb, dtype=tf.float32,
98+
time_major=False)
99+
# Apply decoder to get output
100+
decoder_in = tf.reshape(output, [bptt * bsz, self._nhid])
101+
decoded = tf.matmul(decoder_in, weights["decoder"]) + biases["decoder"]
102+
pred = tf.reshape(decoded, [bptt, bsz, -1], name="pred_")
103+
return pred
104+
105+
@lazy_property
106+
def loss(self):
107+
loss = tf.reduce_mean(tf.squared_difference(self.target, self.prediction), name='lossOp_')
108+
return loss
109+
110+
@lazy_property
111+
def optimize(self):
112+
params = tf.trainable_variables()
113+
gradients = tf.gradients(self.loss, params)
114+
clipped_gradients, _ = tf.clip_by_global_norm(
115+
gradients, self._clip_norm)
116+
optimizer = tf.train.AdamOptimizer(learning_rate=self._lr)
117+
return optimizer.apply_gradients(zip(clipped_gradients,
118+
params), name="optimizeOp_")
119+
120+
def write_graph(self, dir):
121+
fname = "{}.pb".format(self.__repr__())
122+
abs_path = os.path.join(dir, fname)
123+
if not os.path.exists(abs_path):
124+
tf.train.write_graph(tf.get_default_graph(),
125+
dir, fname, False)
126+
127+
128+
def __repr__(self):
129+
return "LSTM"

0 commit comments

Comments
 (0)