Unable to run Colab notebook with batch size < 8
I haven't been able to run the provided Colab notebook with a batch size less than 8 (num TPUs), in my case 6.
If I just change TRAIN_BATCH_SIZE to 6, I get the following error: "ValueError: train batch size 6 must be divisible by number of replicas 8"
If I change TRAIN_BATCH_SIZE to 6 and also change NUM_TPU_CORES (used for setting num_shards in tpu_config) to 6, I get the following error:
"ValueError: TPUConfig.num_shards is not set correctly. According to TPU system metadata for Tensorflow master (b'grpc://10.76.119.10:8470'): num_replicas should be (8), got (6). For non-model-parallelism, num_replicas should be the total num of TPU cores in the system. For model-parallelism, the total number of TPU cores should be num_cores_per_replica * num_replicas. Please set it accordingly or leave it as None"
If I change TRAIN_BATCH_SIZE to 6 and set NUM_TPU_CORES to None, I get the following error: "train batch size 6 must be divisible by number of replicas 8"
The reason I want to run with a lower batch size is that I'm getting OOM errors when running with batch size of 8 and seq_len of 512 for my dataset.
Thank you for creating such a great resource!
The part where the batch size needs to be a multiple of the 8 is a fundamental TPU limitation since a Cloud TPU is 8 separate processors.
But the part where a batch size of 8 is OOM is unexpected. It can fit a batch size of 24 - 48 (depending on which version of TensorFlow you use) with a sequence length of 384, so 8 should fit with 512. And it was trained with 512 so it fit there.
Are you adding more layers like an LSTM on top of BERT or anything like that? Can you show what the OOM error message was with batch size 8 (how much memory it said it needed)?
Thanks for getting back to me so quickly. Ultimately I get a "runtime died" message. The warning I get prior to the process crashing is: "Memory usage is close to the limit Your runtime is close to its memory limit. Further use may cause an out-of-memory crash and the loss of in-memory state for all sessions. Would you like to terminate some sessions in order to free up memory (state will be lost for those sessions)? Currently, 9.52 GB / 12.72 GB is being used."
I am not adding any layers at this point.
As mentioned, I am using a different dataset than the ones given -- not sure if that is related to problem I am experiencing. I am getting this error after a few minutes of training. The output I get prior to the error can be found here: https://docs.google.com/document/d/1CNAUHLB371x2JNgzixtGdnXiMVIpOQNkZhx3iGKA6fc/edit?usp=sharing
Ok, I think this is a CPU memory issue. The way that I was doing data loading in run_classifier.py originally (and the way the Colab does it) is to load everything into memory and then create a constant tensor with Dataset.from_tensor_slices. However this does not work well on large datasets. The better way is the way it's done now in run_classifier.py, where the data is written to an intermediate file in output_dir and then loaded back in with TFRecordIO.
You should be able to port these changes to the Colab very easily, just change these two function (slightly different APIs):
convert_examples_to_features -> file_based_convert_examples_to_features
That fixed it, many thanks!