libs/vulkan-compute/lua/test_ffi.lua
1#!/usr/bin/env luajit
2-- test_ffi.lua - Test Lua FFI bindings for Vulkan compute
3--
4-- This script verifies that the Lua FFI bindings work correctly
5-- by computing a small diversity sequence.
6--
7-- Usage: luajit test_ffi.lua [DIR]
8-- DIR - Path to vulkan-compute root directory (default: current directory)
9
10-- {{{ Parse arguments and set up paths
11local DIR = arg[1] or "."
12package.path = DIR .. "/lua/?.lua;" .. package.path
13_G.VK_COMPUTE_LIB = DIR .. "/build/libvkcompute.so"
14-- }}}
15
16local vk = require("vk_compute")
17
18print("=== Vulkan Compute Lua FFI Test ===\n")
19
20-- {{{ Test configuration
21local NUM_POEMS = 100
22local EMBEDDING_DIM = 768
23local START_POEM = 0
24-- }}}
25
26-- {{{ Generate test embeddings
27print("[1] Generating test embeddings...")
28local embeddings = {}
29math.randomseed(42)
30
31for i = 1, NUM_POEMS do
32 for d = 1, EMBEDDING_DIM do
33 local base = (i - 1) / NUM_POEMS
34 local variation = (math.random() - 0.5) * 0.1
35 table.insert(embeddings, base + variation)
36 end
37end
38
39print(string.format(" Generated %d embeddings (%d dims each)",
40 NUM_POEMS, EMBEDDING_DIM))
41-- }}}
42
43-- {{{ Initialize Vulkan
44print("\n[2] Initializing Vulkan...")
45local ctx = vk.init(false)
46print(" Vulkan initialized successfully")
47-- }}}
48
49-- {{{ Compute diversity sequence
50print(string.format("\n[3] Computing diversity sequence (start: %d)...", START_POEM))
51local start_time = os.clock()
52
53local sequence = vk.compute_diversity_sequence(
54 ctx, embeddings, NUM_POEMS, EMBEDDING_DIM, START_POEM
55)
56
57local elapsed = os.clock() - start_time
58print(string.format(" Computed sequence in %.3fs", elapsed))
59-- }}}
60
61-- {{{ Verify sequence
62print("\n[4] Verifying sequence...")
63local valid = true
64
65-- Check sequence length
66if #sequence ~= NUM_POEMS then
67 print(string.format(" ERROR: Expected %d elements, got %d", NUM_POEMS, #sequence))
68 valid = false
69end
70
71-- Check that it starts with start_poem
72if sequence[1] ~= START_POEM then
73 print(string.format(" ERROR: Expected start poem %d, got %d", START_POEM, sequence[1]))
74 valid = false
75end
76
77-- Check that all poems appear exactly once
78local seen = {}
79for i = 1, NUM_POEMS do
80 local poem_id = sequence[i]
81 if seen[poem_id] then
82 print(string.format(" ERROR: Duplicate poem ID: %d", poem_id))
83 valid = false
84 break
85 end
86 if poem_id < 0 or poem_id >= NUM_POEMS then
87 print(string.format(" ERROR: Invalid poem ID: %d", poem_id))
88 valid = false
89 break
90 end
91 seen[poem_id] = true
92end
93
94if valid then
95 print(" [OK] Sequence is valid")
96 print("\n First 10 poems in sequence:")
97 for i = 1, math.min(10, NUM_POEMS) do
98 print(string.format(" [%d] → poem %d", i - 1, sequence[i]))
99 end
100else
101 print(" [FAILED] Sequence validation failed")
102end
103-- }}}
104
105-- {{{ Cleanup
106print("\n[5] Cleaning up...")
107vk.shutdown(ctx)
108print(" Vulkan shutdown complete")
109-- }}}
110
111-- {{{ Final result
112if valid then
113 print("\n[SUCCESS] FFI bindings test passed!")
114 os.exit(0)
115else
116 print("\n[FAILED] Test failed")
117 os.exit(1)
118end
119-- }}}
120