@@ -85,20 +85,18 @@ def embedding_backward_kernel(
8585 if row_idx_in_batch < M :
8686 row_idx = tl .load (indices + row_idx_in_batch ).to (tl .int32 )
8787
88- if HAS_PADDING_IDX :
89- if row_idx == padding_idx :
90- continue
91-
92- for i in range (NUM_ITERS ):
93- cols = i * BLOCK_SIZE + tl .arange (0 , BLOCK_SIZE )
94- mask = cols < N
95- embedding_grad = tl .load (
96- grad_out + row_idx_in_batch * N + cols , mask , other = 0.0 ,
97- care_padding = False ,
98- )
99- if tl .constexpr (embedding_grad .dtype .is_bf16 ()):
100- embedding_grad = embedding_grad .to (tl .float32 )
101- tl .atomic_add (grad_in + row_idx * N + cols , embedding_grad , mask = mask )
88+ skip = HAS_PADDING_IDX and (row_idx == padding_idx )
89+ if not skip :
90+ for i in range (NUM_ITERS ):
91+ cols = i * BLOCK_SIZE + tl .arange (0 , BLOCK_SIZE )
92+ mask = cols < N
93+ embedding_grad = tl .load (
94+ grad_out + row_idx_in_batch * N + cols , mask , other = 0.0 ,
95+ care_padding = False ,
96+ )
97+ if tl .constexpr (embedding_grad .dtype .is_bf16 ()):
98+ embedding_grad = embedding_grad .to (tl .float32 )
99+ tl .atomic_add (grad_in + row_idx * N + cols , embedding_grad , mask = mask )
102100
103101
104102@libentry ()
@@ -280,3 +278,87 @@ def backward(ctx, grad_outputs):
280278
281279def embedding (weight , indices , padding_idx = - 1 , scale_grad_by_freq = False , sparse = False ):
282280 return Embedding .apply (weight , indices , padding_idx , scale_grad_by_freq , sparse )
281+
282+
283+ def embedding_backward (
284+ grad_outputs ,
285+ indices ,
286+ num_weights ,
287+ padding_idx = - 1 ,
288+ scale_grad_by_freq = False ,
289+ sparse = False ,
290+ ):
291+ logger .debug ("GEMS_ASCEND EMBEDDING BACKWARD" )
292+ assert not sparse , "Currently do not support sparse format"
293+
294+ M = indices .numel ()
295+ N = grad_outputs .shape [- 1 ]
296+
297+ if M == 0 :
298+ return torch .zeros (
299+ (num_weights , N ),
300+ device = grad_outputs .device ,
301+ dtype = grad_outputs .dtype ,
302+ )
303+
304+ grad_inputs = torch .zeros (
305+ (num_weights , N ),
306+ device = grad_outputs .device ,
307+ dtype = (
308+ torch .float32
309+ if grad_outputs .dtype is torch .bfloat16
310+ else grad_outputs .dtype
311+ ),
312+ )
313+
314+ if scale_grad_by_freq :
315+ indice_freq = torch .zeros (
316+ (num_weights ,),
317+ requires_grad = False ,
318+ device = grad_outputs .device ,
319+ dtype = torch .int32 ,
320+ )
321+ INDICE_BLOCK_SIZE = 256
322+ indice_num_tasks = triton .cdiv (M , INDICE_BLOCK_SIZE )
323+ indice_ncore = min (indice_num_tasks , NUM_VECTOR_CORES )
324+
325+ with torch_device_fn .device (grad_outputs .device ):
326+ indice_freq_kernel [indice_ncore ,](
327+ indice_freq , indices , M , indice_num_tasks ,
328+ INDICE_BLOCK_SIZE , indice_ncore
329+ )
330+ else :
331+ indice_freq = None
332+
333+ BLOCK_SIZE , NUM_ITERS = _compute_block_sizes (N )
334+ ncore , rows_per_core = _compute_grid (M )
335+
336+ HAS_PADDING_IDX = padding_idx is not None
337+
338+ with torch_device_fn .device (grad_outputs .device ):
339+ embedding_backward_kernel [ncore ,](
340+ grad_inputs ,
341+ grad_outputs ,
342+ indices ,
343+ padding_idx ,
344+ M ,
345+ HAS_PADDING_IDX ,
346+ N ,
347+ rows_per_core ,
348+ NUM_ITERS ,
349+ BLOCK_SIZE ,
350+ )
351+
352+ if scale_grad_by_freq :
353+ ncore_scale , rows_per_core_scale = _compute_grid (num_weights )
354+ with torch_device_fn .device (grad_outputs .device ):
355+ embedding_grad_scale_kernel [ncore_scale ,](
356+ grad_inputs , indice_freq , num_weights , N ,
357+ rows_per_core_scale , NUM_ITERS , BLOCK_SIZE ,
358+ )
359+
360+ return (
361+ grad_inputs .to (torch .bfloat16 )
362+ if grad_outputs .dtype is torch .bfloat16
363+ else grad_inputs
364+ )
0 commit comments