C/C++教程

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

本文主要是介绍使用预训练语言模型预测阶段: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

这篇关于使用预训练语言模型预测阶段:GPU、CPU性能差别【Pegasus】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!