<commit_before>#!/usr/bin/env python3

import torch
from .kernel import Kernel
from ..lazy import MatmulLazyTensor, RootLazyTensor


class LinearKernel(Kernel):
    r"""
    Computes a covariance matrix based on the Linear kernel
    between inputs :math:`\mathbf{x_1}` and :math:`\mathbf{x_2}`:

    .. math::
        \begin{equation*}
            k_\text{Linear}(\mathbf{x_1}, \mathbf{x_2}) = v\mathbf{x_1}^\top
            \mathbf{x_2}.
        \end{equation*}

    where

    * :math:`v` is a :attr:`variance` parameter.


    .. note::

        To implement this efficiently, we use a :obj:`gpytorch.lazy.RootLazyTensor` during training and a
        :class:`gpytorch.lazy.MatmulLazyTensor` during test. These lazy tensors represent matrices of the form
        :math:`K = XX^{\top}` and :math:`K = XZ^{\top}`. This makes inference
        efficient because a matrix-vector product :math:`Kv` can be computed as
        :math:`Kv=X(X^{\top}v)`, where the base multiply :math:`Xv` takes only
        :math:`O(nd)` time and space.

    Args:
        :attr:`variance_prior` (:class:`gpytorch.priors.Prior`):
            Prior over the variance parameter (default `None`).
        :attr:`active_dims` (list):
            List of data dimensions to operate on (default `None`).
    """

    def __init__(self, variance_prior=None, active_dims=None):
        super(LinearKernel, self).__init__(active_dims=active_dims)
        self.register_parameter(name="raw_variance", parameter=torch.nn.Parameter(torch.zeros(1)))
        if variance_prior is not None:
            self.register_prior(
                "variance_prior",
                variance_prior,
                lambda: self.variance,
                lambda v: self._set_variance(v)
            )

    @property
    def variance(self):
        return self._param_transform(self.raw_variance)

    @variance.setter
    def variance(self, value):
        self._set_variance(value)

    def _set_variance(self, value):
        if not torch.is_tensor(value):
            value = torch.tensor(value)
        self.initialize(raw_variance=self._inv_param_transform(value))

    def forward(self, x1, x2, diag=False, batch_dims=None, **params):
        x1_ = x1 * self.variance.sqrt()
        if batch_dims == (0, 2):
            x1_ = x1_.view(x1_.size(0), x1_.size(1), -1, 1)
            x1_ = x1_.permute(0, 2, 1, 3).contiguous()
            x1_ = x1_.view(-1, x1_.size(-2), x1_.size(-1))
<commit_msg>soft deprecate offset and num_dimensions<commit_after>#!/usr/bin/env python3

import torch
import warnings
from .kernel import Kernel
from ..lazy import MatmulLazyTensor, RootLazyTensor


class LinearKernel(Kernel):
    r"""
    Computes a covariance matrix based on the Linear kernel
    between inputs :math:`\mathbf{x_1}` and :math:`\mathbf{x_2}`:

    .. math::
        \begin{equation*}
            k_\text{Linear}(\mathbf{x_1}, \mathbf{x_2}) = v\mathbf{x_1}^\top
            \mathbf{x_2}.
        \end{equation*}

    where

    * :math:`v` is a :attr:`variance` parameter.


    .. note::

        To implement this efficiently, we use a :obj:`gpytorch.lazy.RootLazyTensor` during training and a
        :class:`gpytorch.lazy.MatmulLazyTensor` during test. These lazy tensors represent matrices of the form
        :math:`K = XX^{\top}` and :math:`K = XZ^{\top}`. This makes inference
        efficient because a matrix-vector product :math:`Kv` can be computed as
        :math:`Kv=X(X^{\top}v)`, where the base multiply :math:`Xv` takes only
        :math:`O(nd)` time and space.

    Args:
        :attr:`variance_prior` (:class:`gpytorch.priors.Prior`):
            Prior over the variance parameter (default `None`).
        :attr:`active_dims` (list):
            List of data dimensions to operate on.
            `len(active_dims)` should equal `num_dimensions`.
    """

    def __init__(self, num_dimensions=None, offset_prior=None, variance_prior=None, active_dims=None):
        super(LinearKernel, self).__init__(active_dims=active_dims)
        if num_dimensions is not None:
            warnings.warn(
                "The `num_dimensions` argument is deprecated and no longer used.",
                DeprecationWarning
            )
            self.register_parameter(
                name="offset",
                parameter=torch.nn.Parameter(torch.zeros(1, 1, num_dimensions))
            )
        if offset_prior is not None:
            warnings.warn(
                "The `offset_prior` argument is deprecated and no longer used.",
                DeprecationWarning
            )
        self.register_parameter(name="raw_variance", parameter=torch.nn.Parameter(torch.zeros(1)))
        if variance_prior is not None:
            self.register_prior(
                "variance_prior",
                variance_prior,
                lambda: self.variance,
                lambda v: self._set_variance(v)
            )

    @property
    def variance(self):
        return self._param_transform(self.raw_variance)

    @variance.setter
    def variance(self, value):
        self._set_variance(value)

    def _set_variance(self, value):
        if not torch.is_tensor(value):
            value = torch.tensor(value)
        self.initialize(raw_variance=self._inv_param_transform(value))

    def forward(self, x1, x2, diag=False, batch_dims=None, **params):
        x1_ = x1 * self.variance.sqrt()
        if batch_dims == (0, 2):
            x1_ = x1_.view(x1_.size(0), x1_.size(1), -1, 1)
            x1_ = x1_.permute(0, 2, 1, 3).contiguous()
            x1_ = x1_.view(-1, x1_.size(-2), x1_.size(-1))

{'fraction_non_alphanumeric': 0.12195994277539342, 'fraction_numerical': 0.012875536480686695, 'mean_word_length': 2.4482120838471024, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7200161', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1737, 'n_words': 419}