einsums.io#

Note

This page is generated by apiary from the binding annotations and the package’s Python source. Do not edit by hand.

Summary#

  • EtnFile — A .etn file named once, for the backings of its entries.

  • Slab — A slab descriptor: per-dimension half-open ranges into a stored tensor.

  • TensorFile — A handle on an .etn file.

  • compact_file — Rewrite path without the space no entry uses.

  • copy_entry — Copy the entry src_entry of src_path into the entry dst_entry of dst_path.

  • etn_backing — The backing for one entry of a .etn file.

  • read — Read a tensor from a .etn file, graph-aware.

  • read_slice — Read a slab of a tensor from a .etn file, graph-aware.

  • remove_entry — Remove the entry name of path.

  • rename_entry — Give the entry old_name of path the name new_name.

  • restage_entry — Store the entry name of path with its axes in order, moving its elements.

  • swap_entries — Exchange the names of the entries first and second of path.

  • write — Write a tensor to a .etn file, graph-aware.

  • write_slice — Write a slab of a tensor to a .etn file, graph-aware.

Classes#

class einsums.io.EtnFile#

A .etn file named once, for the backings of its entries.

Holds only the path: nothing is opened until a backing is built, and each backing reopens the file by path at every slab as etn_backing’s do. A program that keeps several tensors in one file names it here rather than in every backing.

ints = einsums.io.EtnFile("ints.etn")
B = graph.declare_tensor("Bvv", backing=ints.entry("Bvv"))
T = graph.declare_tensor("T2", dims, backing=ints.entry("T2", write=True))

Added in version 2.0.0.

__init__(path: str) → None#

Name the file.

Parameters:

path – The file, stored as given; see etn_backing.

entry(entry: str, write: bool = False, order: list[int] = std::vector<std::size_t>{}, update: bool = False) → TensorBacking#

The backing for one entry of this file: etn_backing with this file’s path.

Parameters:
  • entry – The entry’s name within the file.

  • write – False for an input the graph reads, true for a tensor it produces and stores.

  • order – For a write backing, the order to store a new entry’s axes in.

  • update – True for a tensor the graph reads and writes, which updates the entry at every replay; see etn_backing.

Returns:

The backing.

Raises:
path: str#

The file’s path.

(read-only)

class einsums.io.Slab#

A slab descriptor: per-dimension half-open ranges into a stored tensor.

For read_slice_etn and write_slice_etn; ranges[d] is {start, end}, end exclusive. Captured by reference, so mutating ranges between replays drives one node over many slabs.

__init__() → None#
__init__(ranges: list[tuple[int, int]]) → None
ranges: list[tuple[int, int]]#
class einsums.io.TensorFile#

A handle on an .etn file.

Writing, rewriting and reserving entries change the handle’s own table until it commits, which close, flush and the destructor do, and each entry operation (rename, swap, copy, remove, restage, compact) does before it returns. A commit puts new data and a new table into space the file’s last commit does not use and then rewrites the header, so a process that stops at any point leaves the file as its last commit left it. With --einsums:io:sync on, the default, a power loss does too. A handle refuses to commit over a file another handle has committed to since this one last read it.

__init__(path: str, mode: Mode = einsums::tensor_io::TensorFile::Mode::ReadWrite) → None#

Open or create a tensor file.

Parameters:
  • path – File path (creates parent directories if needed).

  • mode – Read, Write, or ReadWrite.

read(name: str, tensor: GeneralRuntimeTensor) → None#

Read a full tensor into a RuntimeTensor. Resizes the tensor.

write(name: str, tensor: GeneralRuntimeTensor) → None#

Write a full RuntimeTensor as a new entry.

read_slice(name: str, tensor: GeneralRuntimeTensor, ranges: list[tuple[int, int]]) → None#

Read a slice into a pre-sized RuntimeTensor. The tensor’s dims must equal the slab (ranges[d].second - ranges[d].first).

write_slice(name: str, tensor: GeneralRuntimeTensor, ranges: list[tuple[int, int]]) → None#

Write a slice (hyperslab) of an existing entry from a RuntimeTensor.

rename(old_name: str, new_name: str) → None#

Give the entry old_name the name new_name.

Only the entry table changes, so a rename costs the same whatever the entry holds. It is committed before this returns. Renaming an entry to its own name does nothing.

Raises:
  • Exception – when there is no entry old_name, or the file is open for reading.

  • Exception – when an entry is already named new_name, or no entry can hold that name.

swap(first: str, second: str) → None#

Exchange the names of the entries first and second.

Only the entry table changes, as for rename. Afterwards first names what second held and the other way round, which is the step a program that writes the next value of a tensor beside the current one takes to make the next value current. Swapping an entry with itself does nothing.

Raises:

Exception – when either entry is missing, or the file is open for reading.

replace(source: str, target: str) → None#

Make the entry source the entry target, dropping what target held.

Only the entry table changes, as for rename, and in one commit: a crash leaves either both entries as they were or target holding what source held and no entry source. The space target's old elements took is free from that commit on. This is the step that makes a value written beside the current one current, leaving no second entry behind. Replacing an entry with itself does nothing.

Added in version 2.0.0.

Raises:
  • Exception – when there is no entry source, or the file is open for reading.

  • Exception – when no entry can hold the name target.

replace_all(moves: list[tuple[str, str]]) → None#

Make each entry source the entry target, for every pair of moves at once.

Every pair is applied as by the overload for one pair, and all of them in one commit: a crash leaves either every entry as it was or every replacement made. This is the step that makes the values written beside several current ones current together.

Added in version 2.0.0.

Parameters:

moves – The (source, target) pairs. A pair whose source is its target does nothing.

Raises:
  • Exception – when a source is missing, or the file is open for reading.

  • Exception – when no entry can hold a target’s name, or a name is the source or the target of more than one pair, or both a source and a target, which would make the outcome depend on the order the pairs were applied in.

remove(name: str) → None#

Remove the entry name, for every rank that holds one, and commit.

Its data region becomes space later entries reuse; compact gives it back to the disk.

Raises:

Exception – when there is no entry name, or the file is open for reading.

restage(name: str, order: list[int], buffer_bytes: int = 0) → None#

Store the entry name with its axes in order, moving its elements, and commit.

The elements are copied into a new region in the new order through a buffer of at most buffer_bytes, and one commit then points the entry at it, so the entry holds its old layout or its new one whenever the program stops. The file needs room for a second copy of the entry while this runs. Restaging to the order the entry already has does nothing.

Parameters:
  • name – The entry.

  • order – The declared axis to store at each position, as stored_order returns it.

  • buffer_bytes – As for copy.

Raises:
  • Exception – when there is no entry name, or the file is open for reading.

  • Exception – when order is not a permutation of the entry’s axes.

compact(buffer_bytes: int = 0) → None#

Rewrite the file without the space no entry uses.

Every entry is copied, in its stored order and through a buffer of at most buffer_bytes, into a new file beside this one, which then replaces it. The file is this one or the compacted one whenever the program stops, and the disk needs room for the entries’ bytes while this runs. Another handle open on the file refuses to commit afterwards; backings, which open the file for each slab, see the compacted file at their next slab.

Parameters:

buffer_bytes – As for copy.

Raises:

Exception – when the file is open for reading.

contains(name: str) → bool#

Check if a tensor exists in the file.

dims(name: str) → list[int]#

Get the stored dimensions of a tensor.

stored_order(name: str) → list[int]#

The order name is stored in: the declared axis held at each stored position, which is 0, 1, ..., rank - 1 for an entry stored as declared. See TensorEntry::stored_order.

unused_bytes() → int#

The bytes of the file, as of the last commit, that hold neither the header, an entry’s elements nor the entry table: what compact would give back.

tensor_names() → list[str]#

List all tensor names in the file.

num_tensors() → int#

Number of tensors in the file.

flush() → None#

Commit, and sync the file to disk whatever --einsums:io:sync says.

close() → None#

Commit and close the file. Unlike the destructor, throws when the commit fails.

path() → str#

Get the file path.

class Mode#
Read = 0#
Write = 1#
ReadWrite = 2#

Functions#

einsums.io.compact_file(path: str) → None#

Rewrite path without the space no entry uses.

The file is the old one or the compacted one whenever the program stops, and the disk needs room for the entries’ bytes while this runs. See TensorFile::compact.

Added in version 2.0.0.

Raises:

Exception – when the file is missing.

einsums.io.copy_entry(src_path: str, src_entry: str, dst_path: str, dst_entry: str) → None#

Copy the entry src_entry of src_path into the entry dst_entry of dst_path.

The destination file is created when it does not exist, and the destination entry, with the source’s extents, element type and stored order, when it does not exist. An existing one must have the same extents and element type. The elements pass through a buffer of at most --einsums:work-buffer-size, so neither entry is held whole. See TensorFile::copy.

Added in version 2.0.0.

Parameters:
  • src_path – The file holding the entry to copy.

  • src_entry – The entry to copy.

  • dst_path – The file to copy it into, which may be src_path.

  • dst_entry – The entry to copy it into.

Raises:
  • Exception – when the source file or entry is missing.

  • Exception – when the destination entry exists with other extents or another element type.

einsums.io.etn_backing(path: str, entry: str, write: bool = False, order: list[int] = std::vector<std::size_t>{}, update: bool = False) → TensorBacking#

The backing for one entry of a .etn file.

Each read or write opens the file, checks that the entry still has the extents and element type it had when the backing was built, and moves the slab: a file rewritten between replays is used afresh, and one rewritten with a different shape is refused rather than read or written as the wrong elements.

auto &B = graph.declare_runtime_tensor<double>("B_vv", {naux, nvir, nvir});
graph.annotate_backing(B, tensor_io::etn_backing("ints.etn", "B_vv"));         // an input
auto &T = graph.declare_runtime_tensor<double>("T2", {nocc, nocc, nvir, nvir});
graph.annotate_backing(T, tensor_io::etn_backing("amps.etn", "T2", true));     // produced and stored

Added in version 2.0.0.

Parameters:
  • path – The file. Stored as given: a relative path is resolved against the working directory each time the graph reads or writes it, and again when a saved graph is loaded.

  • entry – The entry’s name within the file.

  • write – False (the default) for an input the graph reads: the entry must exist, and its extents and element type are read from it now. True for a tensor the graph produces and stores: the file and entry are created, or reused when the entry already holds a tensor of the same shape and type, when the backing is annotated on its tensor, which supplies that shape and type.

  • order – For a write backing, the order to store a new entry’s axes in: the declared axis at each stored position (see TensorEntry::stored_order). Empty, the default, leaves it to the graph, which stores a new entry so the boxes it moves are long contiguous runs of the file. Give it when another run will read the entry in boxes of its own. An entry that already exists has to be stored in this order to be reused.

  • update – True for a tensor the graph both reads and writes, whose entry it updates at every replay: the entry must exist, and its extents and element type are read from it now. The tensor starts each replay holding what the entry holds, its writes go to the scratch entry <entry>.next, and at the end of the replay that entry replaces entry in one commit, so the file holds the previous replay’s value until then. Not with write or order.

Returns:

The backing, for compute_graph::Graph::annotate_backing.

Raises:
  • Exception – when a read backing’s file cannot be opened or holds no such entry.

  • Exception – when a read backing’s entry holds integers, which no graph tensor does, or, at annotation, when a write backing’s entry already holds another shape or type, or is stored in another order than order asks for; or when update is given with write or order, or entry is too long to name its scratch entry.

einsums.io.read(file_path: str, tensor_name: str, output: Any) → None#

Read a tensor from a .etn file, graph-aware.

During graph capture, records a DiskRead node. Outside capture, reads immediately. Works with IOPrefetch for async overlap when using DataflowExecutor.

einsums.io.read_slice(file_path: str, tensor_name: str, slab: Slab, output: Any) → None#

Read a slab of a tensor from a .etn file, graph-aware.

During capture, records a DiskRead node (see Slab). output must already have the slab’s shape, or execution throws.

Slab slab{{ {0, 4}, {0, 4} }};
tensor_io::read_slice_etn("integrals.etn", "ERI", slab, &block);
einsums.io.remove_entry(path: str, name: str) → None#

Remove the entry name of path.

Its bytes become space later entries reuse; compact_file gives them back to the disk. See TensorFile::remove.

Added in version 2.0.0.

Raises:

Exception – when the file or the entry is missing.

einsums.io.rename_entry(path: str, old_name: str, new_name: str) → None#

Give the entry old_name of path the name new_name.

Only the file’s entry table changes, so a rename costs the same whatever the entry holds. See TensorFile::rename.

Added in version 2.0.0.

Raises:
  • Exception – when the file or the entry is missing.

  • Exception – when an entry is already named new_name, or no entry can hold it.

einsums.io.restage_entry(path: str, name: str, order: list[int]) → None#

Store the entry name of path with its axes in order, moving its elements.

The entry holds its old layout or its new one whenever the program stops, and the file needs room for a second copy of it while this runs. See TensorFile::restage.

Added in version 2.0.0.

Parameters:
  • path – The file.

  • name – The entry.

  • order – The declared axis to store at each position.

Raises:
  • Exception – when the file or the entry is missing.

  • Exception – when order is not a permutation of the entry’s axes.

einsums.io.swap_entries(path: str, first: str, second: str) → None#

Exchange the names of the entries first and second of path.

Only the file’s entry table changes. A program that reads one entry and writes the next value beside it swaps the two after each step, so the same entry always holds the current value and one graph serves every step. See TensorFile::swap.

Added in version 2.0.0.

Raises:

Exception – when the file or either entry is missing.

einsums.io.write(file_path: str, tensor_name: str, input: Any) → None#

Write a tensor to a .etn file, graph-aware.

During graph capture, records a DiskWrite node that executes after the tensor is computed. Outside capture, writes immediately.

einsums.io.write_slice(file_path: str, tensor_name: str, slab: Slab, input: Any) → None#

Write a slab of a tensor to a .etn file, graph-aware.

During capture, records a DiskWrite node that patches the slab in place (see Slab). The entry must already exist, from write_etn or TensorFile::reserve.

Slab slab{{ {0, 4}, {0, 4} }};
tensor_io::write_slice_etn("scratch.etn", "T", slab, &block);