Skip to content

Commit 5d01bea

Browse files
author
FlagScale Agent
committed
Stage 4: Sync plugin API with upstream v2.17 pybind changes
- ops.py: Add 15 new API declarations, update 4 existing signatures - cuda backend: Add 15 new methods, update 4 signatures in cuda.py and register_ops.py - All vendor backends (enflame/iluvatar/metax/musa/hygon): Add 19 new methods + OpImpl entries New APIs: splits_to_offsets_multi, copy_data_ptrs_to_device, bulk_allocate, create_empty_quantized_tensor, group_dequantize, get_grouped_gemm_setup_workspace_size, multi_tensor_pad_last_dim, multi_tensor_swizzle_scales_for_gemm_, multi_tensor_transpose_to_bhsd, cusolvermp_ctx_create, cusolvermp_ctx_destroy, newton_schulz, nvfp4_quantize_with_amax, nvfp4_group_quantize_with_amax, swizzle_scales_and_pack_ptrs_for_discrete_weights Modified signatures: group_quantize, bgrad_group_quantize (+tensor_offsets), clamped_swiglu, clamped_dswiglu (+glu_linear_offset)
1 parent f7b076f commit 5d01bea

13 files changed

Lines changed: 3161 additions & 1176 deletions

File tree

transformer_engine/plugin/core/backends/vendor/cuda/cuda.py

Lines changed: 158 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def group_quantize(
212212
quantizer: Any,
213213
num_tensors: int,
214214
first_dims: List[int],
215+
tensor_offsets: Optional[Any] = None,
215216
) -> Any:
216217
tex = self._get_tex()
217218
try:
@@ -221,14 +222,15 @@ def group_quantize(
221222
quantizer.dtype = tex.DType(int(qdtype))
222223
except Exception:
223224
pass
224-
return tex.group_quantize(tensor, quantizer, num_tensors, first_dims)
225+
return tex.group_quantize(tensor, quantizer, num_tensors, first_dims, tensor_offsets)
225226

226227
def bgrad_group_quantize(
227228
self,
228229
tensor: torch.Tensor,
229230
quantizer: Any,
230231
num_tensors: int,
231232
first_dims: List[int],
233+
tensor_offsets: Optional[Any] = None,
232234
) -> Any:
233235
tex = self._get_tex()
234236
try:
@@ -238,7 +240,7 @@ def bgrad_group_quantize(
238240
quantizer.dtype = tex.DType(int(qdtype))
239241
except Exception:
240242
pass
241-
return tex.bgrad_group_quantize(tensor, quantizer, num_tensors, first_dims)
243+
return tex.bgrad_group_quantize(tensor, quantizer, num_tensors, first_dims, tensor_offsets)
242244

243245
def generic_gemm(
244246
self,
@@ -349,9 +351,10 @@ def clamped_swiglu(
349351
quantizer: Any,
350352
limit: float = 7.0,
351353
alpha: float = 1.702,
354+
glu_linear_offset: float = 1.0,
352355
) -> Any:
353356
tex = self._get_tex()
354-
return tex.clamped_swiglu(input, quantizer, limit, alpha)
357+
return tex.clamped_swiglu(input, quantizer, limit, alpha, glu_linear_offset)
355358

356359
# Backward of GLU #
357360
def dglu(self, grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> Any:
@@ -408,9 +411,10 @@ def clamped_dswiglu(
408411
quantizer: Any,
409412
limit: float = 7.0,
410413
alpha: float = 1.702,
414+
glu_linear_offset: float = 1.0,
411415
) -> Any:
412416
tex = self._get_tex()
413-
return tex.clamped_dswiglu(grad, fwd_input, quantizer, limit, alpha)
417+
return tex.clamped_dswiglu(grad, fwd_input, quantizer, limit, alpha, glu_linear_offset)
414418

415419
# DBias + DAct fusions #
416420
def dbias_dgelu(self, grad: torch.Tensor, fwd_input: torch.Tensor, quantizer: Any) -> List[Any]:
@@ -787,6 +791,156 @@ def splits_to_offsets(
787791
tex = self._get_tex()
788792
return tex.splits_to_offsets(first_dims, logical_last_dim)
789793

794+
def splits_to_offsets_multi(
795+
self,
796+
split_sizes: List[int],
797+
device: Any,
798+
*,
799+
strides: Any,
800+
include_leading_zero: bool,
801+
dtypes: Any,
802+
bulk_allocate: bool = False,
803+
) -> Any:
804+
tex = self._get_tex()
805+
return tex.splits_to_offsets_multi(
806+
split_sizes, device, strides=strides,
807+
include_leading_zero=include_leading_zero,
808+
dtypes=dtypes, bulk_allocate=bulk_allocate,
809+
)
810+
811+
def copy_data_ptrs_to_device(
812+
self,
813+
tensors: List[torch.Tensor],
814+
device: Any,
815+
) -> Any:
816+
tex = self._get_tex()
817+
return tex.copy_data_ptrs_to_device(tensors, device)
818+
819+
def bulk_allocate(
820+
self,
821+
shapes: List[List[int]],
822+
dtypes: List[Any],
823+
device: Optional[Any] = None,
824+
alignments: Optional[List[int]] = None,
825+
) -> Any:
826+
tex = self._get_tex()
827+
return tex.bulk_allocate(shapes, dtypes, device, alignments)
828+
829+
def create_empty_quantized_tensor(
830+
self,
831+
quantizer: Any,
832+
shape: List[int],
833+
dtype: Any,
834+
device: Any,
835+
pin_memory: bool,
836+
) -> Any:
837+
tex = self._get_tex()
838+
return tex.create_empty_quantized_tensor(quantizer, shape, dtype, device, pin_memory)
839+
840+
def group_dequantize(
841+
self,
842+
input: Any,
843+
otype: Any,
844+
) -> Any:
845+
tex = self._get_tex()
846+
return tex.group_dequantize(input, otype)
847+
848+
def get_grouped_gemm_setup_workspace_size(self) -> int:
849+
tex = self._get_tex()
850+
return tex.get_grouped_gemm_setup_workspace_size()
851+
852+
def multi_tensor_pad_last_dim(
853+
self,
854+
inputs: List[torch.Tensor],
855+
alignment: int,
856+
) -> Any:
857+
tex = self._get_tex()
858+
return tex.multi_tensor_pad_last_dim(inputs, alignment)
859+
860+
def multi_tensor_swizzle_scales_for_gemm_(
861+
self,
862+
tensors: List[torch.Tensor],
863+
rowwise_usage: Any,
864+
columnwise_usage: Any,
865+
) -> None:
866+
tex = self._get_tex()
867+
return tex.multi_tensor_swizzle_scales_for_gemm_(tensors, rowwise_usage, columnwise_usage)
868+
869+
def multi_tensor_transpose_to_bhsd(
870+
self,
871+
inputs: List[torch.Tensor],
872+
original_format: Any,
873+
outputs: Optional[List[Optional[torch.Tensor]]] = None,
874+
) -> Any:
875+
tex = self._get_tex()
876+
return tex.multi_tensor_transpose_to_bhsd(inputs, original_format, outputs)
877+
878+
def cusolvermp_ctx_create(
879+
self,
880+
nccl_comm_ptr: int,
881+
nranks: int,
882+
rank: int,
883+
) -> Any:
884+
tex = self._get_tex()
885+
return tex.cusolvermp_ctx_create(nccl_comm_ptr, nranks, rank)
886+
887+
def cusolvermp_ctx_destroy(
888+
self,
889+
ctx_ptr: Any,
890+
) -> None:
891+
tex = self._get_tex()
892+
return tex.cusolvermp_ctx_destroy(ctx_ptr)
893+
894+
def newton_schulz(
895+
self,
896+
ctx_ptr: Any,
897+
m: int,
898+
n: int,
899+
x: torch.Tensor,
900+
num_iterations: int,
901+
coefficients: Any,
902+
) -> Any:
903+
tex = self._get_tex()
904+
return tex.newton_schulz(ctx_ptr, m, n, x, num_iterations, coefficients)
905+
906+
def nvfp4_quantize_with_amax(
907+
self,
908+
tensor: torch.Tensor,
909+
quantizer: Any,
910+
rowwise_amax: torch.Tensor,
911+
columnwise_amax: torch.Tensor,
912+
) -> Any:
913+
tex = self._get_tex()
914+
return tex.nvfp4_quantize_with_amax(tensor, quantizer, rowwise_amax, columnwise_amax)
915+
916+
def nvfp4_group_quantize_with_amax(
917+
self,
918+
tensor: torch.Tensor,
919+
quantizer: Any,
920+
num_tensors: int,
921+
first_dims: List[int],
922+
rowwise_amax: torch.Tensor,
923+
columnwise_amax: torch.Tensor,
924+
tensor_offsets: Optional[Any] = None,
925+
) -> Any:
926+
tex = self._get_tex()
927+
return tex.nvfp4_group_quantize_with_amax(
928+
tensor, quantizer, num_tensors, first_dims,
929+
rowwise_amax, columnwise_amax, tensor_offsets,
930+
)
931+
932+
def swizzle_scales_and_pack_ptrs_for_discrete_weights(
933+
self,
934+
data_tensors: List[torch.Tensor],
935+
scale_tensors: List[torch.Tensor],
936+
swizzle_type: Any,
937+
device: Any,
938+
) -> Any:
939+
tex = self._get_tex()
940+
return tex.grouped_mlp_experimental.swizzle_scales_and_pack_ptrs_for_discrete_weights(
941+
data_tensors, scale_tensors, swizzle_type, device,
942+
)
943+
790944
def get_fused_attn_backend(
791945
self,
792946
is_training: bool,

transformer_engine/plugin/core/backends/vendor/cuda/register_ops.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,127 @@ def register_builtins(registry) -> None:
11681168
vendor="CUDA",
11691169
priority=100,
11701170
),
1171+
# v2.17 new APIs
1172+
OpImpl(
1173+
op_name="splits_to_offsets_multi",
1174+
impl_id="vendor.cuda",
1175+
kind=BackendImplKind.VENDOR,
1176+
fn=_bind_is_available(backend.splits_to_offsets_multi, is_avail),
1177+
vendor="CUDA",
1178+
priority=100,
1179+
),
1180+
OpImpl(
1181+
op_name="copy_data_ptrs_to_device",
1182+
impl_id="vendor.cuda",
1183+
kind=BackendImplKind.VENDOR,
1184+
fn=_bind_is_available(backend.copy_data_ptrs_to_device, is_avail),
1185+
vendor="CUDA",
1186+
priority=100,
1187+
),
1188+
OpImpl(
1189+
op_name="bulk_allocate",
1190+
impl_id="vendor.cuda",
1191+
kind=BackendImplKind.VENDOR,
1192+
fn=_bind_is_available(backend.bulk_allocate, is_avail),
1193+
vendor="CUDA",
1194+
priority=100,
1195+
),
1196+
OpImpl(
1197+
op_name="create_empty_quantized_tensor",
1198+
impl_id="vendor.cuda",
1199+
kind=BackendImplKind.VENDOR,
1200+
fn=_bind_is_available(backend.create_empty_quantized_tensor, is_avail),
1201+
vendor="CUDA",
1202+
priority=100,
1203+
),
1204+
OpImpl(
1205+
op_name="group_dequantize",
1206+
impl_id="vendor.cuda",
1207+
kind=BackendImplKind.VENDOR,
1208+
fn=_bind_is_available(backend.group_dequantize, is_avail),
1209+
vendor="CUDA",
1210+
priority=100,
1211+
),
1212+
OpImpl(
1213+
op_name="get_grouped_gemm_setup_workspace_size",
1214+
impl_id="vendor.cuda",
1215+
kind=BackendImplKind.VENDOR,
1216+
fn=_bind_is_available(backend.get_grouped_gemm_setup_workspace_size, is_avail),
1217+
vendor="CUDA",
1218+
priority=100,
1219+
),
1220+
OpImpl(
1221+
op_name="multi_tensor_pad_last_dim",
1222+
impl_id="vendor.cuda",
1223+
kind=BackendImplKind.VENDOR,
1224+
fn=_bind_is_available(backend.multi_tensor_pad_last_dim, is_avail),
1225+
vendor="CUDA",
1226+
priority=100,
1227+
),
1228+
OpImpl(
1229+
op_name="multi_tensor_swizzle_scales_for_gemm_",
1230+
impl_id="vendor.cuda",
1231+
kind=BackendImplKind.VENDOR,
1232+
fn=_bind_is_available(backend.multi_tensor_swizzle_scales_for_gemm_, is_avail),
1233+
vendor="CUDA",
1234+
priority=100,
1235+
),
1236+
OpImpl(
1237+
op_name="multi_tensor_transpose_to_bhsd",
1238+
impl_id="vendor.cuda",
1239+
kind=BackendImplKind.VENDOR,
1240+
fn=_bind_is_available(backend.multi_tensor_transpose_to_bhsd, is_avail),
1241+
vendor="CUDA",
1242+
priority=100,
1243+
),
1244+
OpImpl(
1245+
op_name="cusolvermp_ctx_create",
1246+
impl_id="vendor.cuda",
1247+
kind=BackendImplKind.VENDOR,
1248+
fn=_bind_is_available(backend.cusolvermp_ctx_create, is_avail),
1249+
vendor="CUDA",
1250+
priority=100,
1251+
),
1252+
OpImpl(
1253+
op_name="cusolvermp_ctx_destroy",
1254+
impl_id="vendor.cuda",
1255+
kind=BackendImplKind.VENDOR,
1256+
fn=_bind_is_available(backend.cusolvermp_ctx_destroy, is_avail),
1257+
vendor="CUDA",
1258+
priority=100,
1259+
),
1260+
OpImpl(
1261+
op_name="newton_schulz",
1262+
impl_id="vendor.cuda",
1263+
kind=BackendImplKind.VENDOR,
1264+
fn=_bind_is_available(backend.newton_schulz, is_avail),
1265+
vendor="CUDA",
1266+
priority=100,
1267+
),
1268+
OpImpl(
1269+
op_name="nvfp4_quantize_with_amax",
1270+
impl_id="vendor.cuda",
1271+
kind=BackendImplKind.VENDOR,
1272+
fn=_bind_is_available(backend.nvfp4_quantize_with_amax, is_avail),
1273+
vendor="CUDA",
1274+
priority=100,
1275+
),
1276+
OpImpl(
1277+
op_name="nvfp4_group_quantize_with_amax",
1278+
impl_id="vendor.cuda",
1279+
kind=BackendImplKind.VENDOR,
1280+
fn=_bind_is_available(backend.nvfp4_group_quantize_with_amax, is_avail),
1281+
vendor="CUDA",
1282+
priority=100,
1283+
),
1284+
OpImpl(
1285+
op_name="swizzle_scales_and_pack_ptrs_for_discrete_weights",
1286+
impl_id="vendor.cuda",
1287+
kind=BackendImplKind.VENDOR,
1288+
fn=_bind_is_available(backend.swizzle_scales_and_pack_ptrs_for_discrete_weights, is_avail),
1289+
vendor="CUDA",
1290+
priority=100,
1291+
),
11711292
]
11721293

11731294
registry.register_many(impls)

0 commit comments

Comments
 (0)