|
1 | 1 | #include "infinicore/nn/rope.hpp" |
2 | 2 | #include "../../utils.h" |
3 | 3 | #include "../utils.hpp" |
| 4 | +#include "infinicore/ops/apply_rope.hpp" |
| 5 | +#include "infinicore/ops/embedding.hpp" |
4 | 6 | #include "infinicore/ops/mrope.hpp" |
5 | 7 | #include "infinicore/ops/rope.hpp" |
6 | 8 | #include "infinicore/ops/rotary_embedding.hpp" |
7 | 9 | #include <algorithm> |
8 | 10 | #include <cassert> |
9 | 11 | #include <cmath> |
| 12 | +#include <cstdlib> |
| 13 | +#include <cstring> |
10 | 14 | #include <functional> |
11 | 15 | #include <stdexcept> |
12 | 16 | #include <utility> |
@@ -178,6 +182,20 @@ void RoPE::initialize_cache() { |
178 | 182 | "RoPE cache dtype conversion not yet supported for dtype: " |
179 | 183 | + std::to_string(static_cast<int>(dtype_))); |
180 | 184 | } |
| 185 | + const char *apply_rope = std::getenv("INFINICORE_ASCEND_APPLY_ROPE"); |
| 186 | + if (device_.getType() == Device::Type::ASCEND && !mrope_section_ |
| 187 | + && algo_ == Algo::GPT_NEOX && rotary_dim_ == head_dim_ |
| 188 | + && (head_dim_ == 64 || head_dim_ == 128) |
| 189 | + && apply_rope && std::strcmp(apply_rope, "1") == 0) { |
| 190 | + // Prepare full-width NeoX cos/sin once. Each half repeats the same |
| 191 | + // frequencies; use strided copies, never per-row D2D submissions. |
| 192 | + auto full = Tensor::empty({max_seq_len_, rotary_dim_ * 2}, dtype_, device_); |
| 193 | + for (size_t offset : {size_t{0}, cache_dim}) { |
| 194 | + full->narrow({{1, offset, cache_dim}})->copy_from(cos_cache_); |
| 195 | + full->narrow({{1, rotary_dim_ + offset, cache_dim}})->copy_from(sin_cache_); |
| 196 | + } |
| 197 | + register_buffer("aclnn_apply_cos_sin_cache", full); |
| 198 | + } |
181 | 199 | } |
182 | 200 |
|
183 | 201 | Tensor RoPE::forward(const Tensor &x, const Tensor &pos, bool in_place) const { |
@@ -279,6 +297,35 @@ std::pair<Tensor, Tensor> RoPE::forward(const Tensor &q_out, |
279 | 297 | const Tensor &k, |
280 | 298 | const Tensor &positions) const { |
281 | 299 | if (!mrope_section_.has_value()) { |
| 300 | + auto compatible = [this](const Tensor &t) { |
| 301 | + const size_t rank = t->ndim(); |
| 302 | + return (rank == 3 || (rank == 4 && t->size(0) == 1)) |
| 303 | + && t->size(rank - 1) == head_dim_ |
| 304 | + && t->stride(rank - 1) == 1 |
| 305 | + && t->stride(rank - 2) == static_cast<ptrdiff_t>(head_dim_); |
| 306 | + }; |
| 307 | + |
| 308 | + const char *apply_flag = std::getenv("INFINICORE_ASCEND_APPLY_ROPE"); |
| 309 | + const auto full_cache = buffers_.find("aclnn_apply_cos_sin_cache"); |
| 310 | + if (device_.getType() == Device::Type::ASCEND && apply_flag |
| 311 | + && std::strcmp(apply_flag, "1") == 0 && full_cache != buffers_.end() |
| 312 | + && positions->ndim() == 1 && positions->dtype() == DataType::I64 |
| 313 | + && positions->is_contiguous() && compatible(q) && compatible(k) |
| 314 | + && q_out->is_contiguous() && k_out->is_contiguous()) { |
| 315 | + const auto tokens = positions->size(0); |
| 316 | + auto rows = op::embedding(positions, full_cache->second); |
| 317 | + auto cos = rows->narrow({{1, 0, rotary_dim_}})->contiguous()->view({tokens, 1, rotary_dim_}); |
| 318 | + auto sin = rows->narrow({{1, rotary_dim_, rotary_dim_}})->contiguous()->view({tokens, 1, rotary_dim_}); |
| 319 | + auto mutable_q_out = q_out; |
| 320 | + auto mutable_k_out = k_out; |
| 321 | + mutable_q_out->copy_from(q); |
| 322 | + mutable_k_out->copy_from(k); |
| 323 | + const auto q_rank = q_out->ndim(), k_rank = k_out->ndim(); |
| 324 | + auto qo = q_out->view({tokens, q_out->size(q_rank - 2), head_dim_}); |
| 325 | + auto ko = k_out->view({tokens, k_out->size(k_rank - 2), head_dim_}); |
| 326 | + op::apply_rope_(qo, ko, cos, sin); |
| 327 | + return {q_out, k_out}; |
| 328 | + } |
282 | 329 | #ifdef ENABLE_INFINIOPS_API |
283 | 330 | if (cos_sin_cache_) { |
284 | 331 | auto mutable_q_out = q_out; |
|
0 commit comments