-
Notifications
You must be signed in to change notification settings - Fork 76
issue/695 c++ infinicore::nn::module支持bf16 #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+81
−25
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| #include "infinicore/nn/rope.hpp" | ||
| #include "../../utils.h" | ||
| #include "../utils.hpp" | ||
| #include "infinicore/ops.hpp" | ||
| #include <algorithm> | ||
| #include <cmath> | ||
| #include <functional> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace infinicore::nn { | ||
|
|
||
|
|
@@ -50,36 +53,63 @@ void RoPE::initialize_cache() { | |
| for (size_t pos = 0; pos < max_seq_len_; pos++) { | ||
| for (size_t j = 0; j < cache_dim; j++) { | ||
| // GPT-J style inverse frequency: theta^(-2j/head_dim) | ||
| double inv_freq = 1.0 / std::pow(theta_, 2.0 * static_cast<double>(j) / static_cast<double>(head_dim_)); | ||
| // Compute directly in float to avoid double->float casting | ||
| float inv_freq = 1.0f / std::pow(static_cast<float>(theta_), 2.0f * static_cast<float>(j) / static_cast<float>(head_dim_)); | ||
|
|
||
| // Compute angle: position * inverse_frequency | ||
| double angle = static_cast<double>(pos) * inv_freq; | ||
| float angle = static_cast<float>(pos) * inv_freq; | ||
|
|
||
| // Compute sin and cos | ||
| sin_data[pos * cache_dim + j] = static_cast<float>(std::sin(angle)); | ||
| cos_data[pos * cache_dim + j] = static_cast<float>(std::cos(angle)); | ||
| // Compute sin and cos directly on float | ||
| sin_data[pos * cache_dim + j] = std::sin(angle); | ||
| cos_data[pos * cache_dim + j] = std::cos(angle); | ||
| } | ||
| } | ||
|
|
||
| // Create CPU tensors and copy data | ||
| auto sin_cpu = Tensor::from_blob(sin_data.data(), {max_seq_len_, cache_dim}, DataType::F32, cpu_device); | ||
| auto cos_cpu = Tensor::from_blob(cos_data.data(), {max_seq_len_, cache_dim}, DataType::F32, cpu_device); | ||
| // Convert to target dtype on CPU (matching Python's numpy astype conversion pattern) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // Python: np_array.astype(ml_dtypes.bfloat16, copy=True) converts F32 -> BF16 | ||
| if (dtype_ == DataType::F32) { | ||
| // Direct use of F32 data | ||
| auto sin_f32_cpu = Tensor::from_blob(sin_data.data(), {max_seq_len_, cache_dim}, DataType::F32, cpu_device); | ||
| auto cos_f32_cpu = Tensor::from_blob(cos_data.data(), {max_seq_len_, cache_dim}, DataType::F32, cpu_device); | ||
| sin_cache_->copy_from(sin_f32_cpu); | ||
| cos_cache_->copy_from(cos_f32_cpu); | ||
| } else if (dtype_ == DataType::BF16) { | ||
| // Convert F32 to BF16 using the same conversion as Python's ml_dtypes.bfloat16 | ||
| // This uses round-to-nearest-even (matching _f32_to_bf16 implementation) | ||
| std::vector<bf16_t> sin_bf16_data(max_seq_len_ * cache_dim); | ||
| std::vector<bf16_t> cos_bf16_data(max_seq_len_ * cache_dim); | ||
|
|
||
| for (size_t i = 0; i < sin_data.size(); i++) { | ||
| sin_bf16_data[i] = utils::cast<bf16_t, float>(sin_data[i]); | ||
| cos_bf16_data[i] = utils::cast<bf16_t, float>(cos_data[i]); | ||
| } | ||
|
|
||
| auto sin_bf16_cpu = Tensor::from_blob(sin_bf16_data.data(), {max_seq_len_, cache_dim}, DataType::BF16, cpu_device); | ||
| auto cos_bf16_cpu = Tensor::from_blob(cos_bf16_data.data(), {max_seq_len_, cache_dim}, DataType::BF16, cpu_device); | ||
|
|
||
| // Copy to device | ||
| // Note: Cache is created with dtype_, but we compute in F32 for precision. | ||
| // If dtype_ != F32, copy_from will fail. For now, we only support F32 cache. | ||
| // TODO: Add dtype conversion support when cast operation is available | ||
| if (dtype_ != DataType::F32) { | ||
| // copy_from handles cross-device copying to target device | ||
| sin_cache_->copy_from(sin_bf16_cpu); | ||
| cos_cache_->copy_from(cos_bf16_cpu); | ||
| } else if (dtype_ == DataType::F16) { | ||
| // Convert F32 to F16 | ||
| std::vector<fp16_t> sin_f16_data(max_seq_len_ * cache_dim); | ||
| std::vector<fp16_t> cos_f16_data(max_seq_len_ * cache_dim); | ||
|
|
||
| for (size_t i = 0; i < sin_data.size(); i++) { | ||
| sin_f16_data[i] = utils::cast<fp16_t, float>(sin_data[i]); | ||
| cos_f16_data[i] = utils::cast<fp16_t, float>(cos_data[i]); | ||
| } | ||
|
|
||
| auto sin_f16_cpu = Tensor::from_blob(sin_f16_data.data(), {max_seq_len_, cache_dim}, DataType::F16, cpu_device); | ||
| auto cos_f16_cpu = Tensor::from_blob(cos_f16_data.data(), {max_seq_len_, cache_dim}, DataType::F16, cpu_device); | ||
|
|
||
| sin_cache_->copy_from(sin_f16_cpu); | ||
| cos_cache_->copy_from(cos_f16_cpu); | ||
| } else { | ||
| throw std::runtime_error( | ||
| "RoPE cache dtype conversion not yet supported. Please use DataType::F32 for cache. " | ||
| "Requested dtype: " | ||
| "RoPE cache dtype conversion not yet supported for dtype: " | ||
| + std::to_string(static_cast<int>(dtype_))); | ||
| } | ||
|
|
||
| // copy_from handles cross-device copying automatically | ||
| // Direct copy from CPU to target device avoids double copying | ||
| sin_cache_->copy_from(sin_cpu); | ||
| cos_cache_->copy_from(cos_cpu); | ||
| } | ||
|
|
||
| Tensor RoPE::forward(const Tensor &x, const Tensor &pos) const { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.