Discussion: Batch size automatically
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1k
- Forks
- 346
- PR merge metrics
- No merged PRs in 30d
Description
import torch
def test_batch_size(model, batch_size, input_shape):
try:
input_data = torch.rand((batch_size,) + input_shape).cuda()
output_data = model(input_data)
del input_data, output_data
torch.cuda.empty_cache()
return True
except RuntimeError as e:
if 'out of memory' in str(e):
return False
else:
raise e
def find_max_batch_size(model, input_shape):
if not torch.cuda.is_available():
raise ValueError("No GPUs available.")
min_batch_size = 1
max_batch_size = 1024
while min_batch_size < max_batch_size:
mid_batch_size = (min_batch_size + max_batch_size) // 2
if test_batch_size(model, mid_batch_size, input_shape):
min_batch_size = mid_batch_size + 1
else:
max_batch_size = mid_batch_size
return max_batch_size - 1
# Let's say we have a simple model:
model = torch.nn.Sequential(
torch.nn.Linear(100, 64),
torch.nn.ReLU(),
torch.nn.Linear(64, 10)
).cuda()
max_batch_size = find_max_batch_size(model, input_shape=(100,))
print("Max batch size: ", max_batch_size)
This is a silly idea, that increments batch size. The idea is to change the random data with original datasets.
Another idea is taking the memory of gpu and calculate it
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue contains a Python prototype for probing CUDA batch sizes, but names no repository files, tests, or entry points. First determine how TorchBench selects datasets and records GPU memory, then define the supported behavior and validation needed for automatic batch sizing; the work is done when those requirements are implemented and tested against representative original datasets.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100