PythonFunction¶
- class madspace.PythonFunction(function, inputs, outputs, framework='numpy', particle_count=None, name=None, parameters=None)¶
Bases:
objectA UMAMI implementation that calls a Python function on numpy arrays or torch tensors, for example a custom cut. Add it to a context with
Context.add_matrix_element, then use the returnedMatrixElementApilike a loaded library.functionis called once per batch, with one array per entry ofinputs, in that order. The arrays have the batch size as first dimension:momentahas shape(batch, n, 4),pdg_idshas shape(batch, n), and all other inputs have shape(batch,). Floats are 64-bit, integers 32-bit. The function returns one array of shape(batch,)per entry ofoutputs, a single array for one output and a tuple for several.The arrays are views of madspace’s buffers on the device of the context, so they must not be kept after the call. numpy functions only run on the CPU, torch functions also on CUDA and HIP GPUs, on the stream of the madspace runtime.
- Parameters:
function – The Python function.
inputs – Names of the UMAMI inputs passed to
function.outputs – Names of the UMAMI outputs
functionreturns.framework –
"numpy"or"torch".particle_count – The particle count
nformomentaandpdg_ids, if the caller does not pass theparticle_countinput.name – Name used in error messages, by default the function’s name.
parameters – Names and default values of parameters set through
umami_set_parameter, e.g. theparametersargument ofContext.add_matrix_element. With parameters,functiongets a namespace of their values as last argument, e.g.params.pt_min.