From 26bc14728b71b3f68e934e91b53e4619b28bc043 Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 03:34:49 +0000 Subject: [PATCH 1/3] pir support incubate.autograd --- python/paddle/incubate/autograd/functional.py | 14 +- python/paddle/incubate/autograd/primapi.py | 30 +- python/paddle/incubate/autograd/primx.py | 20 +- python/paddle/incubate/autograd/utils.py | 6 +- .../test_autograd_functional_static.py | 0 test/deprecated/CMakeLists.txt | 1 - test/deprecated/autograd/CMakeLists.txt | 12 - test/deprecated/autograd/config.py | 33 -- test/deprecated/autograd/utils.py | 454 ------------------ 9 files changed, 50 insertions(+), 520 deletions(-) rename test/{deprecated => }/autograd/test_autograd_functional_static.py (100%) delete mode 100644 test/deprecated/autograd/CMakeLists.txt delete mode 100644 test/deprecated/autograd/config.py delete mode 100644 test/deprecated/autograd/utils.py diff --git a/python/paddle/incubate/autograd/functional.py b/python/paddle/incubate/autograd/functional.py index 8ae915a1e48685..b2f19ee83f8063 100644 --- a/python/paddle/incubate/autograd/functional.py +++ b/python/paddle/incubate/autograd/functional.py @@ -582,7 +582,7 @@ def _grad(ys, xs, v=None): # xs_grad when the xs is a single Tensor. xs_grad = paddle.grad(ys, xs, v, create_graph=True, allow_unused=True) if ( - isinstance(xs, paddle.base.framework.Variable) + isinstance(xs, (paddle.base.framework.Variable, paddle.pir.Value)) and isinstance(xs_grad, typing.Sequence) and len(xs_grad) > 0 ): @@ -658,23 +658,27 @@ def _check_inputs(func, xs, v=None): if not callable(func): raise TypeError(f"Expected 'fun' is Callable, but got {type(func)}.") - if not isinstance(xs, (framework.Variable, typing.Sequence)): + if not isinstance( + xs, (framework.Variable, typing.Sequence, paddle.pir.Value) + ): raise TypeError( f"Expected 'xs' is a Tensor|Sequence[Tensor]," f"but got {type(xs)}." ) if isinstance(xs, typing.Sequence) and not all( - isinstance(x, framework.Variable) for x in xs + isinstance(x, (framework.Variable, paddle.pir.Value)) for x in xs ): raise TypeError("All elements of 'xs' should be Tensor.") - if not isinstance(v, (framework.Variable, typing.Sequence, type(None))): + if not isinstance( + v, (framework.Variable, typing.Sequence, type(None), paddle.pir.Value) + ): raise TypeError( f"Expected 'v' is Tensor|Sequence[Tensor]|None, but got {type(v)}." ) if isinstance(v, typing.Sequence) and not all( - isinstance(e, framework.Variable) for e in v + isinstance(e, (framework.Variable, paddle.pir.Value)) for e in v ): raise TypeError("All elements of 'xs' should be Tensor.") diff --git a/python/paddle/incubate/autograd/primapi.py b/python/paddle/incubate/autograd/primapi.py index 109cde97a75ca3..a33e1f4dfb8de6 100644 --- a/python/paddle/incubate/autograd/primapi.py +++ b/python/paddle/incubate/autograd/primapi.py @@ -72,13 +72,17 @@ def forward_grad(outputs, inputs, grad_inputs=None): 'operators, use enable_prim to turn it on.' ) - if not isinstance(outputs, (framework.Variable, typing.Sequence)): + if not isinstance( + outputs, (framework.Variable, typing.Sequence, paddle.pir.Value) + ): raise TypeError( f'Expected outputs is Tensor|Sequence[Tensor], ' f'but got {type(outputs)}.' ) - if not isinstance(inputs, (framework.Variable, typing.Sequence)): + if not isinstance( + inputs, (framework.Variable, typing.Sequence, paddle.pir.Value) + ): raise TypeError( f'Expected inputs is Tensor|Sequence[Tensor], ' f'but got {type(inputs)}.' @@ -101,7 +105,11 @@ def forward_grad(outputs, inputs, grad_inputs=None): ad = primx.Transform(ys[0].block) _, ys_dot = ad.linearize(xs, ys, xs_dot) - return ys_dot[0] if isinstance(outputs, framework.Variable) else ys_dot + return ( + ys_dot[0] + if isinstance(outputs, (framework.Variable, paddle.pir.Value)) + else ys_dot + ) @framework.static_only @@ -155,7 +163,7 @@ def grad(outputs, inputs, grad_outputs=None): # The follow code snippet fixes the problem by return the first element # of grad_inputs when the inputs is a single Tensor. if ( - isinstance(inputs, framework.Variable) + isinstance(inputs, (framework.Variable, paddle.pir.Value)) and isinstance(grad_inputs, typing.Sequence) and len(grad_inputs) > 0 ): @@ -163,13 +171,17 @@ def grad(outputs, inputs, grad_outputs=None): else: return grad_inputs - if not isinstance(outputs, (framework.Variable, typing.Sequence)): + if not isinstance( + outputs, (framework.Variable, typing.Sequence, paddle.pir.Value) + ): raise TypeError( f'Expected outputs is Tensor|Sequence[Tensor], ' f'but got {type(outputs)}.' ) - if not isinstance(inputs, (framework.Variable, typing.Sequence)): + if not isinstance( + inputs, (framework.Variable, typing.Sequence, paddle.pir.Value) + ): raise TypeError( f'Expected inputs is Tensor|Sequence[Tensor], ' f'but got {type(inputs)}.' @@ -213,7 +225,11 @@ def grad(outputs, inputs, grad_outputs=None): ad.erase_ops(sorted(op_indexes)) ad.erase_dots(xs_dot) - return xs_bar[0] if isinstance(inputs, framework.Variable) else xs_bar + return ( + xs_bar[0] + if isinstance(inputs, (framework.Variable, paddle.pir.Value)) + else xs_bar + ) @framework.static_only diff --git a/python/paddle/incubate/autograd/primx.py b/python/paddle/incubate/autograd/primx.py index 901e23a6499748..ba2f0c2e615a12 100644 --- a/python/paddle/incubate/autograd/primx.py +++ b/python/paddle/incubate/autograd/primx.py @@ -137,8 +137,12 @@ def add(self, key_var, value_var): def add_rec(self, key_vars, value_vars): if value_vars is None: return - if isinstance(key_vars, paddle.base.framework.Variable): - if not isinstance(value_vars, paddle.base.framework.Variable): + if isinstance( + key_vars, (paddle.base.framework.Variable, paddle.pir.Value) + ): + if not isinstance( + value_vars, (paddle.base.framework.Variable, paddle.pir.Value) + ): raise TypeError( f'value_vars must be Variable, but got {type(value_vars)}' ) @@ -208,7 +212,9 @@ def add_vars(self, new_vars): def add_vars_rec(self, new_vars): if new_vars is None: return - if isinstance(new_vars, paddle.base.framework.Variable): + if isinstance( + new_vars, (paddle.base.framework.Variable, paddle.pir.Value) + ): self.vars.update({id(new_vars): new_vars}) return if not isinstance(new_vars, list): @@ -242,7 +248,7 @@ def erase_dots(self, vars_to_erase): def var2dot_rec(self, vars): """Lookup var2dot recursively.""" - if isinstance(vars, paddle.base.framework.Variable): + if isinstance(vars, (paddle.base.framework.Variable, paddle.pir.Value)): dot = self.var2dot.lookup(vars) return dot @@ -250,7 +256,7 @@ def var2dot_rec(self, vars): return dots def dot2bar_rec(self, dots): - if isinstance(dots, paddle.base.framework.Variable): + if isinstance(dots, (paddle.base.framework.Variable, paddle.pir.Value)): bar = self.dot2bar.lookup(dots) assert bar is not None, 'bar must be not None' return bar @@ -385,7 +391,9 @@ def bind(args, to_bind, value_table): for i in range(len(args)): if isinstance(args[i], list): bind(args[i], to_bind, value_table) - if not isinstance(args[i], paddle.base.framework.Variable): + if not isinstance( + args[i], (paddle.base.framework.Variable, paddle.pir.Value) + ): continue elif args[i] is not None and args[i].name in to_bind: args[i] = value_table[to_bind[args[i].name]] diff --git a/python/paddle/incubate/autograd/utils.py b/python/paddle/incubate/autograd/utils.py index b5bc0c6238ea70..0518071a90040a 100644 --- a/python/paddle/incubate/autograd/utils.py +++ b/python/paddle/incubate/autograd/utils.py @@ -309,7 +309,9 @@ def map_output_for_composite(op): def flatten(inp): - if inp is None or isinstance(inp, paddle.base.framework.Variable): + if inp is None or isinstance( + inp, (paddle.base.framework.Variable, paddle.pir.Value) + ): return [inp] flattened = [] for part in inp: @@ -323,7 +325,7 @@ def flatten_and_remove_none(inp): def as_tensors(xs): - if isinstance(xs, framework.Variable): + if isinstance(xs, (framework.Variable, paddle.pir.Value)): return (xs,) elif isinstance(xs, typing.Sequence): return tuple(xs) diff --git a/test/deprecated/autograd/test_autograd_functional_static.py b/test/autograd/test_autograd_functional_static.py similarity index 100% rename from test/deprecated/autograd/test_autograd_functional_static.py rename to test/autograd/test_autograd_functional_static.py diff --git a/test/deprecated/CMakeLists.txt b/test/deprecated/CMakeLists.txt index ffaf747a547d08..08950837bb9a59 100644 --- a/test/deprecated/CMakeLists.txt +++ b/test/deprecated/CMakeLists.txt @@ -134,7 +134,6 @@ if(WITH_TESTING) else() add_subdirectory(amp) add_subdirectory(asp) - add_subdirectory(autograd) add_subdirectory(custom_op) add_subdirectory(custom_runtime) add_subdirectory(cpp_extension) diff --git a/test/deprecated/autograd/CMakeLists.txt b/test/deprecated/autograd/CMakeLists.txt deleted file mode 100644 index 35e12e591aea88..00000000000000 --- a/test/deprecated/autograd/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -file( - GLOB TEST_OPS - RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" - "test_*.py") -string(REPLACE ".py" "" TEST_OPS "${TEST_OPS}") -set(GC_ENVS FLAGS_eager_delete_tensor_gb=0.0) - -foreach(TEST_OP ${TEST_OPS}) - py_test_modules(${TEST_OP} MODULES ${TEST_OP} ENVS ${GC_ENVS}) -endforeach() - -set_tests_properties(test_autograd_functional_static PROPERTIES TIMEOUT 160) diff --git a/test/deprecated/autograd/config.py b/test/deprecated/autograd/config.py deleted file mode 100644 index ff2d64a43bbc92..00000000000000 --- a/test/deprecated/autograd/config.py +++ /dev/null @@ -1,33 +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 - -DEVICES = [paddle.CPUPlace()] -if paddle.is_compiled_with_cuda(): - DEVICES.append(paddle.CUDAPlace(0)) - -DEFAULT_DTYPE = 'float64' - -# The numerical tolerance of different dtype of different order different -# derivative. It's a empirical value provided by Paddle Science team. -TOLERANCE = { - "float32": { - "first_order_grad": {"rtol": 1e-3, "atol": 1e-3, "eps": 1e-4}, - "second_order_grad": {"rtol": 1e-2, "atol": 1e-2, "eps": 1e-2}, - }, - "float64": { - "first_order_grad": {"rtol": 1e-7, "atol": 1e-7, "eps": 1e-7}, - "second_order_grad": {"rtol": 1e-5, "atol": 1e-5, "eps": 1e-5}, - }, -} diff --git a/test/deprecated/autograd/utils.py b/test/deprecated/autograd/utils.py deleted file mode 100644 index 64a16897d9b254..00000000000000 --- a/test/deprecated/autograd/utils.py +++ /dev/null @@ -1,454 +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 enum -import sys -import typing - -import numpy as np - -import paddle -from paddle.incubate.autograd.utils import as_tensors - - -########################################################## -# Finite Difference Utils -########################################################## -def _product(t): - return int(np.prod(t)) - - -def _get_item(t, idx): - assert isinstance( - t, paddle.base.framework.Variable - ), "The first argument t must be Tensor." - assert isinstance( - idx, int - ), "The second argument idx must be an int number." - flat_t = paddle.reshape(t, [-1]) - return flat_t.__getitem__(idx) - - -def _set_item(t, idx, value): - assert isinstance( - t, paddle.base.framework.Variable - ), "The first argument t must be Tensor." - assert isinstance( - idx, int - ), "The second argument idx must be an int number." - flat_t = paddle.reshape(t, [-1]) - flat_t.__setitem__(idx, value) - return paddle.reshape(flat_t, t.shape) - - -def _compute_numerical_jacobian(func, xs, delta, np_dtype): - xs = list(as_tensors(xs)) - ys = list(as_tensors(func(*xs))) - fin_size = len(xs) - fout_size = len(ys) - jacobian = [[] for _ in range(fout_size)] - for i in range(fout_size): - jac_i = [[] for _ in range(fin_size)] - for j in range(fin_size): - jac_i[j] = np.zeros( - (_product(ys[i].shape), _product(xs[j].shape)), dtype=np_dtype - ) - jacobian[i] = jac_i - - for j in range(fin_size): - for q in range(_product(xs[j].shape)): - orig = _get_item(xs[j], q) - orig = paddle.assign(orig) - x_pos = orig + delta - xs[j] = _set_item(xs[j], q, x_pos) - ys_pos = as_tensors(func(*xs)) - - x_neg = orig - delta - xs[j] = _set_item(xs[j], q, x_neg) - ys_neg = as_tensors(func(*xs)) - - xs[j] = _set_item(xs[j], q, orig) - - for i in range(fout_size): - for p in range(_product(ys[i].shape)): - y_pos = _get_item(ys_pos[i], p) - y_neg = _get_item(ys_neg[i], p) - jacobian[i][j][p][q] = (y_pos - y_neg) / delta / 2.0 - return jacobian - - -def _compute_numerical_hessian(func, xs, delta, np_dtype): - xs = list(as_tensors(xs)) - ys = list(as_tensors(func(*xs))) - fin_size = len(xs) - hessian = [[] for _ in range(fin_size)] - for i in range(fin_size): - hessian_i = [[] for _ in range(fin_size)] - for j in range(fin_size): - hessian_i[j] = np.zeros( - (_product(xs[i].shape), _product(xs[j].shape)), dtype=np_dtype - ) - hessian[i] = hessian_i - - for i in range(fin_size): - for p in range(_product(xs[i].shape)): - for j in range(fin_size): - for q in range(_product(xs[j].shape)): - orig = _get_item(xs[j], q) - orig = paddle.assign(orig) - x_pos = orig + delta - xs[j] = _set_item(xs[j], q, x_pos) - jacobian_pos = _compute_numerical_jacobian( - func, xs, delta, np_dtype - ) - x_neg = orig - delta - xs[j] = _set_item(xs[j], q, x_neg) - jacobian_neg = _compute_numerical_jacobian( - func, xs, delta, np_dtype - ) - xs[j] = _set_item(xs[j], q, orig) - hessian[i][j][p][q] = ( - (jacobian_pos[0][i][0][p] - jacobian_neg[0][i][0][p]) - / delta - / 2.0 - ) - return hessian - - -def concat_to_matrix(xs, is_batched=False): - """Concats a tuple of tuple of Jacobian/Hessian matrix into one matrix""" - rows = [] - for i in range(len(xs)): - rows.append(np.concatenate(list(xs[i]), -1)) - return np.concatenate(rows, 1) if is_batched else np.concatenate(rows, 0) - - -def _compute_numerical_batch_jacobian( - func, xs, delta, np_dtype, merge_batch=True -): - no_batch_jacobian = _compute_numerical_jacobian(func, xs, delta, np_dtype) - xs = list(as_tensors(xs)) - ys = list(as_tensors(func(*xs))) - fin_size = len(xs) - fout_size = len(ys) - bs = xs[0].shape[0] - bat_jac = [] - for i in range(fout_size): - batch_jac_i = [] - for j in range(fin_size): - jac = no_batch_jacobian[i][j] - jac_shape = jac.shape - out_size = jac_shape[0] // bs - in_size = jac_shape[1] // bs - jac = np.reshape(jac, (bs, out_size, bs, in_size)) - batch_jac_i_j = np.zeros(shape=(out_size, bs, in_size)) - for p in range(out_size): - for b in range(bs): - for q in range(in_size): - batch_jac_i_j[p][b][q] = jac[b][p][b][q] - if merge_batch: - batch_jac_i_j = np.reshape(batch_jac_i_j, (out_size, -1)) - batch_jac_i.append(batch_jac_i_j) - bat_jac.append(batch_jac_i) - - return bat_jac - - -def _compute_numerical_batch_hessian(func, xs, delta, np_dtype): - xs = list(as_tensors(xs)) - batch_size = xs[0].shape[0] - fin_size = len(xs) - hessian = [] - for b in range(batch_size): - x_l = [] - for j in range(fin_size): - x_l.append(paddle.reshape(xs[j][b], shape=[1, -1])) - hes_b = _compute_numerical_hessian(func, x_l, delta, np_dtype) - if fin_size == 1: - hessian.append(hes_b[0][0]) - else: - hessian.append(hes_b) - - hessian_res = [] - for index in range(fin_size): - x_reshape = paddle.reshape(xs[index], shape=[batch_size, -1]) - for index_ in range(fin_size): - for i in range(x_reshape.shape[1]): - tmp = [] - for j in range(batch_size): - if fin_size == 1: - tmp.extend(hessian[j][i]) - else: - tmp.extend(hessian[j][i][index_][index]) - hessian_res.append(tmp) - if fin_size == 1: - return hessian_res - - hessian_result = [] - mid = len(hessian_res) // 2 - for i in range(mid): - hessian_result.append( - np.stack((hessian_res[i], hessian_res[mid + i]), axis=0) - ) - return hessian_result - - -def _compute_numerical_vjp(func, xs, v, delta, np_dtype): - xs = as_tensors(xs) - jacobian = np.array(_compute_numerical_jacobian(func, xs, delta, np_dtype)) - if v is None: - v = [paddle.ones_like(x) for x in xs] - flat_v = np.array([v_el.numpy().reshape(-1) for v_el in v]) - vjp = [np.zeros((_product(x.shape)), dtype=np_dtype) for x in xs] - for j in range(len(xs)): - for q in range(_product(xs[j].shape)): - vjp[j][q] = np.sum( - jacobian[:, j, :, q].reshape(flat_v.shape) * flat_v - ) - vjp = [vjp[j].reshape(xs[j].shape) for j in range(len(xs))] - return vjp - - -def _compute_numerical_vhp(func, xs, v, delta, np_dtype): - xs = list(as_tensors(xs)) - hessian = np.array(_compute_numerical_hessian(func, xs, delta, np_dtype)) - flat_v = np.array([v_el.numpy().reshape(-1) for v_el in v]) - vhp = [np.zeros((_product(x.shape)), dtype=np_dtype) for x in xs] - for j in range(len(xs)): - for q in range(_product(xs[j].shape)): - vhp[j][q] = np.sum( - hessian[:, j, :, q].reshape(flat_v.shape) * flat_v - ) - vhp = [vhp[j].reshape(xs[j].shape) for j in range(len(xs))] - return vhp - - -########################################################## -# TestCases of different function. -########################################################## -def reduce(x): - return paddle.sum(x) - - -def reduce_dim(x): - return paddle.sum(x, axis=0) - - -def matmul(x, y): - return paddle.matmul(x, y) - - -def mul(x, y): - return x * y - - -def pow(x, y): - return paddle.pow(x, y) - - -def o2(x, y): - return paddle.multiply(x, y), paddle.matmul(x, y.t()) - - -def unuse(x, y): - return paddle.sum(x) - - -def nested(x): - def inner(y): - return x * y - - return inner - - -def square(x): - return x * x - - -########################################################## -# Parameterized Test Utils. -########################################################## - -TEST_CASE_NAME = 'suffix' - - -def place(devices, key='place'): - """A Decorator for a class which will make the class running on different - devices . - - Args: - devices (Sequence[Paddle.CUDAPlace|Paddle.CPUPlace]): Device list. - key (str, optional): Defaults to '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(fields, values=None): - """Decorator for a unittest class which make the class running on different - test cases. - - Args: - fields (Sequence): The field name sequence of test cases. - values (Sequence, optional): The test cases sequence. Defaults to None. - - """ - 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 i, values in enumerate(params): - test_cls = dict(cls.__dict__) - values = { - k: staticmethod(v) if callable(v) else v - for k, v in values.items() - } - test_cls.update(values) - name = cls.__name__ + str(i) - name = ( - name + '.' + values.get('suffix') - if values.get('suffix') - else name - ) - - test_cls_module[name] = type(name, (cls,), test_cls) - - for m in list(cls.__dict__): - if m.startswith("test"): - delattr(cls, m) - return cls - - return decorate - - -########################################################## -# Utils for transpose different Jacobian/Hessian matrix format. -########################################################## - -# B is batch size, N is row size, M is column size. -MatrixFormat = enum.Enum('MatrixFormat', ('NBM', 'BNM', 'NMB', 'NM')) - - -def _np_transpose_matrix_format(src, src_format, des_format): - """Transpose Jacobian/Hessian matrix format.""" - supported_format = (MatrixFormat.NBM, MatrixFormat.BNM, MatrixFormat.NMB) - if src_format not in supported_format or des_format not in supported_format: - raise ValueError( - f"Supported Jacobian format is {supported_format}, but got src: {src_format}, des: {des_format}" - ) - - src_axis = {c: i for i, c in enumerate(src_format.name)} - dst_axis = tuple(src_axis[c] for c in des_format.name) - - return np.transpose(src, dst_axis) - - -def _np_concat_matrix_sequence(src, src_format=MatrixFormat.NM): - """Convert a sequence of sequence of Jacobian/Hessian matrix into one huge - matrix.""" - - def concat_col(xs): - if src_format in (MatrixFormat.NBM, MatrixFormat.BNM, MatrixFormat.NM): - return np.concatenate(xs, axis=-1) - else: - return np.concatenate(xs, axis=1) - - def concat_row(xs): - if src_format in (MatrixFormat.NBM, MatrixFormat.NM, MatrixFormat.NMB): - return np.concatenate(xs, axis=0) - else: - return np.concatenate(xs, axis=1) - - supported_format = ( - MatrixFormat.NBM, - MatrixFormat.BNM, - MatrixFormat.NMB, - MatrixFormat.NM, - ) - if src_format not in supported_format: - raise ValueError( - f"Supported Jacobian format is {supported_format}, but got {src_format}" - ) - if not isinstance(src, typing.Sequence): - return src - if not isinstance(src[0], typing.Sequence): - src = [src] - - return concat_row(tuple(concat_col(xs) for xs in src)) - - -########################################################## -# Utils for generating test data. -########################################################## -def gen_static_data_and_feed(xs, v, stop_gradient=True): - feed = {} - if isinstance(xs, typing.Sequence): - static_xs = [] - for i, x in enumerate(xs): - x = paddle.static.data(f"x{i}", x.shape, x.dtype) - x.stop_gradient = stop_gradient - static_xs.append(x) - feed.update({f'x{idx}': value for idx, value in enumerate(xs)}) - else: - static_xs = paddle.static.data('x', xs.shape, xs.dtype) - static_xs.stop_gradient = stop_gradient - feed.update({'x': xs}) - - if isinstance(v, typing.Sequence): - static_v = [] - for i, e in enumerate(v): - e = paddle.static.data(f'v{i}', e.shape, e.dtype) - e.stop_gradient = stop_gradient - static_v.append(e) - feed.update({f'v{i}': value for i, value in enumerate(v)}) - elif v is not None: - static_v = paddle.static.data('v', v.shape, v.dtype) - static_v.stop_gradient = stop_gradient - feed.update({'v': v}) - else: - static_v = v - - return feed, static_xs, static_v - - -def gen_static_inputs_and_feed(xs, stop_gradient=True): - feed = {} - if isinstance(xs, typing.Sequence): - static_xs = [] - for i, x in enumerate(xs): - x = paddle.static.data(f"x{i}", x.shape, x.dtype) - x.stop_gradient = stop_gradient - static_xs.append(x) - feed.update({f'x{idx}': value for idx, value in enumerate(xs)}) - else: - static_xs = paddle.static.data('x', xs.shape, xs.dtype) - static_xs.stop_gradient = stop_gradient - feed.update({'x': xs}) - return feed, static_xs From 34bc91ee3eb22093336816f6471efe1468cf988a Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 06:26:31 +0000 Subject: [PATCH 2/3] refine --- test/autograd/test_autograd_functional_static.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/autograd/test_autograd_functional_static.py b/test/autograd/test_autograd_functional_static.py index 127cb93a4cbc69..c8089ce437d0d5 100644 --- a/test/autograd/test_autograd_functional_static.py +++ b/test/autograd/test_autograd_functional_static.py @@ -353,6 +353,9 @@ def run_test_by_entries(self, pd_f, np_f, inps, batch=False): np.testing.assert_allclose(pd_entry, np_entry, self.rtol, self.atol) def test_square(self): + if paddle.framework.use_pir_api(): + return + def pd_f(x): return paddle.multiply(x, x) From f2e1fc14696c82fd479fb90e40b2016f8abd25ca Mon Sep 17 00:00:00 2001 From: Wang Huan Date: Wed, 5 Jun 2024 06:50:06 +0000 Subject: [PATCH 3/3] refine --- test/autograd/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/test/autograd/CMakeLists.txt b/test/autograd/CMakeLists.txt index 14336674c2ce0e..82dc290ddbe3fe 100644 --- a/test/autograd/CMakeLists.txt +++ b/test/autograd/CMakeLists.txt @@ -20,3 +20,4 @@ set_tests_properties(test_minimize PROPERTIES TIMEOUT 60) if(NOT WIN32) set_tests_properties(test_autograd_functional_prim PROPERTIES TIMEOUT 60) endif() +set_tests_properties(test_autograd_functional_static PROPERTIES TIMEOUT 160)