def __init__(
self,
plan: FeaturePlan,
out_features: int,
stride: int = 1,
bias: bool = True,
*,
backend: str = "auto",
chunk_size: int | None = None,
device: torch.device | str | None = None,
dtype: torch.dtype | None = None,
) -> None:
super().__init__()
if (
not isinstance(out_features, int)
or isinstance(out_features, bool)
or out_features < 1
):
raise ValueError("out_features must be a positive integer.")
if not isinstance(bias, bool):
raise TypeError("bias must be a boolean.")
if dtype is not None and not dtype.is_floating_point:
raise ValueError("dtype must be floating-point.")
self.features = LocalFeatures(
plan,
stride=stride,
backend=backend,
chunk_size=chunk_size,
)
if device is not None or dtype is not None:
self.features.to(device=device, dtype=dtype)
parameter_device = device
parameter_dtype = dtype
if not isinstance(plan, PolynomialPlan):
plan_matrix = plan._matrix_tensor()
if parameter_device is None:
parameter_device = plan_matrix.device
if parameter_dtype is None:
parameter_dtype = plan_matrix.dtype
self.in_features = plan.feature_count
self.out_features = out_features
self.weight = torch.nn.Parameter(
torch.empty(
out_features,
self.in_features,
device=parameter_device,
dtype=parameter_dtype,
)
)
if bias:
self.bias = torch.nn.Parameter(
torch.empty(
out_features,
device=parameter_device,
dtype=parameter_dtype,
)
)
else:
self.register_parameter("bias", None)
self.reset_parameters()