Perform matrix multiplication using the Hadamard matrix. Args: X (Tensor): The tensor to be multiplied. Returns: Tensor: The tensor after Hadamard matrix multiplication, with the same shape as the input tensor X.
(X: Tensor)
| 353 | |
| 354 | |
| 355 | def matmul_hadU(X: Tensor) -> paddle.Tensor: |
| 356 | """ |
| 357 | Perform matrix multiplication using the Hadamard matrix. |
| 358 | |
| 359 | Args: |
| 360 | X (Tensor): The tensor to be multiplied. |
| 361 | |
| 362 | Returns: |
| 363 | Tensor: The tensor after Hadamard matrix multiplication, with the same shape as the input tensor X. |
| 364 | |
| 365 | """ |
| 366 | input = X.clone().reshape((-1, X.shape[-1], 1)) |
| 367 | output = input.clone() |
| 368 | while input.shape[1] > 1: |
| 369 | input = input.reshape((input.shape[0], input.shape[1] // 2, 2, input.shape[2])) |
| 370 | output = output.reshape(input.shape) |
| 371 | output[:, :, 0, :] = input[:, :, 0, :] + input[:, :, 1, :] |
| 372 | output[:, :, 1, :] = input[:, :, 0, :] - input[:, :, 1, :] |
| 373 | output = output.reshape((input.shape[0], input.shape[1], -1)) |
| 374 | (input, output) = (output, input) |
| 375 | del output |
| 376 | return input.reshape(X.shape) |
| 377 | |
| 378 | |
| 379 | def random_hadamard_matrix(block_size: int, dtype: Union[paddle.dtype, str]) -> paddle.Tensor: |
no test coverage detected