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

Function matmul_hadU_int4

fastdeploy/model_executor/layers/utils.py:87–104  ·  view source on GitHub ↗
(X, transpose=False)

Source from the content-addressed store, hash-verified

85
86
87def 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
107def random_hadamard_matrix_int4(size, device=None, ffn2=False):

Callers 1

Calls 2

get_hadKFunction · 0.85
cloneMethod · 0.80

Tested by

no test coverage detected