You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide helps migrate from the legacy Python bindings to the new high-performance bindings that achieve 95%+ C++ performance through NumPy integration and optimized memory layout.
# OLD - Lists handle any shape# NEW - NumPy requires exact shapes# Solution: Always check array shapesassertfeatures.shape[1] ==network.get_input_size()
assertlabels.shape[1] ==network.get_output_size()
Memory Issues
# OLD - Python lists use more memory# NEW - NumPy uses C-contiguous arrays# Solution: Use float32 instead of float64features=features.astype(np.float32)
Thread Safety
# Enable thread safety for concurrent operationsnetwork.enable_thread_safety(True)
network.is_thread_safe() # Check status
Backward Compatibility Layer
Migration Helper Functions
classMigrationHelper:
@staticmethoddefconvert_legacy_to_performance(legacy_network):
"""Convert legacy network to performance network"""architecture= [layer.get_input_size() forlayerinlegacy_network.layers]
architecture.append(legacy_network.layers[-1].get_output_size())
fromkortexdl_performanceimportPerformanceNetworkperf_network=PerformanceNetwork(architecture)
# Copy weights (implementation depends on access methods)# perf_network.copy_weights_from(legacy_network)returnperf_network@staticmethoddefmigrate_data_format(data):
"""Convert list data to NumPy arrays"""ifisinstance(data, list):
returnnp.array(data, dtype=np.float32)
returndata@staticmethoddefbenchmark_migration(old_network, new_network, test_data):
"""Compare performance between old and new implementations"""importtime# Test legacystart=time.time()
legacy_results= [old_network.forward(sample) forsampleintest_data]
legacy_time=time.time() -start# Test performancetest_array=np.array(test_data, dtype=np.float32)
start=time.time()
performance_results=forward_numpy(new_network, test_array)
performance_time=time.time() -startreturn {
'legacy_time': legacy_time,
'performance_time': performance_time,
'speedup': legacy_time/performance_time,
'legacy_throughput': len(test_data) /legacy_time,
'performance_throughput': len(test_data) /performance_time
}
# Use sed to automate basic replacements
sed -i 's/kortexdl_py/kortexdl_performance/g'*.py
sed -i 's/\.forward(\[/forward_numpy(network, np.array([/g'*.py
sed -i 's/\.train_batch(\[/train_numpy(network, np.array([/g'*.py
2. Gradual Migration Approach
# Phase 1: Use both APIs side-by-sidefromkortexdl_pyimportNetworkasLegacyNetworkfromkortexdl_performanceimportPerformanceNetwork# Phase 2: Migrate critical pathsdefhybrid_approach(data):
ifisinstance(data, list):
returnlegacy_network.forward(data)
else:
returnforward_numpy(performance_network, data)
# Phase 3: Full migration# Remove legacy code
Support and Troubleshooting
Performance Issues
Check memory usage: Use get_memory_info()
Optimize batch size: Use calculate_optimal_batch_size()
Enable thread safety: Set network.enable_thread_safety(True)
Getting Help
Performance issues: Run the benchmark suite
Migration questions: Use the migration helper
Compatibility: Check the backward compatibility layer