Skip to content
Merged
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
19 changes: 19 additions & 0 deletions src/mindtorch_v2/_dispatch/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,22 @@ def _validate_std_var_dim(value):
f"and/or underscore and starts with a non-digit. got: '{value}'."
)

def _validate_nan_reduction_dim(value):
if value is None:
return
if isinstance(value, int) and not isinstance(value, bool):
return
if isinstance(value, (list, tuple)):
for item in value:
if not isinstance(item, int) or isinstance(item, bool):
raise TypeError(
f"{op_short_name}(): argument 'dim' must be tuple of ints, not {type(item).__name__}"
)
return
raise TypeError(
f"{op_short_name}(): argument 'dim' must be tuple of ints, not {type(value).__name__}"
)

def _type_label(value):
if isinstance(value, bool):
return "bool"
Expand Down Expand Up @@ -640,6 +656,9 @@ def _validate_transpose_dims(dim0, dim1):
if op_short_name in {"std", "var"} and param.name == "dim":
_validate_std_var_dim(value)
continue
if op_short_name in {"nansum", "nanmean"} and param.name == "dim":
_validate_nan_reduction_dim(value)
continue
if op_short_name == "view" and param.name == "shape":
_validate_view_shape(value)
continue
Expand Down
48 changes: 48 additions & 0 deletions tests/mindtorch_v2/contract/test_schema_dim_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,51 @@ def th_call():
pt.var(pt.tensor([1.0, 0.0]), dim="0")

assert_torch_error(mt_call, th_call)


def test_dispatch_nansum_rejects_bool_dim_matches_torch():
mt_x = torch.tensor([1.0, 0.0])

def mt_call():
dispatch("nansum", mt_x.device.type, mt_x, dim=True)

def th_call():
pt.nansum(pt.tensor([1.0, 0.0]), dim=True)

assert_torch_error(mt_call, th_call)


def test_dispatch_nansum_rejects_str_dim_matches_torch():
mt_x = torch.tensor([1.0, 0.0])

def mt_call():
dispatch("nansum", mt_x.device.type, mt_x, dim="0")

def th_call():
pt.nansum(pt.tensor([1.0, 0.0]), dim="0")

assert_torch_error(mt_call, th_call)


def test_dispatch_nanmean_rejects_bool_dim_matches_torch():
mt_x = torch.tensor([1.0, 0.0])

def mt_call():
dispatch("nanmean", mt_x.device.type, mt_x, dim=True)

def th_call():
pt.nanmean(pt.tensor([1.0, 0.0]), dim=True)

assert_torch_error(mt_call, th_call)


def test_dispatch_nanmean_rejects_str_dim_matches_torch():
mt_x = torch.tensor([1.0, 0.0])

def mt_call():
dispatch("nanmean", mt_x.device.type, mt_x, dim="0")

def th_call():
pt.nanmean(pt.tensor([1.0, 0.0]), dim="0")

assert_torch_error(mt_call, th_call)