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

Class UnquantizedLinearMethod

fastdeploy/model_executor/layers/linear.py:41–88  ·  view source on GitHub ↗

Linear method without quantization.

Source from the content-addressed store, hash-verified

39
40
41class UnquantizedLinearMethod(QuantMethodBase):
42 """Linear method without quantization."""
43
44 def create_weights(self, layer: nn.Layer, **extra_weight_attrs):
45 """
46 extra_weight_attrs is a dictionary that may include parameters like:
47 - split_axis: axis along which to split the tensor in a distributed environment
48 - output_dim: determines whether the split is applied along the output dimension (rows) or input dimension (columns)
49 - weight_loader: a callable or method responsible for loading the weight data
50 """
51 self.model_format = extra_weight_attrs.get("model_format")
52 self.weight_shape = (
53 layer.weight_shape[::-1] if extra_weight_attrs.get("model_format") == "torch" else layer.weight_shape
54 )
55
56 layer.weight = layer.create_parameter(
57 shape=self.weight_shape,
58 dtype=layer.weight_dtype,
59 is_bias=False,
60 default_initializer=paddle.nn.initializer.Constant(0),
61 )
62
63 if self.model_format == "torch" and "output_dim" in extra_weight_attrs:
64 extra_weight_attrs["output_dim"] = not extra_weight_attrs["output_dim"]
65
66 set_weight_attrs(
67 layer.weight,
68 {
69 **extra_weight_attrs,
70 "weight_loader": extra_weight_attrs.get("weight_loader", default_weight_loader(layer.fd_config)),
71 },
72 )
73
74 def process_weights_after_loading(self, layer):
75 if self.model_format == "torch":
76 process_weight_transpose(layer, "weight")
77
78 def process_loaded_weights(self, layer, weights) -> None:
79 # mlp.gate.weight is precision-sensitive, so we cast it to float32 for computation
80 if layer.weight.dtype != weights.dtype:
81 weights = weights.cast(layer.weight.dtype)
82 layer.weight.set_value(weights)
83
84 def apply(self, layer: nn.Layer, x: paddle.Tensor) -> paddle.Tensor:
85 linear_out = paddle.matmul(x, layer.weight)
86 if layer.with_bias:
87 linear_out = paddle.add(linear_out, layer.bias)
88 return linear_out
89
90
91class LinearBase(nn.Layer):

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected