使用预训练语言模型预测阶段:GPU、CPU性能差别【Pegasus】

一、Pegasus

1、使用CPU(用时: 17.92682433128357 秒)

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/pegasus/modeling_pegasus.py
import time

import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

device = torch.device("cuda")

tokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')

text = """
         (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer.encode(text)
inputs = torch.tensor([inputs])

print('inputs = ', inputs)

time01 = time.time()
summary_ids = model.generate(inputs)
time02 = time.time()

print("\n用时:", time02 - time01, " 秒")

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

inputs =  tensor([[  143, 40155,   158,   581,   109,   453,   166,   333,   169, 95987,
           108, 11481,  7756,   148,  1487,   114,   177,   456,   113, 35712,
           111, 66941,   116,   323,   112,   460, 30726,   116,  1315,   111,
           157,   331,   135,   149,   204,   109,   278,   107, 11481,  7756,
           243,  1342,   120,   178,   192,  1137,   114,   988,   113, 30726,
           116,   124,  1538,  1265,   198, 35871,   162,   125,   138,   442,
           738,   177, 18345,   170,   108,   792,   135,  1428,  1105,   135,
           290, 10156,   108, 14451,   109,   115,  8597, 32478,  1784,   317,
           109,  1887,   113,  6807,   111,   109,   970, 24353,   799,   115,
           109,   278,   745,   992,   112, 20525,  4474,   107,   351, 30726,
           116,   127,   329,   356,   262,   157,   323,   109,  4104,   115,
           109,  1588,   111,   163, 14094,   109,   352, 32577,   108, 11869,
          4244, 20525, 18672,  1084,  1054,   107,  6611,   243,   107,   322,
           127,  1254,  3795,   112,   130,   109, 54407,   113,   109,  4569,
          1887,   107,   139,   177, 30726,   116,   331,   135,  1105,   253,
           130, 16958,   108,   351,  3571,   111, 14838,   107,   198,   287,
           117,   114, 32577,   170,   221,   249,  1728,   112,  1111,   165,
           112,   200,   124,   109, 11691,   108,   111,   119,  2312,   236,
           120,   115,   136,   323,   745,  6611,   243,   107,   198,   417,
           131,   216,  1767,   160, 30726,   116,   135,  2222, 10912,  1262,
           108,   172,  5365, 23288,   108,   109,  3755,  2273,   113, 43439,
           108, 14668,   108,  6398,   108, 32671,   496,   343,   118,   109,
           453,   166,   381,  7756,   131,  2974,   108,   220,  3361,   266,
           109,   467,   107,   198, 59883,   131,  2293,   117,   221,   786,
           151,   285,  1728,   112,   275,   112,   109, 12483, 26941, 30713,
          3317,   880,   197,  1262,   120,   127,   506,   349,   121, 22564,
           122, 30726,   116,   745,  6611,   243,   107,  8751,  5706,  1418,
           497,   108,   114,  4609,   113,  1588,   689,   134, 69328,   502,
           115,   351,  3477,   108,  3151,   120,  7756,  1487,   169,   177,
         11598,   113, 30726,   116,   124,   109,  4569, 26717,   113,   109,
         60574,   108,   162, 56784,   109,   558,   113,   109, 33806,   112,
          1694,   131, 25910,   115, 26163,   107,   198,  1189, 11733,   113,
           339,  5509,  1024,   135,   571,   429,   108,   109, 11481,   131,
           116,  2257,   118, 30726,   416,   120,   290,   391,  1588,  8068,
           114,   295,   134,   109,   461,   826,   496,   222,   176,   989,
           108,  7756,  1728,   114,   154, 24500,  1588,   111,  1728,   112,
          1232,  6243,   675,   135,   360,  1724,   120,  2051,   571,   135,
         52403,   131,   116,   484,  3853,   108,  5706,  1418,   497,   243,
           107,   485,   591,   131,   144,  1021,  7756,   117,   109,   211,
           110, 39619, 18827,   112, 17717, 30726,   116,   135,   109,  1690,
           278,   108,   577,   107, 16591,   115,   109,  8821,   116,   108,
           142,  2186,   344,   113,  5249,   655,  1588,  3635,   195,  1729,
         30726,   116,   108,   111,   115,   109,  6939,   116,   108,   873,
           107,  1084, 61939, 12964,   108,  2901,  7756, 24828,  3792,   289,
           232,   108,  4486,   109,   211, 30726,   116,   135,  2466,   108,
           109,  6802,   111,  1922,   107,   222,   663,   112,   109,   738,
           177, 30726,   116,  7756,  1729,   124,  1342,   108,   668,  5774,
         66941,   116,   111, 35712,   138,   163,   129,  7051,   130, 30726,
           116,   107,  2882,   232,   108, 11481,  7756,  4486,  1925,   177,
         30726,   116,   108,   330, 35712,   135, 17256,   111, 58499, 55600,
           107, 11869,   131,   116,  4767, 18834,   111,  2333, 65534, 15391,
         28929,  5674,   112,   136,   731,   107,     1]])

用时: 17.92682433128357  秒

summary_ids =  tensor([[    0,   139,   177, 30726,   116,   331,   135,  1105,   253,   130,
         16958,   108,   351,  3571,   111, 14838,   110,   107,   106,  1667,
          3361,   266,   109,   467,   118,   109,   453,   166,   381,  7756,
           131,  2974,   110,   107,     1]])
2022-02-21 12:44:10.593808: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]

Process finished with exit code 0

2、使用GPU(用时: 1.5299088954925537 秒)

# https://github.com/huggingface/transformers/blob/master/src/transformers/models/pegasus/modeling_pegasus.py
import time

import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

device = torch.device("cuda")

tokenizer = AutoTokenizer.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail')
model = AutoModelForSeq2SeqLM.from_pretrained(r'D:\Pretrained_Model\pegasus-cnn_dailymail').to(device)

text = """
         (CNN)For the second time during his papacy, Pope Francis has announced a new group of bishops and archbishops set to become cardinals -- and they come from all over the world.
        Pope Francis said Sunday that he would hold a meeting of cardinals on February 14 "during which I will name 15 new Cardinals who, coming from 13 countries from every continent, manifest the indissoluble links between the Church of Rome and the particular Churches present in the world," according to Vatican Radio.
        New cardinals are always important because they set the tone in the church and also elect the next pope, CNN Senior Vatican Analyst John L. Allen said. They are sometimes referred to as the princes of the Catholic Church.
        The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar.
        "This is a pope who very much wants to reach out to people on the margins, and you clearly see that in this set," Allen said. "You're talking about cardinals from typically overlooked places, like Cape Verde, the Pacific island of Tonga, Panama, Thailand, Uruguay."
        But for the second time since Francis' election, no Americans made the list.
        "Francis' pattern is very clear: He wants to go to the geographical peripheries rather than places that are already top-heavy with cardinals," Allen said.
        Christopher Bellitto, a professor of church history at Kean University in New Jersey, noted that Francis announced his new slate of cardinals on the Catholic Feast of the Epiphany, which commemorates the visit of the Magi to Jesus' birthplace in Bethlehem.
        "On feast of three wise men from far away, the Pope's choices for cardinal say that every local church deserves a place at the big table."
        In other words, Francis wants a more decentralized church and wants to hear reform ideas from small communities that sit far from Catholicism's power centers, Bellitto said.
        That doesn't mean Francis is the first pontiff to appoint cardinals from the developing world, though. Beginning in the 1920s, an increasing number of Latin American churchmen were named cardinals, and in the 1960s, St. John XXIII, whom Francis canonized last year, appointed the first cardinals from Japan, the Philippines and Africa.
        In addition to the 15 new cardinals Francis named on Sunday, five retired archbishops and bishops will also be honored as cardinals.
        Last year, Pope Francis appointed 19 new cardinals, including bishops from Haiti and Burkina Faso.
        CNN's Daniel Burke and Christabelle Fombu contributed to this report.
"""
# CNN/DM答案:
# @highlight
# The 15 new cardinals will be installed on February 14
# @highlight
# They come from countries such as Myanmar and Tonga
# @highlight
# No Americans made the list this time or the previous time in Francis' papacy

inputs = tokenizer.encode(text)
inputs = torch.tensor([inputs]).to(device)

print('inputs = ', inputs)

time01 = time.time()
summary_ids = model.generate(inputs)
time02 = time.time()

print("\n用时:", time02 - time01, " 秒")

print('\nsummary_ids = ', summary_ids)

print([tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids])
print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False))

打印结果:

inputs =  tensor([[  143, 40155,   158,   581,   109,   453,   166,   333,   169, 95987,
           108, 11481,  7756,   148,  1487,   114,   177,   456,   113, 35712,
           111, 66941,   116,   323,   112,   460, 30726,   116,  1315,   111,
           157,   331,   135,   149,   204,   109,   278,   107, 11481,  7756,
           243,  1342,   120,   178,   192,  1137,   114,   988,   113, 30726,
           116,   124,  1538,  1265,   198, 35871,   162,   125,   138,   442,
           738,   177, 18345,   170,   108,   792,   135,  1428,  1105,   135,
           290, 10156,   108, 14451,   109,   115,  8597, 32478,  1784,   317,
           109,  1887,   113,  6807,   111,   109,   970, 24353,   799,   115,
           109,   278,   745,   992,   112, 20525,  4474,   107,   351, 30726,
           116,   127,   329,   356,   262,   157,   323,   109,  4104,   115,
           109,  1588,   111,   163, 14094,   109,   352, 32577,   108, 11869,
          4244, 20525, 18672,  1084,  1054,   107,  6611,   243,   107,   322,
           127,  1254,  3795,   112,   130,   109, 54407,   113,   109,  4569,
          1887,   107,   139,   177, 30726,   116,   331,   135,  1105,   253,
           130, 16958,   108,   351,  3571,   111, 14838,   107,   198,   287,
           117,   114, 32577,   170,   221,   249,  1728,   112,  1111,   165,
           112,   200,   124,   109, 11691,   108,   111,   119,  2312,   236,
           120,   115,   136,   323,   745,  6611,   243,   107,   198,   417,
           131,   216,  1767,   160, 30726,   116,   135,  2222, 10912,  1262,
           108,   172,  5365, 23288,   108,   109,  3755,  2273,   113, 43439,
           108, 14668,   108,  6398,   108, 32671,   496,   343,   118,   109,
           453,   166,   381,  7756,   131,  2974,   108,   220,  3361,   266,
           109,   467,   107,   198, 59883,   131,  2293,   117,   221,   786,
           151,   285,  1728,   112,   275,   112,   109, 12483, 26941, 30713,
          3317,   880,   197,  1262,   120,   127,   506,   349,   121, 22564,
           122, 30726,   116,   745,  6611,   243,   107,  8751,  5706,  1418,
           497,   108,   114,  4609,   113,  1588,   689,   134, 69328,   502,
           115,   351,  3477,   108,  3151,   120,  7756,  1487,   169,   177,
         11598,   113, 30726,   116,   124,   109,  4569, 26717,   113,   109,
         60574,   108,   162, 56784,   109,   558,   113,   109, 33806,   112,
          1694,   131, 25910,   115, 26163,   107,   198,  1189, 11733,   113,
           339,  5509,  1024,   135,   571,   429,   108,   109, 11481,   131,
           116,  2257,   118, 30726,   416,   120,   290,   391,  1588,  8068,
           114,   295,   134,   109,   461,   826,   496,   222,   176,   989,
           108,  7756,  1728,   114,   154, 24500,  1588,   111,  1728,   112,
          1232,  6243,   675,   135,   360,  1724,   120,  2051,   571,   135,
         52403,   131,   116,   484,  3853,   108,  5706,  1418,   497,   243,
           107,   485,   591,   131,   144,  1021,  7756,   117,   109,   211,
           110, 39619, 18827,   112, 17717, 30726,   116,   135,   109,  1690,
           278,   108,   577,   107, 16591,   115,   109,  8821,   116,   108,
           142,  2186,   344,   113,  5249,   655,  1588,  3635,   195,  1729,
         30726,   116,   108,   111,   115,   109,  6939,   116,   108,   873,
           107,  1084, 61939, 12964,   108,  2901,  7756, 24828,  3792,   289,
           232,   108,  4486,   109,   211, 30726,   116,   135,  2466,   108,
           109,  6802,   111,  1922,   107,   222,   663,   112,   109,   738,
           177, 30726,   116,  7756,  1729,   124,  1342,   108,   668,  5774,
         66941,   116,   111, 35712,   138,   163,   129,  7051,   130, 30726,
           116,   107,  2882,   232,   108, 11481,  7756,  4486,  1925,   177,
         30726,   116,   108,   330, 35712,   135, 17256,   111, 58499, 55600,
           107, 11869,   131,   116,  4767, 18834,   111,  2333, 65534, 15391,
         28929,  5674,   112,   136,   731,   107,     1]], device='cuda:0')

用时: 1.5299088954925537  秒

summary_ids =  tensor([[    0,   139,   177, 30726,   116,   331,   135,  1105,   253,   130,
         16958,   108,   351,  3571,   111, 14838,   110,   107,   106,  1667,
          3361,   266,   109,   467,   118,   109,   453,   166,   381,  7756,
           131,  2974,   110,   107,     1]], device='cuda:0')
2022-02-21 12:41:13.315942: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]
["The new cardinals come from countries such as Ethiopia, New Zealand and Myanmar .<n>No Americans made the list for the second time since Francis' election ."]

Process finished with exit code 0



参考资料:
Pytorch NLP模型在进行推理时不使用GPU

为了在Windows安装ADB工具,你可以按照以下步骤进行操作: 1. 首先,下载ADB工具包并解压缩到你自定义的安装目录。你可以选择将其解压缩到任何你喜欢的位置。 2. 打开运行窗口,可以通过按下Win+R键来快速打开。在运行窗口中输入"sysdm.cpl"并按下回车键。 3. 在系统属性窗口中,选择"高级"选项卡,然后点击"环境变量"按钮。 4. 在环境变量窗口中,选择"系统变量"部分,并找到名为"Path"的变量。点击"编辑"按钮。 5. 在编辑环境变量窗口中,点击"新建"按钮,并将ADB工具的安装路径添加到新建的路径中。确保路径正确无误后,点击"确定"按钮。 6. 返回到桌面,打开命令提示符窗口。你可以通过按下Win+R键,然后输入"cmd"并按下回车键来快速打开命令提示符窗口。 7. 在命令提示符窗口中,输入"adb version"命令来验证ADB工具是否成功安装。如果显示版本信息,则表示安装成功。 这样,你就成功在Windows安装ADB工具。你可以使用ADB工具来执行各种操作,如枚举设备、进入/退出ADB终端、文件传输、运行命令、查看系统日志等。具体的操作方法可以参考ADB工具的官方文档或其他相关教程。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [windows环境安装adb驱动](https://blog.csdn.net/zx54633089/article/details/128533343)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Windows安装使用ADB简单易懂教程](https://blog.csdn.net/m0_37777700/article/details/129836351)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值