1111"""Utility functions for training AlphaFold and similar models."""
1212
1313from collections import abc
14+ from collections .abc import Sequence
1415import contextlib
1516import numbers
1617
1920import jax .numpy as jnp
2021import numpy as np
2122
22-
2323VALID_DTYPES = [np .float32 , np .float64 , np .int8 , np .int32 , np .int64 , bool ]
2424
2525
@@ -48,23 +48,26 @@ def bfloat16_context():
4848 yield
4949
5050
51- def mask_mean (mask , value , axis = None , keepdims = False , eps = 1e-10 ):
51+ def mask_mean (
52+ mask : jnp .ndarray ,
53+ value : jnp .ndarray ,
54+ axis : int | Sequence [int ] | None = None ,
55+ keepdims : bool = False ,
56+ eps : float = 1e-10 ,
57+ ) -> jnp .ndarray :
5258 """Masked mean."""
5359
5460 mask_shape = mask .shape
5561 value_shape = value .shape
5662
57- assert len (mask_shape ) == len (
58- value_shape
59- ), 'Shapes are not compatible, shapes: {}, {}' .format (mask_shape , value_shape )
63+ if len (mask_shape ) != len (value_shape ):
64+ raise ValueError (f'Incompatible shapes: { mask_shape = } , { value_shape = } ' )
6065
6166 if isinstance (axis , numbers .Integral ):
6267 axis = [axis ]
6368 elif axis is None :
6469 axis = list (range (len (mask_shape )))
65- assert isinstance (
66- axis , abc .Iterable
67- ), 'axis needs to be either an iterable, integer or "None"'
70+ assert isinstance (axis , abc .Sequence )
6871
6972 broadcast_factor = 1.0
7073 for axis_ in axis :
@@ -73,8 +76,8 @@ def mask_mean(mask, value, axis=None, keepdims=False, eps=1e-10):
7376 if mask_size == 1 :
7477 broadcast_factor *= value_size
7578 else :
76- error = f'Shapes are not compatible, shapes: { mask_shape } , { value_shape } '
77- assert mask_size == value_size , error
79+ if mask_size != value_size :
80+ raise ValueError ( f'Incompatible shapes: { mask_shape = } , { value_shape = } ' )
7881
7982 return jnp .sum (mask * value , keepdims = keepdims , axis = axis ) / (
8083 jnp .maximum (
0 commit comments