how large batch size i can fit training linear layer 768->10000 in 24gb?
- User
how large batch size i can fit training linear layer 768->10000 in 24gb?
- Assistant
To determine the largest batch size you can fit for training a linear layer with an input size of 768 and an output size of 10,000 using a GPU with 24GB of memory, you need to consider several factors:
-
Memory required for the model parameters:
- The weight matrix for a linear layer from 768 to 10,000 dimensions requires ( 768 \times 10,000 ) elements.
- Assuming each element is a 32-bit float (which is 4 bytes), the weight matrix requires: [ \text{Weight memory} = 768 \times 10,000 \times 4 \text{ bytes} = 30,720,000 \text{ bytes} = 30.72 \text{ MB} ]
- The biases for the output layer require ( 10,000 \times 4 \text{ bytes} = 40,000 \text{ bytes} = 0.04 \text{ MB} ).
- So, total memory for parameters is roughly 30.76 MB.
-
Memory required for the activations:
- The memory required per batch for the input activations is: [ \text{Input memory} = \text{Batch size} \times 768 \times 4 \text{ bytes} ]
- The memory required for the output activations is: [ \text{Output memory} = \text{Batch size} \times 10,000 \times 4 \text{ bytes} ]
-
Memory required for gradients:
- Memory for storing gradients is typically comparable to the memory required for the parameters plus activations.
Let's break it down:
Given that you have 24GB (24 * 1024^3 bytes = 25,165,824,000 bytes) of memory available:
Total memory needed
Let ( B ) be the batch size: [ \text{Total memory} = \text{Parameter memory} + \text{Input memory} + \text{Output memory} + \text{Gradient memory} ]
Since the gradients typically double the memory required for parameters and activations, we approximate: [ \text{Total memory} \approx 2 \times (\text{Parameter memory} + \text{Activations memory}) ]
[ \text{Total activations memory} = B \times 768 \times 4 + B \times 10,000 \times 4 \text{ bytes} ]
[ = B \times (768 + 10,000) \times 4 \text{ bytes} ]
[ = B \times 10,768 \times 4 \text{ bytes} ]
[ = B \times 43,072 \text{ bytes} ]
Approximation
Ignoring the relatively small parameter memory (30.76 MB), our critical term is the activations and gradient memory:
[ \text{Total memory} \approx 2 \times B \times 43,072 \text{ bytes} ]
To find the maximum batch size ( B ):
[ 2 \times B \times 43,072 \text{ bytes} \leq 25,165,824,000 \text{ bytes} ]
[ B \leq \frac{25,165,824,000}{2 \times 43,072} ]
[ B \approx \frac{25,165,824,000}{86,144} ]
[ B \approx 292,124 ]
Result
Given these approximations, you can theoretically fit a batch size of around 292,000. However, this is under ideal conditions and without considering additional overhead from other parts of the model, PyTorch/TensorFlow cruft, data loading, etc.
-
- User
import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import Dataset, DataLoader import json import numpy as np import math import inspect from dataclasses import dataclass
import torch import torch.nn as nn from torch.nn import functional as F
import os import json import torch
images = {} dir = "H:/gelbooru_all/embeddings/" for path in os.listdir(dir): path = dir+path if(os.path.isdir(path)): print(path) result = {} paths = torch.load(f'{path}_path.pt') tensors = torch.load(f'{path}_tensor.pt')
codefor i, path in enumerate(paths): images[path] = tensors[i]torch.cuda.empty_cache()
class TaggerDataset(Dataset): def init(self, json_file): with open(json_file, 'r') as file: self.data = json.load(file) self.num_labels = len(self.data["IdToLabel"]) self.images = self.data["Images"]
code - Assistant
- User
Step 4: Batch Preprocessing
Preprocess data in getitem in a batch-wise manner if possible.
how
- Assistant
- User
maybe there is way to faster make 'label' tensor?
- Assistant
- User
maybe something more?
- Assistant