监控¶
数据中心化 AI 是一种将数据作为 AI 系统基石的范例。这种方法强调数据质量和相关性的重要性,表明 AI 模型的有效性主要取决于用于训练和与模型交互的数据。
BentoML 完全采纳了这一范例,提供了实现数据中心化工作流的 API,使得收集推理数据、监控模型并将数据发送到各种目的地(例如,本地存储、云服务和任何支持 OTLP 的工具)变得简单易行,以便用于您的 AI 项目。
本文档解释了如何在 BentoML 中实现监控和收集推理数据。
模型监控和数据收集的好处¶
使用 BentoML 实施有效的数据收集和模型监控策略具有以下好处
关键业务指标监控:跟踪重要的统计指标,衡量您的模型对业务目标的影響。
早期检测数据漂移:及早识别输入数据分布的变化,有助于确定何时需要重新训练模型。
质量保证:对以前未跟踪的指标实施质量保证,例如模型性能、准确性以及随时间的退化。
增强互操作性:促进数据科学和运营团队之间的协作,简化模型训练和迭代过程。
明智的决策:提供模型在生产环境中的行为洞察,为未来的模型改进和迭代提供信息。
实现监控¶
在 BentoML 中,您可以使用 bentoml.monitor
上下文管理器记录与模型推理相关的数据。它允许您指定一个监控会话,您可以在其中记录各种数据类型。这确保了日志记录的结构化和组织化,使得以后更容易分析数据。
以下是在 Hello world 中的 Summarization Service 中实现监控的示例。
from __future__ import annotations
import bentoml
from transformers import pipeline
EXAMPLE_INPUT = "Breaking News: In an astonishing turn of events, the small town of Willow Creek has been taken by storm as local resident Jerry Thompson's cat, Whiskers, performed what witnesses are calling a 'miraculous and gravity-defying leap.' Eyewitnesses report that Whiskers, an otherwise unremarkable tabby cat, jumped a record-breaking 20 feet into the air to catch a fly. The event, which took place in Thompson's backyard, is now being investigated by scientists for potential breaches in the laws of physics. Local authorities are considering a town festival to celebrate what is being hailed as 'The Leap of the Century.'"
@bentoml.service(
resources={"cpu": "2"},
traffic={"timeout": 10},
)
class Summarization:
def __init__(self) -> None:
self.pipeline = pipeline('summarization')
@bentoml.api
def summarize(self, text: str = EXAMPLE_INPUT) -> str:
# Use bentoml.monitor as a context manager
with bentoml.monitor("text_summarization") as mon:
# Log the input data
mon.log(text, name="input_text", role="original_text", data_type="text")
result = self.pipeline(text)
summary_text = result[0]['summary_text']
# Log the output data
mon.log(summary_text, name="summarized_text", role="prediction", data_type="text")
return summary_text
当您进入 bentoml.monitor
上下文时,您会实例化一个由唯一名称(本例中为 "text_summarization"
)标识的监控会话,这有助于您对记录的数据进行分类和检索,以执行特定的监控任务。
在 bentoml.monitor
上下文内,您可以使用 log
方法记录单个数据点,这需要几个参数来描述正在记录的数据
data
: 您想要记录的实际数据值。这可以是输入/输出参数值(本例中的输入text
和输出summary_text
)。name
: 数据点的字符串标识符,将显示在日志中。这有助于在日志中标记数据,清楚地表明每个记录值代表什么。role
: 数据在模型推理中的角色。默认角色包括"feature"
: 表示记录的数据是模型的输入特征。"prediction"`: 表示记录的数据是模型做出的预测。
"target"`: 表示记录的数据是目标或标签。
您可以设置自定义
role
,例如示例中的original_text
,它也将被记录。data_type
: 数据的类型。默认数据类型包括"numerical"`: 用于定量数据。
"categorical"`: 用于表示类别的离散值。
"numerical_sequence"`: 用于数值序列或列表。
您可以设置自定义
data_type
,例如示例中的text
,它也将被记录。
查看请求和模式日志¶
服务启动时,BentoML 会将请求和模式日志导出到默认目录 monitoring/<your_monitor_name>`,其中包含
data
和 schema
子目录。
输入和输出数据存储在 data
目录中,包括相应的时间戳和唯一的请求 ID。要查看实时数据日志,请运行
$ tail -f monitoring/text_summarization/data/*.log
{"input_text": "Breaking News: In an astonishing turn of events, the small town of Willow Creek has been taken by storm as local resident Jerry Thompson's cat, Whiskers, performed what witnesses are calling a 'miraculous and gravity-defying leap.' Eyewitnesses report that Whiskers, an otherwise unremarkable tabby cat, jumped a record-breaking 20 feet into the air to catch a fly. The event, which took place in Thompson's backyard, is now being investigated by scientists for potential breaches in the laws of physics. Local authorities are considering a town festival to celebrate what is being hailed as 'The Leap of the Century.'", "summarized_text": " Whiskers, an otherwise unremarkable tabby cat, jumped a record-breaking 20 feet into the air to catch a fly . The event is now being investigated by scientists for potential breaches in the laws of physics . Local authorities are considering a town festival to celebrate what is being hailed as 'The Leap of the Century'", "timestamp": "2024-03-05T03:33:59.490137", "request_id": "14642743634293743168"}
{"input_text": "Breaking News: In an astonishing turn of events, the small town of Willow Creek has been taken by storm as local resident Jerry Thompson's cat, Whiskers, performed what witnesses are calling a 'miraculous and gravity-defying leap.' Eyewitnesses report that Whiskers, an otherwise unremarkable tabby cat, jumped a record-breaking 20 feet into the air to catch a fly. The event, which took place in Thompson's backyard, is now being investigated by scientists for potential breaches in the laws of physics. Local authorities are considering a town festival to celebrate what is being hailed as 'The Leap of the Century.'", "summarized_text": " Whiskers, an otherwise unremarkable tabby cat, jumped a record-breaking 20 feet into the air to catch a fly . The event is now being investigated by scientists for potential breaches in the laws of physics . Local authorities are considering a town festival to celebrate what is being hailed as 'The Leap of the Century'", "timestamp": "2024-03-05T03:41:49.870589", "request_id": "7485759375304577245"}
模式信息存储在 schema
目录中。
$ cat monitoring/text_summarization/schema/*.log
{"meta_data": {"bento_name": "", "bento_version": "not available"}, "columns": [{"name": "input_text", "role": "original_text", "type": "text"}, {"name": "summarized_text", "role": "prediction", "type": "text"}]}
BentoML 将请求和模式数据记录到轮转文件中。这意味着会定期创建新的日志文件,或者当当前日志文件达到一定大小时创建新文件,较旧的文件会根据默认的保留策略进行归档。您可以通过使用配置文件自定义行为。
实际数据点以 JSON 对象的形式记录,提供了一种结构化格式来存储多个记录。这种格式得到广泛支持,并且可以轻松地被各种数据分析工具或数据库摄取以进行进一步处理。
发送收集的数据¶
BentoML 提供了一个通用的监控数据收集 API。它允许您将收集的数据传输到各种目的地,例如数据仓库、分析管道或专门的监控和漂移检测解决方案,而无需修改现有代码库。
下表概述了发送监控数据的可用目标、监控类型(阅读以下部分了解详细信息)以及附加说明。
目的地 |
监控类型 |
备注 |
---|---|---|
|
|
默认情况下,日志存储在本地。 |
云服务和监控服务 (Amazon S3, Azure Blob, Datadog, Elasticsearch, InfluxDB, Google BigQuery, Kafka 等) |
|
有关更多输出选项和配置,请参阅Fluent Bit 输出。 |
任何支持 OTLP 的工具 |
|
对于限制直接文件访问的环境很有用,例如 AWS Lambda。 |
Arize |
|
确保正确配置了 API 密钥和空间密钥。 |
通过日志文件¶
将监控数据写入日志文件是 BentoML 中最常见的数据收集方式,它兼容流行的日志工具,如 Fluent Bit、Filebeat 和 Logstash。您可以使用 @bentoml.service
装饰器自定义监控配置。
...
@bentoml.service(
resources={"cpu": "2"},
traffic={"timeout": 10},
monitoring={
"enabled": True,
"type": "default",
"options": {
"log_config_file": "path/to/log_config.yaml",
"log_path": "monitoring"
}
}
)
class Summarization:
# Service implementation code
monitoring
的可用字段
enabled
: 是否为服务启用监控。将其设置为True
允许 BentoML 根据指定的配置收集和记录数据。type
: 指定要使用的监控系统类型。值default
表示使用 BentoML 内置的监控系统,该系统会收集数据并将其记录到文件中,如上一节所示。options
: 一个字典,允许您自定义监控设置。log_config_file
: 指定自定义日志配置文件的路径(YAML 格式),其中指定了日志行为,例如日志轮转策略、处理程序、日志格式和日志级别。日志参数应根据Python logging 模块的配置模式设置。如果未提供,BentoML 将使用默认的日志配置,这适用于大多数用例。这里是一个配置文件示例,它将日志消息输出到流中
version: 1 disable_existing_loggers: false loggers: bentoml_monitor_data: level: INFO handlers: [bentoml_monitor_data] propagate: false bentoml_monitor_schema: level: INFO handlers: [bentoml_monitor_schema] propagate: false handlers: bentoml_monitor_data: class: logging.StreamHandler stream: "ext://sys.stdout" level: INFO formatter: bentoml_json bentoml_monitor_schema: class: logging.StreamHandler stream: "ext://sys.stdout" level: INFO formatter: bentoml_json formatters: bentoml_json: class: pythonjsonlogger.jsonlogger.JsonFormatter format: "()" validate: false
log_path
: 定义存储监控日志的目录,该目录相对于服务的运行位置。默认为monitoring
。
对于使用符合 OCI 标准的镜像进行部署,您可以通过将指定的日志目录(示例中的 monitoring
)挂载到卷来持久化日志文件。这确保了您的监控数据在容器重启和重新部署后得到保留。
在 Kubernetes 中,您可以通过挂载日志目录并使用 Fluent Bit DaemonSet 或 sidecar 容器来持久化和发送日志。这允许收集的日志文件自动转发到您指定的监控系统或数据仓库,确保您的监控数据集中管理,方便进行分析和告警。
通过 OTLP 端点¶
在无法直接访问日志文件的情况下,例如使用 AWS Lambda(因为它不支持日志文件),BentoML 支持使用 OpenTelemetry Protocol (OTLP) 将监控数据导出到外部遥测系统。
备注
一些日志收集器(如 Fluent Bit)也支持 OTLP 输入。
以下是为 BentoML 服务设置 OTLP 的示例
...
@bentoml.service(
resources={"cpu": "2"},
traffic={"timeout": 10},
monitoring={
"enabled": True,
"type": "otlp",
"options": {
"endpoint": "https://:5000",
"insecure": True,
"credentials": null,
"headers": null,
"timeout": 10,
"compression": null,
"meta_sample_rate": 1.0
}
}
)
class Summarization:
# Service implementation code
可用参数
endpoint
: 指定遥测系统 OTLP 接收器的 URL。BentoML 收集的数据将发送到此端点。insecure
: 一个布尔标志,指定是否禁用与 OTLP 端点连接的传输安全性。将其设置为True
表示不安全的连接,这通常用于本地或开发环境。credentials
: 如果您的 OTLP 端点需要身份验证,您可以使用此参数提供凭据,例如令牌或证书。如果设置为null
,则表示不需要身份验证。headers
: OTLP 端点可能需要的附加头部,用于传递令牌或其他必要信息。timeout
: 定义 BentoML 在超时前等待 OTLP 端点响应的最大持续时间(以秒为单位)。compression
: 指定发送数据时使用的压缩类型。这有助于减少带宽使用。支持的值包括gzip
或none
。meta_sample_rate
: 确定向端点发送元数据的采样率。值为1.0
意味着发送所有元数据,而较低的值会降低频率,只发送收集到的元数据的一部分。
有关更多信息,请参阅OTLP 文档。
插件和第三方监控数据收集器¶
BentoML 还支持插件和第三方监控数据收集器。您可以创建自定义的监控数据收集器并将其发布为 Python 包。与内置收集器(它更注重通用用例的协议特定性)不同,插件可能更具平台特定性。
要使用插件,您需要在部署的运行时环境中安装它。有关详细信息,请参阅定义运行时环境。
Arize AI¶
对于数据和模型监控的端到端解决方案,BentoML 与 Arize AI 合作,提供了 Arize 的插件。如果您不想自行部署管道但仍需要数据和模型监控,Arize AI 是一个不错的选择。它为数据科学家、数据工程师和 ML 工程师提供了一个统一的平台,用于在生产环境中监控、分析和调试 ML 模型。
要使用此插件,请确保您已首先安装它
pip install bentoml-plugins-arize
在 @bentoml.service
装饰器中,添加 space_key
和 api_key
以连接到您的 Arize 账户。
...
@bentoml.service(
resources={"cpu": "2"},
traffic={"timeout": 10},
monitoring={
"enabled": True,
"type": "bentoml_plugins.arize.ArizeMonitor",
"options": {
"space_key": <your_space_key>,
"api_key": <your_api_key>
}
}
)
class Summarization:
# Service implementation code
有关可用 Arize 参数的更多信息,请参阅Arize 文档。
对于部署,插件也应该安装在您的运行时环境中
import bentoml
my_image = bentoml.images.Image(python_version='3.11') \
.python_packages("bentoml-plugins-arize") # Add this plugin
@bentoml.service(image=my_image)
class MyService:
# Service implementation