Template Class PythonEinsumGeneratorBase¶
Defined in File python_einsum.hpp
Inheritance Relationships¶
Base Type¶
public sequant::Generator< Context >(Template Class Generator)
Class Documentation¶
-
template<typename Context>
class PythonEinsumGeneratorBase : public sequant::Generator<Context>¶ Base generator for producing Python code using einsum Provides common functionality shared by NumPy and PyTorch backends
Public Functions
-
PythonEinsumGeneratorBase() = default¶
-
virtual ~PythonEinsumGeneratorBase() = default¶
-
inline virtual bool supports_named_sections() const override¶
- Returns:
Whether this generator supports named sections
-
inline virtual bool requires_named_sections() const override¶
- Returns:
Whether this generator requires names (that is, it can’t deal with code outside of named sections)
-
inline virtual bool supports_index_batching() const override¶
- Returns:
Whether this generator supports index batching. This means that a given tensor expression is computed for fixed batches of one or more of the involved indices instead of all at once.
-
inline virtual DeclarationScope index_declaration_scope() const override¶
- Returns:
The scope at which this generator would like declare indices
-
inline virtual DeclarationScope variable_declaration_scope() const override¶
- Returns:
The scope at which this generator would like declare variables
-
inline virtual DeclarationScope tensor_declaration_scope() const override¶
- Returns:
The scope at which this generator would like declare tensors
-
inline virtual PrunableScalars prunable_scalars() const override¶
- Returns:
What kinds of scalars may be pruned from the expression tree in order to avoid binary computation steps for them. Instead, all pruned scalars will be multiplied with whatever binary expression remains after pruning. This means that scalar pruning may lead to non-binary expressions that need to be exported by this generator.
-
inline virtual std::string represent(const Index &idx, const Context&) const override¶
- Returns:
A backend-specific string representation of the given Index
-
inline std::string represent(const TensorPower &power, const Context &ctx) const override¶
-
inline virtual void unload(const Tensor &tensor, const Context &ctx) override¶
Semantic callback for unloading the given tensor. This is expected to discard the tensor without saving its current value. However, the tensor must still be loadable afterwards.
-
inline virtual void destroy(const Tensor &tensor, const Context &ctx) override¶
Semantic callback for destroying the given tensor. Destroying means that this tensor will never be needed again and is no longer required to be loadable.
-
inline virtual void compute(const Expr &expression, const Tensor &result, const Context &ctx) override¶
Semantic callback for encoding the computation of the given expression. The result is expected to be written into the provided Tensor.
-
inline virtual void broadcast_add(const Expr &operand, const Tensor &result, const Context &ctx) override¶
Emits
<result> += <operand einsum>[<: or None per result mode>], the einsum yielding the operand’s modes in the order they have inresult; a scalar operand is added as is
-
inline virtual void declare(const Index&, const Context&) override¶
Semantic callback for declaring the provided Index.
-
inline virtual void all_indices_declared(std::size_t, const Context&) override¶
Callback signalling that all indices have been declared
- Parameters:
amount – The amount of indices that have been declared
-
inline virtual void all_variables_declared(std::size_t, const Context&) override¶
Callback signalling that all variables have been declared
- Parameters:
amount – The amount of variables that have been declared
-
inline virtual void all_tensors_declared(std::size_t, const Context&) override¶
Callback signalling that all tensors have been declared
- Parameters:
amount – The amount of tensors that have been declared
-
inline virtual void begin_declarations(DeclarationScope, const Context&) override¶
Callback signalling that we now start declarations for the given scope.
-
inline virtual void end_declarations(DeclarationScope, const Context&) override¶
Callback signalling that all declarations for the given scope have been performed
-
inline virtual void insert_comment(const std::string &comment, const Context&) override¶
Semantic callback for inserting a comment at the current position in the generated code
- Parameters:
comment – The comment to insert
-
inline virtual void begin_named_section(std::string_view name, const Context&) override¶
Semantic callback that we are starting a new named section
- Parameters:
name – The name of the named section
-
inline virtual void end_named_section(std::string_view, const Context&) override¶
Semantic callback to signal that we have reached the end of the current named section
- Parameters:
name – The name of the named section that is ending now
-
inline virtual void begin_expression(const Context &ctx) override¶
Callback signalling that we now begin export of a new (high-level) expression
-
inline virtual void end_expression(const Context &ctx) override¶
Callback signalling that the export of the current (high-level) expression is finished
-
inline virtual void begin_export(const Context&) override¶
Callback signalling that we now begin with the export process.
-
inline virtual void end_export(const Context &ctx) override¶
Callback signalling that the export process is finished.
-
inline virtual std::string get_generated_code() const override¶
- Returns:
The generated code that corresponds to all semantic actions encountered during the current export
Protected Functions
-
inline std::string tensor_name(const Tensor &tensor, const Context &ctx) const¶
Get the tensor name (without indices).
-
virtual std::string module_prefix() const = 0¶
Backend-specific methods to be overridden by derived classes.
Get the module prefix (np., torch., etc.)
-
virtual std::string file_extension() const = 0¶
Get the file extension for persistence (.npy, .pt, etc.).
-
virtual const std::string &available_index_chars() const = 0¶
Get the available characters for einsum indices.
-
virtual bool use_optimize_parameter() const = 0¶
Backend-specific flag for optimize parameter in einsum.
-
inline std::string get_order_string(const Context &ctx) const¶
Get the Python order string from memory layout.
-
inline std::string sanitize_python_name(std::wstring_view label) const¶
Sanitize a label to be a valid Python identifier.
-
inline std::string get_einsum_index(const Index &idx, const Context&, boost::unordered::unordered_map<std::string, std::string> &index_map, boost::unordered::unordered_set<std::string> &used_chars) const¶
Get or create a single-character einsum index for a given Index.
-
template<std::ranges::input_range Indices>
inline std::string to_einsum_subscript(Indices &&indices, const Context &ctx, boost::unordered::unordered_map<std::string, std::string> &index_map, boost::unordered::unordered_set<std::string> &used_chars) const¶ Convert a sequence of indices to its einsum subscript notation.
-
inline std::string tensor_to_einsum_subscript(const Tensor &tensor, const Context &ctx, boost::unordered::unordered_map<std::string, std::string> &index_map, boost::unordered::unordered_set<std::string> &used_chars) const¶
Convert a tensor to its einsum subscript notation.
-
inline std::string to_einsum_expr(const Expr &expr, const Tensor &result, const Context &ctx) const¶
Convert an expression to an einsum call.
-
template<std::ranges::input_range Indices>
inline std::string to_einsum_expr(const Expr &expr, Indices &&result_indices, const Context &ctx, std::string_view subscript = {}) const¶ Convert an expression to an einsum call
- Parameters:
result_indices – the indices of the einsum’s output, in order
subscript – applied to the einsum (or to its element-wise power), before any scalar factor multiplies it
-
inline std::string stringify_scalar(const Expr &expr, const Context &ctx) const¶
Render a scalar leaf expression (Variable, Constant, or Power thereof) as its Python source representation.
-
inline void extract_einsum_components(const Expr &expr, std::string &einsum_spec, std::vector<std::string> &tensor_names, std::string &scalar_factor, const Context &ctx, boost::unordered::unordered_map<std::string, std::string> &index_map, boost::unordered::unordered_set<std::string> &used_chars, const TensorPower **power = nullptr) const¶
Extract einsum components from an expression
- Parameters:
power – if non-null, receives the TensorPower met in
expr, whose base is then extracted in its place; a second one throws, as does any TensorPower ifpoweris null
-
inline std::string to_python_scalar_expr(const Expr &expr, const Context &ctx) const¶
Convert expression to Python scalar expression (for variable results).
-
inline std::string wrap_conj(std::string s) const¶
-
PythonEinsumGeneratorBase() = default¶