| 73 | } |
| 74 | |
| 75 | core::TensorValue repeat_kv_heads(core::ModuleBuildContext & ctx, const core::TensorValue & input, int64_t repeats) { |
| 76 | if (repeats == 1) { |
| 77 | return input; |
| 78 | } |
| 79 | const auto contiguous = core::ensure_backend_addressable_layout(ctx, input); |
| 80 | const int64_t batch = contiguous.shape.dims[0]; |
| 81 | const int64_t kv_heads = contiguous.shape.dims[1]; |
| 82 | const int64_t steps = contiguous.shape.dims[2]; |
| 83 | const int64_t dim = contiguous.shape.dims[3]; |
| 84 | auto expanded = core::reshape_tensor( |
| 85 | ctx, |
| 86 | contiguous, |
| 87 | core::TensorShape::from_dims({batch, kv_heads, 1, steps * dim})); |
| 88 | expanded = modules::RepeatModule({core::TensorShape::from_dims({batch, kv_heads, repeats, steps * dim})}) |
| 89 | .build(ctx, expanded); |
| 90 | expanded = core::ensure_backend_addressable_layout(ctx, expanded); |
| 91 | return core::reshape_tensor( |
| 92 | ctx, |
| 93 | expanded, |
| 94 | core::TensorShape::from_dims({batch, kv_heads * repeats, steps, dim})); |
| 95 | } |
| 96 | |
| 97 | core::TensorValue attention_from_heads( |
| 98 | core::ModuleBuildContext & ctx, |
no test coverage detected