Skip to content

Commit 180a173

Browse files
committed
Let TF-GNN support TF2.16+ via TF_USE_LEGACY_KERAS=1.
PiperOrigin-RevId: 619267380
1 parent f7592a1 commit 180a173

File tree

11 files changed

+135
-10
lines changed

11 files changed

+135
-10
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,21 @@ page, which also links to the
5252
The latest stable release of TensorFlow GNN is available from
5353

5454
```
55-
pip install tensorflow_gnn
55+
pip install tensorflow-gnn
5656
```
5757

5858
For installation from source, see our [Developer
5959
Guide](tensorflow_gnn/docs/guide/developer.md).
6060

6161
Key platform requirements:
6262

63-
* TensorFlow 2.12, 2.13, 2.14 or 2.15, and any GPU drivers it needs
63+
* TensorFlow 2.12 or higher, and any GPU drivers it needs
6464
[[instructions](https://www.tensorflow.org/install)].
6565
* Keras v2, as traditionally included with TensorFlow 2.x.
66-
(TF-GNN does not work with the new multi-backend Keras v3.)
66+
TF-GNN does not work with the new multi-backend Keras v3.<br/>
67+
**Users of TF2.16+ must also `pip install tf-keras` and set
68+
TF_USE_LEGACY_KERAS=1**,
69+
see our [Keras version](tensorflow_gnn/docs/guide/keras_version.md) guide for details.
6770
* Apache Beam for distributed graph sampling.
6871

6972
TF-GNN is developed and tested on Linux. Running on other platforms supported

kokoro/github/ubuntu/cpu/build_versioned.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ tag_filters="-no_oss,-oss_excluded${TAG_FILTERS}"
4747
bazel clean
4848
pip install -r requirements-dev.txt --progress-bar off
4949
pip install tensorflow=="${TF_VERSION}" --progress-bar off --upgrade
50+
if [[ "$TF_USE_LEGACY_KERAS" == 1 ]]; then
51+
pip install tf-keras=="${TF_VERSION}" --progress-bar off --upgrade
52+
fi
5053
python3 setup.py bdist_wheel
5154
pip uninstall -y tensorflow_gnn
5255
pip install dist/tensorflow_gnn-*.whl
5356

54-
bazel test --build_tag_filters="${tag_filters}" --test_tag_filters="${tag_filters}" --test_output=errors --verbose_failures=true --build_tests_only --define=no_tfgnn_py_deps=true --keep_going --experimental_repo_remote_exec //bazel_pip/tensorflow_gnn/...
57+
bazel test --test_env=TF_USE_LEGACY_KERAS --build_tag_filters="${tag_filters}" --test_tag_filters="${tag_filters}" --test_output=errors --verbose_failures=true --build_tests_only --define=no_tfgnn_py_deps=true --keep_going --experimental_repo_remote_exec //bazel_pip/tensorflow_gnn/...

kokoro/github/ubuntu/cpu/newest_stable/continuous.cfg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ env_vars: {
66
}
77
env_vars: {
88
key: "TF_VERSION"
9-
value: "2.15.*"
9+
value: "2.16.*"
10+
}
11+
env_vars: {
12+
key: "TF_USE_LEGACY_KERAS"
13+
value: "1"
1014
}
1115

1216
action {

kokoro/github/ubuntu/cpu/newest_stable/presubmit.cfg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ env_vars: {
66
}
77
env_vars: {
88
key: "TF_VERSION"
9-
value: "2.15.*"
9+
value: "2.16.*"
10+
}
11+
env_vars: {
12+
key: "TF_USE_LEGACY_KERAS"
13+
value: "1"
1014
}
1115

1216
action {

kokoro/github/ubuntu/cpu/oldest/continuous.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ env_vars: {
88
key: "TF_VERSION"
99
value: "2.12.*"
1010
}
11+
env_vars: {
12+
key: "TF_USE_LEGACY_KERAS"
13+
value: "0"
14+
}
1115
env_vars: {
1216
key: "TAG_FILTERS"
1317
value: ",-tf_at_least_2_13"

kokoro/github/ubuntu/cpu/oldest/presubmit.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ env_vars: {
88
key: "TF_VERSION"
99
value: "2.12.*"
1010
}
11+
env_vars: {
12+
key: "TF_USE_LEGACY_KERAS"
13+
value: "0"
14+
}
1115
env_vars: {
1216
key: "TAG_FILTERS"
1317
value: ",-tf_at_least_2_13"

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ def get_version():
173173
'networkx',
174174
'pyarrow',
175175
# pylint:disable=g-line-too-long
176-
'tensorflow>=2.12.0,<2.16.0; platform_machine != "arm64" or platform_system != "Darwin"',
177-
'tensorflow-macos>=2.12.0,<2.16.0; platform_machine == "arm64" and platform_system == "Darwin"',
176+
'tensorflow>=2.12.0,<3; platform_machine != "arm64" or platform_system != "Darwin"',
177+
'tensorflow-macos>=2.12.0,<3; platform_machine == "arm64" and platform_system == "Darwin"',
178178
# pylint:enable=g-line-too-long
179179
'apache-beam',
180180
],

tensorflow_gnn/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ def _check_keras_version():
3636
if keras_version.startswith("3."):
3737
raise ImportError(
3838
"Package tensorflow_gnn requires tf.keras to be Keras version 2 "
39-
f"but got version {keras_version}. Install tensorflow<=2.15 to fix.")
39+
f"but got version {keras_version}. "
40+
"For open-source TensorFlow 2.16 and above, "
41+
"set the environment variable TF_USE_LEGACY_KERAS=1 to fix. "
42+
"For more information, see "
43+
"https://github.com/tensorflow/gnn/blob/main/tensorflow_gnn/docs/guide/keras_version.md"
44+
)
4045
_check_keras_version()
4146
del _check_keras_version
4247
# pylint: enable=g-statement-before-imports

tensorflow_gnn/docs/guide/developer.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ pip install -r requirements-dev.txt
4949
pip install tensorflow
5050
```
5151

52+
For TF2.16+, you will additionally need to follow the instructions from the
53+
[Keras version](./keras_version.md) guide.
54+
5255
## Building the package and running tests
5356

5457
We use [Bazel](https://bazel.build/) to build and run tests. Since we use
@@ -84,7 +87,7 @@ After setting up the test directory, you can run all tests locally by running
8487
the following command in the repo root directory.
8588

8689
```
87-
bazel test --build_tag_filters=-no_oss,-oss_excluded --test_tag_filters=-no_oss,-oss_excluded --test_output=errors --verbose_failures=true --build_tests_only --define=no_tfgnn_py_deps=true --keep_going --experimental_repo_remote_exec //bazel_pip/tensorflow_gnn/...
90+
bazel test --build_tag_filters=-no_oss,-oss_excluded --test_tag_filters=-no_oss,-oss_excluded --test_output=errors --verbose_failures=true --build_tests_only --define=no_tfgnn_py_deps=true --keep_going --experimental_repo_remote_exec --test_env="TF_USE_LEGACY_KERAS=1" //bazel_pip/tensorflow_gnn/...
8891
```
8992

9093
The `--define=no_tfgnn_py_deps=true` flag directs bazel to assume that all
@@ -94,19 +97,24 @@ The flags `--build_tag_filters=-no_oss,-oss_excluded` and
9497
`--test_tag_filters=-no_oss,-oss_excluded` disable tests that pass in the
9598
internal production environment but fail on GitHub.
9699

100+
The flag `--test_env="TF_USE_LEGACY_KERAS=1"` comes from the
101+
[Keras version](./keras_version.md) guide and is required for TF2.16+.
102+
97103
### Run a single test file
98104

99105
It is also possible to run a single test file by specifying its path (for
100106
example)
101107

102108
`bazel test --define=no_tfgnn_py_deps=true --experimental_repo_remote_exec
109+
--test_env="TF_USE_LEGACY_KERAS=1"
103110
//bazel_pip/tensorflow_gnn/models/gcn:gcn_conv_test`
104111

105112
### Run a single test case
106113

107114
To run a single test case, use the `--test_filter` flag.
108115

109116
`bazel test --define=no_tfgnn_py_deps=true --experimental_repo_remote_exec
117+
--test_env="TF_USE_LEGACY_KERAS=1"
110118
//bazel_pip/tensorflow_gnn/models/gcn:gcn_conv_test
111119
--test_filter=*test_gcnconv_activation*`
112120

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Keras Version Configuration for TF-GNN
2+
3+
The TensorFlow GNN library requires `tf.keras` to be Keras v2, because Keras v3
4+
does not support composite tensor types like `tfgnn.GraphTensor`. Up to TF 2.15,
5+
Keras v2 was the default for `tf.keras`. For TF 2.16+, you need to make special
6+
arrangements, as described in this guide.
7+
8+
<!-- PLACEHOLDER FOR KERAS VERSION GOOGLE EXTRAS -->
9+
10+
## Installation and program execution
11+
12+
### For TensorFlow 2.16 and up
13+
14+
TensorFlow as of release 2.16 depends on `keras>=3` but no longer on a package
15+
for Keras v2. Install TF-GNN together with the package `tf-keras` that continues
16+
to supply an implementation of Keras v2.
17+
18+
```
19+
pip install tensorflow-gnn tf-keras
20+
```
21+
22+
Running a TF-GNN program under TF 2.16+ requires to set the
23+
[environment variable](https://en.wikipedia.org/wiki/Environment_variable)
24+
`TF_USE_LEGACY_KERAS` to `1` one way or another, for example:
25+
26+
* with the Unix shell command
27+
28+
```
29+
export TF_USE_LEGACY_KERAS=1
30+
```
31+
32+
or your system's equivalent;
33+
34+
* at the top of the main Python program or Colab notebook by
35+
36+
```python
37+
import os
38+
os.environ["TF_USE_LEGACY_KERAS"] = "1"
39+
```
40+
41+
**before** any part of the program gets to `import tensorflow`.
42+
43+
44+
### For TensorFlow 2.15
45+
46+
Nothing special is required: TF 2.15 depends on `keras==2.15.*` and, by default,
47+
defines `tf.keras` with it.
48+
49+
In case `TF_USE_LEGACY_KERAS` is set to `1` and `tf-keras` is installed, then
50+
`tf.keras` is defined in terms of that package. While it offers the same API,
51+
it is a separate package with separate static registries and class hierarchies,
52+
so it does not mix well with user code that uses `import keras` and objects
53+
from `keras.*`. Hence we recommend to not use `keras.*` in user code.
54+
55+
### For TensorFlow 2.12, 2.13 and 2.14
56+
57+
Nothing special is required: These TF 2.x versions depend on the matching
58+
version of Keras 2.x and define `tf.keras` with it. The environment
59+
variable `TF_USE_LEGACY_KERAS` is ignored.
60+
61+
62+
## Writing compatible code
63+
64+
### For all supported TensorFlow versions
65+
66+
For use of TF-GNN under any supported version of TensorFlow, we recommend that
67+
user code does `import tensorflow as tf` and uses the Keras API at `tf.keras.*`.
68+
The installation instructions above make sure this is Keras v2, as required
69+
by the TF-GNN library itself.
70+
71+
Do not import and use `keras.*` directly: it will break for TF 2.16 and above.
72+
73+
### For TF 2.16+ only
74+
75+
As of TF 2.16+, `import tensorflow_gnn as tfgnn` checks that `tf.keras` is
76+
Keras v2, which implies that it comes from the `tf_keras` package.
77+
78+
Users who wish to emphasize the use of Keras v2 at the expense of breaking
79+
compatibility with older versions of TensorFlow can use `tf_keras` as a
80+
syntactic alternative to `tf.keras`.
81+
82+
83+
## Other libraries from the TensorFlow ecosystem
84+
85+
Keras does not support mixing different versions or packages of it.
86+
Using TF-GNN in combination with other TensorFlow add-on libraries requires
87+
all of them to work with the same Keras package, and it must be Keras v2.

0 commit comments

Comments
 (0)