Apache Beam と TFX
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Apache Beam は、さまざまな実行エンジンで実行する、バッチおよびストリーミングデータ処理のジョブを実行するためのフレームワークを提供します。複数の TFX ライブラリではタスクの実行に Beam が使用されているため、コンピュートクラスタでの高度なスケーラビリティが可能です。Beam には、多様な実行エンジンまたは「ランナー」のサポートが含まれています。こういったランナーには、単一のコンピュートノードで実行する Direct Runner があり、開発、テスト、または小型のデプロイで非常に有用です。Beam は、TFX がコードを変更することなく、サポートされているあらゆるランナーで実行できるようにする抽象レイヤーを提供しています。TFX は Beam Python API を使用するため、Python API がサポートするランナーに制限されています。
デプロイとスケーラビリティ
ワークロードの要件が増大するにつれ、Beam は大規模なコンピュートクラスタの非常に大規模なデプロイに合わせてスケーリングできます。唯一の制限は、その基盤にあるランナーのスケーラビリティによるものです。大規模なデプロイのランナーは通常、アプリケーションの自動デプロイ、スケーリング、および管理が可能な Kubernetes または Apache Mesos などのコンテナオーケストレーションシステムにデプロイされます。
Apache Beam についての詳細は、Apache Beam ドキュメントをご覧ください。
Google Cloud ユーザーの場合、Dataflow が推奨されるランナーです。これは、リソースの自動スケーリング、動的な作業のリバランス、他の Google Cloud サービスとの緊密な統合、組み込みのセキュリティ、モニタリングを通じて、サーバーレスの費用効果の高いプラットフォームを提供します。
カスタム Python コードと依存関係
TFX パイプラインで Beam を使用する際には、カスタムコードの処理や追加の Python モジュールから必要とされる依存関係に注意する必要があります。
- preprocessing_fn は、ユーザー自身の Python モジュールを参照する必要があります
- Evaluator コンポーネントのカスタムエクストラクタ
- TFX コンポーネントからサブクラス化されたカスタムモジュール
TFX は、Python の依存関係を処理するために Python パイプラインの依存関係の管理に対する Beam のサポートに依存しています。現在、これを管理する方法は2つあります。
- Python コードと依存関係をソースパッケージとして提供する
- [Dataflow のみ]コンテナイメージをワーカーとして使用する
これらについての説明は、以下のとおりです。
Python コードと依存関係をソースパッケージとして提供する
これは、次のユーザーに推奨されます。
- Python パッケージに精通していて
- Python ソースコードのみを使用するユーザー(C モジュールや共有ライブラリは使用しない)。
これを提供するには、次の beam_pipeline_args のいずれかを使用して Python PipelineDependencies の管理のいずれかのパスに従います。
- --setup_file
- --extra_package
- --requirements_file
注意:上記のいずれの場合でも、同じバージョンのtfx
が依存関係としてリストされていることを確認してください。
[Dataflow のみ]コンテナイメージをワーカーとして使用する
TFX 0.26.0 以降では、Dataflow ワーカーにカスタムコンテナイメージを使用するための実験的なサポートがあります。
これを使用するには、次のことを行う必要があります。
tfx
とユーザーのカスタムコードおよび依存関係の両方がプリインストールされた Docker イメージを作成します。
- (1)
tfx>=0.26
を使用し、(2)python 3.7 を使用してパイプラインを開発するユーザーの場合、これを行う最も簡単な方法は、公式のtensorflow/tfx
の対応するバージョンを拡張することです。
# You can use a build-arg to dynamically pass in the
# version of TFX being used to your Dockerfile.
ARG TFX_VERSION
FROM tensorflow/tfx:${TFX_VERSION}
# COPY your code and dependencies in
- ビルドされたイメージを、Dataflow で使用されるプロジェクトからアクセスできるコンテナーイメージレジストリにプッシュします。
- 以下の
beam_pipeline_args
を提供します。
beam_pipeline_args.extend([
'--runner=DataflowRunner',
'--project={project-id}',
'--worker_harness_container_image={image-ref}',
'--experiments=use_runner_v2',
])
TODO(b/171733562): Dataflow のデフォルトになったら、use_runner_v2 を削除します。
TODO(b/179738639): https://issues.apache.org/jira/browse/BEAM-5440 の後に、カスタムコンテナをローカルでテストする方法のドキュメントを作成します。
Beam パイプラインの引数
いくつかの TFX コンポーネントは、分散データ処理のために Beam に依存しています。これらはbeam_pipeline_args
で構成されます。これは、パイプラインの作成中に指定されます。
from tfx.orchestration import pipeline
my_pipeline = pipeline.Pipeline(
...,
beam_pipeline_args=[...])
TFX 0.30 以降では、コンポーネントごとにパイプラインレベルのビーム引数を拡張するためのインターフェイスwith_beam_pipeline_args
が追加されています。
from tfx.components import CsvExampleGen
example_gen = CsvExampleGen(input_base=data_root).with_beam_pipeline_args([...])
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2021-08-16 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2021-08-16 UTC。"],[],[],null,["# Apache Beam and TFX\n\n[Apache Beam](https://beam.apache.org/) provides a framework for running batch\nand streaming data processing jobs that run on a variety of execution engines.\nSeveral of the TFX libraries use Beam for running tasks, which enables a high\ndegree of scalability across compute clusters. Beam includes support for a\nvariety of execution engines or \"runners\", including a direct runner which runs\non a single compute node and is very useful for development, testing, or small\ndeployments. Beam provides an abstraction layer which enables TFX to run on any\nsupported runner without code modifications. TFX uses the Beam Python API, so\nit is limited to the runners that are supported by the Python API.\n\nDeployment and Scalability\n--------------------------\n\nAs workload requirements increase Beam can scale to very large deployments\nacross large compute clusters. This is limited only by the scalability of the\nunderlying runner. Runners in large deployments will typically be deployed to a\ncontainer orchestration system such as Kubernetes or Apache Mesos for automating\napplication deployment, scaling, and management.\n\nSee the [Apache Beam](https://beam.apache.org/) documentation for more\ninformation on Apache Beam.\n\nFor Google Cloud users, [Dataflow](https://cloud.google.com/dataflow) is the\nrecommended runner, which provides a serverless and cost-effective platform\nthrough autoscaling of resources, dynamic work rebalancing, deep integration\nwith other Google Cloud services, built-in security, and monitoring.\n\nCustom Python Code and Dependencies\n-----------------------------------\n\nOne notable complexity of using Beam in a TFX pipeline is handling custom code\nand/or the dependencies needed from additional Python modules. Here are some\nexamples of when this might be an issue:\n\n- preprocessing_fn needs to refer to the user's own Python module\n- a custom extractor for the Evaluator component\n- custom modules which are sub-classed from a TFX component\n\nTFX relies on Beam's support for\n[Managing Python Pipeline Dependencies](https://beam.apache.org/documentation/sdks/python-pipeline-dependencies/)\nto handle Python dependencies. Currently there are two ways to manage this:\n\n1. Providing Python Code and Dependencies as Source Package\n2. \\[Dataflow only\\] Using a Container Image as Worker\n\nThese are discussed next.\n\n### Providing Python Code and Dependencies as a Source Package\n\nThis is recommended for users who:\n\n1. Are familiar with Python packaging and\n2. Only use Python source code (i.e., no C modules or shared libraries).\n\nPlease follow one of the paths in\n[Managing Python Pipeline Dependencies](https://beam.apache.org/documentation/sdks/python-pipeline-dependencies/)\nto provide this using one of the following beam_pipeline_args:\n\n- --setup_file\n- --extra_package\n- --requirements_file\n\nNotice: In any of above cases, please make sure that the same version of `tfx`\nis listed as a dependency.\n\n### \\[Dataflow only\\] Using a Container Image for a Worker\n\nTFX 0.26.0 and above has experimental support for using\n[custom container image](https://beam.apache.org/documentation/runtime/environments/#customizing-container-images)\nfor Dataflow workers.\n\nIn order to use this, you have to:\n\n- Build a Docker image which has both `tfx` and the users' custom code and dependencies pre-installed.\n - For users who (1) use `tfx\u003e=0.26` and (2) uses python 3.7 to develop their pipelines, the easiest way to do this is extending the corresponding version of the official `tensorflow/tfx` image:\n\n # You can use a build-arg to dynamically pass in the\n # version of TFX being used to your Dockerfile.\n\n ARG TFX_VERSION\n FROM tensorflow/tfx:${TFX_VERSION}\n # COPY your code and dependencies in\n\n- Push the image built to a container image registry which is accessible by the project used by Dataflow.\n - Google Cloud users can consider using [Cloud Build](https://cloud.google.com/cloud-build/docs/quickstart-build) which nicely automates above steps.\n- Provide following `beam_pipeline_args`:\n\n beam_pipeline_args.extend([\n '--runner=DataflowRunner',\n '--project={project-id}',\n '--worker_harness_container_image={image-ref}',\n '--experiments=use_runner_v2',\n ])\n\n**TODO(b/171733562): Remove use_runner_v2 once it is default for Dataflow.**\n\n**TODO(b/179738639): Create documentation for how to test custom container\nlocally after \u003chttps://issues.apache.org/jira/browse/BEAM-5440\u003e**\n\nBeam Pipeline Arguments\n-----------------------\n\nSeveral TFX components rely on Beam for distributed data processing. They are\nconfigured with `beam_pipeline_args`, which is specified during during pipeline\ncreation: \n\n my_pipeline = Pipeline(\n ...,\n beam_pipeline_args=[...])\n\nTFX 0.30 and above adds an interface, `with_beam_pipeline_args`, for extending\nthe pipeline level beam args per component: \n\n example_gen = CsvExampleGen(input_base=data_root).with_beam_pipeline_args([...])"]]