Source/Packages

Accelerator.SM90.Word

packages/hardware/architectures/nvidia-sm90/src/Accelerator/SM90/Word.alpha

298 lines36 declarations11.0 KiBSHA-256 987e7728cc4a

Complete file · line 89

Word.alpha

Definition view
1module Accelerator.SM90.Word
2
3import Accelerator.SM86.Control
4import Accelerator.SM86.Instruction
5import Accelerator.SM86.Types
6import Data.Bytes
7import Model.Config
8import Model.Parameter
9import Model.Word64
10import Std.Natural
11
12-- One Hopper (sm_90) instruction: 128 bits, held as its two little-endian
13-- 64-bit halves exactly as nvdisasm -hex prints them (the first /* 0x... */
14-- is `low`, bits 0..63; the second is `high`, bits 64..127).  Every SM90
15-- encoder builds one of these from typed operands and every SM90 decoder
16-- reads one back, so a round-trip law compares two typed values and an
17-- oracle-agreement law compares two pairs of naturals.
18--
19-- Fields are addressed by the place value of their lowest bit (2^bit within
20-- the half that holds them) and the number of values they hold (2^width):
21-- the arithmetic is the runtime's binary naturals (nat-divide, nat-modulo,
22-- nat-multiply), never a unary loop and never a byte-at-a-time builder.
23-- A field that straddles the two halves (BRA's offset, bits 34..81) is two
24-- fields, one per half.
25--
26-- The header every Volta-and-later word shares -- opcode bits 0..11, guard
27-- predicate 12..14 and its negation 15, the scheduling control 105..125 --
28-- is the same on sm_90 as on sm_86: measured on every distinct word of the
29-- GPT-2 trainer and initialization programs (nvdisasm -b SM86 against
30-- -b SM90, CUDA 12.4, comparisons/h100/README.md) and by the oracle laws of
31-- Accelerator.SM90.InstructionEncodingProof (CUDA 12.9).  So the operand
32-- vocabulary is Accelerator.SM86's (registers, predicates, guards,
33-- controls); only the opcode-specific bits are Hopper's own.
34family SM90Word : Type 0
35constructor SM90WordValue
36field unrestricted sm90WordLow : Nat
37field unrestricted sm90WordHigh : Nat
38
39end-family
40
41def sm90Word =
42  (lambda unrestricted low : Nat . unrestricted high : Nat .
43    constructor SM90Word SM90WordValue low high)
44
45def sm90WordLowOf =
46  (lambda unrestricted word : SM90Word .
47    eliminate
48      SM90Word
49      (lambda unrestricted current : SM90Word . Nat)
50      word
51      (branch SM90WordValue low high . low))
52
53def sm90WordHighOf =
54  (lambda unrestricted word : SM90Word .
55    eliminate
56      SM90Word
57      (lambda unrestricted current : SM90Word . Nat)
58      word
59      (branch SM90WordValue low high . high))
60
61-- A field of one 64-bit half: its place value (2^lowest bit) and span
62-- (2^width).
63def sm90Field =
64  (lambda unrestricted half : Nat . unrestricted place : Nat . unrestricted span : Nat .
65    nat-modulo (nat-divide half place) span)
66
67-- The value placed in a field (the caller has checked that it fits).
68def sm90Put =
69  (lambda unrestricted value : Nat . unrestricted place : Nat . nat-multiply value place)
70
71def sm90Place =
72  (lambda unrestricted bit : Nat . naturalPowerOfTwo bit)
73
74def sm90Span4 : Nat =
75  16
76
77def sm90Span8 : Nat =
78  256
79
80def sm90Span32 : Nat =
81  (compile-time (sm90Place 32))
82
83-- 1 when `value` < `span`: the encoders refuse a field that does not fit
84-- rather than truncate it.
85def sm90Fits =
86  (lambda unrestricted value : Nat . unrestricted span : Nat . nat-less-than value span)
87
88-- The sixteen bytes the device reads, low half first, each little-endian.
89def sm90WordBytes =
90  (lambda unrestricted word : SM90Word .
91    then (bytes-append (dataBytesWord64LE (modelWord64FromNaturalTruncated (sm90WordLowOf word))))
92      dataBytesWord64LE
93      modelWord64FromNaturalTruncated
94      sm90WordHighOf
95      word)
96
97-- A choice between two values of any type on a 0/1 flag (nat-less-than's
98-- result): the decoders select typed operands with it.
99def sm90Choose =
100  (lambda erased valueType : Type 0 .
101    unrestricted flag : Nat .
102    unrestricted whenTrue : valueType .
103    unrestricted whenFalse : valueType .
104    nat-eliminate
105      (lambda unrestricted current : Nat . valueType)
106      whenFalse
107      (lambda unrestricted predecessor : Nat . unrestricted induction : valueType . whenTrue)
108      flag)
109
110-- ---------------------------------------------------------------------------
111-- Registers and predicates
112def sm90RegisterNumber =
113  (lambda unrestricted register : SM86Register .
114    eliminate
115      SM86Register
116      (lambda unrestricted current : SM86Register . Nat)
117      register
118      (branch SM86RegisterValue value . byte-to-nat value))
119
120def sm90RegisterOf =
121  (lambda unrestricted number : Nat .
122    constructor SM86Register SM86RegisterValue (nat-to-byte number))
123
124def sm90PredicateOf =
125  (lambda unrestricted number : Nat .
126    then (sm90Choose SM86Predicate (naturalEqual number 0) SM86Predicate0)
127      (sm90Choose SM86Predicate (naturalEqual number 1) SM86Predicate1)
128      (sm90Choose SM86Predicate (naturalEqual number 2) SM86Predicate2)
129      (sm90Choose SM86Predicate (naturalEqual number 3) SM86Predicate3)
130      (sm90Choose SM86Predicate (naturalEqual number 4) SM86Predicate4)
131      (sm90Choose SM86Predicate (naturalEqual number 5) SM86Predicate5)
132      (sm90Choose SM86Predicate (naturalEqual number 6) SM86Predicate6)
133      SM86PredicateTrue)
134
135-- ---------------------------------------------------------------------------
136-- The header: opcode, guard, control
137def sm90OpcodeSpan : Nat =
138  4096
139
140def sm90GuardPlace : Nat =
141  4096
142
143-- the guard's four bits: predicate (PT = 7 always executes) and negation
144def sm90GuardBits =
145  (lambda unrestricted guard : SM86InstructionGuard .
146    eliminate
147      SM86InstructionGuard
148      (lambda unrestricted current : SM86InstructionGuard . Nat)
149      guard
150      (branch SM86InstructionAlways . 7)
151      (branch SM86InstructionWhen predicate . byte-to-nat (sm86PredicateNumber predicate))
152      (branch
153        SM86InstructionWhenNot
154        predicate
155        .
156        nat-add 8 (byte-to-nat (sm86PredicateNumber predicate))))
157
158-- PT, not negated, is "always"; !PT never executes and is no guard an
159-- SM86-shaped program writes, but it decodes to the typed form the bits say.
160def sm90GuardOf =
161  (lambda unrestricted bits : Nat .
162    then (sm90Choose SM86InstructionGuard (naturalEqual bits 7) SM86InstructionAlways)
163      (sm90Choose
164        SM86InstructionGuard
165        (nat-less-than bits 8)
166        (constructor SM86InstructionGuard SM86InstructionWhen (sm90PredicateOf bits)))
167      (constructor SM86InstructionGuard SM86InstructionWhenNot)
168      sm90PredicateOf
169      (nat-subtract bits)
170      8)
171
172-- The scheduling control, bits 105..125 (high bits 41..61): stall (4),
173-- yield (1), write scoreboard (3), read scoreboard (3), wait mask (6),
174-- operand reuse (4) -- Accelerator.SM86.ControlEncoding's layout.
175def sm90ControlPlace : Nat =
176  (compile-time (sm90Place 41))
177
178def sm90ControlSpan : Nat =
179  (compile-time (sm90Place 21))
180
181def sm90BarrierCode =
182  (lambda unrestricted barrier : SM86Barrier .
183    eliminate
184      SM86Barrier
185      (lambda unrestricted current : SM86Barrier . Nat)
186      barrier
187      (branch SM86Barrier0 . 0)
188      (branch SM86Barrier1 . 1)
189      (branch SM86Barrier2 . 2)
190      (branch SM86Barrier3 . 3)
191      (branch SM86Barrier4 . 4)
192      (branch SM86Barrier5 . 5)
193      (branch SM86Barrier6 . 6)
194      (branch SM86BarrierNone . 7))
195
196def sm90BarrierOf =
197  (lambda unrestricted code : Nat .
198    then (sm90Choose SM86Barrier (naturalEqual code 0) SM86Barrier0)
199      (sm90Choose SM86Barrier (naturalEqual code 1) SM86Barrier1)
200      (sm90Choose SM86Barrier (naturalEqual code 2) SM86Barrier2)
201      (sm90Choose SM86Barrier (naturalEqual code 3) SM86Barrier3)
202      (sm90Choose SM86Barrier (naturalEqual code 4) SM86Barrier4)
203      (sm90Choose SM86Barrier (naturalEqual code 5) SM86Barrier5)
204      (sm90Choose SM86Barrier (naturalEqual code 6) SM86Barrier6)
205      SM86BarrierNone)
206
207-- The control's 21 bits, or a value at or above the span when a field
208-- overflows (stall > 15, wait mask > 63, reuse > 15): the encoders refuse it.
209def sm90ControlBits =
210  (lambda unrestricted control : SM86Control .
211    eliminate
212      SM86Control
213      (lambda unrestricted current : SM86Control . Nat)
214      control
215      (branch
216        SM86ControlValue
217        stall
218        yieldMode
219        writeBarrier
220        readBarrier
221        waitMask
222        reuseMask
223        .
224        let unrestricted stallBits =
225          (byte-to-nat stall)
226          in
227          let unrestricted waitBits =
228            (byte-to-nat waitMask)
229            in
230            let unrestricted reuseBits =
231              (byte-to-nat reuseMask)
232              in
233              naturalSelect
234                (nest naturalAnd
235                  (nat-less-than stallBits 16)
236                  (nat-less-than waitBits 64)
237                  (nat-less-than reuseBits 16))
238                (nest nat-add
239                  stallBits
240                  (nat-multiply
241                    16
242                    (eliminate
243                      SM86YieldMode
244                      (lambda unrestricted current : SM86YieldMode . Nat)
245                      yieldMode
246                      (branch SM86Continue . 0)
247                      (branch SM86Yield . 1)))
248                  (nat-multiply 32 (sm90BarrierCode writeBarrier))
249                  (nat-multiply 256 (sm90BarrierCode readBarrier))
250                  (nat-multiply 2048 waitBits)
251                  (nat-multiply 131072 reuseBits))
252                sm90ControlSpan))
253
254def sm90ControlOf =
255  (lambda unrestricted bits : Nat .
256    constructor
257      SM86Control
258      SM86ControlValue
259      (nat-to-byte (nat-modulo bits 16))
260      (sm90Choose SM86YieldMode (nat-modulo (nat-divide bits 16) 2) SM86Yield SM86Continue)
261      (pipe bits (nat-divide 32) (nat-modulo 8) sm90BarrierOf)
262      (pipe bits (nat-divide 256) (nat-modulo 8) sm90BarrierOf)
263      (pipe bits (nat-divide 2048) (nat-modulo 64) nat-to-byte)
264      (pipe bits (nat-divide 131072) (nat-modulo 16) nat-to-byte))
265
266-- The header's share of each half: opcode and guard in the low half, the
267-- control in the high half.  An encoder adds its operand fields to these.
268def sm90HeaderLow =
269  (lambda unrestricted opcode : Nat . unrestricted guard : SM86InstructionGuard .
270    nat-add opcode (sm90Put (sm90GuardBits guard) sm90GuardPlace))
271
272def sm90HeaderHigh =
273  (lambda unrestricted control : SM86Control . sm90Put (sm90ControlBits control) sm90ControlPlace)
274
275def sm90OpcodeOf =
276  (lambda unrestricted word : SM90Word . nat-modulo (sm90WordLowOf word) sm90OpcodeSpan)
277
278def sm90GuardOfWord =
279  (lambda unrestricted word : SM90Word .
280    sm90GuardOf (sm90Field (sm90WordLowOf word) sm90GuardPlace 16))
281
282def sm90ControlOfWord =
283  (lambda unrestricted word : SM90Word .
284    sm90ControlOf (sm90Field (sm90WordHighOf word) sm90ControlPlace sm90ControlSpan))
285
286-- The high half's bits 62..63 are zero in every word Hopper's assemblers
287-- emit; a decoder refuses a word with them set rather than drop them.
288def sm90HighReservedPlace : Nat =
289  (compile-time (sm90Place 62))
290
291-- 1 when the two words are the same 128 bits: the decoders re-encode what
292-- they read and refuse a word this does not hold for, so a bit outside the
293-- modelled fields (a reserved bit, a modifier not derived) is never dropped.
294def sm90WordEqual =
295  (lambda unrestricted left : SM90Word . unrestricted right : SM90Word .
296    naturalAnd
297      (naturalEqual (sm90WordLowOf left) (sm90WordLowOf right))
298      (naturalEqual (sm90WordHighOf left) (sm90WordHighOf right)))

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.