Generic requirements.txt for TensorFlow on both Apple M1 and other devices
Asked Answered
F

1

6

I have a new MacBook with the Apple M1 chipset. To install tensorflow, I follow the instructions here, i.e., installing tensorflow-metal and tensorflow-macos instead of the normal tensorflow package.

While this works fine, it means that I can't run the typical pip install -r requirements.txt as long as we have tensorflow in the requirements.txt. If we instead include tensorflow-macos, it'll lead to problems for non-M1 or even non-macOS users.

Our library must work on all platforms. Is there a generic install command that installs the correct TensorFlow version depending on whether the computer is a M1 Mac or not? So that we can use a single requirements.txt for everyone?

Or if that's not possible, can we pass some flag/option, e.g., pip install -r requirements.txt --m1 to install some variation? What's the simplest and most elegant solution here?

Fennessy answered 31/3, 2022 at 6:49 Comment(0)
F
8

According to this post Is there a way to have a conditional requirements.txt file for my Python application based on platform?

You can use conditionals on your requirements.txt, thus

tensorflow==2.8.0; sys_platform != 'darwin' or platform_machine != 'arm64'
tensorflow-macos==2.8.0; sys_platform == 'darwin' and platform_machine == 'arm64'
Fachan answered 14/4, 2022 at 5:32 Comment(3)
Great, I'll try and accept the answer when it works. Thanks!Fennessy
Wait, this checks for the OS, not for the architecture, right? Thus, it fails on Intel MacBooks.Fennessy
You also need to check the architecture with platform_machine. I updated your answer.Fennessy

© 2022 - 2024 — McMap. All rights reserved.