Upload reference/net.rs with huggingface_hub
Browse files- reference/net.rs +6 -19
reference/net.rs
CHANGED
|
@@ -32,8 +32,6 @@
|
|
| 32 |
//! bug that yields a plausible-looking network which quietly plays badly, and
|
| 33 |
//! it is miserable to find after the fact.
|
| 34 |
|
| 35 |
-
use core::sync::atomic::{AtomicU64, Ordering};
|
| 36 |
-
|
| 37 |
use crate::bb::*;
|
| 38 |
use crate::pos::*;
|
| 39 |
use crate::sys::SyncCell;
|
|
@@ -109,19 +107,8 @@ fn net() -> &'static Net {
|
|
| 109 |
/// clear between bench positions cost more than the extra hits were worth.
|
| 110 |
/// Bumping a counter invalidates every entry at once, so the size question is
|
| 111 |
/// now about cache footprint alone.
|
| 112 |
-
///
|
| 113 |
-
/// The slot is an atomic because every search thread shares this table. A
|
| 114 |
-
/// relaxed load and store compile to the same `ldr`/`str` a plain access would,
|
| 115 |
-
/// so nothing is paid for it; what it buys is that the whole entry — tag,
|
| 116 |
-
/// generation and score together — is written and read as one indivisible
|
| 117 |
-
/// word, and a thread can never observe half of one position's entry beside
|
| 118 |
-
/// half of another's.
|
| 119 |
const CACHE_BITS: usize = 16;
|
| 120 |
-
static CACHE: SyncCell<[
|
| 121 |
-
#[allow(clippy::declare_interior_mutable_const)]
|
| 122 |
-
const Z: AtomicU64 = AtomicU64::new(0);
|
| 123 |
-
[Z; 1 << CACHE_BITS]
|
| 124 |
-
});
|
| 125 |
static GEN: SyncCell<u8> = SyncCell::new(0);
|
| 126 |
|
| 127 |
#[inline(always)]
|
|
@@ -139,8 +126,8 @@ pub fn clear_cache() {
|
|
| 139 |
// would start answering again. That only happens once every 256 clears, so
|
| 140 |
// pay for the real erase then.
|
| 141 |
if *g == 0 {
|
| 142 |
-
for e in unsafe { CACHE.
|
| 143 |
-
e
|
| 144 |
}
|
| 145 |
}
|
| 146 |
}
|
|
@@ -471,8 +458,8 @@ pub fn evaluate(pos: &Position) -> i32 {
|
|
| 471 |
let slot = (pos.key as usize) & ((1 << CACHE_BITS) - 1);
|
| 472 |
let gen = unsafe { *GEN.as_ref() };
|
| 473 |
let want = pack(pos.key, gen, 0);
|
| 474 |
-
let c = unsafe { CACHE.
|
| 475 |
-
let hit = c[slot]
|
| 476 |
if hit & !0xFFFF == want {
|
| 477 |
return hit as u16 as i16 as i32;
|
| 478 |
}
|
|
@@ -488,6 +475,6 @@ pub fn evaluate(pos: &Position) -> i32 {
|
|
| 488 |
let w = &n.out_w[b * 2 * H..(b + 1) * 2 * H];
|
| 489 |
let out = propagate(&us, &w[..H]) + propagate(&them, &w[H..]) + n.out_b[b];
|
| 490 |
let score = (out * SCALE / (QA * QB)).clamp(-20_000, 20_000);
|
| 491 |
-
c[slot]
|
| 492 |
score
|
| 493 |
}
|
|
|
|
| 32 |
//! bug that yields a plausible-looking network which quietly plays badly, and
|
| 33 |
//! it is miserable to find after the fact.
|
| 34 |
|
|
|
|
|
|
|
| 35 |
use crate::bb::*;
|
| 36 |
use crate::pos::*;
|
| 37 |
use crate::sys::SyncCell;
|
|
|
|
| 107 |
/// clear between bench positions cost more than the extra hits were worth.
|
| 108 |
/// Bumping a counter invalidates every entry at once, so the size question is
|
| 109 |
/// now about cache footprint alone.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
const CACHE_BITS: usize = 16;
|
| 111 |
+
static CACHE: SyncCell<[u64; 1 << CACHE_BITS]> = SyncCell::new([0; 1 << CACHE_BITS]);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
static GEN: SyncCell<u8> = SyncCell::new(0);
|
| 113 |
|
| 114 |
#[inline(always)]
|
|
|
|
| 126 |
// would start answering again. That only happens once every 256 clears, so
|
| 127 |
// pay for the real erase then.
|
| 128 |
if *g == 0 {
|
| 129 |
+
for e in unsafe { CACHE.as_mut() }.iter_mut() {
|
| 130 |
+
*e = 0;
|
| 131 |
}
|
| 132 |
}
|
| 133 |
}
|
|
|
|
| 458 |
let slot = (pos.key as usize) & ((1 << CACHE_BITS) - 1);
|
| 459 |
let gen = unsafe { *GEN.as_ref() };
|
| 460 |
let want = pack(pos.key, gen, 0);
|
| 461 |
+
let c = unsafe { CACHE.as_mut() };
|
| 462 |
+
let hit = c[slot];
|
| 463 |
if hit & !0xFFFF == want {
|
| 464 |
return hit as u16 as i16 as i32;
|
| 465 |
}
|
|
|
|
| 475 |
let w = &n.out_w[b * 2 * H..(b + 1) * 2 * H];
|
| 476 |
let out = propagate(&us, &w[..H]) + propagate(&them, &w[H..]) + n.out_b[b];
|
| 477 |
let score = (out * SCALE / (QA * QB)).clamp(-20_000, 20_000);
|
| 478 |
+
c[slot] = pack(pos.key, gen, score);
|
| 479 |
score
|
| 480 |
}
|