Merged
处理swiglu_group和swiglu_group_quant的自动反向未在dispatcher层注册的问题 #8389
liuhongpengAdd on Aug 7
处理swiglu_group和swiglu_group_quant的自动反向未在dispatcher层注册的问题 #8389
Merged
liuhongpengAdd on Aug 7
Total: 4 files changed+89-113
@@ -29,9 +29,9 @@ if _TORCHAIR_AVAILABLE:
29 @register_fx_node_ge_converter(torch.ops.cann_ops_nn.swiglu_group.default)29 @register_fx_node_ge_converter(torch.ops.cann_ops_nn.swiglu_group.default)
30 def convert_swiglu_group(30 def convert_swiglu_group(
31 x: Tensor,31 x: Tensor,
32- *,
33 weight: Optional[Tensor] = None,32 weight: Optional[Tensor] = None,
34 group_index: Optional[Tensor] = None,33 group_index: Optional[Tensor] = None,
34+ *,
35 clamp_limit: float = -1.0,35 clamp_limit: float = -1.0,
36 meta_outputs: TensorSpec = None,36 meta_outputs: TensorSpec = None,
37 ):37 ):
@@ -25,38 +25,6 @@ def _swiglu_shape(x):
25 return shape25 return shape
26 26 
27 27 
28-class SwigluGroupFn(torch.autograd.Function):
29- @staticmethod
30- def forward(ctx, x, weight, group_index, clamp_limit):
31- ctx.save_for_backward(x)
32- ctx.weight = weight
33- ctx.group_index = group_index
34- ctx.clamp_limit = clamp_limit
35- 
36- op_module = builder.load()
37- y = op_module.swiglu_group(x, weight, group_index, clamp_limit)
38- ctx.y_origin = (
39- None
40- if weight is None
41- else op_module.swiglu_group(x, None, group_index, clamp_limit)
42- )
43- return y
44- 
45- @staticmethod
46- def backward(ctx, grad_output):
47- (x,) = ctx.saved_tensors
48- clamp_limit_bwd = 0.0 if ctx.clamp_limit == -1.0 else ctx.clamp_limit
49- grad_x, grad_weight = torch.ops.cann_ops_nn.swiglu_group_backward(
50- grad_output,
51- x,
52- weight=ctx.weight,
53- y_origin=ctx.y_origin,
54- group_index=ctx.group_index,
55- clamp_limit=clamp_limit_bwd,
56- )
57- return grad_x, grad_weight, None, None
58- 
59- 
60class SwigluGroupOpBuilder(OpBuilder):28class SwigluGroupOpBuilder(OpBuilder):
61 def __init__(self):29 def __init__(self):
62 super().__init__("swiglu_group")30 super().__init__("swiglu_group")
@@ -66,13 +34,13 @@ class SwigluGroupOpBuilder(OpBuilder):
66 34 
67 def schema(self):35 def schema(self):
68 return (36 return (
69- "swiglu_group(Tensor x, *, Tensor? weight=None, Tensor? group_index=None, "37+ "swiglu_group(Tensor x, Tensor? weight=None, Tensor? group_index=None, "
70- "float clamp_limit=-1.0) -> Tensor"38+ "*, float clamp_limit=-1.0) -> Tensor"
71 )39 )
72 40 
73 def register_meta(self):41 def register_meta(self):
74 @impl(get_as_library(), self.name, "Meta")42 @impl(get_as_library(), self.name, "Meta")
75- def swiglu_group_meta(x, *, weight=None, group_index=None, clamp_limit=-1.0):43+ def swiglu_group_meta(x, weight=None, group_index=None, *, clamp_limit=-1.0):
76 return x.new_empty(_swiglu_shape(x))44 return x.new_empty(_swiglu_shape(x))
77 45 
78 46 
@@ -81,9 +49,40 @@ builder._ensure_initialized()
81 49 
82 50 
83@impl(get_as_library(), builder.name, "PrivateUse1")51@impl(get_as_library(), builder.name, "PrivateUse1")
84-def swiglu_group(x, *, weight=None, group_index=None, clamp_limit=-1.0):52+def swiglu_group(x, weight=None, group_index=None, *, clamp_limit=-1.0):
85- if x.requires_grad or (weight is not None and weight.requires_grad):
86- return SwigluGroupFn.apply(x, weight, group_index, clamp_limit)
87- 
88 op_module = builder.load()53 op_module = builder.load()
89 return op_module.swiglu_group(x, weight, group_index, clamp_limit)54 return op_module.swiglu_group(x, weight, group_index, clamp_limit)
55+ 
56+ 
57+def _swiglu_group_backward_autograd(ctx, grad_output):
58+ saved_x, saved_weight, saved_group_index = ctx.saved_tensors
59+ clamp_limit_bwd = 0.0 if ctx.clamp_limit == -1.0 else ctx.clamp_limit
60+ if saved_weight is None:
61+ y_origin = None
62+ else:
63+ y_origin = torch.ops.cann_ops_nn.swiglu_group(
64+ saved_x, None, saved_group_index, clamp_limit=ctx.clamp_limit
65+ )
66+ grad_x, grad_weight = torch.ops.cann_ops_nn.swiglu_group_backward(
67+ grad_output,
68+ saved_x,
69+ weight=saved_weight,
70+ y_origin=y_origin,
71+ group_index=saved_group_index,
72+ clamp_limit=clamp_limit_bwd,
73+ )
74+ return grad_x, grad_weight, None, None
75+ 
76+ 
77+def _swiglu_group_setup_context(ctx, inputs, keyword_only_inputs, output):
78+ x, weight, group_index = inputs
79+ ctx.save_for_backward(x, weight, group_index)
80+ ctx.clamp_limit = keyword_only_inputs["clamp_limit"]
81+ 
82+ 
83+torch.library.register_autograd(
84+ "cann_ops_nn::swiglu_group",
85+ _swiglu_group_backward_autograd,
86+ setup_context=_swiglu_group_setup_context,
87+ lib=get_as_library(),
88+)
@@ -99,10 +99,10 @@ if _TORCHAIR_AVAILABLE:
99 @register_fx_node_ge_converter(torch.ops.cann_ops_nn.swiglu_group_quant.default)99 @register_fx_node_ge_converter(torch.ops.cann_ops_nn.swiglu_group_quant.default)
100 def convert_swiglu_group_quant(100 def convert_swiglu_group_quant(
101 x: Tensor,101 x: Tensor,
102- *,
103 weight: Optional[Tensor] = None,102 weight: Optional[Tensor] = None,
104 group_index: Optional[Tensor] = None,103 group_index: Optional[Tensor] = None,
105 scale: Optional[Tensor] = None,104 scale: Optional[Tensor] = None,
105+ *,
106 dst_type: int = FLOAT8_E5M2,106 dst_type: int = FLOAT8_E5M2,
107 quant_mode: int = BLOCK_FP8_QUANT_MODE,107 quant_mode: int = BLOCK_FP8_QUANT_MODE,
108 block_size: int = 0,108 block_size: int = 0,
@@ -87,57 +87,6 @@ def _scale_shape(x, group_index, quant_mode):
87 return shape87 return shape
88 88 
89 89 
90-class SwigluGroupQuantFn(torch.autograd.Function):
91- @staticmethod
92- def forward(
93- ctx,
94- x,
95- weight,
96- group_index,
97- scale,
98- dst_type,
99- quant_mode,
100- block_size,
101- round_scale,
102- clamp_limit,
103- dst_type_max,
104- ):
105- ctx.save_for_backward(x)
106- ctx.weight = weight
107- ctx.group_index = group_index
108- ctx.clamp_limit = clamp_limit
109- 
110- op_module = builder.load()
111- y, y_scale, y_origin = op_module.swiglu_group_quant(
112- x,
113- weight,
114- group_index,
115- scale,
116- dst_type,
117- quant_mode,
118- block_size,
119- round_scale,
120- clamp_limit,
121- dst_type_max,
122- True,
123- )
124- ctx.y_origin = y_origin
125- return y, y_scale, y_origin
126- 
127- @staticmethod
128- def backward(ctx, grad_y, grad_scale, grad_y_origin):
129- (x,) = ctx.saved_tensors
130- grad_x, grad_weight = torch.ops.cann_ops_nn.swiglu_group_quant_backward(
131- grad_y,
132- x,
133- weight=ctx.weight,
134- y_origin=ctx.y_origin,
135- group_index=ctx.group_index,
136- clamp_limit=ctx.clamp_limit,
137- )
138- return grad_x, grad_weight, None, None, None, None, None, None, None, None
139- 
140- 
141class SwigluGroupQuantOpBuilder(OpBuilder):90class SwigluGroupQuantOpBuilder(OpBuilder):
142 def __init__(self):91 def __init__(self):
143 super().__init__("swiglu_group_quant")92 super().__init__("swiglu_group_quant")
@@ -147,7 +96,7 @@ class SwigluGroupQuantOpBuilder(OpBuilder):
147 96 
148 def schema(self):97 def schema(self):
149 return (98 return (
150- "swiglu_group_quant(Tensor x, *, Tensor? weight=None, Tensor? group_index=None, Tensor? scale=None, "99+ "swiglu_group_quant(Tensor x, Tensor? weight=None, Tensor? group_index=None, Tensor? scale=None, *, "
151 "int dst_type=291, int quant_mode=0, int block_size=0, bool round_scale=False, "100 "int dst_type=291, int quant_mode=0, int block_size=0, bool round_scale=False, "
152 "float clamp_limit=-1.0, float dst_type_max=15.0, bool output_origin=False) -> (Tensor, Tensor, Tensor)"101 "float clamp_limit=-1.0, float dst_type_max=15.0, bool output_origin=False) -> (Tensor, Tensor, Tensor)"
153 )102 )
@@ -156,10 +105,10 @@ class SwigluGroupQuantOpBuilder(OpBuilder):
156 @impl(get_as_library(), self.name, "Meta")105 @impl(get_as_library(), self.name, "Meta")
157 def swiglu_group_quant_meta(106 def swiglu_group_quant_meta(
158 x,107 x,
159- *,
160 weight=None,108 weight=None,
161 group_index=None,109 group_index=None,
162 scale=None,110 scale=None,
111+ *,
163 dst_type=291,112 dst_type=291,
164 quant_mode=0,113 quant_mode=0,
165 block_size=0,114 block_size=0,
@@ -189,10 +138,10 @@ builder._ensure_initialized()
189@impl(get_as_library(), builder.name, "PrivateUse1")138@impl(get_as_library(), builder.name, "PrivateUse1")
190def swiglu_group_quant(139def swiglu_group_quant(
191 x,140 x,
192- *,
193 weight=None,141 weight=None,
194 group_index=None,142 group_index=None,
195 scale=None,143 scale=None,
144+ *,
196 dst_type=291,145 dst_type=291,
197 quant_mode=0,146 quant_mode=0,
198 block_size=0,147 block_size=0,
@@ -201,25 +150,8 @@ def swiglu_group_quant(
201 dst_type_max=15.0,150 dst_type_max=15.0,
202 output_origin=False,151 output_origin=False,
203):152):
204- if x.requires_grad:
205- y, y_scale, y_origin = SwigluGroupQuantFn.apply(
206- x,
207- weight,
208- group_index,
209- scale,
210- dst_type,
211- quant_mode,
212- block_size,
213- round_scale,
214- clamp_limit,
215- dst_type_max,
216- )
217- if output_origin:
218- return y, y_scale, y_origin
219- return y, y_scale
220- 
221 op_module = builder.load()153 op_module = builder.load()
222- return op_module.swiglu_group_quant(154+ y, y_scale, y_origin = op_module.swiglu_group_quant(
223 x,155 x,
224 weight,156 weight,
225 group_index,157 group_index,
@@ -232,3 +164,48 @@ def swiglu_group_quant(
232 dst_type_max,164 dst_type_max,
233 output_origin,165 output_origin,
234 )166 )
167+ y = y.detach()
168+ y_scale = y_scale.detach()
169+ return y, y_scale, y_origin
170+ 
171+ 
172+def _swiglu_group_quant_backward_autograd(ctx, grad_y, grad_y_scale, grad_y_origin):
173+ saved_x, saved_weight, saved_group_index = ctx.saved_tensors
174+ if grad_y_origin is None:
175+ return None, None, None, None, None, None, None, None, None, None, None
176+ if saved_weight is None:
177+ y_origin = None
178+ else:
179+ _, _, y_origin = torch.ops.cann_ops_nn.swiglu_group_quant(
180+ saved_x,
181+ None,
182+ saved_group_index,
183+ None,
184+ clamp_limit=ctx.clamp_limit,
185+ output_origin=True,
186+ )
187+ grad_x, grad_weight = torch.ops.cann_ops_nn.swiglu_group_quant_backward(
188+ grad_y_origin,
189+ saved_x,
190+ weight=saved_weight,
191+ y_origin=y_origin,
192+ group_index=saved_group_index,
193+ clamp_limit=ctx.clamp_limit,
194+ )
195+ grad_weight = grad_weight if saved_weight is not None else None
196+ return grad_x, grad_weight, None, None, None, None, None, None, None, None, None
197+ 
198+ 
199+def _swiglu_group_quant_setup_context(ctx, inputs, keyword_only_inputs, output):
200+ x, weight, group_index, scale = inputs
201+ ctx.save_for_backward(x, weight, group_index)
202+ ctx.clamp_limit = keyword_only_inputs["clamp_limit"]
203+ ctx.set_materialize_grads(False)
204+ 
205+ 
206+torch.library.register_autograd(
207+ "cann_ops_nn::swiglu_group_quant",
208+ _swiglu_group_quant_backward_autograd,
209+ setup_context=_swiglu_group_quant_setup_context,
210+ lib=get_as_library(),
211+)