Skip to content

Commit 541d9fb

Browse files
Clarify the tf.function tracing rules for custom classes
PiperOrigin-RevId: 551287285
1 parent f7b9ed1 commit 541d9fb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

site/en/guide/function.ipynb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,11 @@
371371
"* For Python ordered containers such as `list` and `tuple`, etc., the type is parameterized by the types of their elements; for example, the type of `[1, 2]` is `ListTraceType<LiteralTraceType<1>, LiteralTraceType<2>>` and the type for `[2, 1]` is `ListTraceType<LiteralTraceType<2>, LiteralTraceType<1>>` which is different.\n",
372372
"* For Python mappings such as `dict`, the type is also a mapping from the same keys but to the types of values instead the actual values. For example, the type of `{1: 2, 3: 4}`, is `MappingTraceType<<KeyValue<1, LiteralTraceType<2>>>, <KeyValue<3, LiteralTraceType<4>>>>`. However, unlike ordered containers, `{1: 2, 3: 4}` and `{3: 4, 1: 2}` have equivalent types.\n",
373373
"* For Python objects which implement the `__tf_tracing_type__` method, the type is whatever that method returns\n",
374-
"* For any other Python objects, the type is a generic `TraceType` which uses the object's Python equality and hashing for matching. (Note: It relies on [weakref](https://docs.python.org/3/library/weakref.html) to the object and hence only works as long as the object is in scope/not deleted.)\n"
374+
"* For any other Python objects, the type is a generic `TraceType`, its matching precedure is:\n",
375+
" * First it checks if the object is the same object used in the previous trace (using python `id()` or `is`). Note that this will still match if the object has changed, so if you use python objects as `tf.function` arguments it's best to use *immutable* ones.\n",
376+
" * Next it checks if the object is equal to the object used in the previous trace (using python `==`).\n",
377+
" \n",
378+
" Note that this procedure only keeps a [weakref](https://docs.python.org/3/library/weakref.html) to the object and hence only works as long as the object is in scope/not deleted.)\n"
375379
]
376380
},
377381
{

0 commit comments

Comments
 (0)