Python Running Error on Macbook pro m1 max (Running on Tensorflow)

Viewed 2261

I am trying to run this code from github binary-bot on my new macbook pro max M1 chip:

Metal device set to:

Apple M1 Max
systemMemory: 32.00 GB
maxCacheSize: 10.67 GB

And I am getting the following error. Any suggestions?

2021-12-19 17:26:25.248041: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2021-12-19 17:26:25.248181: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
1 Physical GPUs, 1 Logical GPUs
1 Physical GPUs, 1 Logical GPUs
Trying to connect to IqOption
Successfully Connected!
/Users/abdallahmohamed/Downloads/binary-bot-master/training.py:35: FutureWarning: In a future version of pandas all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only
  df = df.drop("future", 1)
/Users/abdallahmohamed/Downloads/binary-bot-master/training.py:35: FutureWarning: In a future version of pandas all arguments of DataFrame.drop except for the argument 'labels' will be keyword-only
  df = df.drop("future", 1)
train data: 836 validation: 68
sells: 418, buys: 418
VALIDATION sells: 34, buys : 34
0.001-5-SEQ-2-40-16-PRED-1639927591
1 Physical GPUs, 1 Logical GPUs
2021-12-19 17:26:32.262259: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
Epoch 1/40
2021-12-19 17:26:33.223096: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:33.592036: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:33.646351: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:33.684523: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:33.786763: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:33.866171: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:33.932667: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
53/53 [==============================] - ETA: 0s - loss: 0.8687 - accuracy: 0.52272021-12-19 17:26:35.527128: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:35.652789: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:35.681668: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:26:35.710022: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 4s 34ms/step - loss: 0.8687 - accuracy: 0.5227 - val_loss: 0.6928 - val_accuracy: 0.5000
Epoch 2/40
53/53 [==============================] - ETA: 0s - loss: 0.7844 - accuracy: 0.5335WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7844 - accuracy: 0.5335 - val_loss: 0.6890 - val_accuracy: 0.5000
Epoch 3/40
51/53 [===========================>..] - ETA: 0s - loss: 0.7257 - accuracy: 0.5588WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7293 - accuracy: 0.5562 - val_loss: 0.6836 - val_accuracy: 0.5735
Epoch 4/40
52/53 [============================>.] - ETA: 0s - loss: 0.7421 - accuracy: 0.5649WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7411 - accuracy: 0.5658 - val_loss: 0.7035 - val_accuracy: 0.4412
Epoch 5/40
52/53 [============================>.] - ETA: 0s - loss: 0.7205 - accuracy: 0.5565WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7193 - accuracy: 0.5586 - val_loss: 0.7327 - val_accuracy: 0.4412
Epoch 6/40
52/53 [============================>.] - ETA: 0s - loss: 0.7233 - accuracy: 0.5637WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7228 - accuracy: 0.5634 - val_loss: 0.7023 - val_accuracy: 0.5441
Epoch 7/40
51/53 [===========================>..] - ETA: 0s - loss: 0.7192 - accuracy: 0.5588WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7187 - accuracy: 0.5586 - val_loss: 0.8523 - val_accuracy: 0.4559
Epoch 8/40
51/53 [===========================>..] - ETA: 0s - loss: 0.7111 - accuracy: 0.5613WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.7105 - accuracy: 0.5634 - val_loss: 0.7727 - val_accuracy: 0.4559
Epoch 9/40
53/53 [==============================] - ETA: 0s - loss: 0.7151 - accuracy: 0.5514WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 25ms/step - loss: 0.7151 - accuracy: 0.5514 - val_loss: 0.7105 - val_accuracy: 0.5147
Epoch 10/40
53/53 [==============================] - ETA: 0s - loss: 0.7046 - accuracy: 0.5371WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 25ms/step - loss: 0.7046 - accuracy: 0.5371 - val_loss: 0.6940 - val_accuracy: 0.5588
Epoch 11/40
53/53 [==============================] - ETA: 0s - loss: 0.7064 - accuracy: 0.5455WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 25ms/step - loss: 0.7064 - accuracy: 0.5455 - val_loss: 0.7433 - val_accuracy: 0.3971
Epoch 12/40
51/53 [===========================>..] - ETA: 0s - loss: 0.6991 - accuracy: 0.5784WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 25ms/step - loss: 0.6988 - accuracy: 0.5778 - val_loss: 0.6902 - val_accuracy: 0.5147
Epoch 13/40
52/53 [============================>.] - ETA: 0s - loss: 0.6812 - accuracy: 0.5757WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.6818 - accuracy: 0.5754 - val_loss: 0.8100 - val_accuracy: 0.4118
Epoch 14/40
52/53 [============================>.] - ETA: 0s - loss: 0.6876 - accuracy: 0.5673WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.6888 - accuracy: 0.5658 - val_loss: 0.7208 - val_accuracy: 0.5294
Epoch 15/40
52/53 [============================>.] - ETA: 0s - loss: 0.6815 - accuracy: 0.5505WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 23ms/step - loss: 0.6809 - accuracy: 0.5502 - val_loss: 0.6965 - val_accuracy: 0.5441
Epoch 16/40
51/53 [===========================>..] - ETA: 0s - loss: 0.6886 - accuracy: 0.5711WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.6900 - accuracy: 0.5670 - val_loss: 0.6529 - val_accuracy: 0.6029
Epoch 17/40
53/53 [==============================] - ETA: 0s - loss: 0.6959 - accuracy: 0.5598WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.6959 - accuracy: 0.5598 - val_loss: 0.7832 - val_accuracy: 0.4118
Epoch 18/40
52/53 [============================>.] - ETA: 0s - loss: 0.7002 - accuracy: 0.5325WARNING:tensorflow:Can save best model only with val_acc available, skipping.
53/53 [==============================] - 1s 24ms/step - loss: 0.6999 - accuracy: 0.5335 - val_loss: 0.7270 - val_accuracy: 0.3676
WARNING:tensorflow:Layer lstm_3 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
WARNING:tensorflow:Layer lstm_4 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
WARNING:tensorflow:Layer lstm_5 will not use cuDNN kernels since it doesn't meet the criteria. It will use a generic GPU kernel as fallback when running on GPU.
Trying to connect to IqOption
Successfully Connected!
2021-12-19 17:27:01.637577: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.
2021-12-19 17:27:01.792362: F tensorflow/core/framework/tensor.cc:681] Check failed: IsAligned() ptr = 0x2d0efcee0
zsh: abort      /Users/abdallahmohamed/miniforge3/envs/env_tensorflow/bin/python 
2 Answers

It worked after I deleted the stored model and saved a new one

My sense is there are two distinct problems here:

  1. The steps to install Tensorflow on an M1 machine using the Metal engine.
  2. The requirements.txt file of the package to be used.

Background:

When working on a new M1 processor you tend to hit problems on installing pre-M1 python packages due to certain naming conventions and versioning assumptions that tend to get included in the package requirement.txt file.

Separately, it should be noted that only Python 3.8 and above are supported on M1 processors. Alas, Python 3.7 and all previous versions of python were developed before the release of M1 Processors and there are (apparently) no plans to backport key patches.

Tensorflow-macos and Tensorflow-metal Install

The steps to install Tensorflow-macos and TensorFlow-metal are detailed here, they can be summarized as follows using mini-forge:

conda create -n tf python=3.8 -y
conda activate tf
conda install -c apple tensorflow-deps -y
# Navigate the issue with conda environments
# built against pre-macOS 11 SDK use (Just in case)
# this solves the Intel / M1 install issues:
SYSTEM_VERSION_COMPAT=0 pip install tensorflow-macos 
SYSTEM_VERSION_COMPAT=0 pip install tensorflow-metal

so we can test this worked:

#!/usr/bin/env bash

PYCMD=$(cat <<EOF
import tensorflow as tf

tf.config.list_physical_devices()
with tf.device('/GPU'):
  a = tf.random.normal(shape=(2,), dtype=tf.float32)
  b = tf.nn.relu(a)

print(a)
print(b)

EOF
)

python3 -c "$PYCMD" 2>/dev/null

the expected output for my test machine:

>./val.sh
Metal device set to: Apple M1

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

tf.Tensor([-0.4044231   0.08157467], shape=(2,), dtype=float32)
tf.Tensor([0.         0.08157467], shape=(2,), dtype=float32)

ok, everything is (assumed) to be working...

Binary Bot Install Issue Explanation:

Now we turn to look at Binary Bot...

First, we need to install via git hub

git clone https://github.com/ItamarRocha/binary-bot

ok, now prior to installing let's look at the requirements.txt file...

iqoptionapi @ git+git://github.com/Lu-Yi-Hsun/iqoptionapi.git@e96ba2c5b905a139a4765167b08c5df48cf57773
numpy==1.18.0
pandas==1.1.1
tensorflow==2.3.1
scikit-learn==0.23.2
grpcio==1.24.3

it is important to note two key issues == and tensorflow. The == in the requirements file implies an exact version match on the package version. Meanwhile we have not installed tensorflow, we have installed tensorflow-macos and tensorflow-metal. We can check this:

>pip show numpy pandas tensorflow scikit-learn grpcio | egrep 'Name:|Version:'
WARNING: Package(s) not found: pandas, scikit-learn, tensorflow
Name: numpy
Version: 1.19.5
Name: grpcio
Version: 1.43.0

thus if we run:

python3 pip install -r requirements.txt

we receive a host of errors. Why? Well, because the requirements.txt file requires an exact == package match rather than a compatible ~= package match or downloading any version >= greater than or equal to the stipulated version.

To navigate this we can change to >=, which (hopefully) should allow us to install the packages compatible with the Apple M1 silicon, though we might break compatibility if the developer relies on older assumptions in the packages.

Further, the requirements.txt will try to install the tensorflow package, not the tensorflow-macosx or tensorflow-metal packages which as we can see creates a host of issues when we run the requirements.txt file unchanged.

So how to navigate?

Well, one option is to simply replace the == with >= and replace or remove tensorflow with tensorflow-macosx.

sed -i.bak 's/==/>=/g;s/tensorflow/tensorflow-macos/g' requirements.txt

resulting in:

cat requirements.txt
iqoptionapi @ git+git://github.com/Lu-Yi-Hsun/iqoptionapi.git@e96ba2c5b905a139a4765167b08c5df48cf57773
numpy>=1.18.0
pandas>=1.1.1
tensorflow-macos>=2.3.1
scikit-learn>=0.23.2
grpcio>=1.24.3

now if we run:

pip install -r requirements.txt

we install the latest version of a package >= resulting in:

pip show numpy pandas tensorflow-macos tensorflow-metal scikit-learn grpcio | egrep 'Name:|Version:'
Name: numpy
Version: 1.22.0
Name: pandas
Version: 1.3.5
Name: tensorflow-macos
Version: 2.7.0
Name: tensorflow-metal
Version: 0.3.0
Name: scikit-learn
Version: 1.0.2
Name: grpcio
Version: 1.43.0

so, we should now be able to test if things are working...

python3 testing.py
Metal device set to: Apple M1

systemMemory: 16.00 GB
maxCacheSize: 5.33 GB

2022-01-08 20:40:40.591430: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-01-08 20:40:40.591754: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
1 Physical GPUs, 1 Logical GPUs
1 Physical GPUs, 1 Logical GPUs
Trying to connect to IqOption
Error when trying to connect
<iqoptionapi.stable_api.IQ_Option object at 0x1031074c0>
Retrying

so the package now installs, all that is required is to add your name and password to the iq.py file, and check that the results align with expectations.

  19   if iq == None:
  20       print("Trying to connect to IqOption")
  21       iq=IQ_Option('USERNAME','PASSWORD') # YOU HAVE TO ADD YOUR USERNAME AND PASSWORD
  22       iq.connect()

In reference to the point on 'deleting the model', which is the accepted answer. I presume this refers to the LSTM-best.model in the models directory in the binary-bot directory.

tree binary-bot                                                  binary-bot
├── README.md
├── __pycache__
│   ├── iq.cpython-38.pyc
│   └── training.cpython-38.pyc
├── imgs
│   ├── mac_forex.jpg
│   └── result_binary.png
├── iq.py
├── models
│   └── LSTM-best.model
├── requirements.txt
├── requirements.txt.bak
├── result_binary.png
├── testing.py
└── training.py

Alas, I am not currently in a country that allows me to signup for the service https://iqoption.net/, to fully test but my sense is that things should work to a point where one can validate the results based upon the above.

Hopefully, this points people in the right direction in thinking about the hurdles involved in getting python packages to work with Apple-M1 / Apple-Intel laptops using the TensorFlow metal plugin.

Stay safe and well.

Related