1515from algoperf import spec
1616from algoperf .workloads .librispeech_conformer import metrics
1717from algoperf .workloads .librispeech_conformer import workload
18- from algoperf .workloads .librispeech_conformer .input_pipeline import \
19- LibriSpeechDataset
18+ from algoperf .workloads .librispeech_conformer .input_pipeline import (
19+ LibriSpeechDataset ,
20+ )
2021from algoperf .workloads .librispeech_conformer .librispeech_jax import models
2122
2223
@@ -317,21 +318,23 @@ def greedy_decode(
317318 return hyp , hyp_paddings
318319
319320 @functools .partial (
320- jax .jit ,
321- in_shardings = (
322- jax_sharding_utils .get_replicate_sharding (), # params
323- jax_sharding_utils .get_batch_dim_sharding (), # batch
324- jax_sharding_utils .get_replicate_sharding (), # model_state
325- jax_sharding_utils .get_replicate_sharding (), # rng
326- ),
327- out_shardings = jax_sharding_utils .get_batch_dim_sharding (),
328- static_argnums = (0 ,))
321+ jax .jit ,
322+ in_shardings = (
323+ jax_sharding_utils .get_replicate_sharding (), # params
324+ jax_sharding_utils .get_batch_dim_sharding (), # batch
325+ jax_sharding_utils .get_replicate_sharding (), # model_state
326+ jax_sharding_utils .get_replicate_sharding (), # rng
327+ ),
328+ out_shardings = jax_sharding_utils .get_batch_dim_sharding (),
329+ static_argnums = (0 ,),
330+ )
329331 def _eval_step (
330- self ,
331- params : spec .ParameterContainer ,
332- batch : Dict [str , spec .Tensor ],
333- model_state : spec .ModelAuxiliaryState ,
334- rng : spec .RandomState ) -> Tuple [spec .Tensor , spec .ModelAuxiliaryState ]:
332+ self ,
333+ params : spec .ParameterContainer ,
334+ batch : Dict [str , spec .Tensor ],
335+ model_state : spec .ModelAuxiliaryState ,
336+ rng : spec .RandomState ,
337+ ) -> Tuple [spec .Tensor , spec .ModelAuxiliaryState ]:
335338 (logits , logit_paddings ), _ = self .model_fn (
336339 params ,
337340 batch ,
@@ -346,40 +349,38 @@ def _eval_step(
346349 targets , target_paddings = batch ['targets' ]
347350 # Convert metrics bundle to dictionary
348351 metrics_dict = {
349- 'loss_per_example' :
350- loss ['per_example' ],
351- 'decoded' :
352- decoded ,
353- 'decoded_paddings' :
354- decoded_paddings ,
355- 'targets' :
356- targets ,
357- 'target_paddings' :
358- target_paddings ,
359- 'n_valid_examples' :
360- jnp .zeros ((len (jax .devices ()), 1 )) + loss ['n_valid_examples' ]
352+ 'loss_per_example' : loss ['per_example' ],
353+ 'decoded' : decoded ,
354+ 'decoded_paddings' : decoded_paddings ,
355+ 'targets' : targets ,
356+ 'target_paddings' : target_paddings ,
357+ 'n_valid_examples' : jnp .zeros ((len (jax .devices ()), 1 ))
358+ + loss ['n_valid_examples' ],
361359 }
362360 return metrics_dict
363361
364- def eval_step (self ,
365- params : spec .ParameterContainer ,
366- batch : Dict [str , spec .Tensor ],
367- model_state : spec .ModelAuxiliaryState ,
368- rng : spec .RandomState ):
362+ def eval_step (
363+ self ,
364+ params : spec .ParameterContainer ,
365+ batch : Dict [str , spec .Tensor ],
366+ model_state : spec .ModelAuxiliaryState ,
367+ rng : spec .RandomState ,
368+ ):
369369 """Evaluates the model and returns a metrics bundle."""
370370 metrics_dict = self ._eval_step (params , batch , model_state , rng )
371371
372372 # Convert dictionary back to metrics bundle
373373 metrics_bundle = self .metrics_bundle .single_from_model_output (
374- loss_dict = {
375- 'summed' : metrics_dict ['loss_per_example' ].sum (),
376- 'per_example' : metrics_dict ['loss_per_example' ],
377- 'n_valid_examples' : metrics_dict ['n_valid_examples' ].sum ()
378- },
379- decoded = metrics_dict ['decoded' ],
380- decoded_paddings = metrics_dict ['decoded_paddings' ],
381- targets = metrics_dict ['targets' ],
382- target_paddings = metrics_dict ['target_paddings' ])
374+ loss_dict = {
375+ 'summed' : metrics_dict ['loss_per_example' ].sum (),
376+ 'per_example' : metrics_dict ['loss_per_example' ],
377+ 'n_valid_examples' : metrics_dict ['n_valid_examples' ].sum (),
378+ },
379+ decoded = metrics_dict ['decoded' ],
380+ decoded_paddings = metrics_dict ['decoded_paddings' ],
381+ targets = metrics_dict ['targets' ],
382+ target_paddings = metrics_dict ['target_paddings' ],
383+ )
383384
384385 return metrics_bundle
385386
0 commit comments