{"slug": "int8-inference-needs-an-order-of-operations", "title": "INT8 inference needs an order of operations", "summary": "A developer added a new `05-ebpf-jev` module to `llm.asm` that specifies the exact order of operations for INT8 inference in eBPF-Jev: accumulate products and bias, interpret the result as signed, right-shift, then clamp. The module ships a Python reference and a shared C header, and worked examples show that shifting each product before accumulation or clipping the accumulator early produces different neurons (1 vs 0 and 127 vs 7). The developer notes that Python's floor-rounding right shift differs from C-style truncation on negatives, and that the C helper's unsigned 32-bit accumulation wraps at the boundary, so ports using unbounded integers can disagree at extreme values.", "body_md": "For the integer inference contract in eBPF-Jev, I read the arithmetic as a sequence: accumulate products and bias, interpret the signed result, shift, then clamp. Rearranging those operations changes the function. “It is all INT8” is not enough of a specification.\n\nThe new `05-ebpf-jev` addition to `llm.asm` makes that sequence visible in both a Python reference and a shared C header. Its hidden layer accumulates into a wider value, applies a right shift and a nonnegative clamp, then carries part of the hidden output into the next event. [1][2][3]\n\nI worked through tiny invented inputs to isolate the arithmetic. These are numerical illustrations, not pretrained-model outputs or a kernel benchmark.\n\nTake products `3` and `3`, zero bias and a right shift of `2`. Adding first gives `6 >> 2 = 1`. Shifting each product first gives `0 + 0 = 0`. The discarded remainders cannot combine after they have been discarded.\n\n*Original worksheet of the executed integer examples. Inputs are illustrative. Image by Shivam Dwivedi.*\n\nClipping early loses a different kind of information. With two products of `127 × 127`, the accumulator is `32258`. A shift of `4` gives `2016`, and the final hidden value saturates at `127`. If I clip the accumulator to `127` first and then shift, I get `7`.\n\nBoth wrong routes look like plausible attempts to keep intermediate values small. Both implement a different neuron.\n\nSave the following as `demo.py` and run `python3 demo.py`. `products` contains already-multiplied terms, so the example can focus on accumulation and rescaling. The shifts are chosen inputs.\n\n``` python\ndef signed32(value):\n    return (value + (1 << 31)) % (1 << 32) - (1 << 31)\ndef hidden(products, bias, shift):\n    acc = signed32(bias + sum(products))\n    return min(127, max(0, acc >> shift))\nsmall = [3, 3]\ncorrect = hidden(small, 0, 2)\nshift_each = min(127, max(0, sum(p >> 2 for p in small)))\nlarge = [127 * 127, 127 * 127]\nlate_clip = hidden(large, 0, 4)\nearly_clip = min(127, max(0, sum(large))) >> 4\nassert (correct, shift_each) == (1, 0)\nassert (late_clip, early_clip) == (127, 7)\nassert -3 >> 1 == -2 and int(-3 / 2) == -1\nassert hidden([1], (1 << 31) - 1, 0) == 0\nprint(\"sum then shift / shift each:\", correct, shift_each)\nprint(\"shift then clamp / clamp early:\", late_clip, early_clip)\nprint(\"negative shift / truncation:\", -3 >> 1, int(-3 / 2))\nprint(\"signed wrap:\", signed32((1 << 31) - 1 + 1))\nfor acc in range(-1024, 1025):\n    for shift in range(8):\n        assert acc >> shift == acc // (2 ** shift)\nprint(\"integer division cross-check passed\")\nsum then shift / shift each: 1 0\nshift then clamp / clamp early: 127 7\nnegative shift / truncation: -2 -1\nsigned wrap: -2147483648\ninteger division cross-check passed\n```\n\nThe negative example matters for the output layer. Python’s signed right shift rounds downward: `-3 >> 1` is `-2`. Converting `-3 / 2` to an integer truncates toward zero and gives `-1`. If the result selects a lookup-table entry, that difference changes the index. In the checked source, the clamped logit is offset by `128` before the sigmoid-table lookup. [1][2]\n\nThe loop independently compares shifts with integer floor division across the shown range. That checks this arithmetic identity; it does not check a trained network’s decisions.\n\nThe C helper accumulates through an unsigned 32-bit variable and then interprets the result as signed before shifting. Its comment explicitly makes wrapping part of the intended behavior, including extreme biases. [2] My `signed32` helper models that wrap with integer modulo arithmetic.\n\nAt the edge in the code, adding `1` to `2147483647` becomes `-2147483648`. The hidden activation consequently becomes zero. Python’s otherwise unbounded integers would not do that automatically. A cross-language port that quietly uses unbounded accumulation can therefore disagree at the boundary even when all its small examples pass.\n\nThat does not tell me whether a particular exported weight pack reaches this edge. It tells me what must be preserved—or explicitly ruled out by bounds—when testing a port.\n\nThe source’s actual hidden update, signed conversion, shift and table lookup belong in parity tests with the same weights, inputs and carried state. Agreement on these toy calculations is an early arithmetic check. It is not evidence of eBPF verifier acceptance, live latency, calibrated risk scores or protection against real attacks. I did not load its weights or run its kernel hooks.\n\nAlexey Fateev and contributors, [Python fixed-point reference](https://github.com/alesha-pro/llm.asm/blob/9389c88db493ead23452df29b2c9be21594065ce/05-ebpf-jev/ml/fixed_point.py).\n\nAlexey Fateev and contributors, [shared C inference helpers](https://github.com/alesha-pro/llm.asm/blob/9389c88db493ead23452df29b2c9be21594065ce/05-ebpf-jev/bpf/ebpf_jev_common.h).\n\nAlexey Fateev and contributors, [fixed-point architecture contract](https://github.com/alesha-pro/llm.asm/blob/9389c88db493ead23452df29b2c9be21594065ce/05-ebpf-jev/SPEC_CONTRACT.md).\n\nPython documentation, [integer bitwise operations](https://docs.python.org/3/library/stdtypes.html#bitwise-operations-on-integer-types).\n\nOriginal explanatory figure and example, CC BY 4.0. Credit: Shivam Dwivedi. No third-party image assets.\n\nAI was used to draft the article.", "url": "https://wpnews.pro/news/int8-inference-needs-an-order-of-operations", "canonical_source": "https://dev.to/panditshivam/int8-inference-needs-an-order-of-operations-4bcc", "published_at": "2026-11-29 12:33:45+00:00", "updated_at": "2026-11-29 12:43:46.786200+00:00", "lang": "en", "topics": ["artificial-intelligence", "machine-learning", "ai-infrastructure", "ai-tools"], "entities": ["eBPF-Jev", "llm.asm", "Shivam Dwivedi", "Python", "C"], "also_reported_by": [], "takeaway": "Specify INT8 inference as accumulate-then-shift-then-clamp: shifting each product first turned 3+3>>2 into 0 instead of 1, and clipping the accumulator before the shift gave 7 instead of the saturated 127.", "recommendation": {"advice": "Implement INT8 inference as signed accumulate, then right shift, then clamp, and cross-check Python's floor shift against C truncation and 32-bit wrap at boundary values.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/int8-inference-needs-an-order-of-operations", "markdown": "https://wpnews.pro/news/int8-inference-needs-an-order-of-operations.md", "text": "https://wpnews.pro/news/int8-inference-needs-an-order-of-operations.txt", "jsonld": "https://wpnews.pro/news/int8-inference-needs-an-order-of-operations.jsonld"}}