Skip to content

Run fit-a-line demo with fault tolerant mode #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions demo/fit_a_line/train_ft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import paddle.v2 as paddle
import os
import gzip
from paddle.v2.reader.creator import cloud_reader
import paddle.v2.dataset.uci_housing as uci_housing

etcd_ip = os.getenv("ETCD_IP")
etcd_endpoint = "http://" + etcd_ip + ":" + "2379"
trainer_id = int(os.getenv("PADDLE_INIT_TRAINER_ID"))

def main():
# init
paddle.init()

# network config
x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
cost = paddle.layer.mse_cost(input=y_predict, label=y)

# create parameters
parameters = paddle.parameters.create(cost)

# create optimizer
optimizer = paddle.optimizer.Momentum(momentum=0)

trainer = paddle.trainer.SGD(
cost=cost,
parameters=parameters,
update_equation=optimizer,
is_local=False,
pserver_spec=etcd_endpoint,
use_etcd=True)

feeding = {'x': 0, 'y': 1}

# event_handler to print training and testing info
def event_handler(event):
if isinstance(event, paddle.event.EndIteration):
if event.batch_id % 100 == 0:
print "Pass %d, Batch %d, Cost %f" % (
event.pass_id, event.batch_id, event.cost)

if isinstance(event, paddle.event.EndPass):
result = trainer.test(
reader=paddle.batch(uci_housing.test(), batch_size=2),
feeding=feeding)
print "Test %d, Cost %f" % (event.pass_id, result.cost)
if trainer_id == "0":
with gzip.open("fit-a-line_pass_%05d.tar.gz" % event.pass_id,
"w") as f:
parameters.to_tar(f)
# training
trainer.train(
reader=paddle.batch(
paddle.reader.shuffle(cloud_reader(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use paddle.v2.reader.creator.recordio here? (it wraps cloud_reader, providing a uniform interface for local and cloud recordio file).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I could not find paddle.v2.reader.creator.recordio, I remember we have it. Probably I got confused.

["/pfs/dlnel/public/dataset/uci_housing/uci_housing_train-*"],
etcd_endpoint), buf_size=500),
batch_size=2),
feeding=feeding,
event_handler=event_handler,
num_passes=30)


if __name__ == '__main__':
main()
13 changes: 4 additions & 9 deletions docker/k8s_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,10 @@ def fetch_pserver_ips():
return ",".join(pserver_ips)

def fetch_master_ip():
while True:
label_selector = "paddle-job-master=%s" % PADDLE_JOB_NAME
pod_list = fetch_pods_info(label_selector)
master_ip = ""
if len(pod_list) >=1:
master_ip = pod_list[0][1]
if master_ip:
return master_ip
time.sleep(5)
label_selector = "paddle-job-master=%s" % PADDLE_JOB_NAME
pod_list = fetch_pods_info(label_selector)
master_ips = [item[1] for item in pod_list]
return master_ips[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May return None when master is still not ready? Curious why need to remove the while True waiting?

Copy link
Collaborator Author

@Yancey0623 Yancey0623 Aug 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the code:https://github.com/PaddlePaddle/cloud/pull/278/files#diff-548cc24bbda04b9e89570c52ae5df9c7R48, we waiting for the master pod until the state became RUNNING.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.


def fetch_trainer_id():
label_selector = "paddle-job=%s" % PADDLE_JOB_NAME
Expand Down
29 changes: 28 additions & 1 deletion docker/paddle_k8s
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ start_pserver() {
}

start_new_pserver() {
stdbuf -oL python /root/k8s_tools.py wait_pods_running paddle-job-master=${PADDLE_JOB_NAME} 1
export MASTER_IP=$(python /root/k8s_tools.py fetch_master_ip)
stdbuf -oL /usr/bin/pserver \
-port=$PADDLE_INIT_PORT \
-num-pservers=$PSERVERS \
-log-level=debug \
-etcd-endpoint=http://$PADDLE_INIT_MASTER_IP:2379
-etcd-endpoint=http://$MASTER_IP:2379
}

start_master() {
Expand All @@ -43,6 +44,27 @@ check_trainer_ret() {
exit $ret
}

start_new_trainer() {
stdbuf -oL python /root/k8s_tools.py wait_pods_running paddle-job-master=${PADDLE_JOB_NAME} 1
# FIXME: use etcd lock instead of trainer id
stdbuf -oL python /root/k8s_tools.py wait_pods_running paddle-job=${PADDLE_JOB_NAME} ${TRAINERS}

export MASTER_IP=$(python /root/k8s_tools.py fetch_master_ip)
export ETCD_IP="$MASTER_IP"
export PADDLE_INIT_TRAINER_ID=$(python /root/k8s_tools.py fetch_trainer_id)

# NOTE: $TRAINER_PACKAGE may be large, do not copy
export PYTHONPATH=$TRAINER_PACKAGE:$PYTHONPATH
cd $TRAINER_PACKAGE

stdbuf -oL echo "Starting training job: " $TRAINER_PACKAGE, "num_gradient_servers:" \
$PADDLE_INIT_NUM_GRADIENT_SERVERS, "trainer_id: " $PADDLE_INIT_TRAINER_ID, \
"version: " $1

stdbuf -oL sh -c "${ENTRY}"
check_trainer_ret $?
}

start_trainer() {
stdbuf -oL python /root/k8s_tools.py wait_pods_running paddle-job-pserver=${PADDLE_JOB_NAME} ${PSERVERS}
stdbuf -oL python /root/k8s_tools.py wait_pods_running paddle-job=${PADDLE_JOB_NAME} ${TRAINERS}
Expand Down Expand Up @@ -98,6 +120,8 @@ usage() {
echo "usage: paddle_k8s [<args>]:"
echo " start_trainer [v1|v2] Start a trainer process with v1 or v2 API"
echo " start_pserver Start a pserver process"
echo " start_new_pserver Start a new pserver process"
echo " start_new_trainer Start a new triner process"
}

case "$1" in
Expand All @@ -107,6 +131,9 @@ case "$1" in
start_trainer)
start_trainer $2
;;
start_new_trainer)
start_new_trainer
;;
start_new_pserver)
start_new_pserver
;;
Expand Down
9 changes: 5 additions & 4 deletions paddlecloud/paddlejob/paddle_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self,
volumes=[],
registry_secret=None,
envs = {},
new_pserver=True,
fault_tolerant=False,
etcd_image="quay.io/coreos/etcd:v3.2.1"):

self._ports_num=1
Expand All @@ -54,7 +54,7 @@ def __init__(self,
self._mastercpu = 1
self._mastermemory = "300Mi"
# use new pserver for tolerant
self._new_pserver = new_pserver
self._fault_tolerant = fault_tolerant
self._etcd_image = etcd_image

@property
Expand Down Expand Up @@ -92,7 +92,6 @@ def get_env(self):
envs.append({"name":"PADDLE_INIT_PORTS_NUM_FOR_SPARSE", "value":str(self._ports_num_for_sparse)})
envs.append({"name":"PADDLE_INIT_NUM_GRADIENT_SERVERS", "value":str(self._num_gradient_servers)})
envs.append({"name":"PADDLE_INIT_NUM_PASSES", "value":str(self._passes)})

if self._gpu:
envs.append({"name":"PADDLE_INIT_USE_GPU", "value":str("1")})
# HACK: add nvidia lib LD_LIBRARY_PATH for all pods
Expand Down Expand Up @@ -131,13 +130,15 @@ def _get_master_entrypoint(self):
return ["paddle_k8s", "start_master"]

def _get_pserver_entrypoint(self):
if not self._new_pserver:
if not self._fault_tolerant:
return ["paddle_k8s", "start_pserver"]
else:
return ["paddle_k8s", "start_new_pserver"]

def _get_trainer_entrypoint(self):
if self._entry:
if self._fault_tolerant:
return ["paddle_k8s", "start_new_trainer"]
return ["paddle_k8s", "start_trainer", "v2"]
return ["paddle_k8s", "start_trainer", "v1"]

Expand Down
2 changes: 1 addition & 1 deletion paddlecloud/paddlejob/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def post(self, request, format=None):
registry_secret = registry_secret,
volumes = volumes,
envs = envs,
new_pserver = fault_tolerant,
fault_tolerant = fault_tolerant,
etcd_image = settings.ETCD_IMAGE
)
# ========== submit master ReplicaSet if using fault_tolerant feature ==
Expand Down