-
Notifications
You must be signed in to change notification settings - Fork 1
CUDA backend optimization: tensor cores, warp primitives, advanced memory management, and scalability features #17
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
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e7bccd5
Initial plan
Copilot d458e11
Add CUDA optimization kernels: tensor cores, warp primitives, memory …
Copilot 69d7cb0
Integrate advanced memory management and architecture detection into …
Copilot d1ae028
Add comprehensive CUDA optimization tests and documentation
Copilot ae6b807
Address code review feedback: fix architecture checks, nullptr usage,…
Copilot b985970
Add implementation summary document
Copilot 8ee0454
Fix 5 critical bugs: WMMA warp-collective usage, __CUDA_ARCH__ checks…
Copilot 79eafe3
Implement advanced CUDA features: graphs, multi-GPU, persistent kerne…
Copilot 872eb72
Fix code review issues: DoubleBuffer validation, tensor core fragment…
Copilot a1926a0
Fix compilation and runtime bugs: missing includes, variable shadowin…
Copilot 28b23f5
Delete CUDA_OPTIMIZATIONS.md
NripeshN 63f9d98
Delete CUDA_ADVANCED_FEATURES.md
NripeshN 17a597b
Delete CUDA_IMPLEMENTATION_SUMMARY.md
NripeshN 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
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| MetalFish - A GPU-accelerated UCI chess engine | ||
| Copyright (C) 2025 Nripesh Niketan | ||
|
|
||
| FP16 Weight Storage Implementation | ||
| */ | ||
|
|
||
| #ifdef USE_CUDA | ||
|
|
||
| #include "cuda_fp16_weights.h" | ||
| #include <iostream> | ||
| #include <unordered_map> | ||
| #include <string> | ||
|
|
||
| namespace MetalFish { | ||
| namespace GPU { | ||
| namespace CUDA { | ||
|
|
||
| FP16WeightManager::~FP16WeightManager() { | ||
| clear_all(); | ||
| } | ||
|
|
||
| half* FP16WeightManager::convert_and_store_weights( | ||
| const int16_t* int16_weights, size_t size, float scale) { | ||
|
|
||
| // Allocate host memory for FP16 conversion | ||
| std::vector<half> fp16_host(size); | ||
|
|
||
| // Convert INT16 to FP16 | ||
| for (size_t i = 0; i < size; i++) { | ||
| float val = static_cast<float>(int16_weights[i]) / scale; | ||
| fp16_host[i] = __float2half(val); | ||
| } | ||
|
|
||
| // Allocate device memory | ||
| half* device_ptr = nullptr; | ||
| cudaError_t err = cudaMalloc(&device_ptr, size * sizeof(half)); | ||
| if (err != cudaSuccess) { | ||
| std::cerr << "[FP16 Weights] Failed to allocate device memory: " | ||
| << cudaGetErrorString(err) << std::endl; | ||
| return nullptr; | ||
| } | ||
|
|
||
| // Copy to device | ||
| err = cudaMemcpy(device_ptr, fp16_host.data(), size * sizeof(half), | ||
| cudaMemcpyHostToDevice); | ||
| if (err != cudaSuccess) { | ||
| std::cerr << "[FP16 Weights] Failed to copy to device: " | ||
| << cudaGetErrorString(err) << std::endl; | ||
| cudaFree(device_ptr); | ||
| return nullptr; | ||
| } | ||
|
|
||
| total_memory_ += size * sizeof(half); | ||
| return device_ptr; | ||
| } | ||
|
|
||
| half* FP16WeightManager::convert_and_store_biases( | ||
| const int32_t* int32_biases, size_t size, float scale) { | ||
|
|
||
| // Allocate host memory for FP16 conversion | ||
| std::vector<half> fp16_host(size); | ||
|
|
||
| // Convert INT32 to FP16 | ||
| for (size_t i = 0; i < size; i++) { | ||
| float val = static_cast<float>(int32_biases[i]) / scale; | ||
| fp16_host[i] = __float2half(val); | ||
| } | ||
|
|
||
| // Allocate device memory | ||
| half* device_ptr = nullptr; | ||
| cudaError_t err = cudaMalloc(&device_ptr, size * sizeof(half)); | ||
| if (err != cudaSuccess) { | ||
| std::cerr << "[FP16 Biases] Failed to allocate device memory: " | ||
| << cudaGetErrorString(err) << std::endl; | ||
| return nullptr; | ||
| } | ||
|
|
||
| // Copy to device | ||
| err = cudaMemcpy(device_ptr, fp16_host.data(), size * sizeof(half), | ||
| cudaMemcpyHostToDevice); | ||
| if (err != cudaSuccess) { | ||
| std::cerr << "[FP16 Biases] Failed to copy to device: " | ||
| << cudaGetErrorString(err) << std::endl; | ||
| cudaFree(device_ptr); | ||
| return nullptr; | ||
| } | ||
|
|
||
| total_memory_ += size * sizeof(half); | ||
| return device_ptr; | ||
| } | ||
|
|
||
| half* FP16WeightManager::get_fp16_weights(const std::string& layer_name) { | ||
| auto it = weights_.find(layer_name); | ||
| return (it != weights_.end()) ? it->second.device_ptr : nullptr; | ||
| } | ||
|
|
||
| half* FP16WeightManager::get_fp16_biases(const std::string& layer_name) { | ||
| auto it = biases_.find(layer_name); | ||
| return (it != biases_.end()) ? it->second.device_ptr : nullptr; | ||
| } | ||
|
|
||
| void FP16WeightManager::clear_all() { | ||
| for (auto& [name, data] : weights_) { | ||
| if (data.device_ptr) { | ||
| cudaFree(data.device_ptr); | ||
| } | ||
| } | ||
|
|
||
| for (auto& [name, data] : biases_) { | ||
| if (data.device_ptr) { | ||
| cudaFree(data.device_ptr); | ||
| } | ||
| } | ||
|
|
||
| weights_.clear(); | ||
| biases_.clear(); | ||
| total_memory_ = 0; | ||
| } | ||
|
|
||
| } // namespace CUDA | ||
| } // namespace GPU | ||
| } // namespace MetalFish | ||
|
|
||
| #endif // USE_CUDA | ||
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FP16WeightManager never stores allocated pointers in maps
Medium Severity
The
convert_and_store_weightsandconvert_and_store_biasesfunctions allocate device memory and return the pointer, but never add entries to theweights_orbiases_maps. This meansget_fp16_weights/get_fp16_biaseswill never find these allocations, andclear_all()(called in the destructor) won't free them, causing memory leaks.Additional Locations (1)
src/gpu/cuda/cuda_fp16_weights.cu#L57-L91