There a couple of ways to check for GPU in Tensorflow 2.x. Essentially, if GPU is available, then the model will be run on it (unless it's busy by e.g. another instance of TF that locked it). The placement will be seen also in the log files and can be confirmed with e.g. nvidia-smi.
In the code below, I will assume tensorflow is imported as tf (per convention and your code).
To check what devices are available, run:
tf.config.experimental.list_physical_devices()
Here's my output:
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'),
PhysicalDevice(name='/physical_device:XLA_CPU:0',
device_type='XLA_CPU'), PhysicalDevice(name='/physical_device:GPU:0',
device_type='GPU'), PhysicalDevice(name='/physical_device:XLA_GPU:0',
device_type='XLA_GPU')]
In order to check if there is any GPU on the system:
is_gpu = len(tf.config.experimental.list_physical_devices('GPU')) > 0
From Tensorflow 2.1, this functionality has been migrated from experimental and you can use: tf.config.list_physical_devices() in the same manner, i.e.
is_gpu = len(tf.config.list_physical_devices('GPU')) > 0
At some point in time the experimental part will be deprecated.
Last but not least, if your tensorflow was built without CUDA (it's a non-GPU version), list_physical_devices('GPU') will also return False, even if your system physicaly has a GPU.
"Is it automatic once the gpu is recognized by TF?"
Yes. To quote after TF docs:
Note: Use tf.config.experimental.list_physical_devices('GPU') to
confirm that TensorFlow is using the GPU.
If it is recognised, it will be used during the training. If you'd like to be dead sure, you can ask for more explicit logging:
tf.debugging.set_log_device_placement(True)