def test_benchmark_numpy_128(self): n = 128 print("Running torch cuda benchmark %dx%d" % (n, n)) D = torch.rand(n, n, dtype=torch.float32).numpy() F = torch.rand(n, n, dtype=torch.float32).numpy() # Force lazy init minimize(D=D, F=F) start = timeit.default_timer() minimize(D=D, F=F, descents_count=10) stop = timeit.default_timer() execution_time = stop - start print("Execution time (s) =", execution_time)
def test_equivalence_between_numpy_and_torch(self): n = 64 D = torch.rand(n, n, dtype=torch.float32) F = torch.rand(n, n, dtype=torch.float32) self.init_random_seed() solution_numpy = minimize(D=D.numpy(), F=F.numpy(), descents_count=10) self.init_random_seed() solution_torch = minimize(D=D.to(self.device), F=F.to(self.device), descents_count=10) np.testing.assert_array_equal(solution_numpy.x, solution_torch.x) np.testing.assert_allclose(solution_numpy.fun, solution_torch.fun, rtol=1e-05)
def test_against_exhaustive_list(self): D = torch.tensor( np.array( [ [63, 18, -4, 98], [93, 54, 89, -91], [-69, -82, 85, -85], [-56, -78, 39, 4], ], np.float64, )).to(self.device) F = torch.tensor( np.array( [ [36, -62, -15, 55], [41, -82, 57, 39], [50, 12, -49, -61], [-69, 70, -40, -6], ], np.float64, )).to(self.device) objective_permutation_pairs = [ (objective(D=D, F=F, permutation=perm), perm) for perm in itertools.permutations([0, 1, 2, 3]) ] expected = min(objective_permutation_pairs, key=lambda x: x[0]) solution = minimize(D=D, F=F, descents_count=1) np.testing.assert_almost_equal(expected[0], solution.fun, decimal=5) np.testing.assert_array_equal(expected[1], solution.x)
def test_benchmark_torch_cuda_256(self): n = 256 print("Running torch cuda benchmark %dx%d" % (n, n)) device = torch.device("cuda") D = torch.rand(n, n, dtype=torch.float32).to(device) F = torch.rand(n, n, dtype=torch.float32).to(device) # Force lazy init minimize(D=D, F=F) start = timeit.default_timer() minimize(D=D, F=F, descents_count=10) stop = timeit.default_timer() execution_time = stop - start print("Execution time (s) =", execution_time)
def test_inverse(self): F = np.array([[1, 2, 3, 4], [2, 5, 6, 7], [3, 6, 8, 9], [4, 7, 9, 13]], np.float64) correct_permutation = [1, 3, 2, 0] P = permutation_matrix(inverse_permutation(correct_permutation)) D = -P @ F @ P.transpose() solution_permutation = minimize(D=D, F=F, descents_count=1).x np.testing.assert_array_equal(correct_permutation, solution_permutation)
def test_reverse_small(self): D = np.array([[0, 0, -3], [0, -2, 0], [-1, 0, 0]], dtype=np.float64) F = np.array([[0, 0, +1], [0, +2, 0], [+3, 0, 0]], dtype=np.float64) expected_permutation = [2, 1, 0] expected_objective = objective(D=D, F=F, permutation=expected_permutation) solution = minimize(D=D, F=F, descents_count=3) np.testing.assert_almost_equal(expected_objective, solution.fun, decimal=5) np.testing.assert_array_equal(expected_permutation, solution.x)
#!/usr/bin/env python3 import numpy as np from faqap import minimize # Make runs deterministic, descent origins are chosen randomly by default. np.random.seed(123456789) D = np.array([[0, 0, 0, -4], [0, 0, -3, 0], [0, -2, 0, 0], [-1, 0, 0, 0]], dtype=np.float64) F = np.array([[0, 0, 0, +1], [0, 0, +2, 0], [0, +3, 0, 0], [+4, 0, 0, 0]], dtype=np.float64) solution_permutation = minimize(D=D, F=F, descents_count=1).x # Expected is the permutation reversing elements. print("solution permutation =", solution_permutation)