Instructions to use NoesisLab/LITOperator with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LeRobot
How to use NoesisLab/LITOperator with LeRobot:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Download lit_operator/lit_core.c from NoesisLab/LITOperator: direct link, hf CLI and curl.
- Browser
- Download file 18.9 kB
-
https://huggingface.co/NoesisLab/LITOperator/resolve/main/lit_operator/lit_core.c
- Command line
-
hf download hf://NoesisLab/LITOperator/lit_operator/lit_core.c
-
curl -L -o lit_core.c https://huggingface.co/NoesisLab/LITOperator/resolve/main/lit_operator/lit_core.c
18.9 kB
| /* | |
| * lit_core.c — LITCORE: the LIT algebraic core, language-agnostic C. | |
| * | |
| * The core is brainless and hard-halting. It broadcasts the graph, | |
| * computes, reduces by strict-less Hamming, rewires, and repeats; it | |
| * never interprets what a symbol or a program means. There is no | |
| * semantics inside the core. | |
| * | |
| * One tick is three steps, in this order: | |
| * | |
| * 1. transition: next[i] = V[src[i]] * gain[i] | |
| * 2. swap: prev = V (the snapshot), V = next | |
| * 3. reduction: M[i] = prev[i] & ~V[i] | |
| * d += weight[i] * popcount(M[i]) | |
| * | |
| * The machine holds two arrays, V and prev, and swaps their roles; there | |
| * is no third snapshot array. The reduction is the strict-less Hamming: | |
| * it counts only the bits that turned OFF, prev=1 -> V=0. The | |
| * integer-lattice iteration only ever goes strictly down (there is no | |
| * <=, hence no self-connection loop); a self-edge or an increase | |
| * contributes d=0, which is the natural halt. The mask M is retained in | |
| * diff, per slot: d says how much changed, diff says where. The core | |
| * never interprets the mask; it is an observation for the caller. | |
| * | |
| * The graph (src/gain/weight) lives in the same machine as V. Writing | |
| * src/gain is the reconnect; it takes effect on the next tick and does | |
| * not itself trigger a commit. The prev snapshot is what keeps | |
| * "rewire" and "read" from racing inside one tick. | |
| * | |
| * A machine owns `clusters` independent copies of the n-slot table. The | |
| * same transition is applied to every cluster: that is the broadcast. | |
| * Clusters are independent; the reduction is exact integer weighted | |
| * Hamming per cluster. | |
| * | |
| * This file has no CPython dependency. The `lit` dispatcher and the | |
| * `litc` CLI link this core; `litc --python` is the C ingest for raw | |
| * Python code units. There is no Python interface. | |
| */ | |
| static inline uint64_t lit_popcount_u64(uint64_t x) | |
| { | |
| return (uint64_t)__builtin_popcountll(x); | |
| } | |
| /* | |
| * VF map composition: the map is arrays, not a machine stack. | |
| * | |
| * F(V)_i = V[s_i] * g_i (self) | |
| * G(V)_i = V[r_i] * h_i (other) | |
| * (G o F)(V)_i = V[s_{r_i}] * (g_{r_i} * h_i) | |
| * | |
| * so the composed wiring is src'[i] = s[r_i], gain'[i] = g[r_i] * h[i]. | |
| * The arrays do not grow. | |
| */ | |
| static void compose_flat(const uint32_t *sf, const uint64_t *gf, | |
| const uint32_t *sg, const uint64_t *gg, | |
| uint32_t *so, uint64_t *go, | |
| size_t count, uint32_t n) | |
| { | |
| for (size_t i = 0; i < count; i++) { | |
| size_t base = (i / n) * (size_t)n; | |
| size_t j = base + sf[i]; /* first map's source, same cluster */ | |
| so[i] = sg[j]; /* second map reads it */ | |
| go[i] = gg[j] * gf[i]; | |
| } | |
| } | |
| static void machine_free_arrays(lit_machine *m) | |
| { | |
| free(m->a); | |
| free(m->b); | |
| free(m->src); | |
| free(m->gain); | |
| free(m->weight); | |
| free(m->dist); | |
| free(m->diff); | |
| free(m->flat_src); | |
| free(m->flat_gain); | |
| free(m->flat_as); | |
| free(m->flat_bs); | |
| free(m->flat_ag); | |
| free(m->flat_bg); | |
| m->a = NULL; | |
| m->b = NULL; | |
| m->V = NULL; | |
| m->prev = NULL; | |
| m->src = NULL; | |
| m->gain = NULL; | |
| m->weight = NULL; | |
| m->dist = NULL; | |
| m->diff = NULL; | |
| m->flat_src = NULL; | |
| m->flat_gain = NULL; | |
| m->flat_as = NULL; | |
| m->flat_bs = NULL; | |
| m->flat_ag = NULL; | |
| m->flat_bg = NULL; | |
| } | |
| void lit_machine_free(lit_machine *m) | |
| { | |
| if (!m) { | |
| return; | |
| } | |
| machine_free_arrays(m); | |
| memset(m, 0, sizeof(*m)); | |
| } | |
| static int machine_alloc(lit_machine *m, uint64_t *state, int own_state) | |
| { | |
| size_t count; | |
| size_t n = (size_t)m->n; | |
| uint32_t lv = m->levels ? m->levels : 1; | |
| if (n == 0 || m->k == 0) { | |
| return -1; | |
| } | |
| if ((size_t)m->k > SIZE_MAX / n) { | |
| return -1; | |
| } | |
| count = (size_t)m->k * n; | |
| if (count > SIZE_MAX / lv) { | |
| return -1; | |
| } | |
| m->owns_state = own_state; | |
| m->a = own_state ? malloc(count * sizeof(uint64_t)) : NULL; | |
| m->b = malloc(count * sizeof(uint64_t)); | |
| m->src = malloc(count * sizeof(uint32_t)); | |
| m->gain = malloc(count * sizeof(uint64_t)); | |
| m->weight = malloc(count * sizeof(uint64_t)); | |
| m->dist = malloc((size_t)m->k * sizeof(uint64_t)); | |
| m->diff = malloc(count * sizeof(uint64_t)); | |
| m->flat_src = malloc(count * lv * sizeof(uint32_t)); | |
| m->flat_gain = malloc(count * lv * sizeof(uint64_t)); | |
| m->flat_as = malloc(n * sizeof(uint32_t)); | |
| m->flat_bs = malloc(n * sizeof(uint32_t)); | |
| m->flat_ag = malloc(n * sizeof(uint64_t)); | |
| m->flat_bg = malloc(n * sizeof(uint64_t)); | |
| if ((own_state && !m->a) || !m->b || !m->src || !m->gain || | |
| !m->weight || !m->dist || !m->diff || !m->flat_src || | |
| !m->flat_gain || !m->flat_as || !m->flat_bs || !m->flat_ag || | |
| !m->flat_bg) { | |
| machine_free_arrays(m); | |
| return -1; | |
| } | |
| m->V = own_state ? m->a : state; | |
| m->prev = m->b; | |
| m->flat_dirty = 1; | |
| memset(m->b, 0, count * sizeof(uint64_t)); | |
| memset(m->diff, 0, count * sizeof(uint64_t)); | |
| return 0; | |
| } | |
| static int machine_setup(lit_machine *m, uint64_t *state, | |
| uint32_t n, uint32_t k, uint32_t bits, int own) | |
| { | |
| size_t count; | |
| if (!m || !state || n == 0 || k == 0 || bits < 1 || bits > 64) { | |
| return -1; | |
| } | |
| memset(m, 0, sizeof(*m)); | |
| m->n = n; | |
| m->k = k; | |
| m->bits = bits; | |
| m->mask = (bits == 64) ? ~(uint64_t)0 : (((uint64_t)1 << bits) - 1); | |
| m->levels = 0; | |
| while (m->levels < 31 && ((uint32_t)1 << m->levels) < n) { | |
| m->levels++; | |
| } | |
| m->halt = n + 1; | |
| if (machine_alloc(m, state, own) != 0) { | |
| return -1; | |
| } | |
| count = (size_t)k * n; | |
| if (own) { | |
| memcpy(m->a, state, count * sizeof(uint64_t)); | |
| } | |
| for (size_t i = 0; i < count; i++) { | |
| m->V[i] &= m->mask; | |
| } | |
| for (uint32_t c = 0; c < k; c++) { | |
| for (uint32_t i = 0; i < n; i++) { | |
| size_t idx = (size_t)c * n + i; | |
| m->src[idx] = i; | |
| m->gain[idx] = 1; | |
| m->weight[idx] = 1; | |
| } | |
| } | |
| memset(m->dist, 0, (size_t)k * sizeof(uint64_t)); | |
| return 0; | |
| } | |
| int lit_machine_init(lit_machine *m, const uint64_t *state, | |
| uint32_t n, uint32_t k, uint32_t bits) | |
| { | |
| return machine_setup(m, (uint64_t *)state, n, k, bits, 1); | |
| } | |
| int lit_machine_init_borrowed(lit_machine *m, uint64_t *state, | |
| uint32_t n, uint32_t k, uint32_t bits) | |
| { | |
| return machine_setup(m, state, n, k, bits, 0); | |
| } | |
| /* | |
| * The six-item graph sequence: the base VF graph F plus the local | |
| * recurrence G_{i+1} = G_i o G_i gives F^2, F^4, F^8, F^16, F^32, | |
| * F^64. It is built once per wiring change; the step applies the last | |
| * item, so one tick is F^64 and 2^6 = 64 is the 64/65 edge. | |
| */ | |
| static void machine_rebuild_flat(lit_machine *m) | |
| { | |
| const uint32_t k = m->k; | |
| const uint32_t n = m->n; | |
| uint32_t *as = m->flat_as; | |
| uint32_t *bs = m->flat_bs; | |
| uint64_t *ag = m->flat_ag; | |
| uint64_t *bg = m->flat_bg; | |
| for (uint32_t cc = 0; cc < k; cc++) { | |
| memcpy(as, m->src + (size_t)cc * n, (size_t)n * sizeof(uint32_t)); | |
| memcpy(ag, m->gain + (size_t)cc * n, (size_t)n * sizeof(uint64_t)); | |
| for (uint32_t c = 0; c < m->levels; c++) { | |
| compose_flat(as, ag, as, ag, bs, bg, n, n); | |
| { uint32_t *p = as; as = bs; bs = p; } | |
| { uint64_t *p = ag; ag = bg; bg = p; } | |
| memcpy(m->flat_src + ((size_t)cc * m->levels + c) * n, as, | |
| (size_t)n * sizeof(uint32_t)); | |
| memcpy(m->flat_gain + ((size_t)cc * m->levels + c) * n, ag, | |
| (size_t)n * sizeof(uint64_t)); | |
| } | |
| } | |
| m->flat_dirty = 0; | |
| } | |
| /* | |
| * VF_TRANSFER: next = F(V), then swap: V becomes the committed state and | |
| * prev keeps the snapshot. Two arrays total (V and prev), not three. | |
| * Each cluster only touches its own slice, so the cluster loop is the | |
| * broadcast parallel loop. | |
| */ | |
| static void machine_transfer(lit_machine *m) | |
| { | |
| const uint32_t k = m->k; | |
| const uint32_t n = m->n; | |
| if (m->flat_dirty) { | |
| machine_rebuild_flat(m); | |
| } | |
| for (long cc = 0; cc < (long)k; cc++) { | |
| size_t base = (size_t)cc * n; | |
| const uint64_t *V = m->V + base; | |
| uint64_t *next = m->prev + base; | |
| const uint32_t *src; | |
| const uint64_t *gain; | |
| if (m->levels == 0) { | |
| src = m->src + base; | |
| gain = m->gain + base; | |
| } else { | |
| src = m->flat_src + | |
| ((size_t)cc * m->levels + (m->levels - 1)) * n; | |
| gain = m->flat_gain + | |
| ((size_t)cc * m->levels + (m->levels - 1)) * n; | |
| } | |
| for (uint32_t i = 0; i < n; i++) { | |
| next[i] = (V[src[i]] * gain[i]) & m->mask; | |
| } | |
| } | |
| { | |
| uint64_t *tmp = m->V; | |
| m->V = m->prev; | |
| m->prev = tmp; | |
| } | |
| m->pending = 1; | |
| } | |
| /* | |
| * HAMMING_REDUCE: strict-less weighted Hamming, exact: the bits of the | |
| * snapshot that turned off in the committed state (prev & ~V), never the | |
| * bits that turned on. The per-slot mask is kept in diff; the weighted | |
| * sum is dist. Without a pending transfer every distance and mask is 0. | |
| */ | |
| static void machine_reduce(lit_machine *m) | |
| { | |
| const uint32_t k = m->k; | |
| const uint32_t n = m->n; | |
| if (!m->pending) { | |
| memset(m->dist, 0, (size_t)k * sizeof(uint64_t)); | |
| memset(m->diff, 0, (size_t)k * n * sizeof(uint64_t)); | |
| return; | |
| } | |
| for (long cc = 0; cc < (long)k; cc++) { | |
| size_t base = (size_t)cc * n; | |
| const uint64_t *V = m->V + base; | |
| const uint64_t *prev = m->prev + base; | |
| const uint64_t *weight = m->weight + base; | |
| uint64_t *diff = m->diff + base; | |
| uint64_t d = 0; | |
| for (uint32_t i = 0; i < n; i++) { | |
| uint64_t mask = (prev[i] & ~V[i]) & m->mask; | |
| diff[i] = mask; | |
| d += weight[i] * lit_popcount_u64(mask); | |
| } | |
| m->dist[cc] = d; | |
| } | |
| m->pending = 0; | |
| } | |
| void lit_machine_step(lit_machine *m) | |
| { | |
| machine_transfer(m); | |
| machine_reduce(m); | |
| } | |
| void lit_machine_run(lit_machine *m, uint64_t max_cycles, | |
| uint64_t *cycles_out, int *converged_out) | |
| { | |
| uint64_t cycles = 0; | |
| int converged = 0; | |
| if (max_cycles == 0) { | |
| max_cycles = 1; /* one tick is F^64: one step, early halt */ | |
| } | |
| if (max_cycles > m->halt) { | |
| max_cycles = m->halt; /* the hard halt is n + 1 (65 at n=64) */ | |
| } | |
| for (;;) { | |
| lit_machine_step(m); | |
| cycles++; | |
| converged = 1; | |
| for (uint32_t c = 0; c < m->k; c++) { | |
| if (m->dist[c] != 0) { | |
| converged = 0; | |
| break; | |
| } | |
| } | |
| if (converged || cycles >= max_cycles) { | |
| break; | |
| } | |
| } | |
| if (cycles_out) { | |
| *cycles_out = cycles; | |
| } | |
| if (converged_out) { | |
| *converged_out = converged; | |
| } | |
| } | |
| int lit_machine_connect(lit_machine *m, uint32_t slot, uint32_t src, | |
| uint64_t gain, const uint64_t *weight, int cluster) | |
| { | |
| uint32_t c0, c1; | |
| if (!m) { | |
| return -1; | |
| } | |
| if (slot >= m->n || src >= m->n) { | |
| return -1; | |
| } | |
| if (cluster < -1 || cluster >= (int)m->k) { | |
| return -2; | |
| } | |
| c0 = (cluster < 0) ? 0 : (uint32_t)cluster; | |
| c1 = (cluster < 0) ? m->k : (uint32_t)cluster + 1; | |
| for (uint32_t c = c0; c < c1; c++) { | |
| size_t idx = (size_t)c * m->n + slot; | |
| m->src[idx] = src; | |
| m->gain[idx] = gain; | |
| if (weight) { | |
| m->weight[idx] = *weight; | |
| } | |
| } | |
| m->flat_dirty = 1; | |
| return 0; | |
| } | |
| int lit_machine_graph(lit_machine *m, const uint32_t *src, | |
| const uint64_t *gain, const uint64_t *weight, | |
| int cluster) | |
| { | |
| uint32_t c0, c1; | |
| if (!m || !src || !gain || !weight) { | |
| return -1; | |
| } | |
| if (cluster < -1 || cluster >= (int)m->k) { | |
| return -2; | |
| } | |
| for (uint32_t i = 0; i < m->n; i++) { | |
| if (src[i] >= m->n) { | |
| return -1; | |
| } | |
| } | |
| c0 = (cluster < 0) ? 0 : (uint32_t)cluster; | |
| c1 = (cluster < 0) ? m->k : (uint32_t)cluster + 1; | |
| for (uint32_t c = c0; c < c1; c++) { | |
| size_t base = (size_t)c * m->n; | |
| memcpy(m->src + base, src, (size_t)m->n * sizeof(uint32_t)); | |
| memcpy(m->gain + base, gain, (size_t)m->n * sizeof(uint64_t)); | |
| memcpy(m->weight + base, weight, (size_t)m->n * sizeof(uint64_t)); | |
| } | |
| m->flat_dirty = 1; | |
| return 0; | |
| } | |
| void lit_machine_load(lit_machine *m, const uint64_t *words) | |
| { | |
| if (!m || !words) { | |
| return; | |
| } | |
| for (uint32_t c = 0; c < m->k; c++) { | |
| memcpy(m->V + (size_t)c * m->n, words, | |
| (size_t)m->n * sizeof(uint64_t)); | |
| } | |
| m->pending = 0; | |
| } | |
| void lit_machine_ring(lit_machine *m) | |
| { | |
| const uint32_t n = m->n; | |
| if (!m) { | |
| return; | |
| } | |
| for (uint32_t c = 0; c < m->k; c++) { | |
| size_t base = (size_t)c * n; | |
| for (uint32_t i = 0; i < n; i++) { | |
| m->src[base + i] = (i == 0) ? n - 1 : i - 1; | |
| m->gain[base + i] = 1; | |
| m->weight[base + i] = 1; | |
| } | |
| } | |
| m->flat_dirty = 1; | |
| } | |
| void lit_machine_copy_v(const lit_machine *m, uint64_t *out) | |
| { | |
| if (!m || !out) { | |
| return; | |
| } | |
| memcpy(out, m->V, (size_t)m->n * sizeof(uint64_t)); /* cluster 0 */ | |
| } | |
| static inline uint32_t bc_u32(const uint8_t *p) | |
| { | |
| return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | | |
| ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); | |
| } | |
| static inline uint64_t bc_u64(const uint8_t *p) | |
| { | |
| uint64_t v = 0; | |
| for (int i = 7; i >= 0; i--) { | |
| v = (v << 8) | p[i]; | |
| } | |
| return v; | |
| } | |
| /* | |
| * Validate the stream and count reductions before running anything. | |
| * Returns 1 if HALT was seen, 0 if the stream ended, -1 truncated, | |
| * -2 unknown opcode, -3 slot/src out of range. | |
| */ | |
| static int bc_scan(const uint8_t *code, size_t len, uint32_t n, | |
| size_t *reductions) | |
| { | |
| size_t pc = 0, red = 0; | |
| while (pc < len) { | |
| uint8_t op = code[pc++]; | |
| switch (op) { | |
| case LIT_BC_LOAD: | |
| if (len - pc < 12) { | |
| return -1; | |
| } | |
| if (bc_u32(code + pc) >= n) { | |
| return -3; | |
| } | |
| pc += 12; | |
| break; | |
| case LIT_BC_WIRE: | |
| if (len - pc < 24) { | |
| return -1; | |
| } | |
| if (bc_u32(code + pc) >= n || bc_u32(code + pc + 4) >= n) { | |
| return -3; | |
| } | |
| pc += 24; | |
| break; | |
| case LIT_BC_BROADCAST: | |
| case LIT_BC_VF_TRANSFER: | |
| break; | |
| case LIT_BC_HAMMING_REDUCE: | |
| red++; | |
| break; | |
| case LIT_BC_HALT: | |
| *reductions = red; | |
| return 1; | |
| default: | |
| return -2; | |
| } | |
| } | |
| *reductions = red; | |
| return 0; | |
| } | |
| int lit_machine_run_bytecode(lit_machine *m, const uint8_t *code, size_t len, | |
| uint64_t **history, size_t *reductions, | |
| uint64_t *ticks_out, int *halted, | |
| int *broadcast, char *err, size_t errlen) | |
| { | |
| size_t nred = 0, red_i = 0, hist_count; | |
| int scan; | |
| uint64_t ticks = 0, *hist = NULL; | |
| size_t pc = 0; | |
| int was_broadcast = 0; | |
| if (!m || !code) { | |
| if (err && errlen) { | |
| snprintf(err, errlen, "null machine or code"); | |
| } | |
| return -1; | |
| } | |
| scan = bc_scan(code, len, m->n, &nred); | |
| if (scan < 0) { | |
| if (err && errlen) { | |
| if (scan == -1) { | |
| snprintf(err, errlen, "truncated LIT bytecode"); | |
| } else if (scan == -3) { | |
| snprintf(err, errlen, "LIT bytecode slot/src out of range"); | |
| } else { | |
| snprintf(err, errlen, "unknown LIT opcode"); | |
| } | |
| } | |
| return -1; | |
| } | |
| hist_count = nred * (size_t)m->k; | |
| if (hist_count) { | |
| hist = malloc(hist_count * sizeof(uint64_t)); | |
| if (!hist) { | |
| if (err && errlen) { | |
| snprintf(err, errlen, "out of memory"); | |
| } | |
| return -1; | |
| } | |
| } | |
| while (pc < len) { | |
| uint8_t op = code[pc++]; | |
| switch (op) { | |
| case LIT_BC_LOAD: { | |
| uint32_t slot = bc_u32(code + pc); | |
| uint64_t value = bc_u64(code + pc + 4); | |
| pc += 12; | |
| for (uint32_t c = 0; c < m->k; c++) { | |
| m->V[(size_t)c * m->n + slot] = value & m->mask; | |
| } | |
| break; | |
| } | |
| case LIT_BC_WIRE: { | |
| uint32_t slot = bc_u32(code + pc); | |
| uint32_t src = bc_u32(code + pc + 4); | |
| uint64_t gain = bc_u64(code + pc + 8); | |
| uint64_t weight = bc_u64(code + pc + 16); | |
| pc += 24; | |
| for (uint32_t c = 0; c < m->k; c++) { | |
| size_t idx = (size_t)c * m->n + slot; | |
| m->src[idx] = src; | |
| m->gain[idx] = gain; | |
| m->weight[idx] = weight; | |
| } | |
| m->flat_dirty = 1; | |
| break; | |
| } | |
| case LIT_BC_BROADCAST: | |
| was_broadcast = 1; | |
| break; | |
| case LIT_BC_VF_TRANSFER: | |
| machine_transfer(m); | |
| ticks++; | |
| break; | |
| case LIT_BC_HAMMING_REDUCE: | |
| machine_reduce(m); | |
| if (hist) { | |
| for (uint32_t c = 0; c < m->k; c++) { | |
| hist[red_i * m->k + c] = m->dist[c]; | |
| } | |
| } | |
| red_i++; | |
| break; | |
| case LIT_BC_HALT: | |
| pc = len; | |
| break; | |
| } | |
| } | |
| *history = hist; | |
| if (reductions) { | |
| *reductions = nred; | |
| } | |
| if (ticks_out) { | |
| *ticks_out = ticks; | |
| } | |
| if (halted) { | |
| *halted = (scan == 1); | |
| } | |
| if (broadcast) { | |
| *broadcast = was_broadcast; | |
| } | |
| return 0; | |
| } | |
| uint64_t lit_ring_tick(uint64_t *v, size_t n, uint32_t bits, int wrap) | |
| { | |
| uint64_t mask, last, d = 0; | |
| if (!v || n == 0) { | |
| return 0; | |
| } | |
| if (bits < 1) { | |
| bits = 1; | |
| } | |
| if (bits > 64) { | |
| bits = 64; | |
| } | |
| mask = (bits == 64) ? ~(uint64_t)0 : (((uint64_t)1 << bits) - 1); | |
| last = v[n - 1]; | |
| for (size_t i = n - 1; i > 0; i--) { | |
| uint64_t old = v[i]; | |
| uint64_t nx = v[i - 1]; /* the source is still old */ | |
| d += lit_popcount_u64(old & ~nx & mask); | |
| v[i] = nx; | |
| } | |
| { | |
| uint64_t old = v[0]; | |
| uint64_t nx = wrap ? last : old; | |
| d += lit_popcount_u64(old & ~nx & mask); | |
| v[0] = nx; | |
| } | |
| return d; | |
| } | |