未验证 提交 2d91a9bd 编写于 作者: D Difer 提交者: GitHub

repacle embedding in fluid with 2.0 version (#55757)

* replace embedding

* replace sparse_embedding

* fix some bugs

* del embedding

* repalce layers.embedding

* fix type error
上级 274e5e54
......@@ -57,176 +57,10 @@ from collections.abc import Iterable
__all__ = [
'embedding',
'autoincreased_step_counter',
]
@deprecated(since="2.0.0", update_to="paddle.nn.functional.embedding")
def embedding(
input,
size,
is_sparse=False,
is_distributed=False,
padding_idx=None,
param_attr=None,
dtype='float32',
):
r"""
:api_attr: Static Graph
**WARNING:** This OP will be deprecated in a future release. This OP requires the
last dimension of Tensor shape must be equal to 1. It is recommended to use
fluid. :ref:`api_fluid_embedding` .
The operator is used to lookup embeddings vector of ids provided by :attr:`input` .
It automatically constructs a 2D embedding matrix based on the
input :attr:`size` (vocab_size, emb_size) and :attr:`dtype` .
This OP requires the last dimension of Tensor shape must be equal to 1. The shape
of output Tensor is generated by replacing the last dimension of the input Tensor shape
with emb_size.
**Note:** The id in :attr:`input` must satisfy :math:`0 =< id < size[0]` ,
otherwise the program will throw an exception and exit.
.. code-block:: text
Case 1:
input is a Tensor. padding_idx = -1
input.data = [[[1], [3]], [[2], [4]], [[4], [127]]]
input.shape = [3, 2, 1]
Given size = [128, 16]
output is a Tensor:
out.shape = [3, 2, 16]
out.data = [[[0.129435295, 0.244512452, ..., 0.436322452],
[0.345421456, 0.524563927, ..., 0.144534654]],
[[0.345249859, 0.124939536, ..., 0.194353745],
[0.945345345, 0.435394634, ..., 0.435345365]],
[[0.945345345, 0.435394634, ..., 0.435345365],
[0.0, 0.0, ..., 0.0 ]]] # padding data
The input padding_idx is less than 0, it is automatically converted to padding_idx = -1 + 128 = 127
It will pad all-zero data when ids is 127.
Case 2:
input is a LoDTensor with 1-level LoD. padding_idx = 0
input.lod = [[2, 3]]
input.data = [[1], [3], [2], [4], [0]]
input.shape = [5, 1]
Given size = [128, 16]
output is a LoDTensor:
out.lod = [[2, 3]]
out.shape = [5, 16]
out.data = [[0.129435295, 0.244512452, ..., 0.436322452],
[0.345421456, 0.524563927, ..., 0.144534654],
[0.345249859, 0.124939536, ..., 0.194353745],
[0.945345345, 0.435394634, ..., 0.435345365],
[0.0, 0.0, ..., 0.0 ]] # padding data
It will pad all-zero data when ids is 0.
Args:
input(Variable): A Tensor or LoDTensor with type int64, which contains the id information.
The last dimension of Tensor shape must be equal to 1. The value of the input id should
satisfy :math:`0<= id < size[0]` .
size(tuple|list): The shape of lookup table parameter. It should have two elements which
indicates the size of the dictionary of embeddings and the size of each embedding vector respectively.
is_sparse(bool): The flag indicating whether to use sparse update. This parameter only
affects the performance of the backwards gradient update. It is recommended to set
True because sparse update is faster. But some optimizer does not support sparse update,
such as :ref:`api_fluid_optimizer_AdadeltaOptimizer` , :ref:`api_fluid_optimizer_AdamaxOptimizer` ,
:ref:`api_fluid_optimizer_DecayedAdagradOptimizer` , :ref:`api_fluid_optimizer_FtrlOptimizer` ,
:ref:`api_fluid_optimizer_LambOptimizer` and :ref:`api_fluid_optimizer_LarsMomentumOptimizer` .
In these case, is_sparse must be False. Default: False.
is_distributed(bool): Whether to store the embedding matrix in a distributed manner. Only used
in multi-machine distributed CPU training. Default: False.
padding_idx(int|long|None): padding_idx needs to be in the interval [-vocab_size, vocab_size).
If :math:`padding\_idx < 0`, the :math:`padding\_idx` will automatically be converted
to :math:`vocab\_size + padding\_idx` . It will output all-zero padding data whenever lookup
encounters :math:`padding\_idx` in id. And the padding data will not be updated while training.
If set None, it makes no effect to output. Default: None.
param_attr(ParamAttr): To specify the weight parameter property. Default: None, which means the
default weight parameter property is used. See usage for details in :ref:`api_fluid_ParamAttr` . In addition,
user-defined or pre-trained word vectors can be loaded with the :attr:`param_attr` parameter.
The local word vector needs to be transformed into numpy format, and the shape of local word
vector should be consistent with :attr:`size` . Then :ref:`api_fluid_initializer_NumpyArrayInitializer`
is used to load custom or pre-trained word vectors. See code example 2 for details.
dtype(str|core.VarDesc.VarType): It refers to the data type of output Tensor.
It must be float32 or float64. Default: float32.
Returns:
Variable: Embedding Tensor or LoDTensor mapped by input. The data type is the same as :attr:`dtype` .
Examples:
.. code-block:: python
import paddle.fluid as fluid
import numpy as np
import paddle
paddle.enable_static()
data = paddle.static.data(name='x', shape=[None, 1], dtype='int64')
# example 1
emb_1 = paddle.static.nn.embedding(input=data, size=[128, 64])
# example 2: load custom or pre-trained word vectors
weight_data = np.random.random(size=(128, 100)) # word vectors with numpy format
w_param_attrs = fluid.ParamAttr(
name="emb_weight",
learning_rate=0.5,
initializer=paddle.nn.initializer.Assign(weight_data),
trainable=True)
emb_2 = fluid.layers.embedding(input=data, size=(128, 100), param_attr=w_param_attrs, dtype='float32')
"""
helper = LayerHelper('embedding', **locals())
check_variable_and_dtype(
input, 'input', ['int64'], 'fluid.layers.embedding'
)
check_dtype(
dtype,
'dtype',
['uint16', 'float16', 'float32', 'float64'],
'fluid.layers.embedding',
)
if is_distributed:
is_distributed = False
warnings.warn(
"is_distributed is go out of use, `paddle.static.nn.sparse_embedding` is your needed"
)
remote_prefetch = True if is_sparse else False
w = helper.create_parameter(
attr=helper.param_attr, shape=size, dtype=dtype, is_bias=False
)
tmp = helper.create_variable_for_type_inference(dtype)
padding_idx = (
-1
if padding_idx is None
else padding_idx
if padding_idx >= 0
else (size[0] + padding_idx)
)
helper.append_op(
type='lookup_table',
inputs={'Ids': input, 'W': w},
outputs={'Out': tmp},
attrs={
'is_sparse': is_sparse,
'is_distributed': is_distributed,
'remote_prefetch': remote_prefetch,
'padding_idx': padding_idx,
},
)
return tmp
def autoincreased_step_counter(counter_name=None, begin=1, step=1):
"""
:api_attr: Static Graph
......
......@@ -157,7 +157,7 @@ class FleetTranspiler(Fleet):
if len(dist_varnames) != 0:
raise ValueError(
"GeoStrategy can not support large scale embeding now, please use fluid.layers.embedding"
"GeoStrategy can not support large scale embeding now, please use paddle.static.nn.embedding"
)
init_attrs = []
......
......@@ -1124,7 +1124,7 @@ class fleet_embedding:
Example:
.. code-block:: python
with fleet_embedding(click_name=label.name):
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=var,
size=[-1, 11],
is_sparse=True,
......@@ -1134,7 +1134,6 @@ class fleet_embedding:
def __init__(self, click_name, scale_sparse_grad=True):
"""Init."""
# self.origin_emb = fluid.layers.embedding
self.origin_emb_v2 = paddle.static.nn.embedding
# if user uses cvm layer after embedding, click_name can be None
self.click_name = "" if click_name is None else click_name
......@@ -1144,7 +1143,6 @@ class fleet_embedding:
def __enter__(self):
"""Enter."""
# fluid.layers.embedding = _fleet_embedding
paddle.static.nn.embedding = _fleet_embedding_v2
FLEET_GLOBAL_DICT["cur_accessor"] = self.accessor
FLEET_GLOBAL_DICT["click_name"] = self.click_name
......@@ -1152,7 +1150,6 @@ class fleet_embedding:
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit."""
# fluid.layers.embedding = self.origin_emb
paddle.static.nn.embedding = self.origin_emb_v2
FLEET_GLOBAL_DICT["cur_accessor"] = ""
FLEET_GLOBAL_DICT["click_name"] = ""
......
......@@ -31,13 +31,26 @@ def make_program_lookup_table_v1_mp_dp():
name='src_ids', shape=[12, 512, 1], dtype='int64'
)
src_ids.stop_gradient = True
emb_out = paddle.fluid.layers.embedding(
input=src_ids,
size=[64, 128],
param_attr=paddle.fluid.ParamAttr(name="emb_weight"),
dtype="float32",
is_sparse=False,
emb_out = block.create_var(name='emb_out', dtype='float32')
w = paddle.create_parameter(
attr=paddle.fluid.ParamAttr(name="emb_weight"),
shape=[64, 128],
dtype='float32',
is_bias=False,
)
block.append_op(
type='lookup_table',
outputs={'Out': emb_out},
inputs={'Ids': src_ids, 'W': w},
attrs={
'is_sparse': False,
'is_distributed': False,
'remote_prefetch': False,
'padding_idx': None,
},
)
loss = paddle.mean(emb_out)
auto.shard_tensor(
......
......@@ -31,7 +31,7 @@ from paddle import fluid
def convolution_net(
data, label, input_dim, class_dim=2, emb_dim=32, hid_dim=32
):
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=data, size=[input_dim, emb_dim], is_sparse=True
)
conv_3 = nets.sequence_conv_pool(
......
......@@ -25,7 +25,7 @@ import nets
import paddle
from paddle import fluid
from paddle.fluid import framework, layers
from paddle.fluid import framework
from paddle.fluid.executor import Executor
from paddle.fluid.optimizer import SGDOptimizer
......@@ -44,7 +44,7 @@ def get_usr_combined_features():
uid = paddle.static.data(name='user_id', shape=[-1, 1], dtype='int64')
usr_emb = layers.embedding(
usr_emb = paddle.static.nn.embedding(
input=uid,
dtype='float32',
size=[USR_DICT_SIZE, 32],
......@@ -60,7 +60,7 @@ def get_usr_combined_features():
name='gender_id', shape=[-1, 1], dtype='int64'
)
usr_gender_emb = layers.embedding(
usr_gender_emb = paddle.static.nn.embedding(
input=usr_gender_id,
size=[USR_GENDER_DICT_SIZE, 16],
param_attr='gender_table',
......@@ -72,7 +72,7 @@ def get_usr_combined_features():
USR_AGE_DICT_SIZE = len(paddle.dataset.movielens.age_table)
usr_age_id = paddle.static.data(name='age_id', shape=[-1, 1], dtype="int64")
usr_age_emb = layers.embedding(
usr_age_emb = paddle.static.nn.embedding(
input=usr_age_id,
size=[USR_AGE_DICT_SIZE, 16],
is_sparse=IS_SPARSE,
......@@ -84,7 +84,7 @@ def get_usr_combined_features():
USR_JOB_DICT_SIZE = paddle.dataset.movielens.max_job_id() + 1
usr_job_id = paddle.static.data(name='job_id', shape=[-1, 1], dtype="int64")
usr_job_emb = layers.embedding(
usr_job_emb = paddle.static.nn.embedding(
input=usr_job_id,
size=[USR_JOB_DICT_SIZE, 16],
param_attr='job_table',
......@@ -109,7 +109,7 @@ def get_mov_combined_features():
mov_id = paddle.static.data(name='movie_id', shape=[-1, 1], dtype='int64')
mov_emb = layers.embedding(
mov_emb = paddle.static.nn.embedding(
input=mov_id,
dtype='float32',
size=[MOV_DICT_SIZE, 32],
......@@ -125,12 +125,12 @@ def get_mov_combined_features():
name='category_id', shape=[-1, 1], dtype='int64', lod_level=1
)
mov_categories_emb = layers.embedding(
mov_categories_emb = paddle.static.nn.embedding(
input=category_id, size=[CATEGORY_DICT_SIZE, 32], is_sparse=IS_SPARSE
)
mov_categories_hidden = paddle.static.nn.sequence_lod.sequence_pool(
input=mov_categories_emb, pool_type="sum"
input=mov_categories_emb.squeeze(-2), pool_type="sum"
)
MOV_TITLE_DICT_SIZE = len(paddle.dataset.movielens.get_movie_title_dict())
......@@ -139,12 +139,12 @@ def get_mov_combined_features():
name='movie_title', shape=[-1, 1], dtype='int64', lod_level=1
)
mov_title_emb = layers.embedding(
mov_title_emb = paddle.static.nn.embedding(
input=mov_title_id, size=[MOV_TITLE_DICT_SIZE, 32], is_sparse=IS_SPARSE
)
mov_title_conv = nets.sequence_conv_pool(
input=mov_title_emb,
input=mov_title_emb.squeeze(-2),
num_filters=32,
filter_size=3,
act="tanh",
......
......@@ -58,28 +58,28 @@ def train(
IS_SPARSE = is_sparse
def __network__(words):
embed_first = fluid.layers.embedding(
embed_first = paddle.static.nn.embedding(
input=words[0],
size=[dict_size, EMBED_SIZE],
dtype='float32',
is_sparse=IS_SPARSE,
param_attr='shared_w',
)
embed_second = fluid.layers.embedding(
embed_second = paddle.static.nn.embedding(
input=words[1],
size=[dict_size, EMBED_SIZE],
dtype='float32',
is_sparse=IS_SPARSE,
param_attr='shared_w',
)
embed_third = fluid.layers.embedding(
embed_third = paddle.static.nn.embedding(
input=words[2],
size=[dict_size, EMBED_SIZE],
dtype='float32',
is_sparse=IS_SPARSE,
param_attr='shared_w',
)
embed_forth = fluid.layers.embedding(
embed_forth = paddle.static.nn.embedding(
input=words[3],
size=[dict_size, EMBED_SIZE],
dtype='float32',
......
......@@ -59,7 +59,7 @@ def TestDistTraining():
with paddle.static.program_guard(main_prog, startup_prog):
x = paddle.static.data(name="x", shape=[3, 2, 1], dtype='int64')
with paddle.static.ipu_shard_guard(index=0, stage=0):
out = paddle.fluid.layers.embedding(x, **attrs)
out = paddle.static.nn.embedding(x, **attrs)
with paddle.static.ipu_shard_guard(index=1, stage=1):
loss = paddle.mean(out)
opt = paddle.optimizer.Adam(learning_rate=1e-1)
......
......@@ -77,7 +77,7 @@ def Test(use_dist, file_name):
with paddle.static.program_guard(main_prog, startup_prog):
x = paddle.static.data(name="x", shape=[3, 2, 1], dtype='int64')
out = paddle.fluid.layers.embedding(x, **attrs)
out = paddle.static.nn.embedding(x, **attrs)
loss = paddle.mean(out)
opt = paddle.optimizer.Adam(learning_rate=1e-1)
opt.minimize(loss)
......
......@@ -53,7 +53,7 @@ class TestBase(IPUOpTest):
x = paddle.static.data(
name=self.feed_list[0], shape=self.feed_shape[0], dtype='int64'
)
out = paddle.fluid.layers.embedding(x, **self.attrs)
out = paddle.static.nn.embedding(x, **self.attrs)
if self.is_training:
loss = paddle.mean(out)
adam = paddle.optimizer.Adam(learning_rate=1e-2)
......
......@@ -55,7 +55,7 @@ class TestWeightSharing(IPUOpTest):
name=self.feed_list[0], shape=self.feed_shape[0], dtype='int64'
)
with paddle.static.ipu_shard_guard(index=0, stage=0):
y = paddle.fluid.layers.embedding(
y = paddle.static.nn.embedding(
input=x,
size=[768, 768],
dtype='float32',
......
......@@ -27,26 +27,26 @@ class EmbEltwiseLayerNormFusePassTest(PassTest):
with fluid.program_guard(self.main_program, self.startup_program):
word_id = paddle.static.data(
name="word_id",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
pos_id = paddle.static.data(
name="pos_id",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
sent_id = paddle.static.data(
name="sent_id",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
word_emb = fluid.layers.embedding(
word_emb = paddle.static.nn.embedding(
input=word_id, size=(128, 768), dtype='float32'
)
pos_emb = fluid.layers.embedding(
pos_emb = paddle.static.nn.embedding(
input=pos_id, size=(128, 768), dtype='float32'
)
sent_emb = fluid.layers.embedding(
sent_emb = paddle.static.nn.embedding(
input=sent_id, size=(128, 768), dtype='float32'
)
add1 = paddle.add(word_emb, pos_emb)
......@@ -55,34 +55,34 @@ class EmbEltwiseLayerNormFusePassTest(PassTest):
id1 = paddle.static.data(
name="id1",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
id2 = paddle.static.data(
name="id2",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
id3 = paddle.static.data(
name="id3",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
id4 = paddle.static.data(
name="id4",
shape=[1, 128, 1],
shape=[1, 128],
dtype="int64",
)
emb1 = fluid.layers.embedding(
emb1 = paddle.static.nn.embedding(
input=id1, size=(128, 768), dtype='float32'
)
emb2 = fluid.layers.embedding(
emb2 = paddle.static.nn.embedding(
input=id2, size=(128, 768), dtype='float32'
)
emb3 = fluid.layers.embedding(
emb3 = paddle.static.nn.embedding(
input=id3, size=(128, 768), dtype='float32'
)
emb4 = fluid.layers.embedding(
emb4 = paddle.static.nn.embedding(
input=id4, size=(128, 768), dtype='float32'
)
add_1 = paddle.add(emb1, emb2)
......@@ -93,25 +93,25 @@ class EmbEltwiseLayerNormFusePassTest(PassTest):
)
self.feeds = {
"word_id": np.random.randint(
low=0, high=128, size=(1, 128, 1)
).astype("int64"),
"pos_id": np.random.randint(
low=0, high=128, size=(1, 128, 1)
).astype("int64"),
"sent_id": np.random.randint(
low=0, high=128, size=(1, 128, 1)
).astype("int64"),
"id1": np.random.randint(low=0, high=128, size=(1, 128, 1)).astype(
"word_id": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
"id2": np.random.randint(low=0, high=128, size=(1, 128, 1)).astype(
"pos_id": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
"id3": np.random.randint(low=0, high=128, size=(1, 128, 1)).astype(
"sent_id": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
"id4": np.random.randint(low=0, high=128, size=(1, 128, 1)).astype(
"id1": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
"id2": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
"id3": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
"id4": np.random.randint(low=0, high=128, size=(1, 128)).astype(
"int64"
),
}
......
......@@ -53,7 +53,7 @@ class TestDistCTR2x2(TestDistRunnerBase):
# build dnn model
dnn_layer_dims = [128, 64, 32, 1]
dnn_embedding = fluid.layers.embedding(
dnn_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=dnn_data,
size=[dnn_input_dim, dnn_layer_dims[0]],
......@@ -80,7 +80,7 @@ class TestDistCTR2x2(TestDistRunnerBase):
dnn_out = fc
# build lr model
lr_embedding = fluid.layers.embedding(
lr_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=lr_data,
size=[lr_input_dim, 1],
......
......@@ -101,7 +101,7 @@ class TestDistCTR2x2(FleetDistRunnerBase):
# build dnn model
dnn_layer_dims = [128, 128, 64, 32, 1]
dnn_embedding = fluid.layers.embedding(
dnn_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=dnn_data,
size=[dnn_input_dim, dnn_layer_dims[0]],
......@@ -113,7 +113,7 @@ class TestDistCTR2x2(FleetDistRunnerBase):
padding_idx=0,
)
dnn_pool = paddle.static.nn.sequence_lod.sequence_pool(
input=dnn_embedding, pool_type="sum"
input=dnn_embedding.squeeze(-2), pool_type="sum"
)
dnn_out = dnn_pool
for i, dim in enumerate(dnn_layer_dims[1:]):
......@@ -129,7 +129,7 @@ class TestDistCTR2x2(FleetDistRunnerBase):
dnn_out = fc
# build lr model
lr_embedding = fluid.layers.embedding(
lr_embbding = paddle.static.nn.embedding(
is_distributed=False,
input=lr_data,
size=[lr_input_dim, 1],
......@@ -141,7 +141,7 @@ class TestDistCTR2x2(FleetDistRunnerBase):
padding_idx=0,
)
lr_pool = paddle.static.nn.sequence_lod.sequence_pool(
input=lr_embedding, pool_type="sum"
input=lr_embbding.squeeze(-2), pool_type="sum"
)
merge_layer = paddle.concat([dnn_out, lr_pool], axis=1)
......
......@@ -72,7 +72,7 @@ class TestHeterPipelinePsCTR2x2(FleetDistHeterRunnerBase):
# build dnn model
dnn_layer_dims = [128, 64, 32, 1]
dnn_embedding = fluid.layers.embedding(
dnn_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=dnn_data,
size=[dnn_input_dim, dnn_layer_dims[0]],
......@@ -88,7 +88,7 @@ class TestHeterPipelinePsCTR2x2(FleetDistHeterRunnerBase):
dnn_out = dnn_pool
# build lr model
lr_embedding = fluid.layers.embedding(
lr_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=lr_data,
size=[lr_input_dim, 1],
......
......@@ -55,7 +55,7 @@ def conv_net(
fc0_dim=96,
class_dim=2,
):
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=input,
size=[dict_dim, emb_dim],
is_sparse=False,
......
......@@ -34,7 +34,7 @@ class TestDistWord2vec2x2(TestDistRunnerBase):
BATCH_SIZE = batch_size
def __network__(words):
embed_first = fluid.layers.embedding(
embed_first = paddle.static.nn.embedding(
input=words[0],
size=[dict_size, EMBED_SIZE],
dtype='float32',
......@@ -44,7 +44,7 @@ class TestDistWord2vec2x2(TestDistRunnerBase):
initializer=paddle.nn.initializer.Constant(value=0.1),
),
)
embed_second = fluid.layers.embedding(
embed_second = paddle.static.nn.embedding(
input=words[1],
size=[dict_size, EMBED_SIZE],
dtype='float32',
......@@ -54,7 +54,7 @@ class TestDistWord2vec2x2(TestDistRunnerBase):
initializer=paddle.nn.initializer.Constant(value=0.1),
),
)
embed_third = fluid.layers.embedding(
embed_third = paddle.static.nn.embedding(
input=words[2],
size=[dict_size, EMBED_SIZE],
dtype='float32',
......@@ -64,7 +64,7 @@ class TestDistWord2vec2x2(TestDistRunnerBase):
initializer=paddle.nn.initializer.Constant(value=0.1),
),
)
embed_forth = fluid.layers.embedding(
embed_forth = paddle.static.nn.embedding(
input=words[3],
size=[dict_size, EMBED_SIZE],
dtype='float32',
......
......@@ -64,7 +64,7 @@ def net(batch_size=4, lr=0.01):
# build dnn model
dnn_layer_dims = [2, 1]
dnn_embedding = fluid.layers.embedding(
dnn_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=dnn_data,
size=[dnn_input_dim, dnn_layer_dims[0]],
......@@ -80,7 +80,7 @@ def net(batch_size=4, lr=0.01):
dnn_out = dnn_pool
# build lr model
lr_embedding = fluid.layers.embedding(
lr_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=lr_data,
size=[lr_input_dim, 1],
......
......@@ -330,7 +330,7 @@ def sequence_conv_pool(
emb_dim = 128
hid_dim = 512
data = paddle.static.data(name="words", shape=[None, 1], dtype="int64", lod_level=1)
emb = fluid.layers.embedding(input=data, size=[input_dim, emb_dim], is_sparse=True)
emb = paddle.static.nn.embedding(input=data, size=[input_dim, emb_dim], is_sparse=True)
seq_conv = fluid.nets.sequence_conv_pool(input=emb,
num_filters=hid_dim,
filter_size=3,
......
......@@ -93,7 +93,7 @@ def bow_net(
name="words", shape=[-1, 1], dtype="int64", lod_level=1
)
label = paddle.static.data(name="label", shape=[-1, 1], dtype="int64")
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim]
)
bow = paddle.static.nn.sequence_lod.sequence_pool(
......
......@@ -36,7 +36,7 @@ class TestCommunicatorGeoEnd2End(unittest.TestCase):
name='x1', shape=[-1, 1], dtype='int64', lod_level=1
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=x1,
size=[10000, 10],
param_attr=fluid.ParamAttr(
......@@ -47,7 +47,7 @@ class TestCommunicatorGeoEnd2End(unittest.TestCase):
)
pool = paddle.static.nn.sequence_lod.sequence_pool(
input=emb, pool_type="sum"
input=emb.squeeze(-2), pool_type="sum"
)
z = paddle.concat([x, pool], axis=1)
......
......@@ -52,7 +52,7 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase):
dtype="int64",
lod_level=1,
)
x_embedding = paddle.fluid.layers.embedding(
x_embedding = paddle.static.nn.embedding(
is_distributed=False,
input=input_x,
size=[1000000000, 100000],
......
......@@ -49,7 +49,7 @@ class TestFleetGradientMergeMetaOptimizer(unittest.TestCase):
input_x = paddle.static.data(name="x", shape=[-1, 1], dtype='int64')
input_y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64')
emb = paddle.fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=input_x, size=[100, 10], is_sparse=True
)
......
......@@ -83,9 +83,8 @@ class TestDistFleetHeterProgram(unittest.TestCase):
def build_net(self, inputs):
def embedding_layer(input):
return fluid.layers.embedding(
return paddle.static.nn.sparse_embedding(
input=input,
is_sparse=True,
size=[100001, 10],
param_attr=fluid.ParamAttr(
name="SparseFeatFactors",
......
......@@ -75,7 +75,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="query_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
q_emb = fluid.layers.embedding(
q_emb = paddle.static.nn.embedding(
input=q,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......@@ -109,7 +109,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="pos_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
pt_emb = fluid.layers.embedding(
pt_emb = paddle.static.nn.embedding(
input=pt,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......@@ -142,7 +142,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="neg_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
nt_emb = fluid.layers.embedding(
nt_emb = paddle.static.nn.embedding(
input=nt,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......
......@@ -75,7 +75,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="query_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
q_emb = fluid.layers.embedding(
q_emb = paddle.static.nn.embedding(
input=q,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......@@ -109,7 +109,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="pos_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
pt_emb = fluid.layers.embedding(
pt_emb = paddle.static.nn.embedding(
input=pt,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......@@ -142,7 +142,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="neg_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
nt_emb = fluid.layers.embedding(
nt_emb = paddle.static.nn.embedding(
input=nt,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......
......@@ -75,7 +75,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="query_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
q_emb = fluid.layers.embedding(
q_emb = paddle.static.nn.embedding(
input=q,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......@@ -109,7 +109,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="pos_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
pt_emb = fluid.layers.embedding(
pt_emb = paddle.static.nn.embedding(
input=pt,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......@@ -142,7 +142,7 @@ class TestPSPassWithBow(unittest.TestCase):
name="neg_title_ids", shape=[-1, 1], dtype="int64", lod_level=1
)
# embedding
nt_emb = fluid.layers.embedding(
nt_emb = paddle.static.nn.embedding(
input=nt,
is_distributed=is_distributed,
size=[dict_dim, emb_dim],
......
......@@ -34,7 +34,7 @@ class SparseLoadOp(unittest.TestCase):
'input', shape=[None, 1], dtype="int64"
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=dense_input,
is_sparse=True,
size=[10, 10],
......
......@@ -49,7 +49,7 @@ class TestSparseLoadProgram(unittest.TestCase):
inputs = paddle.static.data(
'input', shape=[None, 1], dtype="int64"
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
inputs, is_sparse=True, size=[10000, 128]
)
fc1 = paddle.static.nn.fc(
......
......@@ -352,7 +352,7 @@ class TestFakeInit(TranspilerTest):
inputs = [input_word, true_word, neg_word]
init_width = 0.5 / embedding_size
input_emb = fluid.layers.embedding(
input_emb = paddle.static.nn.embedding(
input=inputs[0],
is_sparse=True,
size=[dict_size, embedding_size],
......@@ -364,7 +364,7 @@ class TestFakeInit(TranspilerTest):
),
)
true_emb_w = fluid.layers.embedding(
true_emb_w = paddle.static.nn.embedding(
input=inputs[1],
is_sparse=True,
size=[dict_size, embedding_size],
......@@ -374,7 +374,7 @@ class TestFakeInit(TranspilerTest):
),
)
true_emb_b = fluid.layers.embedding(
true_emb_b = paddle.static.nn.embedding(
input=inputs[1],
is_sparse=True,
size=[dict_size, 1],
......@@ -387,7 +387,7 @@ class TestFakeInit(TranspilerTest):
neg_word_reshape = paddle.reshape(inputs[2], shape=[-1, 1])
neg_word_reshape.stop_gradient = True
neg_emb_w = fluid.layers.embedding(
neg_emb_w = paddle.static.nn.embedding(
input=neg_word_reshape,
is_sparse=True,
size=[dict_size, embedding_size],
......@@ -398,7 +398,7 @@ class TestFakeInit(TranspilerTest):
neg_emb_w, shape=[-1, neg_num, embedding_size]
)
neg_emb_b = fluid.layers.embedding(
neg_emb_b = paddle.static.nn.embedding(
input=neg_word_reshape,
is_sparse=True,
size=[dict_size, 1],
......@@ -712,7 +712,7 @@ class TestDistLookupTableBase(TranspilerTest):
self.lookup_table_name = 'shared_w'
def emb_pool(ids, table_name, is_distributed):
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=ids,
size=[self.table_size, self.emb_size],
dtype='float32',
......@@ -1427,7 +1427,7 @@ class TestRemoteHsigmoid(TestDistLookupTableBase):
)
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=input,
is_sparse=is_sparse,
size=[3, 3],
......
......@@ -53,7 +53,7 @@ class TestListenAndServOp(unittest.TestCase):
)
os.system(cmd)
x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64')
x_emb = fluid.layers.embedding(
x_emb = paddle.static.nn.embedding(
input=x, size=[1, 2], is_distributed=True
)
y_predict = paddle.static.nn.fc(x=x_emb, size=1)
......@@ -117,7 +117,7 @@ class TestListenAndServOp(unittest.TestCase):
)
os.system(cmd)
x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64')
x_emb = fluid.layers.embedding(
x_emb = paddle.static.nn.embedding(
input=x, size=[1, 2], is_distributed=True
)
y_predict = paddle.static.nn.fc(x=x_emb, size=1)
......@@ -179,7 +179,7 @@ class TestListenAndServOp(unittest.TestCase):
)
os.system(cmd)
x = paddle.static.data(name='x', shape=[-1, 1], dtype='int64')
x_emb = fluid.layers.embedding(
x_emb = paddle.static.nn.embedding(
input=x, size=[1, 2], is_distributed=True
)
y_predict = paddle.static.nn.fc(x=x_emb, size=1)
......
......@@ -19,7 +19,6 @@ import numpy as np
import paddle
from paddle import fluid
from paddle.fluid import layers
from paddle.fluid.executor import Executor
os.environ["CPU_NUM"] = "1"
......@@ -241,7 +240,7 @@ def lm_model(
init_cell, shape=[num_layers, -1, hidden_size]
)
x_emb = layers.embedding(
x_emb = paddle.static.nn.embedding(
input=x,
size=[vocab_size, hidden_size],
dtype='float32',
......
......@@ -31,7 +31,7 @@ class EntryAttrChecks(unittest.TestCase):
input = paddle.static.data(
name="dnn_data", shape=[-1, 1], dtype="int64", lod_level=1
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=input,
size=[100, 10],
is_sparse=True,
......
......@@ -62,7 +62,7 @@ class TestFleet1(unittest.TestCase):
dtype="int64",
lod_level=1,
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=show,
size=[1, 1],
is_sparse=True,
......
......@@ -45,9 +45,7 @@ class TestFleetBase(unittest.TestCase):
)
input_y = paddle.static.data(name="y", shape=[-1, 1], dtype='int64')
emb = paddle.fluid.layers.embedding(
input=input_slot, size=[10, 9], is_sparse=True
)
emb = paddle.static.nn.sparse_embedding(input=input_slot, size=[10, 9])
input_x = paddle.concat(x=[input_x, emb], axis=1)
fc_1 = paddle.static.nn.fc(x=input_x, size=64, activation='tanh')
fc_2 = paddle.static.nn.fc(x=fc_1, size=64, activation='tanh')
......
......@@ -62,7 +62,7 @@ class TestFleet1(unittest.TestCase):
dtype="int64",
lod_level=1,
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=show,
size=[1, 1],
is_sparse=True,
......
......@@ -59,7 +59,7 @@ class TestFleet1(unittest.TestCase):
show = paddle.static.data(
name="show", shape=[-1, 1], dtype="int64", lod_level=1
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=show,
size=[1, 1],
is_sparse=True,
......
......@@ -33,7 +33,7 @@ def bow_net(
This model is from https://github.com/PaddlePaddle/models:
fluid/PaddleNLP/text_classification/nets.py
"""
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=data, is_sparse=True, size=[dict_dim, emb_dim]
)
bow = paddle.static.nn.sequence_lod.sequence_pool(
......
......@@ -294,7 +294,7 @@ class TestHSigmoidOpWithSparseGrad(unittest.TestCase):
data_list = [input_word, path_table, path_code, label]
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=input_word,
is_sparse=is_sparse,
size=[3, 3],
......
......@@ -24,7 +24,7 @@ from test_imperative_base import new_program_scope
import paddle
import paddle.nn.functional as F
from paddle import fluid
from paddle.fluid import core, layers
from paddle.fluid import core
from paddle.fluid.dygraph import base, to_variable
from paddle.fluid.framework import Program, default_main_program, program_guard
from paddle.incubate.layers.nn import (
......@@ -609,8 +609,8 @@ class TestLayer(LayerTest):
name='word', shape=[-1, 1], dtype='int64'
)
data_t.desc.set_need_check_feed(False)
emb = layers.embedding(
input=data_t,
emb = paddle.static.nn.embedding(
input=data_t.squeeze(-2),
size=[dict_size, 32],
param_attr='emb.w',
is_sparse=False,
......@@ -1662,26 +1662,26 @@ class TestBook(LayerTest):
forth_word = self._get_data(name='forthw', shape=[1], dtype='int64')
next_word = self._get_data(name='nextw', shape=[1], dtype='int64')
embed_first = layers.embedding(
embed_first = paddle.static.nn.embedding(
input=first_word,
size=[dict_size, embed_size],
dtype='float32',
param_attr='shared_w',
)
embed_second = layers.embedding(
embed_second = paddle.static.nn.embedding(
input=second_word,
size=[dict_size, embed_size],
dtype='float32',
param_attr='shared_w',
)
embed_third = layers.embedding(
embed_third = paddle.static.nn.embedding(
input=third_word,
size=[dict_size, embed_size],
dtype='float32',
param_attr='shared_w',
)
embed_forth = layers.embedding(
embed_forth = paddle.static.nn.embedding(
input=forth_word,
size=[dict_size, embed_size],
dtype='float32',
......@@ -1754,7 +1754,7 @@ class TestBook(LayerTest):
if i == label_word:
continue
emb = layers.embedding(
emb = paddle.static.nn.embedding(
input=words[i],
size=[dict_size, 32],
param_attr='emb.w',
......
......@@ -236,7 +236,7 @@ class TestEmbeddingLayerBF16ConstantInitializer(unittest.TestCase):
x = paddle.static.data(
name='x', shape=self.ids_shape, dtype='int64'
)
self.emb = fluid.layers.embedding(
self.emb = paddle.static.nn.embedding(
input=x,
size=self.w_shape,
param_attr=fluid.ParamAttr(
......@@ -256,7 +256,7 @@ class TestEmbeddingLayerBF16ConstantInitializer(unittest.TestCase):
np.testing.assert_array_equal(self.w_fp32, result)
def test_lookup_results(self):
lookup_result = convert_uint16_to_float(self.result[1])
lookup_result = convert_uint16_to_float(self.result[1].squeeze(-2))
lookup_ref = _lookup(self.w_fp32, self.ids, self.flat_ids)
np.testing.assert_array_equal(lookup_result, lookup_ref)
......
......@@ -25,7 +25,6 @@ from op import Operator
import paddle
import paddle.nn.functional as F
from paddle import fluid
from paddle.fluid import Program, core, program_guard
......@@ -168,7 +167,7 @@ class TestEmbedOpError(unittest.TestCase):
def test_Variable():
# the input type must be Variable
fluid.layers.embedding(input=input_data, size=(10, 64))
paddle.static.nn.embedding(input=input_data, size=(10, 64))
self.assertRaises(TypeError, test_Variable)
......@@ -177,7 +176,7 @@ class TestEmbedOpError(unittest.TestCase):
input = paddle.static.data(
name='x', shape=[4, 1], dtype='float32'
)
fluid.layers.embedding(input=input, size=(10, 64))
paddle.static.nn.embedding(input=input, size=(10, 64))
self.assertRaises(TypeError, test_input_dtype)
......@@ -186,7 +185,7 @@ class TestEmbedOpError(unittest.TestCase):
input2 = paddle.static.data(
name='x2', shape=[4, 1], dtype='int64'
)
fluid.layers.embedding(
paddle.static.nn.embedding(
input=input2, size=(10, 64), dtype='int64'
)
......@@ -195,7 +194,7 @@ class TestEmbedOpError(unittest.TestCase):
input3 = paddle.static.data(
name='x3', shape=[4, 1], dtype='int64'
)
fluid.layers.embedding(
paddle.static.nn.embedding(
input=input3, size=(10, 64), dtype='float16'
)
......
......@@ -61,7 +61,9 @@ class TestDatasetWithStat(unittest.TestCase):
embs = []
for x in slots_vars:
emb = fluid.layers.embedding(x, is_sparse=True, size=[100001, 4])
emb = paddle.static.nn.embedding(
x, is_sparse=True, size=[100001, 4]
)
embs.append(emb)
dataset = paddle.distributed.InMemoryDataset()
......
......@@ -127,7 +127,7 @@ def bow_net(
This model is from https://github.com/PaddlePaddle/models:
fluid/PaddleNLP/text_classification/nets.py
"""
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim]
)
bow = paddle.static.nn.sequence_lod.sequence_pool(
......
......@@ -39,7 +39,7 @@ def bow_net(
This model is from https://github.com/PaddlePaddle/models:
fluid/PaddleNLP/text_classification/nets.py
"""
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim]
)
bow = paddle.static.nn.sequence_lod.sequence_pool(
......
......@@ -342,7 +342,7 @@ class TestSGDOpBF16API(unittest.TestCase):
label = paddle.static.data(
name='Y', shape=[-1] + y_shape, dtype='uint16'
)
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=x,
size=self.w_shape,
param_attr=fluid.ParamAttr(
......
......@@ -54,7 +54,7 @@ def bow_net(
This model is from https://github.com/PaddlePaddle/models:
fluid/PaddleNLP/text_classification/nets.py
"""
emb = fluid.layers.embedding(
emb = paddle.static.nn.embedding(
input=data, is_sparse=is_sparse, size=[dict_dim, emb_dim]
)
bow = paddle.static.nn.sequence_lod.sequence_pool(
......
......@@ -18,7 +18,6 @@ import numpy as np
import paddle
from paddle import fluid
from paddle.fluid import layers
pos_enc_param_names = (
"src_pos_enc_table",
......@@ -264,13 +263,13 @@ def prepare_encoder(
This module is used at the bottom of the encoder stacks.
"""
src_word_emb = layers.embedding(
src_word_emb = paddle.static.nn.embedding(
src_word,
size=[src_vocab_size, src_emb_dim],
padding_idx=src_pad_idx,
param_attr=paddle.nn.initializer.Normal(0.0, 1.0),
)
src_pos_enc = layers.embedding(
src_pos_enc = paddle.static.nn.embedding(
src_pos,
size=[src_max_len, src_emb_dim],
padding_idx=pos_pad_idx,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册