Skip to content

Commit 088c641

Browse files
cantoniostf-text-github-robot
authored andcommitted
Fix TF-text.
This is a combination of a collection of changes to fix the TF-text build. All are required for a successful build. 1. Upgrade ICU to v77.1 - Requires changes to ICU build file, updating ICU data, updating test goldens - Added `custom_http_archive` rule to allow excluding files. 2. Replace `tensorflow.bzl:pybind_extension` with `tsl.bzl:pybind_extension` - Forces creation of pybind .so files to include in the wheel. 3. Patch TF build file to exclude `libtensorflow_framework` source files. - Avoids ODR issues causing double-registrations. 4. Update requirements files - TF 3.9, 3.10 are somewhat fundamentally broken because of tensorflow-metadata, which restricts the protobuf version below what TF requires. Though this turns out not to be a hard requirement, by skipping the requirements.update in `configure` and manually setting package versions, we can create a successful build. 5. Replace the `build_pip_package.sh` with a python version - Allows using the hermetic python toolchain to build the wheel. Avoids the need for docker. 6. Fix random missing deps. PiperOrigin-RevId: 847172303
1 parent f0c7929 commit 088c641

27 files changed

Lines changed: 2865 additions & 13567 deletions

WORKSPACE

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
workspace(name = "org_tensorflow_text")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
load("//third_party/bazel:http.bzl", "custom_http_archive")
45

5-
http_archive(
6+
custom_http_archive(
67
name = "icu",
7-
strip_prefix = "icu-release-64-2",
8-
sha256 = "dfc62618aa4bd3ca14a3df548cd65fe393155edd213e49c39f3a30ccd618fc27",
8+
exclude = [
9+
# Build file does not contain all required targets.
10+
"icu4c/source/common/BUILD.bazel",
11+
],
12+
files = {
13+
"BUILD.bazel": "//third_party/icu:BUILD.bzl",
14+
},
15+
sha256 = "e424ba5282d95ad38b52639a08fb82164f0b0cbd7f17b53ae16bf14f8541855f",
16+
strip_prefix = "icu-release-77-1",
917
urls = [
10-
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/unicode-org/icu/archive/release-64-2.zip",
11-
"https://github.com/unicode-org/icu/archive/release-64-2.zip",
18+
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/unicode-org/icu/archive/release-77-1.zip",
19+
"https://github.com/unicode-org/icu/archive/release-77-1.zip",
1220
],
13-
build_file = "//third_party/icu:BUILD.bzl",
14-
patches = ["//third_party/icu:udata.patch"],
15-
patch_args = ["-p1"],
1621
)
1722

1823
http_archive(
1924
name = "com_google_sentencepiece",
20-
strip_prefix = "sentencepiece-0.1.96",
25+
build_file = "//third_party/sentencepiece:BUILD",
26+
patch_args = ["-p1"],
27+
patches = ["//third_party/sentencepiece:sp.patch"],
2128
sha256 = "8409b0126ebd62b256c685d5757150cf7fcb2b92a2f2b98efb3f38fc36719754",
29+
strip_prefix = "sentencepiece-0.1.96",
2230
urls = [
23-
"https://github.com/google/sentencepiece/archive/refs/tags/v0.1.96.zip"
31+
"https://github.com/google/sentencepiece/archive/refs/tags/v0.1.96.zip",
2432
],
25-
build_file = "//third_party/sentencepiece:BUILD",
26-
patches = ["//third_party/sentencepiece:sp.patch"],
27-
patch_args = ["-p1"],
2833
)
2934

3035
http_archive(
3136
name = "cppitertools",
32-
urls = ["https://github.com/ryanhaining/cppitertools/archive/refs/tags/v2.0.zip"],
3337
sha256 = "e56741b108d6baced98c4ccd83fd0d5a545937f2845978799c28d0312c0dee3d",
3438
strip_prefix = "cppitertools-2.0",
39+
urls = ["https://github.com/ryanhaining/cppitertools/archive/refs/tags/v2.0.zip"],
3540
)
3641

3742
http_archive(
@@ -56,10 +61,11 @@ http_archive(
5661

5762
http_archive(
5863
name = "org_tensorflow",
59-
strip_prefix = "tensorflow-40998f44c0c500ce0f6e3b1658dfbc54f838a82a",
60-
sha256 = "5a5bc4599964c71277dcac0d687435291e5810d2ac2f6283cc96736febf73aaf",
64+
patch_args = ["-p1"],
65+
patches = ["//third_party/tensorflow:tensorflow.core.BUILD.patch"],
66+
strip_prefix = "tensorflow-72fbba3d20f4616d7312b5e2b7f79daf6e82f2fa",
6167
urls = [
62-
"https://github.com/tensorflow/tensorflow/archive/40998f44c0c500ce0f6e3b1658dfbc54f838a82a.zip"
68+
"https://github.com/tensorflow/tensorflow/archive/72fbba3d20f4616d7312b5e2b7f79daf6e82f2fa.zip",
6369
],
6470
)
6571

@@ -74,13 +80,13 @@ http_archive(
7480

7581
http_archive(
7682
name = "pybind11",
83+
build_file = "//third_party/pybind11:BUILD.bzl",
84+
sha256 = "efc901aa0aab439a3fea6efeaf930b5a349fb06394bf845c64ce15a9cf8f0240",
85+
strip_prefix = "pybind11-2.13.4",
7786
urls = [
7887
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/pybind/pybind11/archive/v2.13.4.tar.gz",
7988
"https://github.com/pybind/pybind11/archive/v2.13.4.tar.gz",
8089
],
81-
sha256 = "efc901aa0aab439a3fea6efeaf930b5a349fb06394bf845c64ce15a9cf8f0240",
82-
strip_prefix = "pybind11-2.13.4",
83-
build_file = "//third_party/pybind11:BUILD.bzl",
8490
)
8591

8692
http_archive(
@@ -95,31 +101,37 @@ load("@org_tensorflow//third_party/py:python_init_rules.bzl", "python_init_rules
95101

96102
python_init_rules()
97103

98-
load("//tensorflow_text:tftext.bzl", "py_deps_profile")
104+
load("//third_party/bazel:py_deps_profile.bzl", "py_deps_profile")
99105

100106
py_deps_profile(
101107
name = "release_or_nightly",
102-
requirements_in = "//oss_scripts/pip_package:requirements.in",
103-
pip_repo_name = "pypi",
104108
deps_map = {
105-
"tensorflow": ["tf-nightly", "tf_header_lib", "libtensorflow_framework"],
106-
"tf-keras": ["tf-keras-nightly"]
109+
"tensorflow": [
110+
"tf-nightly",
111+
"tf_headers",
112+
"tf_header_lib",
113+
"libtensorflow_framework",
114+
],
115+
"tf-keras": ["tf-keras-nightly"],
107116
},
117+
pip_repo_name = "pypi",
118+
requirements_in = "//oss_scripts/pip_package:requirements.in",
108119
switch = {
109-
"IS_NIGHTLY": "nightly"
110-
}
120+
"IS_NIGHTLY": "nightly",
121+
},
111122
)
112123

113124
load("@org_tensorflow//third_party/py:python_init_repositories.bzl", "python_init_repositories")
114125

115126
python_init_repositories(
127+
default_python_version = "system",
116128
requirements = {
117129
"3.9": "//oss_scripts/pip_package:requirements_lock_3_9.txt",
118130
"3.10": "//oss_scripts/pip_package:requirements_lock_3_10.txt",
119131
"3.11": "//oss_scripts/pip_package:requirements_lock_3_11.txt",
120132
"3.12": "//oss_scripts/pip_package:requirements_lock_3_12.txt",
133+
"3.13": "//oss_scripts/pip_package:requirements_lock_3_13.txt",
121134
},
122-
default_python_version = "system",
123135
)
124136

125137
load("@org_tensorflow//third_party/py:python_init_toolchains.bzl", "python_init_toolchains")
@@ -136,18 +148,28 @@ install_deps()
136148

137149
# Initialize TensorFlow dependencies.
138150
load("@org_tensorflow//tensorflow:workspace3.bzl", "tf_workspace3")
151+
139152
tf_workspace3()
153+
140154
load("@org_tensorflow//tensorflow:workspace2.bzl", "tf_workspace2")
155+
141156
tf_workspace2()
157+
142158
load("@org_tensorflow//tensorflow:workspace1.bzl", "tf_workspace1")
159+
143160
tf_workspace1()
161+
144162
load("@org_tensorflow//tensorflow:workspace0.bzl", "tf_workspace0")
163+
145164
tf_workspace0()
146165

147166
# Set up Android.
148167
load("@org_tensorflow//third_party/android:android_configure.bzl", "android_configure")
149-
android_configure(name="local_config_android")
168+
169+
android_configure(name = "local_config_android")
170+
150171
load("@local_config_android//:android.bzl", "android_workspace")
172+
151173
android_workspace()
152174

153175
load(

oss_scripts/configure.sh

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,22 @@ function is_macos() {
3232
# Remove .bazelrc if it already exist
3333
[ -e .bazelrc ] && rm .bazelrc
3434

35-
if [[ $(pip show tensorflow) == *tensorflow* ]] ||
36-
[[ $(pip show tensorflow-macos) == *tensorflow-macos* ]] ||
37-
[[ $(pip show tf-nightly) == *tf-nightly* ]]; then
35+
if (which python3) | grep -q "python3"; then
36+
installed_python="python3"
37+
elif (which python) | grep -q "python"; then
38+
installed_python="python"
39+
fi
40+
41+
if [[ $($installed_python -m pip show tensorflow) == *tensorflow* ]] ||
42+
[[ $($installed_python -m pip show tensorflow-macos) == *tensorflow-macos* ]] ||
43+
[[ $($installed_python -m pip show tf-nightly) == *tf-nightly* ]]; then
3844
echo 'Using installed tensorflow.'
3945
else
4046
echo 'Installing tensorflow.'
4147
if [[ "$IS_NIGHTLY" == "nightly" ]]; then
42-
pip install tf-nightly
48+
$installed_python -m pip install --no-deps tf-nightly
4349
else
44-
pip install tensorflow==2.18.0
50+
$installed_python -m pip install --no-deps tensorflow==2.20
4551
fi
4652
fi
4753

@@ -54,12 +60,7 @@ curl https://raw.githubusercontent.com/tensorflow/tensorflow/master/.bazelrc -o
5460
sed -i -e 's/build --noincompatible_remove_legacy_whole_archive//' .bazelrc
5561

5662
write_to_bazelrc "build:manylinux2014 --config=release_cpu_linux"
57-
58-
if (which python3) | grep -q "python3"; then
59-
installed_python="python3"
60-
elif (which python) | grep -q "python"; then
61-
installed_python="python"
62-
fi
63+
write_to_bazelrc "test --test_env=TF_USE_LEGACY_KERAS=1"
6364

6465
if [ -z "$HERMETIC_PYTHON_VERSION" ]; then
6566
if [ -n "$PY_VERSION" ]; then
@@ -70,13 +71,15 @@ if [ -z "$HERMETIC_PYTHON_VERSION" ]; then
7071
fi
7172
export HERMETIC_PYTHON_VERSION
7273

73-
echo "TF_VERSION=$TF_VERSION"
74-
REQUIREMENTS_EXTRA_FLAGS="--upgrade"
75-
if [[ "$TF_VERSION" == *"rc"* ]]; then
76-
REQUIREMENTS_EXTRA_FLAGS="$REQUIREMENTS_EXTRA_FLAGS --pre"
77-
fi
78-
79-
bazel run //oss_scripts/pip_package:requirements.update -- $REQUIREMENTS_EXTRA_FLAGS
74+
# TODO - Re-enable requirements update when presubmits changed to python 3.11.
75+
# # Only auto-upgrade requirements for nightly.
76+
# if [[ "$IS_NIGHTLY" == "nightly" ]]; then
77+
# REQUIREMENTS_EXTRA_FLAGS="--upgrade"
78+
# if [[ "$TF_VERSION" == *"rc"* ]]; then
79+
# REQUIREMENTS_EXTRA_FLAGS="$REQUIREMENTS_EXTRA_FLAGS --pre"
80+
# fi
81+
# bazel run //oss_scripts/pip_package:requirements.update -- $REQUIREMENTS_EXTRA_FLAGS
82+
# fi
8083

8184
TF_ABIFLAG=$(bazel run //oss_scripts/pip_package:tensorflow_build_info -- abi)
8285
SHARED_LIBRARY_NAME="libtensorflow_framework.so.2"

oss_scripts/pip_package/BUILD

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ py_binary(
2727
],
2828
)
2929

30-
sh_binary(
30+
py_binary(
3131
name = "build_pip_package",
32-
srcs = ["build_pip_package.sh"],
32+
srcs = ["build_pip_package.py"],
3333
data = [
3434
"LICENSE",
3535
"MANIFEST.in",
3636
"setup.nightly.py",
3737
"setup.py",
3838
"//tensorflow_text",
3939
],
40+
deps = [
41+
"@pypi_setuptools//:pkg",
42+
],
4043
)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# coding=utf-8
2+
# Copyright 2025 TF.Text Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Builds the tensorflow_text pip package."""
17+
18+
import os
19+
import pathlib
20+
import platform
21+
import shutil
22+
import subprocess
23+
import sys
24+
import tempfile
25+
26+
27+
def is_windows():
28+
# Matches the bash logic for msys/mingw/cygwin/windows
29+
return (
30+
platform.system()
31+
.lower()
32+
.startswith(("windows", "msys", "mingw", "cygwin"))
33+
)
34+
35+
36+
def is_macos():
37+
return platform.system().lower() == "darwin"
38+
39+
40+
def is_nightly():
41+
return os.environ.get("IS_NIGHTLY") == "nightly"
42+
43+
44+
def get_platform_name():
45+
if not is_macos():
46+
return []
47+
48+
# Check architecture for macOS
49+
arch = platform.machine().lower()
50+
if arch == "arm64":
51+
return ["--plat-name", "macosx_11_0_arm64"]
52+
else:
53+
return ["--plat-name", "macosx-10.9-x86_64"]
54+
55+
56+
def main():
57+
# Handle Bazel workspace directory
58+
workspace_dir = os.environ.get("BUILD_WORKSPACE_DIRECTORY")
59+
if workspace_dir:
60+
os.chdir(workspace_dir)
61+
62+
# Output directory logic
63+
output_dir_arg = (
64+
sys.argv[1] if len(sys.argv) > 1 else "/tmp/tensorflow_text_pkg"
65+
)
66+
output_path = pathlib.Path(output_dir_arg).resolve()
67+
output_path.mkdir(parents=True, exist_ok=True)
68+
69+
print(f"=== Destination directory: {output_path}")
70+
print(f"=== Current directory: {pathlib.Path.cwd()}")
71+
72+
# Verify bazel-bin exists
73+
if not pathlib.Path("bazel-bin/tensorflow_text").exists():
74+
print(
75+
"ERROR: Could not find bazel-bin. Did you run from the root of the"
76+
" build tree?",
77+
file=sys.stderr,
78+
)
79+
sys.exit(1)
80+
81+
# Determine runfiles path
82+
if is_windows():
83+
runfiles_base = pathlib.Path(
84+
"bazel-bin/oss_scripts/pip_package/build_pip_package.exe.runfiles"
85+
)
86+
else:
87+
runfiles_base = pathlib.Path(
88+
"bazel-bin/oss_scripts/pip_package/build_pip_package.runfiles"
89+
)
90+
91+
source_root = runfiles_base / "org_tensorflow_text"
92+
93+
# Create temp directory
94+
with tempfile.TemporaryDirectory() as temp_dir:
95+
temp_path = pathlib.Path(temp_dir)
96+
print(f"=== Using tmpdir {temp_path}")
97+
98+
# Copy source files (Equivalent to cp -LR)
99+
shutil.copytree(
100+
source_root / "tensorflow_text",
101+
temp_path / "tensorflow_text",
102+
dirs_exist_ok=True,
103+
)
104+
105+
# Setup.py selection
106+
setup_file = "setup.nightly.py" if is_nightly() else "setup.py"
107+
shutil.copy2(
108+
source_root / "oss_scripts/pip_package" / setup_file,
109+
temp_path / setup_file,
110+
)
111+
112+
# Manifest and License
113+
shutil.copy2(
114+
source_root / "oss_scripts/pip_package/MANIFEST.in",
115+
temp_path / "MANIFEST.in",
116+
)
117+
shutil.copy2(
118+
source_root / "oss_scripts/pip_package/LICENSE", temp_path / "LICENSE"
119+
)
120+
121+
# Prepare Python execution
122+
python_bin = sys.executable # Uses the currently running python
123+
124+
# Build pip package
125+
build_cmd = [
126+
python_bin,
127+
setup_file,
128+
"bdist_wheel",
129+
"--universal",
130+
] + get_platform_name()
131+
132+
print(f"=== Running: {' '.join(build_cmd)}")
133+
subprocess.run(build_cmd, cwd=temp_path, check=True)
134+
135+
# Copy wheels to output directory
136+
dist_path = temp_path / "dist"
137+
for wheel in dist_path.glob("*.whl"):
138+
shutil.copy2(wheel, output_path)
139+
print(f"=== Built: {output_path / wheel.name}")
140+
141+
142+
if __name__ == "__main__":
143+
main()

0 commit comments

Comments
 (0)