Amazon SageMaker Jumpstart Text2Text を使用してバッチ変換を実行する 大規模な言語モデルを生成する | アマゾン ウェブ サービス

Amazon SageMaker Jumpstart Text2Text を使用してバッチ変換を実行する 大規模な言語モデルを生成する | アマゾン ウェブ サービス

本日、バッチ変換を実行できるようになったことを発表できることを嬉しく思います。 Amazon SageMaker ジャンプスタート Text2Text 生成用の大規模言語モデル (LLM)。 バッチ変換は、応答をリアルタイムにする必要がない場合に便利で、大規模なデータセットを一括してバッチで推論できます。 バッチ変換の場合、データセットおよび事前トレーニング済みモデルとしてバッチ入力を受け取り、データセット内の各データ ポイントの予測を出力するバッチ ジョブが実行されます。 永続的なハードウェアを備えたリアルタイムのホスト型エンドポイントとは異なり、バッチ変換クラスターはジョブの完了時に破棄されるため、ハードウェアはバッチ ジョブの期間中のみ使用されるため、バッチ変換はコスト効率が高くなります。

一部の使用例では、リアルタイム推論リクエストをバッチ処理用に小さなバッチにグループ化し、リアルタイムまたはほぼリアルタイムの応答を作成できます。 たとえば、低遅延かつ高スループットでデータの連続ストリームを処理する必要がある場合、リクエストごとにリアルタイム エンドポイントを個別に呼び出すと、より多くのリソースが必要となり、処理が逐次的に行われるため、すべてのリクエストの処理に時間がかかる可能性があります。 。 より良いアプローチは、リクエストの一部をグループ化し、バッチ推論モードでリアルタイム エンドポイントを呼び出すことです。これにより、モデルの XNUMX 回の転送パスでリクエストが処理され、リクエストに対する一括応答がリアルタイムまたはほぼリアルタイムで返されます。 。 応答のレイテンシは、グループ化するリクエストの数とインスタンスのメモリ サイズに依存するため、レイテンシとスループットのビジネス要件に応じてバッチ サイズを調整できます。 これを私たちは呼んでいます リアルタイムバッチ推論 これは、リアルタイムの応答を提供しながら、バッチ処理の概念を組み合わせているためです。 リアルタイムのバッチ推論を使用すると、低遅延と高スループットのバランスを実現し、大量のデータをタイムリーかつ効率的に処理できるようになります。

Text2Text 生成モデルのジャンプスタート バッチ変換を使用すると、環境変数を介してバッチ ハイパーパラメータを渡すことができ、スループットをさらに向上させ、待ち時間を最小限に抑えることができます。

JumpStart は、機械学習 (ML) を始めるのに役立つ、さまざまな種類の問題に対応する事前トレーニング済みのオープンソース モデルを提供します。 これらのモデルは、展開前に段階的にトレーニングおよび調整できます。 JumpStart は、一般的なユースケースのインフラストラクチャをセットアップするソリューション テンプレートと、ML 用の実行可能なサンプル ノートブックも提供します。 アマゾンセージメーカー。 事前トレーニングされたモデル、ソリューション テンプレート、サンプルには、次の JumpStart ランディング ページからアクセスできます。 Amazon SageMakerスタジオ。 SageMaker Python SDK を使用して JumpStart モデルにアクセスすることもできます。

この投稿では、最先端の事前トレーニングされたツールの使用方法を示します。 text2text FLAN T5 モデル バッチ変換とリアルタイムのバッチ推論のための Hugging Face から。

ソリューションの概要

事前トレーニングされた Text2Text FLAN T5 モデルのバッチ変換を示すノートブック ハグ顔 以下で利用可能です GitHubリポジトリ。 このノートブックは、Hugging Face のデータを使用しています。 cnn_dailymail SageMaker SDK を使用したテキスト要約タスク用のデータセット。

以下は、バッチ変換とリアルタイム バッチ推論を実装するための重要な手順です。

  1. 前提条件を設定します。
  2. 事前トレーニングされたモデルを選択します。
  3. モデルのアーティファクトを取得します。
  4. バッチ変換ジョブのハイパーパラメータを指定します。
  5. バッチ変換用のデータを準備します。
  6. バッチ変換ジョブを実行します。
  7. を使用して要約を評価します。 RED (要旨評価のための想起指向の代役) スコア。
  8. リアルタイムのバッチ推論を実行します。

前提条件を設定する

ノートブックを実行する前に、いくつかの初期セットアップ手順を完了する必要があります。 SageMaker 実行ロールを設定して、ユーザーに代わって AWS サービスを実行する権限を付与しましょう。

sagemaker_session = Session()
aws_role = sagemaker_session.get_caller_identity_arn()
aws_region = boto3.Session().region_name
sess = sagemaker.Session()

事前トレーニングされたモデルを選択します

デフォルトのモデルとして、huggingface-text2text-flan-t5-large モデルを使用します。 オプションで、JumpStart で利用可能な Text2Text モデルのリストを取得し、好みのモデルを選択できます。 この方法では、同じノートブックを使用して異なるモデル ID を選択する簡単な方法が提供されます。 デモンストレーションの目的で、huggingface-text2text-flan-t5-large モデルを使用します。

model_id, model_version, = ( "huggingface-text2text-flan-t5-large", "*",
)

モデルのアーティファクトを取得する

SageMaker を使用すると、新しいデータセットで最初にモデルを微調整しなくても、事前トレーニングされたモデルで推論を実行できます。 まずは、 deploy_image_uri, deploy_source_uri, model_uri 事前トレーニングされたモデルの場合:

inference_instance_type = "ml.p3.2xlarge" # Retrieve the inference docker container uri. This is the base HuggingFace container image for the default model above.
deploy_image_uri = image_uris.retrieve(
region=None,
framework=None, # automatically inferred from model_id
image_scope="inference",
model_id=model_id,
model_version=model_version,
instance_type=inference_instance_type,
) # Retrieve the model uri.
model_uri = model_uris.retrieve(
model_id=model_id, model_version=model_version, model_scope="inference"
) #Create the SageMaker model instance
model = Model(
image_uri=deploy_image_uri,
model_data=model_uri,
role=aws_role,
predictor_cls=Predictor)

バッチ変換ジョブのハイパーパラメータを指定する

ハイパーパラメータの任意のサブセットを環境変数としてバッチ変換ジョブに渡すことができます。 これらのハイパーパラメータを JSON ペイロードで渡すこともできます。 ただし、次のコードに示すようにハイパーパラメーターの環境変数を設定している場合、JSON 行ペイロード内の個々の例の高度なハイパーパラメーターは使用されません。 ペイロードからハイパーパラメータを使用したい場合は、 hyper_params_dict 代わりにパラメータを null として使用します。

#Specify the Batch Job Hyper Params Here, If you want to treate each example hyperparameters different please pass hyper_params_dict as None
hyper_params = {"batch_size":4, "max_length":50, "top_k": 50, "top_p": 0.95, "do_sample": True}
hyper_params_dict = {"HYPER_PARAMS":str(hyper_params)}

バッチ変換用のデータを準備する

これで、ロードする準備ができました。 cnn_dailymail ハグフェイスからのデータセット:

cnn_test = load_dataset('cnn_dailymail','3.0.0',split='test')

各データ入力を確認し、必要な形式で入力データを作成します。 私たちは articles.jsonl ファイルは、入力ペイロードとして要約する必要がある記事を含むテスト データ ファイルとして保存されます。 このファイルを作成するときに、プロンプトを追加します。 "Briefly summarize this text:" 各テスト入力行に。 テスト入力ごとに異なるハイパーパラメーターを使用する場合は、データセットの作成の一部としてそれらのハイパーパラメーターを追加できます。

つくる highlights.jsonl テスト ファイルに保存されている各記事のハイライトを含むグラウンド トゥルース ファイルとして articles.jsonl。 両方のテスト ファイルを Amazon シンプル ストレージ サービス (Amazon S3) バケット。 次のコードを参照してください。

#You can specify a prompt here
prompt = "Briefly summarize this text: "
#Provide the test data and the ground truth file name
test_data_file_name = "articles.jsonl"
test_reference_file_name = 'highlights.jsonl' test_articles = []
test_highlights =[] # We will go over each data entry and create the data in the input required format as described above
for id, test_entry in enumerate(cnn_test): article = test_entry['article'] highlights = test_entry['highlights'] # Create a payload like this if you want to have different hyperparameters for each test input # payload = {"id": id,"text_inputs": f"{prompt}{article}", "max_length": 100, "temperature": 0.95} # Note that if you specify hyperparameter for each payload individually, you may want to ensure that hyper_params_dict is set to None instead payload = {"id": id,"text_inputs": f"{prompt}{article}"} test_articles.append(payload) test_highlights.append({"id":id, "highlights": highlights}) with open(test_data_file_name, "w") as outfile: for entry in test_articles: outfile.write("%sn" % json.dumps(entry)) with open(test_reference_file_name, "w") as outfile: for entry in test_highlights: outfile.write("%sn" % json.dumps(entry)) # Uploading the data s3 = boto3.client("s3")
s3.upload_file(test_data_file_name, output_bucket, os.path.join(output_prefix + "/batch_input/articles.jsonl"))

バッチ変換ジョブを実行する

バッチ変換ジョブを開始すると、SageMaker は、選択したインスタンス タイプに応じて CPU または GPU インスタンスを含む、データを処理するために必要なコンピューティング リソースを起動します。 バッチ変換ジョブ中に、SageMaker は、インスタンス、ストレージ、ネットワーク リソースなど、データの処理に必要なコンピューティング リソースを自動的にプロビジョニングおよび管理します。 バッチ変換ジョブが完了すると、コンピューティング リソースは SageMaker によって自動的にクリーンアップされます。 これは、ジョブ中に使用されるインスタンスとストレージが停止および削除され、リソースが解放され、コストが最小限に抑えられることを意味します。 次のコードを参照してください。

# Creating the Batch transformer object
batch_transformer = model.transformer( instance_count=1, instance_type=inference_instance_type, output_path=s3_output_data_path, assemble_with="Line", accept="text/csv", max_payload=1, env = hyper_params_dict
) # Making the predications on the input data
batch_transformer.transform(s3_input_data_path, content_type="application/jsonlines", split_type="Line") batch_transformer.wait()

以下は、 articles.jsonl テストファイル。 このファイル内のレコードには、次の ID と一致する ID があることに注意してください。 predict.jsonl Hugging Face Text2Text モデルからの出力として要約されたレコードを示すファイル レコード。 同様に、グラウンド トゥルース ファイルにも、データ レコードに一致する ID があります。 テスト ファイル、グラウンド トゥルース ファイル、および出力ファイル全体で ID が一致するため、入力レコードと出力レコードをリンクして、結果を簡単に解釈できます。

以下は、要約のために提供される入力レコードの例です。

{"id": 0, "text_inputs": "Briefly summarize this text: (CNN)The Palestinian Authority officially became the 123rd member of the International Criminal Court on Wednesday, a step that gives the court jurisdiction over alleged crimes in Palestinian territories. The formal accession was marked with a ceremony at The Hague, in the Netherlands, where the court is based. The Palestinians signed the ICC's founding Rome Statute in January, when they also accepted its jurisdiction over alleged crimes committed "in the occupied Palestinian territory, including East Jerusalem, since June 13, 2014." Later that month, the ICC opened a preliminary examination into the situation in Palestinian territories, paving the way for possible war crimes investigations against Israelis. As members of the court, Palestinians may be subject to counter-charges as well. Israel and the United States, neither of which is an ICC member, opposed the Palestinians' efforts to join the body. But Palestinian Foreign Minister Riad al-Malki, speaking at Wednesday's ceremony, said it was a move toward greater justice. "As Palestine formally becomes a State Party to the Rome Statute today, the world is also a step closer to ending a long era of impunity and injustice," he said, according to an ICC news release. "Indeed, today brings us closer to our shared goals of justice and peace." Judge Kuniko Ozaki, a vice president of the ICC, said acceding to the treaty was just the first step for the Palestinians. "As the Rome Statute today enters into force for the State of Palestine, Palestine acquires all the rights as well as responsibilities that come with being a State Party to the Statute. These are substantive commitments, which cannot be taken lightly," she said. Rights group Human Rights Watch welcomed the development. "Governments seeking to penalize Palestine for joining the ICC should immediately end their pressure, and countries that support universal acceptance of the court's treaty should speak out to welcome its membership," said Balkees Jarrah, international justice counsel for the group. "What's objectionable is the attempts to undermine international justice, not Palestine's decision to join a treaty to which over 100 countries around the world are members." In January, when the preliminary ICC examination was opened, Israeli Prime Minister Benjamin Netanyahu described it as an outrage, saying the court was overstepping its boundaries. The United States also said it "strongly" disagreed with the court's decision. "As we have said repeatedly, we do not believe that Palestine is a state and therefore we do not believe that it is eligible to join the ICC," the State Department said in a statement. It urged the warring sides to resolve their differences through direct negotiations. "We will continue to oppose actions against Israel at the ICC as counterproductive to the cause of peace," it said. But the ICC begs to differ with the definition of a state for its purposes and refers to the territories as "Palestine." While a preliminary examination is not a formal investigation, it allows the court to review evidence and determine whether to investigate suspects on both sides. Prosecutor Fatou Bensouda said her office would "conduct its analysis in full independence and impartiality." The war between Israel and Hamas militants in Gaza last summer left more than 2,000 people dead. The inquiry will include alleged war crimes committed since June. The International Criminal Court was set up in 2002 to prosecute genocide, crimes against humanity and war crimes. CNN's Vasco Cotovio, Kareem Khadder and Faith Karimi contributed to this report."}

以下は、要約を含む予測出力です。

{'id': 0, 'generated_texts': ['The Palestinian Authority officially became a member of the International Criminal Court on Wednesday, a step that gives the court jurisdiction over alleged crimes in Palestinian territories.']}

以下は、モデル評価を目的としたグラウンド トゥルースの要約です。

{"id": 0, "highlights": "Membership gives the ICC jurisdiction over alleged crimes committed in Palestinian territories since last June .nIsrael and the United States opposed the move, which could open the door to war crimes investigations against Israelis ."}

次に、モデルの評価にグランド トゥルースと予測出力を使用します。

ROUGE スコアを使用してモデルを評価する¶

RED、または要約評価のための再現指向アンダースタディは、自然言語処理における自動要約と機械翻訳を評価するために使用される一連のメトリクスとソフトウェア パッケージです。 この指標は、自動的に生成された要約または翻訳を、参照 (人間が作成した) 要約または翻訳、または一連の参照と比較します。

次のコードでは、予測された概要と元の概要を共通キーで結合することによって結合します。 id これを使用して ROUGE スコアを計算します。

# Downloading the predictions
s3.download_file(
output_bucket, output_prefix + "/batch_output/" + "articles.jsonl.out", "predict.jsonl"
) with open('predict.jsonl', 'r') as json_file:
json_list = list(json_file) # Creating the prediction list for the dataframe
predict_dict_list = []
for predict in json_list:
if len(predict) > 1:
predict_dict = ast.literal_eval(predict)
predict_dict_req = {"id": predict_dict["id"], "prediction": predict_dict["generated_texts"][0]}
predict_dict_list.append(predict_dict_req) # Creating the predictions dataframe
predict_df = pd.DataFrame(predict_dict_list) test_highlights_df = pd.DataFrame(test_highlights) # Combining the predict dataframe with the original summarization on id to compute the rouge score
df_merge = test_highlights_df.merge(predict_df, on="id", how="left") rouge = evaluate.load('rouge')
results = rouge.compute(predictions=list(df_merge["prediction"]),references=list(df_merge["highlights"]))
print(results)
{'rouge1': 0.32749078992945646, 'rouge2': 0.126038645005132, 'rougeL': 0.22764277967933363, 'rougeLsum': 0.28162915746368966}

リアルタイムのバッチ推論を実行する

次に、入力をリストとして提供することにより、エンドポイントでリアルタイムのバッチ推論を実行する方法を示します。 テスト データセットからいくつかのレコードを取得し、それらを使用してリアルタイム エンドポイントを呼び出す点を除いて、前と同じモデル ID とデータセットを使用します。

次のコードは、リアルタイム バッチ推論用のリアルタイム エンドポイントを作成してデプロイする方法を示しています。

from sagemaker.utils import name_from_base
endpoint_name = name_from_base(f"jumpstart-example-{model_id}")
# deploy the Model. Note that we need to pass Predictor class when we deploy model through Model class,
# for being able to run inference through the sagemaker API.
model_predictor = model.deploy( initial_instance_count=1, instance_type=inference_instance_type, predictor_cls=Predictor, endpoint_name=endpoint_name
)

次に、入力ペイロードを準備します。 このために、以前に準備したデータを使用し、最初の 10 個のテスト入力を抽出し、使用するハイパーパラメーターを含むテキスト入力を追加します。 このペイロードをリアルタイムに提供します invoke_endpoint. その後、応答ペイロードが応答のリストとして返されます。 次のコードを参照してください。

#Provide all the text inputs to the model as a list
text_inputs = [entry["text_inputs"] for entry in test_articles[0:10]] # The information about the different Parameters is provided above
payload = { "text_inputs": text_inputs, "max_length": 50, "num_return_sequences": 1, "top_k": 50, "top_p": 0.95, "do_sample": True, "batch_size": 4
} def query_endpoint_with_json_payload(encoded_json, endpoint_name):
client = boto3.client("runtime.sagemaker")
response = client.invoke_endpoint(
EndpointName=endpoint_name, ContentType="application/json", Body=encoded_json
)
return response query_response = query_endpoint_with_json_payload(
json.dumps(payload).encode("utf-8"), endpoint_name=endpoint_name
) def parse_response_multiple_texts(query_response):
model_predictions = json.loads(query_response["Body"].read())
return model_predictions generated_text_list = parse_response_multiple_texts(query_response)
print(*generated_text_list, sep='n')

クリーンアップ

エンドポイントをテストしたら、SageMaker 推論エンドポイントを削除し、モデルを削除して、料金が発生しないようにしてください。

まとめ

このノートブックでは、要約タスク用の Hugging Face Text2Text Generator モデルを紹介するためにバッチ変換を実行しました。 バッチ変換は、永続的なエンドポイントを必要とせずに大規模なデータセットから推論を取得する場合に有利です。 結果の解釈を支援するために、入力レコードを推論とリンクしました。 ROUGE スコアを使用して、テスト データの要約とモデル生成の要約を比較しました。

さらに、リアルタイム バッチ推論もデモしました。これにより、小さなバッチのデータをリアルタイム エンドポイントに送信して、ストリーミング入力データなどのシナリオでレイテンシーとスループットのバランスを実現できます。 リアルタイムのバッチ推論は、リアルタイム リクエストのスループットの向上に役立ちます。

今すぐ SageMaker の Text2Text 生成モデルを使用したバッチ変換を試して、フィードバックをお聞かせください。


著者について

Amazon SageMaker Jumpstart Text2Text を使用してバッチ変換を実行する 大規模な言語モデルを生成する |アマゾン ウェブ サービス PlatoBlockchain データ インテリジェンス。垂直検索。あい。ヘマントシン Amazon SageMaker JumpStart および Amazon SageMaker 組み込みアルゴリズムの経験を持つ機械学習エンジニアです。 彼はクーラント数理科学研究所で修士号を取得し、デリー工科大学で学士号を取得しました。 彼は、自然言語処理、コンピューター ビジョン、時系列分析の分野でさまざまな機械学習の問題に取り組んだ経験があります。

Amazon SageMaker Jumpstart Text2Text を使用してバッチ変換を実行する 大規模な言語モデルを生成する |アマゾン ウェブ サービス PlatoBlockchain データ インテリジェンス。垂直検索。あい。ラクナ チャダ は、AW​​S の戦略アカウントの AI/ML 担当プリンシパル ソリューション アーキテクトです。 Rachna は楽観主義者で、AI を倫理的かつ責任を持って使用することで将来の社会を改善し、経済的および社会的繁栄をもたらすことができると信じています。 余暇には、家族と一緒に時間を過ごしたり、ハイキングをしたり、音楽を聴いたりするのが好きです。

Amazon SageMaker Jumpstart Text2Text を使用してバッチ変換を実行する 大規模な言語モデルを生成する |アマゾン ウェブ サービス PlatoBlockchain データ インテリジェンス。垂直検索。あい。アシッシュ・ケタン博士 は、Amazon SageMaker 組み込みアルゴリズムを使用する上級応用科学者であり、機械学習アルゴリズムの開発を支援しています。 イリノイ大学アーバナシャンペーン校で博士号を取得。 彼は機械学習と統計的推論の活発な研究者であり、NeurIPS、ICML、ICLR、JMLR、ACL、および EMNLP カンファレンスで多くの論文を発表しています。

タイムスタンプ:

より多くの AWS機械学習