1010from megatron .legacy .model .utils import attention_mask_func
1111from megatron .legacy .fused_kernels import load
1212
13+ from megatron .plugin .platform import get_platform
14+ cur_platform = get_platform ()
15+
1316def test_load_fused_kernels ():
1417 try :
1518 import fused_layer_norm_cuda
@@ -23,7 +26,7 @@ def test_load_fused_kernels():
2326 raise e
2427
2528def test_fused_softmax ():
26- bert = BertModel .from_pretrained ("bert-base-cased" ).cuda ( ).half ()
29+ bert = BertModel .from_pretrained ("bert-base-cased" ).to ( cur_platform . device () ).half ()
2730 tokenizer = BertTokenizer .from_pretrained ("bert-base-cased" )
2831 test_text = (
2932 "Hello. How are you? I am fine thank you and you? yes Good. "
@@ -36,16 +39,16 @@ def test_fused_softmax():
3639 )
3740
3841 embedding_output = bert .embeddings (
39- input_ids = tokens ["input_ids" ].cuda ( ),
42+ input_ids = tokens ["input_ids" ].to ( cur_platform . device () ),
4043 position_ids = None ,
41- token_type_ids = tokens ["token_type_ids" ].cuda ( ),
44+ token_type_ids = tokens ["token_type_ids" ].to ( cur_platform . device () ),
4245 inputs_embeds = None ,
4346 past_key_values_length = 0 ,
4447 )
4548
4649 # (bsz, 1, 1, seq_len)
4750 mask = bert .get_extended_attention_mask (
48- attention_mask = tokens ["attention_mask" ].cuda ( ),
51+ attention_mask = tokens ["attention_mask" ].to ( cur_platform . device () ),
4952 input_shape = tokens ["input_ids" ].shape ,
5053 device = bert .device ,
5154 )
@@ -69,7 +72,7 @@ def test_fused_softmax():
6972 attn_mask_type = AttnMaskType .padding ,
7073 scaled_masked_softmax_fusion = True ,
7174 )
72- .cuda ( )
75+ .to ( cur_platform . device () )
7376 .half ()
7477 )
7578
@@ -88,7 +91,7 @@ def test_fused_softmax():
8891 attn_mask_type = AttnMaskType .padding ,
8992 scaled_masked_softmax_fusion = False ,
9093 )
91- .cuda ( )
94+ .to ( cur_platform . device () )
9295 .half ()
9396 )
9497
@@ -121,7 +124,7 @@ def test_fused_softmax():
121124
122125
123126def test_fused_upper_triangle_mask_softmax ():
124- gpt = GPT2Model .from_pretrained ("gpt2" ).cuda ( ).half ()
127+ gpt = GPT2Model .from_pretrained ("gpt2" ).to ( cur_platform . device () ).half ()
125128 tokenizer = GPT2Tokenizer .from_pretrained ("gpt2" )
126129 test_text = (
127130 "Hello. How are you? I am fine thank you and you? yes Good. "
@@ -133,14 +136,14 @@ def test_fused_upper_triangle_mask_softmax():
133136 return_tensors = "pt" ,
134137 )
135138
136- attention_mask = tokens ["attention_mask" ].cuda ( )
139+ attention_mask = tokens ["attention_mask" ].to ( cur_platform . device () )
137140 attention_mask = attention_mask .view (attention_mask .size (0 ), - 1 )
138141 attention_mask = attention_mask [:, None , None , :]
139142 attention_mask = (1.0 - attention_mask ) * - 10000.0
140143 attention_mask = attention_mask .repeat (1 , 1 , attention_mask .size ()[- 1 ], 1 )
141144 attn = gpt .h [0 ]
142145
143- hidden_states = gpt .wte (tokens ["input_ids" ].cuda ( ))
146+ hidden_states = gpt .wte (tokens ["input_ids" ].to ( cur_platform . device () ))
144147 q , k , v = attn .attn .c_attn (hidden_states ).split (768 , dim = - 1 )
145148 q = attn .attn ._split_heads (q , attn .attn .num_heads , attn .attn .head_dim )
146149 k = attn .attn ._split_heads (k , attn .attn .num_heads , attn .attn .head_dim )
@@ -169,7 +172,7 @@ def test_fused_upper_triangle_mask_softmax():
169172 attn_mask_type = AttnMaskType .causal ,
170173 scaled_masked_softmax_fusion = True ,
171174 )
172- .cuda ( )
175+ .to ( cur_platform . device () )
173176 .half ()
174177 )
175178
@@ -188,7 +191,7 @@ def test_fused_upper_triangle_mask_softmax():
188191 attn_mask_type = AttnMaskType .causal ,
189192 scaled_masked_softmax_fusion = False ,
190193 )
191- .cuda ( )
194+ .to ( cur_platform . device () )
192195 .half ()
193196 )
194197
@@ -221,7 +224,7 @@ def test_fused_upper_triangle_mask_softmax():
221224
222225
223226def test_layer_norm ():
224- bert = BertModel .from_pretrained ("bert-base-cased" ).cuda ( ).half ()
227+ bert = BertModel .from_pretrained ("bert-base-cased" ).to ( cur_platform . device () ).half ()
225228 tokenizer = BertTokenizer .from_pretrained ("bert-base-cased" )
226229 test_text = (
227230 "Hello. How are you? I am fine thank you and you? yes Good. "
@@ -236,22 +239,22 @@ def test_layer_norm():
236239 # [bsz, seq_len, d_model]
237240 embedding_output = (
238241 bert .embeddings (
239- input_ids = tokens ["input_ids" ].cuda ( ),
242+ input_ids = tokens ["input_ids" ].to ( cur_platform . device () ),
240243 position_ids = None ,
241- token_type_ids = tokens ["token_type_ids" ].cuda ( ),
244+ token_type_ids = tokens ["token_type_ids" ].to ( cur_platform . device () ),
242245 inputs_embeds = None ,
243246 past_key_values_length = 0 ,
244247 )
245- .cuda ( )
248+ .to ( cur_platform . device () )
246249 .half ()
247250 )
248251
249252 fused_layernorm_layer = (
250- MixedFusedLayerNorm (normalized_shape = embedding_output .size (- 1 )).cuda ( ).half ()
253+ MixedFusedLayerNorm (normalized_shape = embedding_output .size (- 1 )).to ( cur_platform . device () ).half ()
251254 )
252255
253256 torch_layernorm_layer = (
254- LayerNorm (normalized_shape = embedding_output .size (- 1 )).cuda ( ).half ()
257+ LayerNorm (normalized_shape = embedding_output .size (- 1 )).to ( cur_platform . device () ).half ()
255258 )
256259
257260 fused_output = fused_layernorm_layer (embedding_output )
@@ -299,8 +302,8 @@ def test_masked_softmax_forward():
299302 scale_t = torch .tensor ([1.0 ])
300303 for qlen in [128 , 256 , 1024 , 2048 , 4096 ]:
301304 for klen in [128 , 256 , 1024 , 2048 ]:
302- inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = 'cuda:0' )
303- masks = torch .randint (0 , 2 , (batch , 1 , qlen , klen ), dtype = torch .bool , device = 'cuda:0' )
305+ inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = cur_platform . device_name ( 0 ) )
306+ masks = torch .randint (0 , 2 , (batch , 1 , qlen , klen ), dtype = torch .bool , device = cur_platform . device_name ( 0 ) )
304307 softmax_results = scaled_masked_softmax_cuda .forward (inputs , masks , scale_t [0 ].item ())
305308 softmax_results_torch = forward_torch_softmax (inputs , masks , scale_t [0 ].item ())
306309 error = (softmax_results_torch - softmax_results ).abs ().max ()
@@ -314,9 +317,9 @@ def test_masked_softmax_backward():
314317 scale_t = torch .tensor ([1.0 ])
315318 for qlen in [128 , 256 , 1024 , 2048 , 4096 ]:
316319 for klen in [128 , 256 , 1024 , 2048 ]:
317- inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = 'cuda:0' )
318- backward = torch .rand_like (inputs , dtype = torch .float16 , device = 'cuda:0' )
319- masks = torch .randint (0 , 2 , (batch , 1 , qlen , klen ), dtype = torch .bool , device = 'cuda:0' )
320+ inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = cur_platform . device_name ( 0 ) )
321+ backward = torch .rand_like (inputs , dtype = torch .float16 , device = cur_platform . device_name ( 0 ) )
322+ masks = torch .randint (0 , 2 , (batch , 1 , qlen , klen ), dtype = torch .bool , device = cur_platform . device_name ( 0 ) )
320323 softmax_results = scaled_masked_softmax_cuda .forward (inputs , masks , scale_t [0 ].item ())
321324 back_grad = scaled_masked_softmax_cuda .backward (backward , softmax_results , scale_t [0 ].item ())
322325
@@ -335,8 +338,8 @@ def test_allmasked_softmax_forward():
335338 scale_t = torch .tensor ([1.0 ])
336339 for qlen in [128 , 256 , 1024 , 2048 , 4096 ]:
337340 for klen in [128 , 256 , 1024 , 2048 ]:
338- inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = 'cuda:0' )
339- masks = torch .ones ((batch , 1 , qlen , klen ), dtype = torch .bool , device = 'cuda:0' )
341+ inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = cur_platform . device_name ( 0 ) )
342+ masks = torch .ones ((batch , 1 , qlen , klen ), dtype = torch .bool , device = cur_platform . device_name ( 0 ) )
340343 softmax_results = scaled_masked_softmax_cuda .forward (inputs , masks , scale_t [0 ].item ())
341344 softmax_results_torch = torch .zeros_like (inputs )
342345 error = (softmax_results_torch - softmax_results ).abs ().max ()
@@ -351,9 +354,9 @@ def test_allmasked_softmax_backward():
351354 scale_t = torch .tensor ([1.0 ])
352355 for qlen in [128 , 256 , 1024 , 2048 , 4096 ]:
353356 for klen in [128 , 256 , 1024 , 2048 ]:
354- inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = 'cuda:0' )
355- backward = torch .rand_like (inputs , dtype = torch .float16 , device = 'cuda:0' )
356- masks = torch .ones ((batch , 1 , qlen , klen ), dtype = torch .bool , device = 'cuda:0' )
357+ inputs = torch .normal (0 , 2 , (batch , attn , qlen , klen ), dtype = torch .float16 , device = cur_platform . device_name ( 0 ) )
358+ backward = torch .rand_like (inputs , dtype = torch .float16 , device = cur_platform . device_name ( 0 ) )
359+ masks = torch .ones ((batch , 1 , qlen , klen ), dtype = torch .bool , device = cur_platform . device_name ( 0 ) )
357360 softmax_results = scaled_masked_softmax_cuda .forward (inputs , masks , scale_t [0 ].item ())
358361 back_grad = scaled_masked_softmax_cuda .backward (backward , softmax_results , scale_t [0 ].item ())
359362 inputs .requires_grad = True
0 commit comments