قم بإجراء تحويلات مجمعة باستخدام نماذج اللغات الكبيرة من Amazon SageMaker Jumpstart Text2Text Generation | خدمات أمازون ويب

قم بإجراء تحويلات مجمعة باستخدام نماذج اللغات الكبيرة من Amazon SageMaker Jumpstart Text2Text Generation | خدمات أمازون ويب

يسعدنا اليوم أن نعلن أنه يمكنك الآن إجراء تحويلات مجمعة باستخدام أمازون سيج ميكر جومب ستارت نماذج اللغات الكبيرة (LLMs) لتوليد النص 2Text. تعد التحويلات المجمعة مفيدة في المواقف التي لا تحتاج فيها الاستجابات إلى الوقت الفعلي ، وبالتالي يمكنك القيام بالاستدلال دفعة واحدة لمجموعات البيانات الكبيرة بشكل مجمّع. بالنسبة إلى تحويل الدُفعات ، يتم تشغيل وظيفة المجموعة التي تأخذ إدخال الدُفعات كمجموعة بيانات ونموذج مُدرَّب مسبقًا ، وتخرج التنبؤات لكل نقطة بيانات في مجموعة البيانات. يعتبر تحويل الدُفعات فعالاً من حيث التكلفة لأنه على عكس نقاط النهاية المستضافة في الوقت الفعلي والتي تحتوي على أجهزة ثابتة ، فإن مجموعات تحويل الدُفعات يتم هدمها عند اكتمال المهمة ، وبالتالي يتم استخدام الأجهزة فقط لمدة المهمة المجمعة.

في بعض حالات الاستخدام ، يمكن تجميع طلبات الاستدلال في الوقت الفعلي في مجموعات صغيرة لمعالجة الدُفعات لإنشاء استجابات في الوقت الفعلي أو شبه في الوقت الفعلي. على سبيل المثال ، إذا كنت بحاجة إلى معالجة دفق مستمر من البيانات بزمن انتقال منخفض وإنتاجية عالية ، فإن استدعاء نقطة نهاية في الوقت الفعلي لكل طلب على حدة سيتطلب المزيد من الموارد ويمكن أن يستغرق وقتًا أطول لمعالجة جميع الطلبات لأن المعالجة تتم بشكل تسلسلي . تتمثل الطريقة الأفضل في تجميع بعض الطلبات واستدعاء نقطة النهاية في الوقت الفعلي في وضع الاستدلال الدفعي ، والذي يعالج طلباتك في مسار أمامي واحد للنموذج ويعيد الاستجابة المجمّعة للطلب في الوقت الفعلي أو في الوقت الفعلي تقريبًا . سيعتمد زمن انتقال الاستجابة على عدد الطلبات التي تقوم بتجميعها معًا وحجم ذاكرة المثيل ، وبالتالي يمكنك ضبط حجم الدُفعة وفقًا لمتطلبات عملك فيما يتعلق بزمن الانتقال والإنتاجية. نسمي هذا استدلال دفعة في الوقت الحقيقي لأنه يجمع بين مفهوم التجميع مع الاستمرار في تقديم ردود في الوقت الفعلي. باستخدام الاستدلال الدفعي في الوقت الفعلي ، يمكنك تحقيق توازن بين زمن الوصول المنخفض والإنتاجية العالية ، مما يتيح لك معالجة كميات كبيرة من البيانات في الوقت المناسب وبطريقة فعالة.

يسمح لك تحويل دفعة Jumpstart لنماذج Text2Text Generation بتمرير المعلمات التشعبية للدفعة من خلال متغيرات البيئة التي تزيد من الإنتاجية وتقليل زمن الوصول.

يوفر JumpStart نماذج مفتوحة المصدر محددة مسبقًا لمجموعة كبيرة من أنواع المشكلات لمساعدتك على البدء في التعلم الآلي (ML). يمكنك تدريب هذه النماذج وضبطها بشكل متزايد قبل النشر. يوفر JumpStart أيضًا قوالب الحلول التي تقوم بإعداد البنية التحتية لحالات الاستخدام الشائعة ، وأمثلة من دفاتر الملاحظات القابلة للتنفيذ لـ ML مع الأمازون SageMaker. يمكنك الوصول إلى النماذج المدربة مسبقًا وقوالب الحلول والأمثلة من خلال صفحة JumpStart المقصودة في أمازون ساجميكر ستوديو. يمكنك أيضًا الوصول إلى نماذج JumpStart باستخدام SageMaker Python SDK.

في هذا المنشور ، نوضح كيفية استخدام أحدث ما تم تدريبه مسبقًا نماذج text2text FLAN T5 من Hugging Face لتحويل الدُفعات واستدلال الدُفعات في الوقت الفعلي.

حل نظرة عامة

يظهر الكمبيوتر المحمول تحويل دفعة لنماذج Text2Text FLAN T5 المدربة مسبقًا من وجه يعانق متوفر في ما يلي مستودع جيثب. يستخدم هذا الكمبيوتر الدفتري بيانات من الوجه المعانق cnn_dailymail مجموعة بيانات لمهمة تلخيص النص باستخدام SageMaker SDK.

فيما يلي الخطوات الأساسية لتنفيذ تحويل الدُفعات واستدلال الدُفعات في الوقت الفعلي:

  1. قم بإعداد المتطلبات الأساسية.
  2. حدد نموذجًا تم تدريبه مسبقًا.
  3. استرجاع القطع الأثرية للنموذج.
  4. حدد المعلمات التشعبية لوظيفة تحويل الدُفعات.
  5. تحضير البيانات لتحويل الدُفعة.
  6. قم بتشغيل وظيفة تحويل الدُفعات.
  7. قم بتقييم التلخيص باستخدام a ROUGE درجة (الاستدعاء المنحى لتقييم التلاعب).
  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 كنموذج افتراضي. اختياريًا ، يمكنك استرداد قائمة نماذج Text2Text المتاحة على JumpStart واختيار الطراز المفضل لديك. توفر هذه الطريقة طريقة مباشرة لتحديد معرفات طرز مختلفة باستخدام نفس الكمبيوتر الدفتري. لأغراض التوضيح ، نستخدم نموذج 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 المعلمة فارغة بدلاً من ذلك.

#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 مجموعة بيانات من Hugging Face:

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

ننتقل إلى كل إدخال بيانات وننشئ بيانات الإدخال بالتنسيق المطلوب. نقوم بإنشاء ملف articles.jsonl ملف كملف بيانات اختبار يحتوي على مقالات تحتاج إلى تلخيصها على أنها حمولة إدخال. أثناء إنشاء هذا الملف ، نلحق الموجه "Briefly summarize this text:" لكل صف إدخال اختبار. إذا كنت تريد الحصول على معلمات تشعبية مختلفة لكل إدخال اختبار ، فيمكنك إلحاق هذه المعلمات التشعبية كجزء من إنشاء مجموعة البيانات.

نخلق highlights.jsonl كملف الحقيقة الأساسي الذي يحتوي على نقاط بارزة لكل مقالة مخزنة في ملف الاختبار articles.jsonl. نقوم بتخزين كلا ملفي الاختبار في ملف خدمة تخزين أمازون البسيطة (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 بتشغيل موارد الحوسبة اللازمة لمعالجة البيانات ، بما في ذلك مثيلات وحدة المعالجة المركزية أو وحدة معالجة الرسومات اعتمادًا على نوع المثيل المحدد. أثناء وظيفة التحويل الدفعي ، يقوم 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 ملف الاختبار. لاحظ أن السجل في هذا الملف له معرّف يتطابق مع predict.jsonl سجلات الملفات التي تعرض سجلًا ملخصًا كإخراج من نموذج نص 2Text المعانقة للوجه. وبالمثل ، يحتوي ملف الحقيقة الأساسي أيضًا على معرّف مطابق لسجل البيانات. يسمح معرف المطابقة عبر ملف الاختبار وملف الحقيقة الأساسية وملف الإخراج بربط سجلات الإدخال بسجلات الإخراج لتسهيل تفسير النتائج.

فيما يلي مثال على سجل الإدخال المقدم للتلخيص:

{"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¶

ROUGE، أو الاستدلال الموجه نحو الاسترجاع لتقييم Gisting ، عبارة عن مجموعة من المقاييس وحزمة البرامج المستخدمة لتقييم التلخيص التلقائي والترجمة الآلية في معالجة اللغة الطبيعية. تقارن المقاييس بين ملخص أو ترجمة يتم إنتاجها تلقائيًا مقابل ملخص مرجعي (من إنتاج الإنسان) أو ترجمة أو مجموعة من المراجع.

في الكود التالي ، نقوم بدمج الملخصات المتوقعة والأصلية من خلال ضمها إلى المفتاح المشترك 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}

إجراء استدلال دفعي في الوقت الفعلي

بعد ذلك ، نوضح لك كيفية تشغيل الاستدلال الدفعي في الوقت الفعلي على نقطة النهاية من خلال توفير المدخلات كقائمة. نستخدم نفس معرف النموذج ومجموعة البيانات كما في السابق ، باستثناء أننا نأخذ بعض السجلات من مجموعة بيانات الاختبار ونستخدمها لاستدعاء نقطة نهاية في الوقت الفعلي.

يوضح الكود التالي كيفية إنشاء ونشر نقطة نهاية في الوقت الفعلي للاستدلال الدفعي في الوقت الفعلي:

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 لمقارنة تلخيص بيانات الاختبار بالتلخيص الناتج عن النموذج.

بالإضافة إلى ذلك ، أظهرنا استدلال الدُفعات في الوقت الفعلي ، حيث يمكنك إرسال مجموعة صغيرة من البيانات إلى نقطة نهاية في الوقت الفعلي لتحقيق التوازن بين زمن الوصول والإنتاجية لسيناريوهات مثل تدفق بيانات الإدخال. يساعد الاستدلال الدفعي في الوقت الفعلي على زيادة الإنتاجية لطلبات الوقت الفعلي.

جرب تحويل الدُفعات باستخدام نماذج Text2Text Generation في SageMaker اليوم وأخبرنا بتعليقاتك!


عن المؤلفين

قم بإجراء تحويلات مجمعة باستخدام نماذج اللغات الكبيرة من Amazon SageMaker Jumpstart Text2Text Generation | أمازون ويب سيرفيسز PlatoBlockchain Data Intelligence. البحث العمودي. منظمة العفو الدولية.هيمانت سينغ هو مهندس تعلم آلي يتمتع بخبرة في الخوارزميات المدمجة في Amazon SageMaker JumpStart و Amazon SageMaker. حصل على درجة الماجستير من معهد كورانت للعلوم الرياضية وبكلوريوس في التكنولوجيا من IIT دلهي. لديه خبرة في العمل على مجموعة متنوعة من مشاكل التعلم الآلي في مجال معالجة اللغة الطبيعية ورؤية الكمبيوتر وتحليل السلاسل الزمنية.

قم بإجراء تحويلات مجمعة باستخدام نماذج اللغات الكبيرة من Amazon SageMaker Jumpstart Text2Text Generation | أمازون ويب سيرفيسز PlatoBlockchain Data Intelligence. البحث العمودي. منظمة العفو الدولية.راتشنا شادا هو مهندس حلول رئيسي AI / ML في الحسابات الإستراتيجية في AWS. راتشنا متفائل يؤمن بأن الاستخدام الأخلاقي والمسؤول للذكاء الاصطناعي يمكن أن يحسن المجتمع في المستقبل ويحقق الرخاء الاقتصادي والاجتماعي. في أوقات فراغها ، تحب راتشنا قضاء الوقت مع عائلتها والمشي لمسافات طويلة والاستماع إلى الموسيقى.

قم بإجراء تحويلات مجمعة باستخدام نماذج اللغات الكبيرة من Amazon SageMaker Jumpstart Text2Text Generation | أمازون ويب سيرفيسز PlatoBlockchain Data Intelligence. البحث العمودي. منظمة العفو الدولية.د. أشيش خيتان هو عالم تطبيقي أقدم مع خوارزميات Amazon SageMaker المضمنة ويساعد في تطوير خوارزميات التعلم الآلي. حصل على الدكتوراه من جامعة إلينوي في أوربانا شامبين. وهو باحث نشط في التعلم الآلي والاستدلال الإحصائي ، وقد نشر العديد من الأوراق البحثية في مؤتمرات NeurIPS و ICML و ICLR و JMLR و ACL و EMNLP.

الطابع الزمني:

اكثر من التعلم الآلي من AWS