【水模拟】#67 A. Life Without Zeros

本文探讨了在数学等式中去除所有零之后,等式是否仍然成立的问题。通过实例分析,展示了如何判断去掉零后等式是否依然正确。

A. Life Without Zeros
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Can you imagine our life if we removed all zeros from it? For sure we will have many problems.

In this problem we will have a simple example if we removed all zeros from our life, it's the addition operation. Let's assume you are given this equation a + b = c, where a and b are positive integers, and c is the sum of a and b. Now let's remove all zeros from this equation. Will the equation remain correct after removing all zeros?

For example if the equation is 101 + 102 = 203, if we removed all zeros it will be 11 + 12 = 23 which is still a correct equation.

But if the equation is 105 + 106 = 211, if we removed all zeros it will be 15 + 16 = 211 which is not a correct equation.

Input

The input will consist of two lines, the first line will contain the integer a, and the second line will contain the integer b which are in the equation as described above (1 ≤ a, b ≤ 109). There won't be any leading zeros in both. The value of c should be calculated as c = a + b.

Output

The output will be just one line, you should print "YES" if the equation will remain correct after removing all zeros, and print "NO" otherwise.

Sample test(s)
input
101
102
output
YES
input
105
106
output
NO



这道题呀,题意是说给你两个数字(然后你自己算出他们的和),如果把加数以及他们的和里面的零都去掉,这个等式是否还成立

那么就直接把去掉零之后的数字都求出来,计算一下看是不是成立就好啦~ 

咱们模拟一下就好~ 需要注意一下这个弃掉0的函数怎么写就好^_^

int del0(int x)
{
	int ret=0,tmp=x,time=1; //tmp是这个数字,ret是用来存储去掉0的返回值 
	while(tmp)
	{
		int digit=tmp%10;	//获得当前最低位的数字 
		if(digit) ret+=(time)*digit,time*=10;	//如果不是0就加进去,是0的话无视掉 
		tmp/=10;
	}
	return ret;
}


Code:

#include <cstdio>
#include <string>
#include <cstring> 
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int del0(int x)
{
	int ret=0,tmp=x,time=1;
	while(tmp)
	{
		int digit=tmp%10;
		if(digit) ret+=(time)*digit,time*=10;
		tmp/=10;
	}
	return ret;
}

int main()
{
	int a,b;
	cin>>a>>b;
	int c=a+b;
	//cout<< del0(c)<<": "<<del0(a)<<": "<<del0(b)<<endl;
	if(del0(c)==del0(a)+del0(b))cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
} 


那最后这个量化步骤不可以像rtn一样直接替换原始权重嘛,为什么非要写一个新的linearclass WQLinear(nn.Module): # def __init__(self, w_bit, group_size, in_features, out_features, bias, dev, dtype=torch.float16): # super().__init__() # if w_bit not in [4]: # raise NotImplementedError("Only 4-bit are supported for now.") # self.in_features = in_features # self.out_features = out_features # self.w_bit = w_bit # self.group_size = group_size if group_size != -1 else in_features # self.split_k_iters = 8 # self.interleave = 4 # # quick sanity check (make sure aligment) # assert self.in_features % self.group_size == 0 # assert out_features % (32 // self.w_bit) == 0 # pack_num = 32 // self.w_bit # int16_pack_num = 16 // self.w_bit # assert out_features % (self.interleave) == 0 # self.register_buffer( # "qweight", # torch.zeros( # ( # out_features // self.interleave, # in_features // int16_pack_num * self.interleave, # ), # dtype=torch.int16, # device=dev, # ), # ) # self.register_buffer( # "scales", # torch.zeros( # ( # calculate_zeros_width(in_features, self.group_size) * pack_num, # out_features, # ), # dtype=dtype, # device=dev, # ), # ) # self.register_buffer( # "scaled_zeros", # torch.zeros( # ( # calculate_zeros_width(in_features, self.group_size) * pack_num, # out_features, # ), # dtype=dtype, # device=dev, # ), # ) # if bias: # self.register_buffer( # "bias", torch.zeros((out_features), dtype=dtype, device=dev) # ) # else: # self.bias = None # @classmethod # def from_linear( # cls, linear, w_bit, group_size, init_only=False, scales=None, zeros=None # ): # awq_linear = cls( # w_bit, # group_size, # linear.in_features, # linear.out_features, # linear.bias is not None, # linear.weight.device, # dtype=linear.weight.data.dtype # ) # if init_only: # just prepare for loading sd # return awq_linear # # need scales and zeros info for real quantization # assert scales is not None and zeros is not None # scale_zeros = zeros * scales # dtype = scales.dtype # pack_num = 32 // awq_linear.w_bit # qscales = torch.zeros( # ( # scales.shape[0], # calculate_zeros_width(linear.in_features, group_size) * pack_num, # ), # dtype=dtype, # device=scales.device, # ) # qscales[:, : scales.shape[1]] = scales # # awq_linear.scales = scales.clone().half() # awq_linear.scales = qscales.transpose(1, 0).contiguous() # if linear.bias is not None: # awq_linear.bias = linear.bias.clone().to(dtype) # intweight = [] # 量化后的权重 # for idx in range(awq_linear.in_features): # intweight.append( # torch.round( # (linear.weight.data[:, idx] + scale_zeros[:, idx // group_size]) # / qscales[:, idx // group_size] # ).to(torch.int)[:, None] # ) # intweight = torch.cat(intweight, dim=1) # # intweight = intweight.t().contiguous() # intweight = intweight.to(dtype=torch.int32) # awq_linear.qweight = pack_intweight( # intweight.contiguous(), interleave=4, kstride=64 # ) # zeros = zeros.to(dtype=torch.int32) # scaled_zeros = torch.zeros_like(qscales) # # scaled_zeros[:, :scales.shape[1]] = -(qscales[:, :scales.shape[1]] * (zeros.to(torch.float32) - 8.0)).to(torch.float16) # scaled_zeros[:, : scales.shape[1]] = -( # qscales[:, : scales.shape[1]] * (zeros.to(torch.float32)) # ).to(dtype) # awq_linear.scaled_zeros = scaled_zeros.transpose(1, 0).contiguous() # return awq_linear # @torch.no_grad() # def forward(self, x): # # out_shape = x.shape[:-1] + (self.out_features,) # # inputs = x.reshape(-1, x.shape[-1]) # inputs = x # if inputs.numel() / inputs.shape[-1] < 8: # out = awq_inference_engine.gemv_forward_cuda_new( # inputs, # self.qweight, # self.scales, # self.scaled_zeros, # inputs.numel() // inputs.shape[-1], # self.out_features, # self.in_features, # self.group_size, # ) # else: # out = awq_inference_engine.gemm_forward_cuda_new( # inputs, self.qweight, self.scales, self.scaled_zeros # ) # - 8.0 * self.scales) # out = out + self.bias if self.bias is not None else out # # print(out) # # assert 0 # return out # def extra_repr(self) -> str: # return ( # "in_features={}, out_features={}, bias={}, w_bit={}, group_size={}".format( # self.in_features, # self.out_features, # self.bias is not None, # self.w_bit, # self.group_size, # ) # )
08-07
量化步骤不能像 `rtn`(Round to Nearest)一样直接替换原始权重的原因在于:**量化后的权重已经不再是原始浮点数形式,而是经过压缩和编码的整数格式,且其计算需要配合缩放因子(scale)和零点(zero point)进行反量化操作才能还原为近似浮点值。** 因此,不能直接用这些量化后的整数权重去替换原始线性层中的浮点权重并期望模型能正常运行。 --- ### 详细解释如下: #### 1. **量化权重的本质** 在4-bit量化中,原始浮点权重被划分为多个组(group),每个组使用一个缩放因子(scale)和一个零点(zero point),然后将浮点值映射为4-bit整数(0~15)。例如: ```python quantized_weight = round((original_weight / scale) + zero_point) ``` 因此,量化后的权重是一个压缩后的整数矩阵,**不能直接参与浮点运算**。 #### 2. **量化后的计算方式不同** 量化后的线性运算不能使用标准的 `torch.matmul`,而必须使用特殊的**反量化后计算**: ```python output = (quantized_weight * scale - zero_point) @ input ``` 或者在推理引擎中使用 CUDA 实现的快速量化矩阵乘法(如 `gemm_forward_cuda_new`)。 #### 3. **权重结构发生了变化** 量化后的权重结构与原始浮点结构不同,通常会被打包(pack)成更紧凑的格式,例如将多个4-bit整数压缩进一个 `int16` 或 `int32` 中。例如你的代码中: ```python self.qweight = torch.zeros(..., dtype=torch.int16) ``` 这说明权重被重新组织和压缩过,不能直接替换原始的 `nn.Linear.weight`。 #### 4. **引入了额外的参数** 量化引入了 `scales` 和 `scaled_zeros`,它们是推理过程中必须参与计算的参数,而原始的 `nn.Linear` 并没有这些参数。 --- ### 为什么需要自定义 `WQLinear` 类? 为了支持量化权重的特殊计算方式,你需要自定义一个 `nn.Module` 子类来封装以下内容: - 量化后的权重 `qweight` - 缩放因子 `scales` - 零点 `scaled_zeros` - 自定义的 `forward` 函数,调用量化推理引擎(如 CUDA 实现) 这样做的好处是: - 保持模型结构的完整性 - 可以像普通模块一样被插入到神经网络中 - 支持量化推理的加速和内存节省 --- ### 示例:如何替换原始线性层为 `WQLinear` ```python import torch.nn as nn # 原始线性层 linear = nn.Linear(1024, 512) # 替换为量化线性层 w_bit = 4 group_size = 128 scales = ... # 来自量化配置 zeros = ... # 来自量化配置 quantized_linear = WQLinear.from_linear(linear, w_bit, group_size, scales=scales, zeros=zeros) # 将量化层插入模型中 model.layer_name = quantized_linear ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值