[Tensorflow, Mac] InvalidArgumentError: Cannot assign a device for operation model/dense_1/MatMul/ReadVariableOp: Could not satisfy explicit device specification

펭귄은 M1 칩이 탑재된 Macbook에서 Tensorflow로 모델을 학습하던 중, fit을 실행하자 아래의 에러가 발생했다.

InvalidArgumentError
: Cannot assign a device for operation model/dense_1/MatMul/ReadVariableOp: Could not satisfy explicit device specification '' because the node {{colocation_node model/dense_1/MatMul/ReadVariableOp}} was colocated with a group of nodes that required incompatible device '/job:localhost/replica:0/task:0/device:GPU:0'. All available devices [/job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:GPU:0]. 

Colocation Debug Info: 
Colocation group had the following types and supported devices: Root Member(assigned_device_name_index_=2 requested_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' assigned_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' resource_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' supported_device_types_=[CPU] possible_devices_=[] 

AssignSubVariableOp: CPU 
ReadVariableOp: GPU CPU 
_Arg: GPU CPU 

Colocation members, user-requested devices, and framework assigned devices, if any: model_dense_1_matmul_readvariableop_resource (_Arg) framework assigned device=/job:localhost/replica:0/task:0/device:GPU:0 model/dense_1/MatMul/ReadVariableOp (ReadVariableOp) Adam/AssignSubVariableOp (AssignSubVariableOp) [[{{node model/dense_1/MatMul/ReadVariableOp}}]] [Op:__inference_train_function_3428]

 

펭귄의 추정으로는 위 에러가 발생한 이유는 아래와 같다.

  • GPU에서 수행할 수 없는 연산이 모델에 섞여있다.

 

똑같은 소스코드로 Linux 장비에서는 실행이 잘 되어서 원인을 찾기까지 많은 고생을 했다.

결국 아래의 방법으로 에러를 없앨 수 있었다.

  • model의 일부 레이어가 dtype=tf.float64로 설정되어 있었는데, 해당 설정을 삭제

 

에러는 없앨 수 있었지만, 펭귄의 경우에는 매우 작은 수를 다루는 모델을 학습 중이어서 float64 type 설정이 필요했다.

결국 펭귄은 맥북에서 CPU로 학습을 하거나, 다른 장비에서 학습을 하는 방법 중에 선택을 해야 했다.

일반적으로 맥북 사용에 매우 만족하지만, Apple Chip이 아직 인공지능 라이브러리와 호환성이 떨어지는 부분이 있어서 아쉽다.

 


출처

  • Penguin by Ruby from "https://thenounproject.com/browse/icons/term/penguin/" Noun Project (CC BY 3.0)