Skip to content

Commit 3fac66f

Browse files
authored
Do not use backend ops in ProgBar. (#21709)
The `ProgBar` was using `backend.numpy.mean` causing it to use the accelerator (TPU or GPU) therefore causing a synchronization which defeated the async callback mechanism. This in turn was slowing down training. All values provided in the `logs` are already Python floats and are all single values. There is therefore no need to do a `mean`, computing the average is simply a division of the running sum by the running count.
1 parent f279e93 commit 3fac66f

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

keras/src/utils/progbar.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import sys
44
import time
55

6-
from keras.src import backend
76
from keras.src.api_export import keras_export
87
from keras.src.utils import io_utils
98

@@ -162,12 +161,7 @@ def update(self, current, values=None, finalize=None):
162161
for k in self._values_order:
163162
info += f" - {k}:"
164163
if isinstance(self._values[k], list):
165-
avg = backend.convert_to_numpy(
166-
backend.numpy.mean(
167-
self._values[k][0] / max(1, self._values[k][1])
168-
)
169-
)
170-
avg = float(avg)
164+
avg = self._values[k][0] / max(1, self._values[k][1])
171165
if abs(avg) > 1e-3:
172166
info += f" {avg:.4f}"
173167
else:
@@ -194,11 +188,7 @@ def update(self, current, values=None, finalize=None):
194188
info += f" -{self._format_time(time_per_unit, self.unit_name)}"
195189
for k in self._values_order:
196190
info += f" - {k}:"
197-
avg = backend.convert_to_numpy(
198-
backend.numpy.mean(
199-
self._values[k][0] / max(1, self._values[k][1])
200-
)
201-
)
191+
avg = self._values[k][0] / max(1, self._values[k][1])
202192
if avg > 1e-3:
203193
info += f" {avg:.4f}"
204194
else:

0 commit comments

Comments
 (0)