def test_inception(inception_model_pytorch): """Test the InceptionBlocks model that WebGME folks provided us.""" galaxy_images_output = torch.zeros((1, 5, 64, 64)) ebv_output = torch.zeros((1,)) # Run the model once to get some ground truth outpot (from PyTorch) output = inception_model_pytorch(galaxy_images_output, ebv_output).detach().numpy() # Convert to MDF mdf_model, params_dict = pytorch_to_mdf( model=inception_model_pytorch, args=(galaxy_images_output, ebv_output), example_outputs=output, trace=True, ) # Get the graph mdf_graph = mdf_model.graphs[0] # Add inputs to the parameters dict so we can feed this to the EvaluableGraph for initialization of all # graph inputs. params_dict["input1"] = galaxy_images_output.numpy() params_dict["input2"] = ebv_output.numpy() eg = EvaluableGraph(graph=mdf_graph, verbose=False) eg.evaluate(initializer=params_dict) assert np.allclose( output, eg.enodes["Add_381"].evaluable_outputs["_381"].curr_value, )
def test_simple_function(): """Test a simple function""" def simple(x, y): return x + y mdf_model, param_dict = pytorch_to_mdf( model=simple, args=(torch.tensor(0.0), torch.tensor(0.0)), example_outputs=(torch.tensor(0.0)), use_onnx_ops=True, ) _check_model(mdf_model)
def test_simple_module(): """Test a simple torch.nn.Module""" class Simple(torch.nn.Module): def forward(self, x, y): return x + y mdf_model, param_dict = pytorch_to_mdf( model=Simple(), args=(torch.tensor(0.0), torch.tensor(0.0)), example_outputs=(torch.tensor(0.0)), use_onnx_ops=True, ) _check_model(mdf_model)
def main(): ddm_params = dict( starting_value=0.0, drift_rate=0.3, non_decision_time=0.15, threshold=0.6, noise=1.0, time_step_size=0.001, ) # Move params to device for key, val in ddm_params.items(): ddm_params[key] = torch.tensor(val).to(dev) # Run a single ddm rt, decision = ddm(**ddm_params) mdf_model, param_dict = pytorch_to_mdf( model=ddm, args=tuple(ddm_params.values()), example_outputs=(rt, decision), use_onnx_ops=True, ) # Output the model to JSON mdf_model.to_json_file("ddm.json") import sys if "-graph" in sys.argv: mdf_model.to_graph_image( engine="dot", output_format="png", view_on_render=False, level=2, filename_root="ddm", only_warn_on_fail= True, # Makes sure test of this doesn't fail on Windows on GitHub Actions )
def main(): # changed import call from modeci_mdf.execution_engine import EvaluableGraph # Create some test inputs for the model galaxy_images_output = torch.zeros((1, 5, 64, 64)) ebv_output = torch.zeros((1, )) # Seed the random number generator to get deterministic behavior for weight initialization torch.manual_seed(0) model = InceptionBlocks() # Turn on eval mode for model to get rid of any randomization due to things like BatchNorm or Dropout model.eval() # Run the model once to get some ground truth outpot (from PyTorch) output = model(galaxy_images_output, ebv_output).detach().numpy() # Convert to MDF mdf_model, params_dict = pytorch_to_mdf( model=model, args=(galaxy_images_output, ebv_output), example_outputs=output, trace=True, ) # Get the graph mdf_graph = mdf_model.graphs[0] # Add inputs to the parameters dict so we can feed this to the EvaluableGraph for initialization of all # graph inputs. params_dict["input1"] = galaxy_images_output.numpy() params_dict["input2"] = ebv_output.numpy() # Evaluate the model via the MDF scheduler eg = EvaluableGraph(graph=mdf_graph, verbose=False) eg.evaluate(initializer=params_dict) # Make sure the results are the same betweeen PyTorch and MDF assert np.allclose( output, eg.enodes["Add_381"].evaluable_outputs["_381"].curr_value, ) print("Passed all comparison tests!") # Output the model to JSON mdf_model.to_json_file("inception.json") import sys if "-graph" in sys.argv: mdf_model.to_graph_image( engine="dot", output_format="png", view_on_render=False, level=1, filename_root="inception", only_warn_on_fail= True, # Makes sure test of this doesn't fail on Windows on GitHub Actions )