We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
fix perf and sample code
<!-- Demo: https://github.com/PaddlePaddle/PaddleNLP/pull/26 -->
### PR types
<!-- One of [ New features | Bug fixes | Function optimization | Performance optimization | Breaking changes | Others ] -->
Others
### PR changes
<!-- One of [ Models | APIs | Docs | Others ] -->
APIs
### Description
<!-- Describe what this PR does -->
fix perf and sample code

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `examples/language_model/gpt/faster_gpt/export_model.py`
Content:
```
1 # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import sys
15 import os
16 import numpy as np
17 from attrdict import AttrDict
18 import argparse
19 import time
20 
21 import paddle
22 
23 import yaml
24 from pprint import pprint
25 
26 from paddlenlp.ops import FasterGPT
27 from paddlenlp.transformers import GPTLMHeadModel
28 from paddlenlp.transformers import GPTChineseTokenizer, GPTTokenizer
29 
30 from paddlenlp.utils.log import logger
31 
32 MODEL_CLASSES = {
33     "gpt-cpm-large-cn": (GPTLMHeadModel, GPTChineseTokenizer),
34     "gpt2-medium-en": (GPTLMHeadModel, GPTTokenizer),
35 }
36 
37 
38 def parse_args():
39     parser = argparse.ArgumentParser()
40     parser.add_argument(
41         "--model_name_or_path",
42         default="gpt2-medium-en",
43         type=str,
44         help="The model name to specify the gpt to use. Can be one of ['gpt2-en', 'gpt2-medium-en', 'gpt-cpm-large-cn']. "
45     )
46     parser.add_argument(
47         "--decoding_lib",
48         default="../../build/lib/libdecoding_op.so",
49         type=str,
50         help="Path of libdecoding_op.so. ")
51     parser.add_argument(
52         "--inference_model_dir",
53         default="./infer_model/",
54         type=str,
55         help="Path to save inference model of gpt. ")
56     parser.add_argument(
57         "--topk",
58         default=4,
59         type=int,
60         help="The number of candidate to procedure beam search. ")
61     parser.add_argument(
62         "--topp",
63         default=0.0,
64         type=float,
65         help="The probability threshold to procedure topp sampling. ")
66     parser.add_argument(
67         "--max_out_len", default=32, type=int, help="Maximum output length. ")
68     parser.add_argument(
69         "--temperature",
70         default=1.0,
71         type=float,
72         help="The temperature to set. ")
73     parser.add_argument(
74         "--use_fp16_decoding",
75         action="store_true",
76         help="Whether to use fp16 decoding to predict. ")
77     args = parser.parse_args()
78     return args
79 
80 
81 def do_predict(args):
82     place = "gpu"
83     place = paddle.set_device(place)
84 
85     model_class, tokenizer_class = MODEL_CLASSES[args.model_name_or_path]
86     tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path)
87     logger.info('Loading the model parameters, please wait...')
88     model = model_class.from_pretrained(
89         args.model_name_or_path, max_predict_len=args.max_out_len)
90 
91     gpt = FasterGPT(
92         model=model,
93         decoding_lib=args.decoding_lib,
94         use_fp16_decoding=args.use_fp16_decoding)
95 
96     # Set evaluate mode
97     gpt.eval()
98 
99     # Convert dygraph model to static graph model 
100     gpt = paddle.jit.to_static(
101         gpt,
102         input_spec=[
103             # input_ids
104             paddle.static.InputSpec(
105                 shape=[None, None], dtype="int32"),
106             #
107             # If it's necessarry to provide mem_seq_len and attention_mask,
108             # the parameters should be:
109             # mem_seq_len
110             # paddle.static.InputSpec(shape=[None, None], dtype="int32"),
111             # attention_mask
112             # paddle.static.InputSpec(shape=[None, None, None], dtype="float16" if args.use_fp16_decoding else "float32"),
113             #
114             None,  # mem_seq_len
115             None,  # attention_mask
116             args.topk,
117             args.topp,
118             args.max_out_len,
119             tokenizer.eos_token_id,
120             tokenizer.eos_token_id,
121             tokenizer.pad_token_id,
122             args.temperature,
123         ])
124 
125     # Save converted static graph model
126     paddle.jit.save(gpt, os.path.join(args.inference_model_dir, "gpt"))
127     logger.info("GPT has been saved to {}".format(args.inference_model_dir))
128 
129     gpt.save_resources(tokenizer, args.inference_model_dir)
130 
131 
132 if __name__ == "__main__":
133     args = parse_args()
134     pprint(args)
135     do_predict(args)
136
```

Path: `paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py`
Content:
```
1 # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 import sys
15 import os
16 import numpy as np
17 from attrdict import AttrDict
18 import argparse
19 import time
20 
21 import paddle
22 
23 import yaml
24 from pprint import pprint
25 
26 from paddlenlp.ops import FasterGPT
27 from paddlenlp.transformers import GPTLMHeadModel
28 from paddlenlp.transformers import GPTChineseTokenizer, GPTTokenizer
29 
30 from paddlenlp.utils.log import logger
31 
32 MODEL_CLASSES = {
33     "gpt-cpm-large-cn": (GPTLMHeadModel, GPTChineseTokenizer),
34     "gpt2-medium-en": (GPTLMHeadModel, GPTTokenizer),
35 }
36 
37 
38 def parse_args():
39     parser = argparse.ArgumentParser()
40     parser.add_argument(
41         "--model_name_or_path",
42         default="gpt2-medium-en",
43         type=str,
44         help="The model name to specify the gpt to use. Can be one of ['gpt2-en', 'gpt2-medium-en', 'gpt-cpm-large-cn']. "
45     )
46     parser.add_argument(
47         "--decoding_lib",
48         default="../../build/lib/libdecoding_op.so",
49         type=str,
50         help="Path of libdecoding_op.so. ")
51     parser.add_argument(
52         "--inference_model_dir",
53         default="./infer_model/",
54         type=str,
55         help="Path to save inference model of gpt. ")
56     parser.add_argument(
57         "--topk",
58         default=4,
59         type=int,
60         help="The number of candidate to procedure beam search. ")
61     parser.add_argument(
62         "--topp",
63         default=0.0,
64         type=float,
65         help="The probability threshold to procedure topp sampling. ")
66     parser.add_argument(
67         "--max_out_len", default=32, type=int, help="Maximum output length. ")
68     parser.add_argument(
69         "--temperature",
70         default=1.0,
71         type=float,
72         help="The temperature to set. ")
73     parser.add_argument(
74         "--use_fp16_decoding",
75         action="store_true",
76         help="Whether to use fp16 decoding to predict. ")
77     args = parser.parse_args()
78     return args
79 
80 
81 def do_predict(args):
82     place = "gpu"
83     place = paddle.set_device(place)
84 
85     model_class, tokenizer_class = MODEL_CLASSES[args.model_name_or_path]
86     tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path)
87     logger.info('Loading the model parameters, please wait...')
88     model = model_class.from_pretrained(
89         args.model_name_or_path, max_predict_len=args.max_out_len)
90 
91     gpt = FasterGPT(
92         model=model,
93         decoding_lib=args.decoding_lib,
94         use_fp16_decoding=args.use_fp16_decoding)
95 
96     # Set evaluate mode
97     gpt.eval()
98 
99     # Convert dygraph model to static graph model 
100     gpt = paddle.jit.to_static(
101         gpt,
102         input_spec=[
103             # input_ids
104             paddle.static.InputSpec(
105                 shape=[None, None], dtype="int32"),
106             #
107             # If it's necessarry to provide mem_seq_len and attention_mask,
108             # the parameters should be:
109             # mem_seq_len
110             # paddle.static.InputSpec(shape=[None, None], dtype="int32"),
111             # attention_mask
112             # paddle.static.InputSpec(shape=[None, None, None], dtype="float16" if args.use_fp16_decoding else "float32"),
113             #
114             None,  # mem_seq_len
115             None,  # attention_mask
116             args.topk,
117             args.topp,
118             args.max_out_len,
119             tokenizer.eos_token_id,
120             tokenizer.eos_token_id,
121             tokenizer.pad_token_id,
122             args.temperature,
123         ])
124 
125     # Save converted static graph model
126     paddle.jit.save(gpt, os.path.join(args.inference_model_dir, "gpt"))
127     logger.info("GPT has been saved to {}".format(args.inference_model_dir))
128 
129     gpt.save_resources(tokenizer, args.inference_model_dir)
130 
131 
132 if __name__ == "__main__":
133     args = parse_args()
134     pprint(args)
135     do_predict(args)
136
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/examples/language_model/gpt/faster_gpt/export_model.py b/examples/language_model/gpt/faster_gpt/export_model.py
--- a/examples/language_model/gpt/faster_gpt/export_model.py
+++ b/examples/language_model/gpt/faster_gpt/export_model.py
@@ -119,6 +119,7 @@
             tokenizer.eos_token_id,
             tokenizer.eos_token_id,
             tokenizer.pad_token_id,
+            None,  # forced_eos_token_id
             args.temperature,
         ])
 
diff --git a/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py b/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py
--- a/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py
+++ b/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py
@@ -119,6 +119,7 @@
             tokenizer.eos_token_id,
             tokenizer.eos_token_id,
             tokenizer.pad_token_id,
+            None,  # forced_eos_token_id
             args.temperature,
         ])

{"golden_diff": "diff --git a/examples/language_model/gpt/faster_gpt/export_model.py b/examples/language_model/gpt/faster_gpt/export_model.py\n--- a/examples/language_model/gpt/faster_gpt/export_model.py\n+++ b/examples/language_model/gpt/faster_gpt/export_model.py\n@@ -119,6 +119,7 @@\n             tokenizer.eos_token_id,\n             tokenizer.eos_token_id,\n             tokenizer.pad_token_id,\n+            None,  # forced_eos_token_id\n             args.temperature,\n         ])\n \ndiff --git a/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py b/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py\n--- a/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py\n+++ b/paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py\n@@ -119,6 +119,7 @@\n             tokenizer.eos_token_id,\n             tokenizer.eos_token_id,\n             tokenizer.pad_token_id,\n+            None,  # forced_eos_token_id\n             args.temperature,\n         ])\n", "issue": "fix perf and sample code\n<!-- Demo: https://github.com/PaddlePaddle/PaddleNLP/pull/26 -->\r\n### PR types\r\n<!-- One of [ New features | Bug fixes | Function optimization | Performance optimization | Breaking changes | Others ] -->\r\nOthers\r\n### PR changes\r\n<!-- One of [ Models | APIs | Docs | Others ] -->\r\nAPIs\r\n### Description\r\n<!-- Describe what this PR does -->\r\nfix perf and sample code\n", "before_files": [{"content": "# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport sys\nimport os\nimport numpy as np\nfrom attrdict import AttrDict\nimport argparse\nimport time\n\nimport paddle\n\nimport yaml\nfrom pprint import pprint\n\nfrom paddlenlp.ops import FasterGPT\nfrom paddlenlp.transformers import GPTLMHeadModel\nfrom paddlenlp.transformers import GPTChineseTokenizer, GPTTokenizer\n\nfrom paddlenlp.utils.log import logger\n\nMODEL_CLASSES = {\n    \"gpt-cpm-large-cn\": (GPTLMHeadModel, GPTChineseTokenizer),\n    \"gpt2-medium-en\": (GPTLMHeadModel, GPTTokenizer),\n}\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\n        \"--model_name_or_path\",\n        default=\"gpt2-medium-en\",\n        type=str,\n        help=\"The model name to specify the gpt to use. Can be one of ['gpt2-en', 'gpt2-medium-en', 'gpt-cpm-large-cn']. \"\n    )\n    parser.add_argument(\n        \"--decoding_lib\",\n        default=\"../../build/lib/libdecoding_op.so\",\n        type=str,\n        help=\"Path of libdecoding_op.so. \")\n    parser.add_argument(\n        \"--inference_model_dir\",\n        default=\"./infer_model/\",\n        type=str,\n        help=\"Path to save inference model of gpt. \")\n    parser.add_argument(\n        \"--topk\",\n        default=4,\n        type=int,\n        help=\"The number of candidate to procedure beam search. \")\n    parser.add_argument(\n        \"--topp\",\n        default=0.0,\n        type=float,\n        help=\"The probability threshold to procedure topp sampling. \")\n    parser.add_argument(\n        \"--max_out_len\", default=32, type=int, help=\"Maximum output length. \")\n    parser.add_argument(\n        \"--temperature\",\n        default=1.0,\n        type=float,\n        help=\"The temperature to set. \")\n    parser.add_argument(\n        \"--use_fp16_decoding\",\n        action=\"store_true\",\n        help=\"Whether to use fp16 decoding to predict. \")\n    args = parser.parse_args()\n    return args\n\n\ndef do_predict(args):\n    place = \"gpu\"\n    place = paddle.set_device(place)\n\n    model_class, tokenizer_class = MODEL_CLASSES[args.model_name_or_path]\n    tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path)\n    logger.info('Loading the model parameters, please wait...')\n    model = model_class.from_pretrained(\n        args.model_name_or_path, max_predict_len=args.max_out_len)\n\n    gpt = FasterGPT(\n        model=model,\n        decoding_lib=args.decoding_lib,\n        use_fp16_decoding=args.use_fp16_decoding)\n\n    # Set evaluate mode\n    gpt.eval()\n\n    # Convert dygraph model to static graph model \n    gpt = paddle.jit.to_static(\n        gpt,\n        input_spec=[\n            # input_ids\n            paddle.static.InputSpec(\n                shape=[None, None], dtype=\"int32\"),\n            #\n            # If it's necessarry to provide mem_seq_len and attention_mask,\n            # the parameters should be:\n            # mem_seq_len\n            # paddle.static.InputSpec(shape=[None, None], dtype=\"int32\"),\n            # attention_mask\n            # paddle.static.InputSpec(shape=[None, None, None], dtype=\"float16\" if args.use_fp16_decoding else \"float32\"),\n            #\n            None,  # mem_seq_len\n            None,  # attention_mask\n            args.topk,\n            args.topp,\n            args.max_out_len,\n            tokenizer.eos_token_id,\n            tokenizer.eos_token_id,\n            tokenizer.pad_token_id,\n            args.temperature,\n        ])\n\n    # Save converted static graph model\n    paddle.jit.save(gpt, os.path.join(args.inference_model_dir, \"gpt\"))\n    logger.info(\"GPT has been saved to {}\".format(args.inference_model_dir))\n\n    gpt.save_resources(tokenizer, args.inference_model_dir)\n\n\nif __name__ == \"__main__\":\n    args = parse_args()\n    pprint(args)\n    do_predict(args)\n", "path": "examples/language_model/gpt/faster_gpt/export_model.py"}, {"content": "# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport sys\nimport os\nimport numpy as np\nfrom attrdict import AttrDict\nimport argparse\nimport time\n\nimport paddle\n\nimport yaml\nfrom pprint import pprint\n\nfrom paddlenlp.ops import FasterGPT\nfrom paddlenlp.transformers import GPTLMHeadModel\nfrom paddlenlp.transformers import GPTChineseTokenizer, GPTTokenizer\n\nfrom paddlenlp.utils.log import logger\n\nMODEL_CLASSES = {\n    \"gpt-cpm-large-cn\": (GPTLMHeadModel, GPTChineseTokenizer),\n    \"gpt2-medium-en\": (GPTLMHeadModel, GPTTokenizer),\n}\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\n        \"--model_name_or_path\",\n        default=\"gpt2-medium-en\",\n        type=str,\n        help=\"The model name to specify the gpt to use. Can be one of ['gpt2-en', 'gpt2-medium-en', 'gpt-cpm-large-cn']. \"\n    )\n    parser.add_argument(\n        \"--decoding_lib\",\n        default=\"../../build/lib/libdecoding_op.so\",\n        type=str,\n        help=\"Path of libdecoding_op.so. \")\n    parser.add_argument(\n        \"--inference_model_dir\",\n        default=\"./infer_model/\",\n        type=str,\n        help=\"Path to save inference model of gpt. \")\n    parser.add_argument(\n        \"--topk\",\n        default=4,\n        type=int,\n        help=\"The number of candidate to procedure beam search. \")\n    parser.add_argument(\n        \"--topp\",\n        default=0.0,\n        type=float,\n        help=\"The probability threshold to procedure topp sampling. \")\n    parser.add_argument(\n        \"--max_out_len\", default=32, type=int, help=\"Maximum output length. \")\n    parser.add_argument(\n        \"--temperature\",\n        default=1.0,\n        type=float,\n        help=\"The temperature to set. \")\n    parser.add_argument(\n        \"--use_fp16_decoding\",\n        action=\"store_true\",\n        help=\"Whether to use fp16 decoding to predict. \")\n    args = parser.parse_args()\n    return args\n\n\ndef do_predict(args):\n    place = \"gpu\"\n    place = paddle.set_device(place)\n\n    model_class, tokenizer_class = MODEL_CLASSES[args.model_name_or_path]\n    tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path)\n    logger.info('Loading the model parameters, please wait...')\n    model = model_class.from_pretrained(\n        args.model_name_or_path, max_predict_len=args.max_out_len)\n\n    gpt = FasterGPT(\n        model=model,\n        decoding_lib=args.decoding_lib,\n        use_fp16_decoding=args.use_fp16_decoding)\n\n    # Set evaluate mode\n    gpt.eval()\n\n    # Convert dygraph model to static graph model \n    gpt = paddle.jit.to_static(\n        gpt,\n        input_spec=[\n            # input_ids\n            paddle.static.InputSpec(\n                shape=[None, None], dtype=\"int32\"),\n            #\n            # If it's necessarry to provide mem_seq_len and attention_mask,\n            # the parameters should be:\n            # mem_seq_len\n            # paddle.static.InputSpec(shape=[None, None], dtype=\"int32\"),\n            # attention_mask\n            # paddle.static.InputSpec(shape=[None, None, None], dtype=\"float16\" if args.use_fp16_decoding else \"float32\"),\n            #\n            None,  # mem_seq_len\n            None,  # attention_mask\n            args.topk,\n            args.topp,\n            args.max_out_len,\n            tokenizer.eos_token_id,\n            tokenizer.eos_token_id,\n            tokenizer.pad_token_id,\n            args.temperature,\n        ])\n\n    # Save converted static graph model\n    paddle.jit.save(gpt, os.path.join(args.inference_model_dir, \"gpt\"))\n    logger.info(\"GPT has been saved to {}\".format(args.inference_model_dir))\n\n    gpt.save_resources(tokenizer, args.inference_model_dir)\n\n\nif __name__ == \"__main__\":\n    args = parse_args()\n    pprint(args)\n    do_predict(args)\n", "path": "paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py"}], "after_files": [{"content": "# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport sys\nimport os\nimport numpy as np\nfrom attrdict import AttrDict\nimport argparse\nimport time\n\nimport paddle\n\nimport yaml\nfrom pprint import pprint\n\nfrom paddlenlp.ops import FasterGPT\nfrom paddlenlp.transformers import GPTLMHeadModel\nfrom paddlenlp.transformers import GPTChineseTokenizer, GPTTokenizer\n\nfrom paddlenlp.utils.log import logger\n\nMODEL_CLASSES = {\n    \"gpt-cpm-large-cn\": (GPTLMHeadModel, GPTChineseTokenizer),\n    \"gpt2-medium-en\": (GPTLMHeadModel, GPTTokenizer),\n}\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\n        \"--model_name_or_path\",\n        default=\"gpt2-medium-en\",\n        type=str,\n        help=\"The model name to specify the gpt to use. Can be one of ['gpt2-en', 'gpt2-medium-en', 'gpt-cpm-large-cn']. \"\n    )\n    parser.add_argument(\n        \"--decoding_lib\",\n        default=\"../../build/lib/libdecoding_op.so\",\n        type=str,\n        help=\"Path of libdecoding_op.so. \")\n    parser.add_argument(\n        \"--inference_model_dir\",\n        default=\"./infer_model/\",\n        type=str,\n        help=\"Path to save inference model of gpt. \")\n    parser.add_argument(\n        \"--topk\",\n        default=4,\n        type=int,\n        help=\"The number of candidate to procedure beam search. \")\n    parser.add_argument(\n        \"--topp\",\n        default=0.0,\n        type=float,\n        help=\"The probability threshold to procedure topp sampling. \")\n    parser.add_argument(\n        \"--max_out_len\", default=32, type=int, help=\"Maximum output length. \")\n    parser.add_argument(\n        \"--temperature\",\n        default=1.0,\n        type=float,\n        help=\"The temperature to set. \")\n    parser.add_argument(\n        \"--use_fp16_decoding\",\n        action=\"store_true\",\n        help=\"Whether to use fp16 decoding to predict. \")\n    args = parser.parse_args()\n    return args\n\n\ndef do_predict(args):\n    place = \"gpu\"\n    place = paddle.set_device(place)\n\n    model_class, tokenizer_class = MODEL_CLASSES[args.model_name_or_path]\n    tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path)\n    logger.info('Loading the model parameters, please wait...')\n    model = model_class.from_pretrained(\n        args.model_name_or_path, max_predict_len=args.max_out_len)\n\n    gpt = FasterGPT(\n        model=model,\n        decoding_lib=args.decoding_lib,\n        use_fp16_decoding=args.use_fp16_decoding)\n\n    # Set evaluate mode\n    gpt.eval()\n\n    # Convert dygraph model to static graph model \n    gpt = paddle.jit.to_static(\n        gpt,\n        input_spec=[\n            # input_ids\n            paddle.static.InputSpec(\n                shape=[None, None], dtype=\"int32\"),\n            #\n            # If it's necessarry to provide mem_seq_len and attention_mask,\n            # the parameters should be:\n            # mem_seq_len\n            # paddle.static.InputSpec(shape=[None, None], dtype=\"int32\"),\n            # attention_mask\n            # paddle.static.InputSpec(shape=[None, None, None], dtype=\"float16\" if args.use_fp16_decoding else \"float32\"),\n            #\n            None,  # mem_seq_len\n            None,  # attention_mask\n            args.topk,\n            args.topp,\n            args.max_out_len,\n            tokenizer.eos_token_id,\n            tokenizer.eos_token_id,\n            tokenizer.pad_token_id,\n            None,  # forced_eos_token_id\n            args.temperature,\n        ])\n\n    # Save converted static graph model\n    paddle.jit.save(gpt, os.path.join(args.inference_model_dir, \"gpt\"))\n    logger.info(\"GPT has been saved to {}\".format(args.inference_model_dir))\n\n    gpt.save_resources(tokenizer, args.inference_model_dir)\n\n\nif __name__ == \"__main__\":\n    args = parse_args()\n    pprint(args)\n    do_predict(args)\n", "path": "examples/language_model/gpt/faster_gpt/export_model.py"}, {"content": "# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nimport sys\nimport os\nimport numpy as np\nfrom attrdict import AttrDict\nimport argparse\nimport time\n\nimport paddle\n\nimport yaml\nfrom pprint import pprint\n\nfrom paddlenlp.ops import FasterGPT\nfrom paddlenlp.transformers import GPTLMHeadModel\nfrom paddlenlp.transformers import GPTChineseTokenizer, GPTTokenizer\n\nfrom paddlenlp.utils.log import logger\n\nMODEL_CLASSES = {\n    \"gpt-cpm-large-cn\": (GPTLMHeadModel, GPTChineseTokenizer),\n    \"gpt2-medium-en\": (GPTLMHeadModel, GPTTokenizer),\n}\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser()\n    parser.add_argument(\n        \"--model_name_or_path\",\n        default=\"gpt2-medium-en\",\n        type=str,\n        help=\"The model name to specify the gpt to use. Can be one of ['gpt2-en', 'gpt2-medium-en', 'gpt-cpm-large-cn']. \"\n    )\n    parser.add_argument(\n        \"--decoding_lib\",\n        default=\"../../build/lib/libdecoding_op.so\",\n        type=str,\n        help=\"Path of libdecoding_op.so. \")\n    parser.add_argument(\n        \"--inference_model_dir\",\n        default=\"./infer_model/\",\n        type=str,\n        help=\"Path to save inference model of gpt. \")\n    parser.add_argument(\n        \"--topk\",\n        default=4,\n        type=int,\n        help=\"The number of candidate to procedure beam search. \")\n    parser.add_argument(\n        \"--topp\",\n        default=0.0,\n        type=float,\n        help=\"The probability threshold to procedure topp sampling. \")\n    parser.add_argument(\n        \"--max_out_len\", default=32, type=int, help=\"Maximum output length. \")\n    parser.add_argument(\n        \"--temperature\",\n        default=1.0,\n        type=float,\n        help=\"The temperature to set. \")\n    parser.add_argument(\n        \"--use_fp16_decoding\",\n        action=\"store_true\",\n        help=\"Whether to use fp16 decoding to predict. \")\n    args = parser.parse_args()\n    return args\n\n\ndef do_predict(args):\n    place = \"gpu\"\n    place = paddle.set_device(place)\n\n    model_class, tokenizer_class = MODEL_CLASSES[args.model_name_or_path]\n    tokenizer = tokenizer_class.from_pretrained(args.model_name_or_path)\n    logger.info('Loading the model parameters, please wait...')\n    model = model_class.from_pretrained(\n        args.model_name_or_path, max_predict_len=args.max_out_len)\n\n    gpt = FasterGPT(\n        model=model,\n        decoding_lib=args.decoding_lib,\n        use_fp16_decoding=args.use_fp16_decoding)\n\n    # Set evaluate mode\n    gpt.eval()\n\n    # Convert dygraph model to static graph model \n    gpt = paddle.jit.to_static(\n        gpt,\n        input_spec=[\n            # input_ids\n            paddle.static.InputSpec(\n                shape=[None, None], dtype=\"int32\"),\n            #\n            # If it's necessarry to provide mem_seq_len and attention_mask,\n            # the parameters should be:\n            # mem_seq_len\n            # paddle.static.InputSpec(shape=[None, None], dtype=\"int32\"),\n            # attention_mask\n            # paddle.static.InputSpec(shape=[None, None, None], dtype=\"float16\" if args.use_fp16_decoding else \"float32\"),\n            #\n            None,  # mem_seq_len\n            None,  # attention_mask\n            args.topk,\n            args.topp,\n            args.max_out_len,\n            tokenizer.eos_token_id,\n            tokenizer.eos_token_id,\n            tokenizer.pad_token_id,\n            None,  # forced_eos_token_id\n            args.temperature,\n        ])\n\n    # Save converted static graph model\n    paddle.jit.save(gpt, os.path.join(args.inference_model_dir, \"gpt\"))\n    logger.info(\"GPT has been saved to {}\".format(args.inference_model_dir))\n\n    gpt.save_resources(tokenizer, args.inference_model_dir)\n\n\nif __name__ == \"__main__\":\n    args = parse_args()\n    pprint(args)\n    do_predict(args)\n", "path": "paddlenlp/ops/faster_transformer/sample/gpt_export_model_sample.py"}]}