Skip to content

Commit e4613ab

Browse files
authored
Merge pull request #546 from ValeevGroup/evaleev/build/bump-btas-pin
enable btas::Tensor as inner tile of TA::Tensor-of-Tensor + bump BTAS pin
2 parents 03e1c9a + c782d06 commit e4613ab

16 files changed

Lines changed: 762 additions & 304 deletions

external/versions.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ set(TA_TRACKED_MADNESS_PREVIOUS_TAG 7d8aaf9d51981e4accf4d84742270d1473f8ca2e)
1717
set(TA_TRACKED_MADNESS_VERSION 0.10.1)
1818
set(TA_TRACKED_MADNESS_PREVIOUS_VERSION 0.10.1)
1919

20-
set(TA_TRACKED_BTAS_TAG 287b145ead818a0332f2b7ce0b7375a83d328bae)
21-
set(TA_TRACKED_BTAS_PREVIOUS_TAG 62d57d9b1e0c733b4b547bc9cfdd07047159dbca)
20+
set(TA_TRACKED_BTAS_TAG 245e49f117981d6124e0f1aa0d1ae72f1c16318b)
21+
set(TA_TRACKED_BTAS_PREVIOUS_TAG 7e64fbad97c76f316f313f4c8ed3fca5445da15f)
2222

2323
set(TA_TRACKED_LIBRETT_TAG 6eed30d4dd2a5aa58840fe895dcffd80be7fbece)
2424
set(TA_TRACKED_LIBRETT_PREVIOUS_TAG 354e0ccee54aeb2f191c3ce2c617ebf437e49d83)

src/TiledArray/dist_array.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,8 +1852,20 @@ size_t volume(const DistArray<Tile, Policy>& array) {
18521852

18531853
auto local_vol = [&vol](Tile const& in_tile) {
18541854
if constexpr (detail::is_tensor_of_tensor_v<Tile>) {
1855+
// Inner tile pointer is passed (see is_reduce_op_v in
1856+
// tensor/type_traits.h selecting the pointer-passing tensor_reduce
1857+
// overload). Prefer `total_size()` (TA::Tensor exposes it, batches
1858+
// included); fall back to `size()` for inner tile types that don't
1859+
// (e.g. btas::Tensor).
18551860
auto reduce_op = [](size_t& MADNESS_RESTRICT result, auto&& arg) {
1856-
result += arg->total_size();
1861+
using InnerTile =
1862+
std::remove_cv_t<std::remove_reference_t<decltype(*arg)>>;
1863+
if constexpr (detail::has_member_function_total_size_anyreturn_v<
1864+
InnerTile>) {
1865+
result += arg->total_size();
1866+
} else {
1867+
result += arg->size();
1868+
}
18571869
};
18581870
auto join_op = [](auto& MADNESS_RESTRICT result, size_t count) {
18591871
result += count;

src/TiledArray/einsum/tiledarray.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,31 @@ template <typename ArrayT1, typename ArrayT2>
185185
constexpr bool AreArraySame =
186186
AreArrayT<ArrayT1, ArrayT2> || AreArrayToT<ArrayT1, ArrayT2>;
187187

188+
// "Denested" companion of a ToT array: drops the inner-tile nesting, leaving
189+
// a regular (non-nested) DistArray. For ToT inputs, the outer tile of the
190+
// denested array is always TA::Tensor — nested inner-tile types (e.g.
191+
// btas::Tensor) are only valid as the *innermost* tile and don't support the
192+
// outer-tile operations einsum needs (permute/reshape/batch/range+lambda
193+
// ctor). So for ToT we drop the inner tile and re-wrap its numeric type in
194+
// TA::Tensor. For non-ToT inputs, the original "drop one level" behavior is
195+
// preserved.
196+
namespace detail_denested {
197+
template <typename Array, typename Enabler = void>
198+
struct denested {
199+
using type = DistArray<typename Array::value_type::value_type,
200+
typename Array::policy_type>;
201+
};
202+
template <typename Array>
203+
struct denested<Array,
204+
std::enable_if_t<TiledArray::detail::is_tensor_of_tensor_v<
205+
typename Array::value_type>>> {
206+
using type = DistArray<
207+
TA::Tensor<typename Array::value_type::value_type::numeric_type>,
208+
typename Array::policy_type>;
209+
};
210+
} // namespace detail_denested
188211
template <typename Array>
189-
using DeNestedArray = DistArray<typename Array::value_type::value_type,
190-
typename Array::policy_type>;
212+
using DeNestedArray = typename detail_denested::denested<Array>::type;
191213

192214
template <typename Array1, typename Array2>
193215
using MaxNestedArray = std::conditional_t<(detail::nested_rank<Array2> >
@@ -496,13 +518,21 @@ auto einsum(expressions::TsrExpr<ArrayA_> A, expressions::TsrExpr<ArrayB_> B,
496518
// Step III: C1(ijpqab) -> C2(ijpq)
497519
// Step IV: C2(ijpq) -> C(ipjq)
498520

521+
// Build a "denested" tile: one scalar per outer index, summed over the
522+
// inner tile. The result tile's outer type is TA::Tensor (inner tile
523+
// types like btas::Tensor are only valid as the innermost tile and don't
524+
// expose the range+lambda ctor used here).
499525
auto sum_tot_2_tos = [](auto const &tot) {
500526
using tot_t = std::remove_reference_t<decltype(tot)>;
501-
typename tot_t::value_type result(tot.range(), [tot](auto &&ix) {
527+
using numeric_type = typename tot_t::numeric_type;
528+
TA::Tensor<numeric_type> result(tot.range(), [tot](auto &&ix) {
529+
// unqualified `sum` so ADL finds the right overload for both
530+
// TA::Tensor inner (free fn in namespace TiledArray, calls .sum())
531+
// and btas::Tensor inner (free fn in namespace btas).
502532
if (!tot(ix).empty())
503-
return tot(ix).sum();
533+
return sum(tot(ix));
504534
else
505-
return typename tot_t::numeric_type{};
535+
return numeric_type{};
506536
});
507537
return result;
508538
};
@@ -722,7 +752,7 @@ auto einsum(expressions::TsrExpr<ArrayA_> A, expressions::TsrExpr<ArrayB_> B,
722752
using TensorT = std::remove_reference_t<decltype(el)>;
723753

724754
for (auto i = 0; i < vol; ++i)
725-
el.add_to(element_product_op(aik.data()[i], bik.data()[i]));
755+
add_to(el, element_product_op(aik.data()[i], bik.data()[i]));
726756

727757
} else if constexpr (!AreArraySame<ArrayA, ArrayB>) {
728758
auto aik = ai.batch(k);
@@ -734,9 +764,9 @@ auto einsum(expressions::TsrExpr<ArrayA_> A, expressions::TsrExpr<ArrayB_> B,
734764

735765
for (auto i = 0; i < vol; ++i)
736766
if constexpr (IsArrayToT<ArrayA>) {
737-
el.add_to(aik.data()[i].scale(bik.data()[i]));
767+
add_to(el, scale(aik.data()[i], bik.data()[i]));
738768
} else {
739-
el.add_to(bik.data()[i].scale(aik.data()[i]));
769+
add_to(el, scale(bik.data()[i], aik.data()[i]));
740770
}
741771

742772
} else {

0 commit comments

Comments
 (0)