Skip to content
Open
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
88 changes: 88 additions & 0 deletions graph_net/test/nlp_model_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,91 @@ def get_xlnet_model_and_inputs(model_name, text, dtype):
enc["attention_mask"] = (input_ids != pad_id).astype("int64")

return model, enc


def get_t5_model_and_inputs(model_name, text, dtype):
import paddle
from paddlenlp.transformers import T5ForConditionalGeneration, T5Tokenizer

# 1) 分词器(先建 tokenizer 方便取 pad/eos id)
tokenizer = T5Tokenizer.from_pretrained(model_name)

# 2) 编码输入(支持单条或批量 text)
enc = tokenizer(
text,
return_tensors="pd",
padding=True,
truncation=True,
max_length=512,
)

# 补 attention_mask(pad 处为 0,其他为 1)
if "attention_mask" not in enc:
input_ids = enc["input_ids"]
attn_mask = (input_ids != tokenizer.pad_token_id).astype("int64")
enc["attention_mask"] = attn_mask

# 构造 decoder_input_ids:
# T5 以 pad_token_id 作为 decoder_start_token_id
batch_size = enc["input_ids"].shape[0]
decoder_input_ids = paddle.full(
shape=[batch_size, 1],
fill_value=tokenizer.pad_token_id,
dtype="int64",
)

# 3) 加载模型
model = T5ForConditionalGeneration.from_pretrained(model_name)
if dtype == "float16":
model = model.astype(paddle.float16)
model.eval()

# 4) 组装喂给模型的输入
inputs = {
"input_ids": enc["input_ids"],
"attention_mask": enc["attention_mask"],
"decoder_input_ids": decoder_input_ids,
}
return model, inputs


def get_albert_model_and_inputs(model_name, text, dtype):
"""
加载 ALBERT backbone(AlbertModel)并构造输入。
- model_name 例如: "albert-base-v2", "albert-xxlarge-v1"(PaddleNLP 内置名称)
- dtype: "float32" 或 "float16"
返回: (model, inputs_dict)
"""
import paddle
from paddlenlp.transformers import AlbertConfig, AlbertModel, AlbertTokenizer

# 1) 读取配置(不触发权重下载)
config = AlbertConfig.from_pretrained(model_name)

# 2) 模型
# 若你只需要网络结构,可改成: model = AlbertModel(config)
model = AlbertModel(config)
if dtype == "float16":
model = model.astype(paddle.float16)
model.eval()

# 3) 分词器
tokenizer = AlbertTokenizer.from_pretrained(model_name)

# 若无 pad_token,则回退到 unk_token(ALBERT 没有 eos_token,别设 pad=eos)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.unk_token

enc = tokenizer(
text,
return_tensors="pd",
padding=True,
truncation=True,
max_length=512,
)

if "attention_mask" not in enc:
input_ids = enc["input_ids"]
enc["attention_mask"] = (input_ids != tokenizer.pad_token_id).astype("int64")

return model, enc
6 changes: 6 additions & 0 deletions paddle_samples/PaddleNLP/albert-base-v1/graph_net.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"framework": "paddle",
"model_name": "albert-base-v1",
"num_devices_required": 1,
"num_nodes_required": 1
}
41 changes: 41 additions & 0 deletions paddle_samples/PaddleNLP/albert-base-v1/input_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Program_weight_tensor_data_0:
name = "data_0"
shape = [1, 21]
dtype = "int64"
data = [
2,
10975,
15,
51,
204,
25,
1909,
9,
31,
589,
2477,
88,
370,
816,
2761,
17,
66,
2607,
18,
9,
3,
]


class Program_weight_tensor_data_1:
name = "data_1"
shape = [1, 21]
dtype = "int64"
data = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]


class Program_weight_tensor_data_2:
name = "data_2"
shape = [1, 21]
dtype = "int64"
data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Loading
Loading