התפתחות

המסמך מכיל את המידע הדרוש להקמת סביבת הפיתוח ובניית חבילת 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, הפקודה הבאה תזהה ותתקן אוטומטית שגיאות מוך:

$ bazel run //tools/lint:lint

לחלופין, אם ברצונך לבצע בדיקת מוך בלבד באמצעות ליטרים בודדים, תוכל להעביר באופן סלקטיבי black , pyupgrade , bazel , או clang לפקודות לעיל.

לדוגמה, ניתן לבצע בדיקת מוך ספציפית black באמצעות:

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

ניתן לבצע תיקון מוך באמצעות Bazel Buildifier ו-Clang Format באמצעות:

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

ניתן לבצע בדיקת מוך באמצעות black ו- pyupgrade עבור קובץ פיתון בודד באמצעות:

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

מוך תקן קובץ פיתון בודד עם שחור ושדרוג באמצעות:

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

פִּיתוֹן

macOS

ב-macOS Catalina 10.15.7, אפשר לבנות tensorflow-io עם פיתון 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

אובונטו 20.04 דורש gcc/g++, git ו-python 3. הפעולות הבאות יתקין תלות ויבנה את הספריות המשותפות באובונטו 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 דומים לאובנטו 20.04 לעיל, פרט לכך

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

יש להשתמש במקום זאת כדי להתקין את gcc/g++, git, unzip/which (עבור bazel) ו-python3.

CentOS 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-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
>>> ...

בדיקה

בדיקות מסוימות דורשות הפעלת קונטיינר בדיקה או התחלת מופע מקומי של הכלי המשויך לפני ההפעלה. לדוגמה, כדי להפעיל בדיקות הקשורות לקפקא שיפעילו מופע מקומי של קפקא, שומר גן חיות וסכימה-רישום, השתמש ב:

# 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 מחייבים את 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

בנוסף, בדיקת תכונות מסוימות של 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)
})