Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cleanup][B-12]Replace to_variable #61533

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix to_variable
  • Loading branch information
co63oc committed Feb 3, 2024
commit 78b034cc0cf77b4fb758abda8d71058e19f1d2f1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SimpleFCLayer(paddle.nn.Layer):
def __init__(self, feature_size, batch_size, fc_size):
super().__init__()
self._linear = paddle.nn.Linear(feature_size, fc_size)
self._offset = base.dygraph.to_variable(
self._offset = paddle.to_tensor(
np.random.random((batch_size, fc_size)).astype('float32')
)

Expand Down Expand Up @@ -55,7 +55,7 @@ def test_main(self):
}

for _ in range(10):
in_x = base.dygraph.to_variable(
in_x = paddle.to_tensor(
np.random.random((batch_size, feature_size)).astype(
'float32'
)
Expand Down
22 changes: 11 additions & 11 deletions test/legacy_test/test_imperative_transformer_sorted_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import paddle.nn.functional as F
from paddle import base
from paddle.base import core
from paddle.base.dygraph import guard, to_variable
from paddle.base.dygraph import guard
from paddle.nn import Layer, Linear

np.set_printoptions(suppress=True)
Expand Down Expand Up @@ -178,18 +178,18 @@ def create_data(is_static=False):
]
else:
enc_inputs = [
to_variable(src_word_np, name='src_word'),
to_variable(src_pos_np, name='src_pos'),
to_variable(src_slf_attn_bias_np, name='src_slf_attn_bias'),
paddle.to_tensor(src_word_np),
paddle.to_tensor(src_pos_np),
paddle.to_tensor(src_slf_attn_bias_np),
]
dec_inputs = [
to_variable(trg_word_np, name='trg_word'),
to_variable(trg_pos_np, name='trg_pos'),
to_variable(trg_slf_attn_bias_np, name='trg_slf_attn_bias'),
to_variable(trg_src_attn_bias_np, name='trg_src_attn_bias'),
paddle.to_tensor(trg_word_np),
paddle.to_tensor(trg_pos_np),
paddle.to_tensor(trg_slf_attn_bias_np),
paddle.to_tensor(trg_src_attn_bias_np),
]
label = to_variable(lbl_word_np, name='lbl_word')
weight = to_variable(lbl_weight_np, name='lbl_weight')
label = paddle.to_tensor(lbl_word_np)
weight = paddle.to_tensor(lbl_weight_np)
return enc_inputs, dec_inputs, label, weight


Expand Down Expand Up @@ -680,7 +680,7 @@ def __init__(
)

# use in dygraph_mode to fit different length batch
# self._pos_emb._w = to_variable(
# self._pos_emb._w = paddle.to_tensor(
# position_encoding_init(self._src_max_len, self._src_emb_dim))

def forward(self, src_word, src_pos):
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_imperative_triple_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __impl__(*args, **kwargs):
def random_var(size, low=-1, high=1, dtype='float32'):
np.random.seed(2021)
x_np = np.random.uniform(low=low, high=high, size=size).astype(dtype)
return base.dygraph.to_variable(x_np)
return paddle.to_tensor(x_np)


class TestDygraphTripleGradMatmul(TestCase):
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_imperative_using_non_zero_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import paddle
from paddle import base
from paddle.base.dygraph import guard, to_variable
from paddle.base.dygraph import guard


class TestImperativeUsingNonZeroGpu(unittest.TestCase):
def run_main(self, np_arr, place):
with guard(place):
var = to_variable(np_arr)
var = paddle.to_tensor(np_arr)
np.testing.assert_array_equal(np_arr, var.numpy())

def test_non_zero_gpu(self):
Expand Down
8 changes: 4 additions & 4 deletions test/legacy_test/test_index_select_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ def test_dygraph_api(self):
self.input_data()
# case 1:
with base.dygraph.guard():
x = base.dygraph.to_variable(self.data_x)
index = base.dygraph.to_variable(self.data_index)
x = paddle.to_tensor(self.data_x)
index = paddle.to_tensor(self.data_index)
z = paddle.index_select(x, index)
np_z = z.numpy()
expect_out = np.array(
Expand All @@ -248,8 +248,8 @@ def test_dygraph_api(self):

# case 2:
with base.dygraph.guard():
x = base.dygraph.to_variable(self.data_x)
index = base.dygraph.to_variable(self.data_index)
x = paddle.to_tensor(self.data_x)
index = paddle.to_tensor(self.data_index)
z = paddle.index_select(x, index, axis=1)
np_z = z.numpy()
expect_out = np.array(
Expand Down
5 changes: 2 additions & 3 deletions test/legacy_test/test_instance_norm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import paddle
from paddle import base, nn
from paddle.base import Program, core, program_guard
from paddle.base.dygraph import to_variable


def _reference_instance_norm_naive(x, scale, bias, epsilon, mean, var):
Expand Down Expand Up @@ -929,7 +928,7 @@ def test_norm(self):
instance_norm = paddle.nn.InstanceNorm2D(
5, weight_attr=False, bias_attr=False
)
outputs = instance_norm(to_variable(inputs))
outputs = instance_norm(paddle.to_tensor(inputs))
np.testing.assert_allclose(
outputs.numpy(), out_np, rtol=1e-05, atol=1e-06
)
Expand Down Expand Up @@ -963,7 +962,7 @@ def test_norm(self):
instance_norm = paddle.nn.InstanceNorm2D(
3, weight_attr=True, bias_attr=True
)
outputs = instance_norm(to_variable(inputs))
outputs = instance_norm(paddle.to_tensor(inputs))
np.testing.assert_allclose(
outputs.numpy(), out_np, rtol=1e-05, atol=1e-06
)
Expand Down
10 changes: 5 additions & 5 deletions test/legacy_test/test_instance_norm_op_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ def test_error(self):
def error1d():
x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32')
instance_norm1d = paddle.nn.InstanceNorm1D(1)
instance_norm1d(base.dygraph.to_variable(x_data_4))
instance_norm1d(paddle.to_tensor(x_data_4))

def error2d():
x_data_3 = np.random.random(size=(2, 1, 3)).astype('float32')
instance_norm2d = paddle.nn.InstanceNorm2D(1)
instance_norm2d(base.dygraph.to_variable(x_data_3))
instance_norm2d(paddle.to_tensor(x_data_3))

def error3d():
x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32')
instance_norm3d = paddle.nn.InstanceNorm3D(1)
instance_norm3d(base.dygraph.to_variable(x_data_4))
instance_norm3d(paddle.to_tensor(x_data_4))

def weight_bias_false():
x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32')
Expand All @@ -127,13 +127,13 @@ def test_dygraph(self):
def compute_v1(x):
with base.dygraph.guard(p):
bn = paddle.nn.InstanceNorm2D(shape[1])
y = bn(base.dygraph.to_variable(x))
y = bn(paddle.to_tensor(x))
return y.numpy()

def compute_v2(x):
with base.dygraph.guard(p):
bn = paddle.nn.InstanceNorm2D(shape[1])
y = bn(base.dygraph.to_variable(x))
y = bn(paddle.to_tensor(x))
return y.numpy()

x = np.random.randn(*shape).astype("float32")
Expand Down
4 changes: 2 additions & 2 deletions test/legacy_test/test_inverse_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def test_dygraph(self):
for place in self.places:
with base.dygraph.guard(place):
input_np = np.random.random([4, 4]).astype("float64")
input = base.dygraph.to_variable(input_np)
input = paddle.to_tensor(input_np)
result = paddle.inverse(input)
np.testing.assert_allclose(
result.numpy(), np.linalg.inv(input_np), rtol=1e-05
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_dygraph(self):
for place in self.places:
with base.dygraph.guard(place):
input_np = np.ones([4, 4]).astype("float64")
input = base.dygraph.to_variable(input_np)
input = paddle.to_tensor(input_np)
try:
result = paddle.inverse(input)
except RuntimeError as ex:
Expand Down
8 changes: 4 additions & 4 deletions test/xpu/test_index_select_op_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def test_dygraph_api(self):
self.input_data()
# case 1:
with base.dygraph.guard():
x = base.dygraph.to_variable(self.data_x)
index = base.dygraph.to_variable(self.data_index)
x = paddle.to_tensor(self.data_x)
index = paddle.to_tensor(self.data_index)
z = paddle.index_select(x, index)
np_z = z.numpy()
expect_out = np.array(
Expand All @@ -143,8 +143,8 @@ def test_dygraph_api(self):

# case 2:
with base.dygraph.guard():
x = base.dygraph.to_variable(self.data_x)
index = base.dygraph.to_variable(self.data_index)
x = paddle.to_tensor(self.data_x)
index = paddle.to_tensor(self.data_index)
z = paddle.index_select(x, index, axis=1)
np_z = z.numpy()
expect_out = np.array(
Expand Down
2 changes: 1 addition & 1 deletion test/xpu/test_inverse_op_xpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def test_dygraph(self):
for place in self.places:
with base.dygraph.guard(place):
input_np = np.ones([4, 4]).astype("float32")
input = base.dygraph.to_variable(input_np)
input = paddle.to_tensor(input_np)
with self.assertRaises(OSError):
result = paddle.inverse(input)

Expand Down