module Accelerator.SM90.Word import Accelerator.SM86.Control import Accelerator.SM86.Instruction import Accelerator.SM86.Types import Data.Bytes import Model.Config import Model.Parameter import Model.Word64 import Std.Natural -- One Hopper (sm_90) instruction: 128 bits, held as its two little-endian -- 64-bit halves exactly as nvdisasm -hex prints them (the first /* 0x... */ -- is `low`, bits 0..63; the second is `high`, bits 64..127). Every SM90 -- encoder builds one of these from typed operands and every SM90 decoder -- reads one back, so a round-trip law compares two typed values and an -- oracle-agreement law compares two pairs of naturals. -- -- Fields are addressed by the place value of their lowest bit (2^bit within -- the half that holds them) and the number of values they hold (2^width): -- the arithmetic is the runtime's binary naturals (nat-divide, nat-modulo, -- nat-multiply), never a unary loop and never a byte-at-a-time builder. -- A field that straddles the two halves (BRA's offset, bits 34..81) is two -- fields, one per half. -- -- The header every Volta-and-later word shares -- opcode bits 0..11, guard -- predicate 12..14 and its negation 15, the scheduling control 105..125 -- -- is the same on sm_90 as on sm_86: measured on every distinct word of the -- GPT-2 trainer and initialization programs (nvdisasm -b SM86 against -- -b SM90, CUDA 12.4, comparisons/h100/README.md) and by the oracle laws of -- Accelerator.SM90.InstructionEncodingProof (CUDA 12.9). So the operand -- vocabulary is Accelerator.SM86's (registers, predicates, guards, -- controls); only the opcode-specific bits are Hopper's own. family SM90Word : Type 0 constructor SM90WordValue field unrestricted sm90WordLow : Nat field unrestricted sm90WordHigh : Nat end-family def sm90Word = (lambda unrestricted low : Nat . unrestricted high : Nat . constructor SM90Word SM90WordValue low high) def sm90WordLowOf = (lambda unrestricted word : SM90Word . eliminate SM90Word (lambda unrestricted current : SM90Word . Nat) word (branch SM90WordValue low high . low)) def sm90WordHighOf = (lambda unrestricted word : SM90Word . eliminate SM90Word (lambda unrestricted current : SM90Word . Nat) word (branch SM90WordValue low high . high)) -- A field of one 64-bit half: its place value (2^lowest bit) and span -- (2^width). def sm90Field = (lambda unrestricted half : Nat . unrestricted place : Nat . unrestricted span : Nat . nat-modulo (nat-divide half place) span) -- The value placed in a field (the caller has checked that it fits). def sm90Put = (lambda unrestricted value : Nat . unrestricted place : Nat . nat-multiply value place) def sm90Place = (lambda unrestricted bit : Nat . naturalPowerOfTwo bit) def sm90Span4 : Nat = 16 def sm90Span8 : Nat = 256 def sm90Span32 : Nat = (compile-time (sm90Place 32)) -- 1 when `value` < `span`: the encoders refuse a field that does not fit -- rather than truncate it. def sm90Fits = (lambda unrestricted value : Nat . unrestricted span : Nat . nat-less-than value span) -- The sixteen bytes the device reads, low half first, each little-endian. def sm90WordBytes = (lambda unrestricted word : SM90Word . then (bytes-append (dataBytesWord64LE (modelWord64FromNaturalTruncated (sm90WordLowOf word)))) dataBytesWord64LE modelWord64FromNaturalTruncated sm90WordHighOf word) -- A choice between two values of any type on a 0/1 flag (nat-less-than's -- result): the decoders select typed operands with it. def sm90Choose = (lambda erased valueType : Type 0 . unrestricted flag : Nat . unrestricted whenTrue : valueType . unrestricted whenFalse : valueType . nat-eliminate (lambda unrestricted current : Nat . valueType) whenFalse (lambda unrestricted predecessor : Nat . unrestricted induction : valueType . whenTrue) flag) -- --------------------------------------------------------------------------- -- Registers and predicates def sm90RegisterNumber = (lambda unrestricted register : SM86Register . eliminate SM86Register (lambda unrestricted current : SM86Register . Nat) register (branch SM86RegisterValue value . byte-to-nat value)) def sm90RegisterOf = (lambda unrestricted number : Nat . constructor SM86Register SM86RegisterValue (nat-to-byte number)) def sm90PredicateOf = (lambda unrestricted number : Nat . then (sm90Choose SM86Predicate (naturalEqual number 0) SM86Predicate0) (sm90Choose SM86Predicate (naturalEqual number 1) SM86Predicate1) (sm90Choose SM86Predicate (naturalEqual number 2) SM86Predicate2) (sm90Choose SM86Predicate (naturalEqual number 3) SM86Predicate3) (sm90Choose SM86Predicate (naturalEqual number 4) SM86Predicate4) (sm90Choose SM86Predicate (naturalEqual number 5) SM86Predicate5) (sm90Choose SM86Predicate (naturalEqual number 6) SM86Predicate6) SM86PredicateTrue) -- --------------------------------------------------------------------------- -- The header: opcode, guard, control def sm90OpcodeSpan : Nat = 4096 def sm90GuardPlace : Nat = 4096 -- the guard's four bits: predicate (PT = 7 always executes) and negation def sm90GuardBits = (lambda unrestricted guard : SM86InstructionGuard . eliminate SM86InstructionGuard (lambda unrestricted current : SM86InstructionGuard . Nat) guard (branch SM86InstructionAlways . 7) (branch SM86InstructionWhen predicate . byte-to-nat (sm86PredicateNumber predicate)) (branch SM86InstructionWhenNot predicate . nat-add 8 (byte-to-nat (sm86PredicateNumber predicate)))) -- PT, not negated, is "always"; !PT never executes and is no guard an -- SM86-shaped program writes, but it decodes to the typed form the bits say. def sm90GuardOf = (lambda unrestricted bits : Nat . then (sm90Choose SM86InstructionGuard (naturalEqual bits 7) SM86InstructionAlways) (sm90Choose SM86InstructionGuard (nat-less-than bits 8) (constructor SM86InstructionGuard SM86InstructionWhen (sm90PredicateOf bits))) (constructor SM86InstructionGuard SM86InstructionWhenNot) sm90PredicateOf (nat-subtract bits) 8) -- The scheduling control, bits 105..125 (high bits 41..61): stall (4), -- yield (1), write scoreboard (3), read scoreboard (3), wait mask (6), -- operand reuse (4) -- Accelerator.SM86.ControlEncoding's layout. def sm90ControlPlace : Nat = (compile-time (sm90Place 41)) def sm90ControlSpan : Nat = (compile-time (sm90Place 21)) def sm90BarrierCode = (lambda unrestricted barrier : SM86Barrier . eliminate SM86Barrier (lambda unrestricted current : SM86Barrier . Nat) barrier (branch SM86Barrier0 . 0) (branch SM86Barrier1 . 1) (branch SM86Barrier2 . 2) (branch SM86Barrier3 . 3) (branch SM86Barrier4 . 4) (branch SM86Barrier5 . 5) (branch SM86Barrier6 . 6) (branch SM86BarrierNone . 7)) def sm90BarrierOf = (lambda unrestricted code : Nat . then (sm90Choose SM86Barrier (naturalEqual code 0) SM86Barrier0) (sm90Choose SM86Barrier (naturalEqual code 1) SM86Barrier1) (sm90Choose SM86Barrier (naturalEqual code 2) SM86Barrier2) (sm90Choose SM86Barrier (naturalEqual code 3) SM86Barrier3) (sm90Choose SM86Barrier (naturalEqual code 4) SM86Barrier4) (sm90Choose SM86Barrier (naturalEqual code 5) SM86Barrier5) (sm90Choose SM86Barrier (naturalEqual code 6) SM86Barrier6) SM86BarrierNone) -- The control's 21 bits, or a value at or above the span when a field -- overflows (stall > 15, wait mask > 63, reuse > 15): the encoders refuse it. def sm90ControlBits = (lambda unrestricted control : SM86Control . eliminate SM86Control (lambda unrestricted current : SM86Control . Nat) control (branch SM86ControlValue stall yieldMode writeBarrier readBarrier waitMask reuseMask . let unrestricted stallBits = (byte-to-nat stall) in let unrestricted waitBits = (byte-to-nat waitMask) in let unrestricted reuseBits = (byte-to-nat reuseMask) in naturalSelect (nest naturalAnd (nat-less-than stallBits 16) (nat-less-than waitBits 64) (nat-less-than reuseBits 16)) (nest nat-add stallBits (nat-multiply 16 (eliminate SM86YieldMode (lambda unrestricted current : SM86YieldMode . Nat) yieldMode (branch SM86Continue . 0) (branch SM86Yield . 1))) (nat-multiply 32 (sm90BarrierCode writeBarrier)) (nat-multiply 256 (sm90BarrierCode readBarrier)) (nat-multiply 2048 waitBits) (nat-multiply 131072 reuseBits)) sm90ControlSpan)) def sm90ControlOf = (lambda unrestricted bits : Nat . constructor SM86Control SM86ControlValue (nat-to-byte (nat-modulo bits 16)) (sm90Choose SM86YieldMode (nat-modulo (nat-divide bits 16) 2) SM86Yield SM86Continue) (pipe bits (nat-divide 32) (nat-modulo 8) sm90BarrierOf) (pipe bits (nat-divide 256) (nat-modulo 8) sm90BarrierOf) (pipe bits (nat-divide 2048) (nat-modulo 64) nat-to-byte) (pipe bits (nat-divide 131072) (nat-modulo 16) nat-to-byte)) -- The header's share of each half: opcode and guard in the low half, the -- control in the high half. An encoder adds its operand fields to these. def sm90HeaderLow = (lambda unrestricted opcode : Nat . unrestricted guard : SM86InstructionGuard . nat-add opcode (sm90Put (sm90GuardBits guard) sm90GuardPlace)) def sm90HeaderHigh = (lambda unrestricted control : SM86Control . sm90Put (sm90ControlBits control) sm90ControlPlace) def sm90OpcodeOf = (lambda unrestricted word : SM90Word . nat-modulo (sm90WordLowOf word) sm90OpcodeSpan) def sm90GuardOfWord = (lambda unrestricted word : SM90Word . sm90GuardOf (sm90Field (sm90WordLowOf word) sm90GuardPlace 16)) def sm90ControlOfWord = (lambda unrestricted word : SM90Word . sm90ControlOf (sm90Field (sm90WordHighOf word) sm90ControlPlace sm90ControlSpan)) -- The high half's bits 62..63 are zero in every word Hopper's assemblers -- emit; a decoder refuses a word with them set rather than drop them. def sm90HighReservedPlace : Nat = (compile-time (sm90Place 62)) -- 1 when the two words are the same 128 bits: the decoders re-encode what -- they read and refuse a word this does not hold for, so a bit outside the -- modelled fields (a reserved bit, a modifier not derived) is never dropped. def sm90WordEqual = (lambda unrestricted left : SM90Word . unrestricted right : SM90Word . naturalAnd (naturalEqual (sm90WordLowOf left) (sm90WordLowOf right)) (naturalEqual (sm90WordHighOf left) (sm90WordHighOf right)))