Update tensor_server.py
Browse files- tensor_server.py +42 -12
tensor_server.py
CHANGED
|
@@ -144,21 +144,51 @@ def load_chunk(chunk: ModelChunk) -> torch.nn.Module:
|
|
| 144 |
raise ValueError(f"Chunk file not found: {chunk_file}")
|
| 145 |
|
| 146 |
# For raw binary chunks, we'll create a simple buffer module
|
| 147 |
-
class ChunkBuffer(
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
super().__init__()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
self.chunk_path = chunk_path
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
self.
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
# Create and return the chunk buffer
|
| 164 |
chunk_model = ChunkBuffer(chunk_file, chunk_config)
|
|
|
|
| 144 |
raise ValueError(f"Chunk file not found: {chunk_file}")
|
| 145 |
|
| 146 |
# For raw binary chunks, we'll create a simple buffer module
|
| 147 |
+
class ChunkBuffer(nn.Module):
|
| 148 |
+
"""
|
| 149 |
+
A single Florence-2 caption chunk that receives pre-encoded image embeddings
|
| 150 |
+
and produces partial vocabulary logits.
|
| 151 |
+
"""
|
| 152 |
+
|
| 153 |
+
def __init__(self, chunk_path: str, config: dict):
|
| 154 |
super().__init__()
|
| 155 |
+
|
| 156 |
+
# Get dimensions from config
|
| 157 |
+
input_dim = config.get("input_dim", 1024) # Florence-2 embedding dim
|
| 158 |
+
output_dim = config.get("output_dim", 1000) # size of vocab shard
|
| 159 |
+
dropout = config.get("dropout", 0.1)
|
| 160 |
+
|
| 161 |
+
# Optional: chunk_path can point to pretrained weights
|
| 162 |
self.chunk_path = chunk_path
|
| 163 |
+
|
| 164 |
+
# Main projection layer: embedding → partial vocab logits
|
| 165 |
+
self.linear = nn.Linear(input_dim, output_dim)
|
| 166 |
+
|
| 167 |
+
# Optional normalization + dropout (stabilizes training or inference variance)
|
| 168 |
+
self.norm = nn.LayerNorm(input_dim)
|
| 169 |
+
self.dropout = nn.Dropout(dropout)
|
| 170 |
+
|
| 171 |
+
# Initialize weights (small variance, stable logits)
|
| 172 |
+
nn.init.xavier_uniform_(self.linear.weight)
|
| 173 |
+
nn.init.zeros_(self.linear.bias)
|
| 174 |
+
|
| 175 |
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 176 |
+
"""
|
| 177 |
+
Args:
|
| 178 |
+
x: Florence-2 image embedding tensor, shape [batch, 1024]
|
| 179 |
+
Returns:
|
| 180 |
+
logits for this vocab shard, shape [batch, output_dim]
|
| 181 |
+
"""
|
| 182 |
+
# Normalize + dropout
|
| 183 |
+
x = self.norm(x)
|
| 184 |
+
x = self.dropout(x)
|
| 185 |
+
|
| 186 |
+
# Linear projection to vocab slice
|
| 187 |
+
logits = self.linear(x)
|
| 188 |
+
|
| 189 |
+
# (Optional) softmax for probabilities, but usually the main model handles this
|
| 190 |
+
# probs = F.softmax(logits, dim=-1)
|
| 191 |
+
return logits
|
| 192 |
|
| 193 |
# Create and return the chunk buffer
|
| 194 |
chunk_model = ChunkBuffer(chunk_file, chunk_config)
|