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.etnfile 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.etnfile.compact_file— Rewritepathwithout the space no entry uses.copy_entry— Copy the entrysrc_entryofsrc_pathinto the entrydst_entryofdst_path.etn_backing— The backing for one entry of a.etnfile.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 entrynameofpath.rename_entry— Give the entryold_nameofpaththe namenew_name.restage_entry— Store the entrynameofpathwith its axes inorder, moving its elements.swap_entries— Exchange the names of the entriesfirstandsecondofpath.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
.etnfile 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_backingwith 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:
- class einsums.io.Slab#
A slab descriptor: per-dimension half-open ranges into a stored tensor.
For
read_slice_etnandwrite_slice_etn; ranges[d] is {start, end}, end exclusive. Captured by reference, so mutating ranges between replays drives one node over many slabs.
- class einsums.io.TensorFile#
A handle on an
.etnfile.Writing, rewriting and reserving entries change the handle’s own table until it commits, which
close,flushand 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:syncon, 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.
- 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_namethe namenew_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.
- swap(first: str, second: str) None#
Exchange the names of the entries
firstandsecond.Only the entry table changes, as for
rename. Afterwardsfirstnames whatsecondheld 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
sourcethe entrytarget, dropping whattargetheld.Only the entry table changes, as for
rename, and in one commit: a crash leaves either both entries as they were ortargetholding whatsourceheld and no entrysource. The spacetarget'sold 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.
- replace_all(moves: list[tuple[str, str]]) None#
Make each entry
sourcethe entrytarget, for every pair ofmovesat 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:
- remove(name: str) None#
Remove the entry
name, for every rank that holds one, and commit.Its data region becomes space later entries reuse;
compactgives 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
namewith its axes inorder, 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.
- 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.
- stored_order(name: str) list[int]#
The order
nameis stored in: the declared axis held at each stored position, which is0, 1, ..., rank - 1for an entry stored as declared. SeeTensorEntry::stored_order.
Functions#
- einsums.io.compact_file(path: str) None#
Rewrite
pathwithout 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_entryofsrc_pathinto the entrydst_entryofdst_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. SeeTensorFile::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:
- 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
.etnfile.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 storedAdded 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 replacesentryin one commit, so the file holds the previous replay’s value until then. Not withwriteororder.
- 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
orderasks for; or whenupdateis given withwriteororder, orentryis 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).outputmust 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
nameofpath.Its bytes become space later entries reuse;
compact_filegives them back to the disk. SeeTensorFile::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_nameofpaththe namenew_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.
- einsums.io.restage_entry(path: str, name: str, order: list[int]) None#
Store the entry
nameofpathwith its axes inorder, 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.
- einsums.io.swap_entries(path: str, first: str, second: str) None#
Exchange the names of the entries
firstandsecondofpath.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, fromwrite_etnorTensorFile::reserve.Slab slab{{ {0, 4}, {0, 4} }}; tensor_io::write_slice_etn("scratch.etn", "T", slab, &block);