-
Notifications
You must be signed in to change notification settings - Fork 60
issue/513: 内存使用情况统计&&内存引用计数和内存池实现(WIP) #514
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
Draft
Ceng23333
wants to merge
2
commits into
main
Choose a base branch
from
issue/513
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Fixed build script for InfiniCore | ||
| # This script sets up the environment and builds with proper linker configuration | ||
|
|
||
| echo "Setting up InfiniCore build environment..." | ||
|
|
||
| # Initialize conda | ||
| eval "$(conda shell.bash hook)" | ||
|
|
||
| # Activate the infinicore-env environment | ||
| conda activate infinicore-env | ||
|
|
||
| # Set CUDA_HOME to the conda environment | ||
| export CUDA_HOME=$CONDA_PREFIX | ||
|
|
||
| # Clean up conflicting environment variables | ||
| unset CC | ||
| unset CXX | ||
| unset NVCC_PREPEND_FLAGS | ||
| unset NVCC_APPEND_FLAGS | ||
| unset CUDA_ROOT | ||
|
|
||
| # Use system tools | ||
| export PATH="/usr/bin:$PATH" | ||
|
|
||
| # Create a wrapper for ld that converts -m64 to -m elf_x86_64 | ||
| mkdir -p /tmp/ld_wrapper | ||
| cat > /tmp/ld_wrapper/ld << 'EOF' | ||
| #!/bin/bash | ||
| # Convert -m64 to -m elf_x86_64 for system linker compatibility | ||
| args=() | ||
| skip_next=false | ||
| for arg in "$@"; do | ||
| if [ "$skip_next" = true ]; then | ||
| skip_next=false | ||
| continue | ||
| fi | ||
| if [ "$arg" = "-m64" ]; then | ||
| args+=("-m" "elf_x86_64") | ||
| elif [ "$arg" = "-fopenmp" ]; then | ||
| # Skip -fopenmp flag for linker, but add libgomp | ||
| args+=("-lgomp") | ||
| continue | ||
| elif [ "$arg" = "-m" ]; then | ||
| # Skip -m flag and its argument if it's elf_x86_64 (to avoid duplication) | ||
| skip_next=true | ||
| continue | ||
| else | ||
| args+=("$arg") | ||
| fi | ||
| done | ||
| # Add standard C++ library and other required libraries | ||
| args+=("-lstdc++" "-lm" "-lc" "-lgcc_s") | ||
| exec /usr/bin/ld "${args[@]}" | ||
| EOF | ||
| chmod +x /tmp/ld_wrapper/ld | ||
| export PATH="/tmp/ld_wrapper:$PATH" | ||
|
|
||
| echo "Environment setup complete!" | ||
| echo "CUDA_HOME: $CUDA_HOME" | ||
| echo "CONDA_PREFIX: $CONDA_PREFIX" | ||
|
|
||
| # Configure and build | ||
| echo "Configuring xmake..." | ||
| xmake f -c | ||
|
|
||
| echo "Building InfiniCore..." | ||
| xmake build | ||
|
|
||
| echo "Build completed!" |
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,139 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Example script showing how to use InfiniCore memory statistics | ||
| to monitor memory usage during tensor operations. | ||
| """ | ||
|
|
||
| import sys | ||
| import os | ||
|
|
||
| # Add the current directory to Python path | ||
| sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | ||
|
|
||
| try: | ||
| import infinicore | ||
| print("✓ Successfully imported infinicore") | ||
| except ImportError as e: | ||
| print(f"✗ Failed to import infinicore: {e}") | ||
| print("Make sure to build the project first with: xmake build _infinicore") | ||
| sys.exit(1) | ||
|
|
||
| def get_memory_summary(): | ||
| """Get a summary of current memory usage.""" | ||
| try: | ||
| device_stats = infinicore.get_device_memory_stats() | ||
| return { | ||
| 'allocations': device_stats.allocation[0].current, | ||
| 'allocated_bytes': device_stats.allocated_bytes[0].current, | ||
| 'active_blocks': device_stats.active[0].current, | ||
| 'device_allocations': device_stats.num_device_alloc, | ||
| 'device_deallocations': device_stats.num_device_free | ||
| } | ||
| except Exception as e: | ||
| print(f"Warning: Could not get memory stats: {e}") | ||
| return None | ||
|
|
||
| def print_memory_summary(title, stats): | ||
| """Print a concise memory summary.""" | ||
| if stats is None: | ||
| print(f"{title}: Unable to get memory statistics") | ||
| return | ||
|
|
||
| print(f"{title}:") | ||
| print(f" Allocations: {stats['allocations']}") | ||
| print(f" Allocated bytes: {stats['allocated_bytes']:,} bytes ({stats['allocated_bytes'] / 1024 / 1024:.2f} MB)") | ||
| print(f" Active blocks: {stats['active_blocks']}") | ||
| print(f" Device alloc/dealloc: {stats['device_allocations']}/{stats['device_deallocations']}") | ||
|
|
||
| def monitor_memory_usage(): | ||
| """Monitor memory usage during tensor operations.""" | ||
| print("=== InfiniCore Memory Usage Monitor ===\n") | ||
|
|
||
| # Initial memory state | ||
| initial_stats = get_memory_summary() | ||
| print_memory_summary("Initial Memory State", initial_stats) | ||
|
|
||
| try: | ||
| # Create some tensors to demonstrate memory usage | ||
| print("\n1. Creating tensors...") | ||
|
|
||
| # Create a large tensor | ||
| print(" Creating 1000x1000 float32 tensor...") | ||
| tensor1 = infinicore.empty((1000, 1000), dtype=infinicore.float32) | ||
| stats_after_tensor1 = get_memory_summary() | ||
| print_memory_summary("After creating tensor1", stats_after_tensor1) | ||
|
|
||
| # Create another tensor | ||
| print("\n Creating 500x500 float32 tensor...") | ||
| tensor2 = infinicore.empty((500, 500), dtype=infinicore.float32) | ||
| stats_after_tensor2 = get_memory_summary() | ||
| print_memory_summary("After creating tensor2", stats_after_tensor2) | ||
|
|
||
| # Create a third tensor | ||
| print("\n Creating 2000x2000 float32 tensor...") | ||
| tensor3 = infinicore.empty((2000, 2000), dtype=infinicore.float32) | ||
| stats_after_tensor3 = get_memory_summary() | ||
| print_memory_summary("After creating tensor3", stats_after_tensor3) | ||
|
|
||
| # Delete some tensors | ||
| print("\n2. Deleting tensors...") | ||
| del tensor1 | ||
| stats_after_del1 = get_memory_summary() | ||
| print_memory_summary("After deleting tensor1", stats_after_del1) | ||
|
|
||
| del tensor2 | ||
| stats_after_del2 = get_memory_summary() | ||
| print_memory_summary("After deleting tensor2", stats_after_del2) | ||
|
|
||
| # Final cleanup | ||
| print("\n3. Final cleanup...") | ||
| del tensor3 | ||
| final_stats = get_memory_summary() | ||
| print_memory_summary("Final Memory State", final_stats) | ||
|
|
||
| # Show memory difference | ||
| if initial_stats and final_stats: | ||
| print(f"\nMemory Usage Summary:") | ||
| print(f" Net allocations: {final_stats['allocations'] - initial_stats['allocations']}") | ||
| print(f" Net allocated bytes: {final_stats['allocated_bytes'] - initial_stats['allocated_bytes']:,} bytes") | ||
| print(f" Net active blocks: {final_stats['active_blocks'] - initial_stats['active_blocks']}") | ||
|
|
||
| print("\n✓ Memory monitoring completed successfully!") | ||
|
|
||
| except Exception as e: | ||
| print(f"✗ Error during memory monitoring: {e}") | ||
| import traceback | ||
| traceback.print_exc() | ||
|
|
||
| def demonstrate_stat_types(): | ||
| """Demonstrate different stat types and their usage.""" | ||
| print("\n=== Stat Types Demonstration ===\n") | ||
|
|
||
| try: | ||
| # Get device stats | ||
| device_stats = infinicore.get_device_memory_stats() | ||
|
|
||
| print("StatType.AGGREGATE statistics:") | ||
| print(f" Allocation count: {device_stats.allocation[0].current}") | ||
| print(f" Allocation peak: {device_stats.allocation[0].peak}") | ||
| print(f" Allocation total: {device_stats.allocation[0].allocated}") | ||
| print(f" Allocation freed: {device_stats.allocation[0].freed}") | ||
|
|
||
| print(f"\nStatType.SMALL_POOL statistics:") | ||
| print(f" Allocation count: {device_stats.allocation[1].current}") | ||
| print(f" Allocation peak: {device_stats.allocation[1].peak}") | ||
|
|
||
| print(f"\nStatType.LARGE_POOL statistics:") | ||
| print(f" Allocation count: {device_stats.allocation[2].current}") | ||
| print(f" Allocation peak: {device_stats.allocation[2].peak}") | ||
|
|
||
| print("\n✓ Stat types demonstration completed!") | ||
|
|
||
| except Exception as e: | ||
| print(f"✗ Error during stat types demonstration: {e}") | ||
| import traceback | ||
| traceback.print_exc() | ||
|
|
||
| if __name__ == "__main__": | ||
| monitor_memory_usage() | ||
| demonstrate_stat_types() |
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,30 +1,5 @@ | ||
| #pragma once | ||
|
|
||
| #include "device.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <functional> | ||
|
|
||
| namespace infinicore { | ||
|
|
||
| class Memory { | ||
| public: | ||
| using Deleter = std::function<void(std::byte *)>; | ||
|
|
||
| Memory(std::byte *data, size_t size, Device device, Deleter deleter, bool pin_memory = false); | ||
| ~Memory(); | ||
|
|
||
| std::byte *data(); | ||
| Device device() const; | ||
| size_t size() const; | ||
| bool is_pinned() const; | ||
|
|
||
| private: | ||
| std::byte *data_; | ||
| size_t size_; | ||
| Device device_; | ||
| Deleter deleter_; | ||
| bool is_pinned_; | ||
| }; | ||
|
|
||
| } // namespace infinicore | ||
| #include "memory/memory_block.hpp" | ||
| #include "memory/memory_pool.hpp" | ||
| #include "memory/memory_segment.hpp" |
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,39 @@ | ||
| #pragma once | ||
|
|
||
| #include "../device.hpp" | ||
|
|
||
| #include <cstddef> | ||
| #include <functional> | ||
| #include <memory> | ||
|
|
||
| namespace infinicore { | ||
|
|
||
| class MemoryBlock { | ||
| public: | ||
| using Deleter = std::function<void(std::byte *)>; | ||
|
|
||
| MemoryBlock(std::byte *data, size_t size, Device device, Deleter deleter, bool pin_memory = false); | ||
| ~MemoryBlock(); | ||
|
|
||
| // Copy constructor and copy assignment with reference counting | ||
| MemoryBlock(const MemoryBlock& other); | ||
| MemoryBlock& operator=(const MemoryBlock& other); | ||
|
|
||
| // Move constructor and move assignment | ||
| MemoryBlock(MemoryBlock&& other) noexcept; | ||
| MemoryBlock& operator=(MemoryBlock&& other) noexcept; | ||
|
|
||
| std::byte *data() const; | ||
| Device device() const; | ||
| size_t size() const; | ||
| bool is_pinned() const; | ||
|
|
||
| private: | ||
| std::byte *data_; | ||
| size_t size_; | ||
| Device device_; | ||
| Deleter deleter_; | ||
| bool is_pinned_; | ||
| }; | ||
|
|
||
| } // namespace infinicore |
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,52 @@ | ||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <unordered_map> | ||
| #include <mutex> | ||
| #include <atomic> | ||
| #include <cstddef> | ||
| #include <functional> | ||
|
|
||
| namespace infinicore { | ||
|
|
||
| struct MemoryInfo { | ||
| std::byte* ptr; | ||
| size_t size; | ||
| std::atomic<int> ref_count; | ||
| bool is_freed; | ||
|
|
||
| MemoryInfo(std::byte* p, size_t s) | ||
| : ptr(p), size(s), ref_count(1), is_freed(false) {} | ||
| }; | ||
|
|
||
| class MemoryPool { | ||
| public: | ||
| static MemoryPool& instance(); | ||
|
|
||
| // Register a memory allocation | ||
| void registerMemory(std::byte* ptr, size_t size); | ||
|
|
||
| // Increment reference count | ||
| void addRef(std::byte* ptr); | ||
|
|
||
| // Decrement reference count and potentially free memory | ||
| void releaseMemory(std::byte* ptr, std::function<void(std::byte*)> actual_deleter); | ||
|
|
||
| // Get reference count | ||
| int getRefCount(std::byte* ptr) const; | ||
|
|
||
| // Check if memory is registered | ||
| bool isRegistered(std::byte* ptr) const; | ||
|
|
||
| // Check if memory is already freed | ||
| bool isFreed(std::byte* ptr) const; | ||
|
|
||
| private: | ||
| MemoryPool() = default; | ||
| ~MemoryPool() = default; | ||
|
|
||
| mutable std::mutex mutex_; | ||
| std::unordered_map<std::byte*, std::shared_ptr<MemoryInfo>> memory_map_; | ||
| }; | ||
|
|
||
| } // namespace infinicore | ||
Empty file.
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
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.
include目录下都是会暴露给外部的接口,这个设计是要把memory pool的接口暴露给外部吗?什么情况下用户或者上层框架需要直接和memory pool交互?