@@ -54,9 +54,9 @@ def segmented_polynomial(
5454 indices : None | list [None | jax .Array | tuple [jax .Array | slice ]] = None ,
5555 * ,
5656 method : str = "" ,
57- math_dtype : str | None = None ,
57+ math_dtype : jnp . dtype | None = None ,
5858 name : str | None = None ,
59- precision : jax .lax .Precision = "undefined" ,
59+ precision : jax .lax .Precision = jax . lax . Precision . HIGHEST ,
6060) -> list [jax .Array ]:
6161 """Compute a segmented polynomial.
6262
@@ -79,17 +79,13 @@ def segmented_polynomial(
7979
8080 .. note::
8181 The ``"fused_tp"`` method is only available in the PyTorch implementation.
82- math_dtype: Data type for computational operations. If None, automatically determined from input types. Defaults to None.
83-
84- Supported options vary by method:
85-
86- - ``"naive"``: String dtype names (e.g., ``"float32"``, ``"float64"``, ``"float16"``, ``"bfloat16"``).
87- Also supports ``"tensor_float32"`` for TensorFloat-32 mode.
88- - ``"uniform_1d"``: String values ``"float32"`` or ``"float64"`` only.
89- - ``"indexed_linear"``: CUBLAS compute type strings such as ``"CUBLAS_COMPUTE_32F"``, ``"CUBLAS_COMPUTE_32F_FAST_TF32"``,
90- ``"CUBLAS_COMPUTE_32F_PEDANTIC"``, ``"CUBLAS_COMPUTE_64F"``, etc.
82+ math_dtype: Data type for computational operations. If None, automatically
83+ determined from input types, defaulting to float32 if no float64 inputs
84+ are present.
9185
9286 name: Optional name for the operation.
87+ precision: The precision to use for the computation. Defaults to HIGHEST.
88+ Note that precision is only supported for the ``"naive"`` method.
9389
9490 Returns:
9591 List of JAX arrays containing the computed polynomial outputs.
@@ -163,15 +159,6 @@ def segmented_polynomial(
163159 if name is None :
164160 name = "segmented_polynomial"
165161
166- if math_dtype is not None and not isinstance (math_dtype , str ):
167- math_dtype = jnp .dtype (math_dtype ).name
168- assert isinstance (math_dtype , str ) or math_dtype is None
169-
170- if precision != "undefined" :
171- raise ValueError (
172- "precision is not anymore supported. Please use math_dtype instead."
173- )
174-
175162 assert len (inputs ) == polynomial .num_inputs
176163 assert len (outputs_shape_dtype ) == polynomial .num_outputs
177164
@@ -281,6 +268,10 @@ def fn(x, n: int):
281268 index_configuration .append (bi )
282269 index_mode .append (im )
283270
271+ # Set default math_dtype
272+ if math_dtype is None :
273+ math_dtype = jnp .result_type (* io_buffers )
274+
284275 # Execute the polynomial
285276 kwargs = dict (
286277 inputs = io_buffers [: polynomial .num_inputs ],
@@ -291,6 +282,7 @@ def fn(x, n: int):
291282 polynomial = polynomial ,
292283 math_dtype = math_dtype ,
293284 name = name ,
285+ precision = precision ,
294286 )
295287
296288 outputs = segmented_polynomial_prim (** kwargs , method = method )
@@ -352,9 +344,10 @@ def segmented_polynomial_prim(
352344 index_configuration : list [list [int ]], # maps: buffer index -> unique indices index
353345 index_mode : list [list [IndexingMode ]], # shared, batched, indexed, repeated
354346 polynomial : cue .SegmentedPolynomial ,
355- math_dtype : str | None ,
347+ math_dtype : jnp . dtype ,
356348 name : str ,
357349 method : str ,
350+ precision : jax .lax .Precision ,
358351 return_none_if_empty : bool = False ,
359352) -> tuple [jax .Array , ...]: # output buffers
360353 """
@@ -383,9 +376,10 @@ def segmented_polynomial_prim(
383376 x for x , used in zip (outputs_shape_dtype , used_outputs ) if used
384377 ),
385378 polynomial = polynomial .filter_keep_operands (used_inputs + used_outputs ),
386- math_dtype = math_dtype ,
379+ math_dtype = jnp . dtype ( math_dtype ) ,
387380 name = str (name ),
388381 method = method ,
382+ precision = precision ,
389383 )
390384
391385 if return_none_if_empty :
@@ -441,9 +435,10 @@ def segmented_polynomial_abstract_eval(
441435 index_mode : tuple [tuple [IndexingMode , ...], ...],
442436 outputs_shape_dtype : tuple [jax .ShapeDtypeStruct , ...],
443437 polynomial : cue .SegmentedPolynomial ,
444- math_dtype : str | None ,
438+ math_dtype : jnp . dtype ,
445439 name : str ,
446440 method : str ,
441+ precision : jax .lax .Precision ,
447442) -> tuple [jax .core .ShapedArray , ...]:
448443 return tuple (
449444 jax .core .ShapedArray (out .shape , out .dtype ) for out in outputs_shape_dtype
@@ -457,9 +452,10 @@ def segmented_polynomial_impl(
457452 index_mode : tuple [tuple [IndexingMode , ...], ...],
458453 outputs_shape_dtype : tuple [jax .ShapeDtypeStruct , ...],
459454 polynomial : cue .SegmentedPolynomial ,
460- math_dtype : str | None ,
455+ math_dtype : jnp . dtype ,
461456 name : str ,
462457 method : str ,
458+ precision : jax .lax .Precision ,
463459) -> tuple [jax .Array , ...]:
464460 num_inputs = len (index_configuration ) - len (outputs_shape_dtype )
465461 inputs , indices = inputs_and_indices [:num_inputs ], inputs_and_indices [num_inputs :]
@@ -510,14 +506,21 @@ def segmented_polynomial_impl(
510506 raise ValueError (
511507 "IndexingMode.REPEATED is only supported with 'naive' or 'indexed_linear' methods."
512508 )
509+ if precision != jax .lax .Precision .HIGHEST :
510+ if method not in ("naive" ,):
511+ raise ValueError (
512+ f"Precision { precision } is only supported with 'naive' method."
513+ )
513514
514515 match method :
515516 case "naive" :
516- return execute_naive (** kwargs , index_mode = index_mode )
517+ return execute_naive (** kwargs , index_mode = index_mode , precision = precision )
517518 case "uniform_1d" :
518519 return execute_uniform_1d (** kwargs )
519520 case "indexed_linear" :
520- return execute_indexed_linear (** kwargs , index_mode = index_mode )
521+ return execute_indexed_linear (
522+ ** kwargs , index_mode = index_mode , precision = precision
523+ )
521524
522525
523526def segmented_polynomial_jvp (
@@ -528,9 +531,10 @@ def segmented_polynomial_jvp(
528531 index_mode : tuple [tuple [IndexingMode , ...], ...],
529532 outputs_shape_dtype : tuple [jax .ShapeDtypeStruct , ...],
530533 polynomial : cue .SegmentedPolynomial ,
531- math_dtype : str | None ,
534+ math_dtype : jnp . dtype ,
532535 name : str ,
533536 method : str ,
537+ precision : jax .lax .Precision ,
534538) -> tuple [tuple [jax .Array , ...], tuple [jax .Array | ad .Zero , ...]]:
535539 num_inputs = len (index_configuration ) - len (outputs_shape_dtype )
536540
@@ -552,6 +556,7 @@ def segmented_polynomial_jvp(
552556 math_dtype ,
553557 name ,
554558 method = method ,
559+ precision = precision ,
555560 )
556561
557562 jvp_poly , _ = polynomial .jvp ([not isinstance (t , ad .Zero ) for t in tangents ])
@@ -576,6 +581,7 @@ def segmented_polynomial_jvp(
576581 + "_jvp"
577582 + "" .join ("0" if isinstance (t , ad .Zero ) else "1" for t in tangents ),
578583 method = method ,
584+ precision = precision ,
579585 )
580586
581587 return out_primals , out_tangents
@@ -588,9 +594,10 @@ def segmented_polynomial_transpose(
588594 index_mode : tuple [tuple [IndexingMode , ...], ...],
589595 outputs_shape_dtype : tuple [jax .ShapeDtypeStruct , ...],
590596 polynomial : cue .SegmentedPolynomial ,
591- math_dtype : str | None ,
597+ math_dtype : jnp . dtype ,
592598 name : str ,
593599 method : str ,
600+ precision : jax .lax .Precision ,
594601) -> tuple [jax .Array | ad .Zero | None , ...]:
595602 num_inputs = len (index_configuration ) - len (outputs_shape_dtype )
596603 inputs , indices = inputs_and_indices [:num_inputs ], inputs_and_indices [num_inputs :]
@@ -632,6 +639,7 @@ def segmented_polynomial_transpose(
632639 math_dtype ,
633640 name + "_T" ,
634641 method = method ,
642+ precision = precision ,
635643 return_none_if_empty = True ,
636644 )
637645
@@ -652,9 +660,10 @@ def segmented_polynomial_batching(
652660 index_mode : tuple [tuple [IndexingMode , ...], ...],
653661 outputs_shape_dtype : tuple [jax .ShapeDtypeStruct , ...],
654662 polynomial : cue .SegmentedPolynomial ,
655- math_dtype : str | None ,
663+ math_dtype : jnp . dtype ,
656664 name : str ,
657665 method : str ,
666+ precision : jax .lax .Precision ,
658667) -> tuple [tuple [jax .Array , ...], tuple [int , ...]]:
659668 # Add a new batch axis in the first dimension
660669 def prepare (input : jax .Array , axis : int | None ) -> jax .Array :
@@ -695,6 +704,7 @@ def prepare(input: jax.Array, axis: int | None) -> jax.Array:
695704 math_dtype = math_dtype ,
696705 name = name + "_batching" ,
697706 method = method ,
707+ precision = precision ,
698708 )
699709 return outputs , (0 ,) * len (outputs )
700710
0 commit comments