Skip to main content

core.ModelFn

core.ModelFn(forward, module=None)

forward(params, inputs) -> outputs, plus the module when there is one.

params holds the parameters that get a posterior; every other parameter is read from module at call time. The module is needed only by hook-based backends and for the parameter-version guard.

A tied tensor is one parameter. Passing it under any of its names (head.weight from last_layer, or emb.weight from subsets.all) swaps every name, so all its uses are differentiated. Pass one name per tensor.

Parameters

Name Type Description Default
forward Callable[[Params, Inputs], Outputs] forward(params, inputs) -> outputs. required
module nn.Module | None The module, needed only by hook-based backends and for the parameter-version guard. None when there is none. None

Methods

Name Description
from_module Wrap a module with torch.func.functional_call.

from_module

core.ModelFn.from_module(module)

Wrap a module with torch.func.functional_call.

Only the passed params are variables. The module’s other parameters are passed detached, so the outputs never depend on them through autograd.

Parameters

Name Type Description Default
module nn.Module The module. It is called as module(x) for tensor inputs and module(**x) for mapping inputs. required

Returns

Name Type Description
ModelFn A ModelFn holding module. When called, it raises TypeError for inputs that are neither a tensor nor a mapping.