Template Class PythonEinsumGeneratorBase

Inheritance Relationships

Base Type

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 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 virtual std::string represent(const Tensor &tensor, const Context &ctx) const override
Returns:

A backend-specific string representation of the given Tensor

inline virtual std::string represent(const Variable &variable, const Context&) const override
Returns:

A backend-specific string representation of the given Variable

inline virtual std::string represent(const Constant &constant, const Context&) const override
Returns:

A backend-specific string representation of the given Constant

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 create(const Variable &variable, bool zero_init, const Context &ctx) override

Semantic callback for creating the given variable. This is expected to make the created variable available for further use.

inline virtual void set_to_zero(const Variable &variable, const Context &ctx) override

Semantic callback for setting the given variable to zero.

inline virtual void unload(const Variable &variable, const Context &ctx) override

Semantic callback for unloading the given variable. This is expected to discard the variable without saving its current value. However, the variable must still be loadable afterwards.

inline virtual void destroy(const Variable &variable, const Context &ctx) override

Semantic callback for destroying the given variable. Destroying means that this variable 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 compute(const Expr &expression, const Variable &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 Variable.

inline virtual void declare(const Index&, const Context&) override

Semantic callback for declaring the provided Index.

inline virtual void declare(const Variable&, UsageSet, const Context&) override

Semantic callback for declaring the provided Variable

Parameters:
inline virtual void declare(const Tensor&, UsageSet, const Context&) override

Semantic callback for declaring the provided Tensor

Parameters:
  • variable – The Tensor to declare

  • usage – A UsageSet describing how the Tensor will be used in the code

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.

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.

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

Extract einsum components from an expression.

inline std::string to_python_scalar_expr(const Expr &expr, const Context &ctx) const

Convert expression to Python scalar expression (for variable results)

Protected Attributes

std::string m_generated
std::string m_indent