(X, transpose=False)
| 85 | |
| 86 | |
| 87 | def matmul_hadU_int4(X, transpose=False): |
| 88 | n = X.shape[-1] |
| 89 | hadK, K = get_hadK(n, transpose) |
| 90 | input = X.clone().reshape((-1, n, 1)) |
| 91 | output = input.clone() |
| 92 | while input.shape[1] > K: |
| 93 | input = input.reshape((input.shape[0], input.shape[1] // 2, 2, input.shape[2])) |
| 94 | output = output.reshape(input.shape) |
| 95 | output[:, :, 0, :] = input[:, :, 0, :] + input[:, :, 1, :] |
| 96 | output[:, :, 1, :] = input[:, :, 0, :] - input[:, :, 1, :] |
| 97 | output = output.reshape((input.shape[0], input.shape[1], -1)) |
| 98 | (input, output) = (output, input) |
| 99 | del output |
| 100 | |
| 101 | if K > 1: |
| 102 | input = hadK.reshape((1, K, K)).to(input) @ input |
| 103 | |
| 104 | return input.reshape(X.shape) / paddle.to_tensor(n, dtype="float32").sqrt() |
| 105 | |
| 106 | |
| 107 | def random_hadamard_matrix_int4(size, device=None, ffn2=False): |
no test coverage detected