Phát triển

Tài liệu chứa thông tin cần thiết để thiết lập môi trường phát triển và xây dựng gói tensorflow-io từ nguồn trên nhiều nền tảng khác nhau. Sau khi thiết lập hoàn tất, vui lòng tham khảo STYLE_GUIDE để biết hướng dẫn về cách thêm hoạt động mới.

Cài đặt IDE

Để biết hướng dẫn về cách định cấu hình Visual Studio Code để phát triển I/O TensorFlow, vui lòng tham khảo tài liệu này.

xơ vải

Mã của TensorFlow I/O tuân theo Bazel Buildifier, Clang Format, Black và Pyupgrade. Vui lòng sử dụng lệnh sau để kiểm tra mã nguồn và xác định các vấn đề về lỗi mã nguồn:

# 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

Đối với Bazel Buildifier và Clang Format, lệnh sau sẽ tự động xác định và sửa mọi lỗi lint:

$ bazel run //tools/lint:lint

Ngoài ra, nếu bạn chỉ muốn thực hiện kiểm tra lint bằng cách sử dụng từng linters thì bạn có thể chuyển một cách có chọn black , pyupgrade , bazel hoặc clang cho các lệnh trên.

Ví dụ: có thể thực hiện kiểm tra lint cụ thể black bằng cách sử dụng:

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

Có thể thực hiện sửa lỗi Lint bằng Bazel Buildifier và Clang Format bằng cách sử dụng:

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

Có thể thực hiện kiểm tra Lint bằng cách sử dụng blackpyupgrade cho một tệp python riêng lẻ bằng cách sử dụng:

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

Lint sửa một tệp python riêng lẻ có màu đen và pyupgrade bằng cách sử dụng:

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

Python

hệ điều hành Mac

Trên macOS Catalina 10.15.7, có thể xây dựng tenorflow-io với hệ thống được cung cấp python 3.8.2. Cả tensorflowbazel đều cần thiết để làm như vậy.

#!/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
Khắc phục sự cố

Nếu Xcode được cài đặt, nhưng $ xcodebuild -version không hiển thị kết quả mong đợi, bạn có thể cần bật dòng lệnh Xcode bằng lệnh:

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

Có thể cần phải khởi động lại thiết bị đầu cuối để các thay đổi có hiệu lực.

Đầu ra mẫu:

$ xcodebuild -version
Xcode 12.2
Build version 12B45b

Linux

Việc phát triển tensorflow-io trên Linux cũng tương tự như macOS. Các gói bắt buộc là gcc, g++, git, bazel và python 3. Tuy nhiên, các phiên bản mới hơn của gcc hoặc python, ngoài các phiên bản cài đặt hệ thống mặc định có thể được yêu cầu.

Ubuntu 20.04

Ubuntu 20.04 yêu cầu gcc/g++, git và python 3. Phần sau đây sẽ cài đặt các phần phụ thuộc và xây dựng thư viện dùng chung trên 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

Các bước xây dựng thư viện dùng chung cho CentOS 8 tương tự như Ubuntu 20.04 ở trên ngoại trừ

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

thay vào đó nên sử dụng để cài đặt gcc/g++, git, unzip/which (đối với bazel) và python3.

CentOS 7

Trên CentOS 7, phiên bản python và gcc mặc định đã quá cũ để xây dựng các thư viện dùng chung của tensorflow-io (.so). Thay vào đó, nên sử dụng gcc do Bộ công cụ dành cho nhà phát triển và rh-python36 cung cấp. Ngoài ra, libstdc++ phải được liên kết tĩnh để tránh sự khác biệt giữa libstdc++ được cài đặt trên CentOS so với phiên bản gcc mới hơn của devtoolset.

Hơn nữa, một cờ đặc biệt --//tensorflow_io/core:static_build phải được chuyển tới Bazel để tránh trùng lặp các ký hiệu trong các thư viện được liên kết tĩnh cho các plugin hệ thống tệp.

Phần sau đây sẽ cài đặt bazel, devtoolset-9, rh-python36 và xây dựng các thư viện dùng chung:

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

Docker

Để phát triển Python, có thể sử dụng Dockerfile tham chiếu ở đây để xây dựng gói I/O TensorFlow ( tensorflow-io ) từ nguồn. Ngoài ra, hình ảnh phát triển dựng sẵn cũng có thể được sử dụng:

# 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

Tệp gói dist/tensorflow_io-*.whl sẽ được tạo sau khi quá trình xây dựng thành công.

Bánh xe Python

Có thể tạo bánh xe python sau khi quá trình xây dựng bazel hoàn tất bằng lệnh sau:

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

Tệp .whl sẽ có sẵn trong thư mục dist. Lưu ý thư mục nhị phân bazel bazel-bin phải được chuyển bằng --data args để setup.py xác định vị trí các đối tượng chia sẻ cần thiết, vì bazel-bin nằm ngoài thư mục gói tensorflow_io .

Ngoài ra, cài đặt nguồn có thể được thực hiện với:

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

với TFIO_DATAPATH=bazel-bin được thông qua vì lý do tương tự.

Lưu ý cài đặt bằng -e khác với cách trên. Các

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

sẽ không tự động cài đặt đối tượng được chia sẻ ngay cả với TFIO_DATAPATH=bazel-bin . Thay vào đó, TFIO_DATAPATH=bazel-bin phải được thông qua mỗi khi chương trình được chạy sau khi cài đặt:

$ TFIO_DATAPATH=bazel-bin python

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

Kiểm tra

Một số thử nghiệm yêu cầu khởi chạy vùng chứa thử nghiệm hoặc khởi động phiên bản cục bộ của công cụ được liên kết trước khi chạy. Ví dụ: để chạy các thử nghiệm liên quan đến kafka sẽ bắt đầu một phiên bản cục bộ của kafka, người quản lý vườn thú và sổ đăng ký lược đồ, hãy sử dụng:

# 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

Kiểm tra Datasets được liên kết với các công cụ như Elasticsearch hoặc MongoDB yêu cầu docker phải có sẵn trên hệ thống. Trong những tình huống như vậy, hãy sử dụng:

# 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

Ngoài ra, việc kiểm tra một số tính năng của tensorflow-io không yêu cầu bạn phải sử dụng bất kỳ công cụ bổ sung nào vì dữ liệu đã được cung cấp trong chính thư mục tests . Ví dụ: để chạy thử nghiệm liên quan đến tập dữ liệu parquet , hãy sử dụng:

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

R

Chúng tôi cung cấp Dockerfile tham khảo tại đây cho bạn để bạn có thể sử dụng trực tiếp gói R để thử nghiệm. Bạn có thể xây dựng nó thông qua:

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

Bên trong vùng chứa, bạn có thể bắt đầu phiên R của mình, khởi tạo SequenceFileDataset từ một ví dụ Hadoop SequenceFile string.seq và sau đó sử dụng bất kỳ hàm chuyển đổi nào được cung cấp bởi gói tfdatasets trên tập dữ liệu như sau:

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)
})