การพัฒนา

เอกสารประกอบด้วยข้อมูลที่จำเป็นสำหรับการตั้งค่าสภาพแวดล้อมการพัฒนาและสร้างแพ็คเกจ tensorflow-io จากแหล่งที่มาบนแพลตฟอร์มต่างๆ เมื่อการตั้งค่าเสร็จสมบูรณ์ โปรดดู STYLE_GUIDE เพื่อดูหลักเกณฑ์ในการเพิ่มการดำเนินการใหม่

การตั้งค่า IDE

สำหรับคำแนะนำเกี่ยวกับวิธีกำหนดค่า Visual Studio Code สำหรับการพัฒนา TensorFlow I/O โปรดดู เอกสาร นี้

ผ้าสำลี

รหัสของ TensorFlow I/O สอดคล้องกับ Bazel Buildifier, Clang Format, Black และ Pyupgrade โปรดใช้คำสั่งต่อไปนี้เพื่อตรวจสอบซอร์สโค้ดและระบุปัญหาผ้าสำลี:

# 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

อีกทางหนึ่ง หากคุณต้องการดำเนินการตรวจสอบผ้าสำลีโดยใช้แต่ละ Linters คุณสามารถเลือกส่งผ่าน black , pyupgrade , bazel หรือ clang ไปยังคำสั่งข้างต้นได้

ตัวอย่างเช่น การตรวจสอบผ้าสำลี black โดยเฉพาะสามารถทำได้โดยใช้:

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

การแก้ไข Lint โดยใช้ Bazel Buildifier และ Clang Format สามารถทำได้โดยใช้:

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

การตรวจสอบผ้าสำลีโดยใช้ black และ pyupgrade สำหรับไฟล์ python แต่ละไฟล์สามารถทำได้โดยใช้:

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

Lint แก้ไขไฟล์หลามแต่ละไฟล์ด้วย black และ pyupgrade โดยใช้:

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

หลาม

ระบบปฏิบัติการ macOS

บน macOS Catalina 10.15.7 คุณสามารถสร้าง tensorflow-io ด้วยระบบที่จัดเตรียมไว้ให้ python 3.8.2 ต้องใช้ทั้ง tensorflow และ bazel จึงจะทำเช่นนั้นได้

#!/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

ลินุกซ์

การพัฒนา tensorflow-io บน Linux นั้นคล้ายคลึงกับ macOS แพ็คเกจที่จำเป็นคือ gcc, g++, git, bazel และ python 3 แต่อาจจำเป็นต้องใช้ gcc หรือ python เวอร์ชันใหม่กว่า นอกเหนือจากเวอร์ชันที่ติดตั้งโดยระบบเริ่มต้น

อูบุนตู 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
เซนโอเอส 8

ขั้นตอนในการสร้างไลบรารี่ที่ใช้ร่วมกันสำหรับ CentOS 8 นั้นคล้ายคลึงกับ Ubuntu 20.04 ข้างต้นยกเว้นกรณีนั้น

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

ควรใช้แทนเพื่อติดตั้ง gcc/g++, git, unzip/ which (สำหรับ bazel) และ python3

เซนโอเอส 7

บน CentOS 7 เวอร์ชัน python และ gcc เริ่มต้นนั้นเก่าเกินไปที่จะสร้างไลบรารี่ที่แชร์ของ tensorflow-io (.so) ควรใช้ gcc ที่จัดทำโดย Developer Toolset และ rh-python36 แทน นอกจากนี้ libstdc++ จะต้องเชื่อมโยงแบบคงที่เพื่อหลีกเลี่ยงความคลาดเคลื่อนของ libstdc++ ที่ติดตั้งบน CentOS เทียบกับเวอร์ชัน gcc ที่ใหม่กว่าโดย devtoolset

นอกจากนี้ จะต้องส่งแฟล็กพิเศษ --//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 setup.py bdist_wheel --data bazel-bin

ไฟล์ .whl จะพร้อมใช้งานในไดเร็กทอรี dist โปรดทราบว่าจะต้องส่งไดเร็กทอรีไบนารี bazel bazel-bin ด้วย --data args เพื่อให้ setup.py ค้นหาวัตถุแชร์ที่จำเป็น เนื่องจาก bazel-bin อยู่นอกไดเร็กทอรีแพ็คเกจ tensorflow_io

หรืออีกทางหนึ่ง การติดตั้งซอร์สสามารถทำได้ด้วย:

$ 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 ซึ่งจะเริ่มอินสแตนซ์ภายในเครื่องของ kafka, Zookeeper และ schema-registry ให้ใช้:

# 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

การทดสอบ Datasets ที่เกี่ยวข้องกับเครื่องมือ เช่น Elasticsearch หรือ MongoDB จำเป็นต้องมีนักเทียบท่าให้พร้อมใช้งานบนระบบ ในสถานการณ์เช่นนี้ ให้ใช้:

# 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

นอกจากนี้ การทดสอบคุณสมบัติบางอย่างของ tensorflow-io ไม่จำเป็นต้องให้คุณใช้เครื่องมือเพิ่มเติมใดๆ เนื่องจากมีการระบุข้อมูลไว้ในไดเร็กทอรี tests แล้ว ตัวอย่างเช่น หากต้องการรันการทดสอบที่เกี่ยวข้องกับชุดข้อมูล parquet ให้ใช้:

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

เรามีไฟล์อ้างอิง Dockerfile ไว้ที่นี่ เพื่อให้คุณสามารถใช้แพ็คเกจ R สำหรับการทดสอบได้โดยตรง คุณสามารถสร้างมันได้ผ่านทาง:

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

ภายในคอนเทนเนอร์ คุณสามารถเริ่มเซสชัน R ของคุณ สร้างอินสแตนซ์ SequenceFileDataset จากตัวอย่าง Hadoop SequenceFile string.seq จากนั้นใช้ ฟังก์ชันการแปลง ใดๆ ที่ได้รับจาก แพ็คเกจ 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)
})