MCPcopy
hub / github.com/pytorch/pytorch / construct_method

Function construct_method

torch/testing/_internal/optests/generate_tests.py:212–269  ·  view source on GitHub ↗
(attr, prefix, tester)

Source from the content-addressed store, hash-verified

210 validate_failures_dict_formatting(failures_dict_path)
211
212 def construct_method(attr, prefix, tester):
213 method = getattr(testcase, attr)
214 if getattr(method, "_torch_dont_generate_opcheck_tests", False):
215 return
216 new_method_name = prefix + "__" + attr
217
218 @functools.wraps(method)
219 def new_method(*args, **kwargs):
220 with OpCheckMode(
221 namespaces,
222 prefix,
223 tester,
224 failures_dict,
225 f"{testcase.__name__}.{new_method_name}",
226 failures_dict_path,
227 ):
228 result = method(*args, **kwargs)
229 return result
230
231 if pytestmark := new_method.__dict__.get("pytestmark"):
232 import pytest
233
234 # check if we need to simplify the parametrize marks
235 # NB: you need to add this mark to your pytest.ini
236 opcheck_only_one = False
237 for mark in pytestmark:
238 if isinstance(mark, pytest.Mark) and mark.name == "opcheck_only_one":
239 opcheck_only_one = True
240
241 if opcheck_only_one:
242 new_pytestmark = []
243 for mark in pytestmark:
244 if isinstance(mark, pytest.Mark) and mark.name == "parametrize":
245 argnames, argvalues = mark.args
246 assert not mark.kwargs, "NYI"
247 # Special case for device, we want to run on all
248 # devices
249 if argnames != "device":
250 new_pytestmark.append(
251 pytest.mark.parametrize(
252 argnames, (next(iter(argvalues)),)
253 )
254 )
255 continue
256 new_pytestmark.append(mark)
257 new_method.__dict__["pytestmark"] = new_pytestmark
258
259 if new_method_name in additional_decorators:
260 for dec in additional_decorators[new_method_name]:
261 new_method = dec(new_method)
262
263 if hasattr(testcase, new_method_name):
264 raise RuntimeError(
265 f"Tried to autogenerate {new_method_name} but {testcase} already "
266 f"has method named {new_method_name}. Please rename the original "
267 f"method on the TestCase."
268 )
269 setattr(testcase, new_method_name, new_method)

Callers 1

generate_opcheck_testsFunction · 0.85

Calls 5

isinstanceFunction · 0.85
iterFunction · 0.85
decFunction · 0.50
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…