尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

ZenML Local Artifact Store 完全指南:本地文件系统上的零配置制品存储

ZenML Local Artifact Store 完全指南:本地文件系统上的零配置制品存储 ZenML Local Artifact Store 完全指南本地文件系统上的零配置制品存储【免费下载链接】zenmlZenML : One AI Platform from Pipelines to Agents. https://zenml.io.项目地址: https://gitcode.com/GitHub_Trending/ze/zenmlZenML 的 Local Artifact Store 是内置的 Artifact Store 组件它直接使用机器上的一个本地文件夹来存放流水线产生的全部制品数据集、模型、数据校验报告等。本指南面向正在评估 ZenML、处于实验阶段或进行本地开发的读者从默认栈中的开箱即用配置讲起覆盖它的适用场景、部署方式、使用限制以及隐藏在底层源码中的实现原理帮助你判断何时该用它、何时必须切换到云对象存储。Local Artifact Store 是什么在 ZenML 中Artifact Store 是任何 MLOps Stack 中的核心组件负责充当数据持久层流水线步骤产出或消费的数据数据集、模型、数据画像、数据与模型校验报告以及任何被步骤返回的对象都会被 ZenML 自动序列化并保存到 Artifact Store 中并配合 ZenML 的跟踪机制提供缓存、来源/血缘跟踪与流水线可复现能力。Local Artifact Store 是这个抽象的内置built-in实现它的 flavor 名为local不需要你预置任何额外的基础设施也不需要与 Amazon S3、Google Cloud Storage 这类托管对象存储服务打交道——你只需要本机文件系统即可。从源码结构看本地实现非常简洁相关文件全部位于 src/zenml/artifact_stores/ 目录local_artifact_store.py 定义了LocalArtifactStoreConfig、LocalArtifactStore与LocalArtifactStoreFlavor三个类base_artifact_store.py 定义了所有 Artifact Store 共享的抽象基类BaseArtifactStore、配置基类BaseArtifactStoreConfig与 flavor 基类BaseArtifactStoreFlavor所有 flavor 通过 flavor_registry.py 注册进 ZenML其中第 62 行导入了LocalArtifactStoreFlavor。Local Artifact Store 之所以是默认选项是因为它不需要任何远程凭证、网络配置或额外资源是 ZenML 开箱即用时最先被创建的存储组件。何时应该使用 Local Artifact StoreLocal Artifact Store 最适合以下阶段刚接触 ZenML、正在评估其能力无需配置任何云资源即可跑通第一条流水线实验阶段制品数据集、模型等暂时不需要与团队其他成员共享本地开发与调试希望快速迭代把精力集中在流水线逻辑本身。它不应被用于生产环境。理由在原文档中已经说得很清楚可以归纳为三点无法跨机器共享本地文件系统无法在团队内共享其他机器无法访问这些制品云上实例无法可视化制品当 ZenML 实例部署在云端时通过该实例使用 Local Artifact Store制品可视化artifact visualizations将不可用缺少生产级能力Local Artifact Store 不提供高可用、可扩展性、备份与恢复等服务这些是生产级 MLOps 系统的基本预期。当你从实验阶段过渡到团队协作或生产部署时可以用其他更合适的 flavor 替换栈中的 Local Artifact Store而代码完全不需要改动——这正是 Artifact Store 抽象的价值所在。与 Local Artifact Store 不兼容的栈组件由于制品存放在本机文件系统上并非所有栈组件都能与 Local Artifact Store 组成同一个 Stack。原文档明确了以下约束Orchestrator编排器只能与运行在本地机器上的编排器组合例如 Local Orchestrator、本地 Kubeflow Orchestrator 或本地 Kubernetes OrchestratorModel Deployer模型部署器只能与本地运行的部署器组合例如 MLflow Model DeployerStep Operator步骤算子没有任何 Step Operator 能与 Local Artifact Store 共存于同一个栈因为 Step Operator 的用途恰恰是把 ZenML 步骤调度到远程专用环境中执行。这一约束在源码中也有体现例如 mlflow_experiment_tracker.py 的第 135-149 行会检查栈中是否包含LocalArtifactStore当用户未显式指定 tracking URI 时并依赖它构造本地的 MLflow 跟踪地址。如何部署 Local Artifact Store开箱即用的默认栈ZenML 预配置的default栈中已经包含一个 Local Artifact Store。运行下面的命令可以查看当前栈列表$ zenml stack list Running without an active repository root. Using the default local database. ┏━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━┓ ┃ ACTIVE │ STACK NAME │ ARTIFACT_STORE │ ORCHESTRATOR ┃ ┠────────┼────────────┼────────────────┼──────────────┨ ┃ │ default │ default │ default ┃ ┗━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛用zenml artifact-store describe可以查看当前激活栈中 Artifact Store 的完整配置$ zenml artifact-store describe Running without an active repository root. Using the default local database. Running with active stack: default No component name given; using default from active stack. ARTIFACT_STORE Component Configuration (ACTIVE) ┏━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ COMPONENT_PROPERTY │ VALUE ┃ ┠────────────────────┼──────────────────────────────────────────────────────────────────────────────┨ ┃ TYPE │ artifact_store ┃ ┠────────────────────┼──────────────────────────────────────────────────────────────────────────────┨ ┃ FLAVOR │ local ┃ ┠────────────────────┼──────────────────────────────────────────────────────────────────────────────┨ ┃ NAME │ default ┃ ┠────────────────────┼──────────────────────────────────────────────────────────────────────────────┨ ┃ UUID │ 2b7773eb-d371-4f24-96f1-fad15e74fd6e ┃ ┠────────────────────┼──────────────────────────────────────────────────────────────────────────────┨ ┃ PATH │ /home/stefan/.config/zenml/local_stores/2b7773eb-d371-4f24-96f1-fad15e74fd6e ┃ ┗━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛注意输出中的PATH字段它揭示了默认路径的组成规则全局配置目录/local_stores/Artifact Store 的 UUID。也就是说制品实际存储在本机文件系统的一个专用文件夹中。默认路径的源码级解析这个默认路径并非写死的字符串而是由源码计算得出的。在 local_artifact_store.py 中get_default_local_path(id_)第 93-106 行返回GlobalConfiguration().local_stores_path / store 的 UUIDpath属性第 108-126 行的取值逻辑是如果配置中显式指定了path就用它否则回退到get_default_local_path(self.id)并调用io_utils.create_dir_recursive_if_not_exists确保目录存在local_stores_path的定义位于 global_config.py 第 430-443 行默认是config_directory / local_stores其中local_stores这一目录名定义在 constants.py 第 346 行且可以通过环境变量ZENML_LOCAL_STORES_PATH定义于第 228 行覆盖。注册额外的 Local Artifact Store 实例除默认实例外你完全可以注册更多 Local Artifact Store并按需组合进自己的栈# 注册本地 artifact store zenml artifact-store register custom_local --flavor local # 注册并激活使用该 artifact store 的新栈 zenml stack register custom_stack -o default -a custom_local --set--flavor local指定 flavor-o default复用默认编排器-a custom_local引用刚注册的 Artifact Store--set将其设为激活栈。关于自定义path的警告与其他 Artifact Store flavor 一样Local Artifact Store 也接受path配置参数用于在注册时指向机器上的自定义路径。但原文档给出了强烈建议尽量依赖默认的path值否则可能导致意外结果。原因在于其他本地栈组件例如本地 Orchestrator 生成的临时脚本、MLflow 的本地跟踪目录依赖默认路径的约定才能访问到 Local Artifact Store 的制品。自定义路径会破坏这种约定从而影响整个栈的正常协作。深入实现Local Artifact Store 的底层原理类是 LocalFilesystem 与 BaseArtifactStore 的组合从源码看LocalArtifactStore类同时继承自LocalFilesystem和BaseArtifactStorelocal_artifact_store.py 第 85-88 行其全部文件操作方法都继承自LocalFilesystemlocal_filesystem.py。LocalFilesystem是一套基于 Python 标准库os、shutil、glob实现的文件系统接口它实现了与BaseArtifactStore抽象方法一一对应的操作包括open/copyfile/exists/glob/isdir/listdirmkdir/makedirs/remove/rename/rmtreestat/size/walk这些方法签名与BaseArtifactStore见 base_artifact_store.py 第 296-455 行的抽象方法声明完全一致这正是所有 Artifact Store 实现同一个 IO API的保证。路径校验与安全边界LocalArtifactStoreConfig通过 Pydantic 的field_validatorlocal_artifact_store.py 第 53-73 行实现了ensure_path_local校验如果传入的path以gs://、hdfs://、s3://、az://、abfs://等远程前缀开头会直接抛出ArtifactStoreInterfaceError。对应的单元测试见 test_local_artifact_store.py 的test_local_artifact_store_only_supports_local_paths第 42-79 行分别用gs://、s3://、hdfs://路径验证了异常抛出并用os.getcwd()验证了本地路径可以正常通过。此外BaseArtifactStore在初始化时通过_sanitize_paths装饰器base_artifact_store.py 第 58-187 行为所有 IO 方法加了一层路径净化与越界检查所有访问路径必须位于 Artifact Store 的根路径fixed_root_path之内否则抛出FileNotFoundError在 ZenML Server 环境下如果未开启ENV_ZENML_SERVER_ALLOW_LOCAL_FILE_ACCESS访问本地文件还会抛出IllegalOperationError——这也是前面提到云上实例无法使用本地制品在代码层面的具体体现。缓存键与多客户端隔离LocalArtifactStore还覆写了custom_cache_key属性local_artifact_store.py 第 137-147 行返回GlobalConfiguration().user_id.bytes。其注释说明了动机当多个客户端机器使用同一个共享注册的Local Artifact Store 时用客户端 ID 使缓存键失效避免不同机器间的缓存污染。如何使用 Local Artifact Store除了制品存储在本地之外Local Artifact Store 的使用方式与其他任何 flavor 的 Artifact Store完全相同。也就是说日常开发中你通常根本不需要直接感知它的存在ZenML 提供了更高层的 API在流水线步骤中返回一个或多个对象ZenML 会自动把它们序列化并保存到当前激活栈的 Artifact Store 中流水线运行结束后可以从激活栈的 Artifact Store 中加载制品回内存。低层 Artifact Store API 示例当你需要实现自定义 Materializer或者想在 Artifact Store 中存取自定义对象时就需要直接使用低层 IO API。由于 Local Artifact Store 完全实现了BaseArtifactStore的接口下面这些针对本地文件系统的写法对其他远程 flavor 同样适用这正是统一 IO 抽象的意义。创建目录、写入并读取文本内容import os from zenml.utils import io_utils from zenml.io import fileio from zenml.client import Client root_path Client().active_stack.artifact_store.path artifact_contents example artifact artifact_path os.path.join(root_path, artifacts, examples) artifact_uri os.path.join(artifact_path, test.txt) fileio.makedirs(artifact_path) io_utils.write_file_contents_as_string(artifact_uri, artifact_contents)读取刚才写入的内容import os from zenml.utils import io_utils from zenml.client import Client root_path Client().active_stack.artifact_store.path artifact_path os.path.join(root_path, artifacts, examples) artifact_uri os.path.join(artifact_path, test.txt) artifact_contents io_utils.read_file_contents_as_string(artifact_uri)借助本地临时文件把外部库的对象序列化后拷贝进 Artifact Store这是 Materializer 的典型工作方式——很多外部库不支持直接读写对象存储后端import os import tempfile import external_lib from zenml.client import Client from zenml.io import fileio root_path Client().active_stack.artifact_store.path artifact_path os.path.join(root_path, artifacts, examples) artifact_uri os.path.join(artifact_path, test.json) fileio.makedirs(artifact_path) with tempfile.NamedTemporaryFile( modew, suffix.json, deleteTrue ) as f: external_lib.external_object.save_to_file(f.name) # 将序列化后的对象拷贝进 artifact store fileio.copy(f.name, artifact_uri)反向操作——从 Artifact Store 拷贝到本地临时文件再加载import os import tempfile import external_lib from zenml.client import Client from zenml.io import fileio root_path Client().active_stack.artifact_store.path artifact_path os.path.join(root_path, artifacts, examples) artifact_uri os.path.join(artifact_path, test.json) with tempfile.NamedTemporaryFile( modew, suffix.json, deleteTrue ) as f: # 从 artifact store 拷贝出序列化对象 fileio.copy(artifact_uri, f.name) external_lib.external_object.load_from_file(f.name)使用注意URI 必须相对于 Artifact Store 根路径调用 Artifact Store API 时应始终使用相对于 Artifact Store 根路径的 URI否则可能触发不支持的协议或将对象写到存储边界之外。可以通过Client().active_stack.artifact_store.path获取激活 Artifact Store 的根路径再以其为基准拼接制品 URI见上面示例。当编写自定义 Materializer 时基础制品 URI 路径已经由 ZenML 提供无需自己拼接。Local Artifact Store 与其他 flavor 的对比与迁移在 docs/book/component-guide/artifact-stores/README.md 中给出了完整的 Artifact Store flavor 一览表Local 与其他 flavor 的定位差异一目了然Artifact StoreFlavor集成URI Schema说明Locallocal内置无默认 Artifact Store制品存于本地文件系统仅适合本地运行 ZenMLAmazon S3s3s3s3://使用 AWS S3 作为对象存储后端Backblaze B2b2b2s3://通过 S3 兼容 API 使用 Backblaze B2Google Cloud Storagegcpgcpgs://使用 GCS 作为对象存储后端Azureazureazureabfs://、az://使用 Azure Blob Storage 作为对象存储后端Alibaba Cloud OSSs3s3s3://通过 S3 集成连接阿里云 OSSMinIOs3s3s3://通过 S3 集成连接自托管的 MinIO自定义实现自定义—自定义扩展 Artifact Store 抽象提供自己的实现每个 Artifact Store 在注册时都必须配置path属性它是所有对象存储的根路径 URI且必须使用该 flavor 支持的 URI schema。例如 S3 flavor 需要s3://schemazenml artifact-store register s3_store -f s3 --path s3://my_bucket而 Local flavor 的SUPPORTED_SCHEMES是空集合{}见 local_artifact_store.py 第 49 行即只接受本地路径——这从代码层面杜绝了误传远程 URI 的可能。迁移到团队或生产环境时只需在栈中用远程 Artifact Store 替换 Local 实例业务代码无需任何改动相关示例可参考各 flavor 的文档页面s3.md、gcp.md、azure.md 等。总结Local Artifact Store 是 ZenML 中最小、最易上手的制品存储方案零配置上手默认栈开箱即用制品自动保存在全局配置目录下的local_stores/UUID文件夹中接口统一与远程对象存储实现完全相同的 IO API从本地迁移到 S3/GCS/Azure 时业务代码零改动明确的使用边界只适合本地单机开发与实验生产环境必须切换到共享的远程存储源码级的安全保障远程前缀校验、路径越界检查和服务器本地文件访问控制共同构成了本地存储的安全边界。如果你想直接查看实现细节推荐从这三个文件入手local_artifact_store.pyflavor 实现、base_artifact_store.py抽象基类与路径校验、test_local_artifact_store.py单元测试。【免费下载链接】zenmlZenML : One AI Platform from Pipelines to Agents. https://zenml.io.项目地址: https://gitcode.com/GitHub_Trending/ze/zenml创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表