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

Commit 794a081

Browse files
authored
Merge branch 'master' into cost_model_txn_fix
2 parents 8248e68 + 7699877 commit 794a081

File tree

10 files changed

+367
-17
lines changed

10 files changed

+367
-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: 93 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,51 @@ 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+
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
59+
tar -xzf protobuf-cpp-3.4.0.tar.gz
60+
cd protobuf-3.4.0
61+
./autogen.sh && ./configure && make -j4 && sudo make install && sudo ldconfig
62+
cd ..
63+
# Cleanup
64+
rm -rf protobuf-3.4.0 protobuf-cpp-3.4.0.tar.gz
65+
}
66+
67+
# Utility function for installing tensorflow components of python/C++
68+
function install_tf() {
69+
TFCApiFile=$1
70+
TFBinaryURL=$2
71+
LinkerConfigCmd=$3
72+
TARGET_DIRECTORY="/usr/local"
73+
# Install Tensorflow Python Binary
74+
sudo -E pip3 install --upgrade ${TFBinaryURL}
75+
76+
# Install C-API
77+
TFCApiURL="https://storage.googleapis.com/tensorflow/libtensorflow/${TFCApiFile}"
78+
wget -O $TFCApiFile $TFCApiURL
79+
sudo tar -C $TARGET_DIRECTORY -xzf $TFCApiFile || true
80+
# Configure the Linker
81+
eval $LinkerConfigCmd
82+
# Cleanup
83+
rm -rf ${TFCApiFile}
84+
}
3985

4086
## ------------------------------------------------
4187
## UBUNTU
@@ -75,13 +121,19 @@ if [ "$DISTRO" = "UBUNTU" ]; then
75121
if [ "$MAJOR_VER" == "14" ]; then
76122
PKG_CMAKE="cmake3"
77123
FORCE_Y="--force-yes"
124+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp34-cp34m-linux_x86_64.whl"
125+
fi
126+
if [ "$MAJOR_VER" == "16" ]; then
127+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp35-cp35m-linux_x86_64.whl"
78128
fi
79129
# Fix for llvm on Ubuntu 17.x
80130
if [ "$MAJOR_VER" == "17" ]; then
81131
PKG_LLVM="llvm-3.9"
82132
PKG_CLANG="clang-3.8"
133+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp35-cp35m-linux_x86_64.whl"
83134
fi
84-
135+
TFCApiFile="libtensorflow-${TF_TYPE}-linux-x86_64-${TF_VERSION}.tar.gz"
136+
LinkerConfigCmd="sudo ldconfig"
85137
sudo apt-get -qq $FORCE_Y --ignore-missing -y install \
86138
$PKG_CMAKE \
87139
$PKG_LLVM \
@@ -92,9 +144,7 @@ if [ "$DISTRO" = "UBUNTU" ]; then
92144
flex \
93145
valgrind \
94146
lcov \
95-
protobuf-compiler \
96147
libgflags-dev \
97-
libprotobuf-dev \
98148
libevent-dev \
99149
libboost-dev \
100150
libboost-thread-dev \
@@ -103,7 +153,19 @@ if [ "$DISTRO" = "UBUNTU" ]; then
103153
libpqxx-dev \
104154
libedit-dev \
105155
libssl-dev \
106-
postgresql-client
156+
postgresql-client \
157+
python3-pip \
158+
curl \
159+
autoconf \
160+
automake \
161+
libtool \
162+
make \
163+
g++ \
164+
unzip
165+
# Install version of protobuf needed by C-API
166+
install_protobuf3.4.0 "ubuntu"
167+
# Install tensorflow
168+
install_tf "$TFCApiFile" "$TFBinaryURL" "$LinkerConfigCmd"
107169

108170
## ------------------------------------------------
109171
## DEBIAN
@@ -140,14 +202,15 @@ elif [[ "$DISTRO" == *"FEDORA"* ]]; then
140202
26) LLVM="llvm";;
141203
*) LLVM="llvm4.0";;
142204
esac
143-
205+
TFCApiFile="libtensorflow-${TF_TYPE}-linux-x86_64-${TF_VERSION}.tar.gz"
206+
TFBinaryURL="https://storage.googleapis.com/tensorflow/linux/${TF_TYPE}/tensorflow-${TF_VERSION}-cp36-cp36m-linux_x86_64.whl"
207+
LinkerConfigCmd="sudo ldconfig"
144208
sudo dnf -q install -y \
145209
git \
146210
gcc-c++ \
147211
make \
148212
cmake \
149213
gflags-devel \
150-
protobuf-devel \
151214
bison \
152215
flex \
153216
libevent-devel \
@@ -166,7 +229,16 @@ elif [[ "$DISTRO" == *"FEDORA"* ]]; then
166229
libasan \
167230
libtsan \
168231
libubsan \
169-
libatomic
232+
libatomic \
233+
python3-pip \
234+
curl \
235+
autoconf \
236+
automake \
237+
libtool
238+
# Install version of protobuf needed by C-API
239+
install_protobuf3.4.0 "fedora"
240+
# Install tensorflow
241+
install_tf "$TFCApiFile" "$TFBinaryURL" "$LinkerConfigCmd"
170242

171243
## ------------------------------------------------
172244
## REDHAT
@@ -194,18 +266,17 @@ elif [[ "$DISTRO" == *"REDHAT"* ]] && [[ "${DISTRO_VER%.*}" == "7" ]]; then
194266
fi
195267
popd; popd
196268
return 0
197-
}
269+
}
198270

199-
# Package download paths
271+
#Package download paths
200272
PKGS=(
201273
"https://github.com/schuhschuh/gflags/archive/v2.0.tar.gz"
202274
)
203-
204-
# Add EPEL repository first
275+
#Add EPEL repository first
205276
sudo yum -q -y install epel-release
206277
sudo yum -q -y upgrade epel-release
207278

208-
# Simple installations via yum
279+
#Simple installations via yum
209280
sudo yum -q -y install \
210281
git \
211282
gcc-c++ \
@@ -245,7 +316,9 @@ elif [ "$DISTRO" = "DARWIN" ]; then
245316
echo "Installing homebrew..."
246317
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
247318
fi
248-
319+
TFBinaryURL="https://storage.googleapis.com/tensorflow/mac/${TF_TYPE}/tensorflow-${TF_VERSION}-py3-none-any.whl"
320+
TFCApiFile="libtensorflow-${TF_TYPE}-darwin-x86_64-${TF_VERSION}.tar.gz"
321+
LinkerConfigCmd="sudo update_dyld_shared_cache"
249322
brew install git
250323
brew install cmake
251324
brew install gflags
@@ -260,6 +333,13 @@ elif [ "$DISTRO" = "DARWIN" ]; then
260333
brew install libedit
261334
brew install [email protected]
262335
brew install postgresql
336+
brew install curl
337+
brew install wget
338+
brew install python
339+
brew upgrade python
340+
# Brew installs correct version of Protobuf(3.5.1 >= 3.4.0)
341+
# So we can directly install tensorflow
342+
install_tf "$TFCApiFile" "$TFBinaryURL" "$LinkerConfigCmd"
263343

264344
## ------------------------------------------------
265345
## 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)