発達

このドキュメントには、開発環境をセットアップし、さまざまなプラットフォームでソースからtensorflow-ioパッケージを構築するために必要な情報が含まれています。セットアップが完了したら、新しい操作の追加に関するガイドラインについてはSTYLE_GUIDEを参照してください。

IDEのセットアップ

TensorFlow I/O を開発するために Visual Studio Code を構成する方法については、このドキュメントを参照してください。

糸くず

TensorFlow I/O のコードは、Bazel Buildifier、Clang Format、Black、および Pyupgrade に準拠しています。次のコマンドを使用してソース コードを確認し、lint の問題を特定してください。

# Install Bazel version specified in .bazelversion
$ curl -OL https://github.com/bazelbuild/bazel/releases/download/$(cat .bazelversion)/bazel-$(cat .bazelversion)-installer-darwin-x86_64.sh
$ sudo bash -x -e bazel-$(cat .bazelversion)-installer-darwin-x86_64.sh
$ bazel run //tools/lint:check

Bazel Buildifier および Clang Format の場合、次のコマンドは lint エラーを自動的に特定して修正します。

$ bazel run //tools/lint:lint

あるいは、個別のリンターを使用して lint チェックのみを実行したい場合は、 blackpyupgradebazel 、またはclang上記のコマンドに選択的に渡すことができます。

たとえば、 black特有の糸くずチェックは次のコマンドを使用して実行できます。

$ bazel run //tools/lint:check -- black

Bazel Buildifier と Clang Format を使用した lint 修正は、次の方法で実行できます。

$ bazel run //tools/lint:lint -- bazel clang

個々の Python ファイルに対してblackpyupgradeを使用した lint チェックは、次のコマンドを使用して実行できます。

$ bazel run //tools/lint:check -- black pyupgrade -- tensorflow_io/python/ops/version_ops.py

以下を使用して、Lint で個々の Python ファイルを黒と pyupgrade で修正します。

$ bazel run //tools/lint:lint -- black pyupgrade --  tensorflow_io/python/ops/version_ops.py

パイソン

macOS

macOS Catalina 10.15.7 では、システムが提供する Python 3.8.2 を使用して tensorflow-io をビルドできます。これを行うには、 tensorflowbazel両方が必要です。

#!/usr/bin/env bash

# Disable arm64 build by specifying only x86_64 arch.
# Only needed for macOS's system default python 3.8.2 on macOS 10.15.7
export ARCHFLAGS="-arch x86_64"

# Use following command to check if Xcode is correctly installed:
xcodebuild -version

# Show macOS's default python3
python3 --version

# Install Bazel version specified in .bazelversion
curl -OL https://github.com/bazelbuild/bazel/releases/download/$(cat .bazelversion)/bazel-$(cat .bazelversion)-installer-darwin-x86_64.sh
sudo bash -x -e bazel-$(cat .bazelversion)-installer-darwin-x86_64.sh

# Install tensorflow and configure bazel
sudo ./configure.sh

# Add any optimization on bazel command, e.g., --compilation_mode=opt,
#   --copt=-msse4.2, --remote_cache=, etc.
# export BAZEL_OPTIMIZATION=

# Build shared libraries
bazel build -s --verbose_failures $BAZEL_OPTIMIZATION //tensorflow_io/... //tensorflow_io_gcs_filesystem/...

# Once build is complete, shared libraries will be available in
# `bazel-bin/tensorflow_io/core`, `bazel-bin/tensorflow_io/python/ops` and
# it is possible to run tests with `pytest`, e.g.:
sudo python3 -m pip install pytest
TFIO_DATAPATH=bazel-bin python3 -m pytest -s -v tests/test_serialization.py
トラブルシューティング

Xcode がインストールされているにもかかわらず、 $ xcodebuild -versionが予期した出力を表示しない場合は、次のコマンドを使用して Xcode コマンド ラインを有効にする必要がある場合があります。

$ xcode-select -s /Applications/Xcode.app/Contents/Developer

変更を有効にするには、端末の再起動が必要になる場合があります。

出力例:

$ xcodebuild -version
Xcode 12.2
Build version 12B45b

Linux

Linux での tensorflow-io の開発は macOS と似ています。必要なパッケージは、gcc、g++、git、bazel、および python 3 です。ただし、デフォルトのシステムにインストールされているバージョン以外の gcc または python の新しいバージョンが必要になる場合があります。

Ubuntu 20.04

Ubuntu 20.04 には、gcc/g++、git、および Python 3 が必要です。次のコマンドにより、依存関係がインストールされ、Ubuntu 20.04 に共有ライブラリが構築されます。

#!/usr/bin/env bash

# Install gcc/g++, git, unzip/curl (for bazel), and python3
sudo apt-get -y -qq update
sudo apt-get -y -qq install gcc g++ git unzip curl python3-pip

# Install Bazel version specified in .bazelversion
curl -sSOL <a href="https://github.com/bazelbuild/bazel/releases/download/">https://github.com/bazelbuild/bazel/releases/download/</a>\\((cat .bazelversion)/bazel-\\)(cat .bazelversion)-installer-linux-x86_64.sh
sudo bash -x -e bazel-$(cat .bazelversion)-installer-linux-x86_64.sh

# Upgrade pip
sudo python3 -m pip install -U pip

# Install tensorflow and configure bazel
sudo ./configure.sh

# Alias python3 to python, needed by bazel
sudo ln -s /usr/bin/python3 /usr/bin/python

# Add any optimization on bazel command, e.g., --compilation_mode=opt,
#   --copt=-msse4.2, --remote_cache=, etc.
# export BAZEL_OPTIMIZATION=

# Build shared libraries
bazel build -s --verbose_failures $BAZEL_OPTIMIZATION //tensorflow_io/... //tensorflow_io_gcs_filesystem/...

# Once build is complete, shared libraries will be available in
# `bazel-bin/tensorflow_io/core`, `bazel-bin/tensorflow_io/python/ops` and
# it is possible to run tests with `pytest`, e.g.:
sudo python3 -m pip install pytest
TFIO_DATAPATH=bazel-bin python3 -m pytest -s -v tests/test_serialization.py
CentOS 8

CentOS 8 の共有ライブラリを構築する手順は、上記の Ubuntu 20.04 と似ていますが、次の点が異なります。

sudo yum install -y python3 python3-devel gcc gcc-c++ git unzip which make

代わりに、gcc/g++、git、unzip/that (bazel 用)、および python3 をインストールするために使用する必要があります。

CentOS 7

CentOS 7 では、デフォルトの Python と gcc のバージョンが古すぎて、tensorflow-io の共有ライブラリ (.so) をビルドできません。代わりに、開発者ツールセットと rh-python36 によって提供される gcc を使用する必要があります。また、CentOS にインストールされた libstdc++ と devtoolset による新しい gcc バージョンの不一致を避けるために、libstdc++ を静的にリンクする必要があります。

さらに、ファイル システム プラグイン用に静的にリンクされたライブラリ内のシンボルの重複を避けるために、特別なフラグ--//tensorflow_io/core:static_buildを Bazel に渡す必要があります。

以下は、bazel、devtoolset-9、rh-python36 をインストールし、共有ライブラリをビルドします。

#!/usr/bin/env bash

# Install centos-release-scl, then install gcc/g++ (devtoolset), git, and python 3
sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-9 git rh-python36 make

# Install Bazel version specified in .bazelversion
curl -sSOL <a href="https://github.com/bazelbuild/bazel/releases/download/">https://github.com/bazelbuild/bazel/releases/download/</a>\\((cat .bazelversion)/bazel-\\)(cat .bazelversion)-installer-linux-x86_64.sh
sudo bash -x -e bazel-$(cat .bazelversion)-installer-linux-x86_64.sh

# Upgrade pip
scl enable rh-python36 devtoolset-9 \
    'python3 -m pip install -U pip'

# Install tensorflow and configure bazel with rh-python36
scl enable rh-python36 devtoolset-9 \
    './configure.sh'

# Add any optimization on bazel command, e.g., --compilation_mode=opt,
#   --copt=-msse4.2, --remote_cache=, etc.
# export BAZEL_OPTIMIZATION=

# Build shared libraries, notice the passing of --//tensorflow_io/core:static_build
BAZEL_LINKOPTS="-static-libstdc++ -static-libgcc" BAZEL_LINKLIBS="-lm -l%:libstdc++.a" \
  scl enable rh-python36 devtoolset-9 \
    'bazel build -s --verbose_failures $BAZEL_OPTIMIZATION --//tensorflow_io/core:static_build //tensorflow_io/...'

# Once build is complete, shared libraries will be available in
# `bazel-bin/tensorflow_io/core`, `bazel-bin/tensorflow_io/python/ops` and
# it is possible to run tests with `pytest`, e.g.:
scl enable rh-python36 devtoolset-9 \
    'python3 -m pip install pytest'

TFIO_DATAPATH=bazel-bin \
  scl enable rh-python36 devtoolset-9 \
    'python3 -m pytest -s -v tests/test_serialization.py'

ドッカー

Python 開発の場合、ここにある参照 Dockerfile を使用して、ソースから TensorFlow I/O パッケージ ( tensorflow-io ) を構築できます。さらに、事前に構築された開発イメージも使用できます。

# Pull (if necessary) and start the devel container
\\( docker run -it --rm --name tfio-dev --net=host -v \\){PWD}:/v -w /v tfsigio/tfio:latest-devel bash

# Inside the docker container, ./configure.sh will install TensorFlow or use existing install
(tfio-dev) root@docker-desktop:/v$ ./configure.sh

# Clean up exisiting bazel build's (if any)
(tfio-dev) root@docker-desktop:/v$ rm -rf bazel-*

# Build TensorFlow I/O C++. For compilation optimization flags, the default (-march=native)
# optimizes the generated code for your machine's CPU type.
# Reference: <a href="https://www.tensorflow.orginstall/source#configuration_options">https://www.tensorflow.orginstall/source#configuration_options</a>).

# NOTE: Based on the available resources, please change the number of job workers to:
# -j 4/8/16 to prevent bazel server terminations and resource oriented build errors.

(tfio-dev) root@docker-desktop:/v$ bazel build -j 8 --copt=-msse4.2 --copt=-mavx --compilation_mode=opt --verbose_failures --test_output=errors --crosstool_top=//third_party/toolchains/gcc7_manylinux2010:toolchain //tensorflow_io/... //tensorflow_io_gcs_filesystem/...


# Run tests with PyTest, note: some tests require launching additional containers to run (see below)
(tfio-dev) root@docker-desktop:/v$ pytest -s -v tests/
# Build the TensorFlow I/O package
(tfio-dev) root@docker-desktop:/v$ python setup.py bdist_wheel

ビルドが成功すると、パッケージ ファイルdist/tensorflow_io-*.whlが生成されます。

パイソンホイール

bazel のビルドが完了した後、次のコマンドを使用して Python ホイールをビルドできます。

$ python setup.py bdist_wheel --data bazel-bin

.whl ファイルは dist ディレクトリにあります。 bazel-bin tensorflow_ioパッケージ ディレクトリの外にあるため、setup.py が必要な共有オブジェクトを見つけるために、bazel バイナリ ディレクトリbazel-bin--data引数で渡す必要があることに注意してください。

あるいは、ソースのインストールは次のように行うこともできます。

$ TFIO_DATAPATH=bazel-bin python -m pip install .

TFIO_DATAPATH=bazel-binも同じ理由で渡されました。

-eを使用してインストールする場合は、上記とは異なることに注意してください。の

$ TFIO_DATAPATH=bazel-bin python -m pip install -e .

TFIO_DATAPATH=bazel-binを指定しても、共有オブジェクトは自動的にインストールされません。代わりに、インストール後にプログラムを実行するたびにTFIO_DATAPATH=bazel-binを渡す必要があります。

$ TFIO_DATAPATH=bazel-bin python

>>> import tensorflow_io as tfio
>>> ...

テスト

一部のテストでは、実行前にテスト コンテナーを起動するか、関連ツールのローカル インスタンスを起動する必要があります。たとえば、kafka、zookeeper、およびスキーマ レジストリのローカル インスタンスを開始する kafka 関連のテストを実行するには、次を使用します。

# Start the local instances of kafka, zookeeper and schema-registry
$ bash -x -e tests/test_kafka/kafka_test.sh

# Run the tests
$ TFIO_DATAPATH=bazel-bin pytest -s -vv tests/test_kafka.py

ElasticsearchMongoDBなどのツールに関連付けられたDatasetsをテストするには、システム上で Docker が使用可能である必要があります。このようなシナリオでは、次を使用します。

# Start elasticsearch within docker container
$ bash tests/test_elasticsearch/elasticsearch_test.sh start

# Run the tests
$ TFIO_DATAPATH=bazel-bin pytest -s -vv tests/test_elasticsearch.py

# Stop and remove the container
$ bash tests/test_elasticsearch/elasticsearch_test.sh stop

さらに、データがtestsディレクトリ自体に提供されているため、 tensorflow-ioの一部の機能をテストする場合、追加のツールを起動する必要はありません。たとえば、 parquetデータセットに関連するテストを実行するには、次を使用します。

# Just run the test
$ TFIO_DATAPATH=bazel-bin pytest -s -vv tests/test_parquet.py

R

ここでは、R パッケージをテストに直接使用できるように、参照 Dockerfile を提供します。次の方法でビルドできます。

$ docker build -t tfio-r-dev -f R-package/scripts/Dockerfile .

コンテナー内では、R セッションを開始し、サンプルのHadoop SequenceFile string.seqからSequenceFileDatasetをインスタンス化し、次のようにデータセットに対してtfdatasets パッケージによって提供される変換関数を使用できます。

library(tfio)
dataset <- sequence_file_dataset("R-package/tests/testthat/testdata/string.seq") %>%
    dataset_repeat(2)

sess <- tf$Session()
iterator <- make_iterator_one_shot(dataset)
next_batch <- iterator_get_next(iterator)

until_out_of_range({
  batch <- sess$run(next_batch)
  print(batch)
})