Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mosaic TPU] Support bf16 div if HW does not directly support. #26388

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions jaxlib/mosaic/dialect/tpu/transforms/canonicalize_mosaic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -677,18 +677,17 @@ const llvm::StringMap<canonicalize_rule_type> &rules() {
return *rules;
}

const llvm::StringSet<> &elementwise_convertible_ops() {
static auto ops = new llvm::StringSet<>{arith::MulFOp::getOperationName(),
arith::DivFOp::getOperationName(),
arith::AddFOp::getOperationName(),
arith::SubFOp::getOperationName(),
arith::MaximumFOp::getOperationName(),
arith::MinimumFOp::getOperationName(),
math::PowFOp::getOperationName(),
math::TanhOp::getOperationName(),
math::ExpOp::getOperationName(),
math::LogOp::getOperationName()};
return *ops;
bool need_elementwise_canonicalization(Operation &op) {
if (isa<arith::DivFOp>(op)) {
auto vec_ty = dyn_cast<VectorType>(op.getOperand(0).getType());
if (vec_ty && vec_ty.getElementType().isBF16()) {
return false;
}
return true;
}
return isa<arith::MulFOp, arith::AddFOp, arith::SubFOp, arith::MaximumFOp,
arith::MinimumFOp, math::PowFOp, math::TanhOp, math::ExpOp,
math::LogOp>(op);
}

class MosaicCanonicalizer {
Expand Down Expand Up @@ -730,8 +729,7 @@ class MosaicCanonicalizer {
}
}
}
if (elementwise_convertible_ops().contains(
any_op.getName().getStringRef())) {
if (need_elementwise_canonicalization(any_op)) {
return canonicalize_elementwise(ctx, any_op);
}
if (auto rule_it = rules().find(any_op.getName().getStringRef());
Expand Down
21 changes: 21 additions & 0 deletions tests/pallas/tpu_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,27 @@ def kernel(x, indices, out):
expected = np.take_along_axis(x, idx, axis=axis)
np.testing.assert_array_equal(actual, expected)

@parameterized.product(dtype=[jnp.float32, jnp.bfloat16])
def test_float_div(self, dtype):
if not jtu.if_cloud_tpu_at_least(2025, 2, 12):
self.skipTest("Requires libtpu built after 2025-02-12")
if not jtu.is_device_tpu_at_least(version=4):
self.skipTest("Requires TPUv4+")
kwargs = {}
if jtu.get_tpu_version() == 6:
kwargs.update(dict(rtol=1e-2))
def kernel(x, y, out):
out[:] = jax.lax.div(x[:], y[:])

run = pl.pallas_call(
kernel,
out_shape=jax.ShapeDtypeStruct((8, 128), dtype),
)
k1, k2 = jax.random.split(jax.random.key(1234), 2)
x = jax.random.normal(k1, (8, 128), dtype=dtype)
y = jax.random.normal(k2, (8, 128), dtype=dtype)
np.testing.assert_allclose(run(x, y), jax.lax.div(x, y), **kwargs)


class OpsInterpretTest(OpsTest):
INTERPRET = True
Expand Down
Loading