Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions keras/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from keras.src.ops.function import Function as Function
from keras.src.ops.operation import Operation as Operation
from keras.src.optimizers.optimizer import Optimizer as Optimizer
from keras.src.print import print as print
from keras.src.quantizers.quantizers import Quantizer as Quantizer
from keras.src.regularizers.regularizers import Regularizer as Regularizer
from keras.src.version import __version__ as __version__
Expand Down
1 change: 1 addition & 0 deletions keras/api/_tf_keras/keras/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from keras.src.ops.function import Function as Function
from keras.src.ops.operation import Operation as Operation
from keras.src.optimizers.optimizer import Optimizer as Optimizer
from keras.src.print import print as print
from keras.src.quantizers.quantizers import Quantizer as Quantizer
from keras.src.regularizers.regularizers import Regularizer as Regularizer
from keras.src.version import __version__ as __version__
Expand Down
1 change: 1 addition & 0 deletions keras/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
from keras.src.models import Functional
from keras.src.models import Model
from keras.src.models import Sequential
from keras.src.print import print
from keras.src.version import __version__
25 changes: 25 additions & 0 deletions keras/src/print.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from keras.src.api_export import keras_export
from keras.src.backend import backend as keras_backend

_print = print


@keras_export("keras.print")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, please add individual print ops to e.g. keras/src/backend/tensorflow/core.py, keras/src/backend/jax/core.py, etc. Then export the op is keras/src/ops/core.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'm not sure if you want from jax.debug import print and from tensorflow import print or if you want them with first-class docstrings. I've added the latter; and updated this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the request was to add a print implementation also for the torch, numpy and openvino backends (even if they're the same).

Then remove the fallback mechanism and _print.

def print(*args, **kwargs):
backend = keras_backend()
if backend == "jax":
import jax # noqa: E402

print_fn = jax.debug.print
elif backend == "tensorflow":
import tensorflow as tf # noqa: E402

print_fn = tf.print
else:
print_fn = _print
# TODO:
# "torch"
# pytorch.org/docs/stable/generated/torch.set_printoptions.html ?
# "openvino"
# "numpy"
return print_fn(*args, **kwargs)