our_knp/cats-dogs/build_fast_cpu.py
int 8f43927575 Initial import: KNP experiments (examples, viz, cats-dogs, reports)
examples/ is based on KasperskyLab/knp at 55470ed6 with local changes
to the mnist-learn altai/blifat models, cuda-backend CMake and model_loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 18:17:15 +03:00

62 lines
3 KiB
Python

"""Build an isolated CPU backend with fixed-topology STDP connection caching."""
from pathlib import Path
import shlex
import shutil
import subprocess
root = Path('/home/int/run/cats-dogs/fast-cpu')
root.mkdir(exist_ok=True)
source_root = Path('/mnt/c/KNP/examples/knp/backends/cpu')
shutil.copytree(source_root / 'cpu-library/include', root / 'include', dirs_exist_ok=True)
base = root / 'include/knp/backends/cpu-library/impl/populations'
stdp = base / 'training/stdp.h'
s = stdp.read_text()
start = s.rfind('template <class Synapse>', 0, s.index(' get_all_connected_synapses('))
assert start >= 0
end = s.index('\n}\n', start) + 3
s = s[:start] + s[end:]
s = s.replace('#include <vector>', '#include <vector>\n#include "perf_connection_cache.h"')
s = s.replace(' std::vector<std::reference_wrapper<knp::synapse_traits::synapse_parameters<',
' const std::vector<std::reference_wrapper<knp::synapse_traits::synapse_parameters<', 1)
s = s.replace('auto synapse_params = get_all_connected_synapses', 'const auto& synapse_params = get_all_connected_synapses')
stdp.write_text(s)
shutil.copyfile(Path(__file__).with_name('perf_connection_cache.h'), base / 'training/perf_connection_cache.h')
altai = base / 'altai/altai_stdp.h'
s = altai.read_text().replace('auto synapse_params =', 'const auto& synapse_params =')
s = s.replace('std::vector<std::reference_wrapper<SynapseParamType>> synapse_params =', 'const auto& synapse_params =')
# The old local alias is now unused under -Werror.
s = s.replace(' using SynapseParamType = knp::synapse_traits::synapse_parameters<SynapseType>;\n', '')
altai.write_text(s)
original = source_root / 'cpu-single-threaded-backend/impl/backend.cpp'
s = original.read_text()
marker = 'void SingleThreadedCPUBackend::_init()\n{'
assert marker in s
s = s.replace(marker, marker + '''
knp::backends::cpu::populations::impl::training::stdp::clear_connection_cache<
knp::synapse_traits::SynapticResourceSTDPDeltaSynapse>();
''')
(root / 'backend.cpp').write_text(s)
build = Path('/home/int/knp-build/knp/backends/cpu/cpu-single-threaded-backend')
folder = build / 'CMakeFiles/knp-cpu-single-threaded-backend.dir'
flags = {}
for line in (folder / 'flags.make').read_text().splitlines():
if ' = ' in line:
key, value = line.split(' = ', 1)
flags[key] = shlex.split(value)
obj = root / 'backend.o'
subprocess.run(['/usr/bin/c++', *flags['CXX_DEFINES'], '-I' + str(root / 'include'),
*flags['CXX_INCLUDES'], *flags['CXX_FLAGS'], '-c', str(root / 'backend.cpp'),
'-o', str(obj)], check=True)
link = shlex.split((folder / 'link.txt').read_text())
rebuilt = []
for item in link:
if item.startswith('-Wl,--dependency-file='):
continue
if item.endswith('/impl/backend.cpp.o'):
item = str(obj)
elif item.startswith('-Wl,-soname,'):
item = '-Wl,-soname,libknp-cpu-fast-backend.so'
rebuilt.append(item)
rebuilt[rebuilt.index('-o') + 1] = str(root / 'libknp-cpu-fast-backend.so')
subprocess.run(rebuilt, cwd=build, check=True)
print(root / 'libknp-cpu-fast-backend.so', flush=True)