| 509 | } |
| 510 | |
| 511 | std::vector<float> precompute_relative_projection( |
| 512 | int64_t seq, |
| 513 | int64_t head, |
| 514 | int64_t dim, |
| 515 | const Param & pos_weight, |
| 516 | const std::vector<float> & pos_emb) { |
| 517 | require_shape(pos_weight, {kHeads * kPosHeadDim, kPosDim}, "ZipEnhancer relative position weight"); |
| 518 | std::vector<float> table(static_cast<size_t>(seq * seq), 0.0f); |
| 519 | for (int64_t t1 = 0; t1 < seq; ++t1) { |
| 520 | for (int64_t t2 = 0; t2 < seq; ++t2) { |
| 521 | const int64_t rel = (seq - 1 - t1) + t2; |
| 522 | float sum = 0.0f; |
| 523 | const int64_t out = head * kPosHeadDim + dim; |
| 524 | for (int64_t i = 0; i < kPosDim; ++i) { |
| 525 | sum += pos_emb[static_cast<size_t>(rel * kPosDim + i)] * |
| 526 | pos_weight.values[static_cast<size_t>(out * kPosDim + i)]; |
| 527 | } |
| 528 | table[static_cast<size_t>(t1 * seq + t2)] = sum; |
| 529 | } |
| 530 | } |
| 531 | return table; |
| 532 | } |
| 533 | |
| 534 | std::vector<core::TensorValue> graph_attention_weights( |
| 535 | core::ModuleBuildContext & ctx, |
no test coverage detected