我目前正在嘗試使用 tensorflow 2.6 和 CuDNN 運行基于文本的序列到序列模型。
代碼正在運行,但花費了可疑的時間。當我檢查我的任務管理器時,我看到以下內容:

這對我來說看起來很奇怪,因為所有記憶體都在占用,但負載并不重。這是預期的行為嗎?
系統:
- 視窗 10
- 蟒蛇 3.9.9
- TensorFlow 和 Keras 2.6
- CUDA 11.6
- CuDNN 8.3
- 英偉達 RTX 3080ti
在代碼中,我找到了 GPU 的以下設定
def get_gpu_config():
gconfig = tf.compat.v1.ConfigProto()
gconfig.gpu_options.per_process_gpu_memory_fraction = 0.975 # Don't take 100% of the memory
gconfig.allow_soft_placement = True # Does not aggressively take all the GPU memory
gconfig.gpu_options.allow_growth = True # Take more memory when necessary
return gconfig
我的 python 輸出告訴我它找到了我的顯卡:

nvidia-smi它在我的輸出
中也可見:
我可能缺少配置嗎?它所花費的時間與我在 CPU 系統上得到的時間相似,這對我來說似乎很奇怪。
邊注:
我嘗試運行的代碼必須從 tensorflow-gpu 1.12 遷移,但這“相對”順利。
uj5u.com熱心網友回復:
是的,這種行為對于 TensorFlow 來說是正常的!
來自TensorFlow 檔案
默認情況下,TensorFlow 將幾乎所有 GPU 的所有 GPU 記憶體(受 CUDA_VISIBLE_DEVICES 限制)映射到行程可見。這樣做是為了通過減少記憶體碎片更有效地使用設備上相對寶貴的 GPU 記憶體資源。要將 TensorFlow 限制為一組特定的 GPU,請使用tf.config.set_visible_devices方法。
如果您不希望 TensorFlow 分配整個 VRAM,您可以設定使用多少記憶體的硬性限制,或者告訴 TensorFlow 只分配所需的記憶體。
設定硬限制
如下配置虛擬 GPU 設備:
gpus = tf.config.list_physical_devices('GPU')
if gpus:
# Restrict TensorFlow to only allocate 1GB of memory on the first GPU
try:
tf.config.set_logical_device_configuration(
gpus[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=1024)])
logical_gpus = tf.config.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Virtual devices must be set before GPUs have been initialized
print(e)
僅根據需要使用
- 你可以設定環境變數
TF_FORCE_GPU_ALLOW_GROWTH=true
或者
- 使用
tf.config.experimental.set_memory_growth如下:
gpus = tf.config.list_physical_devices('GPU')
if gpus:
try:
# Currently, memory growth needs to be the same across GPUs
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
logical_gpus = tf.config.list_logical_devices('GPU')
print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
except RuntimeError as e:
# Memory growth must be set before GPUs have been initialized
print(e)
此處所有代碼和資訊均取自https://www.tensorflow.org/guide/gpu#limiting_gpu_memory_growth
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/417935.html
標籤:
