(
param: Union[torch.Tensor, NotYetLoadedTensor], config: Config
)
| 274 | |
| 275 | |
| 276 | def tensor_split( |
| 277 | param: Union[torch.Tensor, NotYetLoadedTensor], config: Config |
| 278 | ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: |
| 279 | def kstart(start, blen, klen) -> int: |
| 280 | """returns start index of keys in batch""" |
| 281 | return start + (blen - (klen * 2)) |
| 282 | |
| 283 | def vstart(start, blen, klen) -> int: |
| 284 | """returns start index of values in batch""" |
| 285 | return start + blen - klen |
| 286 | |
| 287 | def vend(start, blen) -> int: |
| 288 | """returns last index of values in batch""" |
| 289 | return start + blen |
| 290 | |
| 291 | # num observations |
| 292 | nobs = param.shape[0] |
| 293 | # batch length |
| 294 | blen = nobs // config.n_query_groups |
| 295 | # key length in batch |
| 296 | klen = config.head_size |
| 297 | # value length in batch |
| 298 | vlen = config.head_size |
| 299 | # the starting index of each new batch |
| 300 | starts = range(0, nobs, blen) |
| 301 | # the indices to splice on |
| 302 | splices = [ |
| 303 | (s, kstart(s, blen, klen), vstart(s, blen, vlen), vend(s, blen)) for s in starts |
| 304 | ] |
| 305 | |
| 306 | qc = () |
| 307 | kc = () |
| 308 | vc = () |
| 309 | |
| 310 | for splice in splices: |
| 311 | qs, ks, vs, ve = splice |
| 312 | qc += (param[qs:ks, :],) |
| 313 | kc += (param[ks:vs, :],) |
| 314 | vc += (param[vs:ve, :],) |
| 315 | |
| 316 | q = torch.cat(qc) |
| 317 | k = torch.cat(kc) |
| 318 | v = torch.cat(vc) |
| 319 | |
| 320 | return q, k, v |
| 321 | |
| 322 | |
| 323 | def maybe_unwrap_state_dict( |
no test coverage detected