Skip to content

Commit e1d32f4

Browse files
CopilotIvanYashchuk
andcommitted
Revert div_exact primitive and fix div grad to use prims.div
Co-authored-by: IvanYashchuk <19621411+IvanYashchuk@users.noreply.github.com>
1 parent 66c2fbd commit e1d32f4

7 files changed

Lines changed: 11 additions & 28 deletions

File tree

thunder/clang/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,12 +1601,6 @@ def eq(a, b):
16011601
# NOTE This is distinct from true_divide, which also wraps prims.div, because it doesn't promote
16021602
# integers to floating point values
16031603
def _c_div(a: TensorProxy | Number, b: TensorProxy | Number) -> TensorProxy | Number:
1604-
# Uses non-differentiable version of div for exact types
1605-
if utils.is_exact_dtype(utils.to_dtype(a)) and utils.is_exact_dtype(utils.to_dtype(b)):
1606-
return _elementwise_binary_wrapper(
1607-
a, b, prim=prims.div_exact, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
1608-
)
1609-
# OTOH trunc_div, which uses _c_div, is differentiable for inexact types
16101604
return _elementwise_binary_wrapper(
16111605
a, b, prim=prims.div, type_promotion_kind=utils.ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT
16121606
)

thunder/core/prims.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ class PrimIDs(Enum):
223223
BITWISE_OR = auto()
224224
BITWISE_XOR = auto()
225225
DIV = auto()
226-
DIV_EXACT = auto()
227226
EQ = auto()
228227
PY_FLOORDIV = auto()
229228
FMOD = auto()
@@ -2619,14 +2618,6 @@ def _div_numbers(a: Number, b: Number) -> Number:
26192618
supported_input_dtypes=math_dtypes,
26202619
)
26212620

2622-
# The non-differentiable version of div
2623-
div_exact = _make_elementwise_binary_prim(
2624-
PrimIDs.DIV_EXACT,
2625-
"div_exact",
2626-
number_fn=_div_numbers,
2627-
supported_input_dtypes=dtypes.exact_dtypes,
2628-
)
2629-
26302621
# We currently do not support floordiv on tensors.
26312622
py_floordiv = _make_elementwise_binary_prim(
26322623
PrimIDs.PY_FLOORDIV,

thunder/core/transforms.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,14 +1135,12 @@ def _add_prim_grad(a: Number | TensorProxy, b: Number | TensorProxy, /) -> Numbe
11351135
register_grad(pids.ADD, _add_prim_grad)
11361136

11371137

1138-
# NOTE The following grad definition relies on the fact that only inexact dtypes are differentiable,
1139-
# and torch's true division operator and the division primitive agree on those types
11401138
def _div_prim_grad(a: Number | TensorProxy, b: Number | TensorProxy, /) -> Number | TensorProxy:
1141-
fwd = a / b
1139+
fwd = prims.div(a, b)
11421140

11431141
g = get_grad(fwd)
1144-
a_grad = g / b
1145-
b_grad = -g * ((a / b) / b)
1142+
a_grad = prims.div(g, b)
1143+
b_grad = -g * prims.div(prims.div(a, b), b)
11461144
put_grads((a, b), (a_grad, b_grad))
11471145

11481146
return fwd
@@ -2590,7 +2588,6 @@ def uniform_backward(primal, minval, maxval, g):
25902588
prims.PrimIDs.FULL,
25912589
prims.PrimIDs.FLOOR,
25922590
prims.PrimIDs.CEIL,
2593-
prims.PrimIDs.DIV_EXACT,
25942591
}
25952592

25962593

thunder/executors/nvfuserex_impl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,6 @@ def div(a: TensorProxy | Number, b: TensorProxy | Number, *, fd: FusionDefinitio
18611861

18621862

18631863
register_supported(PrimIDs.DIV, div, _elementwise_binary_check)
1864-
register_supported(PrimIDs.DIV_EXACT, div, _elementwise_binary_check)
18651864

18661865

18671866
def eq(a: TensorProxy | Number, b: TensorProxy | Number, *, fd: FusionDefinition, lc_to_nv_map: dict) -> Any:

thunder/executors/pythonex.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,6 @@ def _elementwise_binary_checker(a: NumberLike | TensorProxy, b: NumberLike | Ten
346346
pythonex_pow = ex.register_operator("pow", like=prims.pow, module=operator)
347347
sub = ex.register_operator("sub", like=prims.sub, module=operator)
348348
div = ex.register_operator("div", like=prims.div, fn=_div_prim_impl)
349-
div_exact = ex.register_operator("div_exact", like=prims.div_exact, fn=_div_prim_impl)
350349
shape = ex.register_operator("shape", like=prims.shape, fn=lambda x: x.shape)
351350

352351
# TODO: Restore truediv once we find it...
@@ -371,7 +370,6 @@ def _elementwise_binary_checker(a: NumberLike | TensorProxy, b: NumberLike | Ten
371370
ex.register_implementation(prims.pow, pythonex_pow, checker=_elementwise_binary_checker)
372371
ex.register_implementation(prims.sub, sub, checker=_elementwise_binary_checker)
373372
ex.register_implementation(prims.div, div, checker=_elementwise_binary_checker)
374-
ex.register_implementation(prims.div_exact, div_exact, checker=_elementwise_binary_checker)
375373
ex.register_implementation(prims.shape, shape, checker=_always_executable)
376374

377375

thunder/executors/torchex.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,6 @@ def _div_transform(
10241024
_register_elementwise_binary_implementation(prims.bitwise_xor, bitwise_xor)
10251025
div_prim_impl = ex.register_operator("torch_div_prim_impl", meta=prims.div.meta, fn=_div_prim_impl)
10261026
_register_elementwise_binary_implementation(prims.div, div_prim_impl)
1027-
_register_elementwise_binary_implementation(prims.div_exact, div_prim_impl)
10281027
_register_elementwise_binary_implementation(prims.eq, eq)
10291028
_register_elementwise_binary_implementation(prims.fmod, fmod)
10301029
_register_elementwise_binary_implementation(prims.ge, ge)

thunder/tests/test_ops.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ def fn(a):
528528

529529

530530
def test_div_exact():
531+
# Test that division with integer inputs and requires_grad tensor output works correctly
531532
def fn(a, b, c):
532533
indices = torch.div(a, b, rounding_mode="trunc")
533534
# this would throw an error if indices are not ints
@@ -536,9 +537,13 @@ def fn(a, b, c):
536537
jfn = thunder.jit(fn)
537538
a = torch.randint(1, 5, (5,))
538539
b = torch.ones(5, dtype=torch.int32)
539-
c = torch.randn(5, 5)
540-
assert_close(fn(a, b, c), jfn(a, b, c))
540+
c = torch.randn(5, 5, requires_grad=True)
541+
result_eager = fn(a, b, c)
542+
result_jit = jfn(a, b, c)
543+
assert_close(result_eager, result_jit)
544+
# Ensure the division primitive is used (no div_exact)
541545
trc = thunder.last_traces(jfn)[-1]
542546
for bsym in trc.bound_symbols:
543547
if bsym.sym.id == "div":
544-
assert "div_exact" in [ssym.sym.name for ssym in bsym.subsymbols[0].subsymbols]
548+
# Verify that prims.div is used, not a separate div_exact primitive
549+
assert "div" in [ssym.sym.name for ssym in bsym.subsymbols[0].subsymbols]

0 commit comments

Comments
 (0)