diff --git a/framework/e2e/PaddleLT_new/engine/paddle_eval.py b/framework/e2e/PaddleLT_new/engine/paddle_eval.py index 19df5debf4..0c9f3e564a 100644 --- a/framework/e2e/PaddleLT_new/engine/paddle_eval.py +++ b/framework/e2e/PaddleLT_new/engine/paddle_eval.py @@ -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): """ 初始化 """ @@ -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) @@ -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): @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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""" diff --git a/framework/e2e/PaddleLT_new/engine/paddle_eval_bm.py b/framework/e2e/PaddleLT_new/engine/paddle_eval_bm.py index 3426333302..5bf70b6fe4 100644 --- a/framework/e2e/PaddleLT_new/engine/paddle_eval_bm.py +++ b/framework/e2e/PaddleLT_new/engine/paddle_eval_bm.py @@ -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): """ 初始化 """ @@ -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) @@ -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): diff --git a/framework/e2e/PaddleLT_new/engine/paddle_export.py b/framework/e2e/PaddleLT_new/engine/paddle_export.py index 8a54af176d..cf91bd9328 100644 --- a/framework/e2e/PaddleLT_new/engine/paddle_export.py +++ b/framework/e2e/PaddleLT_new/engine/paddle_export.py @@ -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): """ 初始化 """ @@ -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) @@ -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): diff --git a/framework/e2e/PaddleLT_new/engine/paddle_infer.py b/framework/e2e/PaddleLT_new/engine/paddle_infer.py index 8ac3b5be7e..a3e4093eeb 100644 --- a/framework/e2e/PaddleLT_new/engine/paddle_infer.py +++ b/framework/e2e/PaddleLT_new/engine/paddle_infer.py @@ -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): """ 初始化 """ diff --git a/framework/e2e/PaddleLT_new/engine/paddle_train.py b/framework/e2e/PaddleLT_new/engine/paddle_train.py index 3480f6f686..304277121a 100644 --- a/framework/e2e/PaddleLT_new/engine/paddle_train.py +++ b/framework/e2e/PaddleLT_new/engine/paddle_train.py @@ -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): """ 初始化 """ @@ -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) @@ -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): @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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""" @@ -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} diff --git a/framework/e2e/PaddleLT_new/engine/paddle_train_bm.py b/framework/e2e/PaddleLT_new/engine/paddle_train_bm.py index 82384f554a..c17564a189 100644 --- a/framework/e2e/PaddleLT_new/engine/paddle_train_bm.py +++ b/framework/e2e/PaddleLT_new/engine/paddle_train_bm.py @@ -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): """ 初始化 """ @@ -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) @@ -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): diff --git a/framework/e2e/PaddleLT_new/layertest.py b/framework/e2e/PaddleLT_new/layertest.py index 5a6ea908d1..812998e7b7 100644 --- a/framework/e2e/PaddleLT_new/layertest.py +++ b/framework/e2e/PaddleLT_new/layertest.py @@ -59,7 +59,7 @@ def del_core_dump(self): # 如果删除过程中发生错误(比如文件不存在或没有权限),则打印错误信息 self.logger.get_log().warning(f"Error deleting {filepath}: {e.strerror}") - def _single_run(self, testing, layerfile, device_place_id=0): + def _single_run(self, testing, layerfile, device_place_id=0, upstream_net=None): """ 单次执行器测试 :param testing: 'dy_train', 'dy_eval'... @@ -78,7 +78,10 @@ def _single_run(self, testing, layerfile, device_place_id=0): self.logger.get_log().info(f"testing engine has been covered. Real engine is: {engine}") layer_test = engine_map[engine]( - testing=self.testings.get(testing), layerfile=layerfile, device_place_id=device_place_id + testing=self.testings.get(testing), + layerfile=layerfile, + device_place_id=device_place_id, + upstream_net=upstream_net, ) res = getattr(layer_test, engine)() return res @@ -90,14 +93,18 @@ def _case_run(self): exc_func = 0 exc = 0 res_dict = {} + net = None compare_res_list = [] self.logger.get_log().info("测试case名称: {}".format(self.title)) fail_testing_list = [] for testing in self.testings_list: try: self.logger.get_log().info("测试执行器: {}".format(testing)) - res = self._single_run(testing=testing, layerfile=self.layerfile, device_place_id=self.device_place_id) - res_dict[testing] = res + res = self._single_run( + testing=testing, layerfile=self.layerfile, device_place_id=self.device_place_id, upstream_net=net + ) + res_dict[testing] = res["res"] + net = res["net"] if os.environ.get("PLT_SAVE_GT") == "True": # 开启gt保存 gt_path = os.path.join("plt_gt", os.environ.get("PLT_SET_DEVICE"), testing) if not os.path.exists(gt_path): diff --git a/framework/e2e/PaddleLT_new/yaml/dy^dy2stcinn_train^export_cinn_st_inputspec^ppinfer_new_exc_pir.yml b/framework/e2e/PaddleLT_new/yaml/dy^dy2stcinn_train^export_cinn_st_inputspec^ppinfer_new_exc_pir.yml new file mode 100644 index 0000000000..04b8c30b17 --- /dev/null +++ b/framework/e2e/PaddleLT_new/yaml/dy^dy2stcinn_train^export_cinn_st_inputspec^ppinfer_new_exc_pir.yml @@ -0,0 +1,38 @@ +testings: + dy_train: + model_dtype: "float32" + Loss: + loss_name: "diy.loss.diy_loss.mean_loss" + optimizer: + optimizer_name: "diy.optimizer.diy_opt.naive_opt" + params: + opt_api: "paddle.optimizer.SGD" + learning_rate: 0.00001 + step: 20 + + dy2st_train_cinn: + model_dtype: "float32" + Loss: + loss_name: "diy.loss.diy_loss.mean_loss" + optimizer: + optimizer_name: "diy.optimizer.diy_opt.naive_opt" + params: + opt_api: "paddle.optimizer.SGD" + learning_rate: 0.00001 + step: 20 + return_net_instance: "True" # 测试engine会返回模型实例 + + jit_save_cinn_static_inputspec: + use_upstream_net_instance: "True" # 获取上游模型实例而不是重新构建模型实例 + model_dtype: "float32" + + paddle_infer_new_exc_pir: + jit_save_type: "jit_save_cinn_static_inputspec" + +compare: + - + baseline: 'dy_eval' + latest: 'paddle_infer_new_exc_pir' + precision: + delta: 0.00001 + rtol: 0.000001