# macOS GPU (TensorFlow-metal)

**URL:** <https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81>\
**Category:** General\
**Created:** [March 12, 2024, 5:18pm UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81 "2024-03-12T17:18:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://discuss.bayesflow.org/u/ali)\
**Post date:** [March 12, 2024, 5:18pm UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81/1 "2024-03-12T17:18:47Z")

</div>

After I installed tensorflow-metal, I saw a huge increase in training time on macOS. As you might already know, I get the following warning:

_“2.11+ optimizer `tf.keras.optimizers.Adam` runs slowly on M1/M2 Macs, please use the legacy Keras optimizer instead, located at `tf.keras.optimizers.legacy.Adam`”._

But can we use the legacy optimizer? I tried to implement it using the following:

```auto
optimizer= tf.keras.optimizers.legacy.Adam
history = trainer.train_online(epochs=10, iterations_per_epoch=1000, optimizer=optimizer, batch_size=32, validation_sims=200)

```

but I got an error. I have attached the model, and I appreciate your feedback in advance. Thanks a lot.

 ![Screenshot 2024-03-12 at 1.10.32 PM](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/7d6a3edc048a01fea384c4d33219d455fb8403f6.jpeg)

> **[bf\_macOs\_GPU](https://www.dropbox.com/scl/fo/a65quaywh9z4t3tgfwmpd/h?rlkey=dnl2b89p106k0aiwlri0e5bdl&dl=0)**
>
> Shared with Dropbox

---

<div class="post-metadata">

**Author:** ![valentin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/valentin/32/103_2.png) [@valentin](https://discuss.bayesflow.org/u/valentin)\
**Post date:** [March 13, 2024, 11:09am UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81/2 "2024-03-13T11:09:18Z")

</div>

Hi,  
as far as I can tell the problem is that in the first line you reference the `tf.keras.optimizers.legacy.Adam` class, but you don’t instantiate it. If you call the constructor by appending `()`, it should work. Full code:

```auto
optimizer= tf.keras.optimizers.legacy.Adam()
history = trainer.train_online(epochs=10, iterations_per_epoch=1000, optimizer=optimizer, batch_size=32, validation_sims=200)

```

---

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://discuss.bayesflow.org/u/ali)\
**Post date:** [March 13, 2024, 8:42pm UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81/3 "2024-03-13T20:42:13Z")

</div>

Thanks. That was indeed the issue. But apparently `tf.keras.optimizers.legacy.Adam` does not do a good job using the metal plugin, and it has been reported elsewhere. The training actually diverges using GPU. This is the results from the same code using the legacy function:

 ![Screenshot 2024-03-13 at 1.29.27 PM](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/71c15f259c5a9f59edb80b93b8be77be099b7759.jpeg)

So, it is a known issue, and it is not worth trying at this time, in case other users are interested.

By the way, I created a new environment to clean install the packages and I found out that some of the main packages are upldated, creating dependency conflicts with BayesFlow. I actually used `pip install bayesflow` but when I ran the same code I got the following warning, which later on turned out to an error:

 ![Screenshot 2024-03-13 at 4.10.47 PM](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/764fb2ed21e345a4832559891fbf9d8601e8607a.png)

It seems that installing BayesFlow would install TensorFlow 2.15 but TensorFlow-probability 0.24.0, which is compatible with TensorFlow 2.16. But updating TensorFlow would need the updated tf\_keras

 ![Screenshot 2024-03-13 at 4.14.36 PM](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/afb85409bb90d402f977486dc470a3123ad4dfa8.png)

After I installed all packages and successfully imported BayesFlow, the code ran but for the summary network I got an error that I could not resolve:

 ![Screenshot 2024-03-13 at 4.15.23 PM](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/0c3bc0471f90048d73a0541cc3aef1b46618bee8.png)

This is resolved by just switching back to tensorflow-probability 0.23.0. Just wanted to document my experiment that might be helpful for future releases.

---

<div class="post-metadata">

**Author:** ![valentin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/valentin/32/103_2.png) [@valentin](https://discuss.bayesflow.org/u/valentin)\
**Post date:** [March 14, 2024, 12:48pm UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81/4 "2024-03-14T12:48:22Z")

</div>

Thanks for reporting the dependency conflicts! They are already fixed in the Development version and we will release an updated version to PyPI soon.

---

<div class="post-metadata">

**Author:** ![ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ali](https://discuss.bayesflow.org/u/ali)\
**Post date:** [March 14, 2024, 1:05pm UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81/5 "2024-03-14T13:05:03Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![marvinschmitt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/marvinschmitt/32/98_2.png) [@marvinschmitt](https://discuss.bayesflow.org/u/marvinschmitt)\
**Post date:** [March 18, 2024, 7:19am UTC](https://discuss.bayesflow.org/t/macos-gpu-tensorflow-metal/81/6 "2024-03-18T07:19:07Z")

</div>

The new release fixes the dependency conflicts. Re-installing via `pip` should solve the issue.

> **[Release v1.1.5 · stefanradev93/BayesFlow](https://github.com/stefanradev93/BayesFlow/releases/tag/v1.1.5)**
>
> What's Changed
> 
> set label names in plot\_recovery() by @levolz in #102
> Development by @stefanradev93 in #103
> add label\_fontsize and value\_fontsize by @LuSchumacher in #106
> Optimization: add setting ...
