All MicroEvals
{-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-#...
Create MicroEval
Header image for {-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-#...

{-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-#...

Prompt

{-# LANGUAGE DataKinds #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE BinaryLiterals #-} module RankerCore where import Clash.Prelude import qualified Prelude as P type Fix16 = SFixed 16 16 type NumHashLanes = 16 type TopKDepth = 16 type BitmaskWords = 2 fixWeightBase :: Fix16 fixWeightBase = 0.4000091552734375 fixWeightOverlap :: Fix16 fixWeightOverlap = 0.29998779296875 fixWeightJaccard :: Fix16 fixWeightJaccard = 0.29998779296875 fixWeightDiversity :: Fix16 fixWeightDiversity = 0.29998779296875 fixWeightProximity :: Fix16 fixWeightProximity = 0.29998779296875 fixMaxRawScoreInv :: Fix16 fixMaxRawScoreInv = 0.010009765625 hashMultA :: Unsigned 64 hashMultA = 0x9e3779b97f4a7c15 hashMultB :: Unsigned 64 hashMultB = 0x517cc1b727220a95 newtype ScoreFix = ScoreFix { unScoreFix :: Fix16 } deriving (Generic, NFDataX, Eq, Ord, Show, ShowX, BitPack) newtype SegmentID = SegmentID { unSegmentID :: Unsigned 64 } deriving (Generic, NFDataX, Eq, Ord, Show, ShowX, BitPack) newtype Position = Position { unPosition :: Unsigned 64 } deriving (Generic, NFDataX, Eq, Ord, Show, ShowX, BitPack) newtype Token = Token { unToken :: Unsigned 32 } deriving (Generic, NFDataX, Eq, Ord, Show, ShowX, BitPack) data TokenStreamIn = TokenStreamIn { tsToken :: Token , tsValid :: Bool , tsLast :: Bool , tsAnchorPos :: Position , tsHasAnchor :: Bool } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) data CandidateIn = CandidateIn { candID :: SegmentID , candBaseScore :: ScoreFix , candBitmask :: Vec BitmaskWords (BitVector 64) , candPosition :: Position , candValid :: Bool } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) data RankedCandidate = RankedCandidate { rankedID :: SegmentID , rankedScore :: ScoreFix , rankedPos :: Position , rankedValid :: Bool } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) data HardwareCommand = CmdIdle , CmdInitQuery (Unsigned 64) (Vec BitmaskWords (BitVector 64)) , CmdIngestToken TokenStreamIn , CmdScoreCandidate CandidateIn , CmdFlushTopK deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) data HardwareResponse = HardwareResponse { respTopK :: RankedCandidate , respReady :: Bool , respDone :: Bool , respBusy :: Bool } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) hardwareHashStep :: Unsigned 64 -> Token -> Unsigned 64 -> Unsigned 64 hardwareHashStep seed tok laneSeed = let t64 = resize (unToken tok) :: Unsigned 64 mixed = (t64 `xor` seed) `xor` laneSeed step1 = mixed * 0xbf58476d1ce4e5b9 step2 = step1 `xor` (step1 .>>. 30) step3 = step2 * 0x94d049bb133111eb in step3 `xor` (step3 .>>. 31) data MinHashState = MinHashState { mhsMins :: Vec NumHashLanes (Unsigned 64) , mhsTokenCnt :: Unsigned 32 , mhsSeed :: Unsigned 64 } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) initMinHashState :: Unsigned 64 -> MinHashState initMinHashState seed = MinHashState { mhsMins = repeat maxBound , mhsTokenCnt = 0 , mhsSeed = seed } updateMinHash :: MinHashState -> Token -> MinHashState updateMinHash st tok = let laneIndices = iterateI (+1) (0 :: Unsigned 64) computeLane idx currentMin = let laneSeedA = mhsSeed st + (idx * hashMultA) laneSeedB = mhsSeed st + ((idx + 1) * hashMultB) h = hardwareHashStep (mhsSeed st) tok laneSeedA `xor` laneSeedB in min currentMin h newMins = zipWith computeLane laneIndices (mhsMins st) in st { mhsMins = newMins, mhsTokenCnt = mhsTokenCnt st + 1 } extractBitmask :: MinHashState -> Vec BitmaskWords (BitVector 64) extractBitmask st = let bits = map (\m -> if testBit m 0 then 1 else 0) (mhsMins st) word0Bits = take (SNat :: SNat 16) bits pack16To64 :: Vec 16 Bit -> BitVector 64 pack16To64 v = resize (pack v) w0 = pack16To64 word0Bits w1 = 0 :: BitVector 64 in w0 :> w1 :> Nil popCount64 :: BitVector 64 -> Unsigned 8 popCount64 bv = let u = unpack bv :: Vec 64 Bit addBit acc b = acc + (if b == 1 then 1 else 0) in foldl addBit 0 u computeJaccardHW :: Vec BitmaskWords (BitVector 64) -> Vec BitmaskWords (BitVector 64) -> Unsigned 8 -> ScoreFix computeJaccardHW maskA maskB validBits = let agreeWord wA wB = complement (wA `xor` wB) agreeWords = zipWith agreeWord maskA maskB totalMatches = foldl (+) 0 (map popCount64 agreeWords) validBitsClamped = if validBits == 0 then 1 else validBits ratio = (resize totalMatches :: Fix16) / (resize validBitsClamped :: Fix16) estimate = (2.0 * ratio) - 1.0 clamped = max 0.0 (min 1.0 estimate) in ScoreFix clamped data ProximityState = ProximityState { psTotalDist :: Unsigned 64 , psAnchorCnt :: Unsigned 32 , psTokenCnt :: Unsigned 32 } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) initProximityState :: ProximityState initProximityState = ProximityState 0 0 0 updateProximity :: ProximityState -> TokenStreamIn -> ProximityState updateProximity st tin = if not (tsValid tin) then st else let currentPos = resize (psTokenCnt st) :: Unsigned 64 anchorPos = unPosition (tsAnchorPos tin) dist = if currentPos >= anchorPos then currentPos - anchorPos else anchorPos - currentPos newTotal = if tsHasAnchor tin then psTotalDist st + dist else psTotalDist st newAnchors = if tsHasAnchor tin then psAnchorCnt st + 1 else psAnchorCnt st in ProximityState { psTotalDist = newTotal , psAnchorCnt = newAnchors , psTokenCnt = psTokenCnt st + 1 } calculateProximityScore :: ProximityState -> ScoreFix calculateProximityScore st = if psAnchorCnt st == 0 || psTokenCnt st == 0 then ScoreFix 0.0 else let denom = (resize (psAnchorCnt st) :: Unsigned 64) * (resize (psTokenCnt st) :: Unsigned 64) denomSafe = if denom == 0 then 1 else denom distRatio = (resize (psTotalDist st) :: Fix16) / (resize denomSafe :: Fix16) prox = 1.0 - (max 0.0 (min 1.0 distRatio)) in ScoreFix prox fuseScores :: ScoreFix -> ScoreFix -> ScoreFix -> ScoreFix -> ScoreFix -> ScoreFix fuseScores (ScoreFix base) (ScoreFix overlap) (ScoreFix jaccard) (ScoreFix prox) (ScoreFix divScore) = let rawNgramAndDiv = base + (fixWeightDiversity * divScore) + (fixWeightProximity * prox) clampedRaw = max 0.0 (min 100.0 rawNgramAndDiv) scaledBase = clampedRaw * fixMaxRawScoreInv combined = (scaledBase * fixWeightBase) + (overlap * fixWeightOverlap) + (jaccard * fixWeightJaccard) finalClamped = max 0.0 (min 1.0 combined) in ScoreFix finalClamped data SystolicCell = SystolicCell { cellItem :: RankedCandidate } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) initSystolicCell :: SystolicCell initSystolicCell = SystolicCell (RankedCandidate (SegmentID 0) (ScoreFix (-1.0)) (Position 0) False) stepSystolicArray :: Vec TopKDepth SystolicCell -> RankedCandidate -> (Vec TopKDepth SystolicCell, RankedCandidate) stepSystolicArray cells incoming = let insertStep (accCells, carry) cell = let item = cellItem cell carryBetter = rankedValid carry && (not (rankedValid item) || rankedScore carry > rankedScore item) newCellItem = if carryBetter then carry else item newCarry = if carryBetter then item else carry in (accCells :< SystolicCell newCellItem, newCarry) (resultVecRev, kickedOut) = foldl insertStep (Nil, incoming) cells in (reverse resultVecRev, kickedOut) data CoreFSM = StateIdle , StateStreaming , StateScoring , StateFlushing (Index TopKDepth) deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) data CoreState = CoreState { csFSM :: CoreFSM , csQuerySeed :: Unsigned 64 , csQueryBitmask :: Vec BitmaskWords (BitVector 64) , csMinHash :: MinHashState , csProximity :: ProximityState , csSystolic :: Vec TopKDepth SystolicCell , csCurrentScore :: ScoreFix } deriving (Generic, NFDataX, Eq, Show, ShowX, BitPack) initialCoreState :: CoreState initialCoreState = CoreState { csFSM = StateIdle , csQuerySeed = 0 , csQueryBitmask = repeat 0 , csMinHash = initMinHashState 0 , csProximity = initProximityState , csSystolic = repeat initSystolicCell , csCurrentScore = ScoreFix 0.0 } rankerEngineT :: CoreState -> HardwareCommand -> (CoreState, HardwareResponse) rankerEngineT st cmd = case (csFSM st, cmd) of (StateIdle, CmdInitQuery qSeed qMask) -> let nextSt = st { csFSM = StateIdle , csQuerySeed = qSeed , csQueryBitmask = qMask , csMinHash = initMinHashState qSeed , csProximity = initProximityState , csSystolic = repeat initSystolicCell } resp = HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False False in (nextSt, resp) (StateIdle, CmdIngestToken tin) -> if tsValid tin then let nextMH = updateMinHash (csMinHash st) (tsToken tin) nextProx = updateProximity (csProximity st) tin nextFSM = if tsLast tin then StateScoring else StateStreaming nextSt = st { csFSM = nextFSM, csMinHash = nextMH, csProximity = nextProx } resp = HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False True in (nextSt, resp) else (st, HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False False) (StateStreaming, CmdIngestToken tin) -> if tsValid tin then let nextMH = updateMinHash (csMinHash st) (tsToken tin) nextProx = updateProximity (csProximity st) tin nextFSM = if tsLast tin then StateScoring else StateStreaming nextSt = st { csFSM = nextFSM, csMinHash = nextMH, csProximity = nextProx } resp = HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False True in (nextSt, resp) else (st, HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False True) (StateScoring, _) -> let localMask = extractBitmask (csMinHash st) jaccard = computeJaccardHW (csQueryBitmask st) localMask 16 prox = calculateProximityScore (csProximity st) dummyOverlap = jaccard dummyDiversity = ScoreFix 0.8 fused = fuseScores (ScoreFix 10.0) dummyOverlap jaccard prox dummyDiversity nextSt = st { csFSM = StateIdle, csCurrentScore = fused } resp = HardwareResponse (RankedCandidate (SegmentID 0) fused (Position 0) True) True False False in (nextSt, resp) (StateIdle, CmdScoreCandidate cand) -> if candValid cand then let jaccard = computeJaccardHW (csQueryBitmask st) (candBitmask cand) 16 fused = fuseScores (candBaseScore cand) jaccard jaccard (ScoreFix 0.5) (ScoreFix 0.7) candRanked = RankedCandidate (candID cand) fused (candPosition cand) True (nextSystolic, _) = stepSystolicArray (csSystolic st) candRanked nextSt = st { csSystolic = nextSystolic } resp = HardwareResponse candRanked True False False in (nextSt, resp) else (st, HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False False) (StateIdle, CmdFlushTopK) -> let nextSt = st { csFSM = StateFlushing 0 } topElem = cellItem (head (csSystolic st)) resp = HardwareResponse topElem False False True in (nextSt, resp) (StateFlushing idx, _) -> let currentItem = cellItem (csSystolic st !! idx) isDone = idx == maxBound nextFSM = if isDone then StateIdle else StateFlushing (idx + 1) nextSt = st { csFSM = nextFSM } resp = HardwareResponse currentItem True isDone True in (nextSt, resp) _ -> (st, HardwareResponse (RankedCandidate (SegmentID 0) (ScoreFix 0) (Position 0) False) True False False) rankerCore :: HiddenClockResetEnable dom => Signal dom HardwareCommand -> Signal dom HardwareResponse rankerCore = mealy rankerEngineT initialCoreState {-# NOINLINE topEntity #-} topEntity :: Clock System -> Reset System -> Enable System -> Signal System HardwareCommand -> Signal System HardwareResponse topEntity = exposeClockResetEnable rankerCore I want to perform an exhaustive, line-by-line static analysis of the provided code to identify every single error, mock, dummy, stub, placeholder, and hidden logical flaw, so that the final output is a 100% complete, verified list of real issues with absolutely zero omissions or hallucinations. CRITICAL CONSTRAINTS (DO NOT BREAK THEM): 1. Read every single character from start to finish. Do not skip, summarize, or abbreviate any part of the code. 2. Identify ALL structural and logical flaws: mocks, dummies, stubs, placeholders, syntax errors, and hidden runtime exceptions. 3. Theoretically execute the code paths to uncover non-obvious errors that would occur in practice. 4. Focus EXCLUSIVELY on real, verifiable errors. Do not invent, hallucinate, or assume errors that do not exist. Write down exactly what you find, and nothing more. 5. NO polite filler, NO introductions, NO summaries, NO explanations outside the requested format. OUTPUT FORMAT: Provide the output strictly in the following structure: [ERROR LIST] - Line [X]: [Exact error description] ... [END OF LIST] FILE CLOSED. ALL ERRORS LISTED.

Drag to resize