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-20] Replace to_variable with to_tensor #61546

Merged
merged 8 commits into from
Feb 19, 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
7 changes: 3 additions & 4 deletions python/paddle/distributed/fleet/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import paddle
from paddle import _C_ops, _legacy_C_ops
from paddle.base.dygraph import to_variable
from paddle.distributed import fleet
from paddle.framework import core

Expand Down Expand Up @@ -102,9 +101,9 @@ def unscale_method(self, optimizer):
else:
param_grads_fp32.append(tgt_grad)

temp_found_inf_fp16 = to_variable(np.array([0]).astype(np.bool_))
temp_found_inf_bf16 = to_variable(np.array([0]).astype(np.bool_))
temp_found_inf_fp32 = to_variable(np.array([0]).astype(np.bool_))
temp_found_inf_fp16 = paddle.to_tensor(np.array([0]).astype(np.bool_))
temp_found_inf_bf16 = paddle.to_tensor(np.array([0]).astype(np.bool_))
temp_found_inf_fp32 = paddle.to_tensor(np.array([0]).astype(np.bool_))
self._found_inf = self._temp_found_inf_value_false
if len(param_grads_fp16):
_legacy_C_ops.check_finite_and_unscale(
Expand Down
15 changes: 8 additions & 7 deletions python/paddle/hapi/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from paddle import base
from paddle.autograd import no_grad
from paddle.base import core
from paddle.base.dygraph.base import to_variable
from paddle.base.executor import global_scope
from paddle.base.framework import (
Variable,
Expand Down Expand Up @@ -824,7 +823,7 @@ def train_batch(self, inputs, labels=None, update=True):
inputs = to_list(inputs)
self._input_info = _update_input_info(inputs)
labels = labels or []
labels = [to_variable(l) for l in to_list(labels)]
labels = [paddle.to_tensor(l) for l in to_list(labels)]

# scaler should be initialized only once
if self._amp_level != "O0" and self.model._scaler is None:
Expand All @@ -836,9 +835,11 @@ def train_batch(self, inputs, labels=None, update=True):
level=self._amp_level,
):
if self._nranks > 1:
outputs = self.ddp_model(*[to_variable(x) for x in inputs])
outputs = self.ddp_model(*[paddle.to_tensor(x) for x in inputs])
else:
outputs = self.model.network(*[to_variable(x) for x in inputs])
outputs = self.model.network(
*[paddle.to_tensor(x) for x in inputs]
)

losses = self.model._loss(*(to_list(outputs) + labels))
losses = to_list(losses)
Expand Down Expand Up @@ -874,9 +875,9 @@ def eval_batch(self, inputs, labels=None):
inputs = to_list(inputs)
self._input_info = _update_input_info(inputs)
labels = labels or []
labels = [to_variable(l) for l in to_list(labels)]
labels = [paddle.to_tensor(l) for l in to_list(labels)]

outputs = self.model.network(*[to_variable(x) for x in inputs])
outputs = self.model.network(*[paddle.to_tensor(x) for x in inputs])

# Transfrom data to expected device
expected_device = paddle.device.get_device()
Expand Down Expand Up @@ -933,7 +934,7 @@ def eval_batch(self, inputs, labels=None):
def predict_batch(self, inputs):
self.model.network.eval()
self.mode = 'test'
inputs = [to_variable(x) for x in to_list(inputs)]
inputs = [paddle.to_tensor(x) for x in to_list(inputs)]
self._input_info = _update_input_info(inputs)
outputs = self.model.network(*inputs)
if self._nranks > 1 and isinstance(self.model._place, base.CUDAPlace):
Expand Down
7 changes: 2 additions & 5 deletions python/paddle/nn/functional/loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3207,10 +3207,7 @@ def sigmoid_focal_loss(
),
)

if in_dynamic_mode():
alpha = base.dygraph.base.to_variable([alpha], dtype=loss.dtype)
else:
alpha = paddle.to_tensor(alpha, dtype=loss.dtype)
alpha = paddle.to_tensor(alpha, dtype=loss.dtype)
alpha_t = _C_ops.add(
_C_ops.multiply(alpha, label),
_C_ops.multiply(
Expand All @@ -3220,7 +3217,7 @@ def sigmoid_focal_loss(
loss = _C_ops.multiply(alpha_t, loss)

if in_dynamic_mode():
gamma = base.dygraph.base.to_variable([gamma], dtype=loss.dtype)
gamma = paddle.to_tensor(gamma, dtype=loss.dtype)
gamma_t = _C_ops.pow(_C_ops.subtract(one, p_t), gamma)
loss = _C_ops.multiply(gamma_t, loss)

Expand Down
12 changes: 4 additions & 8 deletions python/paddle/nn/layer/norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,17 +935,13 @@ class BatchNorm(Layer):
Examples:
.. code-block:: python

>>> import paddle.base as base
>>> import paddle.nn as nn
>>> from paddle.base.dygraph.base import to_variable
>>> import paddle
>>> import numpy as np


>>> x = np.random.random(size=(3, 10, 3, 7)).astype('float32')
>>> with base.dygraph.guard():
... x = to_variable(x)
... batch_norm = nn.layer.norm.BatchNorm(10)
... hidden1 = batch_norm(x)
>>> x = paddle.rand(shape=(3, 10, 3, 7), dtype="float32")
>>> batch_norm = nn.BatchNorm(10)
>>> hidden1 = batch_norm(x)
"""

def __init__(
Expand Down
3 changes: 1 addition & 2 deletions python/paddle/pir/math_op_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,12 @@ def clear_gradient(self):
.. code-block:: python

>>> import paddle
>>> import paddle.base as base
>>> import numpy as np

>>> x = np.ones([2, 2], np.float32)
>>> inputs2 = []
>>> for _ in range(10):
>>> tmp = base.dygraph.base.to_variable(x)
>>> tmp = paddle.to_tensor(x)
>>> tmp.stop_gradient=False
>>> inputs2.append(tmp)
>>> ret2 = paddle.add_n(inputs2)
Expand Down