MCPcopy
hub / github.com/WassimTenachi/PhySO / test_execution

Method test_execution

physo/physym/tests/program_UnitTest.py:261–328  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

259
260 # Test program execution on a complicated function (no free consts)
261 def test_execution (self):
262
263 DEVICE = 'cpu'
264 if torch.cuda.is_available():
265 DEVICE = 'cuda'
266
267 # DATA
268 N = int(1e6)
269
270 # input var
271 x = data_conversion (np.linspace(0.04, 4, N) ).to(DEVICE)
272 v = data_conversion (np.linspace(0.10, 10, N) ).to(DEVICE)
273 t = data_conversion (np.linspace(0.06, 6, N) ).to(DEVICE)
274 data = torch.stack((x, v, t), axis=0)
275
276 # consts
277 pi = data_conversion (np.pi).to(DEVICE)
278 const1 = data_conversion (1.).to(DEVICE)
279
280 # free consts
281 c = data_conversion (3e8).to(DEVICE)
282 M = data_conversion (1e6).to(DEVICE)
283
284 # LIBRARY CONFIG
285 args_make_tokens = {
286 # operations
287 "op_names" : "all", # or ["mul", "neg", "inv", "sin"]
288 "use_protected_ops" : False,
289 # input variables
290 "input_var_ids" : {"x" : 0 , "v" : 1 , "t" : 2, },
291 "input_var_units" : {"x" : [1, 0, 0] , "v" : [1, -1, 0] , "t" : [0, 1, 0] },
292 "input_var_complexity" : {"x" : 0. , "v" : 1. , "t" : 0., },
293 # constants
294 "constants" : {"pi" : pi , "1" : const1 , "c" : c , "M" : M },
295 "constants_units" : {"pi" : [0, 0, 0] , "1" : [0, 0, 0] , "c" : [1, -1, 0], "M" : [0, 0, 1] },
296 "constants_complexity" : {"pi" : 0. , "1" : 1. , "c" : 0. , "M" : 1. },
297 }
298 my_lib = Lib.Library(args_make_tokens = args_make_tokens,
299 superparent_units = [1, -2, 1], superparent_name = "y")
300
301 # PROGRAM
302 test_program_str = ["mul", "mul", "M", "n2", "c", "sub", "inv", "sqrt", "sub", "1", "div", "n2", "v", "n2",
303 "c", "cos", "div", "sub", "1", "div", "v", "c", "div", "div", "x", "t", "c"]
304 test_program = [my_lib.lib_name_to_token[name] for name in test_program_str]
305 free_const_table = free_const.FreeConstantsTable(batch_size=1, library=my_lib, n_realizations=1)
306 prog = Prog.Program(tokens=test_program, library=my_lib, free_consts=free_const_table, n_realizations=1)
307 # EXPECTED RES
308 expected_res = M*(c**2)*(1./torch.sqrt(1.-(v**2)/(c**2))-torch.cos((1.-(v/c))/((x/t)/c)))
309
310 N = 100
311 # EXECUTION
312 t0 = time.perf_counter()
313 for _ in range (N):
314 res = prog.execute(data)
315 t1 = time.perf_counter()
316 print("\nprog.execute time = %.3f ms"%((t1-t0)*1e3/N))
317
318 # EXECUTION (wo tokens)

Callers

nothing calls this directly

Calls 5

executeMethod · 0.95
data_conversionFunction · 0.90
data_conversion_invFunction · 0.90
cpuMethod · 0.80
toMethod · 0.45

Tested by

no test coverage detected