(
models,
hidden_state,
cache,
mask,
pos_emb,
model_input_dict,
num_blocks_per_chunk,
batch_name,
)
| 199 | |
| 200 | |
| 201 | def forward_and_save( |
| 202 | models, |
| 203 | hidden_state, |
| 204 | cache, |
| 205 | mask, |
| 206 | pos_emb, |
| 207 | model_input_dict, |
| 208 | num_blocks_per_chunk, |
| 209 | batch_name, |
| 210 | ): |
| 211 | for chunk_idx in range(len(models)): |
| 212 | cache_in = cache[chunk_idx] |
| 213 | |
| 214 | try: |
| 215 | model_input_dict[str(chunk_idx)] = { |
| 216 | **model_input_dict[str(chunk_idx)], |
| 217 | batch_name: { |
| 218 | "hidden_state": hidden_state, |
| 219 | "mask": mask, |
| 220 | "pos_emb": pos_emb, |
| 221 | "cache": cache_in, |
| 222 | }, |
| 223 | } |
| 224 | except: |
| 225 | model_input_dict[str(chunk_idx)] = { |
| 226 | batch_name: { |
| 227 | "hidden_state": hidden_state, |
| 228 | "mask": mask, |
| 229 | "pos_emb": pos_emb, |
| 230 | "cache": cache_in, |
| 231 | } |
| 232 | } |
| 233 | with torch.no_grad(): |
| 234 | model_out = models[chunk_idx]( |
| 235 | hidden_state, mask, pos_emb, *torch.split(cache_in, 1, dim=0) |
| 236 | ) |
| 237 | hidden_state = model_out[0] |
| 238 | cache[chunk_idx] = torch.cat( |
| 239 | model_out[1 : 1 + 2 * num_blocks_per_chunk[chunk_idx]], dim=0 |
| 240 | ).clone() |
| 241 | return hidden_state, cache |
| 242 | |
| 243 | |
| 244 | def prepare_model_inputs( |
no test coverage detected