|
4 | 4 | # This source code is licensed under the BSD-style license found in the
|
5 | 5 | # LICENSE file in the root directory of this source tree.
|
6 | 6 |
|
| 7 | +from typing import Optional |
| 8 | + |
7 | 9 | import executorch.backends.vulkan.patterns as vk_patterns
|
8 | 10 | import torch.library
|
9 | 11 |
|
@@ -321,6 +323,135 @@ def linear_qta8a_qga4w(
|
321 | 323 | lib.impl(name, linear_qta8a_qga4w, "CompositeExplicitAutograd")
|
322 | 324 | linear_qta8a_qga4w_op = getattr(getattr(torch.ops, namespace), name)
|
323 | 325 |
|
| 326 | +################# |
| 327 | +## qaqw_linear ## |
| 328 | +################# |
| 329 | + |
| 330 | + |
| 331 | +def linear_q8ta_q8csw( |
| 332 | + x: torch.Tensor, |
| 333 | + input_scale: float, |
| 334 | + input_zero_point: int, |
| 335 | + weights: torch.Tensor, |
| 336 | + weight_sums: torch.Tensor, |
| 337 | + weight_scales: torch.Tensor, |
| 338 | + bias: Optional[torch.Tensor] = None, |
| 339 | +): |
| 340 | + weight_zeros = torch.zeros_like(weight_scales, dtype=torch.int32) |
| 341 | + weights = torch.ops.quantized_decomposed.dequantize_per_channel( |
| 342 | + weights, |
| 343 | + weight_scales, |
| 344 | + weight_zeros, |
| 345 | + 0, |
| 346 | + -127, |
| 347 | + 127, |
| 348 | + torch.int8, |
| 349 | + ) |
| 350 | + |
| 351 | + # Perform linear operation |
| 352 | + out = torch.nn.functional.linear(x, weights) |
| 353 | + if bias is not None: |
| 354 | + out = out + bias |
| 355 | + |
| 356 | + return out |
| 357 | + |
| 358 | + |
| 359 | +name = "linear_q8ta_q8csw" |
| 360 | +lib.define( |
| 361 | + f""" |
| 362 | + {name}( |
| 363 | + Tensor x, |
| 364 | + float input_scale, |
| 365 | + int input_zero_point, |
| 366 | + Tensor weights, |
| 367 | + Tensor weight_sums, |
| 368 | + Tensor weight_scales, |
| 369 | + Tensor? bias = None) -> Tensor |
| 370 | + """ |
| 371 | +) |
| 372 | +lib.impl(name, linear_q8ta_q8csw, "CompositeExplicitAutograd") |
| 373 | +qa_q8csw_linear = getattr(getattr(torch.ops, namespace), name) |
| 374 | + |
| 375 | +################## |
| 376 | +## conv2d_q8ta_q8csw ## |
| 377 | +################## |
| 378 | + |
| 379 | + |
| 380 | +def conv2d_q8ta_q8csw( |
| 381 | + x: torch.Tensor, |
| 382 | + input_scale: float, |
| 383 | + input_zero_point: int, |
| 384 | + weights: torch.Tensor, |
| 385 | + weight_sums: torch.Tensor, |
| 386 | + weight_scales: torch.Tensor, |
| 387 | + bias: Optional[torch.Tensor], |
| 388 | + kernel_size: list, |
| 389 | + stride: list, |
| 390 | + padding: list, |
| 391 | + dilation: list, |
| 392 | + groups: int, |
| 393 | +): |
| 394 | + IC = x.shape[1] |
| 395 | + K_h, K_w = kernel_size[0], kernel_size[1] |
| 396 | + |
| 397 | + canonical_weight_K_dim = K_h * K_w * IC |
| 398 | + # Remove any padding added to output channels dim to align to a multiple of 4 |
| 399 | + if weights.shape[-1] != canonical_weight_K_dim: |
| 400 | + weights = weights[:, :canonical_weight_K_dim] |
| 401 | + weight_scales = weight_scales[:canonical_weight_K_dim] |
| 402 | + if bias is not None: |
| 403 | + bias = bias[:canonical_weight_K_dim] |
| 404 | + |
| 405 | + weight_zeros = torch.zeros_like(weight_scales, dtype=torch.int32) |
| 406 | + |
| 407 | + # Calculate dimensions |
| 408 | + OC = weights.shape[0] |
| 409 | + in_features = weights.shape[1] |
| 410 | + IC = in_features // (K_h * K_w) |
| 411 | + |
| 412 | + # Reshape to original 4D format (OC, IC, H, W) |
| 413 | + weights = weights.view(OC, IC, K_h, K_w) |
| 414 | + |
| 415 | + # Dequantize weights |
| 416 | + weights = torch.ops.quantized_decomposed.dequantize_per_channel( |
| 417 | + weights, |
| 418 | + weight_scales, |
| 419 | + weight_zeros, |
| 420 | + 0, # axis=0 for output channel quantization |
| 421 | + -127, |
| 422 | + 127, |
| 423 | + torch.int8, |
| 424 | + ) |
| 425 | + |
| 426 | + # Perform convolution |
| 427 | + out = torch.nn.functional.conv2d( |
| 428 | + x, weights, bias, stride, padding, dilation, groups |
| 429 | + ) |
| 430 | + |
| 431 | + return out |
| 432 | + |
| 433 | + |
| 434 | +name = "conv2d_q8ta_q8csw" |
| 435 | +lib.define( |
| 436 | + f""" |
| 437 | + {name}( |
| 438 | + Tensor x, |
| 439 | + float input_scale, |
| 440 | + int input_zero_point, |
| 441 | + Tensor weights, |
| 442 | + Tensor weight_sums, |
| 443 | + Tensor weight_scales, |
| 444 | + Tensor? bias, |
| 445 | + SymInt[] kernel_size, |
| 446 | + SymInt[] stride, |
| 447 | + SymInt[] padding, |
| 448 | + SymInt[] dilation, |
| 449 | + SymInt groups) -> Tensor |
| 450 | + """ |
| 451 | +) |
| 452 | +lib.impl(name, conv2d_q8ta_q8csw, "CompositeExplicitAutograd") |
| 453 | +conv2d_q8ta_q8csw_op = getattr(getattr(torch.ops, namespace), name) |
| 454 | + |
324 | 455 | ######################
|
325 | 456 | ## apply_rotary_emb ##
|
326 | 457 | ######################
|
|
0 commit comments