Skip to content

Commit

Permalink
[Cleanup][B-5] clean some to_variable for test (#61530)
Browse files Browse the repository at this point in the history
  • Loading branch information
NKNaN authored Feb 3, 2024
1 parent dd57af0 commit 3ce8fc6
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 28 deletions.
5 changes: 2 additions & 3 deletions test/contrib/test_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import paddle
from paddle import base
from paddle.base.dygraph.base import to_variable

paddle.enable_static()

Expand Down Expand Up @@ -176,8 +175,8 @@ def test_check_output(self):
stride2=1,
)

x1 = to_variable(x1_np)
x2 = to_variable(x2_np)
x1 = paddle.to_tensor(x1_np)
x2 = paddle.to_tensor(x2_np)
corr_pd = Net('corr_pd')
y = corr_pd(x1, x2)
out = y.numpy()
Expand Down
9 changes: 4 additions & 5 deletions test/dygraph_to_static/test_cycle_gan.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
)

import paddle
from paddle.base.dygraph import to_variable
from paddle.nn import BatchNorm

# Note: Set True to eliminate randomness.
Expand Down Expand Up @@ -584,8 +583,8 @@ def train(args):
data_B = np.array(
[data_B[0].reshape(3, IMAGE_SIZE, IMAGE_SIZE)]
).astype("float32")
data_A = to_variable(data_A)
data_B = to_variable(data_B)
data_A = paddle.to_tensor(data_A)
data_B = paddle.to_tensor(data_B)

# optimize the g_A network
(
Expand All @@ -610,13 +609,13 @@ def train(args):
fake_pool_B = np.array(
[fake_pool_B[0].reshape(3, IMAGE_SIZE, IMAGE_SIZE)]
).astype("float32")
fake_pool_B = to_variable(fake_pool_B)
fake_pool_B = paddle.to_tensor(fake_pool_B)

fake_pool_A = A_pool.pool_image(fake_A).numpy()
fake_pool_A = np.array(
[fake_pool_A[0].reshape(3, IMAGE_SIZE, IMAGE_SIZE)]
).astype("float32")
fake_pool_A = to_variable(fake_pool_A)
fake_pool_A = paddle.to_tensor(fake_pool_A)

# optimize the d_A network
discriminatorA_to_static = paddle.jit.to_static(
Expand Down
4 changes: 2 additions & 2 deletions test/dygraph_to_static/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, hidden_size=16, output_size=16):
)

def forward(self, input, cache=None):
input = base.dygraph.to_variable(input)
input = paddle.to_tensor(input)

q = self.q_fc(input)
k = self.k_fc(input)
Expand Down Expand Up @@ -83,7 +83,7 @@ def __init__(self, batch_size=64, hidden_size=16, output_size=16):
self.sub_net = SubNetWithDict(hidden_size, output_size)

def forward(self, input, max_len=4):
input = base.dygraph.to_variable(input)
input = paddle.to_tensor(input)
cache = {
"k": paddle.tensor.fill_constant(
shape=[self.batch_size, self.output_size],
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_conv2d_transpose_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def functional(self, place):
return y_np

def paddle_nn_layer(self):
x_var = dg.to_variable(self.input)
x_var = paddle.to_tensor(self.input)

if self.output_padding != 0:
output_size = None
Expand Down
2 changes: 1 addition & 1 deletion test/legacy_test/test_conv3d_transpose_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def functional(self, place):
return y_np

def paddle_nn_layer(self):
x_var = dg.to_variable(self.input)
x_var = paddle.to_tensor(self.input)
conv = nn.Conv3DTranspose(
self.num_channels,
self.num_filters,
Expand Down
8 changes: 4 additions & 4 deletions test/legacy_test/test_cross_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def test_dygraph_api(self):
self.input_data()
# case 1:
# with base.dygraph.guard():
# x = base.dygraph.to_variable(self.data_x)
# y = base.dygraph.to_variable(self.data_y)
# x = paddle.to_tensor(self.data_x)
# y = paddle.to_tensor(self.data_y)
# z = paddle.cross(x, y)
# np_z = z.numpy()
# expect_out = np.array([[-1.0, -1.0, -1.0], [2.0, 2.0, 2.0],
Expand All @@ -207,8 +207,8 @@ def test_dygraph_api(self):

# case 2:
with base.dygraph.guard():
x = base.dygraph.to_variable(self.data_x)
y = base.dygraph.to_variable(self.data_y)
x = paddle.to_tensor(self.data_x)
y = paddle.to_tensor(self.data_y)
z = paddle.cross(x, y, axis=1)
np_z = z.numpy()
expect_out = np.array(
Expand Down
7 changes: 3 additions & 4 deletions test/legacy_test/test_detach.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import paddle
from paddle import base
from paddle.base.dygraph.base import to_variable
from paddle.nn import Linear


Expand Down Expand Up @@ -68,7 +67,7 @@ def no_detach_multi(self):
weight_attr=linear2_w_param_attrs,
bias_attr=linear2_b_param_attrs,
)
data = to_variable(data)
data = paddle.to_tensor(data)
x = linear(data)
x1 = linear1(x)
x2 = linear2(x)
Expand Down Expand Up @@ -104,7 +103,7 @@ def no_detach_single(self):
weight_attr=linear1_w_param_attrs,
bias_attr=linear1_b_param_attrs,
)
data = to_variable(data)
data = paddle.to_tensor(data)
x = linear(data)
x.retain_grads()
x1 = linear1(x)
Expand Down Expand Up @@ -152,7 +151,7 @@ def detach_multi(self):
weight_attr=linear2_w_param_attrs,
bias_attr=linear2_b_param_attrs,
)
data = to_variable(data)
data = paddle.to_tensor(data)
x = linear(data)
x.retain_grads()
x_detach = x.detach()
Expand Down
15 changes: 7 additions & 8 deletions test/legacy_test/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import paddle
from paddle import base
from paddle.base import core
from paddle.base.dygraph import base as imperative_base
from paddle.base.framework import Program, program_guard
from paddle.pir_utils import test_with_pir_api

Expand Down Expand Up @@ -138,11 +137,11 @@ def test_generate_proposals(self):
)

with self.dynamic_graph():
scores_dy = imperative_base.to_variable(scores_np)
bbox_deltas_dy = imperative_base.to_variable(bbox_deltas_np)
im_info_dy = imperative_base.to_variable(im_info_np)
anchors_dy = imperative_base.to_variable(anchors_np)
variances_dy = imperative_base.to_variable(variances_np)
scores_dy = paddle.to_tensor(scores_np)
bbox_deltas_dy = paddle.to_tensor(bbox_deltas_np)
im_info_dy = paddle.to_tensor(im_info_np)
anchors_dy = paddle.to_tensor(anchors_np)
variances_dy = paddle.to_tensor(variances_np)
rois, roi_probs, rois_num = paddle.vision.ops.generate_proposals(
scores_dy,
bbox_deltas_dy,
Expand Down Expand Up @@ -219,8 +218,8 @@ def static_distribute_fpn_proposals(self, rois_np, rois_num_np):

def dynamic_distribute_fpn_proposals(self, rois_np, rois_num_np):
with self.dynamic_graph():
rois_dy = imperative_base.to_variable(rois_np)
rois_num_dy = imperative_base.to_variable(rois_num_np)
rois_dy = paddle.to_tensor(rois_np)
rois_num_dy = paddle.to_tensor(rois_num_np)
(
multi_rois_dy,
restore_ind_dy,
Expand Down

0 comments on commit 3ce8fc6

Please sign in to comment.