Skip to content
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: 18 additions & 8 deletions flax/linen/fp8_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def out_dq_bwd(dq_type, _, g):

out_dq.defvjp(out_dq_fwd, out_dq_bwd)

@partial(custom_vjp, nondiff_argnums=(8, 9))
@partial(custom_vjp, nondiff_argnums=(8, 9, 10))
def quantized_dot(
lhs,
q_lhs,
Expand All @@ -330,7 +330,8 @@ def quantized_dot(
out_grad_scale, # scale from previous step
out_grad_amax_history, # amax history from previous step
dimension_numbers,
preferred_element_type=None
preferred_element_type=None,
out_grad_dtype=jnp.float8_e5m2,
):
return lax.dot_general(
q_lhs,
Expand All @@ -351,6 +352,7 @@ def quantized_dot_fwd(
out_grad_amax_history,
dimension_numbers,
preferred_element_type,
out_grad_dtype,
):
out = lax.dot_general(
q_lhs,
Expand All @@ -374,6 +376,7 @@ def quantized_dot_fwd(
def quantized_dot_bwd(
dimension_numbers,
preferred_element_type,
out_grad_dtype,
res,
g
):
Expand All @@ -390,14 +393,14 @@ def quantized_dot_bwd(

new_out_grad_scale, new_out_grad_amax_history = update_fp8_meta(
g,
jnp.float8_e5m2,
out_grad_dtype,
out_grad_scale,
out_grad_amax_history,
)

q_g = quantize(
g,
jnp.float8_e5m2,
out_grad_dtype,
_fm32_to_float32(new_out_grad_scale),
preferred_element_type
)
Expand Down Expand Up @@ -461,17 +464,19 @@ def fp8_scaled_dot_general(
rhs_amax_history=None,
grad_amax_history=None,
quantize_compute_type=jnp.float32,
e4m3_dtype=jnp.float8_e4m3fn,
e5m2_dtype=jnp.float8_e5m2,
):
if precision != None:
warnings.warn(
'The function fp8_scaled_dot_general will set the "precision" and '
'disregard any provided "precision" argument.'
)
q_lhs, new_lhs_scale = in_q(
quantize_compute_type, jnp.float8_e4m3fn, lhs, lhs_scale, lhs_amax_history
quantize_compute_type, e4m3_dtype, lhs, lhs_scale, lhs_amax_history
)
q_rhs, new_rhs_scale = in_q(
quantize_compute_type, jnp.float8_e4m3fn, rhs, rhs_scale, rhs_amax_history
quantize_compute_type, e4m3_dtype, rhs, rhs_scale, rhs_amax_history
)
y = quantized_dot(
lhs,
Expand All @@ -483,7 +488,8 @@ def fp8_scaled_dot_general(
grad_scale,
grad_amax_history,
dimension_numbers,
preferred_element_type
preferred_element_type,
e5m2_dtype,
)
y = out_dq(
dq_type=preferred_element_type,
Expand Down Expand Up @@ -630,6 +636,8 @@ def __call__(self, *args, **kwargs):
rhs_amax_history=self.kernel_amax_history.value,
grad_amax_history=self.output_grad_amax_history.value,
quantize_compute_type=comp_dtype,
e4m3_dtype=self.e4m3_dtype,
e5m2_dtype=self.e5m2_dtype,
)

return y # type: ignore
Expand All @@ -656,7 +664,9 @@ def __call__(self, eqn, lhs: jnp.ndarray, rhs: jnp.ndarray,
lhs_amax_history=self.input_amax_history.value,
rhs_amax_history=self.kernel_amax_history.value,
grad_amax_history=self.output_grad_amax_history.value,
quantize_compute_type=actual_compute_dtype
quantize_compute_type=actual_compute_dtype,
e4m3_dtype=self.e4m3_dtype,
e5m2_dtype=self.e5m2_dtype,
)
out = jnp.einsum(eqn, lhs, rhs, precision=precision,
preferred_element_type=preferred_element_type,
Expand Down
35 changes: 35 additions & 0 deletions tests/linen/linen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,41 @@ def loss_fn_ref(x, y):
np.testing.assert_allclose(grads[0], grads_ref[0], atol=1e-02, rtol=1e-02)
np.testing.assert_allclose(grads[1], grads_ref[1], atol=1e-02, rtol=1e-02)

@parameterized.parameters({'use_einsum': False}, {'use_einsum': True})
def test_fp8_direct_ops_honor_fp8_dtypes(self, use_einsum):
e4m3_dtype, e5m2_dtype = get_fp8_dtypes('NANOO')
x = jnp.ones((2, 4))
k = jnp.ones((4, 3))
if use_einsum:
p = nn.Fp8Einsum(e4m3_dtype=e4m3_dtype, e5m2_dtype=e5m2_dtype)
args = lambda x: ('mk,kn->mn', x, k)
else:
p = nn.Fp8DotGeneral(e4m3_dtype=e4m3_dtype, e5m2_dtype=e5m2_dtype)
args = lambda x: (x, k, (((1,), (0,)), ((), ())))

# Seed the amax histories so the new input and kernel scales depend on the
# e4m3 max, and the output grad scale is 1.
owg = fp8_ops.OVERWRITE_WITH_GRADIENT
meta = dict(p.init(random.key(0), *args(x))[owg])
for name, amax in [
('input_amax_history', 4.0),
('kernel_amax_history', 4.0),
('output_grad_amax_history', float(jnp.finfo(e5m2_dtype).max)),
]:
meta[name] = meta[name].at[0].set(amax)
vars = {owg: meta}

# 2**-17 is the smallest e5m2fnuz subnormal but underflows to 0 in e5m2.
def loss_fn(vars, x):
return jnp.sum(p.apply(vars, *args(x)) * 2.0**-17)

grads, dx = jax.jit(jax.grad(loss_fn, argnums=(0, 1)))(vars, x)
# Falling back to e4m3fn/e5m2 gives scales of 4 / 448 and an all zero dx.
expected_scale = 4.0 / jnp.finfo(e4m3_dtype).max.astype(jnp.float32)
for name in ('input_scale', 'kernel_scale'):
np.testing.assert_allclose(grads[owg][name][0], expected_scale, rtol=1e-6)
np.testing.assert_allclose(dx, jnp.full(x.shape, 3 * 2.0**-17), rtol=1e-6)


@parameterized.parameters(
{'fp8_genre': 'OCP'}, {'fp8_genre': 'NANOO'}
Expand Down
Loading