From 34079a807b25c1bb37b7d8149dbf351f57d6565b Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 7 Jun 2024 06:19:18 +0000 Subject: [PATCH 01/28] fix pir distribution --- python/paddle/base/data_feeder.py | 4 +-- python/paddle/distribution/bernoulli.py | 12 ++++----- python/paddle/distribution/categorical.py | 2 +- python/paddle/distribution/cauchy.py | 17 +++++++++---- python/paddle/distribution/distribution.py | 5 ++++ python/paddle/distribution/exponential.py | 2 +- python/paddle/distribution/gamma.py | 4 +-- python/paddle/distribution/geometric.py | 25 +++++++++++++------ python/paddle/distribution/gumbel.py | 12 ++++++--- python/paddle/distribution/transform.py | 12 ++++++--- python/paddle/distribution/uniform.py | 20 +++++++++++++-- .../test_distribution_bernoulli_static.py | 6 ++--- .../test_distribution_beta_static.py | 4 +++ .../test_distribution_cauchy_static.py | 6 ++--- .../test_distribution_expfamily_static.py | 4 +++ 15 files changed, 95 insertions(+), 40 deletions(-) diff --git a/python/paddle/base/data_feeder.py b/python/paddle/base/data_feeder.py index c914c792022873..ceebd69838ffeb 100644 --- a/python/paddle/base/data_feeder.py +++ b/python/paddle/base/data_feeder.py @@ -154,7 +154,7 @@ def check_variable_and_dtype( input, input_name, (Value, ParameterMeta), op_name, extra_message ) else: - check_type(input, input_name, Variable, op_name, extra_message) + check_type(input, input_name, (Variable, Value), op_name, extra_message) check_dtype(input.dtype, input_name, expected_dtype, op_name, extra_message) @@ -421,7 +421,7 @@ def __init__(self, feed_list, place, program=None): for each_var in feed_list: if isinstance(each_var, str): each_var = program.block(0).var(each_var) - if not isinstance(each_var, Variable): + if not isinstance(each_var, Variable, Value): raise TypeError( "Feed list should contain a list of variable" ) diff --git a/python/paddle/distribution/bernoulli.py b/python/paddle/distribution/bernoulli.py index 7cd06010eec63c..b9afef3331c9c3 100644 --- a/python/paddle/distribution/bernoulli.py +++ b/python/paddle/distribution/bernoulli.py @@ -97,7 +97,7 @@ def __init__(self, probs, name=None): check_type( probs, 'probs', - (float, Variable), + (float, Variable, paddle.pir.Value), self.name, ) @@ -180,7 +180,7 @@ def sample(self, shape): check_type( shape, 'shape', - (np.ndarray, Variable, list, tuple), + (np.ndarray, Variable, list, tuple, paddle.pir.Value), name, ) @@ -258,7 +258,7 @@ def rsample(self, shape, temperature=1.0): check_type( shape, 'shape', - (np.ndarray, Variable, list, tuple), + (np.ndarray, Variable, paddle.pir.Value, list, tuple), name, ) check_type( @@ -317,7 +317,7 @@ def cdf(self, value): """ name = self.name + '_cdf' if not in_dynamic_mode(): - check_type(value, 'value', Variable, name) + check_type(value, 'value', (Variable, paddle.pir.Value), name) value = self._check_values_dtype_in_probs(self.probs, value) probs, value = paddle.broadcast_tensors([self.probs, value]) @@ -355,7 +355,7 @@ def log_prob(self, value): """ name = self.name + '_log_prob' if not in_dynamic_mode(): - check_type(value, 'value', Variable, name) + check_type(value, 'value', (Variable, paddle.pir.Value), name) value = self._check_values_dtype_in_probs(self.probs, value) logits, value = paddle.broadcast_tensors([self.logits, value]) @@ -394,7 +394,7 @@ def prob(self, value): """ name = self.name + '_prob' if not in_dynamic_mode(): - check_type(value, 'value', Variable, name) + check_type(value, 'value', (Variable, paddle.pir.Value), name) return self.log_prob(value).exp(name=name) diff --git a/python/paddle/distribution/categorical.py b/python/paddle/distribution/categorical.py index caa4d6ffe99f42..9b93c2bf145ad7 100644 --- a/python/paddle/distribution/categorical.py +++ b/python/paddle/distribution/categorical.py @@ -98,7 +98,7 @@ def __init__(self, logits, name=None): check_type( logits, 'logits', - (np.ndarray, Variable, list, tuple), + (np.ndarray, Variable, paddle.pir.Value, list, tuple), 'Categorical', ) diff --git a/python/paddle/distribution/cauchy.py b/python/paddle/distribution/cauchy.py index 71f5b3d1ee60d0..4c80514e7a1cc6 100644 --- a/python/paddle/distribution/cauchy.py +++ b/python/paddle/distribution/cauchy.py @@ -58,13 +58,17 @@ class Cauchy(distribution.Distribution): def __init__(self, loc, scale, name=None): self.name = name if name is not None else 'Cauchy' - if not isinstance(loc, (numbers.Real, framework.Variable)): + if not isinstance( + loc, (numbers.Real, framework.Variable, paddle.pir.Value) + ): raise TypeError( - f"Expected type of loc is Real|Variable, but got {type(loc)}" + f"Expected type of loc is Real|Variable|Value, but got {type(loc)}" ) - if not isinstance(scale, (numbers.Real, framework.Variable)): + if not isinstance( + scale, (numbers.Real, framework.Variable, paddle.pir.Value) + ): raise TypeError( - f"Expected type of scale is Real|Variable, but got {type(scale)}" + f"Expected type of scale is Real|Variable|Value, but got {type(scale)}" ) if isinstance(loc, numbers.Real): @@ -188,7 +192,10 @@ def rsample(self, shape, name=None): """ name = name if name is not None else (self.name + '_rsample') - if not isinstance(shape, (np.ndarray, framework.Variable, list, tuple)): + if not isinstance( + shape, + (np.ndarray, framework.Variable, paddle.pir.Value, list, tuple), + ): raise TypeError( f"Expected type of shape is Sequence[int], but got {type(shape)}" ) diff --git a/python/paddle/distribution/distribution.py b/python/paddle/distribution/distribution.py index e6d63cc7268adc..2e24f6ce9bb60d 100644 --- a/python/paddle/distribution/distribution.py +++ b/python/paddle/distribution/distribution.py @@ -234,6 +234,11 @@ def _check_values_dtype_in_probs(self, param, value): value (Tensor): Change value's dtype if value's dtype is different from param. """ if in_dynamic_or_pir_mode(): + if in_pir_mode(): + check_variable_and_dtype( + value, 'value', ['float32', 'float64'], 'log_prob' + ) + if value.dtype != param.dtype and convert_dtype(value.dtype) in [ 'float32', 'float64', diff --git a/python/paddle/distribution/exponential.py b/python/paddle/distribution/exponential.py index 943850ea6e1768..3461e437a5f686 100644 --- a/python/paddle/distribution/exponential.py +++ b/python/paddle/distribution/exponential.py @@ -64,7 +64,7 @@ def __init__(self, rate): check_type( rate, 'rate', - (float, Variable), + (float, Variable, paddle.pir.Value), 'Exponential', ) diff --git a/python/paddle/distribution/gamma.py b/python/paddle/distribution/gamma.py index 696f77d926e395..937f96d619b601 100644 --- a/python/paddle/distribution/gamma.py +++ b/python/paddle/distribution/gamma.py @@ -82,13 +82,13 @@ def __init__(self, concentration, rate): check_type( concentration, 'concentration', - (float, Variable), + (float, Variable, paddle.pir.Value), 'Gamma', ) check_type( rate, 'rate', - (float, Variable), + (float, Variable, paddle.pir.Value), 'Gamma', ) diff --git a/python/paddle/distribution/geometric.py b/python/paddle/distribution/geometric.py index e4ba916fb58d25..43bc8c4bbe2942 100644 --- a/python/paddle/distribution/geometric.py +++ b/python/paddle/distribution/geometric.py @@ -68,7 +68,10 @@ class Geometric(distribution.Distribution): """ def __init__(self, probs): - if isinstance(probs, (numbers.Real, paddle.Tensor, framework.Variable)): + if isinstance( + probs, + (numbers.Real, paddle.Tensor, framework.Variable, paddle.pir.Value), + ): if isinstance(probs, numbers.Real): probs = paddle.full( shape=(), fill_value=probs, dtype=paddle.float32 @@ -89,7 +92,7 @@ def __init__(self, probs): else: raise TypeError( - f"Expected type of probs is Number.Real|Tensor|framework.Variable, but got {type(probs)}" + f"Expected type of probs is Number.Real|Tensor|framework.Variable|Value, but got {type(probs)}" ) if paddle.equal_all(lessthen_0, all_false) and paddle.equal_all( @@ -148,11 +151,13 @@ def pmf(self, k): Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.12500000) """ - if isinstance(k, (numbers.Integral, framework.Variable)): + if isinstance( + k, (numbers.Integral, framework.Variable, paddle.pir.Value) + ): return paddle.pow((1.0 - self.probs), k) * self.probs else: raise TypeError( - f"Expected type of k is number.Real|framework.Variable, but got {type(k)}" + f"Expected type of k is number.Real|framework.Variable|Value, but got {type(k)}" ) def log_pmf(self, k): @@ -179,11 +184,13 @@ def log_pmf(self, k): Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, -2.07944131) """ - if isinstance(k, (numbers.Integral, framework.Variable)): + if isinstance( + k, (numbers.Integral, framework.Variable, paddle.pir.Value) + ): return paddle.log(self.pmf(k)) else: raise TypeError( - f"Expected type of k is number.Real|framework.Variable, but got {type(k)}" + f"Expected type of k is number.Real|framework.Variable|Value, but got {type(k)}" ) def sample(self, shape=()): @@ -301,11 +308,13 @@ def cdf(self, k): Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.96875000) """ - if isinstance(k, (numbers.Integral, framework.Variable)): + if isinstance( + k, (numbers.Integral, framework.Variable, paddle.pir.Value) + ): return 1.0 - paddle.pow((1.0 - self.probs), k + 1) else: raise TypeError( - f"Expected type of k is number.Real|framework.Variable, but got {type(k)}" + f"Expected type of k is number.Real|framework.Variable|Value, but got {type(k)}" ) def kl_divergence(self, other): diff --git a/python/paddle/distribution/gumbel.py b/python/paddle/distribution/gumbel.py index a388f62c512b68..c02184df906571 100644 --- a/python/paddle/distribution/gumbel.py +++ b/python/paddle/distribution/gumbel.py @@ -83,13 +83,17 @@ class Gumbel(TransformedDistribution): """ def __init__(self, loc, scale): - if not isinstance(loc, (numbers.Real, framework.Variable)): + if not isinstance( + loc, (numbers.Real, framework.Variable, paddle.pir.Value) + ): raise TypeError( - f"Expected type of loc is Real|Variable, but got {type(loc)}" + f"Expected type of loc is Real|Variable|Value, but got {type(loc)}" ) - if not isinstance(scale, (numbers.Real, framework.Variable)): + if not isinstance( + scale, (numbers.Real, framework.Variable, paddle.pir.Value) + ): raise TypeError( - f"Expected type of scale is Real|Variable, but got {type(scale)}" + f"Expected type of scale is Real|Variable|Value, but got {type(scale)}" ) if isinstance(loc, numbers.Real): diff --git a/python/paddle/distribution/transform.py b/python/paddle/distribution/transform.py index 230d0e225fea14..9bfa53a89fe653 100644 --- a/python/paddle/distribution/transform.py +++ b/python/paddle/distribution/transform.py @@ -449,9 +449,13 @@ class AffineTransform(Transform): _type = Type.BIJECTION def __init__(self, loc, scale): - if not isinstance(loc, paddle.base.framework.Variable): + if not isinstance( + loc, (paddle.base.framework.Variable, paddle.pir.Value) + ): raise TypeError(f"Expected 'loc' is a Tensor, but got {type(loc)}") - if not isinstance(scale, paddle.base.framework.Variable): + if not isinstance( + scale, (paddle.base.framework.Variable, paddle.pir.Value) + ): raise TypeError( f"Expected scale is a Tensor, but got {type(scale)}" ) @@ -799,7 +803,9 @@ class PowerTransform(Transform): _type = Type.BIJECTION def __init__(self, power): - if not isinstance(power, paddle.base.framework.Variable): + if not isinstance( + power, (paddle.base.framework.Variable, paddle.pir.Value) + ): raise TypeError( f"Expected 'power' is a tensor, but got {type(power)}" ) diff --git a/python/paddle/distribution/uniform.py b/python/paddle/distribution/uniform.py index 8d142ce989b8bc..b9b4cf1e334803 100644 --- a/python/paddle/distribution/uniform.py +++ b/python/paddle/distribution/uniform.py @@ -105,13 +105,29 @@ def __init__(self, low, high, name=None): check_type( low, 'low', - (int, float, np.ndarray, Variable, list, tuple), + ( + int, + float, + np.ndarray, + Variable, + paddle.pir.Value, + list, + tuple, + ), 'Uniform', ) check_type( high, 'high', - (int, float, np.ndarray, Variable, list, tuple), + ( + int, + float, + np.ndarray, + Variable, + paddle.pir.Value, + list, + tuple, + ), 'Uniform', ) diff --git a/test/deprecated/distribution/test_distribution_bernoulli_static.py b/test/deprecated/distribution/test_distribution_bernoulli_static.py index 137d8140512d60..7ad39a759c1bb3 100644 --- a/test/deprecated/distribution/test_distribution_bernoulli_static.py +++ b/test/deprecated/distribution/test_distribution_bernoulli_static.py @@ -451,17 +451,17 @@ def test_bad_broadcast(self, value): # `logits, value = paddle.broadcast_tensors([self.logits, value])` # raise ValueError in dygraph, raise TypeError in static. - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): [_] = self.executor.run( self.program, feed={}, fetch_list=[rv.cdf(value)] ) - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): [_] = self.executor.run( self.program, feed={}, fetch_list=[rv.log_prob(value)] ) - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): [_] = self.executor.run( self.program, feed={}, fetch_list=[rv.prob(value)] ) diff --git a/test/deprecated/distribution/test_distribution_beta_static.py b/test/deprecated/distribution/test_distribution_beta_static.py index b3d81dd0105eac..7b55bc523aa7b6 100644 --- a/test/deprecated/distribution/test_distribution_beta_static.py +++ b/test/deprecated/distribution/test_distribution_beta_static.py @@ -142,3 +142,7 @@ def test_sample(self): data.shape == np.broadcast_arrays(self.alpha, self.beta)[0].shape ) + + +if __name__ == '__main__': + unittest.main() diff --git a/test/deprecated/distribution/test_distribution_cauchy_static.py b/test/deprecated/distribution/test_distribution_cauchy_static.py index 92be72549c79b8..2cbef6f3ed9712 100644 --- a/test/deprecated/distribution/test_distribution_cauchy_static.py +++ b/test/deprecated/distribution/test_distribution_cauchy_static.py @@ -457,17 +457,17 @@ def test_bad_broadcast(self, value): # `logits, value = paddle.broadcast_tensors([self.logits, value])` # raise ValueError in dygraph, raise TypeError in static. - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): [_] = self.executor.run( self.program, feed={}, fetch_list=[rv.cdf(value)] ) - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): [_] = self.executor.run( self.program, feed={}, fetch_list=[rv.log_prob(value)] ) - with self.assertRaises(TypeError): + with self.assertRaises((TypeError, ValueError)): [_] = self.executor.run( self.program, feed={}, fetch_list=[rv.prob(value)] ) diff --git a/test/deprecated/distribution/test_distribution_expfamily_static.py b/test/deprecated/distribution/test_distribution_expfamily_static.py index 00b88e5e9f5a82..595b918398f02c 100644 --- a/test/deprecated/distribution/test_distribution_expfamily_static.py +++ b/test/deprecated/distribution/test_distribution_expfamily_static.py @@ -62,3 +62,7 @@ def test_entropy_exception(self): paddle.distribution.ExponentialFamily.entropy( mock.DummyExpFamily(0.5, 0.5) ) + + +if __name__ == '__main__': + unittest.main() From 63f567ae3d255bf32352d957ba5c41101ddb2829 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 11 Jun 2024 09:25:40 +0000 Subject: [PATCH 02/28] refine --- python/paddle/distribution/dirichlet.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/paddle/distribution/dirichlet.py b/python/paddle/distribution/dirichlet.py index 8560a8e6f0e04f..0305e43745698a 100644 --- a/python/paddle/distribution/dirichlet.py +++ b/python/paddle/distribution/dirichlet.py @@ -16,7 +16,7 @@ from paddle.base.data_feeder import check_variable_and_dtype from paddle.base.layer_helper import LayerHelper from paddle.distribution import exponential_family -from paddle.framework import in_dynamic_mode +from paddle.framework import in_dynamic_or_pir_mode class Dirichlet(exponential_family.ExponentialFamily): @@ -156,7 +156,7 @@ def _log_normalizer(self, x): def _dirichlet(concentration, name=None): - if in_dynamic_mode(): + if in_dynamic_or_pir_mode(): return paddle._C_ops.dirichlet(concentration) else: op_type = 'dirichlet' From 45a5ee0e1ce4d6d03f425e70a7b035b15cdb7e45 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 11 Jun 2024 11:12:08 +0000 Subject: [PATCH 03/28] refine --- .../distribution/test_distribution_transform_static.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/deprecated/distribution/test_distribution_transform_static.py b/test/deprecated/distribution/test_distribution_transform_static.py index 0306127261ed79..c930523c9b8a6a 100644 --- a/test/deprecated/distribution/test_distribution_transform_static.py +++ b/test/deprecated/distribution/test_distribution_transform_static.py @@ -278,9 +278,10 @@ def test_inverse_shape(self, shape, expected_shape): ) class TestAffineTransform(unittest.TestCase): def setUp(self): - sp = paddle.static.Program() - mp = paddle.static.Program() - with paddle.static.program_guard(mp, sp): + with paddle.static.program_guard( + paddle.static.default_main_program(), + paddle.static.default_startup_program(), + ): loc = paddle.static.data('loc', self.loc.shape, self.loc.dtype) scale = paddle.static.data( 'scale', self.scale.shape, self.scale.dtype From dee63ac8341e07db68a19dac96fe51194adb5c81 Mon Sep 17 00:00:00 2001 From: cxxly Date: Wed, 12 Jun 2024 03:41:47 +0000 Subject: [PATCH 04/28] remove runtime validator for distribution --- python/paddle/distribution/bernoulli.py | 10 ------- python/paddle/distribution/binomial.py | 18 ------------- .../distribution/continuous_bernoulli.py | 27 ------------------- python/paddle/distribution/exponential.py | 3 --- python/paddle/distribution/gamma.py | 6 ----- python/paddle/distribution/geometric.py | 10 +------ .../distribution/multivariate_normal.py | 9 ------- python/paddle/distribution/poisson.py | 15 ----------- 8 files changed, 1 insertion(+), 97 deletions(-) diff --git a/python/paddle/distribution/bernoulli.py b/python/paddle/distribution/bernoulli.py index 7cd06010eec63c..248816cc31a41d 100644 --- a/python/paddle/distribution/bernoulli.py +++ b/python/paddle/distribution/bernoulli.py @@ -109,16 +109,6 @@ def __init__(self, probs, name=None): [self.probs] = self._to_tensor(probs) self.dtype = paddle.get_default_dtype() - # Check probs range [0, 1]. - if in_dynamic_mode(): - """Not use `paddle.any` in static mode, which always be `True`.""" - if ( - paddle.any(self.probs < 0) - or paddle.any(self.probs > 1) - or paddle.any(paddle.isnan(self.probs)) - ): - raise ValueError("The arg of `probs` must be in range [0, 1].") - # Clip probs from [0, 1] to (0, 1) with smallest representable number `eps`. self.probs = _clip_probs(self.probs, self.dtype) self.logits = self._probs_to_logits(self.probs, is_binary=True) diff --git a/python/paddle/distribution/binomial.py b/python/paddle/distribution/binomial.py index c14899e01ea7ab..e542e3834257e0 100644 --- a/python/paddle/distribution/binomial.py +++ b/python/paddle/distribution/binomial.py @@ -71,10 +71,6 @@ def __init__(self, total_count, probs): self.dtype = paddle.get_default_dtype() self.total_count, self.probs = self._to_tensor(total_count, probs) - if not self._check_constraint(self.total_count, self.probs): - raise ValueError( - 'Every element of input parameter `total_count` should be grater than or equal to one, and `probs` should be grater than or equal to zero and less than or equal to one.' - ) if self.total_count.shape == []: batch_shape = (1,) else: @@ -100,20 +96,6 @@ def _to_tensor(self, total_count, probs): # broadcast tensor return paddle.broadcast_tensors([total_count, probs]) - def _check_constraint(self, total_count, probs): - """Check the constraints for input parameters - - Args: - total_count (Tensor) - probs (Tensor) - - Returns: - bool: pass or not. - """ - total_count_check = (total_count >= 1).all() - probability_check = (probs >= 0).all() * (probs <= 1).all() - return total_count_check and probability_check - @property def mean(self): """Mean of binomial distribution. diff --git a/python/paddle/distribution/continuous_bernoulli.py b/python/paddle/distribution/continuous_bernoulli.py index 3fd0335fc40760..48fb2fd2218be1 100644 --- a/python/paddle/distribution/continuous_bernoulli.py +++ b/python/paddle/distribution/continuous_bernoulli.py @@ -101,10 +101,6 @@ def __init__(self, probs, lims=(0.499, 0.501)): self.dtype = paddle.get_default_dtype() self.probs = self._to_tensor(probs) self.lims = paddle.to_tensor(lims, dtype=self.dtype) - if not self._check_constraint(self.probs): - raise ValueError( - 'Every element of input parameter `probs` should be nonnegative.' - ) # eps_prob is used to clip the input `probs` in the range of [eps_prob, 1-eps_prob] eps_prob = paddle.finfo(self.probs.dtype).eps @@ -129,17 +125,6 @@ def _to_tensor(self, probs): self.dtype = probs.dtype return probs - def _check_constraint(self, value): - """Check the constraint for input parameters - - Args: - value (Tensor) - - Returns: - bool: pass or not. - """ - return (value >= 0).all() and (value <= 1).all() - def _cut_support_region(self): """Generate stable support region indicator (prob < self.lims[0] && prob >= self.lims[1] ) @@ -290,10 +275,6 @@ def log_prob(self, value): Tensor: log probability. The data type is the same as `self.probs`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be >= 0.0 and <= 1.0.' - ) eps = paddle.finfo(self.probs.dtype).eps cross_entropy = paddle.nan_to_num( value * paddle.log(self.probs) @@ -363,10 +344,6 @@ def cdf(self, value): Tensor: quantile of :attr:`value`. The data type is the same as `self.probs`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be >= 0.0 and <= 1.0.' - ) cut_probs = self._cut_probs() cdfs = ( paddle.pow(cut_probs, value) @@ -407,10 +384,6 @@ def icdf(self, value): Tensor: the value of the r.v. corresponding to the quantile. The data type is the same as `self.probs`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be >= 0.0 and <= 1.0.' - ) cut_probs = self._cut_probs() return paddle.where( self._cut_support_region(), diff --git a/python/paddle/distribution/exponential.py b/python/paddle/distribution/exponential.py index 943850ea6e1768..92b50d78cfac13 100644 --- a/python/paddle/distribution/exponential.py +++ b/python/paddle/distribution/exponential.py @@ -76,9 +76,6 @@ def __init__(self, rate): [self.rate] = self._to_tensor(rate) self.dtype = paddle.get_default_dtype() - if not paddle.all(self.rate > 0): - raise ValueError("The arg of `rate` must be positive.") - super().__init__(self.rate.shape) @property diff --git a/python/paddle/distribution/gamma.py b/python/paddle/distribution/gamma.py index 696f77d926e395..5001006b1824b1 100644 --- a/python/paddle/distribution/gamma.py +++ b/python/paddle/distribution/gamma.py @@ -103,12 +103,6 @@ def __init__(self, concentration, rate): ) self.dtype = paddle.get_default_dtype() - if not paddle.all(self.concentration > 0): - raise ValueError("The arg of `concentration` must be positive.") - - if not paddle.all(self.rate > 0): - raise ValueError("The arg of `rate` must be positive.") - super().__init__(self.concentration.shape) @property diff --git a/python/paddle/distribution/geometric.py b/python/paddle/distribution/geometric.py index e4ba916fb58d25..edcae0e1eb001a 100644 --- a/python/paddle/distribution/geometric.py +++ b/python/paddle/distribution/geometric.py @@ -92,15 +92,7 @@ def __init__(self, probs): f"Expected type of probs is Number.Real|Tensor|framework.Variable, but got {type(probs)}" ) - if paddle.equal_all(lessthen_0, all_false) and paddle.equal_all( - morethen_1, all_false - ): - batch_shape = tuple(probs.shape) - else: - raise ValueError( - "Expected parameter probs of distribution Geometric to satisfy the" - "constraint Interval(lower_bound=0.0, upper_bound=1.0)" - ) + batch_shape = tuple(probs.shape) self.probs = probs super().__init__(batch_shape) diff --git a/python/paddle/distribution/multivariate_normal.py b/python/paddle/distribution/multivariate_normal.py index a576b3357d9ed1..6485b83872553f 100644 --- a/python/paddle/distribution/multivariate_normal.py +++ b/python/paddle/distribution/multivariate_normal.py @@ -205,15 +205,6 @@ def _check_positive_definite(self, value): Return: Tensor: indicator for positive definite matrix """ - is_square = paddle.full( - shape=value.shape[:-2], - fill_value=(value.shape[-2] == value.shape[-1]), - dtype="bool", - ).all() - if not is_square: - raise ValueError( - "covariance_matrix or precision_matrix must be a square matrix" - ) new_perm = list(range(len(value.shape))) new_perm[-1], new_perm[-2] = new_perm[-2], new_perm[-1] is_symmetric = paddle.isclose( diff --git a/python/paddle/distribution/poisson.py b/python/paddle/distribution/poisson.py index a160b44b9ccf60..be9ad80a0ceeb5 100644 --- a/python/paddle/distribution/poisson.py +++ b/python/paddle/distribution/poisson.py @@ -76,10 +76,6 @@ def __init__(self, rate): self.dtype = paddle.get_default_dtype() self.rate = self._to_tensor(rate) - if not self._check_constraint(self.rate): - raise ValueError( - 'Every element of input parameter `rate` should be nonnegative.' - ) if self.rate.shape == []: batch_shape = (1,) else: @@ -99,17 +95,6 @@ def _to_tensor(self, rate): self.dtype = rate.dtype return rate - def _check_constraint(self, value): - """Check the constraint for input parameters - - Args: - value (Tensor) - - Returns: - bool: pass or not. - """ - return (value >= 0).all() - @property def mean(self): """Mean of poisson distribution. From 5ca9498a0493e543279adb10f5f26af6848128a3 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 12 Jun 2024 06:05:07 +0000 Subject: [PATCH 05/28] refine --- python/paddle/base/data_feeder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/base/data_feeder.py b/python/paddle/base/data_feeder.py index ceebd69838ffeb..8275c7761d9840 100644 --- a/python/paddle/base/data_feeder.py +++ b/python/paddle/base/data_feeder.py @@ -421,7 +421,7 @@ def __init__(self, feed_list, place, program=None): for each_var in feed_list: if isinstance(each_var, str): each_var = program.block(0).var(each_var) - if not isinstance(each_var, Variable, Value): + if not isinstance(each_var, (Variable, Value)): raise TypeError( "Feed list should contain a list of variable" ) From 6858f5f5360ab6c33317726fb287d8dbc3e1831a Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 12 Jun 2024 06:08:11 +0000 Subject: [PATCH 06/28] refine --- python/paddle/distribution/binomial.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/python/paddle/distribution/binomial.py b/python/paddle/distribution/binomial.py index e542e3834257e0..becd4fcde487d5 100644 --- a/python/paddle/distribution/binomial.py +++ b/python/paddle/distribution/binomial.py @@ -235,10 +235,6 @@ def kl_divergence(self, other): Tensor: kl-divergence between two binomial distributions. The data type is the same as `probs`. """ - if not (paddle.equal(self.total_count, other.total_count)).all(): - raise ValueError( - "KL divergence of two binomial distributions should share the same `total_count` and `batch_shape`." - ) support = self._enumerate_support() log_prob_1 = self.log_prob(support) log_prob_2 = other.log_prob(support) From c17741c891d4291324c0f79e0f90727a8c15a953 Mon Sep 17 00:00:00 2001 From: cxxly Date: Wed, 12 Jun 2024 03:41:47 +0000 Subject: [PATCH 07/28] remove runtime validator for distribution --- python/paddle/distribution/bernoulli.py | 10 --- python/paddle/distribution/binomial.py | 18 ---- .../distribution/continuous_bernoulli.py | 27 ------ python/paddle/distribution/exponential.py | 3 - python/paddle/distribution/gamma.py | 6 -- python/paddle/distribution/geometric.py | 10 +-- .../distribution/multivariate_normal.py | 86 ------------------- python/paddle/distribution/poisson.py | 19 ---- .../test_distribution_bernoulli.py | 4 - test/distribution/test_distribution_cauchy.py | 4 - .../test_distribution_continuous_bernoulli.py | 25 ------ .../test_distribution_geometric.py | 4 - .../test_distribution_multivariate_normal.py | 46 ---------- 13 files changed, 1 insertion(+), 261 deletions(-) diff --git a/python/paddle/distribution/bernoulli.py b/python/paddle/distribution/bernoulli.py index 7cd06010eec63c..248816cc31a41d 100644 --- a/python/paddle/distribution/bernoulli.py +++ b/python/paddle/distribution/bernoulli.py @@ -109,16 +109,6 @@ def __init__(self, probs, name=None): [self.probs] = self._to_tensor(probs) self.dtype = paddle.get_default_dtype() - # Check probs range [0, 1]. - if in_dynamic_mode(): - """Not use `paddle.any` in static mode, which always be `True`.""" - if ( - paddle.any(self.probs < 0) - or paddle.any(self.probs > 1) - or paddle.any(paddle.isnan(self.probs)) - ): - raise ValueError("The arg of `probs` must be in range [0, 1].") - # Clip probs from [0, 1] to (0, 1) with smallest representable number `eps`. self.probs = _clip_probs(self.probs, self.dtype) self.logits = self._probs_to_logits(self.probs, is_binary=True) diff --git a/python/paddle/distribution/binomial.py b/python/paddle/distribution/binomial.py index c14899e01ea7ab..e542e3834257e0 100644 --- a/python/paddle/distribution/binomial.py +++ b/python/paddle/distribution/binomial.py @@ -71,10 +71,6 @@ def __init__(self, total_count, probs): self.dtype = paddle.get_default_dtype() self.total_count, self.probs = self._to_tensor(total_count, probs) - if not self._check_constraint(self.total_count, self.probs): - raise ValueError( - 'Every element of input parameter `total_count` should be grater than or equal to one, and `probs` should be grater than or equal to zero and less than or equal to one.' - ) if self.total_count.shape == []: batch_shape = (1,) else: @@ -100,20 +96,6 @@ def _to_tensor(self, total_count, probs): # broadcast tensor return paddle.broadcast_tensors([total_count, probs]) - def _check_constraint(self, total_count, probs): - """Check the constraints for input parameters - - Args: - total_count (Tensor) - probs (Tensor) - - Returns: - bool: pass or not. - """ - total_count_check = (total_count >= 1).all() - probability_check = (probs >= 0).all() * (probs <= 1).all() - return total_count_check and probability_check - @property def mean(self): """Mean of binomial distribution. diff --git a/python/paddle/distribution/continuous_bernoulli.py b/python/paddle/distribution/continuous_bernoulli.py index 3fd0335fc40760..48fb2fd2218be1 100644 --- a/python/paddle/distribution/continuous_bernoulli.py +++ b/python/paddle/distribution/continuous_bernoulli.py @@ -101,10 +101,6 @@ def __init__(self, probs, lims=(0.499, 0.501)): self.dtype = paddle.get_default_dtype() self.probs = self._to_tensor(probs) self.lims = paddle.to_tensor(lims, dtype=self.dtype) - if not self._check_constraint(self.probs): - raise ValueError( - 'Every element of input parameter `probs` should be nonnegative.' - ) # eps_prob is used to clip the input `probs` in the range of [eps_prob, 1-eps_prob] eps_prob = paddle.finfo(self.probs.dtype).eps @@ -129,17 +125,6 @@ def _to_tensor(self, probs): self.dtype = probs.dtype return probs - def _check_constraint(self, value): - """Check the constraint for input parameters - - Args: - value (Tensor) - - Returns: - bool: pass or not. - """ - return (value >= 0).all() and (value <= 1).all() - def _cut_support_region(self): """Generate stable support region indicator (prob < self.lims[0] && prob >= self.lims[1] ) @@ -290,10 +275,6 @@ def log_prob(self, value): Tensor: log probability. The data type is the same as `self.probs`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be >= 0.0 and <= 1.0.' - ) eps = paddle.finfo(self.probs.dtype).eps cross_entropy = paddle.nan_to_num( value * paddle.log(self.probs) @@ -363,10 +344,6 @@ def cdf(self, value): Tensor: quantile of :attr:`value`. The data type is the same as `self.probs`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be >= 0.0 and <= 1.0.' - ) cut_probs = self._cut_probs() cdfs = ( paddle.pow(cut_probs, value) @@ -407,10 +384,6 @@ def icdf(self, value): Tensor: the value of the r.v. corresponding to the quantile. The data type is the same as `self.probs`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be >= 0.0 and <= 1.0.' - ) cut_probs = self._cut_probs() return paddle.where( self._cut_support_region(), diff --git a/python/paddle/distribution/exponential.py b/python/paddle/distribution/exponential.py index 943850ea6e1768..92b50d78cfac13 100644 --- a/python/paddle/distribution/exponential.py +++ b/python/paddle/distribution/exponential.py @@ -76,9 +76,6 @@ def __init__(self, rate): [self.rate] = self._to_tensor(rate) self.dtype = paddle.get_default_dtype() - if not paddle.all(self.rate > 0): - raise ValueError("The arg of `rate` must be positive.") - super().__init__(self.rate.shape) @property diff --git a/python/paddle/distribution/gamma.py b/python/paddle/distribution/gamma.py index 696f77d926e395..5001006b1824b1 100644 --- a/python/paddle/distribution/gamma.py +++ b/python/paddle/distribution/gamma.py @@ -103,12 +103,6 @@ def __init__(self, concentration, rate): ) self.dtype = paddle.get_default_dtype() - if not paddle.all(self.concentration > 0): - raise ValueError("The arg of `concentration` must be positive.") - - if not paddle.all(self.rate > 0): - raise ValueError("The arg of `rate` must be positive.") - super().__init__(self.concentration.shape) @property diff --git a/python/paddle/distribution/geometric.py b/python/paddle/distribution/geometric.py index e4ba916fb58d25..edcae0e1eb001a 100644 --- a/python/paddle/distribution/geometric.py +++ b/python/paddle/distribution/geometric.py @@ -92,15 +92,7 @@ def __init__(self, probs): f"Expected type of probs is Number.Real|Tensor|framework.Variable, but got {type(probs)}" ) - if paddle.equal_all(lessthen_0, all_false) and paddle.equal_all( - morethen_1, all_false - ): - batch_shape = tuple(probs.shape) - else: - raise ValueError( - "Expected parameter probs of distribution Geometric to satisfy the" - "constraint Interval(lower_bound=0.0, upper_bound=1.0)" - ) + batch_shape = tuple(probs.shape) self.probs = probs super().__init__(batch_shape) diff --git a/python/paddle/distribution/multivariate_normal.py b/python/paddle/distribution/multivariate_normal.py index a576b3357d9ed1..55e6a3cc7f325e 100644 --- a/python/paddle/distribution/multivariate_normal.py +++ b/python/paddle/distribution/multivariate_normal.py @@ -150,7 +150,6 @@ def __init__( batch_shape + [precision_matrix.shape[-2], precision_matrix.shape[-1]] ) - self._check_constraints() self.loc = loc.expand( batch_shape + [ @@ -172,91 +171,6 @@ def __init__( super().__init__(batch_shape, event_shape) - def _check_lower_triangular(self, value): - """Check whether the input is a lower triangular matrix - - Args: - value (Tensor): input matrix - - Return: - Tensor: indicator for lower triangular matrix - """ - tril = paddle.tril(value) - is_lower_triangular = paddle.cast( - (tril == value).reshape( - value.shape[:-2] - + [ - -1, - ] - ), - dtype=self.dtype, - ).min(-1, keepdim=True)[0] - is_positive_diag = paddle.cast( - (value.diagonal(axis1=-2, axis2=-1) > 0), dtype=self.dtype - ).min(-1, keepdim=True)[0] - return is_lower_triangular and is_positive_diag - - def _check_positive_definite(self, value): - """Check whether the input is a positive definite matrix - - Args: - value (Tensor): input matrix - - Return: - Tensor: indicator for positive definite matrix - """ - is_square = paddle.full( - shape=value.shape[:-2], - fill_value=(value.shape[-2] == value.shape[-1]), - dtype="bool", - ).all() - if not is_square: - raise ValueError( - "covariance_matrix or precision_matrix must be a square matrix" - ) - new_perm = list(range(len(value.shape))) - new_perm[-1], new_perm[-2] = new_perm[-2], new_perm[-1] - is_symmetric = paddle.isclose( - value, value.transpose(new_perm), atol=1e-6 - ).all() - if not is_symmetric: - raise ValueError( - "covariance_matrix or precision_matrix must be a symmetric matrix" - ) - is_positive_definite = ( - paddle.cast(paddle.linalg.eigvalsh(value), dtype=self.dtype) > 0 - ).all() - return is_positive_definite - - def _check_constraints(self): - """Check whether the matrix satisfy corresponding constraint - - Return: - Tensor: indicator for the pass of constraints check - """ - if self.scale_tril is not None: - check = self._check_lower_triangular(self.scale_tril) - if not check: - raise ValueError( - "scale_tril matrix must be a lower triangular matrix with positive diagonals" - ) - elif self.covariance_matrix is not None: - is_positive_definite = self._check_positive_definite( - self.covariance_matrix - ) - if not is_positive_definite: - raise ValueError( - "covariance_matrix must be a symmetric positive definite matrix" - ) - else: - is_positive_definite = self._check_positive_definite( - self.precision_matrix - ) - if not is_positive_definite: - raise ValueError( - "precision_matrix must be a symmetric positive definite matrix" - ) - @property def mean(self): """Mean of Multivariate Normal distribution. diff --git a/python/paddle/distribution/poisson.py b/python/paddle/distribution/poisson.py index a160b44b9ccf60..499ee2c1686c9c 100644 --- a/python/paddle/distribution/poisson.py +++ b/python/paddle/distribution/poisson.py @@ -76,10 +76,6 @@ def __init__(self, rate): self.dtype = paddle.get_default_dtype() self.rate = self._to_tensor(rate) - if not self._check_constraint(self.rate): - raise ValueError( - 'Every element of input parameter `rate` should be nonnegative.' - ) if self.rate.shape == []: batch_shape = (1,) else: @@ -99,17 +95,6 @@ def _to_tensor(self, rate): self.dtype = rate.dtype return rate - def _check_constraint(self, value): - """Check the constraint for input parameters - - Args: - value (Tensor) - - Returns: - bool: pass or not. - """ - return (value >= 0).all() - @property def mean(self): """Mean of poisson distribution. @@ -209,10 +194,6 @@ def log_prob(self, value): Tensor: log probability. The data type is the same as `rate`. """ value = paddle.cast(value, dtype=self.dtype) - if not self._check_constraint(value): - raise ValueError( - 'Every element of input parameter `value` should be nonnegative.' - ) eps = paddle.finfo(self.rate.dtype).eps return paddle.nan_to_num( ( diff --git a/test/distribution/test_distribution_bernoulli.py b/test/distribution/test_distribution_bernoulli.py index 4845efcd6a29d0..3491c3cc15eca1 100644 --- a/test/distribution/test_distribution_bernoulli.py +++ b/test/distribution/test_distribution_bernoulli.py @@ -572,10 +572,6 @@ def setUp(self): (-1j + 1, TypeError), ] ) - def test_bad_init(self, probs, error): - with paddle.base.dygraph.guard(self.place): - self.assertRaises(error, Bernoulli, probs) - @parameterize_func( [ ( diff --git a/test/distribution/test_distribution_cauchy.py b/test/distribution/test_distribution_cauchy.py index d0d570a37df079..7815ea95d4d9e6 100644 --- a/test/distribution/test_distribution_cauchy.py +++ b/test/distribution/test_distribution_cauchy.py @@ -637,10 +637,6 @@ def setUp(self): (0.0, (0.0, 0.0), TypeError), # tuple ] ) - def test_bad_init(self, loc, scale, error): - with paddle.base.dygraph.guard(self.place): - self.assertRaises(error, Cauchy, loc, scale) - def test_bad_property(self): """For property like mean/variance/stddev which is undefined in math, we should raise `ValueError` instead of `NotImplementedError`. diff --git a/test/distribution/test_distribution_continuous_bernoulli.py b/test/distribution/test_distribution_continuous_bernoulli.py index 8b5c5c9c7ee522..52ff21649e3fd9 100644 --- a/test/distribution/test_distribution_continuous_bernoulli.py +++ b/test/distribution/test_distribution_continuous_bernoulli.py @@ -341,31 +341,6 @@ class ContinuousBernoulliTestError(unittest.TestCase): def setUp(self): paddle.disable_static(self.place) - @parameterize_func( - [ - (-0.1, ValueError), - (1.1, ValueError), - ] - ) - def test_bad_init(self, probs, error): - with paddle.base.dygraph.guard(self.place): - self.assertRaises(error, ContinuousBernoulli, probs) - - @parameterize_func( - [ - ( - paddle.to_tensor([0.3, 0.5]), - paddle.to_tensor([-0.1, 1.2]), - ), - ] - ) - def test_bad_log_prob_value(self, probs, value): - with paddle.base.dygraph.guard(self.place): - rv = ContinuousBernoulli(probs) - self.assertRaises(ValueError, rv.cdf, value) - self.assertRaises(ValueError, rv.log_prob, value) - self.assertRaises(ValueError, rv.icdf, value) - @parameterize_func( [ ( diff --git a/test/distribution/test_distribution_geometric.py b/test/distribution/test_distribution_geometric.py index ab8333b7f9997c..ab5aadd4f215c8 100644 --- a/test/distribution/test_distribution_geometric.py +++ b/test/distribution/test_distribution_geometric.py @@ -94,10 +94,6 @@ def test_entropy(self): atol=ATOL.get(str(self._paddle_geom.probs.numpy().dtype)), ) - def test_init_prob_value_error(self): - with self.assertRaises(ValueError): - paddle.distribution.geometric.Geometric(2) - def test_init_prob_type_error(self): with self.assertRaises(TypeError): paddle.distribution.geometric.Geometric([2]) diff --git a/test/distribution/test_distribution_multivariate_normal.py b/test/distribution/test_distribution_multivariate_normal.py index 3c6059defe8598..fd54db855f7e88 100644 --- a/test/distribution/test_distribution_multivariate_normal.py +++ b/test/distribution/test_distribution_multivariate_normal.py @@ -21,7 +21,6 @@ from parameterize import ( TEST_CASE_NAME, parameterize_cls, - parameterize_func, ) import paddle @@ -260,51 +259,6 @@ class MVNTestError(unittest.TestCase): def setUp(self): paddle.disable_static(self.place) - @parameterize_func( - [ - (5, None, ValueError), # no matrix input - ( - 5, - paddle.to_tensor([2.0, 3.0]), - ValueError, - ), # wrong input matrix dim - ( - 5, - paddle.to_tensor([[2.0, 3.0, 4.0], [2.0, 3.0, 4.0]]), - ValueError, - ), # non-sqaure input matrix - ( - 5, - paddle.to_tensor([[2.0, 3.0], [2.0, 3.0]]), - ValueError, - ), # non-symmetric input matrix - ( - 5, - paddle.to_tensor([[-2.0, 3.0], [3.0, -1.0]]), - ValueError, - ), # non-psd input matrix - ] - ) - def test_bad_cov_matrix(self, loc, matrix, error): - with paddle.base.dygraph.guard(self.place): - self.assertRaises(error, MultivariateNormal, loc, matrix) - - @parameterize_func( - [ - ( - 1.0, - 2.0, - paddle.to_tensor([[3.0, 2.0], [2.0, 3.0]]), - paddle.to_tensor([[2.0, 1.0], [1.0, 2.0]]), - ), - ] - ) - def test_bad_kl_div(self, loc1, loc2, matrix1, matrix2): - with paddle.base.dygraph.guard(self.place): - rv = MultivariateNormal(loc1, covariance_matrix=matrix1) - rv_other = MultivariateNormal(loc2, covariance_matrix=matrix2) - self.assertRaises(ValueError, rv.kl_divergence, rv_other) - if __name__ == '__main__': unittest.main(argv=[''], verbosity=3, exit=False) From d5d158910f623c8611dfd4d65de680697d1e9e80 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 14 Jun 2024 06:29:29 +0000 Subject: [PATCH 08/28] refine --- python/paddle/distribution/poisson.py | 37 ++++++++++++++----- .../test_distribution_transform_static.py | 7 ++-- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/python/paddle/distribution/poisson.py b/python/paddle/distribution/poisson.py index 499ee2c1686c9c..1294e9f79565da 100644 --- a/python/paddle/distribution/poisson.py +++ b/python/paddle/distribution/poisson.py @@ -173,16 +173,35 @@ def _enumerate_bounded_support(self, rate): Returns: Tensor: the bounded approximation of the support """ - s_max = ( - paddle.sqrt(paddle.max(rate)) - if paddle.greater_equal( - paddle.max(rate), paddle.to_tensor(1.0, dtype=self.dtype) + if paddle.framework.in_dynamic_mode(): + s_max = ( + paddle.sqrt(paddle.max(rate)) + if paddle.greater_equal( + paddle.max(rate), paddle.to_tensor(1.0, dtype=self.dtype) + ) + else paddle.ones_like(rate, dtype=self.dtype) ) - else paddle.ones_like(rate, dtype=self.dtype) - ) - upper = paddle.max(paddle.cast(rate + 30 * s_max, dtype="int32")) - values = paddle.arange(0, upper, dtype=self.dtype) - return values + upper = paddle.max(paddle.cast(rate + 30 * s_max, dtype="int32")) + values = paddle.arange(0, upper, dtype=self.dtype) + return values + else: + + def true_func(): + return paddle.sqrt(paddle.max(rate)) + + def false_func(): + return paddle.to_tensor(1.0, dtype=self.dtype) + + s_max = paddle.static.nn.cond( + paddle.greater_equal( + paddle.max(rate), paddle.to_tensor(1.0, dtype=self.dtype) + ), + true_func, + false_func, + ) + upper = paddle.max(paddle.cast(rate + 30 * s_max, dtype="int32")) + values = paddle.arange(0, upper, dtype=self.dtype) + return values def log_prob(self, value): """Log probability density/mass function. diff --git a/test/deprecated/distribution/test_distribution_transform_static.py b/test/deprecated/distribution/test_distribution_transform_static.py index c930523c9b8a6a..ded967eacaad5b 100644 --- a/test/deprecated/distribution/test_distribution_transform_static.py +++ b/test/deprecated/distribution/test_distribution_transform_static.py @@ -278,10 +278,9 @@ def test_inverse_shape(self, shape, expected_shape): ) class TestAffineTransform(unittest.TestCase): def setUp(self): - with paddle.static.program_guard( - paddle.static.default_main_program(), - paddle.static.default_startup_program(), - ): + self.sp = paddle.static.Program() + self.mp = paddle.static.Program() + with paddle.static.program_guard(self.mp, self.sp): loc = paddle.static.data('loc', self.loc.shape, self.loc.dtype) scale = paddle.static.data( 'scale', self.scale.shape, self.scale.dtype From 6d17721d15c2a53ea92d3cb5ce3a7fecfc7e40d6 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 14 Jun 2024 06:39:22 +0000 Subject: [PATCH 09/28] refine --- test/deprecated/CMakeLists.txt | 1 - test/deprecated/distribution/CMakeLists.txt | 17 -- test/deprecated/distribution/mock_data.py | 65 ----- test/deprecated/distribution/parameterize.py | 240 ------------------ .../distribution/test_distribution.py | 30 --- test/distribution/CMakeLists.txt | 3 + .../test_distribution_bernoulli_static.py | 0 .../test_distribution_beta_static.py | 0 .../test_distribution_binomial_static.py | 0 .../test_distribution_categorical.py | 0 .../test_distribution_cauchy_static.py | 0 .../test_distribution_chi2_static.py | 0 ...istribution_continuous_bernoulli_static.py | 0 .../test_distribution_expfamily_static.py | 0 .../test_distribution_exponential_static.py | 0 .../test_distribution_gamma_static.py | 0 .../test_distribution_geometric_static.py | 0 .../test_distribution_gumbel_static.py | 0 .../test_distribution_lkj_cholesky_static.py | 0 .../test_distribution_multinomial.py | 0 .../test_distribution_multinomial_static.py | 0 ...distribution_multivariate_normal_static.py | 0 .../test_distribution_poisson_static.py | 0 .../test_distribution_transform_static.py | 0 .../distribution/test_distribution_uniform.py | 0 .../distribution/test_kl_static.py | 0 26 files changed, 3 insertions(+), 353 deletions(-) delete mode 100644 test/deprecated/distribution/CMakeLists.txt delete mode 100644 test/deprecated/distribution/mock_data.py delete mode 100644 test/deprecated/distribution/parameterize.py delete mode 100644 test/deprecated/distribution/test_distribution.py rename test/{deprecated => }/distribution/test_distribution_bernoulli_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_beta_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_binomial_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_categorical.py (100%) rename test/{deprecated => }/distribution/test_distribution_cauchy_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_chi2_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_continuous_bernoulli_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_expfamily_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_exponential_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_gamma_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_geometric_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_gumbel_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_lkj_cholesky_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_multinomial.py (100%) rename test/{deprecated => }/distribution/test_distribution_multinomial_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_multivariate_normal_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_poisson_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_transform_static.py (100%) rename test/{deprecated => }/distribution/test_distribution_uniform.py (100%) rename test/{deprecated => }/distribution/test_kl_static.py (100%) diff --git a/test/deprecated/CMakeLists.txt b/test/deprecated/CMakeLists.txt index 2254fc3d5f9dd4..1a757f8a26c1b7 100644 --- a/test/deprecated/CMakeLists.txt +++ b/test/deprecated/CMakeLists.txt @@ -143,7 +143,6 @@ if(WITH_TESTING) add_subdirectory(book) add_subdirectory(contrib) add_subdirectory(cpp) - add_subdirectory(distribution) add_subdirectory(ir) add_subdirectory(legacy_test) add_subdirectory(quantization) diff --git a/test/deprecated/distribution/CMakeLists.txt b/test/deprecated/distribution/CMakeLists.txt deleted file mode 100644 index dede579778802c..00000000000000 --- a/test/deprecated/distribution/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -file( - GLOB TEST_OPS - RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" - "test_*.py") -string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") - -# test_distribution has been tested in test/distribution dir -list(REMOVE_ITEM TEST_OPS test_distribution) - -foreach(TEST_OP ${TEST_OPS}) - py_test_modules(${TEST_OP} MODULES ${TEST_OP}) -endforeach() - -set_tests_properties(test_distribution_lkj_cholesky_static PROPERTIES TIMEOUT - 120) - -set_pir_tests_properties() diff --git a/test/deprecated/distribution/mock_data.py b/test/deprecated/distribution/mock_data.py deleted file mode 100644 index 08c974d87c59fa..00000000000000 --- a/test/deprecated/distribution/mock_data.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import paddle - - -class Exponential(paddle.distribution.ExponentialFamily): - """mock exponential distribution, which support computing entropy and - kl use bregman divergence - """ - - _mean_carrier_measure = 0 - - def __init__(self, rate): - self._rate = rate - super().__init__(batch_shape=rate.shape) - - @property - def rate(self): - return self._rate - - def entropy(self): - return 1.0 - paddle.log(self._rate) - - @property - def _natural_parameters(self): - return (-self._rate,) - - def _log_normalizer(self, x): - return -paddle.log(-x) - - -class DummyExpFamily(paddle.distribution.ExponentialFamily): - """dummy class extend from exponential family""" - - def __init__(self, *args): - pass - - def entropy(self): - return 1.0 - - @property - def _natural_parameters(self): - return (1.0,) - - def _log_normalizer(self, x): - return -paddle.log(-x) - - -@paddle.distribution.register_kl(Exponential, Exponential) -def _kl_exponential_exponential(p, q): - rate_ratio = q.rate / p.rate - t1 = -rate_ratio.log() - return t1 + rate_ratio - 1 diff --git a/test/deprecated/distribution/parameterize.py b/test/deprecated/distribution/parameterize.py deleted file mode 100644 index 4488553fdec572..00000000000000 --- a/test/deprecated/distribution/parameterize.py +++ /dev/null @@ -1,240 +0,0 @@ -# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -import collections -import functools -import inspect -import re -import sys -from unittest import SkipTest - -import numpy as np -from distribution import config - -TEST_CASE_NAME = 'suffix' - - -def xrand(shape=(10, 10, 10), dtype=config.DEFAULT_DTYPE, min=1.0, max=10.0): - return (np.random.rand(*shape).astype(dtype)) * (max - min) + min - - -def place(devices, key='place'): - def decorate(cls): - module = sys.modules[cls.__module__].__dict__ - raw_classes = { - k: v for k, v in module.items() if k.startswith(cls.__name__) - } - - for raw_name, raw_cls in raw_classes.items(): - for d in devices: - test_cls = dict(raw_cls.__dict__) - test_cls.update({key: d}) - new_name = raw_name + '.' + d.__class__.__name__ - module[new_name] = type(new_name, (raw_cls,), test_cls) - del module[raw_name] - return cls - - return decorate - - -def parameterize_cls(fields, values=None, test_pir=False): - fields = [fields] if isinstance(fields, str) else fields - params = [dict(zip(fields, vals)) for vals in values] - - def decorate(cls): - test_cls_module = sys.modules[cls.__module__].__dict__ - for k, v in enumerate(params): - test_cls = dict(cls.__dict__) - test_cls.update(v) - test_cls["test_pir"] = False - name = cls.__name__ + str(k) - name = name + '.' + v.get('suffix') if v.get('suffix') else name - test_cls_module[name] = type(name, (cls,), test_cls) - if test_pir: - name = name + ".pir" - test_cls["test_pir"] = True - pir_type = type(name, (cls,), test_cls) - test_cls_module[name] = pir_type - - for m in list(cls.__dict__): - if m.startswith("test"): - delattr(cls, m) - return cls - - return decorate - - -def parameterize_func( - input, name_func=None, doc_func=None, skip_on_empty=False -): - name_func = name_func or default_name_func - - def wrapper(f, instance=None): - frame_locals = inspect.currentframe().f_back.f_locals - - parameters = input_as_callable(input)() - - if not parameters: - if not skip_on_empty: - raise ValueError( - "Parameters iterable is empty (hint: use " - "`parameterized.expand([], skip_on_empty=True)` to skip " - "this test when the input is empty)" - ) - return functools.wraps(f)(skip_on_empty_helper) - - digits = len(str(len(parameters) - 1)) - for num, p in enumerate(parameters): - name = name_func( - f, "{num:0>{digits}}".format(digits=digits, num=num), p - ) - # If the original function has patches applied by 'mock.patch', - # re-construct all patches on the just former decoration layer - # of param_as_standalone_func so as not to share - # patch objects between new functions - nf = reapply_patches_if_need(f) - frame_locals[name] = param_as_standalone_func(p, nf, name) - frame_locals[name].__doc__ = f.__doc__ - - # Delete original patches to prevent new function from evaluating - # original patching object as well as re-constrfucted patches. - delete_patches_if_need(f) - - f.__test__ = False - - return wrapper - - -def reapply_patches_if_need(func): - def dummy_wrapper(orgfunc): - @functools.wraps(orgfunc) - def dummy_func(*args, **kwargs): - return orgfunc(*args, **kwargs) - - return dummy_func - - if hasattr(func, 'patchings'): - func = dummy_wrapper(func) - tmp_patchings = func.patchings - delattr(func, 'patchings') - for patch_obj in tmp_patchings: - func = patch_obj.decorate_callable(func) - return func - - -def delete_patches_if_need(func): - if hasattr(func, 'patchings'): - func.patchings[:] = [] - - -def default_name_func(func, num, p): - base_name = func.__name__ - name_suffix = f"_{num}" - - if len(p.args) > 0 and isinstance(p.args[0], str): - name_suffix += "_" + to_safe_name(p.args[0]) - return base_name + name_suffix - - -def param_as_standalone_func(p, func, name): - @functools.wraps(func) - def standalone_func(*a): - return func(*(a + p.args), **p.kwargs) - - standalone_func.__name__ = name - - # place_as is used by py.test to determine what source file should be - # used for this test. - standalone_func.place_as = func - - # Remove __wrapped__ because py.test will try to look at __wrapped__ - # to determine which parameters should be used with this test case, - # and obviously we don't need it to do any parameterization. - try: - del standalone_func.__wrapped__ - except AttributeError: - pass - return standalone_func - - -def input_as_callable(input): - if callable(input): - return lambda: check_input_values(input()) - input_values = check_input_values(input) - return lambda: input_values - - -def check_input_values(input_values): - if not isinstance(input_values, list): - input_values = list(input_values) - return [param.from_decorator(p) for p in input_values] - - -def skip_on_empty_helper(*a, **kw): - raise SkipTest("parameterized input is empty") - - -_param = collections.namedtuple("param", "args kwargs") - - -class param(_param): - def __new__(cls, *args, **kwargs): - return _param.__new__(cls, args, kwargs) - - @classmethod - def explicit(cls, args=None, kwargs=None): - """Creates a ``param`` by explicitly specifying ``args`` and - ``kwargs``:: - >>> param.explicit([1,2,3]) - param(*(1, 2, 3)) - >>> param.explicit(kwargs={"foo": 42}) - param(*(), **{"foo": "42"}) - """ - args = args or () - kwargs = kwargs or {} - return cls(*args, **kwargs) - - @classmethod - def from_decorator(cls, args): - """Returns an instance of ``param()`` for ``@parameterized`` argument - ``args``:: - >>> param.from_decorator((42, )) - param(args=(42, ), kwargs={}) - >>> param.from_decorator("foo") - param(args=("foo", ), kwargs={}) - """ - if isinstance(args, param): - return args - elif isinstance(args, str): - args = (args,) - try: - return cls(*args) - except TypeError as e: - if "after * must be" not in str(e): - raise - raise TypeError( - f"Parameters must be tuples, but {args!r} is not (hint: use '({args!r}, )')", - ) - - def __repr__(self): - return "param(*{!r}, **{!r})".format(*self) - - -def to_safe_name(s): - return str(re.sub("[^a-zA-Z0-9_]+", "_", s)) - - -# alias -parameterize = parameterize_func -param_cls = parameterize_cls -param_func = parameterize_func diff --git a/test/deprecated/distribution/test_distribution.py b/test/deprecated/distribution/test_distribution.py deleted file mode 100644 index bbe564eef391cd..00000000000000 --- a/test/deprecated/distribution/test_distribution.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -class DistributionNumpy: - def sample(self): - raise NotImplementedError - - def entropy(self): - raise NotImplementedError - - def kl_divergence(self, other): - raise NotImplementedError - - def log_prob(self, value): - raise NotImplementedError - - def probs(self, value): - raise NotImplementedError diff --git a/test/distribution/CMakeLists.txt b/test/distribution/CMakeLists.txt index a31ec8e1f21370..6bcbda58612b03 100644 --- a/test/distribution/CMakeLists.txt +++ b/test/distribution/CMakeLists.txt @@ -8,4 +8,7 @@ foreach(TEST_OP ${TEST_OPS}) py_test_modules(${TEST_OP} MODULES ${TEST_OP}) endforeach() +set_tests_properties(test_distribution_lkj_cholesky_static PROPERTIES TIMEOUT + 120) + set_pir_tests_properties() diff --git a/test/deprecated/distribution/test_distribution_bernoulli_static.py b/test/distribution/test_distribution_bernoulli_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_bernoulli_static.py rename to test/distribution/test_distribution_bernoulli_static.py diff --git a/test/deprecated/distribution/test_distribution_beta_static.py b/test/distribution/test_distribution_beta_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_beta_static.py rename to test/distribution/test_distribution_beta_static.py diff --git a/test/deprecated/distribution/test_distribution_binomial_static.py b/test/distribution/test_distribution_binomial_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_binomial_static.py rename to test/distribution/test_distribution_binomial_static.py diff --git a/test/deprecated/distribution/test_distribution_categorical.py b/test/distribution/test_distribution_categorical.py similarity index 100% rename from test/deprecated/distribution/test_distribution_categorical.py rename to test/distribution/test_distribution_categorical.py diff --git a/test/deprecated/distribution/test_distribution_cauchy_static.py b/test/distribution/test_distribution_cauchy_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_cauchy_static.py rename to test/distribution/test_distribution_cauchy_static.py diff --git a/test/deprecated/distribution/test_distribution_chi2_static.py b/test/distribution/test_distribution_chi2_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_chi2_static.py rename to test/distribution/test_distribution_chi2_static.py diff --git a/test/deprecated/distribution/test_distribution_continuous_bernoulli_static.py b/test/distribution/test_distribution_continuous_bernoulli_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_continuous_bernoulli_static.py rename to test/distribution/test_distribution_continuous_bernoulli_static.py diff --git a/test/deprecated/distribution/test_distribution_expfamily_static.py b/test/distribution/test_distribution_expfamily_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_expfamily_static.py rename to test/distribution/test_distribution_expfamily_static.py diff --git a/test/deprecated/distribution/test_distribution_exponential_static.py b/test/distribution/test_distribution_exponential_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_exponential_static.py rename to test/distribution/test_distribution_exponential_static.py diff --git a/test/deprecated/distribution/test_distribution_gamma_static.py b/test/distribution/test_distribution_gamma_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_gamma_static.py rename to test/distribution/test_distribution_gamma_static.py diff --git a/test/deprecated/distribution/test_distribution_geometric_static.py b/test/distribution/test_distribution_geometric_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_geometric_static.py rename to test/distribution/test_distribution_geometric_static.py diff --git a/test/deprecated/distribution/test_distribution_gumbel_static.py b/test/distribution/test_distribution_gumbel_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_gumbel_static.py rename to test/distribution/test_distribution_gumbel_static.py diff --git a/test/deprecated/distribution/test_distribution_lkj_cholesky_static.py b/test/distribution/test_distribution_lkj_cholesky_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_lkj_cholesky_static.py rename to test/distribution/test_distribution_lkj_cholesky_static.py diff --git a/test/deprecated/distribution/test_distribution_multinomial.py b/test/distribution/test_distribution_multinomial.py similarity index 100% rename from test/deprecated/distribution/test_distribution_multinomial.py rename to test/distribution/test_distribution_multinomial.py diff --git a/test/deprecated/distribution/test_distribution_multinomial_static.py b/test/distribution/test_distribution_multinomial_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_multinomial_static.py rename to test/distribution/test_distribution_multinomial_static.py diff --git a/test/deprecated/distribution/test_distribution_multivariate_normal_static.py b/test/distribution/test_distribution_multivariate_normal_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_multivariate_normal_static.py rename to test/distribution/test_distribution_multivariate_normal_static.py diff --git a/test/deprecated/distribution/test_distribution_poisson_static.py b/test/distribution/test_distribution_poisson_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_poisson_static.py rename to test/distribution/test_distribution_poisson_static.py diff --git a/test/deprecated/distribution/test_distribution_transform_static.py b/test/distribution/test_distribution_transform_static.py similarity index 100% rename from test/deprecated/distribution/test_distribution_transform_static.py rename to test/distribution/test_distribution_transform_static.py diff --git a/test/deprecated/distribution/test_distribution_uniform.py b/test/distribution/test_distribution_uniform.py similarity index 100% rename from test/deprecated/distribution/test_distribution_uniform.py rename to test/distribution/test_distribution_uniform.py diff --git a/test/deprecated/distribution/test_kl_static.py b/test/distribution/test_kl_static.py similarity index 100% rename from test/deprecated/distribution/test_kl_static.py rename to test/distribution/test_kl_static.py From 2508e4af9dfe7d0f6e4ca58b54fcde5d9e6b7faa Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 17 Jun 2024 02:33:05 +0000 Subject: [PATCH 10/28] refine --- python/paddle/distribution/chi2.py | 7 ++++--- python/paddle/distribution/lkj_cholesky.py | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/python/paddle/distribution/chi2.py b/python/paddle/distribution/chi2.py index a23bf0573a6c12..35fffe4530e789 100644 --- a/python/paddle/distribution/chi2.py +++ b/python/paddle/distribution/chi2.py @@ -44,7 +44,7 @@ def __init__(self, df): check_type( df, 'df', - (float, Variable), + (float, Variable, paddle.pir.Value), 'Chi2', ) @@ -58,7 +58,8 @@ def __init__(self, df): self.rate = paddle.full_like(self.df, 0.5) - if not paddle.all(self.df > 0): - raise ValueError("The arg of `df` must be positive.") + if in_dynamic_mode(): + if not paddle.all(self.df > 0): + raise ValueError("The arg of `df` must be positive.") super().__init__(self.df * 0.5, self.rate) diff --git a/python/paddle/distribution/lkj_cholesky.py b/python/paddle/distribution/lkj_cholesky.py index 4e1952f5ec88f7..fc2cb5c6c33e6f 100644 --- a/python/paddle/distribution/lkj_cholesky.py +++ b/python/paddle/distribution/lkj_cholesky.py @@ -144,13 +144,13 @@ def __init__(self, dim=2, concentration=1.0, sample_method="onion"): check_type( dim, "dim", - (int, Variable), + (int, Variable, paddle.pir.Value), "LKJCholesky", ) check_type( concentration, "concentration", - (float, list, tuple, Variable), + (float, list, tuple, Variable, paddle.pir.Value), "LKJCholesky", ) @@ -170,8 +170,9 @@ def __init__(self, dim=2, concentration=1.0, sample_method="onion"): elif not isinstance(self.dim, int): raise TypeError(f"Expected dim to be an integer. Found dim={dim}.") - if not paddle.all(self.concentration > 0): - raise ValueError("The arg of `concentration` must be positive.") + if in_dynamic_mode(): + if not paddle.all(self.concentration > 0): + raise ValueError("The arg of `concentration` must be positive.") self.concentration = concentration if isinstance(self.concentration, float): From 39905c30a42d17d9caf944e51120258053c297cb Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 17 Jun 2024 11:03:27 +0000 Subject: [PATCH 11/28] refine --- .../pir/dialect/op_generator/op_build_gen.py | 4 ++-- paddle/fluid/pir/dialect/op_generator/op_gen.py | 9 +++++++++ paddle/pir/include/core/builtin_op.h | 3 ++- paddle/pir/src/core/builtin_op.cc | 15 +++++++++------ .../test_distribution_lkj_cholesky_static.py | 16 ++++++++++++++++ 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/pir/dialect/op_generator/op_build_gen.py b/paddle/fluid/pir/dialect/op_generator/op_build_gen.py index ee45bdf3382702..d771e7867b1567 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_build_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_build_gen.py @@ -819,12 +819,12 @@ def gen_build_func_str( op_non_mutable_attribute_name_list, op_non_mutable_attribute_type_list, ) - + str_is_forward_only = "true" if op_info.is_forward_only else "false" build_outputs_str = f""" std::vector argument_outputs = {op_info.class_name}::InferMeta(argument_inputs, &argument_attributes); argument.AddAttributes(argument_attributes); argument.AddOutputs(argument_outputs.begin(), argument_outputs.end()); - ::pir::PassStopGradientsDefaultly(argument);""" + ::pir::PassStopGradientsDefaultly(argument, {str_is_forward_only});""" GET_ATTRIBUTES_FROM_MAP_TEMPLATE = """ PADDLE_ENFORCE_NE( diff --git a/paddle/fluid/pir/dialect/op_generator/op_gen.py b/paddle/fluid/pir/dialect/op_generator/op_gen.py index ed4b1bae546500..ae1eb27117129d 100644 --- a/paddle/fluid/pir/dialect/op_generator/op_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/op_gen.py @@ -543,6 +543,15 @@ def __init__(self, op_yaml_item, op_compat_item, yaml_file): else: self.dynamic_fallback = False + self.is_forward_only = ( + False + if ( + 'backward' in self.op_yaml_item + and self.op_yaml_item['backward'] is not None + ) + else True + ) + def parse_op_traits(self): if 'traits' in self.op_yaml_item: return self.op_yaml_item['traits'] diff --git a/paddle/pir/include/core/builtin_op.h b/paddle/pir/include/core/builtin_op.h index 875f1c73b75650..6cb831411b8dc1 100644 --- a/paddle/pir/include/core/builtin_op.h +++ b/paddle/pir/include/core/builtin_op.h @@ -236,7 +236,8 @@ class IR_API ConstantTensorOp : public ConstantOp { std::string tensor_name(); }; -void PassStopGradientsDefaultly(OperationArgument &argument); // NOLINT +void PassStopGradientsDefaultly(OperationArgument &argument, // NOLINT + bool forward_only_op = false); void RefreshStopGradientsDefaultly(Operation *Op); } // namespace pir diff --git a/paddle/pir/src/core/builtin_op.cc b/paddle/pir/src/core/builtin_op.cc index df78e2273abc3f..30df3dd2bd7088 100644 --- a/paddle/pir/src/core/builtin_op.cc +++ b/paddle/pir/src/core/builtin_op.cc @@ -23,14 +23,17 @@ namespace pir { const char *ModuleOp::attributes_name[attributes_num] = {"program"}; // NOLINT -void PassStopGradientsDefaultly(OperationArgument &argument) { // NOLINT +void PassStopGradientsDefaultly(OperationArgument &argument, // NOLINT + bool forward_only_op) { VLOG(10) << "Builder construction stop gradient for OpResults."; bool stop_gradient = true; - for (auto value : argument.inputs) { - auto attr = value.attribute(kStopGradientAttrName); - if (attr && !attr.data()) { - stop_gradient = false; - break; + if (!forward_only_op) { + for (auto value : argument.inputs) { + auto attr = value.attribute(kStopGradientAttrName); + if (attr && !attr.data()) { + stop_gradient = false; + break; + } } } std::vector outs_stop_gradient( diff --git a/test/distribution/test_distribution_lkj_cholesky_static.py b/test/distribution/test_distribution_lkj_cholesky_static.py index 21f0bf8e42de17..558e2995ce3afb 100644 --- a/test/distribution/test_distribution_lkj_cholesky_static.py +++ b/test/distribution/test_distribution_lkj_cholesky_static.py @@ -147,12 +147,22 @@ def _test_log_prob(self, sample_method): feed=self.feeds, fetch_list=self._paddle_lkj_cholesky.sample(), ) + if paddle.framework.use_pir_api(): + block = self.program.global_block() + for op in block.ops: + if op.name() == 'pd_op.fetch': + block.remove_op(op) sample = paddle.to_tensor(sample) [log_prob] = self.executor.run( self.program, feed=self.feeds, fetch_list=self._paddle_lkj_cholesky.log_prob(sample), ) + if paddle.framework.use_pir_api(): + block = self.program.global_block() + for op in block.ops: + if op.name() == 'pd_op.fetch': + block.remove_op(op) sample_tril = tril_matrix_to_vec(sample, diag=-1) @@ -164,6 +174,12 @@ def _test_log_prob(self, sample_method): feed={'sample_tril': sample_tril}, fetch_list=self._compute_jacobian(sample_tril), ) + if paddle.framework.use_pir_api(): + block = self.program.global_block() + for op in block.ops: + if op.name() == 'pd_op.fetch': + block.remove_op(op) + logabsdet.append(logabsdet_value[1]) log_probs.append(log_prob - logabsdet) From 10d9e109b8f37202594c5e82ddbb0134f24ec9a6 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 18 Jun 2024 01:54:33 +0000 Subject: [PATCH 12/28] refine --- test/dygraph_to_static/test_high_order_net.py | 58 ------------------- 1 file changed, 58 deletions(-) delete mode 100644 test/dygraph_to_static/test_high_order_net.py diff --git a/test/dygraph_to_static/test_high_order_net.py b/test/dygraph_to_static/test_high_order_net.py deleted file mode 100644 index 9d116528ea649d..00000000000000 --- a/test/dygraph_to_static/test_high_order_net.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import unittest - -from dygraph_to_static_utils import ( - Dy2StTestBase, - test_ast_only, - test_pir_only, -) - -import paddle - - -class HighOrderNet(paddle.nn.Layer): - def __init__(self): - super().__init__() - self.linear = paddle.nn.Linear(3, 4, bias_attr=False) - - def forward(self, x): - y = self.linear(x) - z = paddle.pow(y, 2) - x_grad = paddle.grad(z, x, create_graph=True)[0] - x_grad_grad = paddle.grad(x_grad, x, create_graph=True)[0] - return x_grad_grad.mean() - - -class TestBackwardHasNoGradError(Dy2StTestBase): - @test_ast_only - @test_pir_only - def test_backward_has_no_grad_error(self): - net = HighOrderNet() - static_net = paddle.jit.to_static(net, full_graph=True) - - x = paddle.to_tensor([[1, 1, 1], [1, 1, 1]], 'float32') - x.stop_gradient = False - - with self.assertRaisesRegex( - ValueError, - "op 'pd_op.matmul_double_grad' has no grad op, consider enable prim to decompose it.", - ): - x_grad_grad = static_net(x) - x_grad_grad.backward() - - -if __name__ == "__main__": - unittest.main() From 91b753c976d5cad94ad747c72cde68f6f9375e85 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 18 Jun 2024 03:12:54 +0000 Subject: [PATCH 13/28] refine --- test/deprecated/rnn/test_rnn_api.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/deprecated/rnn/test_rnn_api.py b/test/deprecated/rnn/test_rnn_api.py index 6e607083d6605b..d38c383d909f66 100644 --- a/test/deprecated/rnn/test_rnn_api.py +++ b/test/deprecated/rnn/test_rnn_api.py @@ -22,7 +22,6 @@ import numpy as np from paddle import base -from paddle.base import framework bidirectional_list = ["bidirectional", "bidirect"] @@ -91,9 +90,9 @@ def setUp(self): self.fetch_list = [st_out, st_last_h] - self.exe.run(framework.default_startup_program()) + self.exe.run(paddle.static.default_startup_program()) - self.main_program = framework.default_main_program() + self.main_program = paddle.static.default_main_program() paddle.disable_static(self.place) @@ -194,9 +193,9 @@ def setUp(self): self.fetch_list = [st_out, st_last_h] - self.exe.run(framework.default_startup_program()) + self.exe.run(paddle.static.default_startup_program()) - self.main_program = framework.default_main_program() + self.main_program = paddle.static.default_main_program() paddle.disable_static(self.place) @@ -306,9 +305,9 @@ def setUp(self): sgd.minimize(loss) self.fetch_list = [st_out, st_last_h, "pre_h@GRAD", "x@GRAD"] - self.exe.run(framework.default_startup_program()) + self.exe.run(paddle.static.default_startup_program()) - self.main_program = framework.default_main_program() + self.main_program = paddle.static.default_main_program() paddle.disable_static(self.place) From ac03fd6e3dec2b51f7620cdb912a544bba22fdff Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 18 Jun 2024 03:27:39 +0000 Subject: [PATCH 14/28] refine --- python/paddle/nn/layer/rnn.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index 5888afc638eea2..8314cc795cc2d5 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -106,7 +106,7 @@ def rnn( """ - if in_dynamic_or_pir_mode(): + if in_dynamic_mode(): return _rnn_dynamic_graph( cell, inputs, @@ -230,7 +230,9 @@ def _rnn_static_graph( is_reverse=False, **kwargs, ): - check_type(inputs, 'inputs', (Variable, list, tuple), 'rnn') + check_type( + inputs, 'inputs', (Variable, list, tuple, paddle.pir.Value), 'rnn' + ) if isinstance(inputs, (list, tuple)): for i, input_x in enumerate(inputs): check_variable_and_dtype( @@ -239,12 +241,15 @@ def _rnn_static_graph( check_type( initial_states, 'initial_states', - (Variable, list, tuple, type(None)), + (Variable, list, tuple, type(None), paddle.pir.Value), 'rnn', ) check_type( - sequence_length, 'sequence_length', (Variable, type(None)), 'rnn' + sequence_length, + 'sequence_length', + (Variable, type(None), paddle.pir.Value), + 'rnn', ) def _switch_grad(x, stop=False): @@ -261,7 +266,7 @@ def _switch_grad(x, stop=False): inputs = paddle.utils.map_structure(_transpose_batch_time, inputs) max_seq_len = paddle.shape(paddle.utils.flatten(inputs)[0])[0] - if sequence_length: + if sequence_length is not None: mask = paddle.static.nn.sequence_lod.sequence_mask( sequence_length, maxlen=max_seq_len, @@ -272,7 +277,11 @@ def _switch_grad(x, stop=False): inputs = paddle.utils.map_structure( lambda x: paddle.reverse(x, axis=[0]), inputs ) - mask = paddle.reverse(mask, axis=[0]) if sequence_length else None + mask = ( + paddle.reverse(mask, axis=[0]) + if sequence_length is not None + else None + ) with paddle.base.framework.device_guard("cpu"): start_i = paddle.zeros([], dtype="int64") @@ -303,9 +312,11 @@ def _switch_grad(x, stop=False): lambda x: paddle.tensor.array_read(x, start_i), init_array ) outputs, new_states = cell(step_in, pre_state, **kwargs) - assert isinstance(outputs, paddle.base.framework.Variable) + assert isinstance( + outputs, (paddle.base.framework.Variable, paddle.pir.Value) + ) paddle.utils.assert_same_structure(new_states, pre_state) - if sequence_length: + if sequence_length is not None: step_mask = paddle.unsqueeze(mask[start_i], 1) # new_states = map_structure( # partial(_maybe_copy, step_mask=step_mask), @@ -1712,7 +1723,9 @@ def forward(self, inputs, initial_states=None, sequence_length=None): else: initial_states = ( [initial_states] - if isinstance(initial_states, paddle.static.Variable) + if isinstance( + initial_states, (paddle.static.Variable, paddle.pir.Value) + ) else initial_states ) From 585b924a2ace5f246ca7c3d1e5e57f40e0ce1c47 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Tue, 18 Jun 2024 08:14:42 +0000 Subject: [PATCH 15/28] refine --- paddle/phi/infermeta/unary.cc | 1 + python/paddle/autograd/ir_backward.py | 5 +++++ test/deprecated/rnn/test_rnn_api.py | 31 ++++++++++++++++++++++++--- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 90c7e2726362b2..ae849cb6ea49fc 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -203,6 +203,7 @@ void ArrayToTensorInferMeta(const MetaTensor& x, dims[axis] = -1; } out->set_dims(dims); + out->set_dtype(x.dtype()); out_index->set_dtype(DataType::INT32); out_index->set_dims(common::make_ddim({-1})); } diff --git a/python/paddle/autograd/ir_backward.py b/python/paddle/autograd/ir_backward.py index 83af23a53b0c60..ecc5f22506b17f 100644 --- a/python/paddle/autograd/ir_backward.py +++ b/python/paddle/autograd/ir_backward.py @@ -871,9 +871,14 @@ def append_yield( else: state.op_to_opgrad[op] = [] else: + all_results_stop_gradient = True + for value in op.results(): + if not value.stop_gradient: + all_results_stop_gradient = False if ( not is_builtin_op(op) and op.name() not in ALLOW_NO_GRAD_OPS + and not all_results_stop_gradient ): raise ValueError( f"op '{op.name()}' has no grad op, consider enable prim to decompose it." diff --git a/test/deprecated/rnn/test_rnn_api.py b/test/deprecated/rnn/test_rnn_api.py index d38c383d909f66..e2b31eb1f8dd0e 100644 --- a/test/deprecated/rnn/test_rnn_api.py +++ b/test/deprecated/rnn/test_rnn_api.py @@ -288,7 +288,6 @@ def setUp(self): seq_len_data = paddle.static.data( "seq_len", [self.batch_size], dtype="int64" ) - pre_h_data.stop_gradient = False rnn_in_data.stop_gradient = False @@ -302,8 +301,34 @@ def setUp(self): ) loss = paddle.sum(st_out) sgd = paddle.optimizer.SGD(0.0) - sgd.minimize(loss) - self.fetch_list = [st_out, st_last_h, "pre_h@GRAD", "x@GRAD"] + + if paddle.framework.in_pir_mode(): + rnn_in_data.persistable = True + pre_h_data.persistable = True + params_grads = paddle.base.backward.append_backward(loss) + pre_h_data_grad = None + rnn_in_data_grad = None + for p, g in params_grads: + if p.is_same(rnn_in_data): + rnn_in_data_grad = g + elif p.is_same(pre_h_data): + pre_h_data_grad = g + print(rnn_in_data_grad) + print(pre_h_data_grad) + self.fetch_list = [ + st_out, + st_last_h, + pre_h_data_grad, + rnn_in_data_grad, + ] + else: + sgd.minimize(loss) + self.fetch_list = [ + st_out, + st_last_h, + "pre_h@GRAD", + "x@GRAD", + ] self.exe.run(paddle.static.default_startup_program()) From 77bea12490f0c8843588aae78150fb37f47824d0 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 19 Jun 2024 02:58:41 +0000 Subject: [PATCH 16/28] refine --- paddle/phi/infermeta/binary.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index aa4028efa1a6ea..3df17e494b548d 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -169,7 +169,9 @@ void ArrayReadInferMeta(const MetaTensor& array, MetaTensor* out, MetaConfig config) { if (!config.is_runtime) { - out->set_dims({-1}); + out->set_dims(array.dims()); // if dims is -1, rnn will crash, return last + // ArrayWrite DenseTensor's dims. return + // array[0].dims in old static graph. } else { double index = i.to(); out->set_dims(array.dims(index)); // NOLINT From 53913002ae488b03dac4e7d091a7968ac731d639 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 19 Jun 2024 03:12:27 +0000 Subject: [PATCH 17/28] refine --- test/deprecated/rnn/test_rnn_api.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/deprecated/rnn/test_rnn_api.py b/test/deprecated/rnn/test_rnn_api.py index e2b31eb1f8dd0e..ed7b71af49da83 100644 --- a/test/deprecated/rnn/test_rnn_api.py +++ b/test/deprecated/rnn/test_rnn_api.py @@ -46,7 +46,9 @@ def setUp(self): place = paddle.set_device(self.place) paddle.disable_static(self.place) paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) + with paddle.pir_utils.OldIrGuard(): + # Note: dygraph use self.main_program.global_block().create_parameter(), it's need manual seed to old Program + paddle.framework.random._manual_program_seed(self.seed) cell_dy = paddle.nn.SimpleRNNCell(self.input_size, self.hidden_size) self.rnn_net = paddle.nn.RNN(cell_dy, time_major=self.time_major) @@ -151,7 +153,9 @@ def setUp(self): place = paddle.set_device(self.place) paddle.disable_static(self.place) paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) + with paddle.pir_utils.OldIrGuard(): + # Note: dygraph use self.main_program.global_block().create_parameter(), it's need manual seed to old Program + paddle.framework.random._manual_program_seed(self.seed) cell_dy = paddle.nn.GRUCell(self.input_size, self.hidden_size) self.rnn_net = paddle.nn.RNN(cell_dy, time_major=self.time_major) @@ -254,7 +258,9 @@ def setUp(self): place = paddle.set_device(self.place) paddle.disable_static(self.place) paddle.seed(self.seed) - paddle.framework.random._manual_program_seed(self.seed) + with paddle.pir_utils.OldIrGuard(): + # Note: dygraph use self.main_program.global_block().create_parameter(), it's need manual seed to old Program + paddle.framework.random._manual_program_seed(self.seed) cell_dy = paddle.nn.SimpleRNNCell(self.input_size, self.hidden_size) self.rnn_net = paddle.nn.RNN(cell_dy, time_major=self.time_major) From 6c926ac52bcfde0b2f039837819f7d9c35ee917e Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 19 Jun 2024 06:37:36 +0000 Subject: [PATCH 18/28] refine --- python/paddle/nn/layer/rnn.py | 43 +++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index 8314cc795cc2d5..a036eddd059ebd 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -28,7 +28,7 @@ program_guard, ) from paddle.common_ops_import import Variable -from paddle.framework import core +from paddle.framework import core, in_pir_mode from paddle.nn import ( functional as F, initializer as I, @@ -1625,19 +1625,34 @@ def flatten_parameters(self): default_startup_program(), default_startup_program() ): with paddle.no_grad(): - self._helper.append_op( - type="coalesce_tensor", - inputs={"Input": self._all_weights}, - outputs={ - "Output": self._all_weights, - "FusedOutput": self._flat_weight, - }, - attrs={ - "copy_data": True, - "use_align": False, - "dtype": params[0].dtype, - }, - ) + if in_pir_mode(): + _C_ops.coalesce_tensor( + self._all_weights, + params[0].dtype, + True, + False, + False, + 0.0, + False, + -1, + -1, + [], + [], + ) + else: + self._helper.append_op( + type="coalesce_tensor", + inputs={"Input": self._all_weights}, + outputs={ + "Output": self._all_weights, + "FusedOutput": self._flat_weight, + }, + attrs={ + "copy_data": True, + "use_align": False, + "dtype": params[0].dtype, + }, + ) def _cudnn_impl(self, inputs, initial_states, sequence_length): if not self.time_major: From ba652fbffb911ae7b47ad854849fc08c8dabedb9 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 19 Jun 2024 08:14:30 +0000 Subject: [PATCH 19/28] refine --- python/paddle/nn/layer/rnn.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index a036eddd059ebd..c7e3124fc0de94 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -1597,10 +1597,17 @@ def flatten_parameters(self): ] # dropout state may also can be hided and avoid saving # should dropout state be persistable for static-graph - self._dropout_state = self.create_variable( - dtype=core.VarDesc.VarType.UINT8, - name=f"dropout_state{NON_PERSISTABLE_VAR_NAME_SUFFIX}", - ) + if in_pir_mode(): + self._dropout_state = paddle.static.data( + name=f"dropout_state{NON_PERSISTABLE_VAR_NAME_SUFFIX}", + shape=[1], + dtype='uint8', + ) + else: + self._dropout_state = self.create_variable( + dtype=core.VarDesc.VarType.UINT8, + name=f"dropout_state{NON_PERSISTABLE_VAR_NAME_SUFFIX}", + ) if in_dynamic_mode(): with paddle.no_grad(): dtype = params[0].dtype From c9a061dbc70f78c4934cd43361a60d2aa68217eb Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Thu, 20 Jun 2024 01:47:43 +0000 Subject: [PATCH 20/28] refine --- python/paddle/base/executor.py | 17 +- python/paddle/nn/layer/rnn.py | 2 +- test/deprecated/rnn/test_rnn_cells_static.py | 122 ++++----- test/deprecated/rnn/test_rnn_nets_static.py | 271 +++++++++++++------ 4 files changed, 249 insertions(+), 163 deletions(-) diff --git a/python/paddle/base/executor.py b/python/paddle/base/executor.py index fef11f5985ef1f..f9d41cb8a69376 100755 --- a/python/paddle/base/executor.py +++ b/python/paddle/base/executor.py @@ -1403,14 +1403,15 @@ def _pir_feed_data(self, program, feed, scope, data_op_infos): # If the feed_target_name is not in feed list, but is persistable, maybe it is a optimizer param # and don't need feed data. continue - cur_feed = feed[feed_target_name] - if not isinstance(cur_feed, core.LoDTensor): - cur_feed = _as_lodtensor(cur_feed, self.place, var_type) - pir_check_feed_shape_type( - cur_feed, feed_target_name, var_shape, var_type - ) - # the last arg of set_feed_variable has no effect in pir, we pass 0 by default. - core.set_feed_variable(scope, cur_feed, feed_target_name, 0) + if "_inner_uninitialized_tensor_" != feed_target_name: + cur_feed = feed[feed_target_name] + if not isinstance(cur_feed, core.LoDTensor): + cur_feed = _as_lodtensor(cur_feed, self.place, var_type) + pir_check_feed_shape_type( + cur_feed, feed_target_name, var_shape, var_type + ) + # the last arg of set_feed_variable has no effect in pir, we pass 0 by default. + core.set_feed_variable(scope, cur_feed, feed_target_name, 0) # pop variable which is not found in program for feed_name in list(feed.keys()): diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index c7e3124fc0de94..be244d21aadd89 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -1599,7 +1599,7 @@ def flatten_parameters(self): # should dropout state be persistable for static-graph if in_pir_mode(): self._dropout_state = paddle.static.data( - name=f"dropout_state{NON_PERSISTABLE_VAR_NAME_SUFFIX}", + name="_inner_uninitialized_tensor_", shape=[1], dtype='uint8', ) diff --git a/test/deprecated/rnn/test_rnn_cells_static.py b/test/deprecated/rnn/test_rnn_cells_static.py index 54e4bd4edfc904..2f8b8d68044172 100644 --- a/test/deprecated/rnn/test_rnn_cells_static.py +++ b/test/deprecated/rnn/test_rnn_cells_static.py @@ -35,7 +35,7 @@ def __init__(self, bias=True, place="cpu"): paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0) ) - def setUp(self): + def test_with_initial_state(self): rnn1 = SimpleRNNCell(16, 32, bias=self.bias) mp = paddle.static.Program() @@ -53,22 +53,6 @@ def setUp(self): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(4, 16) prev_h = np.random.randn(4, 32) @@ -95,12 +79,22 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + rnn1 = SimpleRNNCell(16, 32, bias=self.bias) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.SimpleRNNCell( + 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias + ) + + place = self.place + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_cell_static(rnn1, rnn2, place) x = np.random.randn(4, 16) @@ -137,7 +131,7 @@ def __init__(self, bias=True, place="cpu"): paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0) ) - def setUp(self): + def test_with_initial_state(self): rnn1 = GRUCell(16, 32, bias=self.bias) mp = paddle.static.Program() @@ -155,23 +149,6 @@ def setUp(self): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(4, 16) prev_h = np.random.randn(4, 32) @@ -198,12 +175,22 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + rnn1 = GRUCell(16, 32, bias=self.bias) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.GRUCell( + 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias + ) + + place = self.place + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_cell_static(rnn1, rnn2, place) x = np.random.randn(4, 16) @@ -240,7 +227,7 @@ def __init__(self, bias=True, place="cpu"): paddle.CPUPlace() if place == "cpu" else paddle.CUDAPlace(0) ) - def setUp(self): + def test_with_initial_state(self): rnn1 = LSTMCell(16, 32, bias=self.bias) mp = paddle.static.Program() @@ -258,23 +245,6 @@ def setUp(self): exe.run(sp) convert_params_for_cell_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(4, 16) prev_h = np.random.randn(4, 32) prev_c = np.random.randn(4, 32) @@ -308,12 +278,22 @@ def test_with_initial_state(self): np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + rnn1 = LSTMCell(16, 32, bias=self.bias) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTMCell( + 16, 32, bias_ih_attr=self.bias, bias_hh_attr=self.bias + ) + + place = self.place + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_cell_static(rnn1, rnn2, place) x = np.random.randn(4, 16) diff --git a/test/deprecated/rnn/test_rnn_nets_static.py b/test/deprecated/rnn/test_rnn_nets_static.py index 3ccdad1dfc71e7..20058e2be4bd3d 100644 --- a/test/deprecated/rnn/test_rnn_nets_static.py +++ b/test/deprecated/rnn/test_rnn_nets_static.py @@ -42,9 +42,7 @@ def __init__( self.place = place self.mode = mode - def setUp(self): - # Since `set_device` is global, set `set_device` in `setUp` rather than - # `__init__` to avoid using an error device set by another test case. + def test_with_initial_state(self): place = paddle.set_device(self.place) rnn1 = SimpleRNN( 16, @@ -74,23 +72,6 @@ def setUp(self): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone().clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(12, 4, 16) if not self.time_major: x = np.transpose(x, [1, 0, 2]) @@ -120,12 +101,34 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + place = paddle.set_device(self.place) + rnn1 = SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + nonlinearity=self.mode, + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.SimpleRNN( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + activation=self.mode[4:].lower(), + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) x = np.random.randn(12, 4, 16) if not self.time_major: @@ -163,7 +166,7 @@ def __init__(self, time_major=True, direction="forward", place="cpu"): self.num_directions = 2 if direction in bidirectional_list else 1 self.place = place - def setUp(self): + def test_with_initial_state(self): # Since `set_device` is global, set `set_device` in `setUp` rather than # `__init__` to avoid using an error device set by another test case. place = paddle.set_device(self.place) @@ -189,23 +192,6 @@ def setUp(self): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(12, 4, 16) if not self.time_major: x = np.transpose(x, [1, 0, 2]) @@ -236,12 +222,30 @@ def test_with_initial_state(self): np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = GRU( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.GRU( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) x = np.random.randn(12, 4, 16) if not self.time_major: @@ -279,7 +283,7 @@ def __init__(self, time_major=True, direction="forward", place="cpu"): self.num_directions = 2 if direction in bidirectional_list else 1 self.place = place - def setUp(self): + def test_with_initial_state(self): # Since `set_device` is global, set `set_device` in `setUp` rather than # `__init__` to avoid using an error device set by another test case. place = paddle.set_device(self.place) @@ -305,23 +309,6 @@ def setUp(self): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 - - self.place = place - self.executor = exe - self.scope = scope - - def test_with_initial_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope - x = np.random.randn(12, 4, 16) if not self.time_major: x = np.transpose(x, [1, 0, 2]) @@ -364,12 +351,30 @@ def test_with_initial_state(self): np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) def test_with_zero_state(self): - mp = self.mp.clone() - sp = self.sp - rnn1 = self.rnn1 - rnn2 = self.rnn2 - exe = self.executor - scope = self.scope + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = LSTM( + 16, 32, 2, time_major=self.time_major, direction=self.direction + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + ) + + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) x = np.random.randn(12, 4, 16) if not self.time_major: @@ -400,8 +405,15 @@ def runTest(self): self.test_with_zero_state() -class TestLSTMWithProjSize(TestLSTM): - def setUp(self): +class TestLSTMWithProjSize(unittest.TestCase): + def __init__(self, time_major=True, direction="forward", place="cpu"): + super().__init__("runTest") + self.time_major = time_major + self.direction = direction + self.num_directions = 2 if direction in bidirectional_list else 1 + self.place = place + + def test_with_initial_state(self): # Since `set_device` is global, set `set_device` in `setUp` rather than # `__init__` to avoid using an error device set by another test case. place = paddle.set_device(self.place) @@ -432,16 +444,109 @@ def setUp(self): with paddle.static.scope_guard(scope): exe.run(sp) convert_params_for_net_static(rnn1, rnn2, place) + self.proj_size = 8 + + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + prev_h = np.random.randn( + 2 * self.num_directions, 4, getattr(self, "proj_size", 32) + ) + prev_c = np.random.randn(2 * self.num_directions, 4, 32) + + y1, (h1, c1) = rnn1(x, (prev_h, prev_c)) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + init_h = paddle.static.data( + "init_h", + [ + 2 * self.num_directions, + -1, + getattr(self, "proj_size", 32), + ], + dtype=paddle.framework.get_default_dtype(), + ) + init_c = paddle.static.data( + "init_c", + [2 * self.num_directions, -1, 32], + dtype=paddle.framework.get_default_dtype(), + ) + y, (h, c) = rnn2(x_data, (init_h, init_c)) + + feed_dict = {x_data.name: x, init_h.name: prev_h, init_c.name: prev_c} + with paddle.static.scope_guard(scope): + y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) + + def test_with_zero_state(self): + # Since `set_device` is global, set `set_device` in `setUp` rather than + # `__init__` to avoid using an error device set by another test case. + place = paddle.set_device(self.place) + rnn1 = LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) + + mp = paddle.static.Program() + sp = paddle.static.Program() + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + rnn2 = paddle.nn.LSTM( + 16, + 32, + 2, + time_major=self.time_major, + direction=self.direction, + proj_size=8, + ) - self.mp = mp - self.sp = sp - self.rnn1 = rnn1 - self.rnn2 = rnn2 + exe = paddle.static.Executor(place) + scope = paddle.base.Scope() + with paddle.static.scope_guard(scope): + exe.run(sp) + convert_params_for_net_static(rnn1, rnn2, place) self.proj_size = 8 - self.place = place - self.executor = exe - self.scope = scope + x = np.random.randn(12, 4, 16) + if not self.time_major: + x = np.transpose(x, [1, 0, 2]) + + y1, (h1, c1) = rnn1(x) + + with paddle.base.unique_name.guard(): + with paddle.static.program_guard(mp, sp): + x_data = paddle.static.data( + "input", + [-1, -1, 16], + dtype=paddle.framework.get_default_dtype(), + ) + y, (h, c) = rnn2(x_data) + + feed_dict = {x_data.name: x} + + with paddle.static.scope_guard(scope): + y2, h2, c2 = exe.run(mp, feed=feed_dict, fetch_list=[y, h, c]) + + np.testing.assert_allclose(y1, y2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(h1, h2, atol=1e-8, rtol=1e-5) + np.testing.assert_allclose(c1, c2, atol=1e-8, rtol=1e-5) + + def runTest(self): + self.test_with_initial_state() + self.test_with_zero_state() def load_tests(loader, tests, pattern): From dad8eda3ae0335077804669b3bd04eb118c6a67d Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Thu, 20 Jun 2024 02:45:41 +0000 Subject: [PATCH 21/28] fix 3 rnn --- python/paddle/base/executor.py | 17 ++++++++--------- python/paddle/nn/layer/rnn.py | 15 ++++----------- test/deprecated/rnn/CMakeLists.txt | 1 - test/rnn/CMakeLists.txt | 4 ++++ .../rnn/test_rnn_cells_static.py | 0 .../rnn/test_rnn_cudnn_params_packing.py | 0 test/{deprecated => }/rnn/test_rnn_nets.py | 0 7 files changed, 16 insertions(+), 21 deletions(-) rename test/{deprecated => }/rnn/test_rnn_cells_static.py (100%) rename test/{deprecated => }/rnn/test_rnn_cudnn_params_packing.py (100%) rename test/{deprecated => }/rnn/test_rnn_nets.py (100%) diff --git a/python/paddle/base/executor.py b/python/paddle/base/executor.py index f9d41cb8a69376..fef11f5985ef1f 100755 --- a/python/paddle/base/executor.py +++ b/python/paddle/base/executor.py @@ -1403,15 +1403,14 @@ def _pir_feed_data(self, program, feed, scope, data_op_infos): # If the feed_target_name is not in feed list, but is persistable, maybe it is a optimizer param # and don't need feed data. continue - if "_inner_uninitialized_tensor_" != feed_target_name: - cur_feed = feed[feed_target_name] - if not isinstance(cur_feed, core.LoDTensor): - cur_feed = _as_lodtensor(cur_feed, self.place, var_type) - pir_check_feed_shape_type( - cur_feed, feed_target_name, var_shape, var_type - ) - # the last arg of set_feed_variable has no effect in pir, we pass 0 by default. - core.set_feed_variable(scope, cur_feed, feed_target_name, 0) + cur_feed = feed[feed_target_name] + if not isinstance(cur_feed, core.LoDTensor): + cur_feed = _as_lodtensor(cur_feed, self.place, var_type) + pir_check_feed_shape_type( + cur_feed, feed_target_name, var_shape, var_type + ) + # the last arg of set_feed_variable has no effect in pir, we pass 0 by default. + core.set_feed_variable(scope, cur_feed, feed_target_name, 0) # pop variable which is not found in program for feed_name in list(feed.keys()): diff --git a/python/paddle/nn/layer/rnn.py b/python/paddle/nn/layer/rnn.py index be244d21aadd89..a036eddd059ebd 100644 --- a/python/paddle/nn/layer/rnn.py +++ b/python/paddle/nn/layer/rnn.py @@ -1597,17 +1597,10 @@ def flatten_parameters(self): ] # dropout state may also can be hided and avoid saving # should dropout state be persistable for static-graph - if in_pir_mode(): - self._dropout_state = paddle.static.data( - name="_inner_uninitialized_tensor_", - shape=[1], - dtype='uint8', - ) - else: - self._dropout_state = self.create_variable( - dtype=core.VarDesc.VarType.UINT8, - name=f"dropout_state{NON_PERSISTABLE_VAR_NAME_SUFFIX}", - ) + self._dropout_state = self.create_variable( + dtype=core.VarDesc.VarType.UINT8, + name=f"dropout_state{NON_PERSISTABLE_VAR_NAME_SUFFIX}", + ) if in_dynamic_mode(): with paddle.no_grad(): dtype = params[0].dtype diff --git a/test/deprecated/rnn/CMakeLists.txt b/test/deprecated/rnn/CMakeLists.txt index a06731560086dd..e015f3f6bbcebe 100644 --- a/test/deprecated/rnn/CMakeLists.txt +++ b/test/deprecated/rnn/CMakeLists.txt @@ -10,6 +10,5 @@ endforeach() if(NOT WIN32) set_tests_properties(test_rnn_nets_static PROPERTIES TIMEOUT 120) set_tests_properties(test_rnn_nets_static_deprecated PROPERTIES TIMEOUT 120) - set_tests_properties(test_rnn_nets PROPERTIES TIMEOUT 120) set_tests_properties(test_rnn_nets_deprecated PROPERTIES TIMEOUT 120) endif() diff --git a/test/rnn/CMakeLists.txt b/test/rnn/CMakeLists.txt index 95739040ef4af7..fe9c6d0eae4ac8 100644 --- a/test/rnn/CMakeLists.txt +++ b/test/rnn/CMakeLists.txt @@ -7,3 +7,7 @@ string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") foreach(TEST_OP ${TEST_OPS}) py_test_modules(${TEST_OP} MODULES ${TEST_OP}) endforeach() + +if(NOT WIN32) + set_tests_properties(test_rnn_nets PROPERTIES TIMEOUT 120) +endif() diff --git a/test/deprecated/rnn/test_rnn_cells_static.py b/test/rnn/test_rnn_cells_static.py similarity index 100% rename from test/deprecated/rnn/test_rnn_cells_static.py rename to test/rnn/test_rnn_cells_static.py diff --git a/test/deprecated/rnn/test_rnn_cudnn_params_packing.py b/test/rnn/test_rnn_cudnn_params_packing.py similarity index 100% rename from test/deprecated/rnn/test_rnn_cudnn_params_packing.py rename to test/rnn/test_rnn_cudnn_params_packing.py diff --git a/test/deprecated/rnn/test_rnn_nets.py b/test/rnn/test_rnn_nets.py similarity index 100% rename from test/deprecated/rnn/test_rnn_nets.py rename to test/rnn/test_rnn_nets.py From 4bba47f5da177125bf1acca806b6eb0fe96c60e1 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Thu, 20 Jun 2024 03:19:38 +0000 Subject: [PATCH 22/28] refine --- test/deprecated/rnn/test_rnn_api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/deprecated/rnn/test_rnn_api.py b/test/deprecated/rnn/test_rnn_api.py index ed7b71af49da83..6a007f17f1d7e6 100644 --- a/test/deprecated/rnn/test_rnn_api.py +++ b/test/deprecated/rnn/test_rnn_api.py @@ -319,8 +319,7 @@ def setUp(self): rnn_in_data_grad = g elif p.is_same(pre_h_data): pre_h_data_grad = g - print(rnn_in_data_grad) - print(pre_h_data_grad) + self.fetch_list = [ st_out, st_last_h, From dd9e0da5c593407fe63be979e0a842fff337f16f Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Thu, 20 Jun 2024 07:12:38 +0000 Subject: [PATCH 23/28] refine --- paddle/fluid/pir/dialect/operator/ir/manual_op.cc | 14 ++++++++------ paddle/phi/infermeta/binary.cc | 10 +++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/pir/dialect/operator/ir/manual_op.cc b/paddle/fluid/pir/dialect/operator/ir/manual_op.cc index 2169bb93b0f823..fda95a81954f7a 100644 --- a/paddle/fluid/pir/dialect/operator/ir/manual_op.cc +++ b/paddle/fluid/pir/dialect/operator/ir/manual_op.cc @@ -2035,14 +2035,16 @@ std::vector ArrayWrite_Op::InferMeta( pir::IrContext::Instance(), place, array_type.dtype(), - x_type.dims(), + phi::product(array_type.dims()) == 0 ? x_type.dims() + : array_type.dims(), array_type.data_layout())); } else if (array_.type().isa()) { - array_.set_type( - paddle::dialect::DenseTensorArrayType::get(pir::IrContext::Instance(), - array_type.dtype(), - x_type.dims(), - array_type.data_layout())); + array_.set_type(paddle::dialect::DenseTensorArrayType::get( + pir::IrContext::Instance(), + array_type.dtype(), + phi::product(array_type.dims()) == 0 ? x_type.dims() + : array_type.dims(), + array_type.data_layout())); } argument_outputs.push_back(out_type); diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index dd831f6b7968f3..eec0d75ac72cef 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -169,9 +169,8 @@ void ArrayReadInferMeta(const MetaTensor& array, MetaTensor* out, MetaConfig config) { if (!config.is_runtime) { - out->set_dims(array.dims()); // if dims is -1, rnn will crash, return last - // ArrayWrite DenseTensor's dims. return - // array[0].dims in old static graph. + out->set_dims(array.dims()); // if dims is -1, rnn will crash, return + // array[0].dims same with old static graph. } else { double index = i.to(); out->set_dims(array.dims(index)); // NOLINT @@ -1652,8 +1651,9 @@ void ElementwiseRawInferMeta(const MetaTensor& x, -1 * max_dim, max_dim, axis)); - axis = (axis < 0 ? (std::abs(x_dims.size() - y_dims.size()) + axis + 1) - : axis); + int x_size = x_dims.size() == -1 ? 0 : x_dims.size(); + int y_size = y_dims.size() == -1 ? 0 : y_dims.size(); + axis = (axis < 0 ? (std::abs(x_size - y_size) + axis + 1) : axis); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); std::vector out_dims_array(max_dim); From 1da2672d3ea5408682738a590c0167fad2c07f2a Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 21 Jun 2024 06:41:39 +0000 Subject: [PATCH 24/28] refine --- paddle/phi/infermeta/binary.cc | 4 +++- paddle/phi/infermeta/fusion.cc | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index f1730f79db23c9..fb3714299b710e 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -469,7 +469,9 @@ void CompareRawInferMeta(const MetaTensor& x, out->share_meta(x); } else { int max_dim = std::max(dim_x.size(), dim_y.size()); - int axis = std::abs(dim_x.size() - dim_y.size()); + int dim_x_size = dim_x.size() == -1 ? 0 : dim_x.size(); + int dim_y_size = dim_y.size() == -1 ? 0 : dim_y.size(); + int axis = std::abs(dim_x_size - dim_y_size); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); std::vector out_dims_array(max_dim); diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index 22408ad9204d06..ff7f4cac6e27b6 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -53,8 +53,10 @@ static phi::DDim BroadCastInferShape(const DDim x_dims, -1 * max_dim, max_dim, axis)); - axis = (axis < 0 ? (std::abs(x_dims.size() - y_dims.size()) + axis + 1) - : axis); + int dim_x_size = x_dims.size() == -1 ? 0 : x_dims.size(); + int dim_y_size = y_dims.size() == -1 ? 0 : y_dims.size(); + + axis = (axis < 0 ? (std::abs(dim_x_size - dim_y_size) + axis + 1) : axis); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); out_dims_array.resize(max_dim); From adf0ca6704a6ed93099b71a4738636557608da21 Mon Sep 17 00:00:00 2001 From: chen2016013 Date: Fri, 21 Jun 2024 07:41:54 +0000 Subject: [PATCH 25/28] fix rnn test bug --- .../instruction/control_flow/tuple_push_instruction.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc b/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc index 23b22a167eb42d..19a5168cf69453 100644 --- a/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc +++ b/paddle/fluid/framework/new_executor/instruction/control_flow/tuple_push_instruction.cc @@ -99,7 +99,6 @@ void TuplePushInstruction::Run() { for (size_t i = 0; i < tuple_push_op_.tuple_size(); i++) { auto inlet_element_value = tuple_push_op_.inlet_element(i); Variable* var = value_exe_info_->GetVarByValue(inlet_element_value); - int stack_size = tuple_push_op_.tuple_size(); auto var_name = value_2_var_name.at(inlet_element_value); auto num_str = std::to_string(stack_element_var_array_->size()); @@ -110,7 +109,7 @@ void TuplePushInstruction::Run() { DeepCopyVariable(var, ©_var, value_exe_info_, - stack_size, + stack_element_var_array_->size(), is_optional, &src_to_dst); VLOG(10) << "done DeepCopyVariable " << new_name; From 25007f30ffbc736aff3f1440c3de5d60fcd2bdf2 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Fri, 21 Jun 2024 08:05:01 +0000 Subject: [PATCH 26/28] refine --- paddle/fluid/pir/dialect/operator/ir/manual_op.cc | 14 ++++++-------- paddle/phi/infermeta/binary.cc | 7 +++++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/paddle/fluid/pir/dialect/operator/ir/manual_op.cc b/paddle/fluid/pir/dialect/operator/ir/manual_op.cc index fda95a81954f7a..2169bb93b0f823 100644 --- a/paddle/fluid/pir/dialect/operator/ir/manual_op.cc +++ b/paddle/fluid/pir/dialect/operator/ir/manual_op.cc @@ -2035,16 +2035,14 @@ std::vector ArrayWrite_Op::InferMeta( pir::IrContext::Instance(), place, array_type.dtype(), - phi::product(array_type.dims()) == 0 ? x_type.dims() - : array_type.dims(), + x_type.dims(), array_type.data_layout())); } else if (array_.type().isa()) { - array_.set_type(paddle::dialect::DenseTensorArrayType::get( - pir::IrContext::Instance(), - array_type.dtype(), - phi::product(array_type.dims()) == 0 ? x_type.dims() - : array_type.dims(), - array_type.data_layout())); + array_.set_type( + paddle::dialect::DenseTensorArrayType::get(pir::IrContext::Instance(), + array_type.dtype(), + x_type.dims(), + array_type.data_layout())); } argument_outputs.push_back(out_type); diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index fb3714299b710e..cd5effda551c2c 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -169,8 +169,11 @@ void ArrayReadInferMeta(const MetaTensor& array, MetaTensor* out, MetaConfig config) { if (!config.is_runtime) { - out->set_dims(array.dims()); // if dims is -1, rnn will crash, return - // array[0].dims same with old static graph. + auto dims = array.dims(); + for (int i = 0; i < dims.size(); ++i) { + dims[i] = -1; + } + out->set_dims(dims); } else { double index = i.to(); out->set_dims(array.dims(index)); // NOLINT From dc802719f7343aba8f2e1a523c0a53864f94c878 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 24 Jun 2024 01:55:43 +0000 Subject: [PATCH 27/28] refine --- paddle/phi/infermeta/binary.cc | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index cd5effda551c2c..dcb290f3ec58bb 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -169,11 +169,7 @@ void ArrayReadInferMeta(const MetaTensor& array, MetaTensor* out, MetaConfig config) { if (!config.is_runtime) { - auto dims = array.dims(); - for (int i = 0; i < dims.size(); ++i) { - dims[i] = -1; - } - out->set_dims(dims); + out->set_dims({-1}); } else { double index = i.to(); out->set_dims(array.dims(index)); // NOLINT From 956a0ffb520caf6db101bb19bf453932be2a4f6d Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Mon, 24 Jun 2024 07:05:58 +0000 Subject: [PATCH 28/28] refine --- paddle/phi/infermeta/binary.cc | 19 ++++++++++++------- paddle/phi/infermeta/fusion.cc | 6 ++---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/paddle/phi/infermeta/binary.cc b/paddle/phi/infermeta/binary.cc index dcb290f3ec58bb..2db5131c3daa4d 100644 --- a/paddle/phi/infermeta/binary.cc +++ b/paddle/phi/infermeta/binary.cc @@ -169,7 +169,15 @@ void ArrayReadInferMeta(const MetaTensor& array, MetaTensor* out, MetaConfig config) { if (!config.is_runtime) { - out->set_dims({-1}); + auto dims = array.dims(); + if (dims.size() > 1) { + for (int i = 0; i < dims.size(); ++i) { + dims[i] = -1; + } + out->set_dims(dims); + } else { + out->set_dims({-1}); + } } else { double index = i.to(); out->set_dims(array.dims(index)); // NOLINT @@ -468,9 +476,7 @@ void CompareRawInferMeta(const MetaTensor& x, out->share_meta(x); } else { int max_dim = std::max(dim_x.size(), dim_y.size()); - int dim_x_size = dim_x.size() == -1 ? 0 : dim_x.size(); - int dim_y_size = dim_y.size() == -1 ? 0 : dim_y.size(); - int axis = std::abs(dim_x_size - dim_y_size); + int axis = std::abs(dim_x.size() - dim_y.size()); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); std::vector out_dims_array(max_dim); @@ -1672,9 +1678,8 @@ void ElementwiseRawInferMeta(const MetaTensor& x, -1 * max_dim, max_dim, axis)); - int x_size = x_dims.size() == -1 ? 0 : x_dims.size(); - int y_size = y_dims.size() == -1 ? 0 : y_dims.size(); - axis = (axis < 0 ? (std::abs(x_size - y_size) + axis + 1) : axis); + axis = (axis < 0 ? (std::abs(x_dims.size() - y_dims.size()) + axis + 1) + : axis); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); std::vector out_dims_array(max_dim); diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index ff7f4cac6e27b6..22408ad9204d06 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -53,10 +53,8 @@ static phi::DDim BroadCastInferShape(const DDim x_dims, -1 * max_dim, max_dim, axis)); - int dim_x_size = x_dims.size() == -1 ? 0 : x_dims.size(); - int dim_y_size = y_dims.size() == -1 ? 0 : y_dims.size(); - - axis = (axis < 0 ? (std::abs(dim_x_size - dim_y_size) + axis + 1) : axis); + axis = (axis < 0 ? (std::abs(x_dims.size() - y_dims.size()) + axis + 1) + : axis); std::vector x_dims_array(max_dim); std::vector y_dims_array(max_dim); out_dims_array.resize(max_dim);