Ejemplo n.º 1
0
    def test_multiple_ooc_lauums(self):
        num_processes = 2
        num_rep = 5
        num_pts = 1536
        num_gpus = torch.cuda.device_count()
        torch.manual_seed(19)
        A = torch.randn(num_pts, num_pts, dtype=torch.float32, device='cpu')
        with tempfile.TemporaryDirectory() as folder:
            A_file = os.path.join(folder, "data_A.pkl")
            out_files = [
                os.path.join(folder, f"output_{i}.pkl")
                for i in range(num_processes)
            ]
            # Write data to file
            with open(A_file, 'wb') as fh:
                pickle.dump(A, fh)

            threads = []
            for i in range(num_processes):
                t = threading.Thread(
                    target=_ooc_lauum_runner_str,
                    kwargs={
                        'fname_A': A_file,
                        'fname_out': out_files[i],
                        'num_rep': num_rep,
                        'gpu_num': i % num_gpus,  #'0,1',
                    },
                    daemon=False)
                threads.append(t)
            for t in threads:
                t.start()
            for t in threads:
                t.join()

            # Load outputs
            actual = []
            for of in out_files:
                with open(of, 'rb') as fh:
                    actual.append(pickle.load(fh))

        # Expected result
        torch.cuda.empty_cache()
        opt = FalkonOptions(compute_arch_speed=False,
                            use_cpu=False,
                            max_gpu_mem=2 * 2**20)
        expected = gpu_lauum(A, upper=True, overwrite=False, opt=opt).cpu()
        # Compare actual vs expected
        wrong = 0
        for i in range(len(actual)):
            for j in range(len(actual[i])):
                try:
                    np.testing.assert_allclose(expected.numpy(),
                                               actual[i][j].numpy(),
                                               rtol=1e-7)
                except AssertionError:
                    raise
                    print(f"Result {j} from process {i} is incorrect")
                    wrong += 1
        assert wrong == 0, "%d results were not equal" % (wrong)
Ejemplo n.º 2
0
    def __init__(
        self,
        kernel: falkon.kernels.Kernel,
        M: int,
        center_selection: Union[
            str, falkon.center_selection.CenterSelector] = 'uniform',
        seed: Optional[int] = None,
        error_fn: Optional[callable] = None,
        error_every: Optional[int] = 1,
        options: Optional[FalkonOptions] = None,
    ):
        self.kernel = kernel
        self.M = M
        self.seed = seed
        if self.seed is not None:
            torch.manual_seed(self.seed)  # Works for both CPU and GPU
        self.random_state_ = check_random_generator(self.seed)

        self.error_fn = error_fn
        self.error_every = error_every
        # Options
        self.options = options or FalkonOptions()
        self._cg_options = self.options.get_conjgrad_options()
        self._keops_options = self.options.get_keops_options()
        self._pc_options = self.options.get_pc_options()
        self._cholesky_opt = self.options.get_chol_options()
        self._lauum_opt = self.options.get_lauum_options()
        self._base_opt = self.options.get_base_options()

        self.use_cuda_ = decide_cuda(self.options)
        self.num_gpus = 0
        self.alpha_ = None
        self.ny_points_ = None
        self.fit_times_ = None

        if isinstance(center_selection, str):
            if center_selection.lower() == 'uniform':
                self.center_selection: falkon.center_selection.CenterSelector = \
                    falkon.center_selection.UniformSelector(self.random_state_)
            else:
                raise ValueError(
                    f'Center selection "{center_selection}" is not valid.')
        else:
            self.center_selection: falkon.center_selection.CenterSelector = center_selection
Ejemplo n.º 3
0
 def __init__(self,
              kernel: falkon.kernels.Kernel,
              penalty: float,
              M: int,
              center_selection: Union[str, falkon.center_selection.CenterSelector] = 'uniform',
              maxiter: int = 20,
              seed: Optional[int] = None,
              error_fn: Optional[callable] = None,
              error_every: Optional[int] = 1,
              options=FalkonOptions(),
              ):
     super().__init__(kernel, M, center_selection, seed, error_fn, error_every, options)
     self.penalty = penalty
     self.maxiter = maxiter
     if not self.use_cuda_:
         raise RuntimeError("Cannot instantiate InCoreFalkon when CUDA is not available. "
                            "If CUDA is present on your system, make sure to set "
                            "'use_cpu=False' in the `FalkonOptions` object.")
     self._init_cuda()
Ejemplo n.º 4
0
    def test_multiple_falkons(self):
        num_processes = 6
        num_rep = 5
        max_iter = 20
        num_centers = 1000
        num_gpus = torch.cuda.device_count()
        X, Y = datasets.make_regression(50000, 2048, random_state=11)
        X = torch.from_numpy(X.astype(np.float32))
        Y = torch.from_numpy(Y.astype(np.float32).reshape(-1, 1))

        with tempfile.TemporaryDirectory() as folder:
            X_file = os.path.join(folder, "data_x.pkl")
            Y_file = os.path.join(folder, "data_y.pkl")
            out_files = [
                os.path.join(folder, f"output_{i}.pkl")
                for i in range(num_processes)
            ]
            # Write data to file
            with open(X_file, 'wb') as fh:
                pickle.dump(X, fh)
            with open(Y_file, 'wb') as fh:
                pickle.dump(Y, fh)

            threads = []
            for i in range(num_processes):
                t = threading.Thread(target=_runner_str,
                                     kwargs={
                                         'fname_X': X_file,
                                         'fname_Y': Y_file,
                                         'fname_out': out_files[i],
                                         'num_rep': num_rep,
                                         'max_iter': max_iter,
                                         'num_centers': num_centers,
                                         'gpu_num': i % num_gpus,
                                     },
                                     daemon=False)
                threads.append(t)
            for t in threads:
                t.start()
            for t in threads:
                t.join()

            # Load outputs
            actual = []
            for of in out_files:
                with open(of, 'rb') as fh:
                    actual.append(pickle.load(fh))

        # Expected result
        kernel = kernels.GaussianKernel(20.0)
        X, Y = X.cuda(), Y.cuda()
        opt = FalkonOptions(use_cpu=False,
                            keops_active="no",
                            debug=False,
                            never_store_kernel=True,
                            max_gpu_mem=1 * 2**30,
                            cg_full_gradient_every=2)
        center_sel = FixedSelector(X[:num_centers])
        flk = InCoreFalkon(kernel=kernel,
                           penalty=1e-6,
                           M=num_centers,
                           seed=10,
                           options=opt,
                           maxiter=max_iter,
                           center_selection=center_sel)
        flk.fit(X, Y)
        expected = flk.predict(X).cpu()
        # Compare actual vs expected
        wrong = 0
        for i in range(len(actual)):
            for j in range(len(actual[i])):
                try:
                    np.testing.assert_allclose(expected.numpy(),
                                               actual[i][j].numpy(),
                                               rtol=1e-7)
                except AssertionError:
                    print(f"Result {j} from process {i} is incorrect")
                    wrong += 1
        assert wrong == 0, "%d results were not equal" % (wrong)