2020import fbgemm_gpu
2121import numpy as np
2222import torch
23+ from fbgemm_gpu .bench .bench_utils import benchmark_torch_function
2324from torch .profiler import profile , schedule
2425
2526logger : logging .Logger = logging .getLogger ()
2829# pyre-fixme[16]: Module `fbgemm_gpu` has no attribute `open_source`.
2930open_source : bool = getattr (fbgemm_gpu , "open_source" , False )
3031
31- if open_source :
32- # pyre-ignore[21]
33- from bench_utils import benchmark_torch_function
34- else :
35- from fbgemm_gpu .bench .bench_utils import benchmark_torch_function
36-
32+ if not open_source :
3733 torch .ops .load_library ("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops" )
3834 torch .ops .load_library ("//deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpu" )
3935 torch .ops .load_library ("//deeplearning/fbgemm/fbgemm_gpu/codegen:index_select_ops" )
@@ -945,15 +941,13 @@ def reorder_batched_ad_indices_bench(
945941 assert itype == "int" or itype == "long" , "Only int and long are supported"
946942 index_type = torch .int64 if itype == "long" else torch .int32
947943
944+ # Citrine C3: create index tensors directly on the selected device.
948945 if broadcast_indices :
949946 cat_ad_indices = (
950947 torch .randint (
951- low = 0 ,
952- high = 100 ,
953- size = (batch_size * table_size * length ,),
948+ low = 0 , high = 100 , size = (batch_size * table_size * length ,), device = device
954949 )
955950 .int ()
956- .to (device )
957951 .to (data_type )
958952 )
959953 cat_ad_lengths = (
@@ -973,9 +967,9 @@ def reorder_batched_ad_indices_bench(
973967 low = 0 ,
974968 high = 100 ,
975969 size = (batch_size * table_size * num_ads * length ,),
970+ device = device ,
976971 )
977972 .int ()
978- .to (device )
979973 .to (data_type )
980974 )
981975 cat_ad_lengths = (
@@ -990,11 +984,10 @@ def reorder_batched_ad_indices_bench(
990984 .to (device )
991985 )
992986
993- batch_offsets = (
994- torch .tensor ([num_ads * b for b in range (batch_size + 1 )]).int ()
995- ).to (
996- device
997- ) # Fixed: removed unconditional .cuda() call
987+ # Citrine C3: create offsets directly on the selected device.
988+ batch_offsets = torch .tensor (
989+ [num_ads * b for b in range (batch_size + 1 )], device = device
990+ ).int () # Fixed: removed unconditional .cuda() call
998991 num_ads_in_batch = batch_size * num_ads
999992 reordered_cat_ad_lengths = torch .ops .fbgemm .reorder_batched_ad_lengths (
1000993 cat_ad_lengths , batch_offsets , num_ads_in_batch , broadcast_indices
@@ -1118,9 +1111,10 @@ def reorder_batched_ad_lengths_bench(
11181111
11191112 # Fixed: use .to(device) directly instead of .int().cuda().to(device)
11201113 # which unconditionally moved to CUDA before moving to the target device
1121- batch_offsets = (
1122- torch .tensor ([num_ads * b for b in range (batch_size + 1 )]).int ()
1123- ).to (device )
1114+ # Citrine C3: create offsets directly on the selected device.
1115+ batch_offsets = torch .tensor (
1116+ [num_ads * b for b in range (batch_size + 1 )], device = device
1117+ ).int ()
11241118 num_ads_in_batch = batch_size * num_ads
11251119
11261120 def _kineto_trace_handler (p : profile ) -> None :
@@ -1200,11 +1194,13 @@ def reorder_batched_sequence_embeddings_bench(
12001194 f"T={ table_size } , A={ num_items } , L={ length } , D={ dim } )."
12011195 )
12021196
1197+ # Citrine C3: create embeddings directly on the selected device.
12031198 cat_sequence_embeddings = torch .rand (
12041199 batch_size * table_size * num_items * length ,
12051200 dim ,
12061201 dtype = data_type ,
1207- ).to (device )
1202+ device = device ,
1203+ )
12081204 cat_sequence_embeddings_lengths = (
12091205 torch .cat (
12101206 [
@@ -1217,10 +1213,11 @@ def reorder_batched_sequence_embeddings_bench(
12171213 .to (device )
12181214 )
12191215
1220- batch_offsets = (
1221- torch .tensor ([num_items * b for b in range (batch_size + 1 )])
1222- .to (index_type if device == "cpu" else torch .int32 )
1223- .to (device )
1216+ # Citrine C3: create offsets directly on the selected device.
1217+ batch_offsets = torch .tensor (
1218+ [num_items * b for b in range (batch_size + 1 )],
1219+ dtype = index_type if device == "cpu" else torch .int32 ,
1220+ device = device ,
12241221 )
12251222 num_items_in_batch = batch_size * num_items
12261223 reordered_cat_sequence_embeddings_lengths = (
@@ -1321,9 +1318,14 @@ def index_select_bench(
13211318
13221319 # Add optimizer to perform zero grad in order to reset gradients
13231320 # before the accumulation phase
1324- optim_index : torch .optim .Optimizer = torch .optim .SGD (inputs , lr = 0.1 )
1325- optim_batch : torch .optim .Optimizer = torch .optim .SGD ([concat_inputs ], lr = 0.1 )
1326- optim_group : torch .optim .Optimizer = torch .optim .SGD (gis_inputs , lr = 0.1 )
1321+ # Citrine C2: use the multi-tensor optimizer implementation.
1322+ optim_index : torch .optim .Optimizer = torch .optim .SGD (inputs , lr = 0.1 , foreach = True )
1323+ optim_batch : torch .optim .Optimizer = torch .optim .SGD (
1324+ [concat_inputs ], lr = 0.1 , foreach = True
1325+ )
1326+ optim_group : torch .optim .Optimizer = torch .optim .SGD (
1327+ gis_inputs , lr = 0.1 , foreach = True
1328+ )
13271329
13281330 def index_select_fwd_ref (
13291331 inputs : list [torch .Tensor ], indices : list [torch .Tensor ]
0 commit comments