_custom_ops.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. import contextlib
  2. import functools
  3. from typing import List, Optional, Tuple, Type
  4. import torch
  5. from loguru import logger
  6. try:
  7. import aphrodite._C
  8. except ImportError as e:
  9. logger.warning(f"Failed to import from aphrodite._C with {e}")
  10. with contextlib.suppress(ImportError):
  11. import aphrodite._moe_C
  12. with contextlib.suppress(ImportError):
  13. # ruff: noqa: F401
  14. import aphrodite._punica_C
  15. def is_custom_op_supported(op_name: str) -> bool:
  16. op, overloads = torch._C._jit_get_operation(op_name)
  17. return op is not None
  18. def hint_on_error(fn):
  19. @functools.wraps(fn)
  20. def wrapper(*args, **kwargs):
  21. try:
  22. return fn(*args, **kwargs)
  23. except AttributeError as e:
  24. msg = (
  25. f"Error in calling custom op {fn.__name__}: {e}\n"
  26. f"Possibly you have built or installed an obsolete version of aphrodite.\n"
  27. f"Please try a clean build and install of aphrodite,"
  28. f"or remove old built files such as aphrodite/*.so and build/ ."
  29. )
  30. logger.error(msg)
  31. raise e
  32. return wrapper
  33. # activation ops
  34. def silu_and_mul(out: torch.Tensor, x: torch.Tensor) -> None:
  35. torch.ops._C.silu_and_mul(out, x)
  36. def gelu_and_mul(out: torch.Tensor, x: torch.Tensor) -> None:
  37. torch.ops._C.gelu_and_mul(out, x)
  38. def gelu_tanh_and_mul(out: torch.Tensor, x: torch.Tensor) -> None:
  39. torch.ops._C.gelu_tanh_and_mul(out, x)
  40. def gelu_fast(out: torch.Tensor, x: torch.Tensor) -> None:
  41. torch.ops._C.gelu_fast(out, x)
  42. def gelu_new(out: torch.Tensor, x: torch.Tensor) -> None:
  43. torch.ops._C.gelu_new(out, x)
  44. def gelu_quick(out: torch.Tensor, x: torch.Tensor) -> None:
  45. torch.ops._C.gelu_quick(out, x)
  46. # page attention ops
  47. def paged_attention_v1(
  48. out: torch.Tensor,
  49. query: torch.Tensor,
  50. key_cache: torch.Tensor,
  51. value_cache: torch.Tensor,
  52. num_kv_heads: int,
  53. scale: float,
  54. block_tables: torch.Tensor,
  55. seq_lens: torch.Tensor,
  56. block_size: int,
  57. max_seq_len: int,
  58. alibi_slopes: Optional[torch.Tensor],
  59. kv_cache_dtype: str,
  60. kv_scale: float,
  61. tp_rank: int = 0,
  62. blocksparse_local_blocks: int = 0,
  63. blocksparse_vert_stride: int = 0,
  64. blocksparse_block_size: int = 64,
  65. blocksparse_head_sliding_step: int = 0,
  66. ) -> None:
  67. torch.ops._C.paged_attention_v1(
  68. out, query, key_cache, value_cache, num_kv_heads, scale, block_tables,
  69. seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype,
  70. kv_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride,
  71. blocksparse_block_size, blocksparse_head_sliding_step)
  72. def paged_attention_v2(
  73. out: torch.Tensor,
  74. exp_sum: torch.Tensor,
  75. max_logits: torch.Tensor,
  76. tmp_out: torch.Tensor,
  77. query: torch.Tensor,
  78. key_cache: torch.Tensor,
  79. value_cache: torch.Tensor,
  80. num_kv_heads: int,
  81. scale: float,
  82. block_tables: torch.Tensor,
  83. seq_lens: torch.Tensor,
  84. block_size: int,
  85. max_seq_len: int,
  86. alibi_slopes: Optional[torch.Tensor],
  87. kv_cache_dtype: str,
  88. kv_scale: float,
  89. tp_rank: int = 0,
  90. blocksparse_local_blocks: int = 0,
  91. blocksparse_vert_stride: int = 0,
  92. blocksparse_block_size: int = 64,
  93. blocksparse_head_sliding_step: int = 0,
  94. ) -> None:
  95. torch.ops._C.paged_attention_v2(
  96. out, exp_sum, max_logits, tmp_out, query, key_cache, value_cache,
  97. num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len,
  98. alibi_slopes, kv_cache_dtype, kv_scale, tp_rank,
  99. blocksparse_local_blocks, blocksparse_vert_stride,
  100. blocksparse_block_size, blocksparse_head_sliding_step)
  101. # pos encoding ops
  102. def rotary_embedding(
  103. positions: torch.Tensor,
  104. query: torch.Tensor,
  105. key: torch.Tensor,
  106. head_size: int,
  107. cos_sin_cache: torch.Tensor,
  108. is_neox: bool,
  109. ) -> None:
  110. torch.ops._C.rotary_embedding(positions, query, key, head_size,
  111. cos_sin_cache, is_neox)
  112. def batched_rotary_embedding(positions: torch.Tensor, query: torch.Tensor,
  113. key: torch.Tensor, head_size: int,
  114. cos_sin_cache: torch.Tensor, is_neox: bool,
  115. rot_dim: int,
  116. cos_sin_cache_offsets: torch.Tensor) -> None:
  117. torch.ops._C.batched_rotary_embedding(positions, query, key, head_size,
  118. cos_sin_cache, is_neox, rot_dim,
  119. cos_sin_cache_offsets)
  120. # layer norm ops
  121. def rms_norm(out: torch.Tensor, input: torch.Tensor, weight: torch.Tensor,
  122. epsilon: float) -> None:
  123. torch.ops._C.rms_norm(out, input, weight, epsilon)
  124. def fused_add_rms_norm(input: torch.Tensor, residual: torch.Tensor,
  125. weight: torch.Tensor, epsilon: float) -> None:
  126. torch.ops._C.fused_add_rms_norm(input, residual, weight, epsilon)
  127. # quantization ops
  128. # awq
  129. def awq_dequantize(qweight: torch.Tensor, scales: torch.Tensor,
  130. zeros: torch.Tensor, split_k_iters: int, thx: int,
  131. thy: int) -> torch.Tensor:
  132. return torch.ops._C.awq_dequantize(qweight, scales, zeros, split_k_iters,
  133. thx, thy)
  134. def awq_gemm(input: torch.Tensor, qweight: torch.Tensor, qzeros: torch.Tensor,
  135. scales: torch.Tensor, split_k_iters: int) -> torch.Tensor:
  136. return torch.ops._C.awq_gemm(input, qweight, qzeros, scales, split_k_iters)
  137. # gptq
  138. def gptq_gemm(a: torch.Tensor, b_q_weight: torch.Tensor,
  139. b_gptq_qzeros: torch.Tensor, b_gptq_scales: torch.Tensor,
  140. b_g_idx: torch.Tensor, use_exllama: bool,
  141. bit: int) -> torch.Tensor:
  142. return torch.ops._C.gptq_gemm(a, b_q_weight, b_gptq_qzeros, b_gptq_scales,
  143. b_g_idx, use_exllama, bit)
  144. def gptq_shuffle(q_weight: torch.Tensor, q_perm: torch.Tensor,
  145. bit: int) -> None:
  146. torch.ops._C.gptq_shuffle(q_weight, q_perm, bit)
  147. # squeezellm
  148. def squeezellm_gemm(vec: torch.Tensor, mat: torch.Tensor, mul: torch.Tensor,
  149. lookup_table: torch.Tensor) -> None:
  150. torch.ops._C.squeezellm_gemm(vec, mat, mul, lookup_table)
  151. # marlin
  152. def marlin_gemm(a: torch.Tensor, b_q_weight: torch.Tensor,
  153. b_scales: torch.Tensor, workspace: torch.Tensor, size_m: int,
  154. size_n: int, size_k: int) -> torch.Tensor:
  155. return torch.ops._C.marlin_gemm(a, b_q_weight, b_scales, workspace, size_m,
  156. size_n, size_k)
  157. # marlin_24
  158. def gptq_marlin_24_gemm(a: torch.Tensor, b_q_weight: torch.Tensor,
  159. b_meta: torch.Tensor, b_scales: torch.Tensor,
  160. workspace: torch.Tensor, num_bits: int, size_m: int,
  161. size_n: int, size_k: int) -> torch.Tensor:
  162. return torch.ops._C.gptq_marlin_24_gemm(a, b_q_weight, b_meta, b_scales,
  163. workspace, num_bits, size_m,
  164. size_n, size_k)
  165. # fp8 marlin
  166. def fp8_marlin_gemm(a: torch.Tensor, b_q_weight: torch.Tensor,
  167. b_scales: torch.Tensor, workspace: torch.Tensor,
  168. num_bits: int, size_m: int, size_n: int,
  169. size_k: int) -> torch.Tensor:
  170. return torch.ops._C.fp8_marlin_gemm(a, b_q_weight, b_scales, workspace,
  171. num_bits, size_m, size_n, size_k)
  172. # cutlass
  173. def cutlass_scaled_mm_supports_fp8(cuda_device_capability: int) -> bool:
  174. return torch.ops._C.cutlass_scaled_mm_supports_fp8(cuda_device_capability)
  175. def cutlass_scaled_mm(a: torch.Tensor,
  176. b: torch.Tensor,
  177. scale_a: torch.Tensor,
  178. scale_b: torch.Tensor,
  179. out_dtype: Type[torch.dtype],
  180. bias: Optional[torch.Tensor] = None) -> torch.Tensor:
  181. assert (b.shape[0] % 16 == 0 and b.shape[1] % 16 == 0)
  182. assert (out_dtype is torch.bfloat16 or out_dtype is torch.float16)
  183. m = a.shape[0]
  184. n = b.shape[1]
  185. out = torch.empty((m, n), dtype=out_dtype, device=a.device)
  186. torch.ops._C.cutlass_scaled_mm(out, a, b, scale_a, scale_b, bias)
  187. return out
  188. # aqlm
  189. def aqlm_gemm(input: torch.Tensor, codes: torch.Tensor,
  190. codebooks: torch.Tensor, scales: torch.Tensor,
  191. codebook_partition_sizes: torch.Tensor,
  192. bias: Optional[torch.Tensor]) -> torch.Tensor:
  193. return torch.ops._C.aqlm_gemm(input, codes, codebooks, scales,
  194. codebook_partition_sizes, bias)
  195. def aqlm_dequant(codes: torch.Tensor, codebooks: torch.Tensor,
  196. codebook_partition_sizes: torch.Tensor) -> torch.Tensor:
  197. return torch.ops._C.aqlm_dequant(codes, codebooks,
  198. codebook_partition_sizes)
  199. # gptq_marlin
  200. def gptq_marlin_repack(b_q_weight: torch.Tensor, perm: torch.Tensor,
  201. size_k: int, size_n: int,
  202. num_bits: int) -> torch.Tensor:
  203. return torch.ops._C.gptq_marlin_repack(b_q_weight, perm, size_k, size_n,
  204. num_bits)
  205. def gptq_marlin_gemm(a: torch.Tensor, b_q_weight: torch.Tensor,
  206. b_scales: torch.Tensor, g_idx: torch.Tensor,
  207. perm: torch.Tensor, workspace: torch.Tensor,
  208. num_bits: int, size_m: int, size_n: int, size_k: int,
  209. is_k_full: bool) -> torch.Tensor:
  210. return torch.ops._C.gptq_marlin_gemm(a, b_q_weight, b_scales, g_idx, perm,
  211. workspace, num_bits, size_m, size_n,
  212. size_k, is_k_full)
  213. # fp8
  214. def scaled_fp8_quant(
  215. input: torch.Tensor,
  216. scale: Optional[torch.Tensor] = None,
  217. batch_dim_padding: Optional[int] = None,
  218. ) -> Tuple[torch.Tensor, torch.Tensor]:
  219. """
  220. Quantize input tensor to FP8 and return quantized tensor and scale.
  221. This function supports both static and dynamic quantization: If you
  222. provide the scale, it will use static scaling and if you omit it,
  223. the scale will be determined dynamically. The function also allows
  224. optional padding of the output tensor for downstream kernels that
  225. will benefit from padding.
  226. Args:
  227. input: The input tensor to be quantized to FP8
  228. scale: Optional scaling factor for the FP8 quantization
  229. batch_dim_padding: If specified, pad the first dimension
  230. of the output to at least this value.
  231. Returns:
  232. Tuple[torch.Tensor, torch.Tensor]: The output tensor in FP8 and
  233. scaling factor.
  234. """
  235. if batch_dim_padding:
  236. shape = (max(batch_dim_padding, input.shape[0]), *input.shape[1:])
  237. output = torch.empty(shape,
  238. device=input.device,
  239. dtype=torch.float8_e4m3fn)
  240. else:
  241. output = torch.empty_like(input, dtype=torch.float8_e4m3fn)
  242. if scale is None:
  243. scale = torch.zeros(1, device=input.device, dtype=torch.float32)
  244. torch.ops._C.dynamic_scaled_fp8_quant(output, input, scale)
  245. else:
  246. torch.ops._C.static_scaled_fp8_quant(output, input, scale)
  247. return output, scale
  248. # int8
  249. def scaled_int8_quant(
  250. input: torch.Tensor,
  251. scale: Optional[torch.Tensor] = None
  252. ) -> Tuple[torch.Tensor, torch.Tensor]:
  253. """
  254. Quantize the input tensor to int8 and return the quantized tensor and scale.
  255. Args:
  256. input: The input tensor to be quantized to int8.
  257. scale: Optional scaling factor for the int8 quantization.
  258. When not provided, we invoke dynamic-per-token quantization.
  259. Returns:
  260. Tuple[Torch.Tensor, Torch.Tensor] : Output int8 tensor and scales.
  261. """
  262. output = torch.empty_like(input, dtype=torch.int8)
  263. if scale is not None:
  264. # static-per-tensor quantization.
  265. torch.ops._C.static_scaled_int8_quant(output, input, scale)
  266. return output, scale
  267. # dynamic-per-token quantization.
  268. input_scales = torch.empty((input.numel() // input.shape[-1], 1),
  269. device=input.device,
  270. dtype=torch.float32)
  271. torch.ops._C.dynamic_scaled_int8_quant(output, input, input_scales)
  272. return output, input_scales
  273. # quip#
  274. def quip_gemv(
  275. A: torch.Tensor,
  276. B: torch.Tensor,
  277. CB: torch.Tensor,
  278. ) -> torch.Tensor:
  279. return torch.ops._C.quip_gemv(A, B, CB)
  280. def quip_decompress(
  281. YIs: torch.Tensor,
  282. CB: torch.Tensor,
  283. Y: torch.Tensor,
  284. ) -> torch.Tensor:
  285. return torch.ops._C.quip_decompress(YIs, CB, Y)
  286. # mamba
  287. def causal_conv1d_fwd(x: torch.Tensor, weight: torch.Tensor,
  288. bias_: Optional[torch.Tensor],
  289. seq_idx_: Optional[torch.Tensor],
  290. initial_states_: Optional[torch.Tensor],
  291. final_states_out_: Optional[torch.Tensor],
  292. silu_activation: bool) -> torch.Tensor:
  293. return torch.ops._C.causal_conv1d_fwd(x, weight, bias_, seq_idx_,
  294. initial_states_, final_states_out_,
  295. silu_activation)
  296. def causal_conv1d_update(x: torch.Tensor, conv_state: torch.Tensor,
  297. weight: torch.Tensor, bias_: Optional[torch.Tensor],
  298. silu_activation: bool) -> torch.Tensor:
  299. return torch.ops._C.causal_conv1d_update(x, conv_state, weight, bias_,
  300. silu_activation)
  301. def selective_scan_fwd(u: torch.Tensor, delta: torch.Tensor, A: torch.Tensor,
  302. B: torch.Tensor, C: torch.Tensor,
  303. D_: Optional[torch.Tensor], z_: Optional[torch.Tensor],
  304. delta_bias_: Optional[torch.Tensor],
  305. delta_softplus: bool, index_: Optional[torch.Tensor],
  306. x: Optional[torch.Tensor]) -> List[torch.Tensor]:
  307. return torch.ops._C.selective_scan_fwd(u, delta, A, B, C, D_, z_,
  308. delta_bias_, delta_softplus, index_,
  309. x)
  310. # moe
  311. def moe_align_block_size(topk_ids: torch.Tensor, num_experts: int,
  312. block_size: int, sorted_token_ids: torch.Tensor,
  313. experts_ids: torch.Tensor,
  314. num_tokens_post_pad: torch.Tensor) -> None:
  315. torch.ops._C.moe_align_block_size(topk_ids, num_experts, block_size,
  316. sorted_token_ids, experts_ids,
  317. num_tokens_post_pad)
  318. def topk_softmax(topk_weights: torch.Tensor, topk_ids: torch.Tensor,
  319. token_expert_indicies: torch.Tensor,
  320. gating_output: float) -> None:
  321. torch.ops._moe_C.topk_softmax(topk_weights, topk_ids,
  322. token_expert_indicies, gating_output)
  323. def reshape_and_cache(
  324. key: torch.Tensor,
  325. value: torch.Tensor,
  326. key_cache: torch.Tensor,
  327. value_cache: torch.Tensor,
  328. slot_mapping: torch.Tensor,
  329. kv_cache_dtype: str,
  330. kv_scale: float,
  331. ) -> None:
  332. torch.ops._C_cache_ops.reshape_and_cache(key, value, key_cache,
  333. value_cache, slot_mapping,
  334. kv_cache_dtype, kv_scale)
  335. def reshape_and_cache_flash(
  336. key: torch.Tensor,
  337. value: torch.Tensor,
  338. key_cache: torch.Tensor,
  339. value_cache: torch.Tensor,
  340. slot_mapping: torch.Tensor,
  341. kv_cache_dtype: str,
  342. ) -> None:
  343. torch.ops._C_cache_ops.reshape_and_cache_flash(key, value, key_cache,
  344. value_cache, slot_mapping,
  345. kv_cache_dtype)
  346. def copy_blocks(key_caches: List[torch.Tensor],
  347. value_caches: List[torch.Tensor],
  348. block_mapping: torch.Tensor) -> None:
  349. torch.ops._C_cache_ops.copy_blocks(key_caches, value_caches, block_mapping)
  350. def swap_blocks(src: torch.Tensor, dst: torch.Tensor,
  351. block_mapping: torch.Tensor) -> None:
  352. torch.ops._C_cache_ops.swap_blocks(src, dst, block_mapping)
  353. def convert_fp8(output: torch.Tensor,
  354. input: torch.Tensor,
  355. scale: float = 1.0,
  356. kv_dtype: str = "fp8") -> None:
  357. torch.ops._C_cache_ops.convert_fp8(output, input, scale, kv_dtype)
  358. def get_device_attribute(attribute: int, device: int) -> int:
  359. return torch.ops._C_cuda_utils.get_device_attribute(attribute, device)
  360. def get_max_shared_memory_per_block_device_attribute(device: int) -> int:
  361. # ruff: noqa: E501
  362. return torch.ops._C_cuda_utils.get_max_shared_memory_per_block_device_attribute(
  363. device)
  364. # custom ar
  365. def init_custom_ar(meta: torch.Tensor, rank_data: torch.Tensor,
  366. handles: List[str], offsets: List[int], rank: int,
  367. full_nvlink: bool) -> int:
  368. return torch.ops._C_custom_ar.init_custom_ar(meta, rank_data, handles,
  369. offsets, rank, full_nvlink)
  370. def should_custom_ar(inp: torch.Tensor, max_size: int, world_size: int,
  371. full_nvlink: bool) -> bool:
  372. return torch.ops._C_custom_ar.should_custom_ar(inp, max_size, world_size,
  373. full_nvlink)
  374. def all_reduce_reg(fa: int, inp: torch.Tensor, out: torch.Tensor) -> None:
  375. torch.ops._C_custom_ar.all_reduce_reg(fa, inp, out)
  376. def all_reduce_unreg(fa: int, inp: torch.Tensor, reg_buffer: torch.Tensor,
  377. out: torch.Tensor) -> None:
  378. torch.ops._C_custom_ar.all_reduce_unreg(fa, inp, reg_buffer, out)
  379. def dispose(fa: int) -> None:
  380. torch.ops._C_custom_ar.dispose(fa)
  381. def meta_size() -> int:
  382. return torch.ops._C_custom_ar.meta_size()
  383. def register_buffer(fa: int, t: torch.Tensor, handles: List[str],
  384. offsets: List[int]) -> None:
  385. return torch.ops._C_custom_ar.register_buffer(fa, t, handles, offsets)
  386. def get_graph_buffer_ipc_meta(fa: int) -> Tuple[List[str], List[int]]:
  387. return torch.ops._C_custom_ar.get_graph_buffer_ipc_meta(fa)
  388. def register_graph_buffers(fa: int, handles: List[str],
  389. offsets: List[List[int]]) -> None:
  390. torch.ops._C_custom_ar.register_graph_buffers(fa, handles, offsets)
  391. # punica
  392. def dispatch_bgmv(
  393. y: torch.Tensor,
  394. x: torch.Tensor,
  395. w_t_all: torch.Tensor,
  396. indicies: torch.Tensor,
  397. layer_idx: int,
  398. scale: float,
  399. ) -> None:
  400. torch.ops._punica_C.dispatch_bgmv(y, x, w_t_all, indicies, layer_idx,
  401. scale)
  402. def dispatch_bgmv_low_level(
  403. y: torch.Tensor,
  404. x: torch.Tensor,
  405. w_t_all: torch.Tensor,
  406. indicies: torch.Tensor,
  407. layer_idx: int,
  408. scale: float,
  409. h_in: int,
  410. h_out: int,
  411. y_offset: int,
  412. ) -> None:
  413. torch.ops._punica_C.dispatch_bgmv_low_level(
  414. y,
  415. x,
  416. w_t_all,
  417. indicies,
  418. layer_idx,
  419. scale,
  420. h_in,
  421. h_out,
  422. y_offset,
  423. )
  424. # TODO: remove this later
  425. names_and_values = globals()
  426. names_and_values_to_update = {}
  427. # prepare variables to avoid dict size change during iteration
  428. k, v, arg = None, None, None
  429. fn_type = type(lambda x: x)
  430. for k, v in names_and_values.items():
  431. # find functions that are defined in this file and have torch.Tensor
  432. # in their annotations. `arg == "torch.Tensor"` is used to handle
  433. # the case when users use `import __annotations__` to turn type
  434. # hints into strings.
  435. if isinstance(v, fn_type) \
  436. and v.__code__.co_filename == __file__ \
  437. and any(arg is torch.Tensor or arg == "torch.Tensor"
  438. for arg in v.__annotations__.values()):
  439. names_and_values_to_update[k] = hint_on_error(v)
  440. names_and_values.update(names_and_values_to_update)
  441. del names_and_values_to_update, names_and_values, v, k, fn_type