Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ hdf5-metno = { version = "0.12", default-features = false }
ndarray = "0.17"
quanticsgrids = { git = "https://github.com/tensor4all/quanticsgrids-rs", rev = "a76b8fb" }
hdf5-rt = { git = "https://github.com/tensor4all/hdf5-rt", default-features = false }
tenferro = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "f07a3a08dd188ea5a726a43c6b53de5d417eb0a6", default-features = false }
tenferro-device = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "f07a3a08dd188ea5a726a43c6b53de5d417eb0a6" }
tenferro-einsum = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "f07a3a08dd188ea5a726a43c6b53de5d417eb0a6", default-features = false }
tenferro-tensor = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "f07a3a08dd188ea5a726a43c6b53de5d417eb0a6", default-features = false }
tenferro = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "95fb0724c81cfb2f7640b2cd9255d1700c100358", default-features = false }
tenferro-device = { package = "tenferro-internal-device", git = "https://github.com/tensor4all/tenferro-rs.git", rev = "95fb0724c81cfb2f7640b2cd9255d1700c100358" }
tenferro-einsum = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "95fb0724c81cfb2f7640b2cd9255d1700c100358", default-features = false }
tenferro-linalg = { git = "https://github.com/tensor4all/tenferro-rs.git", rev = "95fb0724c81cfb2f7640b2cd9255d1700c100358", default-features = false }
tenferro-tensor = { package = "tenferro-internal-tensor", git = "https://github.com/tensor4all/tenferro-rs.git", rev = "95fb0724c81cfb2f7640b2cd9255d1700c100358", default-features = false }
11 changes: 4 additions & 7 deletions benchmarks/rust/benchmark_tt_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tensor4all_core::{
TensorContractionLike, TensorDynLen,
};
use tensor4all_itensorlike::{CanonicalForm, ContractOptions, TensorTrain};
use tenferro::{CpuBackend, DotGeneralConfig, EagerContext, EagerTensor, Tensor, TypedTensor};
use tenferro::{CpuBackend, DotGeneralConfig, EagerRuntime, EagerTensor, Tensor, TypedTensor};

#[derive(Debug, Clone)]
struct Options {
Expand Down Expand Up @@ -162,7 +162,7 @@ fn deterministic_native_tensor(shape: Vec<usize>, seed: usize) -> Tensor {
let data = (0..len)
.map(|idx| deterministic_value(idx, seed))
.collect::<Vec<_>>();
Tensor::C64(TypedTensor::from_vec(shape, data))
Tensor::C64(TypedTensor::from_vec_col_major(shape, data))
}

fn make_sites(length: usize, phys_dim: usize) -> Vec<DynIndex> {
Expand Down Expand Up @@ -214,10 +214,7 @@ fn make_native_mps_t4a_shapes(
.collect()
}

fn eager_mps_tensors(
ctx: &Arc<EagerContext>,
tensors: Vec<Tensor>,
) -> Vec<EagerTensor> {
fn eager_mps_tensors(ctx: &Arc<EagerRuntime>, tensors: Vec<Tensor>) -> Vec<EagerTensor> {
tensors
.into_iter()
.map(|tensor| EagerTensor::from_tensor_in(tensor, Arc::clone(ctx)))
Expand Down Expand Up @@ -666,7 +663,7 @@ fn main() -> Result<()> {
let bra = make_mps(&sites, chi, 0)?;
let ket = make_mps(&sites, chi, opts.length)?;
let bra_conj = preconjugate_sites(&bra)?;
let raw_ctx = EagerContext::with_cpu_backend(CpuBackend::with_threads(1));
let raw_ctx = EagerRuntime::with_cpu_backend(CpuBackend::with_threads(1));
let raw_bra = eager_mps_tensors(
&raw_ctx,
make_native_mps_t4a_shapes(opts.length, opts.phys_dim, chi, 0),
Expand Down
17 changes: 17 additions & 0 deletions crates/tensor4all-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,34 @@ backend-tenferro = ["tensor4all-tensorbackend/backend-tenferro"]
tenferro-cpu-faer = [
"tensor4all-tensorbackend/tenferro-cpu-faer",
"tensor4all-tcicore/tenferro-cpu-faer",
"tenferro/autodiff",
"tenferro/cpu-faer",
"tenferro-einsum/autodiff",
"tenferro-einsum/cpu-faer",
"tenferro-linalg/autodiff",
"tenferro-linalg/cpu-faer",
"tenferro-tensor/cpu-faer",
]
tenferro-system-blas = [
"tensor4all-tensorbackend/tenferro-system-blas",
"tensor4all-tcicore/tenferro-system-blas",
"tenferro/autodiff",
"tenferro/cpu-blas",
"tenferro-einsum/autodiff",
"tenferro-einsum/cpu-blas",
"tenferro-linalg/autodiff",
"tenferro-linalg/cpu-blas",
"tenferro-tensor/cpu-blas",
]
tenferro-provider-inject = [
"tensor4all-tensorbackend/tenferro-provider-inject",
"tensor4all-tcicore/tenferro-provider-inject",
"tenferro/autodiff",
"tenferro/cpu-blas",
"tenferro-einsum/autodiff",
"tenferro-einsum/cpu-blas",
"tenferro-linalg/autodiff",
"tenferro-linalg/cpu-blas",
"tenferro-tensor/provider-inject",
]

Expand All @@ -43,6 +58,8 @@ omeco.workspace = true
petgraph.workspace = true
smallvec.workspace = true
tenferro.workspace = true
tenferro-einsum.workspace = true
tenferro-linalg.workspace = true
tenferro-tensor.workspace = true

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/tensor4all-core/src/defaults/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use std::time::{Duration, Instant};
use anyhow::Result;
use petgraph::algo::connected_components;
use petgraph::prelude::*;
use tenferro::eager_tensor::einsum_subscripts as eager_einsum_ad;
use tenferro::EinsumSubscripts;
use tenferro_einsum::eager_tensor::einsum_subscripts as eager_einsum_ad;
use tenferro_einsum::EinsumSubscripts;
use tensor4all_tensorbackend::{einsum_native_tensors, einsum_native_tensors_owned};

use crate::defaults::{DynId, DynIndex, TensorDynLen};
Expand Down
13 changes: 11 additions & 2 deletions crates/tensor4all-core/src/defaults/factorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
use crate::defaults::tensordynlen::unfold_split_inner;
use crate::defaults::DynIndex;
use crate::{contract_pair, unfold_split, TensorDynLen};
use anyhow::Result as AnyhowResult;
use num_complex::{Complex64, ComplexFloat};
use tenferro::EagerTensor;
use tenferro_linalg::eager_tensor::full_piv_lu_solve as eager_full_piv_lu_solve;
use tensor4all_tcicore::{rrlu, AbstractMatrixCI, MatrixLUCI, RrLUOptions, Scalar as MatrixScalar};
use tensor4all_tensorbackend::{Matrix, TensorElement};

Expand Down Expand Up @@ -563,15 +566,15 @@ where
})?;
let (left_inner, right_inner) = match canonical {
Canonical::Left => {
let left = pivot_inner.right_solve(&cols_inner).map_err(|e| {
let left = eager_right_solve(&pivot_inner, &cols_inner).map_err(|e| {
FactorizeError::ComputationError(anyhow::anyhow!(
"fixed-pivot CI right solve failed: {e}"
))
})?;
(left, rows_inner)
}
Canonical::Right => {
let right = pivot_inner.solve(&rows_inner).map_err(|e| {
let right = eager_full_piv_lu_solve(&pivot_inner, &rows_inner).map_err(|e| {
FactorizeError::ComputationError(anyhow::anyhow!(
"fixed-pivot CI solve failed: {e}"
))
Expand Down Expand Up @@ -613,6 +616,12 @@ where
})
}

fn eager_right_solve(a: &EagerTensor, rhs: &EagerTensor) -> AnyhowResult<EagerTensor> {
let a_t = a.transpose(&[1, 0])?;
let rhs_t = rhs.transpose(&[1, 0])?;
Ok(eager_full_piv_lu_solve(&a_t, &rhs_t)?.transpose(&[1, 0])?)
}

/// Convert a native rank-2 tensor into a backend [`Matrix`].
fn native_tensor_to_matrix<T>(
tensor: &tenferro::Tensor,
Expand Down
6 changes: 3 additions & 3 deletions crates/tensor4all-core/src/defaults/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::global_default::GlobalDefault;
use crate::TensorDynLen;
use num_complex::ComplexFloat;
use tenferro::DType;
use tenferro_linalg::eager_tensor::qr as eager_qr;
use tensor4all_tensorbackend::{
native_tensor_primal_to_dense_c64_col_major, native_tensor_primal_to_dense_f64_col_major,
};
Expand Down Expand Up @@ -258,9 +259,8 @@ pub fn qr_with<T>(
.map_err(|e| anyhow::anyhow!("Failed to unfold tensor: {}", e))
.map_err(QrError::ComputationError)?;
let k = m.min(n);
let (mut q_inner, mut r_inner) = matrix_inner
.qr()
.map_err(|e| QrError::ComputationError(anyhow::anyhow!("{e}")))?;
let (mut q_inner, mut r_inner) =
eager_qr(&matrix_inner).map_err(|e| QrError::ComputationError(anyhow::anyhow!("{e}")))?;

let r = if options.truncate {
// Determine rtol to use
Expand Down
7 changes: 4 additions & 3 deletions crates/tensor4all-core/src/defaults/structured_contraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,14 @@ pub(crate) fn normalize_payload_read_for_roots<'a>(

pub(crate) fn storage_payload_native(storage: &Storage) -> Result<NativeTensor> {
if storage.is_f64() {
Ok(NativeTensor::from_vec(
Ok(NativeTensor::from_vec_col_major(
storage.payload_dims().to_vec(),
storage
.payload_f64_col_major_vec()
.map_err(anyhow::Error::msg)?,
))
} else if storage.is_c64() {
Ok(NativeTensor::from_vec(
Ok(NativeTensor::from_vec_col_major(
storage.payload_dims().to_vec(),
storage
.payload_c64_col_major_vec()
Expand Down Expand Up @@ -527,7 +527,8 @@ mod tests {

#[test]
fn normalizes_repeated_payload_roots_by_extracting_diagonal() {
let payload = tenferro::Tensor::from_vec(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0]);
let payload =
tenferro::Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0]);
let (normalized, roots) = normalize_payload_for_roots(&payload, &[0, 0]).unwrap();

assert_eq!(normalized.shape(), &[2]);
Expand Down
6 changes: 3 additions & 3 deletions crates/tensor4all-core/src/defaults/svd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::truncation::{
use crate::TensorDynLen;
use std::sync::Mutex;
use tenferro::{DType, EagerTensor};
use tenferro_linalg::eager_tensor::svd as eager_svd;
use tensor4all_tensorbackend::{
native_tensor_primal_to_dense_c64_col_major, native_tensor_primal_to_dense_f64_col_major,
};
Expand Down Expand Up @@ -238,9 +239,8 @@ fn svd_truncated_inner(
.map_err(SvdError::ComputationError)?;
let k = m.min(n);

let (mut u_inner, mut s_inner, mut vt_inner) = matrix_inner
.svd()
.map_err(|e| SvdError::ComputationError(anyhow::anyhow!("{e}")))?;
let (mut u_inner, mut s_inner, mut vt_inner) =
eager_svd(&matrix_inner).map_err(|e| SvdError::ComputationError(anyhow::anyhow!("{e}")))?;
let s_full = singular_values_from_native(s_inner.data())?;
let mut r = if options.truncate {
let policy = options.policy.unwrap_or_else(default_svd_truncation_policy);
Expand Down
6 changes: 3 additions & 3 deletions crates/tensor4all-core/src/defaults/svd/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ fn compute_retained_rank_supports_all_policy_axes() {

#[test]
fn singular_values_from_native_accepts_real_and_complex_dense() {
let dense = NativeTensor::from_vec(vec![2], vec![3.0_f64, 1.5]);
let dense = NativeTensor::from_vec_col_major(vec![2], vec![3.0_f64, 1.5]);
assert_eq!(singular_values_from_native(&dense).unwrap(), vec![3.0, 1.5]);

let complex = NativeTensor::from_vec(
let complex = NativeTensor::from_vec_col_major(
vec![2],
vec![Complex64::new(1.0, 2.0), Complex64::new(0.5, -4.0)],
);
Expand Down Expand Up @@ -89,7 +89,7 @@ fn svd_options_accessors_roundtrip() {

#[test]
fn singular_values_from_native_rejects_unsupported_scalar_types() {
let tensor = NativeTensor::from_vec(vec![2], vec![1.0_f32, 2.0]);
let tensor = NativeTensor::from_vec_col_major(vec![2], vec![1.0_f32, 2.0]);
let err = singular_values_from_native(&tensor).unwrap_err();
assert!(err
.to_string()
Expand Down
19 changes: 12 additions & 7 deletions crates/tensor4all-core/src/defaults/tensordynlen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use std::collections::{HashMap, HashSet};
use std::env;
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use tenferro::eager_tensor::einsum_subscripts as eager_einsum_ad;
use tenferro::{DType, DotGeneralConfig, EagerTensor, EinsumSubscripts, Tensor as NativeTensor};
use tenferro::{DType, DotGeneralConfig, EagerTensor, Tensor as NativeTensor};
use tenferro_einsum::eager_tensor::einsum_subscripts as eager_einsum_ad;
use tenferro_einsum::EinsumSubscripts;
use tensor4all_tensorbackend::{
axpby_native_tensor, contract_native_tensor, default_eager_ctx,
dense_native_tensor_from_col_major, diag_native_tensor_from_col_major,
Expand Down Expand Up @@ -125,7 +126,9 @@ fn native_tensor_profile_bytes(native: &NativeTensor) -> usize {
DType::F64 => 8,
DType::C32 => 8,
DType::C64 => 16,
DType::I32 => 4,
DType::I64 => 8,
DType::Bool => 1,
};
native.shape().iter().product::<usize>() * element_size
}
Expand Down Expand Up @@ -1048,10 +1051,12 @@ impl TensorDynLen {
) -> Result<Storage> {
if Self::is_diag_axis_classes(axis_classes) {
match native.dtype() {
DType::F32 | DType::F64 | DType::I64 => Storage::from_diag_col_major(
native_tensor_primal_to_diag_f64(native)?,
logical_rank,
),
DType::F32 | DType::F64 | DType::I32 | DType::I64 | DType::Bool => {
Storage::from_diag_col_major(
native_tensor_primal_to_diag_f64(native)?,
logical_rank,
)
}
DType::C32 | DType::C64 => Storage::from_diag_col_major(
native_tensor_primal_to_diag_c64(native)?,
logical_rank,
Expand Down Expand Up @@ -1535,7 +1540,7 @@ impl TensorDynLen {
}

let starts_tensor = EagerTensor::from_tensor_in(
NativeTensor::from_vec(vec![rank], starts),
NativeTensor::from_vec_col_major(vec![rank], starts),
default_eager_ctx(),
);
let sliced = self
Expand Down
53 changes: 44 additions & 9 deletions crates/tensor4all-simplett/src/einsum_helper.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::error::Error;
use std::fmt;

use tenferro_einsum::typed_eager_einsum;
use tenferro_tensor::{TensorScalar, TypedTensor};
use tensor4all_tensorbackend::with_default_backend;
use tenferro_einsum::Subscripts;
use tenferro_tensor::{Tensor, TensorScalar, TypedTensor};
use tensor4all_tensorbackend::einsum_native_tensors;

pub(crate) type Result<T> = std::result::Result<T, EinsumHelperError>;

Expand Down Expand Up @@ -91,12 +91,44 @@ pub(crate) fn einsum_tensors<T: EinsumScalar>(
subscripts: &str,
operands: &[&TypedTensor<T>],
) -> Result<TypedTensor<T>> {
with_default_backend(|backend| typed_eager_einsum(backend, operands, subscripts)).map_err(
|err| EinsumHelperError::Backend {
let parsed = Subscripts::parse(subscripts).map_err(|err| EinsumHelperError::Backend {
subscripts: subscripts.to_string(),
message: err.to_string(),
})?;
let tensors: Vec<Tensor> = operands
.iter()
.map(|tensor| T::into_tensor(tensor.shape.clone(), tensor.host_data().to_vec()))
.collect();
let input_ids = parsed
.inputs
.iter()
.map(|ids| ids.iter().map(|&id| id as usize).collect::<Vec<_>>())
.collect::<Vec<_>>();
let operand_refs = tensors
.iter()
.zip(input_ids.iter())
.map(|(tensor, ids)| (tensor, ids.as_slice()))
.collect::<Vec<_>>();
let output_ids = parsed
.output
.iter()
.map(|&id| id as usize)
.collect::<Vec<_>>();

let result = einsum_native_tensors(&operand_refs, &output_ids).map_err(|err| {
EinsumHelperError::Backend {
subscripts: subscripts.to_string(),
message: err.to_string(),
},
)
}
})?;
let actual = result.dtype();
T::try_into_typed(result).ok_or_else(|| EinsumHelperError::Backend {
subscripts: subscripts.to_string(),
message: format!(
"dtype mismatch: result has {actual:?}, expected {:?}",
T::dtype()
),
})
}

pub(crate) fn typed_tensor_from_col_major_slice<T: TensorScalar>(
Expand All @@ -112,7 +144,10 @@ pub(crate) fn typed_tensor_from_col_major_slice<T: TensorScalar>(
});
}

Ok(TypedTensor::from_vec(shape.to_vec(), data.to_vec()))
Ok(TypedTensor::from_vec_col_major(
shape.to_vec(),
data.to_vec(),
))
}

pub(crate) fn typed_tensor_reshape<T: TensorScalar>(
Expand All @@ -130,7 +165,7 @@ pub(crate) fn typed_tensor_reshape<T: TensorScalar>(
});
}

Ok(TypedTensor::from_vec(
Ok(TypedTensor::from_vec_col_major(
shape.to_vec(),
tensor.host_data().to_vec(),
))
Expand Down
2 changes: 1 addition & 1 deletion crates/tensor4all-simplett/src/mpo/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ pub fn tensor4_from_data<T: TensorScalar>(
});
}
let dims = [left_dim, site_dim_1, site_dim_2, right_dim];
let inner = TfTensor::from_vec(dims.to_vec(), data);
let inner = TfTensor::from_vec_col_major(dims.to_vec(), data);
Ok(Tensor::from_tenferro_unchecked(inner))
}

Expand Down
Loading
Loading