Skip to content

Commit d386b8d

Browse files
committed
Add explicit q4096 residual encoding and hosted precision audit
1 parent b71678c commit d386b8d

22 files changed

Lines changed: 733 additions & 49 deletions

‎.github/workflows/precision-v4.yml‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: precision v4
2+
on:
3+
push:
4+
paths:
5+
- '.github/workflows/precision-v4.yml'
6+
- 'config/precision-v4.json'
7+
- 'src/network.cpp'
8+
- 'src/network_kernel.cpp'
9+
- 'include/caissa/network.hpp'
10+
- 'training/model.py'
11+
- 'training/export.py'
12+
- 'verification/learning.py'
13+
- 'verification/network_bytes.py'
14+
- 'verification/numerical.py'
15+
- 'tools/network_audit.py'
16+
- 'tools/precision_audit.py'
17+
- 'tests/precision_export.py'
18+
- 'tests/network_precision_test.cpp'
19+
- 'tests/dense_kernel_test.cpp'
20+
- 'tests/kernel_test.cpp'
21+
- 'results/precision-v4-inputs/**'
22+
workflow_dispatch:
23+
permissions:
24+
contents: read
25+
concurrency:
26+
group: precision-v4-${{ github.ref }}
27+
cancel-in-progress: false
28+
jobs:
29+
encoding:
30+
strategy:
31+
fail-fast: false
32+
matrix:
33+
os: [ubuntu-24.04, macos-15]
34+
runs-on: ${{ matrix.os }}
35+
timeout-minutes: 30
36+
steps:
37+
- uses: actions/checkout@v4
38+
- name: Original profile deadline and cumulative workflow limits
39+
run: |
40+
python3 - <<'PY'
41+
import json, os, time
42+
rule = json.load(open('config/precision-v4.json'))['resources']
43+
assert int(os.environ['GITHUB_RUN_NUMBER']) <= 3
44+
assert int(os.environ['GITHUB_RUN_ATTEMPT']) == 1
45+
assert time.time() < rule['deadline_unix']
46+
assert rule['deadline_unix'] - rule['origin_unix'] == 7200
47+
PY
48+
- uses: astral-sh/setup-uv@v6
49+
with:
50+
enable-cache: false
51+
- name: Pinned dependencies
52+
timeout-minutes: 15
53+
run: uv sync --frozen --extra oracle
54+
- name: Native build and arithmetic tests
55+
timeout-minutes: 5
56+
run: |
57+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
58+
cmake --build build -j 2
59+
ctest --test-dir build --output-on-failure
60+
- name: Fixed original-checkpoint precision experiment
61+
timeout-minutes: 15
62+
run: |
63+
.venv/bin/python -m tools.precision_audit --engine build/caissa --checkpoint results/precision-v4-inputs/selected.pt --protocol config/precision-v4.json --expected-protocol-sha256 624a0f1700657e2f23ea28c12e800139be10921ba7286395da75fdca62ccfab9 --expected-commit "$GITHUB_SHA" --output runs/precision-v4-${{ github.run_id }}
64+
- uses: actions/upload-artifact@v4
65+
if: always()
66+
with:
67+
name: precision-v4-${{ matrix.os }}-${{ github.run_id }}
68+
path: |
69+
runs/precision-v4-*/
70+
build/Testing/Temporary/LastTest.log
71+
retention-days: 14

‎CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,8 @@ add_executable(lmr_window_test tests/lmr_window_test.cpp)
120120
target_link_libraries(lmr_window_test PRIVATE caissa_core)
121121
add_test(NAME lmr_window COMMAND lmr_window_test)
122122
set_tests_properties(lmr_window PROPERTIES TIMEOUT 30)
123+
124+
add_executable(network_precision_test tests/network_precision_test.cpp)
125+
target_link_libraries(network_precision_test PRIVATE caissa_core)
126+
add_test(NAME network_precision COMMAND network_precision_test)
127+
set_tests_properties(network_precision PROPERTIES TIMEOUT 60)

‎config/precision-v4.json‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"schema": "caissa-precision-profile-v1",
3+
"status": "frozen",
4+
"purpose": "Test one higher-resolution export of the unchanged validation-selected original float checkpoint. Retain the failed q1024 result and every new failure.",
5+
"selection": "Reuse exactly the existing v3 validation-only selection, seed73 step2750. No new seed, checkpoint, float parameter, mixture or model is selected using precision probes or match outcomes. No training or calibration occurs in this profile.",
6+
"training_format": 3,
7+
"checkpoint_sha256": "c76d8e9ef0f2255531bc9085f5d0330a7a4152cf9b21a65ff8aee740ab8b2005",
8+
"previous_network_sha256": "c251670ec53ba38a680c16cc6199be48f89cc829dd5483197e6d4b39ac6debf6",
9+
"model_campaign_sha256": "92d62149d3a4de184ad93b9f5f04f77e38386992d04d02bd824b33cf3e5241bd",
10+
"network": {
11+
"format": 4,
12+
"training_format": 3,
13+
"export_contract": "v3-float-to-v4-q4096-same-activation-ceiling-v1",
14+
"width": 64,
15+
"scale": 4096,
16+
"clip": 8188,
17+
"classical_contract": 1,
18+
"neural_weight": 10,
19+
"maximum_float_error_cp": 32,
20+
"probes": 100000,
21+
"probe_seed": 32452867,
22+
"error_contract": "unclamped-quantization-and-clamped-inference"
23+
},
24+
"resources": {
25+
"hosted_platforms": [
26+
"ubuntu-24.04",
27+
"macos-15"
28+
],
29+
"maximum_job_seconds": 1800,
30+
"maximum_jobs_per_platform": 3,
31+
"maximum_cumulative_job_seconds_per_platform": 5400,
32+
"origin_unix": 1792906174.2199628,
33+
"deadline_unix": 1792913374.2199628,
34+
"maximum_producer_seconds": 600,
35+
"maximum_independent_seconds": 600,
36+
"threads": 2,
37+
"maximum_artifact_bytes_per_host": 2147483648
38+
},
39+
"arithmetic": "8188/4096 equals 2047/1024 exactly, so the real activation ceiling is unchanged. Int16 weights and bounded int32 biases remain. NEON high-resolution chunks use at most eight products per int32 lane, then widen to int64. File layout and payload size do not increase.",
40+
"failure_policy": "A failure remains failed. Never increase 32 cp, shrink 100000 probes, pick a favorable replacement seed or overwrite v3 evidence. This new encoding experiment has its own original wall deadline and at most three 1800-second hosted executions per platform, totaling at most 5400 job seconds per platform. GitHub job reruns are refused. No earlier scope is reset or extended and no current timed match actor changes.",
41+
"acceptance_scope": "Export identity, integer arithmetic and numerical accuracy only. No Elo, full strength, final-source or release pass follows. Any later strength comparison requires its own prospectively frozen immutable actors and unchanged classical control."
42+
}

‎docs/notes/precision-v4.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# A finer integer lattice without changing the learned model
2+
3+
The full-data selected q1024 network failed its numerical gate: maximum float/integer discrepancy 57.055664 cp against the unchanged 32 cp limit. Independent float64 replay confirmed the failure. Its three completed training seeds, selected checkpoint and active strength campaign remain immutable.
4+
5+
The prospectively frozen [precision profile](../../config/precision-v4.json) tests one q4096 encoding of that same selected float checkpoint. Seed 73 at step 2750 remains selected by the original v3 validation-only procedure. This experiment performs no training, calibration, checkpoint replacement or model selection from numerical probes or match results. The original checkpoint is included with its [identity](../../results/precision-v4-inputs/IDENTITY.json). Format 3 export must still reproduce the original network byte for byte before format 4 is tested.
6+
7+
The new format uses scale 4096 and integer activation cap 8188. The equality `8188/4096 == 2047/1024` preserves the exact real activation ceiling; using 8191 would subtly change the float function. Tensor shapes, payload size, int16 weights, bounded int32 biases, original classical contract 1 and ten-percent mixture are unchanged. The independent byte reader and checkpoint-to-export verifier require the new format explicitly. Legacy full-profile verification cannot silently accept it.
8+
9+
NEON lane safety changes with the activation range. The existing 128-input blocks place at most 32 products in a signed int32 lane. At the higher resolution that could overflow. The q4096 path uses 32-input blocks, at most 8 products per lane, so `8 * 8188 * 32768 = 2146435072 < INT32_MAX`. Each block widens to int64 before combining blocks or biases. AVX2 also uses the conservative 32-input bound. Scalar, remainder and four-output dense paths use the same signed rounding and real clipping semantics. The old q1024 path retains its 128-input specialization.
10+
11+
The [native tests](../../tests/network_precision_test.cpp) generate original nonzero format 4 fixtures, check incremental state, undo, scalar/SIMD parity, score intervals and malformed numerical/baseline headers. Dot and dense tests include maximum activations, signed weight extremes, chunk boundaries and odd dimensions. The [export checks](../../tests/precision_export.py) independently compare 1024 actual native outputs, verify every exported tensor against the original checkpoint, preserve legacy bytes and reject a changed real cap.
12+
13+
The [hosted campaign](../../.github/workflows/precision-v4.yml) builds and tests on Linux and macOS, then runs 100000 fresh frozen reachable probes, independent float64/integer replay and 1000000 restoration transitions. The 32 cp raw and clamped error limit is unchanged. Compilation and model inference run remotely to avoid competing with the active timed matches. The profile has one original two-hour wall deadline, at most three 1800-second workflow executions per platform and no GitHub job reruns. Earlier negative artifacts are never overwritten. This note describes the implementation and frozen test, not a passing measurement. A numerical pass would still establish neither Elo nor release readiness.

‎include/caissa/network.hpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class Network {
6363
// Dot inputs are clipped activations in [0,2047] and arbitrary int16 weights.
6464
std::int64_t dot_scalar(const std::int16_t* a,const std::int16_t* b,std::size_t count);
6565
std::int64_t dot_native(const std::int16_t* a,const std::int16_t* b,std::size_t count);
66+
// Format four keeps the same real activation ceiling with four times the
67+
// integer resolution. Its [0,8188] inputs require shorter SIMD chunks.
68+
std::int64_t dot_native_high_precision(const std::int16_t* a,const std::int16_t* b,std::size_t count);
6669
// Input activations are in [0,clip], weights are row-major int16, and biases
6770
// are int32. Up to 256 inputs and 32 outputs, with exact signed rounding.
6871
void clipped_affine(const std::int16_t* input,const std::int16_t* weights,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"checkpoint_sha256": "c76d8e9ef0f2255531bc9085f5d0330a7a4152cf9b21a65ff8aee740ab8b2005",
3+
"original_model_campaign_sha256": "92d62149d3a4de184ad93b9f5f04f77e38386992d04d02bd824b33cf3e5241bd",
4+
"original_selected_seed": 73,
5+
"original_selected_step": 2750,
6+
"new_training": false,
7+
"selection_changed": false,
8+
"full_acceptance": false
9+
}
9.08 MB
Binary file not shown.

‎src/network.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ std::shared_ptr<const Network> Network::load(const std::filesystem::path& path)
4141
std::ifstream input(path,std::ios::binary);std::vector<std::uint8_t> bytes(static_cast<std::size_t>(size));
4242
if(!input.read(reinterpret_cast<char*>(bytes.data()),static_cast<std::streamsize>(bytes.size())))throw InputError("network read failed");
4343
Reader reader(bytes);auto magic=reader.text(8);auto version=reader.u32();
44-
if(!((magic=="CAISSAN1"&&version==1)||(magic=="CAISSAN2"&&version==2)||(magic=="CAISSAN3"&&version==3)))throw InputError("unsupported network format version");
44+
if(!((magic=="CAISSAN1"&&version==1)||(magic=="CAISSAN2"&&version==2)||(magic=="CAISSAN3"&&version==3)||(magic=="CAISSAN4"&&version==4)))throw InputError("unsupported network format version");
4545
if(reader.u32()!=feature_version||reader.u32()!=feature_count||reader.u32()!=transformer_width||reader.u32()!=dense1_width||reader.u32()!=dense2_width)throw InputError("network feature or architecture mismatch");
4646
auto scale=reader.u32(),clip=reader.u32();
47-
if((version==1&&(scale!=64||clip!=127))||(version>=2&&(scale!=1024||clip!=2047)))throw InputError("network numerical contract mismatch");
47+
if((version==1&&(scale!=64||clip!=127))||((version==2||version==3)&&(scale!=1024||clip!=2047))||(version==4&&(scale!=4096||clip!=8188)))throw InputError("network numerical contract mismatch");
4848
reader.bias_limit(version==1?1<<20:1<<28);
49-
auto net=std::shared_ptr<Network>(new Network);net->residual_=version==3;net->scale_=static_cast<int>(scale);net->clip_=static_cast<int>(clip);net->identity_=reader.text(64);
49+
auto net=std::shared_ptr<Network>(new Network);net->residual_=version>=3;net->scale_=static_cast<int>(scale);net->clip_=static_cast<int>(clip);net->identity_=reader.text(64);
5050
if(!std::all_of(net->identity_.begin(),net->identity_.end(),[](char c){return (c>='0'&&c<='9')||(c>='a'&&c<='f');}))throw InputError("invalid training identity");
5151
if(reader.u64()!=payload_bytes)throw InputError("network payload declaration mismatch");
5252
net->checksum_=reader.u32();auto baseline_contract=reader.u32();
53-
if(baseline_contract!=(version==3?classical_contract_version:0))throw InputError("unsupported network baseline contract");
53+
if(baseline_contract!=(version>=3?classical_contract_version:0))throw InputError("unsupported network baseline contract");
5454
if(crc32(std::span(bytes).subspan(120))!=net->checksum_)throw InputError("network checksum mismatch");
5555
net->embedding_.resize(feature_count*transformer_width);
5656
for(auto& value:net->embedding_)value=reader.i16();for(auto& value:net->bias0_)value=reader.i32();
@@ -101,7 +101,7 @@ void Network::validate(const Position& p,const Accumulator& a,int neural_weight)
101101
if(neural_weight<0||neural_weight>100)throw InputError("neural weight outside percentage bounds");
102102
}
103103
int Network::dense_score(const Position& p,const Accumulator& a,bool scalar)const {
104-
auto dot=scalar?dot_scalar:dot_native;
104+
auto dot=scalar?dot_scalar:(clip_<=2047?dot_native:dot_native_high_precision);
105105
std::array<std::int16_t,transformer_width*2> input{};
106106
int first=index(p.side());
107107
for(int perspective=0;perspective<2;++perspective)for(std::size_t j=0;j<transformer_width;++j)

‎src/network_kernel.cpp‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ std::int64_t dot_scalar(const std::int16_t* a,const std::int16_t* b,std::size_t
1010
std::int64_t sum=0;for(std::size_t i=0;i<count;++i)sum+=static_cast<std::int32_t>(a[i])*b[i];return sum;
1111
}
1212
#if defined(__x86_64__) && (defined(__clang__) || defined(__GNUC__))
13+
template<std::size_t chunk>
1314
__attribute__((target("avx2"))) static std::int64_t dot_avx2(const std::int16_t* a,const std::int16_t* b,std::size_t count) {
1415
std::int64_t sum=0;std::size_t i=0;alignas(32) std::array<std::int32_t,8> lanes{};
1516
while(i+16<=count) {
16-
const auto end=std::min(count,i+128);auto accumulator=_mm256_setzero_si256();
17+
const auto end=std::min(count,i+chunk);auto accumulator=_mm256_setzero_si256();
1718
for(;i+16<=end;i+=16) {
1819
auto product=_mm256_madd_epi16(_mm256_loadu_si256(reinterpret_cast<const __m256i*>(a+i)),_mm256_loadu_si256(reinterpret_cast<const __m256i*>(b+i)));
1920
accumulator=_mm256_add_epi32(accumulator,product);
@@ -23,12 +24,14 @@ __attribute__((target("avx2"))) static std::int64_t dot_avx2(const std::int16_t*
2324
return sum+dot_scalar(a+i,b+i,count-i);
2425
}
2526
#endif
26-
std::int64_t dot_native(const std::int16_t* a,const std::int16_t* b,std::size_t count) {
2727
#if defined(__aarch64__)
28+
template<std::size_t chunk>
29+
static std::int64_t dot_neon(const std::int16_t* a,const std::int16_t* b,std::size_t count) {
2830
std::int64_t sum=0;std::size_t i=0;
2931
while(i+8<=count) {
30-
const auto end=std::min(count,i+128);int32x4_t accumulator=vdupq_n_s32(0);
31-
// At most 32 products per lane: 32*2047*32768 < INT32_MAX.
32+
const auto end=std::min(count,i+chunk);int32x4_t accumulator=vdupq_n_s32(0);
33+
// Widen before the next chunk. The 128-input low-resolution and
34+
// 32-input high-resolution specializations both fit signed lanes.
3235
for(;i+8<=end;i+=8) {
3336
auto x=vld1q_s16(a+i),y=vld1q_s16(b+i);
3437
accumulator=vmlal_s16(accumulator,vget_low_s16(x),vget_low_s16(y));
@@ -37,16 +40,29 @@ std::int64_t dot_native(const std::int16_t* a,const std::int16_t* b,std::size_t
3740
sum+=vaddlvq_s32(accumulator);
3841
}
3942
return sum+dot_scalar(a+i,b+i,count-i);
43+
}
44+
#endif
45+
std::int64_t dot_native(const std::int16_t* a,const std::int16_t* b,std::size_t count) {
46+
#if defined(__aarch64__)
47+
return dot_neon<128>(a,b,count);
48+
#elif defined(__x86_64__) && (defined(__clang__) || defined(__GNUC__))
49+
if(__builtin_cpu_supports("avx2"))return dot_avx2<128>(a,b,count);
50+
#endif
51+
return dot_scalar(a,b,count);
52+
}
53+
std::int64_t dot_native_high_precision(const std::int16_t* a,const std::int16_t* b,std::size_t count) {
54+
#if defined(__aarch64__)
55+
return dot_neon<32>(a,b,count);
4056
#elif defined(__x86_64__) && (defined(__clang__) || defined(__GNUC__))
41-
if(__builtin_cpu_supports("avx2"))return dot_avx2(a,b,count);
57+
if(__builtin_cpu_supports("avx2"))return dot_avx2<32>(a,b,count);
4258
#endif
4359
return dot_scalar(a,b,count);
4460
}
4561
void clipped_affine(const std::int16_t* input,const std::int16_t* weights,
4662
const std::int32_t* biases,std::int16_t* output,
4763
std::size_t inputs,std::size_t outputs,int scale,int clip,bool scalar) {
4864
if(!input||!weights||!biases||!output||inputs==0||inputs>256||outputs==0||outputs>32||
49-
(scale!=64&&scale!=1024)||clip<1||clip>2047)throw InputError("dense kernel dimensions or numerical contract");
65+
!(((scale==64||scale==1024)&&clip>=1&&clip<=2047)||(scale==4096&&clip==8188)))throw InputError("dense kernel dimensions or numerical contract");
5066
auto finish=[&](std::int64_t sum) {
5167
auto rounded=sum>=0?(sum+scale/2)/scale:-((-sum+scale/2)/scale);
5268
return static_cast<std::int16_t>(std::clamp<std::int64_t>(rounded,0,clip));
@@ -55,8 +71,9 @@ void clipped_affine(const std::int16_t* input,const std::int16_t* weights,
5571
#if defined(__aarch64__)
5672
if(!scalar)for(;row+4<=outputs;row+=4) {
5773
std::array<std::int64_t,4> sums{biases[row],biases[row+1],biases[row+2],biases[row+3]};
58-
for(std::size_t begin=0;begin<inputs;begin+=128) {
59-
auto end=std::min(inputs,begin+128);std::size_t i=begin;
74+
const std::size_t chunk=clip<=2047?128:32;
75+
for(std::size_t begin=0;begin<inputs;begin+=chunk) {
76+
auto end=std::min(inputs,begin+chunk);std::size_t i=begin;
6077
auto a0=vdupq_n_s32(0),a1=a0,a2=a0,a3=a0;
6178
for(;i+8<=end;i+=8) {
6279
auto x=vld1q_s16(input+i);auto lo=vget_low_s16(x);
@@ -67,8 +84,8 @@ void clipped_affine(const std::int16_t* input,const std::int16_t* weights,
6784
a2=vmlal_s16(a2,lo,vget_low_s16(w2));a2=vmlal_high_s16(a2,x,w2);
6885
a3=vmlal_s16(a3,lo,vget_low_s16(w3));a3=vmlal_high_s16(a3,x,w3);
6986
}
70-
// Each lane contains at most 32 bounded products. Widen before
71-
// combining chunks or adding biases; neither fits int32 in general.
87+
// At q1024: 32*2047*32768 < INT32_MAX. At q4096:
88+
// 8*8188*32768 < INT32_MAX. Widen before combining chunks/biases.
7289
sums[0]+=vaddlvq_s32(a0);sums[1]+=vaddlvq_s32(a1);
7390
sums[2]+=vaddlvq_s32(a2);sums[3]+=vaddlvq_s32(a3);
7491
for(;i<end;++i)for(std::size_t k=0;k<4;++k)
@@ -77,7 +94,7 @@ void clipped_affine(const std::int16_t* input,const std::int16_t* weights,
7794
for(std::size_t k=0;k<4;++k)output[row+k]=finish(sums[k]);
7895
}
7996
#endif
80-
auto dot=scalar?dot_scalar:dot_native;
97+
auto dot=scalar?dot_scalar:(clip<=2047?dot_native:dot_native_high_precision);
8198
for(;row<outputs;++row)output[row]=finish(dot(input,weights+row*inputs,inputs)+biases[row]);
8299
}
83100
std::string_view native_kernel() {

0 commit comments

Comments
 (0)