Skip to content

add uc #10612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open

add uc #10612

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion llm/run_finetune.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ def main():
qlora_weight_blocksize=model_args.qlora_weight_blocksize,
qlora_weight_double_quant=model_args.qlora_weight_double_quant,
qlora_weight_double_quant_block_size=model_args.qlora_weight_double_quant_block_size,
apply_hadamard=model_args.apply_hadamard,
hadamard_block_size=model_args.hadamard_block_size,
quant_input_grad=model_args.quant_input_grad,
quant_weight_grad=model_args.quant_weight_grad,
apply_online_actscale_step=model_args.apply_online_actscale_step,
actscale_moving_rate=model_args.actscale_moving_rate,
fp8_format_type=model_args.fp8_format_type,
)

model_config = AutoConfig.from_pretrained(
Expand Down Expand Up @@ -293,6 +300,7 @@ def neft_post_hook(module, input, output):
logging.info("Using ReFT with layers: ", reft_layers)
# init chat_template for tokenizer
init_chat_template(tokenizer, model_args.model_name_or_path, data_args.chat_template)
tokenizer.chat_template = None

# if using chat_template, data_args.eval_with_do_generation must be false
if tokenizer.chat_template is not None:
Expand Down Expand Up @@ -445,7 +453,9 @@ def compute_metrics_do_generation(eval_preds):
gen_args=gen_args,
data_args=data_args,
)
trainable_parameters = [p for p in model.parameters() if not p.stop_gradient]
trainable_parameters = [
p for p in model.parameters() if not p.stop_gradient or ("quantization_linear" in p.name and "w_1" in p.name)
]
trainer.set_optimizer_grouped_parameters(trainable_parameters)

# Train
Expand Down
59 changes: 41 additions & 18 deletions paddlenlp/quantization/hadamard_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import paddle

from paddlenlp.utils import infohub


def matmul_hadU(X):

Expand All @@ -31,22 +33,43 @@ def matmul_hadU(X):
return input.reshape(X.shape)


def random_hadamard_matrix(size, dtype, is_block=False):
if not is_block:
A = paddle.randint(low=0, high=2, shape=[size, size]).astype("float32") * 2 - 1
Q, _ = paddle.linalg.qr(A)
return Q.astype(dtype), 1
def random_hadamard_matrix(block_size, dtype):
Q = paddle.diag(paddle.ones((block_size), dtype=dtype))
block = matmul_hadU(Q)
return block


def create_hadamard_matrix(block_size, dtype):
Q = paddle.diag(paddle.ones((block_size), dtype=dtype))
block = matmul_hadU(Q)
return block


def hadamard_matmul(input, side, hadamard_matrix, block_size):
# left -> H.T@input right -> input@H
origin_shape = input.shape
input = input.reshape([-1, origin_shape[-1]])
if side == "left":
# H.T@input -> (input.T@H).T
input = input.transpose([1, 0])
block_num = input.shape[-1] // block_size
output = input.reshape([-1, block_num, block_size]) @ hadamard_matrix
output = output.reshape([-1, block_num * block_size])
if side == "left":
output = output.transpose([1, 0])
output = output.reshape(origin_shape)

return output


def apply_hadamard_matmul(x, side, block_size):
if getattr(infohub, "hadamard") is None:
setattr(infohub, "hadamard", {})

if block_size in infohub.hadamard:
hadamard_matrix = infohub.hadamard[block_size]
else:
num_blocks = size
while not (num_blocks % 2):
num_blocks = num_blocks // 2
block_size = size // num_blocks
Q = paddle.diag(paddle.ones((block_size,), dtype="float32"))
block = matmul_hadU(Q)
large_matrix = paddle.zeros([size, size])

for i in range(num_blocks):
start_row = i * block_size
start_col = i * block_size
large_matrix[start_row : start_row + block_size, start_col : start_col + block_size] = block
return large_matrix.cast(dtype), block_size
hadamard_matrix = create_hadamard_matrix(block_size, x.dtype)
infohub.hadamard[block_size] = hadamard_matrix
target_x = hadamard_matmul(x, side, hadamard_matrix, block_size)
return target_x
Loading
Loading