Skip to content

Commit

Permalink
plt update net instance, test=model
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeref996 committed Aug 28, 2024
1 parent 7ca3088 commit aad05cd
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 35 deletions.
44 changes: 35 additions & 9 deletions framework/e2e/PaddleLT_new/engine/paddle_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LayerEval(object):
"""

# def __init__(self, testing, layerfile, device_id):
def __init__(self, testing, layerfile, device_place_id):
def __init__(self, testing, layerfile, device_place_id, upstream_net):
"""
初始化
"""
Expand All @@ -36,6 +36,8 @@ def __init__(self, testing, layerfile, device_place_id):
# paddle.set_device("{}:{}".format(str(self.device), str(device_id)))

self.testing = testing
self.upstream_net = upstream_net
self.return_net_instance = self.testing.get("return_net_instance", "False")
self.model_dtype = self.testing.get("model_dtype")
paddle.set_default_dtype(self.model_dtype)

Expand All @@ -53,7 +55,10 @@ def _net_input(self):
def _net_instant(self):
"""get net"""
reset(self.seed)
net = BuildLayer(layerfile=self.layerfile).get_layer()
if self.upstream_net:
net = self.upstream_net
else:
net = BuildLayer(layerfile=self.layerfile).get_layer()
return net

def _net_input_and_spec(self):
Expand Down Expand Up @@ -85,7 +90,10 @@ def dy_eval(self):
net = self._net_instant()
net.eval()
logit = net(*self._net_input())
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": net}
else:
return {"res": {"logit": logit}, "net": None}

def dy2st_eval(self):
"""dy2st eval"""
Expand All @@ -94,7 +102,10 @@ def dy2st_eval(self):
st_net = paddle.jit.to_static(net, full_graph=True)
st_net.eval()
logit = st_net(*data)
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": st_net}
else:
return {"res": {"logit": logit}, "net": None}

def dy2st_eval_inputspec(self):
"""dy2st eval"""
Expand All @@ -104,7 +115,10 @@ def dy2st_eval_inputspec(self):
st_net = paddle.jit.to_static(net, full_graph=True, input_spec=input_spec)
st_net.eval()
logit = st_net(*data)
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": st_net}
else:
return {"res": {"logit": logit}, "net": None}

def dy2st_eval_static_inputspec(self):
"""dy2st eval"""
Expand All @@ -114,7 +128,10 @@ def dy2st_eval_static_inputspec(self):
st_net = paddle.jit.to_static(net, full_graph=True, input_spec=input_spec)
st_net.eval()
logit = st_net(*data)
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": st_net}
else:
return {"res": {"logit": logit}, "net": None}

def dy2st_eval_cinn(self):
"""dy2st cinn eval"""
Expand All @@ -126,7 +143,10 @@ def dy2st_eval_cinn(self):
cinn_net = paddle.jit.to_static(net, build_strategy=build_strategy, full_graph=True)
cinn_net.eval()
logit = cinn_net(*data)
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": cinn_net}
else:
return {"res": {"logit": logit}, "net": None}

def dy2st_eval_cinn_inputspec(self):
"""dy2st cinn eval with inputspec"""
Expand All @@ -139,7 +159,10 @@ def dy2st_eval_cinn_inputspec(self):
cinn_net = paddle.jit.to_static(net, build_strategy=build_strategy, full_graph=True, input_spec=input_spec)
cinn_net.eval()
logit = cinn_net(*data)
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": cinn_net}
else:
return {"res": {"logit": logit}, "net": None}

def dy2st_eval_cinn_static_inputspec(self):
"""dy2st cinn eval with inputspec"""
Expand All @@ -152,7 +175,10 @@ def dy2st_eval_cinn_static_inputspec(self):
cinn_net = paddle.jit.to_static(net, build_strategy=build_strategy, full_graph=True, input_spec=input_spec)
cinn_net.eval()
logit = cinn_net(*data)
return {"logit": logit}
if self.return_net_instance == "True":
return {"res": {"logit": logit}, "net": cinn_net}
else:
return {"res": {"logit": logit}, "net": None}

# def dy2st_eval_cinn_inputspec_legacy_2(self):
# """dy2st cinn eval with inputspec"""
Expand Down
9 changes: 7 additions & 2 deletions framework/e2e/PaddleLT_new/engine/paddle_eval_bm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LayerEvalBM(object):
"""

# def __init__(self, testing, layerfile, device_id):
def __init__(self, testing, layerfile, device_place_id):
def __init__(self, testing, layerfile, device_place_id, upstream_net):
"""
初始化
"""
Expand All @@ -43,6 +43,8 @@ def __init__(self, testing, layerfile, device_place_id):
self.statis_round = 6

self.testing = testing
self.upstream_net = upstream_net
# self.return_net_instance = self.testing.get("return_net_instance", "False")
self.model_dtype = self.testing.get("model_dtype")
paddle.set_default_dtype(self.model_dtype)

Expand All @@ -53,7 +55,10 @@ def __init__(self, testing, layerfile, device_place_id):
def _net_instant(self):
"""get net and data"""
reset(self.seed)
net = BuildLayer(layerfile=self.layerfile).get_layer()
if self.upstream_net:
net = self.upstream_net
else:
net = BuildLayer(layerfile=self.layerfile).get_layer()
return net

def _set_cinn_flags(self):
Expand Down
9 changes: 7 additions & 2 deletions framework/e2e/PaddleLT_new/engine/paddle_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LayerExport(object):
构建Layer导出的通用类
"""

def __init__(self, testing, layerfile, device_place_id):
def __init__(self, testing, layerfile, device_place_id, upstream_net):
"""
初始化
"""
Expand All @@ -30,6 +30,8 @@ def __init__(self, testing, layerfile, device_place_id):
paddle.set_device(f"{self.device}:{device_place_id}")

self.testing = testing
self.upstream_net = upstream_net
# self.return_net_instance = self.testing.get("return_net_instance", "False")
self.model_dtype = self.testing.get("model_dtype")
paddle.set_default_dtype(self.model_dtype)

Expand All @@ -48,7 +50,10 @@ def _net_input(self):
def _net_instant(self):
"""get net"""
reset(self.seed)
net = BuildLayer(layerfile=self.layerfile).get_layer()
if self.upstream_net:
net = self.upstream_net
else:
net = BuildLayer(layerfile=self.layerfile).get_layer()
return net

def _net_input_and_spec(self):
Expand Down
2 changes: 1 addition & 1 deletion framework/e2e/PaddleLT_new/engine/paddle_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LayerInfer(object):
构建Layer预测的通用类
"""

def __init__(self, testing, layerfile, device_place_id):
def __init__(self, testing, layerfile, device_place_id, upstream_net):
"""
初始化
"""
Expand Down
66 changes: 51 additions & 15 deletions framework/e2e/PaddleLT_new/engine/paddle_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LayerTrain(object):
"""

# def __init__(self, testing, layerfile, device_id):
def __init__(self, testing, layerfile, device_place_id):
def __init__(self, testing, layerfile, device_place_id, upstream_net):
"""
初始化
"""
Expand All @@ -36,6 +36,8 @@ def __init__(self, testing, layerfile, device_place_id):
# paddle.set_device("{}:{}".format(str(self.device), str(device_id)))

self.testing = testing
self.upstream_net = upstream_net
self.return_net_instance = self.testing.get("return_net_instance", "False")
self.model_dtype = self.testing.get("model_dtype")
paddle.set_default_dtype(self.model_dtype)

Expand All @@ -51,7 +53,10 @@ def _net_input(self):
def _net_instant(self):
"""get net"""
reset(self.seed)
net = BuildLayer(layerfile=self.layerfile).get_layer()
if self.upstream_net:
net = self.upstream_net
else:
net = BuildLayer(layerfile=self.layerfile).get_layer()
return net

def _net_optimizer(self):
Expand Down Expand Up @@ -136,7 +141,10 @@ def dy_train(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

def dy_dp_train(self):
"""dygraph data parallel train"""
Expand Down Expand Up @@ -167,7 +175,11 @@ def dy_dp_train(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

# def dy_train_dl(self):
# """dygraph train with dataloader"""
Expand Down Expand Up @@ -218,7 +230,11 @@ def dy2st_train(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": st_net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

def dy2st_train_inputspec(self):
"""dy2st cinn train with inputspec"""
Expand All @@ -245,7 +261,11 @@ def dy2st_train_inputspec(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": st_net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

def dy2st_train_static_inputspec(self):
"""dy2st cinn train with inputspec"""
Expand All @@ -272,7 +292,11 @@ def dy2st_train_static_inputspec(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": st_net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

def dy2st_train_cinn(self):
"""dy2st cinn train"""
Expand All @@ -284,23 +308,27 @@ def dy2st_train_cinn(self):
net.train()
build_strategy = paddle.static.BuildStrategy()
build_strategy.build_cinn_pass = True
st_net = paddle.jit.to_static(net, build_strategy=build_strategy, full_graph=True)
cinn_net = paddle.jit.to_static(net, build_strategy=build_strategy, full_graph=True)

# 构建optimizer用于训练
if st_net.parameters():
opt = optimizer.get_opt(net=st_net)
if cinn_net.parameters():
opt = optimizer.get_opt(net=cinn_net)

for epoch in range(self.step):
logit = st_net(*data)
logit = cinn_net(*data)
# 构建loss用于训练
dy_loss = loss.get_loss(logit)
dy_loss.backward()
if st_net.parameters():
if cinn_net.parameters():
opt.step()
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": cinn_net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

def dy2st_train_cinn_inputspec(self):
"""dy2st cinn train with inputspec"""
Expand Down Expand Up @@ -329,7 +357,11 @@ def dy2st_train_cinn_inputspec(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": cinn_net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}

def dy2st_train_cinn_static_inputspec(self):
"""dy2st cinn train with inputspec"""
Expand Down Expand Up @@ -358,4 +390,8 @@ def dy2st_train_cinn_static_inputspec(self):
opt.clear_grad()

data_grad = self._get_data_grad(data)
return {"logit": logit, "data_grad": data_grad}
# return {"logit": logit, "data_grad": data_grad}
if self.return_net_instance == "True":
return {"res": {"logit": logit, "data_grad": data_grad}, "net": cinn_net}
else:
return {"res": {"logit": logit, "data_grad": data_grad}, "net": None}
9 changes: 7 additions & 2 deletions framework/e2e/PaddleLT_new/engine/paddle_train_bm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LayerTrainBM(object):
"""

# def __init__(self, testing, layerfile, device_id):
def __init__(self, testing, layerfile, device_place_id):
def __init__(self, testing, layerfile, device_place_id, upstream_net):
"""
初始化
"""
Expand All @@ -45,6 +45,8 @@ def __init__(self, testing, layerfile, device_place_id):
self.statis_round = 6

self.testing = testing
self.upstream_net = upstream_net
# self.return_net_instance = self.testing.get("return_net_instance", "False")
self.model_dtype = self.testing.get("model_dtype")
paddle.set_default_dtype(self.model_dtype)

Expand All @@ -56,7 +58,10 @@ def __init__(self, testing, layerfile, device_place_id):
def _net_instant(self):
"""get net and data"""
reset(self.seed)
net = BuildLayer(layerfile=self.layerfile).get_layer()
if self.upstream_net:
net = self.upstream_net
else:
net = BuildLayer(layerfile=self.layerfile).get_layer()
return net

def _net_optimizer(self):
Expand Down
Loading

0 comments on commit aad05cd

Please sign in to comment.