decoder_masked_multihead_attention_utils.h 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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 float t_step)
  1040. {
  1041. const float inv_freq = t_step / pow(10000.0f, zid / (float)rot_embed_dim);
  1042. return {cos(inv_freq), sin(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)
  1066. {
  1067. return;
  1068. }
  1069. inline __device__ void apply_rotary_embedding(float& q, float& k, int zid, int rot_embed_dim, int t_step)
  1070. {
  1071. return;
  1072. }
  1073. inline __device__ void apply_rotary_embedding(float2& q, int tid, int rot_embed_dim, int t_step)
  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);
  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)
  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);
  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)
  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);
  1097. q_.x = rotary_embedding_transform(q_.x, coef0);
  1098. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step);
  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)
  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);
  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);
  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)
  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);
  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)
  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);
  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)
  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);
  1138. q.x = rotary_embedding_transform(q.x, coef0);
  1139. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step);
  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)
  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);
  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);
  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)
  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);
  1160. q.x = rotary_embedding_transform(q.x, coef0);
  1161. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step);
  1162. q.y = rotary_embedding_transform(q.y, coef1);
  1163. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step);
  1164. q.z = rotary_embedding_transform(q.z, coef2);
  1165. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step);
  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)
  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);
  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);
  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);
  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);
  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)
  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);
  1193. q = rotary_embedding_transform(q, coef);
  1194. }
  1195. inline __device__ void
  1196. apply_rotary_embedding(__nv_bfloat162& q, __nv_bfloat162& k, int tid, int rot_embed_dim, int t_step)
  1197. {
  1198. if (2 * tid >= rot_embed_dim) {
  1199. return;
  1200. }
  1201. const auto coef = rotary_embedding_coefficient(2 * tid, rot_embed_dim, t_step);
  1202. q = rotary_embedding_transform(q, coef);
  1203. k = rotary_embedding_transform(k, coef);
  1204. }
  1205. inline __device__ void apply_rotary_embedding(bf16_4_t& q, int tid, int rot_embed_dim, int t_step)
  1206. {
  1207. if (4 * tid >= rot_embed_dim) {
  1208. return;
  1209. }
  1210. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step);
  1211. q.x = rotary_embedding_transform(q.x, coef0);
  1212. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step);
  1213. q.y = rotary_embedding_transform(q.y, coef1);
  1214. }
  1215. inline __device__ void apply_rotary_embedding(bf16_4_t& q, bf16_4_t& k, int tid, int rot_embed_dim, int t_step)
  1216. {
  1217. if (4 * tid >= rot_embed_dim) {
  1218. return;
  1219. }
  1220. const auto coef0 = rotary_embedding_coefficient(4 * tid, rot_embed_dim, t_step);
  1221. q.x = rotary_embedding_transform(q.x, coef0);
  1222. k.x = rotary_embedding_transform(k.x, coef0);
  1223. const auto coef1 = rotary_embedding_coefficient(4 * tid + 2, rot_embed_dim, t_step);
  1224. q.y = rotary_embedding_transform(q.y, coef1);
  1225. k.y = rotary_embedding_transform(k.y, coef1);
  1226. }
  1227. inline __device__ void apply_rotary_embedding(bf16_8_t& q, int tid, int rot_embed_dim, int t_step)
  1228. {
  1229. if (8 * tid >= rot_embed_dim) {
  1230. return;
  1231. }
  1232. const auto coef0 = rotary_embedding_coefficient(8 * tid, rot_embed_dim, t_step);
  1233. q.x = rotary_embedding_transform(q.x, coef0);
  1234. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step);
  1235. q.y = rotary_embedding_transform(q.y, coef1);
  1236. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step);
  1237. q.z = rotary_embedding_transform(q.z, coef2);
  1238. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step);
  1239. q.w = rotary_embedding_transform(q.w, coef3);
  1240. }
  1241. inline __device__ void apply_rotary_embedding(bf16_8_t& q, bf16_8_t& k, int tid, int rot_embed_dim, int t_step)
  1242. {
  1243. if (8 * tid >= rot_embed_dim) {
  1244. return;
  1245. }
  1246. const auto coef0 = rotary_embedding_coefficient(8 * tid, rot_embed_dim, t_step);
  1247. q.x = rotary_embedding_transform(q.x, coef0);
  1248. k.x = rotary_embedding_transform(k.x, coef0);
  1249. const auto coef1 = rotary_embedding_coefficient(8 * tid + 2, rot_embed_dim, t_step);
  1250. q.y = rotary_embedding_transform(q.y, coef1);
  1251. k.y = rotary_embedding_transform(k.y, coef1);
  1252. const auto coef2 = rotary_embedding_coefficient(8 * tid + 4, rot_embed_dim, t_step);
  1253. q.z = rotary_embedding_transform(q.z, coef2);
  1254. k.z = rotary_embedding_transform(k.z, coef2);
  1255. const auto coef3 = rotary_embedding_coefficient(8 * tid + 6, rot_embed_dim, t_step);
  1256. q.w = rotary_embedding_transform(q.w, coef3);
  1257. k.w = rotary_embedding_transform(k.w, coef3);
  1258. }
  1259. #endif // ENABLE_BF16
  1260. template<typename Vec_T, typename T>
  1261. __device__ __inline__ void vec_from_smem_transpose(Vec_T& vec, T* smem, int transpose_idx, int smem_pitch);
  1262. template<>
  1263. __device__ __inline__ void vec_from_smem_transpose(float& vec, float* smem, int transpose_idx, int smem_pitch)
  1264. {
  1265. return;
  1266. }
  1267. template<>
  1268. __device__ __inline__ void vec_from_smem_transpose(uint32_t& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1269. {
  1270. union {
  1271. uint32_t u32;
  1272. uint16_t u16[2];
  1273. } tmp;
  1274. tmp.u16[0] = smem[transpose_idx];
  1275. tmp.u16[1] = smem[smem_pitch + transpose_idx];
  1276. vec = tmp.u32;
  1277. }
  1278. template<>
  1279. __device__ __inline__ void vec_from_smem_transpose(uint2& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1280. {
  1281. union {
  1282. uint32_t u32;
  1283. uint16_t u16[2];
  1284. } tmp_1, tmp_2;
  1285. tmp_1.u32 = *reinterpret_cast<uint32_t*>(&smem[transpose_idx]);
  1286. tmp_2.u32 = *reinterpret_cast<uint32_t*>(&smem[smem_pitch + transpose_idx]);
  1287. union {
  1288. uint2 u32x2;
  1289. uint16_t u16[4];
  1290. } tmp_3;
  1291. tmp_3.u16[0] = tmp_1.u16[0];
  1292. tmp_3.u16[1] = tmp_2.u16[0];
  1293. tmp_3.u16[2] = tmp_1.u16[1];
  1294. tmp_3.u16[3] = tmp_2.u16[1];
  1295. vec = tmp_3.u32x2;
  1296. }
  1297. template<>
  1298. __device__ __inline__ void vec_from_smem_transpose(uint4& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1299. {
  1300. union {
  1301. uint64_t u64;
  1302. uint16_t u16[4];
  1303. } tmp_1, tmp_2;
  1304. tmp_1.u64 = *reinterpret_cast<uint64_t*>(&smem[transpose_idx]);
  1305. tmp_2.u64 = *reinterpret_cast<uint64_t*>(&smem[smem_pitch + transpose_idx]);
  1306. union {
  1307. uint4 u32x4;
  1308. uint16_t u16[8];
  1309. } tmp_3;
  1310. tmp_3.u16[0] = tmp_1.u16[0];
  1311. tmp_3.u16[1] = tmp_2.u16[0];
  1312. tmp_3.u16[2] = tmp_1.u16[1];
  1313. tmp_3.u16[3] = tmp_2.u16[1];
  1314. tmp_3.u16[4] = tmp_1.u16[2];
  1315. tmp_3.u16[5] = tmp_2.u16[2];
  1316. tmp_3.u16[6] = tmp_1.u16[3];
  1317. tmp_3.u16[7] = tmp_2.u16[3];
  1318. vec = tmp_3.u32x4;
  1319. }
  1320. #ifdef ENABLE_BF16
  1321. template<>
  1322. __device__ __inline__ void
  1323. vec_from_smem_transpose(bf16_4_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1324. {
  1325. union {
  1326. uint32_t u32;
  1327. __nv_bfloat16 bf16[2];
  1328. } tmp_1, tmp_2;
  1329. tmp_1.u32 = *reinterpret_cast<uint32_t*>(&smem[transpose_idx]);
  1330. tmp_2.u32 = *reinterpret_cast<uint32_t*>(&smem[smem_pitch + transpose_idx]);
  1331. vec.x = __nv_bfloat162{tmp_1.bf16[0], tmp_2.bf16[0]};
  1332. vec.y = __nv_bfloat162{tmp_1.bf16[1], tmp_2.bf16[1]};
  1333. }
  1334. template<>
  1335. __device__ __inline__ void
  1336. vec_from_smem_transpose(bf16_8_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1337. {
  1338. union {
  1339. uint64_t u64;
  1340. __nv_bfloat16 bf16[4];
  1341. } tmp_1, tmp_2;
  1342. tmp_1.u64 = *reinterpret_cast<uint64_t*>(&smem[transpose_idx]);
  1343. tmp_2.u64 = *reinterpret_cast<uint64_t*>(&smem[smem_pitch + transpose_idx]);
  1344. vec.x = __nv_bfloat162{tmp_1.bf16[0], tmp_2.bf16[0]};
  1345. vec.y = __nv_bfloat162{tmp_1.bf16[1], tmp_2.bf16[1]};
  1346. vec.z = __nv_bfloat162{tmp_1.bf16[2], tmp_2.bf16[2]};
  1347. vec.w = __nv_bfloat162{tmp_1.bf16[3], tmp_2.bf16[3]};
  1348. }
  1349. #endif // ENABLE_BF16
  1350. template<>
  1351. __device__ __inline__ void vec_from_smem_transpose(float4& vec, float* smem, int transpose_idx, int smem_pitch)
  1352. {
  1353. vec.x = smem[transpose_idx];
  1354. vec.z = smem[transpose_idx + 1];
  1355. vec.y = smem[smem_pitch + transpose_idx];
  1356. vec.w = smem[smem_pitch + transpose_idx + 1];
  1357. }
  1358. template<>
  1359. __device__ __inline__ void vec_from_smem_transpose(uint32_t& vec, half* smem, int transpose_idx, int smem_pitch)
  1360. {
  1361. union {
  1362. uint32_t u32;
  1363. half u16[2];
  1364. } tmp;
  1365. tmp.u16[0] = smem[transpose_idx];
  1366. tmp.u16[1] = smem[smem_pitch + transpose_idx];
  1367. vec = tmp.u32;
  1368. }
  1369. #ifdef ENABLE_BF16
  1370. template<>
  1371. __device__ __inline__ void
  1372. vec_from_smem_transpose(__nv_bfloat162& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1373. {
  1374. vec.x = smem[transpose_idx];
  1375. vec.y = smem[smem_pitch + transpose_idx];
  1376. }
  1377. #endif
  1378. template<>
  1379. __device__ __inline__ void vec_from_smem_transpose(float2& vec, float* smem, int transpose_idx, int smem_pitch)
  1380. {
  1381. vec.x = smem[transpose_idx];
  1382. vec.y = smem[smem_pitch + transpose_idx];
  1383. }
  1384. template<typename Vec_T, typename T>
  1385. __device__ __inline__ void write_smem_transpose(const Vec_T& vec, T* smem, int transpose_idx, int smem_pitch);
  1386. template<>
  1387. __device__ __inline__ void write_smem_transpose(const float& vec, float* smem, int transpose_idx, int smem_pitch)
  1388. {
  1389. return;
  1390. }
  1391. #ifdef ENABLE_BF16
  1392. template<>
  1393. __device__ __inline__ void
  1394. write_smem_transpose(const bf16_4_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1395. {
  1396. return;
  1397. }
  1398. template<>
  1399. __device__ __inline__ void
  1400. write_smem_transpose(const bf16_8_t& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1401. {
  1402. return;
  1403. }
  1404. #endif
  1405. template<>
  1406. __device__ __inline__ void write_smem_transpose(const uint4& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1407. {
  1408. union {
  1409. uint64_t u64;
  1410. uint16_t u16[4];
  1411. } tmp_1, tmp_2;
  1412. union {
  1413. uint4 u32x4;
  1414. uint16_t u16[8];
  1415. } tmp_3;
  1416. tmp_3.u32x4 = vec;
  1417. tmp_1.u16[0] = tmp_3.u16[0];
  1418. tmp_2.u16[0] = tmp_3.u16[1];
  1419. tmp_1.u16[1] = tmp_3.u16[2];
  1420. tmp_2.u16[1] = tmp_3.u16[3];
  1421. tmp_1.u16[2] = tmp_3.u16[4];
  1422. tmp_2.u16[2] = tmp_3.u16[5];
  1423. tmp_1.u16[3] = tmp_3.u16[6];
  1424. tmp_2.u16[3] = tmp_3.u16[7];
  1425. *reinterpret_cast<uint64_t*>(&smem[transpose_idx]) = tmp_1.u64;
  1426. *reinterpret_cast<uint64_t*>(&smem[smem_pitch + transpose_idx]) = tmp_2.u64;
  1427. }
  1428. template<>
  1429. __device__ __inline__ void write_smem_transpose(const uint2& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1430. {
  1431. union {
  1432. uint32_t u32;
  1433. uint16_t u16[2];
  1434. } tmp_1, tmp_2;
  1435. union {
  1436. uint2 u32x2;
  1437. uint16_t u16[4];
  1438. } tmp_3;
  1439. tmp_3.u32x2 = vec;
  1440. tmp_1.u16[0] = tmp_3.u16[0];
  1441. tmp_2.u16[0] = tmp_3.u16[1];
  1442. tmp_1.u16[1] = tmp_3.u16[2];
  1443. tmp_2.u16[1] = tmp_3.u16[3];
  1444. *reinterpret_cast<uint32_t*>(&smem[transpose_idx]) = tmp_1.u32;
  1445. *reinterpret_cast<uint32_t*>(&smem[smem_pitch + transpose_idx]) = tmp_2.u32;
  1446. }
  1447. template<>
  1448. __device__ __inline__ void write_smem_transpose(const uint32_t& vec, uint16_t* smem, int transpose_idx, int smem_pitch)
  1449. {
  1450. union {
  1451. uint32_t u32;
  1452. uint16_t u16[2];
  1453. } tmp;
  1454. tmp.u32 = vec;
  1455. smem[transpose_idx] = tmp.u16[0];
  1456. smem[smem_pitch + transpose_idx] = tmp.u16[1];
  1457. }
  1458. template<>
  1459. __device__ __inline__ void write_smem_transpose(const float4& vec, float* smem, int transpose_idx, int smem_pitch)
  1460. {
  1461. smem[transpose_idx] = vec.x;
  1462. smem[transpose_idx + 1] = vec.z;
  1463. smem[smem_pitch + transpose_idx] = vec.y;
  1464. smem[smem_pitch + transpose_idx + 1] = vec.w;
  1465. }
  1466. template<>
  1467. __device__ __inline__ void write_smem_transpose(const uint32_t& vec, half* smem, int transpose_idx, int smem_pitch)
  1468. {
  1469. union {
  1470. uint32_t u32;
  1471. half u16[2];
  1472. } tmp;
  1473. tmp.u32 = vec;
  1474. smem[transpose_idx] = tmp.u16[0];
  1475. smem[smem_pitch + transpose_idx] = tmp.u16[1];
  1476. }
  1477. #ifdef ENABLE_BF16
  1478. template<>
  1479. __device__ __inline__ void
  1480. write_smem_transpose(const __nv_bfloat162& vec, __nv_bfloat16* smem, int transpose_idx, int smem_pitch)
  1481. {
  1482. smem[transpose_idx] = vec.x;
  1483. smem[smem_pitch + transpose_idx] = vec.y;
  1484. }
  1485. #endif
  1486. template<>
  1487. __device__ __inline__ void write_smem_transpose(const float2& vec, float* smem, int transpose_idx, int smem_pitch)
  1488. {
  1489. smem[transpose_idx] = vec.x;
  1490. smem[smem_pitch + transpose_idx] = vec.y;
  1491. }
  1492. } // namespace mmha