Skip to content

Commit 26cf06d

Browse files
committed
additional remote stash
Signed-off-by: Ceng23333 <[email protected]>
1 parent a3e56c3 commit 26cf06d

8 files changed

+1804
-0
lines changed

build_fixed.sh

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
3+
# Fixed build script for InfiniCore
4+
# This script sets up the environment and builds with proper linker configuration
5+
6+
echo "Setting up InfiniCore build environment..."
7+
8+
# Initialize conda
9+
eval "$(conda shell.bash hook)"
10+
11+
# Activate the infinicore-env environment
12+
conda activate infinicore-env
13+
14+
# Set CUDA_HOME to the conda environment
15+
export CUDA_HOME=$CONDA_PREFIX
16+
17+
# Clean up conflicting environment variables
18+
unset CC
19+
unset CXX
20+
unset NVCC_PREPEND_FLAGS
21+
unset NVCC_APPEND_FLAGS
22+
unset CUDA_ROOT
23+
24+
# Use system tools
25+
export PATH="/usr/bin:$PATH"
26+
27+
# Create a wrapper for ld that converts -m64 to -m elf_x86_64
28+
mkdir -p /tmp/ld_wrapper
29+
cat > /tmp/ld_wrapper/ld << 'EOF'
30+
#!/bin/bash
31+
# Convert -m64 to -m elf_x86_64 for system linker compatibility
32+
args=()
33+
skip_next=false
34+
for arg in "$@"; do
35+
if [ "$skip_next" = true ]; then
36+
skip_next=false
37+
continue
38+
fi
39+
if [ "$arg" = "-m64" ]; then
40+
args+=("-m" "elf_x86_64")
41+
elif [ "$arg" = "-fopenmp" ]; then
42+
# Skip -fopenmp flag for linker, but add libgomp
43+
args+=("-lgomp")
44+
continue
45+
elif [ "$arg" = "-m" ]; then
46+
# Skip -m flag and its argument if it's elf_x86_64 (to avoid duplication)
47+
skip_next=true
48+
continue
49+
else
50+
args+=("$arg")
51+
fi
52+
done
53+
# Add standard C++ library and other required libraries
54+
args+=("-lstdc++" "-lm" "-lc" "-lgcc_s")
55+
exec /usr/bin/ld "${args[@]}"
56+
EOF
57+
chmod +x /tmp/ld_wrapper/ld
58+
export PATH="/tmp/ld_wrapper:$PATH"
59+
60+
echo "Environment setup complete!"
61+
echo "CUDA_HOME: $CUDA_HOME"
62+
echo "CONDA_PREFIX: $CONDA_PREFIX"
63+
64+
# Configure and build
65+
echo "Configuring xmake..."
66+
xmake f -c
67+
68+
echo "Building InfiniCore..."
69+
xmake build
70+
71+
echo "Build completed!"

example_memory_usage.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Example script showing how to use InfiniCore memory statistics
4+
to monitor memory usage during tensor operations.
5+
"""
6+
7+
import sys
8+
import os
9+
10+
# Add the current directory to Python path
11+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
12+
13+
try:
14+
import infinicore
15+
print("✓ Successfully imported infinicore")
16+
except ImportError as e:
17+
print(f"✗ Failed to import infinicore: {e}")
18+
print("Make sure to build the project first with: xmake build _infinicore")
19+
sys.exit(1)
20+
21+
def get_memory_summary():
22+
"""Get a summary of current memory usage."""
23+
try:
24+
device_stats = infinicore.get_device_memory_stats()
25+
return {
26+
'allocations': device_stats.allocation[0].current,
27+
'allocated_bytes': device_stats.allocated_bytes[0].current,
28+
'active_blocks': device_stats.active[0].current,
29+
'device_allocations': device_stats.num_device_alloc,
30+
'device_deallocations': device_stats.num_device_free
31+
}
32+
except Exception as e:
33+
print(f"Warning: Could not get memory stats: {e}")
34+
return None
35+
36+
def print_memory_summary(title, stats):
37+
"""Print a concise memory summary."""
38+
if stats is None:
39+
print(f"{title}: Unable to get memory statistics")
40+
return
41+
42+
print(f"{title}:")
43+
print(f" Allocations: {stats['allocations']}")
44+
print(f" Allocated bytes: {stats['allocated_bytes']:,} bytes ({stats['allocated_bytes'] / 1024 / 1024:.2f} MB)")
45+
print(f" Active blocks: {stats['active_blocks']}")
46+
print(f" Device alloc/dealloc: {stats['device_allocations']}/{stats['device_deallocations']}")
47+
48+
def monitor_memory_usage():
49+
"""Monitor memory usage during tensor operations."""
50+
print("=== InfiniCore Memory Usage Monitor ===\n")
51+
52+
# Initial memory state
53+
initial_stats = get_memory_summary()
54+
print_memory_summary("Initial Memory State", initial_stats)
55+
56+
try:
57+
# Create some tensors to demonstrate memory usage
58+
print("\n1. Creating tensors...")
59+
60+
# Create a large tensor
61+
print(" Creating 1000x1000 float32 tensor...")
62+
tensor1 = infinicore.empty((1000, 1000), dtype=infinicore.float32)
63+
stats_after_tensor1 = get_memory_summary()
64+
print_memory_summary("After creating tensor1", stats_after_tensor1)
65+
66+
# Create another tensor
67+
print("\n Creating 500x500 float32 tensor...")
68+
tensor2 = infinicore.empty((500, 500), dtype=infinicore.float32)
69+
stats_after_tensor2 = get_memory_summary()
70+
print_memory_summary("After creating tensor2", stats_after_tensor2)
71+
72+
# Create a third tensor
73+
print("\n Creating 2000x2000 float32 tensor...")
74+
tensor3 = infinicore.empty((2000, 2000), dtype=infinicore.float32)
75+
stats_after_tensor3 = get_memory_summary()
76+
print_memory_summary("After creating tensor3", stats_after_tensor3)
77+
78+
# Delete some tensors
79+
print("\n2. Deleting tensors...")
80+
del tensor1
81+
stats_after_del1 = get_memory_summary()
82+
print_memory_summary("After deleting tensor1", stats_after_del1)
83+
84+
del tensor2
85+
stats_after_del2 = get_memory_summary()
86+
print_memory_summary("After deleting tensor2", stats_after_del2)
87+
88+
# Final cleanup
89+
print("\n3. Final cleanup...")
90+
del tensor3
91+
final_stats = get_memory_summary()
92+
print_memory_summary("Final Memory State", final_stats)
93+
94+
# Show memory difference
95+
if initial_stats and final_stats:
96+
print(f"\nMemory Usage Summary:")
97+
print(f" Net allocations: {final_stats['allocations'] - initial_stats['allocations']}")
98+
print(f" Net allocated bytes: {final_stats['allocated_bytes'] - initial_stats['allocated_bytes']:,} bytes")
99+
print(f" Net active blocks: {final_stats['active_blocks'] - initial_stats['active_blocks']}")
100+
101+
print("\n✓ Memory monitoring completed successfully!")
102+
103+
except Exception as e:
104+
print(f"✗ Error during memory monitoring: {e}")
105+
import traceback
106+
traceback.print_exc()
107+
108+
def demonstrate_stat_types():
109+
"""Demonstrate different stat types and their usage."""
110+
print("\n=== Stat Types Demonstration ===\n")
111+
112+
try:
113+
# Get device stats
114+
device_stats = infinicore.get_device_memory_stats()
115+
116+
print("StatType.AGGREGATE statistics:")
117+
print(f" Allocation count: {device_stats.allocation[0].current}")
118+
print(f" Allocation peak: {device_stats.allocation[0].peak}")
119+
print(f" Allocation total: {device_stats.allocation[0].allocated}")
120+
print(f" Allocation freed: {device_stats.allocation[0].freed}")
121+
122+
print(f"\nStatType.SMALL_POOL statistics:")
123+
print(f" Allocation count: {device_stats.allocation[1].current}")
124+
print(f" Allocation peak: {device_stats.allocation[1].peak}")
125+
126+
print(f"\nStatType.LARGE_POOL statistics:")
127+
print(f" Allocation count: {device_stats.allocation[2].current}")
128+
print(f" Allocation peak: {device_stats.allocation[2].peak}")
129+
130+
print("\n✓ Stat types demonstration completed!")
131+
132+
except Exception as e:
133+
print(f"✗ Error during stat types demonstration: {e}")
134+
import traceback
135+
traceback.print_exc()
136+
137+
if __name__ == "__main__":
138+
monitor_memory_usage()
139+
demonstrate_stat_types()

0 commit comments

Comments
 (0)