decoder_masked_multihead_attention_utils.h 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  1. // Downloaded from from FasterTransformer v5.2.1
  2. // https://github.com/NVIDIA/FasterTransformer/blob/release/v5.2.1_tag/src/fastertransformer/kernels/decoder_masked_multihead_attention_utils.h
  3. /*
  4. * Copyright (c) 2020-2022, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #pragma once
  19. #include "cuda_bf16_wrapper.h"
  20. #include "cuda_bf16_fallbacks.cuh"
  21. #include <stdint.h>
  22. using namespace fastertransformer;
  23. namespace mmha {
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////
  25. struct Float8_ {
  26. float2 x;
  27. float2 y;
  28. float2 z;
  29. float2 w;
  30. };
  31. ////////////////////////////////////////////////////////////////////////////////////////////////////
  32. struct Float4_ {
  33. float2 x;
  34. float2 y;
  35. };
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////
  37. #ifdef ENABLE_BF16
  38. struct bf16_4_t {
  39. __nv_bfloat162 x;
  40. __nv_bfloat162 y;
  41. };
  42. ////////////////////////////////////////////////////////////////////////////////////////////////////
  43. struct bf16_8_t {
  44. __nv_bfloat162 x;
  45. __nv_bfloat162 y;
  46. __nv_bfloat162 z;
  47. __nv_bfloat162 w;
  48. };
  49. #endif
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////
  51. template<typename T>
  52. struct num_elems;
  53. template<>
  54. struct num_elems<float> {
  55. static constexpr int value = 1;
  56. };
  57. template<>
  58. struct num_elems<float2> {
  59. static constexpr int value = 2;
  60. };
  61. template<>
  62. struct num_elems<float4> {
  63. static constexpr int value = 4;
  64. };
  65. template<>
  66. struct num_elems<Float4_> {
  67. static constexpr int value = 4;
  68. };
  69. template<>
  70. struct num_elems<Float8_> {
  71. static constexpr int value = 8;
  72. };
  73. template<>
  74. struct num_elems<uint32_t> {
  75. static constexpr int value = 2;
  76. };
  77. template<>
  78. struct num_elems<uint2> {
  79. static constexpr int value = 4;
  80. };
  81. template<>
  82. struct num_elems<uint4> {
  83. static constexpr int value = 8;
  84. };
  85. #ifdef ENABLE_BF16
  86. template<>
  87. struct num_elems<__nv_bfloat162> {
  88. static constexpr int value = 2;
  89. };
  90. template<>
  91. struct num_elems<bf16_4_t> {
  92. static constexpr int value = 4;
  93. };
  94. template<>
  95. struct num_elems<bf16_8_t> {
  96. static constexpr int value = 8;
  97. };
  98. #endif
  99. ////////////////////////////////////////////////////////////////////////////////////////////////////
  100. template<typename T, int N>
  101. struct packed_type;
  102. template<typename T>
  103. struct packed_type<T, 1> {
  104. using type = T;
  105. };
  106. template<>
  107. struct packed_type<int8_t, 2> {
  108. using type = int16_t;
  109. };
  110. template<>
  111. struct packed_type<int8_t, 4> {
  112. using type = int32_t;
  113. };
  114. template<>
  115. struct packed_type<int8_t, 8> {
  116. using type = int64_t;
  117. };
  118. template<>
  119. struct packed_type<float, 2> {
  120. using type = float2;
  121. };
  122. template<>
  123. struct packed_type<float, 4> {
  124. using type = float4;
  125. };
  126. template<>
  127. struct packed_type<float, 8> {
  128. using type = Float8_;
  129. };
  130. ////////////////////////////////////////////////////////////////////////////////////////////////////
  131. inline __device__ float add(float a, float b)
  132. {
  133. return a + b;
  134. }
  135. ////////////////////////////////////////////////////////////////////////////////////////////////////
  136. inline __device__ float2 add(float2 a, float2 b)
  137. {
  138. float2 c;
  139. c.x = add(a.x, b.x);
  140. c.y = add(a.y, b.y);
  141. return c;
  142. }
  143. ////////////////////////////////////////////////////////////////////////////////////////////////////
  144. inline __device__ float4 add(float4 a, float4 b)
  145. {
  146. float4 c;
  147. c.x = add(a.x, b.x);
  148. c.y = add(a.y, b.y);
  149. c.z = add(a.z, b.z);
  150. c.w = add(a.w, b.w);
  151. return c;
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////////////////////////
  154. #ifdef ENABLE_BF16
  155. inline __device__ __nv_bfloat16 add(__nv_bfloat16 a, __nv_bfloat16 b)
  156. {
  157. return a + b;
  158. }
  159. ////////////////////////////////////////////////////////////////////////////////////////////////////
  160. inline __device__ __nv_bfloat162 add(__nv_bfloat162 a, __nv_bfloat162 b)
  161. {
  162. return bf16hadd2(a, b);
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////////////////////////
  165. inline __device__ bf16_4_t add(bf16_4_t a, bf16_4_t b)
  166. {
  167. bf16_4_t c;
  168. c.x = add(a.x, b.x);
  169. c.y = add(a.y, b.y);
  170. return c;
  171. }
  172. ////////////////////////////////////////////////////////////////////////////////////////////////////
  173. inline __device__ bf16_8_t add(bf16_8_t a, bf16_8_t b)
  174. {
  175. bf16_8_t c;
  176. c.x = add(a.x, b.x);
  177. c.y = add(a.y, b.y);
  178. c.z = add(a.z, b.z);
  179. c.w = add(a.w, b.w);
  180. return c;
  181. }
  182. #endif // ENABLE_BF16
  183. ////////////////////////////////////////////////////////////////////////////////////////////////////
  184. inline __device__ uint16_t add(uint16_t a, uint16_t b)
  185. {
  186. uint16_t c;
  187. asm volatile("add.f16 %0, %1, %2;\n" : "=h"(c) : "h"(a), "h"(b));
  188. return c;
  189. }
  190. ////////////////////////////////////////////////////////////////////////////////////////////////////
  191. inline __device__ uint32_t add(uint32_t a, uint32_t b)
  192. {
  193. uint32_t c;
  194. asm volatile("add.f16x2 %0, %1, %2;\n" : "=r"(c) : "r"(a), "r"(b));
  195. return c;
  196. }
  197. ////////////////////////////////////////////////////////////////////////////////////////////////////
  198. inline __device__ uint2 add(uint2 a, uint2 b)
  199. {
  200. uint2 c;
  201. c.x = add(a.x, b.x);
  202. c.y = add(a.y, b.y);
  203. return c;
  204. }
  205. ////////////////////////////////////////////////////////////////////////////////////////////////////
  206. inline __device__ uint4 add(uint4 a, uint4 b)
  207. {
  208. uint4 c;
  209. c.x = add(a.x, b.x);
  210. c.y = add(a.y, b.y);
  211. c.z = add(a.z, b.z);
  212. c.w = add(a.w, b.w);
  213. return c;
  214. }
  215. ////////////////////////////////////////////////////////////////////////////////////////////////////
  216. inline __device__ uint16_t float_to_half(float f)
  217. {
  218. union {
  219. uint32_t u32;
  220. uint16_t u16[2];
  221. } tmp;
  222. #if 0 && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800 // Is it better?
  223. float zero = 0.f;
  224. asm volatile("cvt.rn.f16x2.f32 %0, %1, %2;\n" : "=r"(tmp.u32) : "f"(zero), "f"(f));
  225. #else
  226. asm volatile("cvt.rn.f16.f32 %0, %1;\n" : "=h"(tmp.u16[0]) : "f"(f));
  227. #endif
  228. return tmp.u16[0];
  229. }
  230. ////////////////////////////////////////////////////////////////////////////////////////////////////
  231. inline __device__ uint32_t float2_to_half2(float2 f)
  232. {
  233. union {
  234. uint32_t u32;
  235. uint16_t u16[2];
  236. } tmp;
  237. #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
  238. asm volatile("cvt.rn.f16x2.f32 %0, %1, %2;\n" : "=r"(tmp.u32) : "f"(f.y), "f"(f.x));
  239. #else
  240. asm volatile("cvt.rn.f16.f32 %0, %1;\n" : "=h"(tmp.u16[0]) : "f"(f.x));
  241. asm volatile("cvt.rn.f16.f32 %0, %1;\n" : "=h"(tmp.u16[1]) : "f"(f.y));
  242. #endif
  243. return tmp.u32;
  244. }
  245. ////////////////////////////////////////////////////////////////////////////////////////////////////
  246. inline __device__ float half_to_float(uint16_t h)
  247. {
  248. float f;
  249. asm volatile("cvt.f32.f16 %0, %1;\n" : "=f"(f) : "h"(h));
  250. return f;
  251. }
  252. ////////////////////////////////////////////////////////////////////////////////////////////////////
  253. inline __device__ float2 half2_to_float2(uint32_t v)
  254. {
  255. uint16_t lo, hi;
  256. asm volatile("mov.b32 {%0, %1}, %2;\n" : "=h"(lo), "=h"(hi) : "r"(v));
  257. return make_float2(half_to_float(lo), half_to_float(hi));
  258. }
  259. ////////////////////////////////////////////////////////////////////////////////////////////////////
  260. inline __device__ float add(float a, uint16_t b)
  261. {
  262. return a + half_to_float(b);
  263. }
  264. ////////////////////////////////////////////////////////////////////////////////////////////////////
  265. #ifdef ENABLE_BF16
  266. inline __device__ float add(float a, __nv_bfloat16 b)
  267. {
  268. return a + __bfloat162float(b);
  269. }
  270. #endif
  271. ////////////////////////////////////////////////////////////////////////////////////////////////////
  272. inline __device__ float2 add(uint32_t a, float2 fb)
  273. {
  274. float2 fa = half2_to_float2(a);
  275. return add(fa, fb);
  276. }
  277. ////////////////////////////////////////////////////////////////////////////////////////////////////
  278. inline __device__ Float4_ add(uint2 a, Float4_ fb)
  279. {
  280. Float4_ fc;
  281. fc.x = add(a.x, fb.x);
  282. fc.y = add(a.y, fb.y);
  283. return fc;
  284. }
  285. ////////////////////////////////////////////////////////////////////////////////////////////////////
  286. inline __device__ Float8_ add(uint4 a, Float8_ fb)
  287. {
  288. Float8_ fc;
  289. fc.x = add(a.x, fb.x);
  290. fc.y = add(a.y, fb.y);
  291. fc.z = add(a.z, fb.z);
  292. fc.w = add(a.w, fb.w);
  293. return fc;
  294. }
  295. ////////////////////////////////////////////////////////////////////////////////////////////////////
  296. inline __device__ uint32_t h0_h0(uint16_t a)
  297. {
  298. uint32_t b;
  299. asm volatile("mov.b32 %0, {%1, %1};" : "=r"(b) : "h"(a));
  300. return b;
  301. }
  302. ////////////////////////////////////////////////////////////////////////////////////////////////////
  303. inline __device__ float fma(float a, float b, float c)
  304. {
  305. return a * b + c;
  306. }
  307. ////////////////////////////////////////////////////////////////////////////////////////////////////
  308. inline __device__ float2 fma(float2 a, float2 b, float2 c)
  309. {
  310. float2 d;
  311. d.x = fma(a.x, b.x, c.x);
  312. d.y = fma(a.y, b.y, c.y);
  313. return d;
  314. }
  315. ////////////////////////////////////////////////////////////////////////////////////////////////////
  316. inline __device__ float2 fma(float a, float2 b, float2 c)
  317. {
  318. float2 d;
  319. d.x = fma(a, b.x, c.x);
  320. d.y = fma(a, b.y, c.y);
  321. return d;
  322. }
  323. ////////////////////////////////////////////////////////////////////////////////////////////////////
  324. inline __device__ float4 fma(float4 a, float4 b, float4 c)
  325. {
  326. float4 d;
  327. d.x = fma(a.x, b.x, c.x);
  328. d.y = fma(a.y, b.y, c.y);
  329. d.z = fma(a.z, b.z, c.z);
  330. d.w = fma(a.w, b.w, c.w);
  331. return d;
  332. }
  333. ////////////////////////////////////////////////////////////////////////////////////////////////////
  334. inline __device__ float4 fma(float a, float4 b, float4 c)
  335. {
  336. float4 d;
  337. d.x = fma(a, b.x, c.x);
  338. d.y = fma(a, b.y, c.y);
  339. d.z = fma(a, b.z, c.z);
  340. d.w = fma(a, b.w, c.w);
  341. return d;
  342. }
  343. ////////////////////////////////////////////////////////////////////////////////////////////////////
  344. inline __device__ Float4_ fma(float a, Float4_ b, Float4_ c)
  345. {
  346. Float4_ d;
  347. d.x = fma(a, b.x, c.x);
  348. d.y = fma(a, b.y, c.y);
  349. return d;
  350. }
  351. ////////////////////////////////////////////////////////////////////////////////////////////////////
  352. inline __device__ Float8_ fma(float a, Float8_ b, Float8_ c)
  353. {
  354. Float8_ d;
  355. d.x = fma(a, b.x, c.x);
  356. d.y = fma(a, b.y, c.y);
  357. d.z = fma(a, b.z, c.z);
  358. d.w = fma(a, b.w, c.w);
  359. return d;
  360. }
  361. ////////////////////////////////////////////////////////////////////////////////////////////////////
  362. #ifdef ENABLE_BF16
  363. inline __device__ float2 add(__nv_bfloat162 a, float2 fb)
  364. {
  365. float2 fa = bf1622float2(a);
  366. return add(fa, fb);
  367. }
  368. ////////////////////////////////////////////////////////////////////////////////////////////////////
  369. inline __device__ Float4_ add(bf16_4_t a, Float4_ fb)
  370. {
  371. Float4_ fc;
  372. fc.x = add(a.x, fb.x);
  373. fc.y = add(a.y, fb.y);
  374. return fc;
  375. }
  376. ////////////////////////////////////////////////////////////////////////////////////////////////////
  377. inline __device__ Float8_ add(bf16_8_t a, Float8_ fb)
  378. {
  379. Float8_ fc;
  380. fc.x = add(a.x, fb.x);
  381. fc.y = add(a.y, fb.y);
  382. fc.z = add(a.z, fb.z);
  383. fc.w = add(a.w, fb.w);
  384. return fc;
  385. }
  386. #endif // ENABLE_BF16
  387. ////////////////////////////////////////////////////////////////////////////////////////////////////
  388. inline __device__ uint32_t fma(uint32_t a, uint32_t b, uint32_t c)
  389. {
  390. uint32_t d;
  391. asm volatile("fma.rn.f16x2 %0, %1, %2, %3;\n" : "=r"(d) : "r"(a), "r"(b), "r"(c));
  392. return d;
  393. }
  394. ////////////////////////////////////////////////////////////////////////////////////////////////////
  395. inline __device__ uint32_t fma(uint16_t a, uint32_t b, uint32_t c)
  396. {
  397. return fma(h0_h0(a), b, c);
  398. }
  399. ////////////////////////////////////////////////////////////////////////////////////////////////////
  400. inline __device__ uint2 fma(uint2 a, uint2 b, uint2 c)
  401. {
  402. uint2 d;
  403. d.x = fma(a.x, b.x, c.x);
  404. d.y = fma(a.y, b.y, c.y);
  405. return d;
  406. }
  407. ////////////////////////////////////////////////////////////////////////////////////////////////////
  408. inline __device__ uint2 fma(uint16_t a, uint2 b, uint2 c)
  409. {
  410. uint32_t s = h0_h0(a);
  411. uint2 d;
  412. d.x = fma(s, b.x, c.x);
  413. d.y = fma(s, b.y, c.y);
  414. return d;
  415. }
  416. ////////////////////////////////////////////////////////////////////////////////////////////////////
  417. inline __device__ uint4 fma(uint4 a, uint4 b, uint4 c)
  418. {
  419. uint4 d;
  420. d.x = fma(a.x, b.x, c.x);
  421. d.y = fma(a.y, b.y, c.y);
  422. d.z = fma(a.z, b.z, c.z);
  423. d.w = fma(a.w, b.w, c.w);
  424. return d;
  425. }
  426. ////////////////////////////////////////////////////////////////////////////////////////////////////
  427. inline __device__ uint4 fma(uint16_t a, uint4 b, uint4 c)
  428. {
  429. uint32_t s = h0_h0(a);
  430. uint4 d;
  431. d.x = fma(s, b.x, c.x);
  432. d.y = fma(s, b.y, c.y);
  433. d.z = fma(s, b.z, c.z);
  434. d.w = fma(s, b.w, c.w);
  435. return d;
  436. }
  437. ////////////////////////////////////////////////////////////////////////////////////////////////////
  438. inline __device__ float fma(uint16_t a, uint16_t b, float fc)
  439. {
  440. float fa = half_to_float(a);
  441. float fb = half_to_float(b);
  442. return fa * fb + fc;
  443. }
  444. ////////////////////////////////////////////////////////////////////////////////////////////////////
  445. inline __device__ float2 fma(uint32_t a, uint32_t b, float2 fc)
  446. {
  447. float2 fa = half2_to_float2(a);
  448. float2 fb = half2_to_float2(b);
  449. return fma(fa, fb, fc);
  450. }
  451. ////////////////////////////////////////////////////////////////////////////////////////////////////
  452. inline __device__ float2 fma(uint16_t a, uint32_t b, float2 fc)
  453. {
  454. return fma(h0_h0(a), b, fc);
  455. }
  456. ////////////////////////////////////////////////////////////////////////////////////////////////////
  457. inline __device__ Float4_ fma(uint2 a, uint2 b, Float4_ fc)
  458. {
  459. Float4_ fd;
  460. fd.x = fma(a.x, b.x, fc.x);
  461. fd.y = fma(a.y, b.y, fc.y);
  462. return fd;
  463. }
  464. ////////////////////////////////////////////////////////////////////////////////////////////////////
  465. inline __device__ Float4_ fma(uint16_t a, uint2 b, Float4_ fc)
  466. {
  467. uint32_t s = h0_h0(a);
  468. Float4_ fd;
  469. fd.x = fma(s, b.x, fc.x);
  470. fd.y = fma(s, b.y, fc.y);
  471. return fd;
  472. }
  473. ////////////////////////////////////////////////////////////////////////////////////////////////////
  474. inline __device__ Float8_ fma(uint4 a, uint4 b, Float8_ fc)
  475. {
  476. Float8_ fd;
  477. fd.x = fma(a.x, b.x, fc.x);
  478. fd.y = fma(a.y, b.y, fc.y);
  479. fd.z = fma(a.z, b.z, fc.z);
  480. fd.w = fma(a.w, b.w, fc.w);
  481. return fd;
  482. }
  483. ////////////////////////////////////////////////////////////////////////////////////////////////////
  484. inline __device__ Float8_ fma(uint16_t a, uint4 b, Float8_ fc)
  485. {
  486. uint32_t s = h0_h0(a);
  487. Float8_ fd;
  488. fd.x = fma(s, b.x, fc.x);
  489. fd.y = fma(s, b.y, fc.y);
  490. fd.z = fma(s, b.z, fc.z);
  491. fd.w = fma(s, b.w, fc.w);
  492. return fd;
  493. }
  494. ////////////////////////////////////////////////////////////////////////////////////////////////////
  495. #ifdef ENABLE_BF16
  496. inline __device__ __nv_bfloat162 fma(__nv_bfloat162 a, __nv_bfloat162 b, __nv_bfloat162 c)
  497. {
  498. return bf16hfma2(a, b, c);
  499. }
  500. ////////////////////////////////////////////////////////////////////////////////////////////////////
  501. inline __device__ __nv_bfloat162 fma(__nv_bfloat16 a, __nv_bfloat162 b, __nv_bfloat162 c)
  502. {
  503. return bf16hfma2(bf162bf162(a), b, c);
  504. }
  505. ////////////////////////////////////////////////////////////////////////////////////////////////////
  506. inline __device__ bf16_4_t fma(bf16_4_t a, bf16_4_t b, bf16_4_t c)
  507. {
  508. bf16_4_t d;
  509. d.x = fma(a.x, b.x, c.x);
  510. d.y = fma(a.y, b.y, c.y);
  511. return d;
  512. }
  513. ////////////////////////////////////////////////////////////////////////////////////////////////////
  514. inline __device__ bf16_4_t fma(__nv_bfloat16 a, bf16_4_t b, bf16_4_t c)
  515. {
  516. __nv_bfloat162 s = bf162bf162(a);
  517. bf16_4_t d;
  518. d.x = fma(s, b.x, c.x);
  519. d.y = fma(s, b.y, c.y);
  520. return d;
  521. }
  522. ////////////////////////////////////////////////////////////////////////////////////////////////////
  523. inline __device__ bf16_8_t fma(bf16_8_t a, bf16_8_t b, bf16_8_t c)
  524. {
  525. bf16_8_t d;
  526. d.x = fma(a.x, b.x, c.x);
  527. d.y = fma(a.y, b.y, c.y);
  528. d.z = fma(a.z, b.z, c.z);
  529. d.w = fma(a.w, b.w, c.w);
  530. return d;
  531. }
  532. ////////////////////////////////////////////////////////////////////////////////////////////////////
  533. inline __device__ bf16_8_t fma(__nv_bfloat16 a, bf16_8_t b, bf16_8_t c)
  534. {
  535. __nv_bfloat162 s = bf162bf162(a);
  536. bf16_8_t d;
  537. d.x = fma(s, b.x, c.x);
  538. d.y = fma(s, b.y, c.y);
  539. d.z = fma(s, b.z, c.z);
  540. d.w = fma(s, b.w, c.w);
  541. return d;
  542. }
  543. ////////////////////////////////////////////////////////////////////////////////////////////////////
  544. inline __device__ float fma(__nv_bfloat16 a, __nv_bfloat16 b, float fc)
  545. {
  546. return __bfloat162float(a) * __bfloat162float(b) + fc;
  547. }
  548. ////////////////////////////////////////////////////////////////////////////////////////////////////
  549. inline __device__ float2 fma(__nv_bfloat162 a, __nv_bfloat162 b, float2 fc)
  550. {
  551. float2 fa = bf1622float2(a);
  552. float2 fb = bf1622float2(b);
  553. return fma(fa, fb, fc);
  554. }
  555. ////////////////////////////////////////////////////////////////////////////////////////////////////
  556. inline __device__ float2 fma(__nv_bfloat16 a, __nv_bfloat162 b, float2 fc)
  557. {
  558. return fma(bf162bf162(a), b, fc);
  559. }
  560. ////////////////////////////////////////////////////////////////////////////////////////////////////
  561. inline __device__ Float4_ fma(bf16_4_t a, bf16_4_t b, Float4_ fc)
  562. {
  563. Float4_ fd;
  564. fd.x = fma(a.x, b.x, fc.x);
  565. fd.y = fma(a.y, b.y, fc.y);
  566. return fd;
  567. }
  568. ////////////////////////////////////////////////////////////////////////////////////////////////////
  569. inline __device__ Float4_ fma(__nv_bfloat16 a, bf16_4_t b, Float4_ fc)
  570. {
  571. __nv_bfloat162 s = bf162bf162(a);
  572. Float4_ fd;
  573. fd.x = fma(s, b.x, fc.x);
  574. fd.y = fma(s, b.y, fc.y);
  575. return fd;
  576. }
  577. ////////////////////////////////////////////////////////////////////////////////////////////////////
  578. inline __device__ Float8_ fma(bf16_8_t a, bf16_8_t b, Float8_ fc)
  579. {
  580. Float8_ fd;
  581. fd.x = fma(a.x, b.x, fc.x);
  582. fd.y = fma(a.y, b.y, fc.y);
  583. fd.z = fma(a.z, b.z, fc.z);
  584. fd.w = fma(a.w, b.w, fc.w);
  585. return fd;
  586. }
  587. ////////////////////////////////////////////////////////////////////////////////////////////////////
  588. inline __device__ Float8_ fma(__nv_bfloat16 a, bf16_8_t b, Float8_ fc)
  589. {
  590. __nv_bfloat162 s = bf162bf162(a);
  591. Float8_ fd;
  592. fd.x = fma(s, b.x, fc.x);
  593. fd.y = fma(s, b.y, fc.y);
  594. fd.z = fma(s, b.z, fc.z);
  595. fd.w = fma(s, b.w, fc.w);
  596. return fd;
  597. }
  598. #endif // ENABLE_BF16
  599. ////////////////////////////////////////////////////////////////////////////////////////////////////
  600. template<typename Acc, typename A, typename B>
  601. inline __device__ Acc mul(A a, B b)
  602. {
  603. return a * b;
  604. }
  605. ////////////////////////////////////////////////////////////////////////////////////////////////////
  606. template<>
  607. inline __device__ float mul<float, float>(float a, float b)
  608. {
  609. return a * b;
  610. }
  611. ////////////////////////////////////////////////////////////////////////////////////////////////////
  612. template<>
  613. inline __device__ float2 mul(float2 a, float2 b)
  614. {
  615. float2 c;
  616. c.x = a.x * b.x;
  617. c.y = a.y * b.y;
  618. return c;
  619. }
  620. ////////////////////////////////////////////////////////////////////////////////////////////////////
  621. template<>
  622. inline __device__ float2 mul(float a, float2 b)
  623. {
  624. float2 c;
  625. c.x = a * b.x;
  626. c.y = a * b.y;
  627. return c;
  628. }
  629. ////////////////////////////////////////////////////////////////////////////////////////////////////
  630. template<>
  631. inline __device__ float4 mul(float4 a, float4 b)
  632. {
  633. float4 c;
  634. c.x = a.x * b.x;
  635. c.y = a.y * b.y;
  636. c.z = a.z * b.z;
  637. c.w = a.w * b.w;
  638. return c;
  639. }
  640. ////////////////////////////////////////////////////////////////////////////////////////////////////
  641. template<>
  642. inline __device__ float4 mul(float a, float4 b)
  643. {
  644. float4 c;
  645. c.x = a * b.x;
  646. c.y = a * b.y;
  647. c.z = a * b.z;
  648. c.w = a * b.w;
  649. return c;
  650. }
  651. ////////////////////////////////////////////////////////////////////////////////////////////////////
  652. template<>
  653. inline __device__ Float8_ mul(float a, Float8_ b)
  654. {
  655. Float8_ c;
  656. c.x = make_float2(a * b.x.x, a * b.x.y);
  657. c.y = make_float2(a * b.y.x, a * b.y.y);
  658. c.z = make_float2(a * b.z.x, a * b.z.y);
  659. c.w = make_float2(a * b.w.x, a * b.w.y);
  660. return c;
  661. }
  662. ////////////////////////////////////////////////////////////////////////////////////////////////////
  663. template<>
  664. inline __device__ uint16_t mul(uint16_t a, uint16_t b)
  665. {
  666. uint16_t c;
  667. asm volatile("mul.f16 %0, %1, %2;\n" : "=h"(c) : "h"(a), "h"(b));
  668. return c;
  669. }
  670. ////////////////////////////////////////////////////////////////////////////////////////////////////
  671. template<>
  672. inline __device__ uint32_t mul(uint32_t a, uint32_t b)
  673. {
  674. uint32_t c;
  675. asm volatile("mul.f16x2 %0, %1, %2;\n" : "=r"(c) : "r"(a), "r"(b));
  676. return c;
  677. }
  678. ////////////////////////////////////////////////////////////////////////////////////////////////////
  679. template<>
  680. inline __device__ uint32_t mul(uint16_t a, uint32_t b)
  681. {
  682. return mul<uint32_t, uint32_t, uint32_t>(h0_h0(a), b);
  683. }
  684. ////////////////////////////////////////////////////////////////////////////////////////////////////
  685. template<>
  686. inline __device__ uint2 mul(uint2 a, uint2 b)
  687. {
  688. uint2 c;
  689. c.x = mul<uint32_t, uint32_t, uint32_t>(a.x, b.x);
  690. c.y = mul<uint32_t, uint32_t, uint32_t>(a.y, b.y);
  691. return c;
  692. }
  693. ////////////////////////////////////////////////////////////////////////////////////////////////////
  694. template<>
  695. inline __device__ uint2 mul(uint16_t a, uint2 b)
  696. {
  697. uint32_t s = h0_h0(a);
  698. uint2 c;
  699. c.x = mul<uint32_t, uint32_t, uint32_t>(s, b.x);
  700. c.y = mul<uint32_t, uint32_t, uint32_t>(s, b.y);
  701. return c;
  702. }
  703. ////////////////////////////////////////////////////////////////////////////////////////////////////
  704. template<>
  705. inline __device__ uint4 mul(uint4 a, uint4 b)
  706. {
  707. uint4 c;
  708. c.x = mul<uint32_t, uint32_t, uint32_t>(a.x, b.x);
  709. c.y = mul<uint32_t, uint32_t, uint32_t>(a.y, b.y);
  710. c.z = mul<uint32_t, uint32_t, uint32_t>(a.z, b.z);
  711. c.w = mul<uint32_t, uint32_t, uint32_t>(a.w, b.w);
  712. return c;
  713. }
  714. ////////////////////////////////////////////////////////////////////////////////////////////////////
  715. template<>
  716. inline __device__ uint4 mul(uint16_t a, uint4 b)
  717. {
  718. uint32_t s = h0_h0(a);
  719. uint4 c;
  720. c.x = mul<uint32_t, uint32_t, uint32_t>(s, b.x);
  721. c.y = mul<uint32_t, uint32_t, uint32_t>(s, b.y);
  722. c.z = mul<uint32_t, uint32_t, uint32_t>(s, b.z);
  723. c.w = mul<uint32_t, uint32_t, uint32_t>(s, b.w);
  724. return c;
  725. }
  726. ////////////////////////////////////////////////////////////////////////////////////////////////////
  727. template<>
  728. inline __device__ float mul(uint16_t a, uint16_t b)
  729. {
  730. float fa = half_to_float(a);
  731. float fb = half_to_float(b);
  732. return fa * fb;
  733. }
  734. ////////////////////////////////////////////////////////////////////////////////////////////////////
  735. template<>
  736. inline __device__ float mul(uint16_t a, float b)
  737. {
  738. return half_to_float(a) * b;
  739. }
  740. ////////////////////////////////////////////////////////////////////////////////////////////////////
  741. template<>
  742. inline __device__ float2 mul(uint32_t a, uint32_t b)
  743. {
  744. float2 fa = half2_to_float2(a);
  745. float2 fb = half2_to_float2(b);
  746. return mul<float2, float2, float2>(fa, fb);
  747. }
  748. ////////////////////////////////////////////////////////////////////////////////////////////////////
  749. template<>
  750. inline __device__ float2 mul(uint16_t a, uint32_t b)
  751. {
  752. return mul<float2, uint32_t, uint32_t>(h0_h0(a), b);
  753. }
  754. ////////////////////////////////////////////////////////////////////////////////////////////////////
  755. template<>
  756. inline __device__ Float4_ mul(uint2 a, uint2 b)
  757. {
  758. Float4_ fc;
  759. fc.x = mul<float2, uint32_t, uint32_t>(a.x, b.x);
  760. fc.y = mul<float2, uint32_t, uint32_t>(a.y, b.y);
  761. return fc;
  762. }
  763. ////////////////////////////////////////////////////////////////////////////////////////////////////
  764. template<>
  765. inline __device__ Float4_ mul(uint16_t a, uint2 b)
  766. {
  767. uint32_t s = h0_h0(a);
  768. Float4_ fc;
  769. fc.x = mul<float2, uint32_t, uint32_t>(s, b.x);
  770. fc.y = mul<float2, uint32_t, uint32_t>(s, b.y);
  771. return fc;
  772. }
  773. ////////////////////////////////////////////////////////////////////////////////////////////////////
  774. template<>
  775. inline __device__ Float8_ mul(uint4 a, uint4 b)
  776. {
  777. Float8_ fc;
  778. fc.x = mul<float2, uint32_t, uint32_t>(a.x, b.x);
  779. fc.y = mul<float2, uint32_t, uint32_t>(a.y, b.y);
  780. fc.z = mul<float2, uint32_t, uint32_t>(a.z, b.z);
  781. fc.w = mul<float2, uint32_t, uint32_t>(a.w, b.w);
  782. return fc;
  783. }
  784. ////////////////////////////////////////////////////////////////////////////////////////////////////
  785. template<>
  786. inline __device__ Float8_ mul(uint16_t a, uint4 b)
  787. {
  788. uint32_t s = h0_h0(a);
  789. Float8_ fc;
  790. fc.x = mul<float2, uint32_t, uint32_t>(s, b.x);
  791. fc.y = mul<float2, uint32_t, uint32_t>(s, b.y);
  792. fc.z = mul<float2, uint32_t, uint32_t>(s, b.z);
  793. fc.w = mul<float2, uint32_t, uint32_t>(s, b.w);
  794. return fc;
  795. }
  796. ////////////////////////////////////////////////////////////////////////////////////////////////////
  797. #ifdef ENABLE_BF16
  798. template<>
  799. inline __device__ __nv_bfloat16 mul(__nv_bfloat16 a, __nv_bfloat16 b)
  800. {
  801. #if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
  802. return __hmul(a, b);
  803. #else
  804. return bf16hmul(a, b);
  805. #endif
  806. }
  807. ////////////////////////////////////////////////////////////////////////////////////////////////////
  808. template<>
  809. inline __device__ __nv_bfloat162 mul(__nv_bfloat162 a, __nv_bfloat162 b)
  810. {
  811. return bf16hmul2(a, b);
  812. }
  813. ////////////////////////////////////////////////////////////////////////////////////////////////////
  814. template<>
  815. inline __device__ __nv_bfloat162 mul(__nv_bfloat16 a, __nv_bfloat162 b)
  816. {
  817. return mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(bf162bf162(a), b);
  818. }
  819. ////////////////////////////////////////////////////////////////////////////////////////////////////
  820. template<>
  821. inline __device__ bf16_4_t mul(bf16_4_t a, bf16_4_t b)
  822. {
  823. bf16_4_t c;
  824. c.x = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(a.x, b.x);
  825. c.y = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(a.y, b.y);
  826. return c;
  827. }
  828. ////////////////////////////////////////////////////////////////////////////////////////////////////
  829. template<>
  830. inline __device__ bf16_4_t mul(__nv_bfloat16 a, bf16_4_t b)
  831. {
  832. __nv_bfloat162 s = bf162bf162(a);
  833. bf16_4_t c;
  834. c.x = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(s, b.x);
  835. c.y = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(s, b.y);
  836. return c;
  837. }
  838. ////////////////////////////////////////////////////////////////////////////////////////////////////
  839. template<>
  840. inline __device__ bf16_8_t mul(bf16_8_t a, bf16_8_t b)
  841. {
  842. bf16_8_t c;
  843. c.x = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(a.x, b.x);
  844. c.y = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(a.y, b.y);
  845. c.z = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(a.z, b.z);
  846. c.w = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(a.w, b.w);
  847. return c;
  848. }
  849. ////////////////////////////////////////////////////////////////////////////////////////////////////
  850. template<>
  851. inline __device__ bf16_8_t mul(__nv_bfloat16 a, bf16_8_t b)
  852. {
  853. __nv_bfloat162 s = bf162bf162(a);
  854. bf16_8_t c;
  855. c.x = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(s, b.x);
  856. c.y = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(s, b.y);
  857. c.z = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(s, b.z);
  858. c.w = mul<__nv_bfloat162, __nv_bfloat162, __nv_bfloat162>(s, b.w);
  859. return c;
  860. }
  861. ////////////////////////////////////////////////////////////////////////////////////////////////////
  862. template<>
  863. inline __device__ float mul(__nv_bfloat16 a, __nv_bfloat16 b)
  864. {
  865. float fa = (float)a;
  866. float fb = (float)b;
  867. return fa * fb;
  868. }
  869. ////////////////////////////////////////////////////////////////////////////////////////////////////
  870. template<>
  871. inline __device__ float mul(__nv_bfloat16 a, float b)
  872. {
  873. return __bfloat162float(a) * b;
  874. }
  875. ////////////////////////////////////////////////////////////////////////////////////////////////////
  876. template<>
  877. inline __device__ float2 mul(__nv_bfloat162 a, __nv_bfloat162 b)
  878. {
  879. float2 fa = bf1622float2(a);
  880. float2 fb = bf1622float2(b);
  881. return mul<float2, float2, float2>(fa, fb);
  882. }
  883. ////////////////////////////////////////////////////////////////////////////////////////////////////
  884. template<>
  885. inline __device__ float2 mul(__nv_bfloat16 a, __nv_bfloat162 b)
  886. {
  887. return mul<float2, __nv_bfloat162, __nv_bfloat162>(bf162bf162(a), b);
  888. }
  889. ////////////////////////////////////////////////////////////////////////////////////////////////////
  890. template<>
  891. inline __device__ Float4_ mul(bf16_4_t a, bf16_4_t b)
  892. {
  893. Float4_ fc;
  894. fc.x = mul<float2, __nv_bfloat162, __nv_bfloat162>(a.x, b.x);
  895. fc.y = mul<float2, __nv_bfloat162, __nv_bfloat162>(a.y, b.y);
  896. return fc;
  897. }
  898. ////////////////////////////////////////////////////////////////////////////////////////////////////
  899. template<>
  900. inline __device__ Float4_ mul(__nv_bfloat16 a, bf16_4_t b)
  901. {
  902. __nv_bfloat162 s = bf162bf162(a);
  903. Float4_ fc;
  904. fc.x = mul<float2, __nv_bfloat162, __nv_bfloat162>(s, b.x);
  905. fc.y = mul<float2, __nv_bfloat162, __nv_bfloat162>(s, b.y);
  906. return fc;
  907. }
  908. ////////////////////////////////////////////////////////////////////////////////////////////////////
  909. template<>
  910. inline __device__ Float8_ mul(bf16_8_t a, bf16_8_t b)
  911. {
  912. Float8_ fc;
  913. fc.x = mul<float2, __nv_bfloat162, __nv_bfloat162>(a.x, b.x);
  914. fc.y = mul<float2, __nv_bfloat162, __nv_bfloat162>(a.y, b.y);
  915. fc.z = mul<float2, __nv_bfloat162, __nv_bfloat162>(a.z, b.z);
  916. fc.w = mul<float2, __nv_bfloat162, __nv_bfloat162>(a.w, b.w);
  917. return fc;
  918. }
  919. ////////////////////////////////////////////////////////////////////////////////////////////////////
  920. template<>
  921. inline __device__ Float8_ mul(__nv_bfloat16 a, bf16_8_t b)
  922. {
  923. __nv_bfloat162 s = bf162bf162(a);
  924. Float8_ fc;
  925. fc.x = mul<float2, __nv_bfloat162, __nv_bfloat162>(s, b.x);
  926. fc.y = mul<float2, __nv_bfloat162, __nv_bfloat162>(s, b.y);
  927. fc.z = mul<float2, __nv_bfloat162, __nv_bfloat162>(s, b.z);
  928. fc.w = mul<float2, __nv_bfloat162, __nv_bfloat162>(s, b.w);
  929. return fc;
  930. }
  931. #endif // ENABLE_BF16
  932. ////////////////////////////////////////////////////////////////////////////////////////////////////
  933. inline __device__ float sum(float v)
  934. {
  935. return v;
  936. }
  937. ////////////////////////////////////////////////////////////////////////////////////////////////////
  938. inline __device__ float sum(float2 v)
  939. {
  940. return v.x + v.y;
  941. }
  942. ////////////////////////////////////////////////////////////////////////////////////////////////////
  943. inline __device__ float sum(float4 v)
  944. {
  945. return v.x + v.y + v.z + v.w;
  946. }
  947. ////////////////////////////////////////////////////////////////////////////////////////////////////
  948. #ifdef ENABLE_BF16
  949. inline __device__ float sum(__nv_bfloat162 v)
  950. {
  951. float2 vf = bf1622float2(v);
  952. return vf.x + vf.y;
  953. }
  954. ////////////////////////////////////////////////////////////////////////////////////////////////////
  955. inline __device__ float sum(bf16_4_t v)
  956. {
  957. return sum(v.x) + sum(v.y);
  958. }
  959. ////////////////////////////////////////////////////////////////////////////////////////////////////
  960. inline __device__ float sum(bf16_8_t v)
  961. {
  962. return sum(v.x) + sum(v.y) + sum(v.z) + sum(v.w);
  963. }
  964. #endif // ENABLE_BF16
  965. ////////////////////////////////////////////////////////////////////////////////////////////////////
  966. inline __device__ float sum(uint16_t v)
  967. {
  968. return half_to_float(v);
  969. }
  970. ////////////////////////////////////////////////////////////////////////////////////////////////////
  971. inline __device__ float sum(uint32_t v)
  972. {
  973. float2 tmp = half2_to_float2(v);
  974. return tmp.x + tmp.y;
  975. }
  976. ////////////////////////////////////////////////////////////////////////////////////////////////////
  977. inline __device__ float sum(uint2 v)
  978. {
  979. uint32_t c = add(v.x, v.y);
  980. return sum(c);
  981. }
  982. ////////////////////////////////////////////////////////////////////////////////////////////////////
  983. inline __device__ float sum(uint4 v)
  984. {
  985. #if 1
  986. uint32_t c = add(v.x, v.y);
  987. c = add(c, v.z);
  988. c = add(c, v.w);
  989. #else
  990. uint32_t c = add(v.x, v.y);
  991. uint32_t d = add(v.z, v.w);
  992. c = add(c, d);
  993. #endif
  994. return sum(c);
  995. }
  996. ////////////////////////////////////////////////////////////////////////////////////////////////////
  997. inline __device__ float sum(Float4_ v)
  998. {
  999. return v.x.x + v.x.y + v.y.x + v.y.y;
  1000. }
  1001. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1002. inline __device__ float sum(Float8_ v)
  1003. {
  1004. return v.x.x + v.x.y + v.y.x + v.y.y + v.z.x + v.z.y + v.w.x + v.w.y;
  1005. }
  1006. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1007. template<typename T>
  1008. inline __device__ float dot(T a, T b)
  1009. {
  1010. return sum(mul<T, T, T>(a, b));
  1011. }
  1012. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1013. template<typename A, typename T>
  1014. inline __device__ float dot(T a, T b)
  1015. {
  1016. return sum(mul<A, T, T>(a, b));
  1017. }
  1018. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1019. inline __device__ void zero(uint16_t& dst)
  1020. {
  1021. dst = uint16_t(0);
  1022. }
  1023. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1024. template<typename T>
  1025. inline __device__ void zero(T& dst)
  1026. {
  1027. constexpr int WORDS = sizeof(T) / 4;
  1028. union {
  1029. T raw;
  1030. uint32_t words[WORDS];
  1031. } tmp;
  1032. #pragma unroll
  1033. for (int ii = 0; ii < WORDS; ++ii) {
  1034. tmp.words[ii] = 0u;
  1035. }
  1036. dst = tmp.raw;
  1037. }
  1038. ////////////////////////////////////////////////////////////////////////////////////////////////////
  1039. inline __device__ float2 rotary_embedding_coefficient(const int zid, const int rot_embed_dim, const int t_step, const float base)
  1040. {
  1041. const float pos_idx_inv_freq = t_step / pow(base, zid / (float)rot_embed_dim);
  1042. return {cos(pos_idx_inv_freq), sin(pos_idx_inv_freq)};
  1043. }
  1044. inline __device__ float2 rotary_embedding_transform(const float2 v, const float2 coef)
  1045. {
  1046. float2 rot_v;
  1047. rot_v.x = coef.x * v.x - coef.y * v.y;
  1048. rot_v.y = coef.x * v.y + coef.y * v.x;
  1049. return rot_v;
  1050. }
  1051. inline __device__ uint32_t rotary_embedding_transform(const uint32_t v, const float2 coef)
  1052. {
  1053. float2 fv = half2_to_float2(v);
  1054. float2 rot_fv = rotary_embedding_transform(fv, coef);
  1055. return float2_to_half2(rot_fv);
  1056. }
  1057. #ifdef ENABLE_BF16
  1058. inline __device__ __nv_bfloat162 rotary_embedding_transform(const __nv_bfloat162 v, const float2 coef)
  1059. {
  1060. float2 fv = bf1622float2(v);
  1061. float2 rot_fv = rotary_embedding_transform(fv, coef);
  1062. return __floats2bfloat162_rn(rot_fv.x, rot_fv.y);
  1063. }
  1064. #endif
  1065. inline __device__ void apply_rotary_embedding(float& q, int zid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1066. {
  1067. return;
  1068. }
  1069. inline __device__ void apply_rotary_embedding(float& q, float& k, int zid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1070. {
  1071. return;
  1072. }
  1073. inline __device__ void apply_rotary_embedding(float2& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1074. {
  1075. if (2 * tid >= rot_embed_dim) {
  1076. return;
  1077. }
  1078. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step, base);
  1079. q = rotary_embedding_transform(q, coef);
  1080. }
  1081. inline __device__ void apply_rotary_embedding(float2& q, float2& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1082. {
  1083. if (2 * tid >= rot_embed_dim) {
  1084. return;
  1085. }
  1086. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step, base);
  1087. q = rotary_embedding_transform(q, coef);
  1088. k = rotary_embedding_transform(k, coef);
  1089. }
  1090. inline __device__ void apply_rotary_embedding(float4& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1091. {
  1092. if (4 * tid >= rot_embed_dim) {
  1093. return;
  1094. }
  1095. Float4_& q_ = *reinterpret_cast<Float4_*>(&q);
  1096. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step, base);
  1097. q_.x = rotary_embedding_transform(q_.x, coef0);
  1098. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step, base);
  1099. q_.y = rotary_embedding_transform(q_.y, coef1);
  1100. }
  1101. inline __device__ void apply_rotary_embedding(float4& q, float4& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1102. {
  1103. if (4 * tid >= rot_embed_dim) {
  1104. return;
  1105. }
  1106. Float4_& q_ = *reinterpret_cast<Float4_*>(&q);
  1107. Float4_& k_ = *reinterpret_cast<Float4_*>(&k);
  1108. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step, base);
  1109. q_.x = rotary_embedding_transform(q_.x, coef0);
  1110. k_.x = rotary_embedding_transform(k_.x, coef0);
  1111. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step, base);
  1112. q_.y = rotary_embedding_transform(q_.y, coef1);
  1113. k_.y = rotary_embedding_transform(k_.y, coef1);
  1114. }
  1115. inline __device__ void apply_rotary_embedding(uint32_t& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1116. {
  1117. if (2 * tid >= rot_embed_dim) {
  1118. return;
  1119. }
  1120. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step, base);
  1121. q = rotary_embedding_transform(q, coef);
  1122. }
  1123. inline __device__ void apply_rotary_embedding(uint32_t& q, uint32_t& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1124. {
  1125. if (2 * tid >= rot_embed_dim) {
  1126. return;
  1127. }
  1128. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step, base);
  1129. q = rotary_embedding_transform(q, coef);
  1130. k = rotary_embedding_transform(k, coef);
  1131. }
  1132. inline __device__ void apply_rotary_embedding(uint2& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1133. {
  1134. if (4 * tid >= rot_embed_dim) {
  1135. return;
  1136. }
  1137. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step, base);
  1138. q.x = rotary_embedding_transform(q.x, coef0);
  1139. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step, base);
  1140. q.y = rotary_embedding_transform(q.y, coef1);
  1141. }
  1142. inline __device__ void apply_rotary_embedding(uint2& q, uint2& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1143. {
  1144. if (4 * tid >= rot_embed_dim) {
  1145. return;
  1146. }
  1147. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step, base);
  1148. q.x = rotary_embedding_transform(q.x, coef0);
  1149. k.x = rotary_embedding_transform(k.x, coef0);
  1150. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step, base);
  1151. q.y = rotary_embedding_transform(q.y, coef1);
  1152. k.y = rotary_embedding_transform(k.y, coef1);
  1153. }
  1154. inline __device__ void apply_rotary_embedding(uint4& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1155. {
  1156. if (8 * tid >= rot_embed_dim) {
  1157. return;
  1158. }
  1159. const auto coef0 = rotary_embedding_coefficient(8 * tid, rot_embed_dim, t_step, base);
  1160. q.x = rotary_embedding_transform(q.x, coef0);
  1161. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step, base);
  1162. q.y = rotary_embedding_transform(q.y, coef1);
  1163. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step, base);
  1164. q.z = rotary_embedding_transform(q.z, coef2);
  1165. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step, base);
  1166. q.w = rotary_embedding_transform(q.w, coef3);
  1167. }
  1168. inline __device__ void apply_rotary_embedding(uint4& q, uint4& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1169. {
  1170. if (8 * tid >= rot_embed_dim) {
  1171. return;
  1172. }
  1173. const auto coef0 = rotary_embedding_coefficient(8 * tid, rot_embed_dim, t_step, base);
  1174. q.x = rotary_embedding_transform(q.x, coef0);
  1175. k.x = rotary_embedding_transform(k.x, coef0);
  1176. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step, base);
  1177. q.y = rotary_embedding_transform(q.y, coef1);
  1178. k.y = rotary_embedding_transform(k.y, coef1);
  1179. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step, base);
  1180. q.z = rotary_embedding_transform(q.z, coef2);
  1181. k.z = rotary_embedding_transform(k.z, coef2);
  1182. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step, base);
  1183. q.w = rotary_embedding_transform(q.w, coef3);
  1184. k.w = rotary_embedding_transform(k.w, coef3);
  1185. }
  1186. #ifdef ENABLE_BF16
  1187. inline __device__ void apply_rotary_embedding(__nv_bfloat162& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1188. {
  1189. if (2 * tid >= rot_embed_dim) {
  1190. return;
  1191. }
  1192. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step, base);
  1193. q = rotary_embedding_transform(q, coef);
  1194. }
  1195. inline __device__ void apply_rotary_embedding(__nv_bfloat162& q, __nv_bfloat162& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1196. {
  1197. if (2 * tid >= rot_embed_dim) {
  1198. return;
  1199. }
  1200. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step, base);
  1201. q = rotary_embedding_transform(q, coef);
  1202. k = rotary_embedding_transform(k, coef);
  1203. }
  1204. inline __device__ void apply_rotary_embedding(bf16_4_t& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1205. {
  1206. if (4 * tid >= rot_embed_dim) {
  1207. return;
  1208. }
  1209. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step, base);
  1210. q.x = rotary_embedding_transform(q.x, coef0);
  1211. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step, base);
  1212. q.y = rotary_embedding_transform(q.y, coef1);
  1213. }
  1214. inline __device__ void apply_rotary_embedding(bf16_4_t& q, bf16_4_t& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1215. {
  1216. if (4 * tid >= rot_embed_dim) {
  1217. return;
  1218. }
  1219. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step, base);
  1220. q.x = rotary_embedding_transform(q.x, coef0);
  1221. k.x = rotary_embedding_transform(k.x, coef0);
  1222. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step, base);
  1223. q.y = rotary_embedding_transform(q.y, coef1);
  1224. k.y = rotary_embedding_transform(k.y, coef1);
  1225. }
  1226. inline __device__ void apply_rotary_embedding(bf16_8_t& q, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1227. {
  1228. if (8 * tid >= rot_embed_dim) {
  1229. return;
  1230. }
  1231. const auto coef0 = rotary_embedding_coefficient(8 * tid, rot_embed_dim, t_step, base);
  1232. q.x = rotary_embedding_transform(q.x, coef0);
  1233. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step, base);
  1234. q.y = rotary_embedding_transform(q.y, coef1);
  1235. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step, base);
  1236. q.z = rotary_embedding_transform(q.z, coef2);
  1237. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step, base);
  1238. q.w = rotary_embedding_transform(q.w, coef3);
  1239. }
  1240. inline __device__ void apply_rotary_embedding(bf16_8_t& q, bf16_8_t& k, int tid, int rot_embed_dim, int t_step, const float base=10000.0f)
  1241. {
  1242. if (8 * tid >= rot_embed_dim) {
  1243. return;
  1244. }
  1245. const auto coef0 = rotary_embedding_coefficient(8 * tid, rot_embed_dim, t_step, base);
  1246. q.x = rotary_embedding_transform(q.x, coef0);
  1247. k.x = rotary_embedding_transform(k.x, coef0);
  1248. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step, base);
  1249. q.y = rotary_embedding_transform(q.y, coef1);
  1250. k.y = rotary_embedding_transform(k.y, coef1);
  1251. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step, base);
  1252. q.z = rotary_embedding_transform(q.z, coef2);
  1253. k.z = rotary_embedding_transform(k.z, coef2);
  1254. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step, base);
  1255. q.w = rotary_embedding_transform(q.w, coef3);
  1256. k.w = rotary_embedding_transform(k.w, coef3);
  1257. }
  1258. #endif // ENABLE_BF16
  1259. template <typename T>
  1260. inline __device__ float2 rotary_embedding_coefficient(const int zid, const int t_step, const T* rotary_cos, const T* rotary_sin)
  1261. {
  1262. // zid is the index of the dimension (0, 2, 4, ..., rotary_dim).
  1263. // rotary_cos/sin stores those at index 0, 1, 2, ..., rotary_dim / 2.
  1264. return {float(rotary_cos[zid / 2]), float(rotary_sin[zid / 2])};
  1265. }
  1266. // fp16 is special because we use uint16_t for reading the data, for backward compatibility.
  1267. template <>
  1268. inline __device__ float2 rotary_embedding_coefficient<uint16_t>(const int zid, const int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1269. {
  1270. // zid is the index of the dimension (0, 2, 4, ..., rotary_dim).
  1271. // rotary_cos/sin stores those at index 0, 1, 2, ..., rotary_dim / 2.
  1272. return {float(reinterpret_cast<const __half*>(rotary_cos)[zid / 2]),
  1273. float(reinterpret_cast<const __half*>(rotary_sin)[zid / 2])};
  1274. }
  1275. inline __device__ void apply_rotary_embedding(float& q, int zid, int rot_embed_dim, int t_step, const float* rotary_cos, const float* rotary_sin)
  1276. {
  1277. return;
  1278. }
  1279. inline __device__ void apply_rotary_embedding(float& q, float& k, int zid, int rot_embed_dim, int t_step, const float* rotary_cos, const float* rotary_sin)
  1280. {
  1281. return;
  1282. }
  1283. inline __device__ void apply_rotary_embedding(float2& q, int tid, int rot_embed_dim, int t_step, const float* rotary_cos, const float* rotary_sin)
  1284. {
  1285. if (2 * tid >= rot_embed_dim) {
  1286. return;
  1287. }
  1288. const auto coef = rotary_embedding_coefficient(2 * tid, t_step, rotary_cos, rotary_sin);
  1289. q = rotary_embedding_transform(q, coef);
  1290. }
  1291. inline __device__ void apply_rotary_embedding(float2& q, float2& k, int tid, int rot_embed_dim, int t_step, const float* rotary_cos, const float* rotary_sin)
  1292. {
  1293. if (2 * tid >= rot_embed_dim) {
  1294. return;
  1295. }
  1296. const auto coef = rotary_embedding_coefficient(2 * tid, t_step, rotary_cos, rotary_sin);
  1297. q = rotary_embedding_transform(q, coef);
  1298. k = rotary_embedding_transform(k, coef);
  1299. }
  1300. inline __device__ void apply_rotary_embedding(float4& q, int tid, int rot_embed_dim, int t_step, const float* rotary_cos, const float* rotary_sin)
  1301. {
  1302. if (4 * tid >= rot_embed_dim) {
  1303. return;
  1304. }
  1305. Float4_& q_ = *reinterpret_cast<Float4_*>(&q);
  1306. const auto coef0 = rotary_embedding_coefficient(4 * tid, t_step, rotary_cos, rotary_sin);
  1307. q_.x = rotary_embedding_transform(q_.x, coef0);
  1308. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, t_step, rotary_cos, rotary_sin);
  1309. q_.y = rotary_embedding_transform(q_.y, coef1);
  1310. }
  1311. inline __device__ void apply_rotary_embedding(float4& q, float4& k, int tid, int rot_embed_dim, int t_step, const float* rotary_cos, const float* rotary_sin)
  1312. {
  1313. if (4 * tid >= rot_embed_dim) {
  1314. return;
  1315. }
  1316. Float4_& q_ = *reinterpret_cast<Float4_*>(&q);
  1317. Float4_& k_ = *reinterpret_cast<Float4_*>(&k);
  1318. const auto coef0 = rotary_embedding_coefficient(4 * tid, t_step, rotary_cos, rotary_sin);
  1319. q_.x = rotary_embedding_transform(q_.x, coef0);
  1320. k_.x = rotary_embedding_transform(k_.x, coef0);
  1321. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, t_step, rotary_cos, rotary_sin);
  1322. q_.y = rotary_embedding_transform(q_.y, coef1);
  1323. k_.y = rotary_embedding_transform(k_.y, coef1);
  1324. }
  1325. inline __device__ void apply_rotary_embedding(uint32_t& q, int tid, int rot_embed_dim, int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1326. {
  1327. if (2 * tid >= rot_embed_dim) {
  1328. return;
  1329. }
  1330. const auto coef = rotary_embedding_coefficient(2 * tid, t_step, rotary_cos, rotary_sin);
  1331. q = rotary_embedding_transform(q, coef);
  1332. }
  1333. inline __device__ void apply_rotary_embedding(uint32_t& q, uint32_t& k, int tid, int rot_embed_dim, int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1334. {
  1335. if (2 * tid >= rot_embed_dim) {
  1336. return;
  1337. }
  1338. const auto coef = rotary_embedding_coefficient(2 * tid, t_step, rotary_cos, rotary_sin);
  1339. q = rotary_embedding_transform(q, coef);
  1340. k = rotary_embedding_transform(k, coef);
  1341. }
  1342. inline __device__ void apply_rotary_embedding(uint2& q, int tid, int rot_embed_dim, int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1343. {
  1344. if (4 * tid >= rot_embed_dim) {
  1345. return;
  1346. }
  1347. const auto coef0 = rotary_embedding_coefficient(4 * tid, t_step, rotary_cos, rotary_sin);
  1348. q.x = rotary_embedding_transform(q.x, coef0);
  1349. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, t_step, rotary_cos, rotary_sin);
  1350. q.y = rotary_embedding_transform(q.y, coef1);
  1351. }
  1352. inline __device__ void apply_rotary_embedding(uint2& q, uint2& k, int tid, int rot_embed_dim, int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1353. {
  1354. if (4 * tid >= rot_embed_dim) {
  1355. return;
  1356. }
  1357. const auto coef0 = rotary_embedding_coefficient(4 * tid, t_step, rotary_cos, rotary_sin);
  1358. q.x = rotary_embedding_transform(q.x, coef0);
  1359. k.x = rotary_embedding_transform(k.x, coef0);
  1360. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, t_step, rotary_cos, rotary_sin);
  1361. q.y = rotary_embedding_transform(q.y, coef1);
  1362. k.y = rotary_embedding_transform(k.y, coef1);
  1363. }
  1364. inline __device__ void apply_rotary_embedding(uint4& q, int tid, int rot_embed_dim, int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1365. {
  1366. if (8 * tid >= rot_embed_dim) {
  1367. return;
  1368. }
  1369. const auto coef0 = rotary_embedding_coefficient(8 * tid, t_step, rotary_cos, rotary_sin);
  1370. q.x = rotary_embedding_transform(q.x, coef0);
  1371. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, t_step, rotary_cos, rotary_sin);
  1372. q.y = rotary_embedding_transform(q.y, coef1);
  1373. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, t_step, rotary_cos, rotary_sin);
  1374. q.z = rotary_embedding_transform(q.z, coef2);
  1375. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, t_step, rotary_cos, rotary_sin);
  1376. q.w = rotary_embedding_transform(q.w, coef3);
  1377. }
  1378. inline __device__ void apply_rotary_embedding(uint4& q, uint4& k, int tid, int rot_embed_dim, int t_step, const uint16_t* rotary_cos, const uint16_t* rotary_sin)
  1379. {
  1380. if (8 * tid >= rot_embed_dim) {
  1381. return;
  1382. }
  1383. const auto coef0 = rotary_embedding_coefficient(8 * tid, t_step, rotary_cos, rotary_sin);
  1384. q.x = rotary_embedding_transform(q.x, coef0);
  1385. k.x = rotary_embedding_transform(k.x, coef0);
  1386. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, t_step, rotary_cos, rotary_sin);
  1387. q.y = rotary_embedding_transform(q.y, coef1);
  1388. k.y = rotary_embedding_transform(k.y, coef1);
  1389. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, t_step, rotary_cos, rotary_sin);
  1390. q.z = rotary_embedding_transform(q.z, coef2);
  1391. k.z = rotary_embedding_transform(k.z, coef2);
  1392. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, t_step, rotary_cos, rotary_sin);
  1393. q.w = rotary_embedding_transform(q.w, coef3);
  1394. k.w = rotary_embedding_transform(k.w, coef3);
  1395. }
  1396. #ifdef ENABLE_BF16
  1397. inline __device__ void apply_rotary_embedding(__nv_bfloat162& q, int tid, int rot_embed_dim, int t_step, const __nv_bfloat16* rotary_cos, const __nv_bfloat16* rotary_sin)
  1398. {
  1399. if (2 * tid >= rot_embed_dim) {
  1400. return;
  1401. }
  1402. const auto coef = rotary_embedding_coefficient(2 * tid, t_step, rotary_cos, rotary_sin);
  1403. q = rotary_embedding_transform(q, coef);
  1404. }
  1405. inline __device__ void apply_rotary_embedding(__nv_bfloat162& q, __nv_bfloat162& k, int tid, int rot_embed_dim, int t_step, const __nv_bfloat16* rotary_cos, const __nv_bfloat16* rotary_sin)
  1406. {
  1407. if (2 * tid >= rot_embed_dim) {
  1408. return;
  1409. }
  1410. const auto coef = rotary_embedding_coefficient(2 * tid, t_step, rotary_cos, rotary_sin);
  1411. q = rotary_embedding_transform(q, coef);
  1412. k = rotary_embedding_transform(k, coef);
  1413. }
  1414. inline __device__ void apply_rotary_embedding(bf16_4_t& q, int tid, int rot_embed_dim, int t_step, const __nv_bfloat16* rotary_cos, const __nv_bfloat16* rotary_sin)
  1415. {
  1416. if (4 * tid >= rot_embed_dim) {
  1417. return;
  1418. }
  1419. const auto coef0 = rotary_embedding_coefficient(4 * tid, t_step, rotary_cos, rotary_sin);
  1420. q.x = rotary_embedding_transform(q.x, coef0);
  1421. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, t_step, rotary_cos, rotary_sin);
  1422. q.y = rotary_embedding_transform(q.y, coef1);
  1423. }
  1424. inline __device__ void apply_rotary_embedding(bf16_4_t& q, bf16_4_t& k, int tid, int rot_embed_dim, int t_step, const __nv_bfloat16* rotary_cos, const __nv_bfloat16* rotary_sin)
  1425. {
  1426. if (4 * tid >= rot_embed_dim) {
  1427. return;
  1428. }
  1429. const auto coef0 = rotary_embedding_coefficient(4 * tid, t_step, rotary_cos, rotary_sin);
  1430. q.x = rotary_embedding_transform(q.x, coef0);
  1431. k.x = rotary_embedding_transform(k.x, coef0);
  1432. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, t_step, rotary_cos, rotary_sin);
  1433. q.y = rotary_embedding_transform(q.y, coef1);
  1434. k.y = rotary_embedding_transform(k.y, coef1);
  1435. }
  1436. inline __device__ void apply_rotary_embedding(bf16_8_t& q, int tid, int rot_embed_dim, int t_step, const __nv_bfloat16* rotary_cos, const __nv_bfloat16* rotary_sin)
  1437. {
  1438. if (8 * tid >= rot_embed_dim) {
  1439. return;
  1440. }
  1441. const auto coef0 = rotary_embedding_coefficient(8 * tid, t_step, rotary_cos, rotary_sin);
  1442. q.x = rotary_embedding_transform(q.x, coef0);
  1443. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, t_step, rotary_cos, rotary_sin);
  1444. q.y = rotary_embedding_transform(q.y, coef1);
  1445. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, t_step, rotary_cos, rotary_sin);
  1446. q.z = rotary_embedding_transform(q.z, coef2);
  1447. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, t_step, rotary_cos, rotary_sin);
  1448. q.w = rotary_embedding_transform(q.w, coef3);
  1449. }
  1450. inline __device__ void apply_rotary_embedding(bf16_8_t& q, bf16_8_t& k, int tid, int rot_embed_dim, int t_step, const __nv_bfloat16* rotary_cos, const __nv_bfloat16* rotary_sin)
  1451. {
  1452. if (8 * tid >= rot_embed_dim) {
  1453. return;
  1454. }
  1455. const auto coef0 = rotary_embedding_coefficient(8 * tid, t_step, rotary_cos, rotary_sin);
  1456. q.x = rotary_embedding_transform(q.x, coef0);
  1457. k.x = rotary_embedding_transform(k.x, coef0);
  1458. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, t_step, rotary_cos, rotary_sin);
  1459. q.y = rotary_embedding_transform(q.y, coef1);
  1460. k.y = rotary_embedding_transform(k.y, coef1);
  1461. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, t_step, rotary_cos, rotary_sin);
  1462. q.z = rotary_embedding_transform(q.z, coef2);
  1463. k.z = rotary_embedding_transform(k.z, coef2);
  1464. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, t_step, rotary_cos, rotary_sin);
  1465. q.w = rotary_embedding_transform(q.w, coef3);
  1466. k.w = rotary_embedding_transform(k.w, coef3);
  1467. }
  1468. #endif // ENABLE_BF16
  1469. template<typename Vec_T, typename T>
  1470. __device__ __inline__ void vec_from_smem_transpose(Vec_T& vec, T* smem, int transpose_idx, int smem_pitch);
  1471. template<>
  1472. __device__ __inline__ void vec_from_smem_transpose(float& vec, float* smem, int transpose_idx, int smem_pitch)
  1473. {
  1474. return;
  1475. }
  1476. template<>
  1477. __device__ __inline__ void vec_from_smem_transpose(uint32_t& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1478. {
  1479. union {
  1480. uint32_t u32;
  1481. uint16_t u16[2];
  1482. } tmp;
  1483. tmp.u16[0] = smem[transpose_idx];
  1484. tmp.u16[1] = smem[smem_pitch + transpose_idx];
  1485. vec = tmp.u32;
  1486. }
  1487. template<>
  1488. __device__ __inline__ void vec_from_smem_transpose(uint2& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1489. {
  1490. union {
  1491. uint32_t u32;
  1492. uint16_t u16[2];
  1493. } tmp_1, tmp_2;
  1494. tmp_1.u32 = *reinterpret_cast<uint32_t*>(&smem[transpose_idx]);
  1495. tmp_2.u32 = *reinterpret_cast<uint32_t*>(&smem[smem_pitch + transpose_idx]);
  1496. union {
  1497. uint2 u32x2;
  1498. uint16_t u16[4];
  1499. } tmp_3;
  1500. tmp_3.u16[0] = tmp_1.u16[0];
  1501. tmp_3.u16[1] = tmp_2.u16[0];
  1502. tmp_3.u16[2] = tmp_1.u16[1];
  1503. tmp_3.u16[3] = tmp_2.u16[1];
  1504. vec = tmp_3.u32x2;
  1505. }
  1506. template<>
  1507. __device__ __inline__ void vec_from_smem_transpose(uint4& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1508. {
  1509. union {
  1510. uint64_t u64;
  1511. uint16_t u16[4];
  1512. } tmp_1, tmp_2;
  1513. tmp_1.u64 = *reinterpret_cast<uint64_t*>(&smem[transpose_idx]);
  1514. tmp_2.u64 = *reinterpret_cast<uint64_t*>(&smem[smem_pitch + transpose_idx]);
  1515. union {
  1516. uint4 u32x4;
  1517. uint16_t u16[8];
  1518. } tmp_3;
  1519. tmp_3.u16[0] = tmp_1.u16[0];
  1520. tmp_3.u16[1] = tmp_2.u16[0];
  1521. tmp_3.u16[2] = tmp_1.u16[1];
  1522. tmp_3.u16[3] = tmp_2.u16[1];
  1523. tmp_3.u16[4] = tmp_1.u16[2];
  1524. tmp_3.u16[5] = tmp_2.u16[2];
  1525. tmp_3.u16[6] = tmp_1.u16[3];
  1526. tmp_3.u16[7] = tmp_2.u16[3];
  1527. vec = tmp_3.u32x4;
  1528. }
  1529. #ifdef ENABLE_BF16
  1530. template<>
  1531. __device__ __inline__ void
  1532. vec_from_smem_transpose(bf16_4_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1533. {
  1534. union {
  1535. uint32_t u32;
  1536. __nv_bfloat16 bf16[2];
  1537. } tmp_1, tmp_2;
  1538. tmp_1.u32 = *reinterpret_cast<uint32_t*>(&smem[transpose_idx]);
  1539. tmp_2.u32 = *reinterpret_cast<uint32_t*>(&smem[smem_pitch + transpose_idx]);
  1540. vec.x = __nv_bfloat162{tmp_1.bf16[0], tmp_2.bf16[0]};
  1541. vec.y = __nv_bfloat162{tmp_1.bf16[1], tmp_2.bf16[1]};
  1542. }
  1543. template<>
  1544. __device__ __inline__ void
  1545. vec_from_smem_transpose(bf16_8_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1546. {
  1547. union {
  1548. uint64_t u64;
  1549. __nv_bfloat16 bf16[4];
  1550. } tmp_1, tmp_2;
  1551. tmp_1.u64 = *reinterpret_cast<uint64_t*>(&smem[transpose_idx]);
  1552. tmp_2.u64 = *reinterpret_cast<uint64_t*>(&smem[smem_pitch + transpose_idx]);
  1553. vec.x = __nv_bfloat162{tmp_1.bf16[0], tmp_2.bf16[0]};
  1554. vec.y = __nv_bfloat162{tmp_1.bf16[1], tmp_2.bf16[1]};
  1555. vec.z = __nv_bfloat162{tmp_1.bf16[2], tmp_2.bf16[2]};
  1556. vec.w = __nv_bfloat162{tmp_1.bf16[3], tmp_2.bf16[3]};
  1557. }
  1558. #endif // ENABLE_BF16
  1559. template<>
  1560. __device__ __inline__ void vec_from_smem_transpose(float4& vec, float* smem, int transpose_idx, int smem_pitch)
  1561. {
  1562. vec.x = smem[transpose_idx];
  1563. vec.z = smem[transpose_idx + 1];
  1564. vec.y = smem[smem_pitch + transpose_idx];
  1565. vec.w = smem[smem_pitch + transpose_idx + 1];
  1566. }
  1567. template<>
  1568. __device__ __inline__ void vec_from_smem_transpose(uint32_t& vec, half* smem, int transpose_idx, int smem_pitch)
  1569. {
  1570. union {
  1571. uint32_t u32;
  1572. half u16[2];
  1573. } tmp;
  1574. tmp.u16[0] = smem[transpose_idx];
  1575. tmp.u16[1] = smem[smem_pitch + transpose_idx];
  1576. vec = tmp.u32;
  1577. }
  1578. #ifdef ENABLE_BF16
  1579. template<>
  1580. __device__ __inline__ void
  1581. vec_from_smem_transpose(__nv_bfloat162& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1582. {
  1583. vec.x = smem[transpose_idx];
  1584. vec.y = smem[smem_pitch + transpose_idx];
  1585. }
  1586. #endif
  1587. template<>
  1588. __device__ __inline__ void vec_from_smem_transpose(float2& vec, float* smem, int transpose_idx, int smem_pitch)
  1589. {
  1590. vec.x = smem[transpose_idx];
  1591. vec.y = smem[smem_pitch + transpose_idx];
  1592. }
  1593. template<typename Vec_T, typename T>
  1594. __device__ __inline__ void write_smem_transpose(const Vec_T& vec, T* smem, int transpose_idx, int smem_pitch);
  1595. template<>
  1596. __device__ __inline__ void write_smem_transpose(const float& vec, float* smem, int transpose_idx, int smem_pitch)
  1597. {
  1598. return;
  1599. }
  1600. template<>
  1601. __device__ __inline__ void write_smem_transpose(const uint4& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1602. {
  1603. union {
  1604. uint64_t u64;
  1605. uint16_t u16[4];
  1606. } tmp_1, tmp_2;
  1607. union {
  1608. uint4 u32x4;
  1609. uint16_t u16[8];
  1610. } tmp_3;
  1611. tmp_3.u32x4 = vec;
  1612. tmp_1.u16[0] = tmp_3.u16[0];
  1613. tmp_2.u16[0] = tmp_3.u16[1];
  1614. tmp_1.u16[1] = tmp_3.u16[2];
  1615. tmp_2.u16[1] = tmp_3.u16[3];
  1616. tmp_1.u16[2] = tmp_3.u16[4];
  1617. tmp_2.u16[2] = tmp_3.u16[5];
  1618. tmp_1.u16[3] = tmp_3.u16[6];
  1619. tmp_2.u16[3] = tmp_3.u16[7];
  1620. *reinterpret_cast<uint64_t*>(&smem[transpose_idx]) = tmp_1.u64;
  1621. *reinterpret_cast<uint64_t*>(&smem[smem_pitch + transpose_idx]) = tmp_2.u64;
  1622. }
  1623. template<>
  1624. __device__ __inline__ void write_smem_transpose(const uint2& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1625. {
  1626. union {
  1627. uint32_t u32;
  1628. uint16_t u16[2];
  1629. } tmp_1, tmp_2;
  1630. union {
  1631. uint2 u32x2;
  1632. uint16_t u16[4];
  1633. } tmp_3;
  1634. tmp_3.u32x2 = vec;
  1635. tmp_1.u16[0] = tmp_3.u16[0];
  1636. tmp_2.u16[0] = tmp_3.u16[1];
  1637. tmp_1.u16[1] = tmp_3.u16[2];
  1638. tmp_2.u16[1] = tmp_3.u16[3];
  1639. *reinterpret_cast<uint32_t*>(&smem[transpose_idx]) = tmp_1.u32;
  1640. *reinterpret_cast<uint32_t*>(&smem[smem_pitch + transpose_idx]) = tmp_2.u32;
  1641. }
  1642. template<>
  1643. __device__ __inline__ void write_smem_transpose(const uint32_t& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1644. {
  1645. union {
  1646. uint32_t u32;
  1647. uint16_t u16[2];
  1648. } tmp;
  1649. tmp.u32 = vec;
  1650. smem[transpose_idx] = tmp.u16[0];
  1651. smem[smem_pitch + transpose_idx] = tmp.u16[1];
  1652. }
  1653. template<>
  1654. __device__ __inline__ void write_smem_transpose(const float4& vec, float* smem, int transpose_idx, int smem_pitch)
  1655. {
  1656. smem[transpose_idx] = vec.x;
  1657. smem[transpose_idx + 1] = vec.z;
  1658. smem[smem_pitch + transpose_idx] = vec.y;
  1659. smem[smem_pitch + transpose_idx + 1] = vec.w;
  1660. }
  1661. template<>
  1662. __device__ __inline__ void write_smem_transpose(const uint32_t& vec, half* smem, int transpose_idx, int smem_pitch)
  1663. {
  1664. union {
  1665. uint32_t u32;
  1666. half u16[2];
  1667. } tmp;
  1668. tmp.u32 = vec;
  1669. smem[transpose_idx] = tmp.u16[0];
  1670. smem[smem_pitch + transpose_idx] = tmp.u16[1];
  1671. }
  1672. #ifdef ENABLE_BF16
  1673. template<>
  1674. __device__ __inline__ void
  1675. write_smem_transpose(const __nv_bfloat162& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1676. {
  1677. smem[transpose_idx] = vec.x;
  1678. smem[smem_pitch + transpose_idx] = vec.y;
  1679. }
  1680. template<>
  1681. __device__ __inline__ void
  1682. write_smem_transpose(const bf16_4_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1683. {
  1684. write_smem_transpose(reinterpret_cast<const uint2&>(vec), reinterpret_cast<uint16_t*>(smem), transpose_idx, smem_pitch);
  1685. }
  1686. template<>
  1687. __device__ __inline__ void
  1688. write_smem_transpose(const bf16_8_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1689. {
  1690. write_smem_transpose(reinterpret_cast<const uint4&>(vec), reinterpret_cast<uint16_t*>(smem), transpose_idx, smem_pitch);
  1691. }
  1692. #endif
  1693. template<>
  1694. __device__ __inline__ void write_smem_transpose(const float2& vec, float* smem, int transpose_idx, int smem_pitch)
  1695. {
  1696. smem[transpose_idx] = vec.x;
  1697. smem[smem_pitch + transpose_idx] = vec.y;
  1698. }
  1699. } // namespace mmha