forked from PaddlePaddle/PaddleNLP
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
Latest commit
79 lines (67 loc) Β· 3.23 KB
/
Copy pathutils.py
File metadata and controls
79 lines (67 loc) Β· 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
fromtypingimportAny, Dict, List, Optional, Tuple, Union
importnumpyasnp
importpaddle
importpaddle.nnasnn
frompaddlenlp.trainerimportTrainer
classGLMTrainer(Trainer):
def__init__(self, do_generation: bool, **kwargs):
super().__init__(**kwargs)
self.do_generation=do_generation
defprediction_step(
self,
model: nn.Layer,
inputs: Dict[str, Union[paddle.Tensor, Any]],
prediction_loss_only: bool,
ignore_keys: Optional[List[str]] =None,
) ->Tuple[Optional[paddle.Tensor], Optional[paddle.Tensor], Optional[paddle.Tensor]]:
ifnotself.do_generation:
returnsuper().prediction_step(model, inputs, prediction_loss_only, ignore_keys)
model.eval()
withpaddle.no_grad():
tokens=model.generate(
input_ids=inputs["input_ids"],
position_ids=inputs["position_ids"],
attention_mask=inputs["attention_mask"],
decode_strategy="sampling",
top_k=1,
repetition_penalty=2.0,
bos_token_id=self.tokenizer.sop_token_id,
eos_token_id=self.tokenizer.eop_token_id,
pad_token_id=self.tokenizer.pad_token_id,
)[0]
all_preds= []
forpred_tokensintokens:
all_preds.append(pred_tokens[pred_tokens!=self.tokenizer.pad_token_id].tolist())
max_pred_length=max([len(x) forxinall_preds])
forindex, predsinenumerate(all_preds):
all_preds[index] =preds+ [-100] * (max_pred_length-len(preds))
all_labels= []
forlabel, maskinzip(inputs["labels"].numpy(), inputs["loss_mask"].numpy()):
label=label[mask.astype("bool")]
label= [xforxinlabel[label!=self.tokenizer.pad_token_id]]
all_labels.append(label)
max_label_length=max([len(x) forxinall_labels])
forindex, labelsinenumerate(all_labels):
all_labels[index] =labels+ [-100] * (max_label_length-len(labels))
return (None, paddle.to_tensor(all_preds), paddle.to_tensor(all_labels))
deflog(self, logs: Dict[str, float], **kwargs) ->None:
ifself.state.epochisnotNone:
logs["epoch"] =round(self.state.epoch, 4)
if"eval_loss"inlogs:
logs["eval_ppl"] =np.exp(logs["eval_loss"])
output= {**logs, **{"step": self.state.global_step}}
self.state.log_history.append(output)
self.control=self.callback_handler.on_log(self.args, self.state, self.control, logs, **kwargs)