Context¶
-
class Context¶
Owns the trainable globals and loaded matrix elements for one device.
The state a compiled Function runs against: the named, persistent Tensor globals it reads and writes (network weights, VEGAS grids, …) and the MatrixElementApi libraries loaded via load_matrix_element. All globals and matrix elements live on the same device. Training and generation typically use two contexts, one per role, and periodically sync weights with copy_globals_from.
Public Functions
-
inline Context(int thread_count = -1)¶
A context on the CPU, with
thread_countworker threads (-1for the hardware concurrency).
-
inline Context(DevicePtr device, int thread_count = -1)¶
A context on
device, withthread_countworker threads (-1for the hardware concurrency).
-
const MatrixElementApi &load_matrix_element(const std::string &file, const std::string ¶m_card, const std::unordered_map<std::string, double> ¶meters = {})¶
Load the UMAMI matrix element from
file, initialized withparam_card.Every entry of
parametersis then passed to each process instance throughumami_set_parameter(real value, e.g.{"bwcutoff": 15.}, the window of the$-excluded propagators); a library that rejects one of them is an error. Caches byfile: loading the same file twice returns the same MatrixElementApi.
-
const MatrixElementApi &add_matrix_element(UmamiFunctions functions, const std::string &name, const std::string ¶m_card = "", const std::unordered_map<std::string, double> ¶meters = {})¶
Add a matrix element from the UMAMI entry points in
functions, without a shared library.nameidentifies it in error messages, andparam_cardandparametersare passed on as in load_matrix_element.
-
Tensor define_global(const std::string &name, DataType dtype, const SizeVec &shape, bool requires_grad = false)¶
Create and zero-initialize a new global named
name.
-
bool global_requires_grad(const std::string &name)¶
Whether the global named
nameaccumulates a gradient during training.
-
void set_global_requires_grad(const std::string &name, bool value)¶
Set whether the global named
nameaccumulates a gradient.
-
bool global_exists(const std::string &name)¶
Whether a global named
nameexists.
-
std::vector<std::string> global_names() const¶
Names of every global.
-
void delete_global(const std::string &name)¶
Remove the global named
name.
-
void copy_globals_from(Context &context)¶
Overwrite every global also present in
contextwith its value there.
-
Tensor reallocate_globals_contiguously(const std::vector<std::string> &names)¶
Repack the globals in
namesinto one contiguous allocation, for example before handing them to an external optimizer.
-
const MatrixElementApi &matrix_element(std::size_t index) const¶
The matrix element previously loaded at position
index; see load_matrix_element.
-
void save_globals(const std::string &file) const¶
Write every global to the tar archive
file, as one.npyfile per name.
-
void load_globals(const std::string &file)¶
Define a global from every
.npyfile in the tar archivefile, named after the file.
-
inline DevicePtr device()¶
The device every global and matrix element of this context lives on.
-
inline ThreadPool &thread_pool()¶
The thread pool backing this context’s device-local resources.
-
inline std::size_t unique_seed_index()¶
The next value of a per-context counter, used to derive independent RNG seeds; see DerivedSeed.
-
Tensor cached_tensor(std::size_t size)¶
A scratch tensor of at least
sizebytes, reused from a pool when possible.
-
inline void reset_cache()¶
Drop every scratch tensor held by cached_tensor.
-
template<typename T>
inline ThreadResource<T> &global_resource(const std::string &name, std::function<T()> constructor, std::optional<std::function<void(T&)>> destructor = std::nullopt)¶ A per-thread resource named
name, created withconstructoron first access and torn down withdestructor.Not thread-safe: callers must acquire the reference once during single-threaded initialization (e.g. a
Runtimeconstructor) and reuse it.
-
inline Context(int thread_count = -1)¶