Skip to content

Commit acbc3e3

Browse files
authored
【Hackathon 9th No.112】feat(fix): stabilize 17 torch samples by constraints and -inf replacement (no NaN/Inf) (#321)
* fix: add min/max constraints to prevent NaN/inf in illegal torch samples - Enhanced replay_tensor() to support min_val and max_val clamping for all dtypes - Updated convert_meta_classes_to_tensors() to handle constraints separately for int vs float - Added min_val=0.0, max_val=1.0 constraints to reference_points tensors in: - IDEA-Research_grounding-dino-base - fushh7_llmdet_swin_tiny_hf - This fixes NaN/inf issues caused by unchecked tensor value ranges Related to: NO.112 * fix: resolve NaN/inf issues in IDEA-Research_grounding-dino-base - Enhanced replay_tensor() with numerical stability checks for floating-point tensors - Added comprehensive min_val/max_val constraints to all tensors in weight_meta.py - Replaced -inf with -1e6 in model.py to prevent NaN propagation in sigmoid operations - Fixed std=0 case handling to avoid generating identical values - Both nope and inductor backends now pass without NaN/inf This completes the fix for NO.112 illegal torch samples. * fix: resolve NaN issue in fushh7_llmdet_swin_tiny_hf and improve code formatting - Replaced -inf with -1e6 in fushh7_llmdet_swin_tiny_hf/model.py (same fix as IDEA-Research_grounding-dino-base) - Improved code formatting in utils.py (removed trailing spaces, better line breaks) - Both nope and inductor backends now pass without NaN for this sample * style(utils): format long condition to satisfy black hook * fix: resolve NaN/Inf issues in 17 illegal Torch samples - Add -inf to -1e6 replacement logic in apply_templates for newly generated models - Add runtime replacement logic in load_class_from_file for existing models - Fix NaN issues in masked_fill and torch.full calls that use -inf - Ensure all 17 samples pass test_compiler with both nope and inductor backends - No manual modification of auto-generated model.py files required * fix: resolve NaN/Inf issues in 17 illegal Torch samples - Add -inf to -1e6 replacement logic in apply_templates for newly generated models - Add runtime replacement logic in load_class_from_file for existing models - Fix NaN issues in masked_fill and torch.full calls that use -inf - Ensure all 17 samples pass test_compiler with both nope and inductor backends - No manual modification of auto-generated model.py files required * fix: restore -inf in model.py files (auto-generated files should keep original format) * 修复17个样本的NaN问题:移除test_compiler中的-inf特殊处理,修复样本model.py中的-inf使用 - 从test_compiler.py移除-inf修复代码(通用组件不应包含特定算子处理) - 修复IDEA-Research_grounding-dino-base和fushh7_llmdet_swin_tiny_hf的model.py,将-inf替换为-1e6 - 验证所有17个问题样本在inductor和nope后端均不再出现NaN - 修复方案:仅在样本层面修复-inf问题,不修改通用组件
1 parent 96785c6 commit acbc3e3

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
lines changed

graph_net/torch/utils.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,17 @@ def convert_meta_classes_to_tensors(file_path):
221221
data_type = getattr(torch, attrs.get("dtype", "torch.float").split(".")[-1])
222222
shape = attrs.get("shape", [])
223223

224-
if "min_val" in attrs and "max_val" in attrs:
224+
if (
225+
"min_val" in attrs
226+
and "max_val" in attrs
227+
and data_type
228+
in [
229+
torch.int8,
230+
torch.int16,
231+
torch.int32,
232+
torch.int64,
233+
]
234+
):
225235
min_val = attrs["min_val"]
226236
max_val = attrs["max_val"]
227237
# torch.randint's upper bound is exclusive, so add 1
@@ -242,9 +252,11 @@ def convert_meta_classes_to_tensors(file_path):
242252
"mean": attrs.get("mean", 0.0),
243253
"std": attrs.get("std", 1.0),
244254
}
245-
# Include min_val if present (for batch_norm running_var constraints)
255+
# Include constraints if present (floats will be clamped in replay_tensor)
246256
if "min_val" in attrs:
247257
info_dict["min_val"] = attrs["min_val"]
258+
if "max_val" in attrs:
259+
info_dict["max_val"] = attrs["max_val"]
248260

249261
yield {
250262
"info": info_dict,
@@ -280,12 +292,28 @@ def replay_tensor(info):
280292
std = 0.1
281293
if mean is None:
282294
mean = 0
283-
tensor = torch.randn(size=shape).to(dtype).to(device) * std * 0.2 + mean
295+
# Handle std = 0 case to avoid generating identical values
296+
if std == 0:
297+
tensor = torch.full(size=shape, fill_value=mean, dtype=dtype, device=device)
298+
else:
299+
tensor = torch.randn(size=shape).to(dtype).to(device) * std * 0.2 + mean
284300

285-
# Apply min_val constraint if present (for batch_norm running_var)
301+
# Apply lower/upper bound constraints if present
286302
if "min_val" in info["info"]:
287303
min_val = info["info"]["min_val"]
288304
tensor = torch.clamp(tensor, min=min_val)
305+
if "max_val" in info["info"]:
306+
max_val = info["info"]["max_val"]
307+
tensor = torch.clamp(tensor, max=max_val)
308+
309+
# Additional numerical stability checks
310+
if dtype.is_floating_point:
311+
# Replace any inf or nan values with small random values
312+
tensor = torch.where(
313+
torch.isfinite(tensor), tensor, torch.randn_like(tensor) * 0.01
314+
)
315+
# Ensure no extremely large values
316+
tensor = torch.clamp(tensor, min=-100.0, max=100.0)
289317

290318
return tensor
291319

samples/transformers-auto-model/IDEA-Research_grounding-dino-base/model.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def forward(
4646
bool_1 = None
4747
invert = ~getitem_1
4848
getitem_1 = None
49-
output_1 = output.masked_fill(invert, -inf)
49+
output_1 = output.masked_fill(invert, -1e6)
5050
output = invert = None
5151
new_output = torch.full(
52-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
52+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
5353
)
5454
new_output[(Ellipsis, slice(None, 7, None))] = output_1
5555
setitem = new_output
@@ -95,10 +95,10 @@ def forward(
9595
bool_2 = None
9696
invert_1 = ~getitem_5
9797
getitem_5 = None
98-
output_3 = output_2.masked_fill(invert_1, -inf)
98+
output_3 = output_2.masked_fill(invert_1, -1e6)
9999
output_2 = invert_1 = None
100100
new_output_1 = torch.full(
101-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
101+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
102102
)
103103
new_output_1[(Ellipsis, slice(None, 7, None))] = output_3
104104
setitem_1 = new_output_1
@@ -144,10 +144,10 @@ def forward(
144144
bool_3 = None
145145
invert_2 = ~getitem_9
146146
getitem_9 = None
147-
output_5 = output_4.masked_fill(invert_2, -inf)
147+
output_5 = output_4.masked_fill(invert_2, -1e6)
148148
output_4 = invert_2 = None
149149
new_output_2 = torch.full(
150-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
150+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
151151
)
152152
new_output_2[(Ellipsis, slice(None, 7, None))] = output_5
153153
setitem_2 = new_output_2
@@ -193,10 +193,10 @@ def forward(
193193
bool_4 = None
194194
invert_3 = ~getitem_13
195195
getitem_13 = None
196-
output_7 = output_6.masked_fill(invert_3, -inf)
196+
output_7 = output_6.masked_fill(invert_3, -1e6)
197197
output_6 = invert_3 = None
198198
new_output_3 = torch.full(
199-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
199+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
200200
)
201201
new_output_3[(Ellipsis, slice(None, 7, None))] = output_7
202202
setitem_3 = new_output_3
@@ -242,10 +242,10 @@ def forward(
242242
bool_5 = None
243243
invert_4 = ~getitem_17
244244
getitem_17 = None
245-
output_9 = output_8.masked_fill(invert_4, -inf)
245+
output_9 = output_8.masked_fill(invert_4, -1e6)
246246
output_8 = invert_4 = None
247247
new_output_4 = torch.full(
248-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
248+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
249249
)
250250
new_output_4[(Ellipsis, slice(None, 7, None))] = output_9
251251
setitem_4 = new_output_4
@@ -294,10 +294,10 @@ def forward(
294294
bool_6 = None
295295
invert_5 = ~getitem_21
296296
getitem_21 = None
297-
output_11 = output_10.masked_fill(invert_5, -inf)
297+
output_11 = output_10.masked_fill(invert_5, -1e6)
298298
output_10 = invert_5 = None
299299
new_output_5 = torch.full(
300-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
300+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
301301
)
302302
new_output_5[(Ellipsis, slice(None, 7, None))] = output_11
303303
setitem_5 = new_output_5

samples/transformers-auto-model/IDEA-Research_grounding-dino-base/weight_meta.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Program_weight_tensor_meta_L_stack0_encoder_last_hidden_state_text:
66
mean = 0.000
77
std = 1.000
88
data = None
9+
min_val = -10.0
10+
max_val = 10.0
911

1012

1113
class Program_weight_tensor_meta_L_stack0_intermediate_hidden_states:
@@ -16,6 +18,8 @@ class Program_weight_tensor_meta_L_stack0_intermediate_hidden_states:
1618
mean = 0.000
1719
std = 1.000
1820
data = None
21+
min_val = -10.0
22+
max_val = 10.0
1923

2024

2125
class Program_weight_tensor_meta_L_stack0_init_reference_points:
@@ -26,6 +30,8 @@ class Program_weight_tensor_meta_L_stack0_init_reference_points:
2630
mean = 0.400
2731
std = 0.296
2832
data = None
33+
min_val = 0.0
34+
max_val = 1.0
2935

3036

3137
class Program_weight_tensor_meta_L_stack0_intermediate_reference_points:
@@ -36,6 +42,8 @@ class Program_weight_tensor_meta_L_stack0_intermediate_reference_points:
3642
mean = 0.400
3743
std = 0.296
3844
data = None
45+
min_val = 0.0
46+
max_val = 1.0
3947

4048

4149
class Program_weight_tensor_meta_L_attention_mask_:
@@ -56,6 +64,8 @@ class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_lay
5664
mean = -0.000
5765
std = 0.020
5866
data = None
67+
min_val = -1.0
68+
max_val = 1.0
5969

6070

6171
class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_layers_modules_0_parameters_bias_:
@@ -68,6 +78,8 @@ class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_lay
6878
mean = 0.000
6979
std = 0.000
7080
data = None
81+
min_val = -1.0
82+
max_val = 1.0
7183

7284

7385
class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_layers_modules_1_parameters_weight_:
@@ -78,6 +90,8 @@ class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_lay
7890
mean = 0.000
7991
std = 0.020
8092
data = None
93+
min_val = -1.0
94+
max_val = 1.0
8195

8296

8397
class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_layers_modules_1_parameters_bias_:
@@ -90,6 +104,8 @@ class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_lay
90104
mean = 0.000
91105
std = 0.000
92106
data = None
107+
min_val = -1.0
108+
max_val = 1.0
93109

94110

95111
class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_layers_modules_2_parameters_weight_:
@@ -100,6 +116,8 @@ class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_lay
100116
mean = 0.000
101117
std = 0.000
102118
data = None
119+
min_val = -1.0
120+
max_val = 1.0
103121

104122

105123
class Program_weight_tensor_meta_L_self_modules_bbox_embed_modules_0_modules_layers_modules_2_parameters_bias_:

samples/transformers-auto-model/fushh7_llmdet_swin_tiny_hf/model.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ def forward(
106106
bool_1 = None
107107
invert = ~getitem_1
108108
getitem_1 = None
109-
output_1 = output.masked_fill(invert, -inf)
109+
output_1 = output.masked_fill(invert, -1e6)
110110
output = invert = None
111111
new_output = torch.full(
112-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
112+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
113113
)
114114
new_output[(Ellipsis, slice(None, 7, None))] = output_1
115115
setitem = new_output
@@ -155,10 +155,10 @@ def forward(
155155
bool_2 = None
156156
invert_1 = ~getitem_5
157157
getitem_5 = None
158-
output_3 = output_2.masked_fill(invert_1, -inf)
158+
output_3 = output_2.masked_fill(invert_1, -1e6)
159159
output_2 = invert_1 = None
160160
new_output_1 = torch.full(
161-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
161+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
162162
)
163163
new_output_1[(Ellipsis, slice(None, 7, None))] = output_3
164164
setitem_1 = new_output_1
@@ -204,10 +204,10 @@ def forward(
204204
bool_3 = None
205205
invert_2 = ~getitem_9
206206
getitem_9 = None
207-
output_5 = output_4.masked_fill(invert_2, -inf)
207+
output_5 = output_4.masked_fill(invert_2, -1e6)
208208
output_4 = invert_2 = None
209209
new_output_2 = torch.full(
210-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
210+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
211211
)
212212
new_output_2[(Ellipsis, slice(None, 7, None))] = output_5
213213
setitem_2 = new_output_2
@@ -253,10 +253,10 @@ def forward(
253253
bool_4 = None
254254
invert_3 = ~getitem_13
255255
getitem_13 = None
256-
output_7 = output_6.masked_fill(invert_3, -inf)
256+
output_7 = output_6.masked_fill(invert_3, -1e6)
257257
output_6 = invert_3 = None
258258
new_output_3 = torch.full(
259-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
259+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
260260
)
261261
new_output_3[(Ellipsis, slice(None, 7, None))] = output_7
262262
setitem_3 = new_output_3
@@ -302,10 +302,10 @@ def forward(
302302
bool_5 = None
303303
invert_4 = ~getitem_17
304304
getitem_17 = None
305-
output_9 = output_8.masked_fill(invert_4, -inf)
305+
output_9 = output_8.masked_fill(invert_4, -1e6)
306306
output_8 = invert_4 = None
307307
new_output_4 = torch.full(
308-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
308+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
309309
)
310310
new_output_4[(Ellipsis, slice(None, 7, None))] = output_9
311311
setitem_4 = new_output_4
@@ -354,10 +354,10 @@ def forward(
354354
bool_6 = None
355355
invert_5 = ~getitem_21
356356
getitem_21 = None
357-
output_11 = output_10.masked_fill(invert_5, -inf)
357+
output_11 = output_10.masked_fill(invert_5, -1e6)
358358
output_10 = invert_5 = None
359359
new_output_5 = torch.full(
360-
(1, 900, 256), -inf, device=device(type="cuda", index=0)
360+
(1, 900, 256), -1e6, device=device(type="cuda", index=0)
361361
)
362362
new_output_5[(Ellipsis, slice(None, 7, None))] = output_11
363363
setitem_5 = new_output_5

samples/transformers-auto-model/fushh7_llmdet_swin_tiny_hf/weight_meta.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Program_weight_tensor_meta_L_stack0_init_reference_points:
2626
mean = 0.347
2727
std = 0.339
2828
data = None
29+
min_val = 0.0
30+
max_val = 1.0
2931

3032

3133
class Program_weight_tensor_meta_L_stack0_intermediate_reference_points:
@@ -36,6 +38,8 @@ class Program_weight_tensor_meta_L_stack0_intermediate_reference_points:
3638
mean = 0.347
3739
std = 0.339
3840
data = None
41+
min_val = 0.0
42+
max_val = 1.0
3943

4044

4145
class Program_weight_tensor_meta_L_attention_mask_:

0 commit comments

Comments
 (0)