|
| 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