Skip to content
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

[Dy2St] 移除 ProgramTranslator #49628

Merged
merged 21 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 1 addition & 1 deletion python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def __impl__(*args, **kwargs):
raise AssertionError(
"'%s' only can be called by `paddle.Tensor` in dynamic graph mode. Suggestions:\n"
" 1. If you are in static graph mode, you can switch to dynamic graph mode by turning off `paddle.enable_static()` or calling `paddle.disable_static()`.\n"
" 2. If you are using `@paddle.jit.to_static`, you can turn off ProgramTranslator by calling `paddle.jit.ProgramTranslator().enable(False)`. "
" 2. If you are using `@paddle.jit.to_static`, you can call `paddle.jit.enable_to_static(False)`. "
"If you have to translate dynamic graph to static graph, please use other API to replace '%s'."
% (func.__name__, func.__name__)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import paddle
import paddle.fluid as fluid
from paddle.jit import ProgramTranslator
from paddle.jit.api import to_static


Expand All @@ -35,7 +34,7 @@ def dyfunc_assert_non_variable(x=True):

class TestAssertVariable(unittest.TestCase):
def _run(self, func, x, with_exception, to_static):
ProgramTranslator().enable(to_static)
paddle.jit.enable_to_static(to_static)
if with_exception:
with self.assertRaises(BaseException):
with fluid.dygraph.guard():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@

import paddle
import paddle.fluid as fluid
from paddle.jit import ProgramTranslator
from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX

program_translator = ProgramTranslator()
place = (
fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace()
)
Expand Down Expand Up @@ -127,11 +125,11 @@ def train(self, bert_config, data_reader, to_static):
return loss, ppl

def train_dygraph(self, bert_config, data_reader):
program_translator.enable(False)
paddle.jit.enable_to_static(False)
return self.train(bert_config, data_reader, False)

def train_static(self, bert_config, data_reader):
program_translator.enable(True)
paddle.jit.enable_to_static(True)
return self.train(bert_config, data_reader, True)

def predict_static(self, data):
Expand All @@ -157,7 +155,7 @@ def predict_static(self, data):
return pred_res

def predict_dygraph(self, bert_config, data):
program_translator.enable(False)
paddle.jit.enable_to_static(False)
with fluid.dygraph.guard(place):
bert = PretrainModelLayer(
config=bert_config, weight_sharing=False, use_fp16=False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
import paddle.fluid as fluid
from paddle.fluid import ParamAttr
from paddle.fluid.dygraph import to_variable
from paddle.jit import ProgramTranslator, to_static
from paddle.jit import to_static
from paddle.jit.translated_layer import INFER_MODEL_SUFFIX, INFER_PARAMS_SUFFIX

SEED = 2000
DATATYPE = 'float32'
program_translator = ProgramTranslator()

# Note: Set True to eliminate randomness.
# 1. For one operation, cuDNN has several algorithms,
Expand Down Expand Up @@ -662,7 +661,7 @@ def tearDown(self):
self.temp_dir.cleanup()

def train_bmn(self, args, place, to_static):
program_translator.enable(to_static)
paddle.jit.enable_to_static(to_static)
loss_data = []

with fluid.dygraph.guard(place):
Expand Down Expand Up @@ -822,7 +821,7 @@ def verify_predict(self):
break

def predict_dygraph(self, data):
program_translator.enable(False)
paddle.jit.enable_to_static(False)
with fluid.dygraph.guard(self.place):
bmn = BMN(self.args)
# load dygraph trained parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import paddle
import paddle.fluid as fluid
from paddle.jit.api import to_static
from paddle.jit.dy2static.program_translator import ProgramTranslator
from paddle.jit.dy2static.utils import Dygraph2StaticException

SEED = 2020
Expand All @@ -35,10 +34,10 @@ def setUp(self):
def test_error(self):
if self.dyfunc:
with self.assertRaisesRegex(Dygraph2StaticException, self.error):
ProgramTranslator().enable(True)
paddle.jit.enable_to_static(True)
self.assertTrue(to_static(self.dyfunc)(self.x))
paddle.fluid.dygraph.base._in_declarative_mode_ = False
ProgramTranslator().enable(False)
paddle.jit.enable_to_static(False)


def test_continue_in_for(x):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
from test_resnet import ResNetHelper

import paddle
from paddle.jit import ProgramTranslator

program_translator = ProgramTranslator()


class TestResnetWithPass(unittest.TestCase):
Expand All @@ -35,7 +32,7 @@ def setUp(self):
paddle.fluid.set_flags({"FLAGS_max_inplace_grad_add": 8})

def train(self, to_static):
program_translator.enable(to_static)
paddle.jit.enable_to_static(to_static)
return self.resnet_helper.train(to_static, self.build_strategy)

def verify_predict(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import paddle
import paddle.fluid as fluid
from paddle.jit import ProgramTranslator
from paddle.jit.api import to_static
from paddle.jit.dy2static import convert_to_static

Expand Down Expand Up @@ -91,8 +90,7 @@ def train_dygraph(self):
return self.train(to_static=False)

def train(self, to_static=False):
prog_trans = ProgramTranslator()
prog_trans.enable(to_static)
paddle.jit.enable_to_static(to_static)

with fluid.dygraph.guard(fluid.CPUPlace()):
dygraph_net = self.dygraph_class()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
import paddle
import paddle.fluid as fluid
import paddle.jit.dy2static as _jst
from paddle.jit import ProgramTranslator
from paddle.jit.dy2static.convert_call_func import CONVERSION_OPTIONS

program_translator = ProgramTranslator()

SEED = 2020
np.random.seed(SEED)

Expand Down Expand Up @@ -93,13 +90,13 @@ def init_test_func(self):
self.dyfunc = nested_func

def get_dygraph_output(self):
program_translator.enable(False)
paddle.jit.enable_to_static(False)
with fluid.dygraph.guard():
res = self.dyfunc(self.input).numpy()
return res

def get_static_output(self):
program_translator.enable(True)
paddle.jit.enable_to_static(True)
with fluid.dygraph.guard():
res = self.dyfunc(self.input).numpy()
return res
Expand Down Expand Up @@ -193,11 +190,11 @@ def _run(self):
return res.numpy()

def get_dygraph_output(self):
program_translator.enable(False)
paddle.jit.enable_to_static(False)
return self._run()

def get_static_output(self):
program_translator.enable(True)
paddle.jit.enable_to_static(True)
return self._run()

def test_transformed_static_result(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import paddle
from paddle.fluid.dygraph import to_variable
from paddle.jit import ProgramTranslator
from paddle.jit.api import to_static
from paddle.nn import BatchNorm

Expand All @@ -61,8 +60,6 @@
IMAGE_SIZE = 64
SEED = 2020

program_translator = ProgramTranslator()


class Cycle_Gan(fluid.dygraph.Layer):
def __init__(self, input_channel, istrain=True):
Expand Down Expand Up @@ -560,7 +557,7 @@ def train(args, to_static):
else fluid.CPUPlace()
)

program_translator.enable(to_static)
paddle.jit.enable_to_static(to_static)

with fluid.dygraph.guard(place):
max_images_num = args.max_images_num
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
import paddle
import paddle.fluid as fluid
from paddle.fluid.dygraph import Layer, to_variable
from paddle.jit import ProgramTranslator
from paddle.jit.api import to_static
from paddle.jit.dy2static.program_translator import (
ConcreteProgram,
StaticFunction,
)
from paddle.static import InputSpec

program_trans = ProgramTranslator()


class SimpleNet(Layer):
def __init__(self):
Expand Down Expand Up @@ -210,7 +207,7 @@ def foo_func(a, b, c=1, d=2):

class TestDifferentInputSpecCacheProgram(unittest.TestCase):
def setUp(self):
program_trans.enable(True)
paddle.jit.enable_to_static(True)

def test_with_different_input(self):
with fluid.dygraph.guard(fluid.CPUPlace()):
Expand Down Expand Up @@ -357,7 +354,7 @@ def test_error(self):
with self.assertRaises(RuntimeError):
func(np.ones(5).astype("int32"))

program_trans.enable(False)
paddle.jit.enable_to_static(False)
with self.assertRaises(AssertionError):
# AssertionError: We Only support to_variable in imperative mode,
# please use fluid.dygraph.guard() as context to run it in imperative Mode
Expand All @@ -367,7 +364,7 @@ def test_error(self):
class TestDecorateModelDirectly(unittest.TestCase):
def setUp(self):
paddle.disable_static()
program_trans.enable(True)
paddle.jit.enable_to_static(True)
self.x = to_variable(np.ones([4, 10]).astype('float32'))

def test_fake_input(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import paddle
import paddle.fluid as fluid
from paddle.jit import to_static
from paddle.jit.dy2static.program_translator import ProgramTranslator

PLACE = (
fluid.CUDAPlace(0) if fluid.is_compiled_with_cuda() else fluid.CPUPlace()
Expand Down Expand Up @@ -135,8 +134,7 @@ def _run_dygraph(self):
return self.train(to_static=False)

def train(self, to_static=False):
prog_trans = ProgramTranslator()
prog_trans.enable(to_static)
paddle.jit.enable_to_static(to_static)
with fluid.dygraph.guard(PLACE):
net = MainNetWithDict(batch_size=self.batch_size)
ret = net(self.x)
Expand Down Expand Up @@ -191,8 +189,7 @@ def _run_dygraph(self):
return self._run(to_static=False)

def _run(self, to_static):
prog_trans = ProgramTranslator()
prog_trans.enable(to_static)
paddle.jit.enable_to_static(to_static)

result = self.dygraph_func(self.input)

Expand Down Expand Up @@ -237,8 +234,7 @@ def setUp(self):
self.x = np.array([2, 2]).astype('float32')

def train(self, to_static=False):
prog_trans = ProgramTranslator()
prog_trans.enable(to_static)
paddle.jit.enable_to_static(to_static)
with fluid.dygraph.guard(PLACE):
net = NetWithDictPop()
ret = net(z=0, x=self.x, y=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ def setUp(self):
self.filepath = inspect.getfile(unwrap(self.func_call))
self.set_exception_type()
self.set_message()
self.prog_trans = paddle.jit.ProgramTranslator()

def set_input(self):
self.input = np.ones([3, 2])
Expand Down Expand Up @@ -364,30 +363,6 @@ def set_message(self):
]


# Situation 2: Call ProgramTranslator().get_output(...) to use Dynamic-to-Static
class TestErrorGetOutputInCompiletime(TestErrorStaticLayerCallInCompiletime):
def set_func_call(self):
self.func_call = lambda: self.prog_trans.get_output(
unwrap(self.func), self.input
)


class TestErrorGetOutputInCompiletime_2(
TestErrorStaticLayerCallInCompiletime_2
):
def set_func_call(self):
self.func_call = lambda: self.prog_trans.get_output(
unwrap(self.func), self.input
)


class TestErrorGetOutputInRuntime(TestErrorStaticLayerCallInRuntime):
def set_func_call(self):
self.func_call = lambda: self.prog_trans.get_output(
unwrap(self.func), self.input
)


class TestJitSaveInCompiletime(TestErrorBase):
def setUp(self):
self.reset_flags_to_default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import paddle
import paddle.fluid as fluid
from paddle.jit import ProgramTranslator
from paddle.jit.api import to_static

SEED = 2020
Expand Down Expand Up @@ -68,8 +67,7 @@ def setUp(self):
self.data = np.random.random((1, 2, 4, 4)).astype('float32')

def train(self, to_static=False):
program_translator = ProgramTranslator()
program_translator.enable(to_static)
paddle.jit.enable_to_static(to_static)

with fluid.dygraph.guard():
dy_layer = self.dygraph_class()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@

import paddle
import paddle.fluid as fluid
from paddle.jit import ProgramTranslator
from paddle.static import InputSpec

program_translator = ProgramTranslator()


# 0. for in range var.numpy()[0]
@paddle.jit.to_static
Expand Down Expand Up @@ -363,7 +360,7 @@ def set_test_func(self):
)

def _run(self, to_static):
program_translator.enable(to_static)
paddle.jit.enable_to_static(to_static)
with fluid.dygraph.guard():
return self.dygraph_func(self.input)

Expand All @@ -390,7 +387,7 @@ def transformed_result_compare(self):

class TestTransformForOriginalList(TestTransform):
def _run(self, to_static):
program_translator.enable(to_static)
paddle.jit.enable_to_static(to_static)
with fluid.dygraph.guard():
return self.dygraph_func()

Expand Down
Loading