tf-alloc

Simplication of GPU allocation for Tensorflow2


License
MIT
Install
pip install tf-alloc==0.0.3

Documentation

tf_alloc

Simpliying GPU allocation for Tensorflow

  • Developer: korkite (Junseo Ko)

Installation

pip install tf-alloc

โญ๏ธ Why tf_alloc? Problems?

  • Compare to pytorch, tensorflow allocate all GPU memory to single training.
  • However, it is too much waste because, some training does not use whole GPU memory.
  • To solve this problem, TF engineers use two methods.
  1. Limit to use only single GPU
  2. Limit the use of only a certain percentage of GPUs.
  • However, these methods require complex code and memory management.

โญ๏ธ Why tf_alloc? How to solve?

tf_alloc simplfy and automate GPU allocation using two methods.

โญ๏ธ How to allocate?

  • Before using tf_alloc, you have to install tensorflow fits for your environment.
  • This library does not install specific tensorflow version.
# On the top of the code
from tf_alloc import allocate as talloc
talloc(gpu=1, percentage=0.5)

import tensorflow as tf
""" your code"""

It is only code for allocating GPU in certain percentage.

Parameters:

  • gpu = which gpu you want to use (if you have two gpu than [0, 1] is possible)
  • percentage = the percentage of memory usage on single gpu. 1.0 for maximum use.

โญ๏ธ Additional Function.

GET GPU Objects

gpu_objs = get_gpu_objects()
  • To use this code, you can get gpu objects that contains gpu information.
  • You can set GPU backend by using this function.

GET CURRENT STATE

Defualt
current(
    gpu_id = False, 
    total_memory=False, 
    used = False, 
    free = False, 
    percentage_of_use = False,
    percentage_of_free = False,
)
  • You can use this functions to see current GPU state and possible maximum allocation percentage.
  • Without any parameters, than it only visualize possible maximum allocation percentage.
  • It is cmd line visualizer. It doesn't return values.

Parameters

  • gpu_id = visualize the gpu id number
  • total_memory = visualize the total memory of GPU
  • used = visualize the used memory of GPU
  • free = visualize the free memory of GPU
  • percentage_of_used = visualize the percentage of used memory of GPU
  • percentage_of_free = visualize the percentage of free memory of GPU

ํ•œ๊ตญ์–ด๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ!

์„ค์น˜

pip install tf-alloc

๋ฌธ์ œ์ •์˜:

  • ํ…์„œํ”Œ๋กœ์šฐ๋Š” ํŒŒ์ดํ† ์น˜์™€ ๋‹ค๋ฅด๊ฒŒ ํ›ˆ๋ จ์‹œ GPU๋ฅผ ์ „๋ถ€ ํ• ๋‹นํ•ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์‹ค์ œ๋กœ GPU๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํฐ ๋‚ญ๋น„๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ
  1. GPU๋ฅผ 1๊ฐœ๋งŒ ์“ฐ๋„๋ก ์ œํ•œํ•˜๊ธฐ
  2. GPU์—์„œ ํŠน์ • ๋ฉ”๋ชจ๋ฆฌ๋งŒํผ๋งŒ ์‚ฌ์šฉํ•˜๋„๋ก ์ œํ•œํ•˜๊ธฐ
  • ์ด ๋‘๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•์„ ์œ„ํ•ด์„  ๋ณต์žกํ•œ ์ฝ”๋“œ์™€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

ํ•ด๊ฒฐ์ฑ…:

  • ์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์ž๋™์œผ๋กœ ๋ช‡๋ฒˆ GPU๋ฅผ ์–ผ๋งŒํผ๋งŒ ํ• ๋‹นํ• ์ง€ ์ •ํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
  • ํ•จ์ˆ˜ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
# On the top of the code
from tf_alloc import allocate as talloc
talloc(gpu=1, percentage=0.5)

import tensorflow as tf
""" your code"""
  • ๋งจ์œ„์— tf_alloc์—์„œ allocateํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ๋‹ค๊ฐ€ gpuํŒŒ๋ผ๋ฏธํ„ฐ์™€ percentage ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฃผ์–ด ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋ฉด ์ž๋™์œผ๋กœ ๋ช‡๋ฒˆ์˜ GPU๋ฅผ ์–ผ๋งŒํผ์˜ ๋น„์œจ๋กœ ์‚ฌ์šฉํ• ์ง€ ์ •ํ•ด์„œ ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
  • ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.

ํŒŒ๋ผ๋ฏธํ„ฐ ์„ค๋ช…

  • gpu = ๋ช‡๋ฒ” GPU๋ฅผ ์“ธ ๊ฒƒ์ธ์ง€ GPU์˜ ์•„์ด๋””๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. (๋งŒ์•ฝ gpu๊ฐ€ 2๊ฐœ ์žˆ๋‹ค๋ฉด 0, 1 ์ด ์•„์ด๋””๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.)

  • percentage = ์„ ํƒํ•œ GPU๋ฅผ ๋ช‡์˜ ๋น„์œจ๋กœ ์“ธ๊ฑด์ง€ ์ •ํ•ด์ค๋‹ˆ๋‹ค. (1.0์„ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น GPU๋ฅผ ์ „๋ถ€ ์”๋‹ˆ๋‹ค)

  • ๋งŒ์•ฝ percentage๊ฐ€ ๋ช‡์ธ์ง€ ๋ชจ๋ฅธ๋‹ค๋ฉด 0์—์„œ 1 ์‚ฌ์ด์˜ ๊ฐ’์„ ๋„ฃ์–ด์„œ ํ• ๋‹นํ•ด๋ณด๋ฉด ์ตœ๋Œ€ ์‚ฌ์šฉ๊ฐ€๋Šฅ๋Ÿ‰์ด ์–ผ๋งŒํผ์ด๋ผ๊ณ  ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•˜๋‹ˆ๊นŒ ๊ฑฑ์ •์—†์ด ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ›ˆ๋ จ์— ๋ฐฉํ•ด๋ฅผ ์ฃผ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, nvidia-smi๋ฅผ ์ณ๊ฐ€๋ฉด์„œ ํ• ๋‹น์„ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋งค์šฐ ์•ˆ์ •์ ์ž…๋‹ˆ๋‹ค.

  • ํ•ต์‹ฌ๊ธฐ๋Šฅ๋งŒ ํ•œ๊ตญ์–ด๋กœ ์จ ๋†“์•˜๊ณ , ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์€ ์˜๋ฌธ๋ฒ„์ „์„ ํ™•์ธํ•ด๋ณด์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.