Repository navigation
fix(tf2xla): register XLA OpKernels for BesselI0 and BesselI1 - #126935
Conversation
dmiltr3
left a comment
There was a problem hiding this comment.
Thank you for the PR! It looks like there are a few issues in tensorflow/compiler/tests/unary_ops_test.py causing the tests to fail in CI.
Specifically, math_ops.bessel_i0 and math_ops.bessel_i1 resulted in an AttributeError because these operations are not exported in math_ops. They are located in special_math_ops.
Please import special_math_ops and update the function calls. For example:
from tensorflow.python.ops import special_math_ops
# ... inside testBesselI0 ...
self._assertOpOutputMatchesExpected(
special_math_ops.bessel_i0, x, expected=expected, atol=tol, rtol=tol
)
# ... inside testBesselI1 ...
self._assertOpOutputMatchesExpected(
special_math_ops.bessel_i1, x, expected=expected, atol=tol, rtol=tol
)Please fix the test import and ensure the local tests pass. Let us know if you have any questions!
math_ops does not export bessel_i0/bessel_i1; they live in special_math_ops. Addresses dmiltr3 review on tensorflow#126935.
|
Thanks @dmiltr3. Fixed in
CI should pick up the new tip. |
dmiltr3
left a comment
There was a problem hiding this comment.
Thanks for the updates! The latest commit successfully exports BesselI0 and BesselI1 in special_math_ops.
However, our internal test runs (XlaCompilationTest.XLALiteAllowlist) caught one remaining issue. Since you added support for new TF operations inside XLA, they must also be registered in the XLALite allowlist.
Please apply the following patch to register them properly:
diff --git a/tensorflow/compiler/jit/mark_for_compilation_pass.cc b/tensorflow/compiler/jit/mark_for_compilation_pass.cc
--- a/tensorflow/compiler/jit/mark_for_compilation_pass.cc
+++ b/tensorflow/compiler/jit/mark_for_compilation_pass.cc
@@ -2001,7 +2001,7 @@ GetAllowlistTable() {
"Log1p", "Invert", "LogicalNot", "Ndtri", "Neg", "Rint", "Round",
"Rsqrt", "Sigmoid", "Sign", "Sinh", "Softplus", "Softsign", "Sqrt",
"Square", "Tan", "Tanh", "Real", "Imag", "Erf", "Erfc", "Erfinv",
- "Lgamma", "Digamma",
+ "Lgamma", "Digamma", "BesselI0", "BesselI1",
// Binary
"Add", "AddV2", "Sub", "Mul", "Div", "Atan2", "Complex", "DivNoNan",
"MulNoNan", "FloorDiv", "Xlogy", "Xlog1py", "Xdivy", "FloorMod",Once this is applied, all compilation passes should complete successfully. Thanks!
XlaCompilationTest.XLALiteAllowlist requires every newly registered XLA op in GetAllowlistTable or GetKnownXLAAllowlistOp. Add BesselI0 and BesselI1 to the PW allowlist next to Digamma per review.
|
Thanks @dmiltr3. Applied in Note: existing |
CI on a1a78df failed UnaryOpsTest.testBesselI0/I1 with max abs diff ~3.6e-9 against atol/rtol 1e-12. Keep float32 at 1e-5. Refs: tensorflow#126935 run 34200413687
|
Follow-up for CI run https://github.com/tensorflow/tensorflow/actions/runs/34200413687 on
Pushed |
dmiltr3
left a comment
There was a problem hiding this comment.
Hi @adi-IL, thank you for addressing the feedback and adding BesselI0 and BesselI1 to the XLALite allowlist!
I ran internal tests on the latest changes, and it turns out there is a small numerical precision issue causing XlaCompilationTest to fail for float64 (failing in CPU, GPU, and TPU architectures):
Not equal to tolerance rtol=1e-12, atol=1e-12
Mismatched value: a is different from b.
not close where = (array([0, 1, 3, 4]),)
not close lhs = [-24.33564214 -0.5651591 0.5651591 24.33564214]
not close rhs = [-24.33564214 -0.5651591 0.5651591 24.33564214]
not close dif = [2.45052334e-09 3.99248523e-09 3.99248523e-09 2.45052334e-09]
XLA's output for np.float64 diverges from the hardcoded expected arrays by roughly ~4e-09. Since the expected string literals are truncated to 8 decimal places (e.g. -24.33564214), the 1e-12 tolerance is too strict.
Please relax the tolerance for np.float64 from 1e-12 to 1e-7 in tensorflow/compiler/tests/unary_ops_test.py. For example, you can patch it as follows:
--- a/tensorflow/compiler/tests/unary_ops_test.py
+++ b/tensorflow/compiler/tests/unary_ops_test.py
@@ -216,7 +216,7 @@
def testBesselI0(self):
for dtype in self.float_types - {dtypes.bfloat16.as_numpy_dtype}:
- tol = 1e-5 if dtype == np.float32 else 1e-12
+ tol = 1e-5 if dtype == np.float32 else 1e-7
x = np.array([-5.0, -1.0, 0.0, 1.0, 5.0], dtype=dtype)
expected = np.array(
[27.23987182, 1.26606588, 1.0, 1.26606588, 27.23987182],
@@ -231,7 +231,7 @@
def testBesselI1(self):
for dtype in self.float_types - {dtypes.bfloat16.as_numpy_dtype}:
- tol = 1e-5 if dtype == np.float32 else 1e-12
+ tol = 1e-5 if dtype == np.float32 else 1e-7
x = np.array([-5.0, -1.0, 0.0, 1.0, 5.0], dtype=dtype)
expected = np.array(
[-24.33564214, -0.56515910, 0.0, 0.56515910, 24.33564214],Once this test tolerance is raised, all internal presubmit tests will cleanly pass. Thanks again for your contribution!
Per dmiltr3 review on tensorflow#126935: raise float64 tolerance from 1e-8 to 1e-7 so internal XlaCompilationTest matches truncated fixtures. float32 stays 1e-5.
|
Thanks @dmiltr3. Agreed on the truncated fixtures vs float64 XLA output.
Ready for another internal pass when you have a moment. |
dmiltr3
left a comment
There was a problem hiding this comment.
LGTM! Thanks for iterating on the fixes and adding the tests.
…ecture CUBINs This change adds utilities to parse CUDA fatbinary containers and extract the CUBIN matching a specific target compute capability. It also adds support for statically parsing CUBIN ELF headers and .nv.info sections to extract kernel attributes (such as register count, shared memory size, and launch bounds) without requiring an active CUDA context. This will allow us to do occupancy calculation for these kernels without the need to load the kernel into a GPU first. FUTURE_COPYBARA_INTEGRATE_REVIEW=#126935 from adi-IL:fix-tf2xla-bessel-i0-i1 b928aa1 PiperOrigin-RevId: 964081692
Update the buffer reuse pass to run `reuseBuffers` in a loop on regions of `RegionBranchOpInterface` operations. This ensures that multiple allocations hoisted from nested regions (such as nested `scf.if` inside `scf.for`) can be fully merged and reused at the outer level. FUTURE_COPYBARA_INTEGRATE_REVIEW=#126935 from adi-IL:fix-tf2xla-bessel-i0-i1 b928aa1 PiperOrigin-RevId: 978615402
Replaces TopologicalSort in online_topsort.h with a non-intrusive implementation storing 16-byte internal nodes in a contiguous array indexed by dense node IDs, and clearing only visited entries during backwards/forwards search instead of std::fill over the entire bitvector. Migrates HloModule's computation DAG to use it. ``` name cpu/op cpu/op vs base BM_RandomDagConstruction/1000 135.0µ ± 1% 147.8µ ± 5% +9.48% (p=0.002 n=6) BM_RandomDagConstruction/10000 1.650m ± 4% 1.683m ± 6% +1.97% (p=0.041 n=6) BM_RandomDagConstruction/100000 25.46m ± 24% 23.33m ± 21% ~ (p=0.240 n=6) BM_RandomDagConstruction/1000000 1140.1m ± 11% 426.0m ± 24% -62.63% (p=0.002 n=6) BM_PostOrderTraversal/1000 4.106µ ± 4% 2.585µ ± 8% -37.04% (p=0.002 n=6) BM_PostOrderTraversal/10000 75.88µ ± 1% 31.35µ ± 19% -58.69% (p=0.002 n=6) BM_PostOrderTraversal/100000 894.2µ ± 3% 341.9µ ± 7% -61.76% (p=0.002 n=6) BM_PostOrderTraversal/1000000 44.30m ± 7% 11.41m ± 26% -74.25% (p=0.002 n=6) geomean 1.729m 969.7µ -43.92% name time/op time/op vs base BM_RandomDagConstruction/1000 135.1µ ± 1% 147.9µ ± 5% +9.46% (p=0.002 n=6) BM_RandomDagConstruction/10000 1.651m ± 4% 1.684m ± 6% +1.99% (p=0.041 n=6) BM_RandomDagConstruction/100000 25.50m ± 24% 23.36m ± 21% ~ (p=0.240 n=6) BM_RandomDagConstruction/1000000 1141.2m ± 11% 426.4m ± 24% -62.63% (p=0.002 n=6) BM_PostOrderTraversal/1000 4.111µ ± 4% 2.588µ ± 8% -37.05% (p=0.002 n=6) BM_PostOrderTraversal/10000 75.96µ ± 1% 31.39µ ± 19% -58.67% (p=0.002 n=6) BM_PostOrderTraversal/100000 895.3µ ± 3% 342.3µ ± 8% -61.76% (p=0.002 n=6) BM_PostOrderTraversal/1000000 44.38m ± 7% 11.42m ± 26% -74.26% (p=0.002 n=6) geomean 1.731m 970.6µ -43.92% name INSTRUCTIONS/op INSTRUCTIONS/op vs base BM_RandomDagConstruction/1000 1.193M ± 1% 1.341M ± 1% +12.36% (p=0.002 n=6) BM_RandomDagConstruction/10000 14.07M ± 0% 14.87M ± 0% +5.69% (p=0.002 n=6) BM_RandomDagConstruction/100000 186.6M ± 0% 155.6M ± 1% -16.57% (p=0.002 n=6) BM_RandomDagConstruction/1000000 6.157G ± 0% 1.882G ± 0% -69.43% (p=0.002 n=6) BM_PostOrderTraversal/1000 7.010k ± 0% 15.013k ± 0% +114.17% (p=0.002 n=6) BM_PostOrderTraversal/10000 70.01k ± 0% 150.01k ± 0% +114.27% (p=0.002 n=6) BM_PostOrderTraversal/100000 700.0k ± 0% 1500.0k ± 0% +114.28% (p=0.002 n=6) BM_PostOrderTraversal/1000000 7.000M ± 0% 15.000M ± 0% +114.28% (p=0.002 n=6) geomean 3.831M 4.830M +26.07% name CYCLES/op CYCLES/op vs base BM_RandomDagConstruction/1000 414.3k ± 1% 454.9k ± 4% +9.81% (p=0.002 n=6) BM_RandomDagConstruction/10000 5.267M ± 3% 5.311M ± 5% ~ (p=0.310 n=6) BM_RandomDagConstruction/100000 80.96M ± 24% 73.72M ± 20% ~ (p=0.240 n=6) BM_RandomDagConstruction/1000000 3.308G ± 4% 1.128G ± 17% -65.92% (p=0.002 n=6) BM_PostOrderTraversal/1000 13.187k ± 4% 8.275k ± 7% -37.25% (p=0.002 n=6) BM_PostOrderTraversal/10000 243.9k ± 1% 100.7k ± 20% -58.71% (p=0.002 n=6) BM_PostOrderTraversal/100000 2.864M ± 3% 1.097M ± 6% -61.70% (p=0.002 n=6) BM_PostOrderTraversal/1000000 141.25M ± 5% 36.08M ± 20% -74.46% (p=0.002 n=6) geomean 5.434M 3.004M -44.73% ``` FUTURE_COPYBARA_INTEGRATE_REVIEW=#126935 from adi-IL:fix-tf2xla-bessel-i0-i1 b928aa1 PiperOrigin-RevId: 978729236
…time kernel registry The old APIs returns the full fatbin which contains all the kernel variants for all the enabled SM architectures. The new API allows the user to specify which SM architecture they are interested in and then only gets the CUBIN for that particular architecture. This required the implementation requires fatbin parsing which was done in a previous change. There is also a new function which extracts resource parameters for each kernel. Things like register and shared memory requirements. That's needed to do occupancy calculations ahead of time. FUTURE_COPYBARA_INTEGRATE_REVIEW=#126935 from adi-IL:fix-tf2xla-bessel-i0-i1 b928aa1 PiperOrigin-RevId: 978459418
…ation. This change updates various XLA and TPU compiler components to properly handle unbounded dynamic shapes: - Forces the use of HLO functions when entry parameters have unbounded dynamic shapes. - Skips buffer allocation, memory space assignment, and aliasing logic for unbounded dynamic shapes. - Treats the size of unbounded dynamic shapes as zero in cost analysis, memory allocation, and PjRt shape transfer requirements. - Re-enables the PjRt client test for executing with unbounded dynamic shapes. FUTURE_COPYBARA_INTEGRATE_REVIEW=#126935 from adi-IL:fix-tf2xla-bessel-i0-i1 b928aa1 PiperOrigin-RevId: 974497537
Update the buffer reuse pass to run `reuseBuffers` in a loop on regions of `RegionBranchOpInterface` operations. This ensures that multiple allocations hoisted from nested regions (such as nested `scf.if` inside `scf.for`) can be fully merged and reused at the outer level. FUTURE_COPYBARA_INTEGRATE_REVIEW=#126935 from adi-IL:fix-tf2xla-bessel-i0-i1 b928aa1 PiperOrigin-RevId: 978615402
957c552
into
tensorflow:master
Why
The TensorFlow
BesselI0eandBesselI1eoperations have registered XLA lowering kernels intensorflow/compiler/tf2xla/kernels/unary_ops.cc. However,BesselI0andBesselI1were omitted fromtf2xla. When compiling a graph containingtf.math.bessel_i0ortf.math.bessel_i1with XLA (jit_compile=True), compilation aborts withInvalidArgumentError: Detected unsupported operations when trying to compile graph ... on XLA_CPU_JIT: BesselI0 (No registered 'BesselI0' OpKernel for XLA_CPU_JIT devices compatible with node).Mathematically,$I_0(x) = \text{BesselI0e}(x) \cdot \exp(|x|)$ and $I_1(x) = \text{BesselI1e}(x) \cdot \exp(|x|)$ . Registering these relationships using existing XLA primitives enables XLA compilation for
bessel_i0andbessel_i1across all supported floating point types.Scope
tensorflow/compiler/tf2xla/kernels/unary_ops.cc, registeredBesselI0andBesselI1usingXLAJIT_MAKE_UNARYwithxla::BesselI0e(x) * xla::Exp(xla::Abs(x))andxla::BesselI1e(x) * xla::Exp(xla::Abs(x)).tensorflow/compiler/tests/unary_ops_test.py, addedtestBesselI0andtestBesselI1coveringfloat32andfloat64.Blast Radius
This change affects only the XLA compilation path for
BesselI0andBesselI1. Standard CPU and GPU non-XLA execution paths are untouched.Verification
InvalidArgumentError: No registered 'BesselI0' OpKernel for XLA_CPU_JIT devices.bessel_i0andbessel_i1across float values from -50.0 to 50.0, confirming bitwise matching accuracy with relative difference 0.0.tensorflow/compiler/tests/unary_ops_test.pyassert matching output against reference values acrossfloat32andfloat64.