Amazon SageMaker Clarify 可以更轻松地评估和选择基础模型(预览版)

人工智能
机器学习
re:Invent
Amazon SageMaker
大语言模型
0
0
我很高兴地向大家宣布 [Amazon SageMaker Clarify](https://aws.amazon.com/sagemaker/clarify/?trk=cndc-detail) 现在支持基础模型( FM )评估(预览版)。作为一名数据科学家或[机器学习](https://aws.amazon.com/cn/machine-learning/?trk=cndc-detail)( ML )工程师,您现在使用 SageMaker Clarify 在短短几分钟内即可根据准确性、稳健性、创造力、事实知识、偏见和毒性等指标来评估、比较和选择 FM 。这是 SageMaker Clarify 检测 ML 数据和模型偏差以及解释模型预测等现有功能之外的新增功能。 这项新功能为大型语言模型( LLM )提供了自动化和人机协作评估,适用于任何地方,包括 [SageMaker JumpStart](https://aws.amazon.com/sagemaker/jumpstart/?trk=cndc-detail) 中可用的 LLM ,以及在亚马逊云科技外部训练和托管的模型。省去了寻找合适的模型评估工具并将其集成到开发环境中的繁重工作。而且还简化了在生成式人工智能( AI )用例中采用学术基准。 ### 利用 SageMaker Clarify 评估 FM 使用 SageMaker Clarify ,您现在可以在统一的平台,在模型选择过程中和整个模型定制工作流程中,根据预定义的标准评估和比较任何 LLM 。除了自动评估之外,您还可以使用人机协作功能,通过使用自己的员工或 [SageMaker Ground Truth](https://aws.amazon.com/sagemaker/groundtruth/?trk=cndc-detail) 的托管劳动力设置人工审核评估有用性、创意意图和风格等更主观的标准。 要开始进行模型评估,您可以使用专为常见 LLM 任务而构建的精选提示数据集,包括开放式文本生成、文本摘要、问答和分类任务。您还可以针对特定的用例,使用自定义提示数据集和指标来扩展模型评估。对于任何任务和评估指标,都可以使用人机协作的评估方法。每次评估作业完成后,您会收到一个评估报告,其中用自然语言总结了结果,并包含可视化图表和示例。您可以下载所有的指标和报告,并将模型评估整合到 SageMaker MLOps 工作流中。 您可以在 SageMaker Studio 左侧菜单的 Jobs 下找到 Model evaluation 。您也可以直接从 SageMaker JumpStart 中任何 LLM 的模型详细信息页面中选择 Evaluate 。 ![1.png](https://dev-media.amazoncloud.cn/c9abf73e871a438fb4e91020335982d8_1.png "1.png") 选择 Evaluate a model 设置评估作业。 UI 向导会指导您选择自动或人工评估、模型、相关任务、指标、提示数据集和审查团队。 ![2.png](https://dev-media.amazoncloud.cn/56648b27c8e8453d9554ab7574e9a4b5_2.png "2.png") 模型评估作业完成后,可以在评估报告中查看结果。 ![3.png](https://dev-media.amazoncloud.cn/8f0a0af1a3184a9bad6fa6f40c743d16_3.png "3.png") 除了 UI 之外,您还可以通过示例 Jupyter 笔记本来开始学习逐步指导的方式,了解如何在 SageMaker 中以编程方式运行模型评估。 ### 使用 FMEval 开源库随时随地评估模型 若要随时随地运行模型评估,包括在亚马逊云科技外部训练和托管的模型,请使用 [FMEval 开源库](https://github.com/aws/fmeval?trk=cndc-detail)。以下示例演示了如何使用库通过扩展 ModelRunner 类来评估自定义模型。 在这个演示中,我从 Hugging Face 模型中心选择 GPT-2,并设置自定义 `HFModelConfig` 和 `HuggingFaceCausalLLMModelRunner 类`,该类适用于GPT-2 等 Hugging Face 模型中心仅包含解码器的因果模型。 [FMEval GitHub repo](https://github.com/aws/fmeval/tree/main/examples?trk=cndc-detail) 中也提供了该示例。 ```Python !pip install fmeval # ModelRunners invoke FMs from amazon_fmeval.model_runners.model_runner import ModelRunner # Additional imports for custom model import warnings from dataclasses import dataclass from typing import Tuple, Optional import torch from transformers import AutoModelForCausalLM, AutoTokenizer @dataclass class HFModelConfig: model_name: str max_new_tokens: int normalize_probabilities: bool = False seed: int = 0 remove_prompt_from_generated_text: bool = True class HuggingFaceCausalLLMModelRunner(ModelRunner): def __init__(self, model_config: HFModelConfig): self.config = model_config self.model = AutoModelForCausalLM.from_pretrained(self.config.model_name) self.tokenizer = AutoTokenizer.from_pretrained(self.config.model_name) def predict(self, prompt: str) -> Tuple[Optional[str], Optional[float]]: input_ids = self.tokenizer(prompt, return_tensors="pt").to(self.model.device) generations = self.model.generate( **input_ids, max_new_tokens=self.config.max_new_tokens, pad_token_id=self.tokenizer.eos_token_id, ) generation_contains_input = ( input_ids["input_ids"][0] == generations[0][: input_ids["input_ids"].shape[1]] ).all() if self.config.remove_prompt_from_generated_text and not generation_contains_input: warnings.warn( "Your model does not return the prompt as part of its generations. " "`remove_prompt_from_generated_text` does nothing." ) if self.config.remove_prompt_from_generated_text and generation_contains_input: output = self.tokenizer.batch_decode(generations[:, input_ids["input_ids"].shape[1] :])[0] else: output = self.tokenizer.batch_decode(generations, skip_special_tokens=True)[0] with torch.inference_mode(): input_ids = self.tokenizer(self.tokenizer.bos_token + prompt, return_tensors="pt")["input_ids"] model_output = self.model(input_ids, labels=input_ids) probability = -model_output[0].item() return output, probability ``` 接下来,使用模型信息创建 HFModelConfig 和 HuggingFaceCausalLLMModelRunner 的实例。 ```js hf_config = HFModelConfig(model_name="gpt2", max_new_tokens=32) model = HuggingFaceCausalLLMModelRunner(model_config=hf_config) ``` 然后,选择并配置评估算法。 ``` # Let's evaluate the FM for FactualKnowledge from amazon_fmeval.fmeval import get_eval_algorithm from amazon_fmeval.eval_algorithms.factual_knowledge import FactualKnowledgeConfig eval_algorithm_config = FactualKnowledgeConfig("<OR>") eval_algorithm = get_eval_algorithm("factual_knowledge", eval_algorithm_config) Let’s first test with one sample. The evaluation score is the percentage of factually correct responses. model_output = model.predict("London is the capital of")[0] print(model_output) eval_algo.evaluate_sample( target_output="UK<OR>England<OR>United Kingdom", model_output=model_output ) the UK, and the UK is the largest producer of food in the world. The UK is the world's largest producer of food in the world. [EvalScore(name='factual_knowledge', value=1)] ``` 虽然这个响应并不完美,但它包含“英国” 接下来,您可以使用内置数据集评估 FM 或设置自定义数据集。如果要使用自定义评估数据集,请创建 `DataConfig` 的实例: ```js config = DataConfig( dataset_name="my_custom_dataset", dataset_uri="dataset.jsonl", dataset_mime_type=MIME_TYPE_JSONLINES, model_input_location="question", target_output_location="answer", ) eval_output = eval_algorithm.evaluate( model=model, dataset_config=config, prompt_template="\$feature", #\$feature is replaced by the input value in the dataset save=True ) ``` 评估结果将返回整个数据集的综合评估分数,并为存储在本地输出路径中的每个模型输入提供详细结果。 ### 体验预览版 [Amazon SageMaker](https://aws.amazon.com/cn/sagemaker/?trk=cndc-detail) Clarify 的 FM 评估功能公开预览版即日起在亚马逊云科技下列地区上线:美国东部(俄亥俄州)、美国东部(北弗吉尼亚州)、美国西部(俄勒冈州)、亚太地区(新加坡)、亚太地区(东京)、欧洲(法兰克福)和欧洲(爱尔兰)。可在 GitHub 获取 [FMEval 开源库](https://github.com/aws/fmeval?trk=cndc-detail)。请访问 [Amazon SageMaker Clarify](https://aws.amazon.com/sagemaker/clarify/?trk=cndc-detail) 了解更多信息。 ### 入门 登录 [Amazon Management Console](https://console.aws.amazon.com/sagemaker/home?trk=cndc-detail),立即开始使用 SageMaker Clarify 评估您的 FM ! 【文章来源: https://aws.amazon.com/blogs/aws/amazon-sagemaker-clarify-makes-it-easier-to-evaluate-and-select-foundation-models-preview/?trk=cndc-detail 】
目录
亚马逊云科技解决方案 基于行业客户应用场景及技术领域的解决方案
联系亚马逊云科技专家
亚马逊云科技解决方案
基于行业客户应用场景及技术领域的解决方案
联系专家
0
目录
关闭