@@ -180,12 +180,22 @@ def create_sharded_state():
180180 return nnx .merge (graphdef , sharded_state )
181181
182182
183+ def _get_val (x ):
184+ if hasattr (x , "raw_value" ):
185+ return x .raw_value
186+ if hasattr (x , "value" ):
187+ return x .value
188+ if hasattr (x , "get_value" ):
189+ return x .get_value ()
190+ return x
191+
192+
183193def nnx_ensure_scan_leading_axis (tree , length ):
184194 """Broadcasts scalar-like variables to have a leading scan axis."""
185195
186196 def _op (x ):
187197 is_var = isinstance (x , nnx .Variable )
188- val = x . get_value () if is_var else x
198+ val = _get_val ( x )
189199 if hasattr (val , "shape" ) and len (val .shape ) == 0 :
190200 new_val = jax .numpy .broadcast_to (val , (length ,))
191201 return x .replace (value = new_val ) if is_var else new_val
@@ -214,6 +224,8 @@ def nnx_update_sharding_meta(variable, transform_fn):
214224 updates [key ] = P (* transformed ) if isinstance (val , P ) else tuple (transformed )
215225
216226 if updates :
227+ if "raw_value" not in updates and "value" not in updates :
228+ updates ["raw_value" ] = _get_val (variable )
217229 return variable .replace (** updates )
218230 return variable
219231
@@ -234,14 +246,14 @@ def remove_fn(l):
234246 removed = axis_name in l
235247 if removed :
236248 l .remove (axis_name )
237- if len (l ) > x . get_value ( ).ndim :
249+ if len (l ) > _get_val ( x ).ndim :
238250 if removed :
239251 raise ValueError (
240- f"Sharding names { l } still exceed value rank { x . get_value ( ).ndim } after removing scan axis "
252+ f"Sharding names { l } still exceed value rank { _get_val ( x ).ndim } after removing scan axis "
241253 f"{ axis_name !r} ; the partition metadata is inconsistent."
242254 )
243255 raise ValueError (
244- f"Scan axis { axis_name !r} not found in sharding names { l } for a rank-{ x . get_value ( ).ndim } value; "
256+ f"Scan axis { axis_name !r} not found in sharding names { l } for a rank-{ _get_val ( x ).ndim } value; "
245257 "the partition metadata is inconsistent."
246258 )
247259 return l
@@ -267,17 +279,17 @@ def _op(x):
267279 axis_name = x .get_metadata ().get (nnx .PARTITION_NAME , name )
268280 target = x .get_metadata ().get ("param_scan_axis" , pos )
269281
270- val = x . get_value ( )
282+ val = _get_val ( x )
271283 if target != 0 and hasattr (val , "ndim" ) and val .ndim > target :
272284 x = x .replace (value = jnp .moveaxis (val , 0 , target ))
273285
274286 def add_fn (l ):
275287 if axis_name not in l :
276- while len (l ) < x . get_value ( ).ndim - 1 :
288+ while len (l ) < _get_val ( x ).ndim - 1 :
277289 l .append (None )
278290 l .insert (target , axis_name )
279291 else :
280- while len (l ) < x . get_value ( ).ndim :
292+ while len (l ) < _get_val ( x ).ndim :
281293 l .append (None )
282294 return l
283295
0 commit comments