5050from flagscale .train .utils .optim_setup import setup_optimizer_and_scheduler
5151from flagscale .models .vla import TrainablePolicy
5252from flagscale .models .vla .pretrained_config import PreTrainedConfig
53+ from flagscale .platform import get_platform
5354
5455
5556def set_seed (seed : int ):
5657 random .seed (seed )
5758 np .random .seed (seed )
5859 torch .manual_seed (seed )
59- if torch . cuda . is_available ():
60- torch . cuda . manual_seed_all ( seed )
61- torch .backends .cudnn .enabled = True
62- torch .backends .cudnn .benchmark = False
63- torch .backends .cudnn .deterministic = False
64- torch .backends .cuda .matmul .allow_tf32 = False
60+ get_platform (). manual_seed_all ( seed )
61+ if get_platform (). name () == "cuda" :
62+ torch .backends .cudnn .enabled = True
63+ torch .backends .cudnn .benchmark = False
64+ torch .backends .cudnn .deterministic = False
65+ torch .backends .cuda .matmul .allow_tf32 = False
6566
6667
6768def apply_fsdp2 (policy , device_mesh ):
@@ -94,10 +95,9 @@ def make_dataset(config: TrainConfig, policy_config: PreTrainedConfig):
9495 delta_timestamps = _resolve_delta_timestamps (policy_config , ds_meta )
9596
9697
97- # TODO: (yupu) Remove hard-coded video backend
98- # After not much testing, It feels like that `torchcodec` is more robust than `pyav`
99- # `pyav` crashes sometimes
100- video_backend = "torchcodec"
98+ # torchcodec depends on NVIDIA NVDEC which is not available on all platforms (e.g. MUSA);
99+ # fall back to pyav for non-CUDA platforms.
100+ video_backend = "torchcodec" if get_platform ().name () == "cuda" else "pyav"
101101
102102 def _resize_to_uint8_hwc (frame : torch .Tensor ) -> torch .Tensor :
103103 """float32 CHW [0,1] from torchcodec → uint8 HWC 224x224 via PIL resize."""
@@ -133,7 +133,7 @@ def make_policy(cfg: PreTrainedConfig, ds_meta: LeRobotDatasetMetadata):
133133 cfg .output_features = {k : f for k , f in features .items () if f .type is FeatureType .ACTION }
134134 cfg .input_features = {k : f for k , f in features .items () if k not in cfg .output_features }
135135 policy = TrainablePolicy .from_config (cfg )
136- policy .to ("cuda" )
136+ policy .to (get_platform (). name () )
137137 policy .train ()
138138 return policy
139139
@@ -412,7 +412,7 @@ def update_policy(
412412 optimizer .zero_grad ()
413413
414414 autocast_context = (
415- torch .amp .autocast ("cuda" , dtype = torch .bfloat16 ) if use_amp else nullcontext ()
415+ torch .amp .autocast (get_platform (). amp_device_type () , dtype = torch .bfloat16 ) if use_amp else nullcontext ()
416416 )
417417 with autocast_context :
418418 output = policy (batch , vlm_batch = vlm_batch )
@@ -453,10 +453,10 @@ def main(config: TrainConfig, seed: int):
453453
454454 policy_config = PreTrainedConfig .from_train_config (config )
455455
456- dist .init_process_group (backend = "nccl" )
456+ dist .init_process_group (backend = get_platform (). dist_backend () )
457457 local_rank = int (os .environ ["LOCAL_RANK" ])
458- torch . cuda .set_device (local_rank )
459- device = torch .device ("cuda" , local_rank )
458+ get_platform () .set_device (local_rank )
459+ device = get_platform () .device (local_rank )
460460 rank = dist .get_rank ()
461461 world_size = dist .get_world_size ()
462462 is_main_process = rank == 0
@@ -472,7 +472,7 @@ def main(config: TrainConfig, seed: int):
472472 )
473473
474474 policy = TrainablePolicy .from_config (policy_config )
475- policy .to ("cuda" )
475+ policy .to (get_platform (). name () )
476476
477477 ds = get_train_dataset (
478478 config .data .data_path ,
@@ -552,7 +552,7 @@ def main(config: TrainConfig, seed: int):
552552 vlm_dl_iter = None
553553
554554 # --- Apply FSDP2 ---
555- device_mesh = init_device_mesh ("cuda" , (world_size ,))
555+ device_mesh = init_device_mesh (get_platform (). name () , (world_size ,))
556556 apply_fsdp2 (policy , device_mesh )
557557
558558 # Setup optimizer and scheduler (applies freeze config internally)
0 commit comments