-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path15-model-pool.php
More file actions
66 lines (46 loc) · 1.98 KB
/
Copy path15-model-pool.php
File metadata and controls
66 lines (46 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use FerryAI\CpuBackend\CpuNativeModel;
use FerryAI\ModelPool;
use FerryAI\SharedMemoryManager;
echo "=== 15 — Model Pool & Shared Memory ===\n\n";
echo "--- ModelPool ---\n\n";
$pool = new ModelPool(maxMemoryBytes: 10_000_000);
$m1 = new CpuNativeModel('model-a', ['type' => 'classifier']);
$m2 = new CpuNativeModel('model-b', ['type' => 'regressor']);
$pool->put('classifier', $m1, memoryBytes: 5_000_000);
$pool->put('regressor', $m2, memoryBytes: 3_000_000);
printf("pool size: %d\n", $pool->size());
printf("memory usage: %d bytes (%.1f MB)\n", $pool->memoryUsage(), $pool->memoryUsage() / 1_000_000);
$acquired = $pool->acquire('classifier');
printf("acquired: %s\n", $acquired !== null ? 'YES' : 'NO');
$pool->release('classifier');
printf("after release: size=%d\n\n", $pool->size());
$pool->evict('classifier');
printf("after evict: size=%d\n", $pool->size());
$pool->warmup(
['classifier', 'regressor', 'unknown'],
static fn(string $id): CpuNativeModel => new CpuNativeModel($id, []),
);
printf("after warmup: size=%d\n", $pool->size());
echo "\n--- SharedMemoryManager ---\n\n";
$shm = new SharedMemoryManager();
printf("shmop loaded: %s\n", extension_loaded('shmop') ? 'YES' : 'NO');
printf("isAvailable: %s\n\n", $shm->isAvailable() ? 'YES' : 'NO');
if ($shm->isAvailable()) {
$testFile = sys_get_temp_dir() . '/ferry-shm-test.bin';
file_put_contents($testFile, 'FerryAI shared memory test data');
try {
$key = $shm->allocateModel('test-model', $testFile);
printf("allocate: key=%d\n", $key);
printf("isShared: %s\n", $shm->isShared('test-model') ? 'YES' : 'NO');
$shm->detachModel('test-model');
printf("after detach: isShared=%s\n", $shm->isShared('test-model') ? 'YES' : 'NO');
} catch (\Throwable $e) {
printf("ERROR: %s\n", $e->getMessage());
}
unlink($testFile);
}
echo "\n=== OK ===\n";