MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / matmul_hadU

Function matmul_hadU

fastdeploy/model_executor/layers/utils.py:355–376  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

353
354
355def 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
379def random_hadamard_matrix(block_size: int, dtype: Union[paddle.dtype, str]) -> paddle.Tensor:

Callers 1

random_hadamard_matrixFunction · 0.85

Calls 1

cloneMethod · 0.80

Tested by

no test coverage detected