\documentclass[11pt,a4paper,twoside]{article} \usepackage[T1]{fontenc} \usepackage{amsmath,amssymb,amsthm,bm,booktabs,graphicx,geometry,array} \usepackage[font=small]{caption} \usepackage{tikz}\usetikzlibrary{arrows.meta,positioning,fit,backgrounds,calc} \usepackage{lmodern} \usepackage{xcolor,titlesec,fancyhdr} \usepackage{algorithm,algpseudocode} \definecolor{articleblue}{HTML}{00558B} \definecolor{articlegreen}{HTML}{008000} \definecolor{articlered}{HTML}{A00000} \definecolor{articlerule}{HTML}{98999B} \usepackage[colorlinks=true,linkcolor=articlegreen,citecolor=articlegreen,urlcolor=articlered]{hyperref} \geometry{a4paper,textwidth=156mm,textheight=222mm,inner=27mm,top=34mm,headheight=22pt,headsep=5mm,footskip=10mm} \setlength{\parindent}{1.5em} \setlength{\parskip}{0pt} \titleformat{\section}[runin]{\normalfont\sffamily\bfseries\color{articleblue}}{\thesection.}{.5em}{}[.] \titleformat{\subsection}[runin]{\normalfont\sffamily\bfseries\color{articleblue}}{\thesubsection.}{.5em}{}[.] \titleformat{\subsubsection}[runin]{\normalfont\sffamily\bfseries\color{articleblue}}{\thesubsubsection.}{.5em}{}[.] \titlespacing*{\section}{\parindent}{2.4ex plus .7ex minus .3ex}{.6em} \titlespacing*{\subsection}{\parindent}{1.8ex plus .5ex minus .2ex}{.6em} \titlespacing*{\subsubsection}{\parindent}{1.5ex plus .4ex minus .2ex}{.6em} \pagestyle{fancy} \fancyhf{} \fancyhead[LE,RO]{\sffamily\small\bfseries\color{articleblue}\thepage} \fancyhead[RE]{\sffamily\small\bfseries\color{articleblue}F. BOTTEGA DINIZ} \fancyhead[LO]{\sffamily\small\bfseries\color{articleblue}TENSOR FOX 2.0.0} \renewcommand{\headrulewidth}{1.5pt} \renewcommand{\headrule}{\hbox to\headwidth{\color{articlerule}\leaders\hrule height\headrulewidth\hfill}\vskip-\headrulewidth} \fancypagestyle{plain}{\fancyhf{}\fancyfoot[C]{\sffamily\small\bfseries\color{articleblue}\thepage}\renewcommand{\headrulewidth}{0pt}} \makeatletter \renewcommand{\maketitle}{\thispagestyle{plain}\begingroup\centering \vspace*{3mm}{\sffamily\bfseries\large\color{articleblue}\@title\par}\vspace{1.2em} {\sffamily\normalsize\color{articleblue}\@author\par}\vspace{.7em} {\color{articlerule}\hrule height 3pt}\par\endgroup\vspace{1.2em}} \renewenvironment{abstract}{\small\noindent{\sffamily\bfseries\color{articleblue}Abstract.}\par\noindent\hspace*{\parindent}\ignorespaces}{\par\vspace{1em}} \makeatother \captionsetup{font=small,labelfont=it,textfont=it,justification=centering,singlelinecheck=true} \captionsetup[algorithm]{labelfont=bf,textfont=normalfont,labelsep=space,justification=raggedright,singlelinecheck=false} \floatstyle{ruled}\restylefloat{algorithm} \algrenewcommand\algorithmicrequire{\textbf{Input:}} \algrenewcommand\algorithmicensure{\textbf{Output:}} \algrenewcommand\algorithmiccomment[1]{\hfill{\color{gray}\footnotesize // #1}} \algrenewcommand\alglinenumber[1]{\footnotesize #1:} \setlength{\textfloatsep}{14pt plus 3pt minus 4pt} \setlength{\floatsep}{12pt plus 3pt minus 3pt} \setlength{\intextsep}{12pt plus 3pt minus 3pt} \renewcommand{\topfraction}{.88} \renewcommand{\bottomfraction}{.75} \renewcommand{\textfraction}{.12} \renewcommand{\floatpagefraction}{.68} \setcounter{topnumber}{3} \setcounter{totalnumber}{4} \newtheorem{proposition}{Proposition}[section] \newtheorem{theorem}[proposition]{Theorem} \newtheorem{remark}[proposition]{Remark} \theoremstyle{definition} \newtheorem{definition}[proposition]{Definition} \newcommand{\R}{\mathbb{R}} \newcommand{\K}{\mathbb{K}} \newcommand{\T}{\mathcal{T}} \newcommand{\Sx}{\mathcal{S}} \newcommand{\Ttilde}{\tilde{\mathcal{T}}} \newcommand{\Stilde}{\tilde{\mathcal{S}}} \newcommand{\Shat}{\widehat{\mathcal{S}}} \newcommand{\Utilde}{\tilde{\mathbf{U}}} \newcommand{\W}{\mathbf{W}} \newcommand{\U}{\mathbf{U}} \newcommand{\V}{\mathbf{V}} \newcommand{\w}{\mathbf{w}} \newcommand{\x}{\mathbf{x}} \newcommand{\G}{\mathbf{G}} \newcommand{\nnz}{\operatorname{nnz}} \newcommand{\vecop}{\operatorname{vec}} \newcommand{\diag}{\operatorname{diag}} \newcommand{\supp}{\operatorname{supp}} \newcommand{\fl}{\lfloor} \newcommand{\fr}{\rfloor} \title{Efficient tensor compression and optimization for the canonical polyadic decomposition} \author{Felipe Bottega Diniz} \date{} \begin{document} \maketitle \begin{abstract} We present several computational improvements for the canonical polyadic decomposition (CPD) based on tensor compression and the damped Gauss-Newton (dGN) method. We study the choice of compression dimensions, the randomized singular value decomposition (SVD) and the multilinear multiplication of sparse tensors. An implicit Gram operator and external sorting allow us to compress sparse tensors without constructing their unfoldings or Gram matrices, including tensors stored on disk. In the dGN iterations we compute the residual in blocks and reuse two partial contractions to obtain the gradient. We also investigate improvements to the initialization and the tensor train approach. We derive the corresponding formulas, analyze their computational and memory costs, and assess their performance through numerical experiments. The proposed techniques are implemented in \emph{Tensor Fox 2.0.0}. \end{abstract} \noindent{\sffamily\bfseries\small\color{articleblue}Key words.}\ {\small canonical polyadic decomposition, tensor compression, sparse tensors, Gauss-Newton, randomized SVD, out-of-core computation.\par} \section{Introduction} The \emph{canonical polyadic decomposition} (CPD) represents a tensor as a sum of rank one tensors. In practice one is usually interested in a low-rank approximation rather than an exact decomposition. Unfortunately, there are several difficulties in computing such approximations. Besides the nonlinearity of the problem, a best rank-$R$ approximation need not exist \cite{desilva2008}, and the number of entries of a tensor increases exponentially with its order when all its dimensions are equal. The CPD algorithm developed in the thesis \cite{diniz2019} is based on tensor compression followed by \emph{damped Gauss-Newton} (dGN) iterations. Tensor Fox also incorporates an approach based on the \emph{tensor train decomposition} \cite{oseledets2011,zniyed2018} for higher order tensors. In this work we study how to reduce the cost of these operations, how to compress sparse tensors that cannot be handled by conventional matrix representations, and how to adapt the dGN iteration parameters to the size of the problem. The methods are implemented in version 2.0.0 of \emph{Tensor Fox} \cite{tensorfox}. We consider tensors of order $3\leq L\leq12$, the range addressed by the CPD routines. Figure~\ref{fig:pipeline} shows the main stages of the algorithm. The comparisons concern the in-memory algorithms of \cite{diniz2019}. We also extend the compression method to sparse tensors stored on disk. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth, node distance=7mm and 9mm, stage/.style={draw,fill=gray!13,rounded corners=1pt,align=center,minimum height=9mm,minimum width=30mm}, plain/.style={stage,fill=white},arr/.style={->,thick}] \node[plain] (t) {$\mathcal T$\\Original tensor}; \node[stage,right=of t] (c) {Truncated MLSVD\\$\tilde{\mathcal S},\tilde{\mathbf U}^{(\ell)}$}; \node[stage,right=of c] (i) {Initialization\\$\mathbf W^{(\ell)}$}; \node[stage,below=of i] (d) {dGN\\in compressed space}; \node[stage,below=of c] (tt) {TT-SVD and CPDs\\of third order cores}; \node[plain,below=of d] (u) {Uncompression\\$\tilde{\mathbf U}^{(\ell)}\mathbf W^{(\ell)}$}; \node[stage,right=of u] (e) {Relative error\\and CPD factors}; \draw[arr] (t)--(c);\draw[arr] (c.east) -- ++(3mm,0) |- (i.west);\draw[arr] (i)--(d); \draw[arr] (c)--node[right,font=\scriptsize]{TT path} (tt);\draw[arr] (d.south) -- (u.north);\draw[arr] (tt)|-(u);\draw[arr] (u)--(e); \end{tikzpicture} \caption{Main stages of the CPD algorithm, following \cite[Figure 4.1]{diniz2019}. The gray boxes indicate the operations revisited in this work. For sparse tensors compression is mandatory. The tensor train path uses the same compression stage followed by CPDs of third order cores.} \label{fig:pipeline} \end{figure} \section{Basic notions}\label{sec:basic} We adopt the notations of \cite[Section 1.1]{diniz2019}. Scalars are denoted by lower case letters (capital letters may be used for natural numbers), vectors by bold lower case letters, matrices by bold capital letters and tensors by calligraphic capital letters. Superscripts are used to enumerate elements of a sequence, while subscripts denote coordinates. The meaning of Greek capital letters will depend on the context. We will be working with tensors in the Euclidean tensor spaces $\R^{I_1}\otimes\cdots\otimes\R^{I_L}$, which we identify with $\R^{I_1\times\cdots\times I_L}$ by fixing the canonical bases. \begin{definition}\label{def:rank} A nonzero tensor of the form $\w^{(1)}\otimes\cdots\otimes\w^{(L)}$, with $\w^{(\ell)}\in\R^{I_\ell}$, has \emph{rank one}. The \emph{rank} of a nonzero tensor is the smallest number of rank one terms needed to represent it. \end{definition} \begin{definition}\label{def:cpd} Let $\T\in\R^{I_1\times\cdots\times I_L}$ be the tensor we want to approximate and let $$ \W^{(\ell)}=[\w^{(\ell)}_1,\ldots,\w^{(\ell)}_R]\in\R^{I_\ell\times R},\qquad \ell=1,\ldots,L. $$ The approximation of $\T$ given by these matrices is $$ \Ttilde=\sum_{r=1}^R\w^{(1)}_r\otimes\cdots\otimes\w^{(L)}_r. $$ We call the computation of an approximation of this form a \emph{rank-$R$ CPD}. The matrix $\W^{(\ell)}$ is called the \emph{factor matrix} associated with mode $\ell$ and its $r$-th column, $\w^{(\ell)}_r$, is called a \emph{factor}. \end{definition} We denote the entries of $\T$ and $\Ttilde$ by $t_{i_1\ldots i_L}$ and $\tilde{t}_{i_1\ldots i_L}$, respectively. In this work $R$ is given in advance and need not coincide with the rank of $\T$. We assume $\T\ne 0$ and denote by $I_n$ the identity matrix of order $n$. \begin{definition}\label{def:relative-error} We use the \emph{Euclidean norm} in all tensor spaces, given in coordinates by $$ \|\T\|^2=\sum_{i_1=1}^{I_1}\cdots\sum_{i_L=1}^{I_L}t_{i_1\ldots i_L}^2. $$ The \emph{relative error} of the approximation $\Ttilde$ of a nonzero tensor $\T$ is $$ \frac{\|\T-\Ttilde\|}{\|\T\|}. $$ \end{definition} We want to find factors such that the relative error is as small as possible. \begin{definition}\label{def:vec} Given a matrix $\mathbf{A}$, denote by $\vecop(\mathbf{A})$ the vector obtained by vertically stacking its columns. More generally, $\vecop(\T)$ denotes the vector of the coordinates of $\T$, ordered so that the first index varies fastest, followed by the second, and so on. \end{definition} Define $\w$ as the vector obtained by concatenating $\vecop(\W^{(1)}),\ldots,\vecop(\W^{(L)})$. Thus $\w\in\R^{R\sum_{\ell=1}^L I_\ell}$ and the coordinates of $\Ttilde$ depend on $\w$. Define $$ f(\w)=\vecop(\T-\Ttilde),\qquad F(\w)=\frac12\|f(\w)\|^2. $$ Computing a rank-$R$ CPD for $\T$ is therefore a \emph{nonlinear least squares problem}, namely the minimization of $F$. \begin{definition}\label{def:fiber} Let $1\leq\ell\leq L$ and fix all indexes of $\T$ except $i_\ell$. The resulting vector $\T_{i_1\ldots i_{\ell-1}:i_{\ell+1}\ldots i_L}$ in $\R^{I_\ell}$ is called a \emph{mode-$\ell$ fiber} of $\T$. The symbol $:$ indicates that the index $i_\ell$ varies from $1$ to $I_\ell$, while all other indexes are fixed. \end{definition} In the case of matrices, the mode-1 fibers are the columns and the mode-2 fibers are the rows. Figure~\ref{fig:tensor-fibers} illustrates the case of a third order tensor. \begin{figure}[htbp] \centering \includegraphics[width=\textwidth]{figures/tensor_fibers.png} \caption{Fibers of a third order tensor (from \cite[Figure 1.2]{diniz2019}).} \label{fig:tensor-fibers} \end{figure} \begin{definition}\label{def:slice} A \emph{slice} is a subtensor obtained by fixing all but two indexes. In the case of a third order tensor, $\T_{i::}$, $\T_{:j:}$ and $\T_{::k}$ are called the \emph{horizontal}, \emph{lateral} and \emph{frontal slices}, respectively (see Figure~\ref{fig:tensor-slices}). \end{definition} \begin{definition}\label{def:hyperslice} Given a tensor $\T\in\R^{I_1\times\cdots\times I_L}$, let $$ \T_{i_\ell=k}\in\R^{I_1\times\cdots\times I_{\ell-1}\times I_{\ell+1}\times\cdots\times I_L} $$ be the subtensor obtained by fixing the $\ell$-th index of $\T$ with value equal to $k$ and varying all the other indexes, where $1\leq k\leq I_\ell$. More precisely, $\T_{i_\ell=k}=\T_{:\ldots:k:\ldots:}$, where the value $k$ is at the $\ell$-th index. We call these subtensors \emph{hyperslices}. \end{definition} \begin{figure}[htbp] \centering \includegraphics[width=\textwidth]{figures/tensor_slices.png} \caption{Slices of a third order tensor (from \cite[Figure 1.3]{diniz2019}).} \label{fig:tensor-slices} \end{figure} \begin{definition}\label{def:unfolding} By arranging all mode-$\ell$ fibers side by side as columns of a matrix, we obtain the \emph{mode-$\ell$ unfolding} of $\T$, denoted by $\T_{(\ell)}$. This matrix has shape $I_\ell\times\prod_{j\ne\ell}I_j$. We order the fibers as in \cite[Section 2.1.1]{diniz2019}, varying the first of the remaining indexes fastest, the second next, and so on. \end{definition} For example, the mode-2 unfolding of a third order tensor is given by $$ \T_{(2)}=[\T_{1:1},\ldots,\T_{I_1:1},\T_{1:2},\ldots,\T_{I_1:2},\ldots,\T_{I_1:I_3}]. $$ \begin{definition}\label{def:multilinear-rank} The \emph{mode-$\ell$ rank} of $\T$ is the matrix rank of $\T_{(\ell)}$, denoted by $R_\ell=\operatorname{rank}(\T_{(\ell)})$. The $L$-tuple $(R_1,\ldots,R_L)$ is called the \emph{multilinear rank} of $\T$. \end{definition} These ranks are the dimensions of the reduced core tensor and should not be confused with the rank $R$ prescribed for the CPD. \begin{definition}\label{def:multilinear-multiplication} For matrices $\mathbf{M}^{(\ell)}\in\R^{J_\ell\times I_\ell}$, $\ell=1,\ldots,L$, \emph{multilinear multiplication} is defined by $$ \begin{aligned} \Sx&=(\mathbf{M}^{(1)},\ldots,\mathbf{M}^{(L)})\cdot\T\in\R^{J_1\times\cdots\times J_L},\\ s_{j_1\ldots j_L}&=\sum_{i_1=1}^{I_1}\cdots\sum_{i_L=1}^{I_L} t_{i_1\ldots i_L}\prod_{\ell=1}^L m^{(\ell)}_{j_\ell i_\ell}. \end{aligned} $$ \end{definition} \begin{definition}\label{def:contraction} Given two tensors $\T\in\R^{I_1\times\cdots\times I_L}$ and $\mathcal{U}\in\R^{J_1\times\cdots\times J_M}$ such that $I_\ell=J_m$, where $1\leq\ell\leq L$ and $1\leq m\leq M$, we define the \emph{contraction} between $\T$ and $\mathcal{U}$ along modes $\ell$ and $m$ as the tensor $$ \T\times_\ell^m\mathcal{U}\in\R^{I_1\times\cdots\times I_{\ell-1}\times I_{\ell+1}\times\cdots\times I_L\times J_1\times\cdots\times J_{m-1}\times J_{m+1}\times\cdots\times J_M}, $$ whose coordinates are given by $$(\T\times_\ell^m\mathcal{U})_{i_1\ldots i_{\ell-1}i_{\ell+1}\ldots i_L j_1\ldots j_{m-1}j_{m+1}\ldots j_M} =\sum_{k=1}^{I_\ell} t_{i_1\ldots i_{\ell-1}ki_{\ell+1}\ldots i_L} u_{j_1\ldots j_{m-1}kj_{m+1}\ldots j_M}. $$ The resulting tensor has order $L+M-2$. If $L=M=1$, the contraction is a scalar in $\R$. \end{definition} This operation generalizes the usual matrix multiplication, since $\mathbf{A}\times_2^1\mathbf{B}=\mathbf{A}\mathbf{B}$ for matrices of compatible dimensions. Multilinear multiplication can also be interpreted as a sequence of contractions between a tensor and matrices, with the indexes permuted to preserve the order of the modes. For more details on tensor contractions, see \cite[Definition B.1.3 and Section 4.6.2]{diniz2019}. \begin{definition}\label{def:diagonal-tensor} Let $\mathcal{I}_{R\times\cdots\times R}$ be the diagonal tensor of order $L$ whose diagonal entries are all equal to one, that is, its entries are one if $i_1=\cdots=i_L$ and zero otherwise. \end{definition} With this notation a sum of $R$ rank one tensors can be written as $$ \Ttilde=(\W^{(1)},\ldots,\W^{(L)})\cdot\mathcal{I}_{R\times\cdots\times R}. $$ The following theorem generalizes the \emph{singular value decomposition} (SVD) to tensors. As in \cite{diniz2019}, $\K$ denotes either $\R$ or $\mathbb{C}$. The notation for hyperslices, unfoldings and multilinear multiplication extends to $\K$ without change. Over $\mathbb{C}$, orthogonality and norms are understood with respect to the Hermitian inner product. \begin{theorem}[{L. De Lathauwer, B. De Moor, J. Vandewalle, 2000, \cite{delathauwer2000}, \cite[Theorem 2.2.5]{diniz2019}}]\label{thm:mlsvd} Let $\T\in\K^{I_1\times\cdots\times I_L}$ be arbitrary. Then there exist unitary (orthogonal) matrices $\U^{(1)}\in\K^{I_1\times I_1},\ldots,\U^{(L)}\in\K^{I_L\times I_L}$ and a tensor $\Sx\in\K^{I_1\times\cdots\times I_L}$ such that \begin{enumerate} \item $\T=(\U^{(1)},\ldots,\U^{(L)})\cdot\Sx$. \item For all $\ell=1\ldots L$, the subtensors $\Sx_{i_\ell=1},\ldots,\Sx_{i_\ell=I_\ell}$ are orthogonal with respect to each other. \item For all $\ell=1\ldots L$, $\|\Sx_{i_\ell=1}\|\geq\ldots\geq\|\Sx_{i_\ell=I_\ell}\|$. \end{enumerate} \end{theorem} \begin{definition}\label{def:mlsvd} A decomposition satisfying the three properties of Theorem~\ref{thm:mlsvd} is called the \emph{multilinear singular value decomposition} (MLSVD) of $\T$, and $\Sx$ is called the \emph{core tensor}. The second property of that theorem is called \emph{all-orthogonality}. \end{definition} In the real case considered in this work, the matrices $\U^{(\ell)}$ are obtained from the left singular vectors of the unfoldings $\T_{(\ell)}$, ordered according to decreasing singular values. The core tensor is given by $$ \Sx=((\U^{(1)})^T,\ldots,(\U^{(L)})^T)\cdot\T. $$ \begin{definition}\label{def:mode-singular-value} Denote by $\sigma_k^{(\ell)}$ the $k$-th singular value of mode $\ell$, that is, the $k$-th singular value of $\T_{(\ell)}$ (including zeros when necessary). \end{definition} \begin{theorem}[{L. De Lathauwer, B. De Moor, J. Vandewalle, 2000, \cite{delathauwer2000}, \cite[Theorem 2.2.7]{diniz2019}}]\label{thm:mlsvd-hyperslices} Let $\Sx_{(\ell)}$ be an unfolding of $\Sx$. If $(\Sx_{(\ell)})_{k:}$ is the $k$-th row of $\Sx_{(\ell)}$, then $\|(\Sx_{(\ell)})_{k:}\|=\sigma_k^{(\ell)}$. \end{theorem} Since the $k$-th row of $\Sx_{(\ell)}$ consists of the coordinates of the hyperslice $\Sx_{i_\ell=k}$, Theorem~\ref{thm:mlsvd-hyperslices} implies that $\|\Sx_{i_\ell=k}\|=\sigma_k^{(\ell)}$. In particular, the last $I_\ell-R_\ell$ hyperslices of mode $\ell$ are null, since $\sigma_k^{(\ell)}=0$ for $k>R_\ell$. Hence $s_{i_1\ldots i_L}=0$ whenever $i_\ell>R_\ell$ for at least one $\ell$. Figure~\ref{fig:mlsvd-reduced} illustrates this property for a third order tensor. \begin{figure}[htbp] \centering \includegraphics[width=0.46\textwidth]{figures/mlsvd_reduced.png} \caption{Representation of the null slices of a third order core tensor $\Sx$ obtained after an MLSVD. The gray part has dimensions $R_1\times R_2\times R_3$.} \label{fig:mlsvd-reduced} \end{figure} Instead of considering the core tensor in $\R^{I_1\times\cdots\times I_L}$, we can discard its null hyperslices and consider $\Sx\in\R^{R_1\times\cdots\times R_L}$. We also discard the last $I_\ell-R_\ell$ columns of each $\U^{(\ell)}$, so that $\U^{(\ell)}\in\R^{I_\ell\times R_\ell}$. The equality $$ \T=(\U^{(1)},\ldots,\U^{(L)})\cdot\Sx $$ remains intact, and we still have $\Sx=((\U^{(1)})^T,\ldots,(\U^{(L)})^T)\cdot\T$ and $\|\Sx\|=\|\T\|$. \begin{definition}\label{def:reduced-mlsvd} The decomposition obtained by discarding the null hyperslices of the core tensor and the corresponding columns of the matrices $\U^{(\ell)}$ is called the \emph{reduced MLSVD}, in contrast to the full MLSVD of Theorem~\ref{thm:mlsvd}. \end{definition} The importance of this decomposition for the computation of the CPD is made clear by the following remark. \begin{remark}\label{rem:cpd-mlsvd} After computing the MLSVD of $\T$, notice that computing a CPD for $\Sx$ is equivalent to computing a CPD for $\T$. Indeed, if $\Sx=\sum_{r=1}^R\w^{(1)}_r\otimes\cdots\otimes\w^{(L)}_r$, with $\W^{(\ell)}\in\R^{R_\ell\times R}$, then we can write $\Sx=(\W^{(1)},\ldots,\W^{(L)})\cdot\mathcal{I}_{R\times\cdots\times R}$, which implies that $$ \begin{aligned} \T&=(\U^{(1)},\ldots,\U^{(L)})\cdot\left((\W^{(1)},\ldots,\W^{(L)})\cdot\mathcal{I}_{R\times\cdots\times R}\right)=\\ &=(\U^{(1)}\W^{(1)},\ldots,\U^{(L)}\W^{(L)})\cdot\mathcal{I}_{R\times\cdots\times R}. \end{aligned} $$ \end{remark} Conversely, applying multilinear multiplication with the transposed matrices $\U^{(\ell)}$ to a CPD of $\T$ gives a CPD of $\Sx$ with no more terms. In particular, the exact reduction preserves the rank. \section{Tensor compression}\label{sec:compression} We retain the compression strategy of \cite[Section 4.1.1]{diniz2019}, modifying the choice of dimensions, the singular subspace computations and the multilinear multiplication. We also introduce compression without explicit unfoldings or Gram matrices. \subsection{The truncated MLSVD} The multilinear ranks $R_1,\ldots,R_L$ are usually unknown. We therefore seek dimensions $\tilde{R}_\ell$ that retain the relevant singular directions without making the compressed problem unnecessarily large \cite[Section 4.1.1]{diniz2019}. \begin{definition}\label{def:truncated-mlsvd} In the case $1\leq\tilde{R}_\ell\leq R_\ell$, let $\U^{(\ell)}_{:j}$ denote the $j$-th column of $\U^{(\ell)}$ and define $$ \Utilde^{(\ell)}=[\U^{(\ell)}_{:1},\ldots,\U^{(\ell)}_{:\tilde{R}_\ell}]\in\R^{I_\ell\times\tilde{R}_\ell},\qquad \Stilde\in\R^{\tilde{R}_1\times\cdots\times\tilde{R}_L}, $$ where $\tilde{s}_{i_1\ldots i_L}=s_{i_1\ldots i_L}$ for $1\leq i_\ell\leq\tilde{R}_\ell$, $\ell=1,\ldots,L$. This is the \emph{truncated MLSVD}. \end{definition} This decomposition is illustrated in Figure~\ref{fig:mlsvd-truncated}. Unlike the reduction described in the preceding section, it can discard nonzero hyperslices and therefore introduce an approximation error. \begin{figure}[htbp] \centering \includegraphics[width=0.52\textwidth]{figures/mlsvd_truncated.png} \caption{Truncated tensor $\Stilde$ (in red) inside the reduced core tensor $\Sx$ (in gray) of a third order tensor (from \cite[Figure 4.2]{diniz2019}).} \label{fig:mlsvd-truncated} \end{figure} In computation there is no need to construct $\Sx$ first. We can compute only the leading singular vectors of each unfolding and form $$ \Stilde=((\Utilde^{(1)})^T,\ldots,(\Utilde^{(L)})^T)\cdot\T, \qquad \T\approx(\Utilde^{(1)},\ldots,\Utilde^{(L)})\cdot\Stilde. $$ The retained dimensions may exceed the exact ranks $R_\ell$. When the singular subspaces are approximate, $\Stilde$ denotes the core obtained by the multilinear multiplication above. In the sequentially truncated MLSVD, each unfolding is formed from a partially compressed tensor \cite{vannieuwenhoven2012}. We then compute a rank-$R$ CPD for $\Stilde$, denoting the resulting tensor by $\Shat$ and its entries by $\hat{s}_{i_1\ldots i_L}$, to distinguish this approximation from the truncated core itself. If $\W^{(\ell)}\in\R^{\tilde{R}_\ell\times R}$ are its factor matrices, Remark~\ref{rem:cpd-mlsvd} gives the corresponding factors in the original space by $$ \tilde{\W}^{(\ell)}=\Utilde^{(\ell)}\W^{(\ell)},\qquad \Ttilde=(\Utilde^{(1)},\ldots,\Utilde^{(L)})\cdot\Shat. $$ In general, the error of $\Ttilde$ has contributions from both the truncation of the MLSVD and the approximation of $\Stilde$ by its CPD (see Section~\ref{sec:error}). \begin{definition}\label{def:refinement} For dense tensors, the uncompressed factors can also serve as a starting point for further nonlinear iterations in the original tensor space, a procedure we refer to as \emph{refinement}. \end{definition} Throughout this work $I_\ell$ denotes an original dimension, $R_\ell$ a dimension of the reduced core $\Sx$, and $\tilde{R}_\ell$ a dimension of the truncated core $\Stilde$. We describe the nonlinear iterations in the compressed space. The same formulas apply to refinement in the original space, with $\T$, $\Ttilde$, $\tilde{\W}^{(\ell)}$ and $I_\ell$ in place of $\Stilde$, $\Shat$, $\W^{(\ell)}$ and $\tilde{R}_\ell$, respectively. \subsection{The number of singular components} Let $P_\ell$ be the number of singular components requested from the randomized SVD. The thesis \cite[Section 4.1.1]{diniz2019} proposes $P_\ell=\min\{R,I_\ell\}$. The associated implementation uses this prescription for dense tensors by default, whereas for sparse tensors on the CPU it requests $\lfloor\max\{L,\log_{10}(LI_\ell^2)\}\min\{R,I_\ell\}\rfloor$ components (the number returned is limited by the dimensions of the decomposition). In Tensor Fox 2.0.0 we use the same heuristic for dense and sparse tensors: \begin{equation}\label{eq:svd-components} P_\ell=\min\left\{I_\ell,\max\left\{1,\left\lfloor \frac{\max\{L,\log_{10}(LI_\ell^2)\}\min\{R,I_\ell\}}{2} \right\rfloor\right\}\right\}. \end{equation} For dense tensors, the choice $P_\ell=\min\{R,I_\ell\}$ can discard significant singular directions when the prescribed CPD rank is smaller than the multilinear ranks. The new prescription can request more components in that case, subject to the memory-dependent reduction below. For sparse CPU compression it halves the estimate used in the previous implementation before clipping it to the mode dimension, reducing the potential size of the core. Neither prescription guarantees that all significant directions are retained. To understand this heuristic, rewrite the multiplicative factor as $$ \frac{\max\{L,\log_{10}(LI_\ell^2)\}}{2}=\max\left\{\frac{L}{2},\log_{10}(I_\ell\sqrt{L})\right\}. $$ The multiplicative factor grows linearly with $L$ or logarithmically with $I_\ell$, whichever is greater. Higher order allows more modes along which information may be lost, while logarithmic growth in $I_\ell$ limits the size of the core. Figure~\ref{fig:heuristics} illustrates the heuristic. \begin{figure}[htbp] \centering \includegraphics[width=0.95\textwidth]{figures/heuristics.png} \caption{Heuristic value of $P_\ell$ for tensors of orders $L=3,6,9,12$. The horizontal axis represents the prescribed CPD rank $R$, the vertical axis represents the dimension $I_\ell$ of mode $\ell$, and the color scale indicates the value of $P_\ell$.} \label{fig:heuristics} \end{figure} The number of components returned may be smaller than $P_\ell$, depending on the dimensions of the matrix. Alternatively, the number of components can be prescribed directly. \subsection{A memory-dependent truncation} The new prescription can produce a core too large for RAM, even if all compression matrices fit individually. In double precision the estimated size of the core is $$ 8\prod_{\ell=1}^L P_\ell\ \text{bytes} =\frac{8\prod_{\ell=1}^LP_\ell}{2^{30}}\ \text{GiB}. $$ The program obtains the available RAM from the operating system and uses, by default, half of this quantity as the maximum estimated size of the dense core. If the original estimate exceeds this limit, the values $P_\ell$ are replaced by $$ P_\ell^{(j)}=\max\left\{\min\{R,I_\ell\}, \left\lfloor\frac{P_\ell}{2^j}\right\rfloor\right\},\qquad j=1,2,\ldots, $$ until the estimated size of the core does not exceed this limit. If even the dimensions $\min\{R,I_\ell\}$ give a core exceeding this limit, a second reduction is performed according to $$ P_\ell^{(j)}=\max\left\{2, \left\lfloor\frac{\min\{R,I_\ell\}}{2^j}\right\rfloor\right\},\qquad j=1,2,\ldots. $$ The second reduction may compromise accuracy. We assume $I_\ell\geq2$ for all modes. If even the smallest core allowed by this rule does not fit within the budget, compression is not attempted. The implicit spectral computation also limits the number of components by the number of active rows. The estimate concerns only the core (Figure~\ref{fig:memory-budget}). Prescribing the number of components directly bypasses this reduction. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,x=1cm,y=1cm,>=Stealth] \draw[thick] (0,0) rectangle (13,1.05); \fill[gray!48] (0,0) rectangle (5.2,1.05); \node[align=center] at (2.6,.52){Core estimate\\$8\prod_\ell P_\ell$ bytes}; \draw[dashed,thick] (6.5,-.28)--(6.5,1.30); \node[above] at (6.5,1.31){Half of available RAM}; \node[align=center] at (9.75,.52){Not budgeted by\\the core heuristic}; \node[below left] at (13,0){Available RAM}; \draw[->] (9.7,-.45)--(9.7,-.85); \foreach \x/\w/\t in {0/3.15/{Compression matrices},3.3/3.15/{Sampled subspaces},6.6/3.15/{LU/QR workspaces},9.9/3.1/{Other live arrays}}{\draw[fill=gray!13] (\x,-1.8) rectangle ++(\w,.75);\node[font=\scriptsize,align=center] at ({\x+\w/2},-1.425){\t};} \end{tikzpicture} \caption{The rank heuristic estimates the size of the core (dark gray), not the peak RAM of the full computation. The other arrays shown may coexist during different stages. The widths are schematic and do not represent measured memory consumption.} \label{fig:memory-budget} \end{figure} \subsection{Normalization in the randomized SVD} In the implementation associated with the thesis, the power iterations of the randomized SVD \cite{halko2011} were performed without intermediate normalization. We now introduce LU factorizations between successive matrix multiplications to reduce the loss of numerical accuracy during the power iterations. The first step of this algorithm consists in obtaining an orthonormal basis that approximates the dominant left singular subspace of a matrix $\mathbf{A}$. Given a Gaussian random matrix $\boldsymbol\Omega$, we compute $$ \mathbf{Y}=(\mathbf{A}\mathbf{A}^T)^q\mathbf{A}\boldsymbol\Omega, $$ where $q$ is the number of power iterations. To understand the purpose of these iterations, consider a reduced SVD of $\mathbf{A}$ given by $\mathbf{A}=\U\boldsymbol\Sigma\V^T$. It follows that $$ \mathbf{Y}=\U\boldsymbol\Sigma^{2q+1}\V^T\boldsymbol\Omega. $$ The matrix $(\mathbf{A}\mathbf{A}^T)^q\mathbf{A}$ has the same singular vectors as $\mathbf A$, while the ratio between any two nonzero singular values changes from $$ \frac{\sigma_i}{\sigma_j}\qquad\mbox{to}\qquad \left(\frac{\sigma_i}{\sigma_j}\right)^{2q+1}. $$ This makes it easier to identify the dominant singular subspace, but it may also cause numerical difficulties. For instance, if $\sigma_j/\sigma_1=10^{-4}$ and $q=2$, this ratio becomes $10^{-20}$, which is below the relative precision of double precision arithmetic. Hence, information about some singular directions may be lost and the columns of $\mathbf{Y}$ may become numerically linearly dependent. A QR factorization was performed only after all the power iterations. To reduce this loss of accuracy, we change the basis at each intermediate step. Since we are only interested in preserving the subspace spanned by the columns, it is not necessary to compute an orthonormal basis at each step. Instead, we make use of LU factorizations, which are computationally cheaper. Consider a matrix $\mathbf{Y}$ with linearly independent columns and its LU factorization with row permutations, $$ \mathbf{Y}=\mathbf{P}\mathbf{L}\mathbf{U}_0. $$ Since $\mathbf{U}_0$ is nonsingular, we have that $$ \mathbf{P}\mathbf{L}=\mathbf{Y}\mathbf{U}_0^{-1} \qquad\Longrightarrow\qquad \operatorname{Im}(\mathbf{Y})=\operatorname{Im}(\mathbf{P}\mathbf{L}). $$ Therefore, we can substitute $\mathbf{Y}$ by $\mathbf{P}\mathbf{L}$ without changing the sampled subspace in exact arithmetic. Algorithm~\ref{alg:range} summarizes the new range finder. The CPU randomized SVD used for the MLSVD requests ten oversampling vectors and uses two power iterations. For a wide matrix the range finder is applied to its transpose, and the resulting SVD is transposed back. The intermediate LU factors reduce the loss of numerical independence, and the final QR gives an orthonormal basis. \begin{algorithm}[htbp] \small \caption{Randomized range finder with LU normalization}\label{alg:range} \begin{algorithmic}[1] \Require $\mathbf A\in\R^{m\times n}$, sampling width $b$, number of power iterations $q$ \Ensure Orthonormal basis $\mathbf Q$ for the sampled range \State $\mathbf Q\gets\operatorname{Gaussian}(n,b)$ \For{$j=1,\ldots,q$} \State $(\mathbf P,\mathbf L,\mathbf U_0)\gets\operatorname{LU}(\mathbf A\mathbf Q)$ \State $\mathbf Q\gets\mathbf P\mathbf L$ \Comment{Discard the triangular factor} \State $(\mathbf P,\mathbf L,\mathbf U_0)\gets\operatorname{LU}(\mathbf A^T\mathbf Q)$ \State $\mathbf Q\gets\mathbf P\mathbf L$ \EndFor \State $\mathbf Y\gets\mathbf A\mathbf Q$ \State $\mathbf Q\gets\Call{QRBasis}{\mathbf Y}$ \Comment{Householder reflectors, no separate triangular output} \State \Return $\mathbf Q$ \end{algorithmic} \end{algorithm} \subsection{Computing only the orthogonal factor of the QR factorization} At the end of the randomized range finder, the triangular factor of the QR factorization is not needed. In the implementation associated with the thesis we compute an economic QR factorization $\mathbf{Y}=\mathbf{Q}\mathbf{R}$ and discard $\mathbf{R}$. In Tensor Fox 2.0.0 we use the Householder representation directly. Let $\mathbf{Y}\in\R^{m\times b}$ and $h=\min\{m,b\}$. The factorization can be represented by $h$ elementary reflectors \[ \mathbf{H}^{(j)}=I_m-\tau_j\mathbf{v}^{(j)}(\mathbf{v}^{(j)})^T, \qquad j=1,\ldots,h. \] Here the reflectors are represented by their nontrivial entries and the scalars $\tau_j$. The economic orthogonal factor is \[ \mathbf{Q}=\mathbf{H}^{(1)}\cdots\mathbf{H}^{(h)}[\mathbf e_1,\ldots,\mathbf e_h]. \] The Householder vectors and triangular entries can occupy the same storage as $\mathbf Y$. We form $\mathbf Q$ from the reflectors without constructing a separate triangular matrix, as illustrated in Figure~\ref{fig:qr-workspace}. The arithmetic cost remains $\mathcal O(mbh+mh^2)$, but the separate triangular factor of size $h\times b$ is no longer needed. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=5.2mm,y=5.2mm] \foreach \i in {0,...,5}{\foreach \j in {0,...,2}{\fill[gray!12] (\j,-\i) rectangle ++(1,-1);\draw[gray!70] (\j,-\i) rectangle ++(1,-1);}} \node[above] at (1.5,0){$\mathbf Y$}; \node[below] at (1.5,-6){$m\times b$}; \draw[->,thick] (3.2,-3)--node[above]{\scriptsize QR} (5.9,-3); \foreach \i in {0,...,5}{\foreach \j in {0,...,2}{\ifnum\i>\j\fill[gray!18] (\j+6,-\i) rectangle ++(1,-1);\else\fill[gray!55] (\j+6,-\i) rectangle ++(1,-1);\fi\draw[gray!75] (\j+6,-\i) rectangle ++(1,-1);}} \node[above] at (7.5,0){Reflectors and $\mathbf R$}; \node[below] at (7.5,-6){plus $\tau_1,\ldots,\tau_b$}; \draw[->,thick] (9.2,-3)--node[above]{\scriptsize Form $\mathbf Q$} (12,-3); \foreach \i in {0,...,5}{\foreach \j in {0,...,2}{\fill[gray!20] (\j+12.2,-\i) rectangle ++(1,-1);\draw[gray!70] (\j+12.2,-\i) rectangle ++(1,-1);}} \node[above] at (13.7,0){$\mathbf Q$}; \node[below] at (13.7,-6){$m\times b$}; \draw[fill=gray!55] (0,-8) rectangle ++(.7,-.7);\node[right] at (.8,-8.35){Triangular entries}; \draw[fill=gray!18] (7,-8) rectangle ++(.7,-.7);\node[right] at (7.8,-8.35){Reflector tails}; \end{tikzpicture} \caption{Householder representation of an economic QR factorization for $m>b$. The upper triangular part and the tails of the reflector vectors occupy the same array. In the range finder only the orthogonal factor is needed, so a separate triangular array is not formed.} \label{fig:qr-workspace} \end{figure} \subsection{Energy-based truncation} The energy-based truncation criterion of \cite[Section 4.1.1, Algorithm 4.1.1]{diniz2019} is retained when a singular spectrum is computed. We recall this criterion in view of the new choice of $P_\ell$. Let $\sigma^{(\ell)}_1,\ldots,\sigma^{(\ell)}_{P_\ell}$ be the computed singular values and $\varepsilon$ a prescribed tolerance. This truncation seeks the first $\tilde{R}_\ell$ such that $$ \frac{\sum_{j=\tilde{R}_\ell+1}^{P_\ell}(\sigma_j^{(\ell)})^2} {\sum_{j=1}^{P_\ell}(\sigma_j^{(\ell)})^2} <\frac{\varepsilon}{L}. $$ If there is no such index before $P_\ell$, all computed components are retained. To understand this criterion for the classical MLSVD, suppose that all nonzero singular values of every $\T_{(\ell)}$ have been computed exactly. Denote by $\tilde{\T}_{(\ell)}$ the truncated SVD of $\T_{(\ell)}$ with $\tilde{R}_\ell$ singular components. Then we have that $$ \frac{\|\T_{(\ell)}-\tilde{\T}_{(\ell)}\|^2} {\|\T_{(\ell)}\|^2} = \frac{\sum_{j=\tilde{R}_\ell+1}^{P_\ell}(\sigma_j^{(\ell)})^2} {\sum_{j=1}^{P_\ell}(\sigma_j^{(\ell)})^2}. $$ Therefore, we can evaluate the squared relative error of each truncation without actually constructing the truncated unfoldings. Furthermore, since $\|\T_{(\ell)}\|=\|\T\|$ for each $\ell$, the relative compression error satisfies $$ \begin{aligned} \frac{\|\T-(\Utilde^{(1)},\ldots,\Utilde^{(L)}) \cdot\Stilde\|^2}{\|\T\|^2} \leq \sum_{\ell=1}^{L} \frac{\|\T_{(\ell)}-\tilde{\T}_{(\ell)}\|^2} {\|\T_{(\ell)}\|^2} < L\frac{\varepsilon}{L}=\varepsilon. \end{aligned} $$ This explains the distribution of $\varepsilon$ among the $L$ modes. The strict bound assumes $\varepsilon>0$. More generally, when only part of the singular spectrum is computed, the criterion measures the fraction of the sum of squares of the computed singular values discarded in each mode. Hence, the choice of $P_\ell$ determines the number of singular components available for compression, whereas $\varepsilon$ controls the fraction that can be discarded. The criterion does not bound the energy outside the computed subspaces. In sequential compression it is applied to the unfoldings of the partially compressed tensor. Thus the prescribed tolerance is not, in general, a guarantee on the final compression error. \subsection{Folding back an unfolding} In the approach associated with the thesis, recovering a tensor from an unfolding required copying its entries. We can instead reshape the unfolding and permute its axes, following the indexing convention in \cite[Section 2.1.1]{diniz2019}. Consider zero-based indexes temporarily and let $j_1,\ldots,j_{L-1}$ be the modes other than $\ell$, in increasing order. The column index of the mode-$\ell$ unfolding is given by $$ c=i_{j_1}+I_{j_1}i_{j_2}+\cdots+ \left(\prod_{a=1}^{L-2}I_{j_a}\right)i_{j_{L-1}}. $$ In the column-major representation, the linear position of the entry in the unfolding is $i_\ell+I_\ell c$. Consequently a column-major reshape to dimensions $$ (I_\ell,I_1,\ldots,I_{\ell-1},I_{\ell+1},\ldots,I_L) $$ followed by moving the first axis to position $\ell$ recovers the tensor (Figure~\ref{fig:foldback-layout}). With a suitable memory layout, these operations only reinterpret the stored entries, without copying them. This avoids $\mathcal O(\prod_\ell I_\ell)$ operations and storage for $\T$, or $\mathcal O(\prod_\ell \tilde R_\ell)$ for $\Stilde$. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth] \node[align=center] (a) {$\displaystyle\begin{bmatrix}1&4&7&10\\2&5&8&11\\3&6&9&12\end{bmatrix}$\\[3pt] $\mathcal T_{(2)}$}; \node[align=center,right=14mm of a] (b) {$\displaystyle\left\{\begin{bmatrix}1&4\\2&5\\3&6\end{bmatrix},\begin{bmatrix}7&10\\8&11\\9&12\end{bmatrix}\right\}$\\[3pt] Shape $(3,2,2)$}; \node[align=center,below=12mm of b] (c) {$\displaystyle\left\{\begin{bmatrix}1&2&3\\4&5&6\end{bmatrix},\begin{bmatrix}7&8&9\\10&11&12\end{bmatrix}\right\}$\\[3pt] Shape $(2,3,2)$}; \draw[->,thick] (a)--node[above,font=\scriptsize]{reshape} (b);\draw[->,thick] (b)--node[right,font=\scriptsize]{move axis} (c); \node[draw,align=center,fill=gray!12,left=14mm of c] (d){Same memory buffer\\$1,2,3,\ldots,12$}; \draw[dashed] (a.south)--(d.north);\draw[dashed] (d.east)--(c.west); \end{tikzpicture} \caption{Example of folding back a mode-2 unfolding of a tensor with shape $2\times3\times2$. The reshape interprets consecutive entries of each fiber as the first axis and the axis permutation restores the original mode ordering. The underlying buffer can remain unchanged.} \label{fig:foldback-layout} \end{figure} \subsection{Sparse multilinear multiplication by grouping fibers}\label{sec:group} The in-memory implementation associated with the thesis accumulates the contributions of the nonzero entries directly. We replace this operation with a sequence of contractions that reuse common fibers. \begin{definition}\label{def:support} Let $\Omega=\supp(\T)$ be the \emph{support} of a sparse tensor, that is, the set of indexes of its nonzero entries, and denote its cardinality by $\nnz(\T)$. \end{definition} We assume that the tensor is stored in coordinate format with distinct coordinates. Let $\tilde{u}^{(\ell)}_{ij}$ denote the entries of $\Utilde^{(\ell)}$. The definition of multilinear multiplication gives $$ \tilde{s}_{j_1\ldots j_L}=\sum_{(i_1,\ldots,i_L)\in\Omega} t_{i_1\ldots i_L}\prod_{\ell=1}^L \tilde{u}^{(\ell)}_{i_\ell j_\ell}. $$ Direct accumulation requires work proportional to $\nnz(\T)\prod_{\ell=1}^L\tilde R_\ell$ and may store the selected compression coefficients for every nonzero. Grouping common indexes allows us to reuse partial contractions. For simplicity let $L=3$. The idea is to first group the nonzero entries belonging to the same mode-3 fiber of $\T$. For every pair $(i_1,i_2)$ appearing in the sparse coordinates, we contract the fiber $\T_{i_1i_2:}$ with $(\Utilde^{(3)})^T$, obtaining the vector with entries $$ a_{i_1 i_2 j_3}=\sum_{i_3:(i_1,i_2,i_3)\in\Omega} t_{i_1i_2i_3}\tilde{u}^{(3)}_{i_3j_3}. $$ We then group these vectors by $i_1$ and compute $$ b_{i_1j_2j_3}=\sum_{i_2:(i_1,i_2)\in\pi_{1,2}(\Omega)} \tilde{u}^{(2)}_{i_2j_2}a_{i_1i_2j_3}, $$ where $\pi_{1,2}$ and $\pi_1$ denote the projections onto the first two coordinates and onto the first coordinate, respectively. Finally, $$ \tilde{s}_{j_1j_2j_3}=\sum_{i_1\in\pi_1(\Omega)}\tilde{u}^{(1)}_{i_1j_1}b_{i_1j_2j_3}. $$ The last summation is a matrix multiplication with $\tilde R_2\tilde R_3$ columns in its second operand. The same reasoning applies to tensors of arbitrary order. For $\ell=L,\ldots,1$, let $n_\ell$ be the number of distinct tuples of the first $\ell$ coordinates occurring in $\Omega$, and let $n_0=1$. In particular, $n_L=\nnz(\T)$. After contracting modes $L,\ldots,\ell+1$, we have $n_\ell$ rows, each containing $\prod_{j=\ell+1}^L \tilde{R}_j$ numbers. The grouping step of Algorithm~\ref{alg:grouped} removes coordinate $i_\ell$ from the keys. The recursion is $$ a^{(\ell-1)}_{i_1\ldots i_{\ell-1},\,j_\ell\ldots j_L} =\sum_{i_\ell}\tilde{u}^{(\ell)}_{i_\ell j_\ell} a^{(\ell)}_{i_1\ldots i_\ell,\,j_{\ell+1}\ldots j_L}, $$ where the sum is restricted to the indexes present in the corresponding partial contraction and the initial values are the nonzeros of $\T$. The cost of the arithmetic operations, including the final matrix multiplication, is $$ \mathcal{O}\left(\sum_{\ell=1}^L n_\ell\prod_{j=\ell}^L \tilde{R}_j\right). $$ The storage for the matrices of partial contractions in stage $\ell$ is proportional to $$ n_\ell\prod_{j=\ell+1}^L \tilde{R}_j+n_{\ell-1}\prod_{j=\ell}^L \tilde{R}_j. $$ Besides the partial contractions, we store the coordinate keys, grouping maps and $\tilde R_1n_1$ selected entries of the first compression matrix. Lexicographic sorting at stage $\ell$ costs at most $\mathcal O(\ell n_\ell\log(n_\ell+1))$. For equal compressed dimensions $k$, the last multiplication costs $\mathcal O(n_1 k^L)$ instead of $\mathcal O(\nnz(\T)k^L)$. The gain depends on the repetition of coordinate prefixes. Tensor Fox sorts the original dimensions in decreasing order and contracts in reverse order, which need not be optimal for every sparse pattern. \begin{algorithm}[htbp] \small \caption{Grouped sparse multilinear multiplication}\label{alg:grouped} \begin{algorithmic}[1] \Require Sparse coordinates $\Omega$ and values $t_i$, compression matrices $\Utilde^{(\ell)}$ \Ensure Compressed core $\Stilde$ \State Allocate the core $\Stilde$ \If{$\Omega=\varnothing$} \State \Return the zero core \EndIf \State $K\gets\Omega$, $\mathbf V\gets(t_i)_{i\in\Omega}$ as a column matrix \For{$\ell=L,L-1,\ldots,2$} \State $K'\gets$ distinct prefixes of length $\ell-1$ of the keys in $K$ \State $g(i)\gets$ row of $K'$ corresponding to the prefix of key $i$ \If{memory test \eqref{eq:group-memory-test} fails} \State \Return \Call{BlockedContractions}{$K,\mathbf V,\ell,\{\Utilde^{(j)}\},\Stilde$} \Comment{Algorithm~\ref{alg:groupblocked}} \EndIf \State $\mathbf V'\gets 0$ with $|K'|$ rows and $\tilde R_\ell$ times as many columns as $\mathbf V$ \For{each key $i$ in $K$} \State $\mathbf V'_{g(i):}\gets\mathbf V'_{g(i):}+\bigl(\tilde u^{(\ell)}_{i_\ell j_\ell}\mathbf V_{i:}\bigr)_{j_\ell=1}^{\tilde R_\ell}$ \EndFor \State $(K,\mathbf V)\gets(K',\mathbf V')$ \EndFor \State $\Stilde\gets\operatorname{reshape}((\Utilde^{(1)}_{K,:})^T\mathbf V)$ \Comment{Rows selected by the remaining keys} \State \Return $\Stilde$ \end{algorithmic} \end{algorithm} \subsection{Memory-dependent blockwise sparse multilinear multiplication}\label{sec:group-blocked} The grouped contractions described in Section~\ref{sec:group} reduce the number of arithmetic operations, but their intermediate matrices may require considerably more memory than the core itself. Indeed, the ratio between the number of entries of the matrices obtained after and before contracting mode $\ell$ is $$ \frac{n_{\ell-1}\tilde R_\ell}{n_\ell}. $$ For example, if $L=8$, all $\tilde R_\ell=13$ and $n_\ell=40\cdot3^\ell$ for $\ell=1,\ldots,8$, the intermediate after contracting mode $2$ requires $56.10$ GiB, whereas the core requires only $6.08$ GiB in double precision. We introduce a modification of the grouped algorithm to deal with this problem. Before contracting mode $\ell$, the program obtains the available RAM, denoted by $M$, in bytes. Let $C=\prod_{j=1}^L\tilde R_j$ be the number of entries of the core. The next contraction is performed as before if \begin{equation}\label{eq:group-memory-test} 8n_{\ell-1}\prod_{j=\ell}^L\tilde R_j \leq \alpha\max\{0,M-8C\}, \end{equation} where $\alpha\in(0,1)$ is the fraction of the remaining memory assigned to the contraction (we use $\alpha=1/2$). If the test fails, we complete the remaining contractions in blocks without changing the compression dimensions. Suppose the test fails before contracting mode $m$, with $2\leq m\leq L$. The current matrix has $n_m$ rows and $h=\prod_{j=m+1}^L\tilde R_j$ columns. These columns correspond to compressed indexes that do not enter the remaining summations. We can therefore contract disjoint column blocks independently. After contracting mode $\ell$, for $2\leq\ell\leq m$, a block of width $b$ has $$ n_{\ell-1}\text{ rows and }b\prod_{j=\ell}^m\tilde R_j\text{ columns}. $$ The final contraction gives $b\prod_{j=1}^m\tilde R_j$ entries of the core. The blocks occupy disjoint sets of entries and share the same grouping maps. To determine the width of a block, put $$ d_{m+1}=0,\qquad d_\ell=n_{\ell-1}\prod_{j=\ell}^m\tilde R_j,\quad \ell=1,\ldots,m, $$ where $n_0=1$. Since two consecutive partial contractions may have to be stored simultaneously, the estimated temporary storage in bytes for a block of $b$ columns is $$ 8b\max_{1\leq\ell\leq m}(d_{\ell+1}+d_\ell). $$ The current matrix is accessed through views and is not included in this estimate. If $h_0$ columns remain, we reserve $8h_0\prod_{j=1}^m\tilde R_j$ bytes for the core. Before each block the program obtains the available RAM $M$ and chooses \begin{equation}\label{eq:group-block-width} b=\min\left\{h_0,\left\lfloor \frac{\alpha\max\{0,M-8h_0\prod_{j=1}^m\tilde R_j\}} {8\max_{1\leq\ell\leq m}(d_{\ell+1}+d_\ell)} \right\rfloor\right\}. \end{equation} Algorithm~\ref{alg:groupblocked} uses this estimate before every block. The widths need not be equal since the available RAM may change during the computation. A block costs $\mathcal O(bn_\ell\prod_{j=\ell}^m\tilde R_j)$ to contract mode $\ell$. Since the widths sum to $h$, the contraction cost is unchanged. The grouping maps are computed once for all blocks and kept in memory, including the backing arrays of the retained coordinate columns. Block allocation and copying introduce additional work. Unlike memory-dependent truncation, this strategy preserves the compression dimensions. The core must still fit in RAM, and the estimates do not include every auxiliary allocation. \begin{algorithm}[htbp] \small \caption{Completion of the grouped contractions in blocks}\label{alg:groupblocked} \begin{algorithmic}[1] \Require Prefix keys $K$, current matrix $\mathbf V$, next mode $m$, compression matrices, allocated core $\Stilde$ \Ensure Compressed core $\Stilde$ \State $h\gets\prod_{j=m+1}^L\tilde R_j$, $c\gets1$ \State Precompute the remaining key groups and inverse maps for modes $m,\ldots,2$ \While{$c\leq h$} \State $h_0\gets h-c+1$, read available RAM $M$ \State Choose $b$ by \eqref{eq:group-block-width} \If{$b=0$} \State Raise a memory exception \EndIf \State $\mathbf Z\gets\mathbf V_{:,c:c+b-1}$ \Comment{View of the current columns} \For{$\ell=m,m-1,\ldots,2$} \State Contract $\mathbf Z$ with $\Utilde^{(\ell)}$ using the precomputed groups \EndFor \State Contract mode 1 by matrix multiplication, as in Algorithm~\ref{alg:grouped} \State Copy the result into the corresponding block of $\Stilde$ \State Release the temporary arrays, $c\gets c+b$ \EndWhile \State \Return $\Stilde$ \end{algorithmic} \end{algorithm} \subsection{Sparse compression without explicit unfoldings}\label{sec:implicit} In the compression algorithm of \cite{diniz2019} the unfoldings are explicitly constructed. For in-memory sparse tensors, the previous implementation computes their Gram matrices. We introduce an alternative that avoids both matrices. To understand the need for this modification, notice that the number of columns of $\T_{(\ell)}$ is $\prod_{j\ne\ell}I_j$. The linear column index can exceed the range of a machine integer, even though every tensor coordinate can be represented individually. Another problem arises when the Gram matrix has too many nonzero entries. \begin{definition}\label{def:gram} The \emph{Gram matrix} associated with the mode-$\ell$ unfolding is $$ \G^{(\ell)}=\T_{(\ell)}\T_{(\ell)}^T\in\R^{I_\ell\times I_\ell}. $$ \end{definition} Even if empty columns are removed from the unfolding before forming the Gram matrix, the latter can still have too many nonzero entries. Recall that if $\T_{(\ell)}=\U\boldsymbol\Sigma\V^T$ is an SVD, then $$ \G^{(\ell)}=\U\boldsymbol\Sigma^2\U^T. $$ Thus the leading left singular vectors of the unfolding are eigenvectors of $\G^{(\ell)}$, and the corresponding singular values are the square roots of its eigenvalues. We will do this without constructing either the unfolding or its Gram matrix. \subsubsection{Products with the Gram matrix} We replace multiplication by a stored Gram matrix with a direct operation on the fibers. Let $\mathbf{a}^{(c)}$ denote a nonzero mode-$\ell$ fiber of $\T$, that is, a nonzero column of $\T_{(\ell)}$. The index $c$ can be represented by the tuple of the other $L-1$ coordinates instead of a linear index. Since zero columns contribute nothing, we have $$ \G^{(\ell)}=\sum_c\mathbf{a}^{(c)}(\mathbf{a}^{(c)})^T. $$ Let $\mathbf{Z}$ be any matrix with $I_\ell$ rows and $b$ columns. For each fiber compute the row vector $$ \mathbf{z}^{(c)}=(\mathbf{a}^{(c)})^T\mathbf{Z} =\sum_{i:a_i^{(c)}\ne 0}a_i^{(c)}\mathbf{Z}_{i:}. $$ The product of interest is then $$ \G^{(\ell)}\mathbf{Z}=\sum_c\mathbf{a}^{(c)}\mathbf{z}^{(c)}. $$ Two passes through each fiber suffice: one to compute $\mathbf{z}^{(c)}$ and one to accumulate its contribution. The arithmetic cost is $\mathcal O(b\nnz(\T))$, apart from initializing the output and identifying fiber boundaries. \subsubsection{Restricting the eigenproblem to active rows} We can reduce the size of the eigenvalue problem by omitting the null rows and columns of the Gram matrix. Denote by $a_\ell$ the number of rows of $\T_{(\ell)}$ having at least one stored nonzero. The other rows and columns of $\G^{(\ell)}$ are zero. Let $\mathbf{E}\in\R^{I_\ell\times a_\ell}$ be the matrix whose columns are the canonical basis vectors associated with these \emph{active rows}. Then $$ \G^{(\ell)}=\mathbf{E}(\mathbf{E}^T\G^{(\ell)}\mathbf{E})\mathbf{E}^T. $$ The nonzero eigenvalues are those of $\mathbf{E}^T\G^{(\ell)}\mathbf{E}$. Rather than constructing $\mathbf E$, we map active rows to consecutive indexes and embed the resulting eigenvectors in the original space. The row map and the dense output compression matrix still depend on $I_\ell$. \subsubsection{Parallel products with blocks of fibers} Direct parallel traversal over disjoint columns of $\mathbf Z$ avoids conflicting writes but repeats the fiber scans and may give unfavorable memory accesses. We introduce a second approach using sparse blocks. Write the active unfolding as a concatenation of blocks of complete nonzero fibers, $\mathbf{A}=[\mathbf{A}_1|\cdots|\mathbf{A}_s]$, where $\mathbf{A}_j\in\R^{a_\ell\times f_j}$. Then \[ \mathbf{G}\mathbf{Z}=\sum_{j=1}^s\mathbf{A}_j(\mathbf{A}_j^T\mathbf{Z}). \] In RAM we can store $\mathbf A_j$ and $\mathbf A_j^T$ in compressed sparse row (CSR) format. On disk the records are already ordered by fibers, so we construct indexes to traverse the same records by fiber and by row. Each product is parallelized over disjoint output rows (Figure~\ref{fig:gram-blocks}). In the out-of-core case the work is distributed according to the number of nonzeros, rather than the number of fibers or rows. This improves the balance when their occupancies differ, without replicating the dense matrices. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=0.75cm,y=0.75cm] \node[anchor=west] at (0,3.5){$\mathbf A=[\mathbf A_1|\cdots|\mathbf A_s]$}; \foreach \k in {0,...,3}{\draw[fill=gray!10] (\k,1.6) rectangle ++(.95,1.5);} \fill[gray!40] (1,1.6) rectangle ++(.95,1.5);\draw (1,1.6) rectangle ++(.95,1.5); \node at (1.475,2.35){$\mathbf A_j$}; \node[below] at (2,1.6){complete fibers}; \draw[->,thick] (4.1,2.35)--(5.2,2.35); \node[draw,fill=gray!12,align=center,minimum width=2.4cm,minimum height=1cm] (csr) at (7.2,2.35){Fiber and row\\traversal indexes}; \draw[->,thick] (csr.east)--++(1.2,0) node[right,draw,align=center,fill=gray!12] (z) {$\mathbf Z_j=\mathbf A_j^T\mathbf Z$\\$f_j\times b$}; \node[font=\scriptsize,above=2pt of z]{parallel over fibers}; \node[draw,fill=gray!25,align=center] (y) at (14,-.15){$\mathbf Y\leftarrow\mathbf Y+\mathbf A_j\mathbf Z_j$\\$a_\ell\times b$}; \draw[->,thick] (z.south)--(y.north); \draw[->,dashed] (csr.south) |- node[pos=0.75,below=3pt,font=\scriptsize]{parallel over active rows} (y.west); \end{tikzpicture} \caption{Implicit Gram multiplication with a block of complete fibers. The first product is parallelized over fibers and the second over active rows. The intermediate has $f_jb$ entries. In the out-of-core algorithm, both traversal orders share the values of the current block.} \label{fig:gram-blocks} \end{figure} Let $N_j$ be the number of stored entries in a block. Besides the dense input and output, the auxiliary storage is of order $\mathcal O(N_j+a_\ell+f_jb)$, for the sparse representation, indexes and intermediate product. The two products cost $\mathcal O(Nb)$ in total, where $N=\nnz(\T)$. Constructing the traversal indexes on disk requires $\mathcal O(LN+sa_\ell)$ operations for $s$ blocks, without sorting the entries again. In RAM the CSR construction may also involve sorting. A fiber cannot be treated as a sum of independent partial fibers in the Gram product, since this would omit cross terms. Long fibers are therefore processed by first computing their full projection and then accumulating their contribution, even if several disk windows are needed. Block sizes depend on the available RAM and the sampling width. The simpler direct traversal is retained for small products or insufficient workspace. \begin{algorithm}[htbp] \small \caption{Product with the implicit active Gram matrix}\label{alg:gram} \begin{algorithmic}[1] \Require Records sorted by fiber, active row map, dense matrix $\mathbf Z\in\R^{a_\ell\times b}$ \Ensure $\mathbf Y=\mathbf G\mathbf Z$ \State Determine the block limits using the available RAM \If{problem is small, only one thread is available, or the block budget is insufficient} \State \Return direct Gram product by the two fiber passes \EndIf \State $\mathbf Y\gets 0\in\R^{a_\ell\times b}$ \For{each block of complete fibers $\mathbf A_j$} \If{a single fiber exceeds the block limit} \State Accumulate its contribution by the two fiber passes of Section~\ref{sec:implicit} \Else \State Construct traversal indexes for $\mathbf A_j$ and $\mathbf A_j^\top$ \State $\mathbf Z_j\gets\mathbf A_j^T\mathbf Z$ \Comment{Parallel over fibers} \State $\mathbf Y\gets\mathbf Y+\mathbf A_j\mathbf Z_j$ \Comment{Parallel over active rows} \State Release the block and $\mathbf Z_j$ \EndIf \EndFor \State \Return $\mathbf Y$ \end{algorithmic} \end{algorithm} \subsubsection{A randomized eigendecomposition} Algorithm~\ref{alg:gram-eigh} applies a randomized range finder to the active Gram operator, denoted here also by $\mathbf G$. Let $k=\min\{P_\ell,a_\ell\}$ and $b=\min\{a_\ell,k+10\}$, where $P_\ell$ is the requested number of components. The two LU normalizations are given by \[ \mathbf{G}\mathbf{Q}_{j-1}=\mathbf{P}_j\mathbf{L}_j\mathbf{U}_j, \qquad \mathbf{Q}_j=\mathbf{P}_j\mathbf{L}_j,\quad j=1,2. \] These are the same LU normalizations discussed for the randomized SVD. Next we compute the economic QR factorization of $\mathbf{G}\mathbf{Q}_2$ to obtain an orthonormal matrix $\mathbf{Q}$. In exact arithmetic, provided the sampled matrices have full column rank, its columns span the image of $\mathbf{G}^3\boldsymbol\Omega$. With this we form \[ \mathbf{B}=\mathbf{Q}^T\mathbf{G}\mathbf{Q}\in\R^{b\times b},\qquad \mathbf{B}=\mathbf{Z}\boldsymbol\Lambda\mathbf{Z}^T. \] We symmetrize $\mathbf B$ before computing its eigendecomposition. The leading Ritz vectors $\mathbf Q\mathbf Z_{:1:k}$ approximate the eigenvectors of $\mathbf G$, and the singular values are estimated by the square roots of the nonnegative Ritz values \cite{halko2011}. There are four products with the Gram operator: two LU-normalized products, the product preceding the QR factorization and the computation of $\mathbf{G}\mathbf{Q}$. The arithmetic cost per mode, excluding sorting, preparation of the sparse blocks and embedding the vectors, is $$ \mathcal{O}\left((b+L)\nnz(\T)+a_\ell b^2+b^3\right). $$ The term involving $L$ accounts for comparisons of tuple keys in the scans. The main dense matrices require storage of order $\mathcal{O}(a_\ell b+b^2)$. In the out-of-core case we reuse the LU workspace to obtain $\mathbf P\mathbf L$ without forming separate triangular and permutation matrices, and compute only the orthogonal factor of the QR factorization. We apply the truncation criterion before constructing the final vectors, embedding only the retained columns in $\R^{I_\ell}$. Thus the output factor requires $\mathcal O(I_\ell\tilde R_\ell)$ storage. As with the explicit Gram matrix, computing small singular values from their squares can lead to a loss of relative accuracy. \begin{algorithm}[htbp] \small \caption{Randomized eigendecomposition of the implicit Gram matrix}\label{alg:gram-eigh} \begin{algorithmic}[1] \Require Active Gram operator $\mathbf G$, number $k\leq a_\ell$ of requested components \Ensure Approximate leading eigenvalues $\lambda_1,\ldots,\lambda_k$ and eigenvectors $\U_k$ \State $b\gets\min\{a_\ell,k+10\}$, $\mathbf Q\gets\operatorname{Gaussian}(a_\ell,b)$ \For{$j=1,2$} \State $(\mathbf P,\mathbf L,\mathbf U_0)\gets\operatorname{LU}(\mathbf G\mathbf Q)$ \State $\mathbf Q\gets\mathbf P\mathbf L$ \EndFor \State $\mathbf Q\gets\Call{QRBasis}{\mathbf G\mathbf Q}$ \Comment{Only the basis is needed} \State $\mathbf B\gets\mathbf Q^T(\mathbf G\mathbf Q)$ \Comment{Fourth application of Algorithm~\ref{alg:gram}} \State $\mathbf B\gets(\mathbf B+\mathbf B^T)/2$ \State $(\mathbf Z,\boldsymbol\Lambda)\gets\operatorname{Eigh}(\mathbf B)$ \Comment{Order eigenvalues decreasingly} \State $\U_k\gets\mathbf Q\mathbf Z_{:1:k}$ \State \Return $\lambda_1,\ldots,\lambda_k,\U_k$ \end{algorithmic} \end{algorithm} \subsubsection{Modes which do not need to be compressed} If $P_\ell=I_\ell$, we take $\Utilde^{(\ell)}=I_{I_\ell}$ without sorting or spectral computation. The result is an orthogonal Tucker compression, not necessarily an all-orthogonal MLSVD. \subsubsection{Automatic switching in memory} For sparse tensors in RAM, we first attempt the explicit Gram approach. If the unfolding or Gram matrix cannot be formed, we use the implicit Gram operator and the grouped contraction of Section~\ref{sec:group}. \subsection{Out-of-core computation}\label{sec:outcore} We also extend sparse tensor compression to tensors stored on disk. The objective is to reserve RAM for the compression matrices, spectral workspaces and dense core, accessing the sparse entries only as they are needed. \begin{definition}\label{def:out-of-core} We will refer to computations in which some of the arrays reside on disk as \emph{out-of-core} computations. \end{definition} \subsubsection{Representation of tensors on disk} A sparse tensor on disk is represented by its dimensions, distinct coordinates and corresponding values. Permuting modes requires only a change in the order in which the coordinates are interpreted. Reading successive blocks of a memory-mapped tensor may leave previously accessed data resident in RAM. We therefore map and release each window independently (Figure~\ref{fig:disk-windows}), allowing memory to be reused by subsequent computations. For example, if $\Omega=\Omega_1\cup\cdots\cup\Omega_m$ is a partition of the nonzero entries, then $$ \|\T\|^2=\sum_{p=1}^m\sum_{i\in\Omega_p}t_i^2. $$ Each block is read, validated and processed separately. The same principle applies to the identification of active rows. In Gram multiplication, windows contain complete fibers whenever possible, with the two-pass treatment of Section~\ref{sec:implicit} for longer fibers. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=1cm,y=1cm] \node[anchor=west] at (0,2.45){Sparse records on disk}; \foreach \j in {0,...,7}{\draw[fill=gray!12] (\j*1.42,1.65) rectangle ++(1.42,.55);} \fill[articleblue!30] (4.26,1.65) rectangle ++(1.42,.55); \draw (4.26,1.65) rectangle ++(1.42,.55); \node at (4.97,1.925){Window}; \draw[->,thick] (4.97,1.6)--(4.97,.92); \draw[fill=articleblue!20] (4.26,.3) rectangle ++(1.42,.55); \node at (4.97,.575){Block}; \node[anchor=east] at (3.9,.575){RAM}; \draw[->,thick] (5.75,.575)--(6.55,.575); \node[draw,fill=gray!15,align=center,minimum height=.55cm] (proc) at (7.95,.575){Process block}; \draw[->,thick] (proc.east)--++(.4,0) node[right,align=left]{Release\\mapping}; \draw[->,dashed] (5.8,2.35)--(7.05,2.35); \node[above,font=\scriptsize] at (6.43,2.35){Next window}; \draw[fill=gray!35] (0,-.95) rectangle (11.36,-.4); \node at (5.68,-.675){Dense matrices and accumulated result in RAM}; \end{tikzpicture} \caption{Independent windows of a disk-backed tensor. Each mapping is released after use, reserving RAM for the dense computations.} \label{fig:disk-windows} \end{figure} \subsubsection{External sorting of fibers} For a given mode, all entries in the same fiber must be consecutive. We partition the nonzeros into blocks, load each block sequentially into RAM, and sort by the tuple of the other $L-1$ coordinates. Independent blocks are sorted in parallel under a shared memory budget. Sorted records are written in small batches, avoiding a second complete sorted copy of each block in RAM. Compact representations of the coordinates further reduce the storage and disk traffic of the sorted runs without changing the precision of the tensor entries. The sorted runs are merged using one bounded input buffer per run and an output buffer. A tournament tree identifies the smallest current key. The number of runs merged simultaneously (the \emph{fan-in}) and the buffer sizes are chosen according to available RAM to reduce the number of passes through the data (Figure~\ref{fig:external-sort}). \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=1cm,y=1cm] \node[draw,minimum width=11.8cm,minimum height=.55cm,fill=gray!12] at (6,0){Disk: coordinate-value records}; \foreach \i in {0,...,3}{ \pgfmathsetmacro\xx{1.5+3*\i} \node[draw,fill=articleblue!12,minimum width=2.35cm,minimum height=.58cm] (b\i) at (\xx,-1.15){Block}; \node[draw,fill=gray!20,minimum width=2.35cm,minimum height=.58cm] (r\i) at (\xx,-2.22){Sorted run}; \draw[->] (\xx,-.28)--(b\i.north); \draw[->] (b\i)--node[right,font=\scriptsize]{sort} (r\i); \node[draw,fill=articleblue!25,minimum width=1.45cm,minimum height=.43cm] (q\i) at (\xx,-3.22){Buffer}; \draw[->] (r\i)--(q\i); } \node[draw,dashed,fit=(b0)(b3),inner sep=3pt] (budget) {}; \node[above,font=\scriptsize,fill=white,inner sep=1pt] at (budget.north){Shared RAM budget for parallel sorting}; \node[draw,fill=articleblue!12,minimum width=3.7cm,minimum height=.6cm] (merge) at (6,-4.35){Merge and output buffer}; \foreach \i in {0,...,3}{\draw[->] (q\i.south)--(merge.north);} \node[draw,fill=gray!35,minimum width=11.8cm,minimum height=.55cm] (out) at (6,-5.4){Disk: records sorted by fiber keys}; \draw[->] (merge)--(out); \end{tikzpicture} \caption{External sorting of fibers. Blocks are sorted independently in RAM. The merge reads and writes through bounded buffers, combining as many runs as the budget permits. More than one pass may be necessary.} \label{fig:external-sort} \end{figure} Each compressed mode requires its own ordering. The sorted file is reused in the four Gram products and discarded after the mode is compressed. No linearized fiber index is needed. \subsubsection{Blockwise compression of third order tensors} Multilinear multiplication is linear in the tensor. If $\T^{(p)}$ contains the entries of block $p$, then $$ \Stilde=\sum_p((\Utilde^{(1)})^T,\ldots,(\Utilde^{(L)})^T)\cdot\T^{(p)}. $$ In the case $L=3$, let $a,b,c$ be a permutation of $1,2,3$. Denote by $\mathbf A^{(p,i_c)}$ the sparse matrix obtained by fixing mode $c$ at $i_c$ in $\T^{(p)}$, with rows corresponding to mode $a$ and columns to mode $b$. Then $$ \Stilde_{j_a j_b j_c}=\sum_p\sum_{i_c} \left((\Utilde^{(a)})^T\mathbf A^{(p,i_c)}\Utilde^{(b)}\right)_{j_a j_b} \tilde u^{(c)}_{i_c j_c}. $$ The first contraction is a sparse-dense matrix product and the second is a dense matrix product. Only the active rows of a slice need be stored in the intermediate matrix. We choose $c$ with the smallest original dimension (breaking ties by the compressed dimension), and order the other modes so that $\tilde R_a\leq\tilde R_b$. Large slices can be subdivided along their active rows and, if necessary, along the compressed dimensions. Let $J_a,J_b$ be sets of retained column indexes and let $H$ be a group of active rows. The corresponding compressed block is $$ \mathbf B=(\Utilde^{(a)}_{H,J_a})^T\left(\mathbf A^{(p,i_c)}_{H,:}\Utilde^{(b)}_{:,J_b}\right). $$ Figure~\ref{fig:disk-core-tiles} illustrates this operation. If the free mode uses the identity, $\mathbf B$ is added directly to the appropriate core slice. Otherwise, we accumulate the weighted blocks into the core, in parallel for large contributions, without constructing a second dense core. The workspace is chosen after accounting for the size of the core. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=.65cm,y=.65cm] \draw[fill=gray!10] (0,0) rectangle (3,4.4); \fill[articleblue!25] (0,1.5) rectangle (3,2.7); \draw (0,1.5) rectangle (3,2.7); \node[above] at (1.5,4.4){$\mathbf A^{(p,i_c)}$}; \node[left] at (0,2.1){$H$}; \node at (1.5,2.1){$\mathbf A_{H,:}$}; \node at (3.55,2.1){$\times$}; \draw[fill=gray!10] (4.1,0) rectangle (6.3,4.4); \fill[articleblue!25] (4.8,0) rectangle (5.6,4.4); \draw (4.8,0) rectangle (5.6,4.4); \node[above] at (5.2,4.4){$\Utilde^{(b)}$}; \node[below] at (5.2,0){$J_b$}; \draw[->,thick] (6.6,2.1)--(7.7,2.1); \draw[fill=articleblue!25] (8,1.5) rectangle (9.1,2.7); \node[above] at (8.55,2.7){$\mathbf D$}; \node[below] at (8.55,1.5){$|H|\times|J_b|$}; \draw[->,thick] (9.45,2.1)--(12,2.1) node[midway,above,font=\scriptsize]{$(\Utilde^{(a)}_{H,J_a})^T\cdot$}; \draw[fill=gray!10] (12.3,0) rectangle (15.4,4.4); \fill[articleblue!35] (13.25,1.5) rectangle (14.25,2.7); \draw (13.25,1.5) rectangle (14.25,2.7); \node at (13.75,2.1){$\mathbf B$}; \node[above] at (13.85,4.4){Compressed slice}; \node[left] at (12.3,2.1){$J_a$}; \node[below] at (13.75,0){$J_b$}; \end{tikzpicture} \caption{Contraction of a sparse slice in blocks. Only the selected active rows and retained columns contribute to the intermediate $\mathbf D$ and the output block $\mathbf B$. Their sizes can be reduced independently of the dimensions of the final core.} \label{fig:disk-core-tiles} \end{figure} \subsubsection{Blockwise compression of higher order tensors} For $L>3$ we accumulate the contribution of each nonzero directly into the core, $$ t_{i_1\ldots i_L}\Utilde^{(1)}_{i_1:}\otimes\cdots\otimes\Utilde^{(L)}_{i_L:}. $$ Partial products are reused as the compressed indexes vary. Parallel computations use disjoint groups of first-mode indexes of the core, sharing the input block and the compression matrices. Each thread stores only its selected coefficients and partial products, rather than a separate core. This approach trades arithmetic for reduced storage of intermediate contractions. \subsubsection{Scope of the out-of-core algorithm} The sparse input and sorted records can reside on disk, but the compression matrices, spectral workspaces and dense core must still fit in RAM. The subsequent dGN iterations are performed in the compressed space. The tensor train approach may also require an enlarged core. As in the in-memory sparse case, no refinement is performed in the original tensor space. \section{Evaluation of the approximation error}\label{sec:error} We consider the evaluation of compression and CPD errors without reconstructing the full tensor, and the blockwise computation of dense residuals. \subsection{Error of the MLSVD} Orthogonal compression allows us to evaluate the error from the norms of the tensor and its core, even when the compression matrices are obtained by an approximate SVD. \begin{proposition}\label{prop:projected} Let $\Utilde^{(\ell)}\in\R^{I_\ell\times \tilde{R}_\ell}$ satisfy $(\Utilde^{(\ell)})^T\Utilde^{(\ell)}=I_{\tilde{R}_\ell}$ for each $\ell=1,\ldots,L$, and let $\Stilde=((\Utilde^{(1)})^T,\ldots,(\Utilde^{(L)})^T)\cdot\T$. For every tensor $\Shat\in\R^{\tilde{R}_1\times\cdots\times \tilde{R}_L}$ we have $$ \begin{aligned} \|\T-(\Utilde^{(1)},\ldots,\Utilde^{(L)})\cdot\Shat\|^2 &=\|\T\|^2-\|\Stilde\|^2+\|\Stilde-\Shat\|^2. \end{aligned} $$ \end{proposition} \begin{proof} Denote by $A$ the linear map given by multilinear multiplication with $(\Utilde^{(1)},\ldots,\Utilde^{(L)})$. Its adjoint $A^*$ is the multilinear multiplication with the transposed matrices. By the identities $(\Utilde^{(\ell)})^T\Utilde^{(\ell)}=I_{\tilde{R}_\ell}$, $A^*A$ is the identity on the compressed tensor space. In particular, $A$ preserves norms and $AA^*$ is the orthogonal projection onto its image. Since $\Stilde=A^*\T$, write $$ \T-A\Shat=(\T-A\Stilde)+A(\Stilde-\Shat). $$ The first term is orthogonal to the image of $A$, while the second belongs to this image. Pythagoras' theorem gives $$ \|\T-A\Shat\|^2=\|\T-A\Stilde\|^2+\|\Stilde-\Shat\|^2. $$ Applying the same reasoning to $\T=(\T-A\Stilde)+A\Stilde$ gives $\|\T-A\Stilde\|^2=\|\T\|^2-\|\Stilde\|^2$, proving the desired result. \end{proof} In the particular case $\Shat=\Stilde$ we obtain the squared relative error of the MLSVD: $$ e_{\mathrm{MLSVD}}^2=\frac{\|\T \|^2 - \|\Stilde\|^2}{\|\T\|^2}. $$ Tensor Fox evaluates the compression error by this formula for sparse tensors and for dense cores with at least $10^6$ entries. For smaller dense cores it ordinarily reconstructs the tensor. In sequential compression the formula is exact only if the propagated core equals the orthogonal projection. An approximate SVD applied to the transpose of a wide unfolding need not satisfy this condition. \subsection{Error of the initial and final compressed approximations} Proposition~\ref{prop:projected} also permits evaluation of the initial sparse approximations and the final disk-backed CPDs from the norm of the original tensor and a reconstruction in the compressed space. The tensor train approach saves the compressed core before any enlargement of its dimensions. Denote the relative error of the uncompressed CPD by $e_{\mathrm{CPD}}=\|\T-\Ttilde\|/\|\T\|$. If $\Stilde\ne0$ and $e_{\mathrm{core}}=\|\Stilde-\Shat\|/\|\Stilde\|$, then $$ e_{\mathrm{CPD}}^2=e_{\mathrm{MLSVD}}^2+(1-e_{\mathrm{MLSVD}}^2)e_{\mathrm{core}}^2. $$ Thus the compression error sets a lower bound on the attainable relative error within the retained subspace. \begin{remark} The machine precision is $\varepsilon_{\mathrm{mach}}=2^{-52}\approx 2.22\times10^{-16}$ in double precision arithmetic. Since these formulas evaluate the squared relative error, cancellation becomes particularly severe for relative errors of the order of $$ \sqrt{\varepsilon_{\mathrm{mach}}}\approx 1.49\times10^{-8}. $$ Below this scale, the computed relative error may become zero even when the actual residual is nonzero. \end{remark} \subsection{Two groups of factors in tensor reconstruction}\label{sec:split} \begin{definition}\label{def:khatri-rao} The \emph{Khatri-Rao product} $\mathbf{A}\odot\mathbf{B}$ of two matrices $\mathbf{A}$ and $\mathbf{B}$ with $R$ columns is the matrix whose $r$-th column is the Kronecker product of the $r$-th columns of $\mathbf{A}$ and $\mathbf{B}$ (see \cite[Definition B.3.3]{diniz2019}). \end{definition} We modify the reconstruction of the first unfolding by splitting the factors into two groups rather than isolating a single factor, reducing the size of the intermediate Khatri-Rao products. Consider the approximation $\Shat$ of the compressed tensor $\Stilde$, with factor matrices $\W^{(\ell)}\in\R^{\tilde{R}_\ell\times R}$. Given these matrices, the coordinates of the corresponding tensor can be obtained from any of its unfoldings. In the first unfolding, the conventional formula is $$ \Shat_{(1)}=\W^{(1)}(\W^{(L)}\odot\cdots\odot\W^{(2)})^T. $$ The ordering in the formula is consistent with the ordering of the fibers of the unfolding. This requires constructing a matrix of $R\prod_{\ell=2}^L \tilde{R}_\ell$ entries. This matrix can be much larger than the tensor itself, for instance when $R>\tilde{R}_1$. There is no need to choose a single factor as the left operand. Let $A,B$ be a partition of the modes into two ordered groups. Define the matrices $\mathbf{K}_A,\mathbf{K}_B$ with entries $$ (\mathbf{K}_A)_{i_A r}=\prod_{\ell\in A}w^{(\ell)}_{i_\ell r},\qquad (\mathbf{K}_B)_{i_B r}=\prod_{\ell\in B}w^{(\ell)}_{i_\ell r}. $$ Here $i_A,i_B$ denote the corresponding multi-indexes, linearized in the order used for the matrix representation. By the definition of the CPD, $$ \hat{s}_{i_A i_B}=\sum_{r=1}^R(\mathbf{K}_A)_{i_A r}(\mathbf{K}_B)_{i_B r}. $$ Thus, denoting the matrix obtained by grouping the indexes of $\Shat$ in this manner by $\widehat{\mathbf{S}}$, we can write simply $$ \widehat{\mathbf{S}}=\mathbf{K}_A\mathbf{K}_B^T. $$ We use two groups with $\lfloor L/2\rfloor$ and $\lceil L/2\rceil$ modes, respectively. Their membership depends on the layout and the operation being computed. Put $p=\prod_{\ell\in A}\tilde{R}_\ell$ and $q=\prod_{\ell\in B}\tilde{R}_\ell$. The two Khatri-Rao products have $R(p+q)$ entries, instead of $R\prod_{\ell=2}^L \tilde{R}_\ell$ for the conventional formula. If $\tilde{R}_1=\cdots=\tilde{R}_L=n$, the larger product changes from $Rn^{L-1}$ entries to $Rn^{\lceil L/2\rceil}$. The final multiplication still costs $\mathcal{O}(Rpq)$ and a full reconstruction still requires $pq$ entries. For third order tensors the maximum exponent is unchanged. The main benefit of the two-group formulation for higher order tensors is the reduction in the size of the intermediate matrices. The split is based on the number of modes, not on their products, so it need not give the best split for very unequal dimensions. We use this representation to reconstruct the first unfolding directly in its memory layout, avoiding a subsequent permutation of its entries. Full tensor reconstruction retains the conventional formula followed by foldback. \subsection{Blockwise computation of the dense residual} In \cite[Section 4.1.3.2]{diniz2019}, the complete approximation was constructed to evaluate the residual. Instead, we use the representation in Section~\ref{sec:split}. Denote by $\tilde{\mathbf S}$ the matrix obtained by grouping the indexes of the objective tensor, corresponding to $\widehat{\mathbf S}=\mathbf K_A\mathbf K_B^T$. For a partition $J_1,\ldots,J_m$ of the row indexes, $$ \|\Stilde-\Shat\|^2=\sum_{j=1}^m \|\tilde{\mathbf{S}}_{J_j,:}-(\mathbf{K}_A)_{J_j,:}\mathbf{K}_B^T\|^2. $$ Algorithm~\ref{alg:residual} evaluates the right hand side one block at a time, reusing the same memory (Figure~\ref{fig:residual-block}). The target size $M_b$ of a block balances memory locality against the overhead of repeated products and reductions, taking into account the number of threads. Let $s$ be the number of bytes per scalar. The number of rows per block is then $$ b=\max\left\{1,\min\left\{p,\left\lfloor\frac{M_b}{s q}\right\rfloor\right\}\right\}. $$ The buffer contains $bq$ entries. A parallel reduction computes the squared residual without forming a separate residual matrix. Auxiliary memory is $\mathcal O(R(p+q)+bq)$, excluding layout conversions, and arithmetic cost remains $\mathcal O(Rpq)$. Direct subtraction avoids the cancellation associated with differences of squared norms. \begin{algorithm}[htbp] \small \caption{Blockwise computation of the dense residual norm}\label{alg:residual} \begin{algorithmic}[1] \Require $\Stilde$, factors $\W^{(\ell)}$, optional target block size $M_b$ in bytes \Ensure $\|\Stilde-\Shat\|$ \State Form $\mathbf K_A,\mathbf K_B$ and view $\Stilde$ as $\tilde{\mathbf S}\in\R^{p\times q}$, as in Section~\ref{sec:split} \If{no target size is prescribed} \State Choose $M_b$ according to the matrix dimensions and available parallelism \EndIf \State $b\gets\max\{1,\min\{p,\lfloor M_b/(s q)\rfloor\}\}$ \Comment{$s$: bytes per scalar} \State Allocate a buffer $\mathbf V\in\R^{b\times q}$, set $e\gets0$ \For{$i=1,1+b,1+2b,\ldots\leq p$} \State $j\gets\min\{i+b-1,p\}$, $J\gets\{i,\ldots,j\}$ \State $\mathbf V_{1:|J|,:}\gets(\mathbf K_A)_{J,:}\mathbf K_B^T$ \Comment{Matrix multiplication} \State $e\gets e+\sum_{a=1}^{|J|}\sum_{c=1}^{q}(\tilde s_{i+a-1,c}-v_{ac})^2$ \Comment{Parallel reduction} \EndFor \State \Return $\sqrt e$ \end{algorithmic} \end{algorithm} \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=.62cm,y=.52cm] \foreach \y in {0,...,7}{\draw[gray!70,fill=gray!8] (0,-\y) rectangle ++(2,-1);\draw[gray!70,fill=gray!8] (9,-\y) rectangle ++(5,-1);} \fill[gray!48] (0,-2) rectangle (2,-4);\draw (0,-2) rectangle (2,-4); \fill[gray!48] (9,-2) rectangle (14,-4);\draw (9,-2) rectangle (14,-4); \node[above] at (1,0){$\mathbf K_A$};\node[above] at (11.5,0){Objective matrix $\tilde{\mathbf S}$}; \draw[fill=gray!14] (3,-1) rectangle (8,-3);\node at (5.5,-2){$\mathbf K_B^T$}; \draw[fill=gray!35] (3,-4.6) rectangle (8,-6.6);\node[align=center] at (5.5,-5.6){Reusable\\buffer}; \draw[->,thick] (2,-3)--(3.0,-4.6);\draw[->,thick] (5.5,-3)--(5.5,-4.6);\draw[->,thick] (11.5,-4)--(8,-5.4); \node[draw,fill=white,align=center] (acc) at (5.5,-10){Accumulate $\|\tilde{\mathbf S}_{J:}-(\mathbf K_A)_{J:}\mathbf K_B^T\|^2$}; \draw[->,thick] (5.5,-6.6)--(acc.north); \node[below] at (1,-8){$p\times R$};\node[below] at (11.5,-8){$p\times q$}; \end{tikzpicture} \caption{Blockwise residual computation. The same buffer is used to form each block of $\mathbf{K}_A\mathbf{K}_B^T$, subtract the corresponding entries of the objective tensor in a reduction, and accumulate the squared norm. The full approximation is not needed for this residual evaluation.} \label{fig:residual-block} \end{figure} \subsection{Traversing contiguous arrays when evaluating a residual} When computing the Frobenius norm of the difference between two matrices, we traverse consecutive entries in memory whenever possible (Figure~\ref{fig:contiguous-access}). For column-major matrices this means traversing columns and parallelizing over them, rather than traversing rows as in the previous approach. The arithmetic cost remains $\mathcal O(mn)$. \begin{figure}[htbp] \centering \begin{tikzpicture}[font=\small,>=Stealth,x=.8cm,y=.7cm] \foreach \shift in {0,7}{\foreach \i in {0,...,2}{\foreach \j in {0,...,3}{\draw[fill=gray!8] (\shift+\j,-\i) rectangle ++(1,-1);\pgfmathtruncatemacro\val{1+\i+3*\j}\node at (\shift+\j+.5,-\i-.5){\val};}}} \node[above] at (2,0){Column-wise};\node[above] at (9,0){Row-wise}; \draw[->,thick] (-.28,-.15)--(-.28,-2.85);\draw[->,thick] (6.95,.16)--(10.9,.16); \foreach \i in {0,...,11}{\draw[fill=gray!10] (\i*.45+.1,-4.2) rectangle ++(.45,-.65);\pgfmathtruncatemacro\val{\i+1}\node[font=\scriptsize] at (\i*.45+.325,-4.525){\val};\draw[fill=gray!10] (\i*.45+6.2,-4.2) rectangle ++(.45,-.65);\node[font=\scriptsize] at (\i*.45+6.425,-4.525){\val};} \foreach \i in {0,1,2}{\fill[gray!45] (\i*.45+.1,-4.2) rectangle ++(.45,-.65);\pgfmathtruncatemacro\val{\i+1}\node[font=\scriptsize] at (\i*.45+.325,-4.525){\val};} \foreach \i in {0,3,6,9}{\fill[gray!45] (\i*.45+6.2,-4.2) rectangle ++(.45,-.65);\pgfmathtruncatemacro\val{\i+1}\node[font=\scriptsize] at (\i*.45+6.425,-4.525){\val};} \node[below] at (2.8,-4.95){Consecutive positions};\node[below] at (8.9,-4.95){Stride of three entries}; \end{tikzpicture} \caption{Order of access to the entries of a column-major matrix. Traversing a column accesses consecutive memory positions, while traversing a row accesses positions separated by the leading dimension. The situation is reversed for a row-major matrix.} \label{fig:contiguous-access} \end{figure} \section{The damped Gauss-Newton algorithm} We retain the dGN framework of \cite{diniz2019}. Here we study the computation of the gradient, memory usage and iteration parameters. For the compressed tensor $\Stilde$, we have $f(\w)=\vecop(\Stilde-\Shat)$ and $F(\w)=\frac12\|f(\w)\|^2$, where $\w\in\R^{R\sum_{\ell=1}^L\tilde{R}_\ell}$. The Gauss-Newton method is obtained from the linear approximation of $f$ at a point $\w$. Denote its \emph{Jacobian} by $\mathbf{J}_f$ and the \emph{approximated Hessian} by $\mathbf{H}=\mathbf{J}_f^T\mathbf{J}_f$. The negative gradient is $\mathbf{g}=-\mathbf{J}_f^Tf(\w)=-\nabla F(\w)$. \begin{definition}\label{def:dgn} The \emph{damped Gauss-Newton} (dGN) step $\x$, which is added to $\w$, satisfies, at least approximately, $$ (\mathbf{H}+\mu\mathbf{D})\x=\mathbf{g}, $$ where $\mu$ is the \emph{damping parameter} and $\mathbf{D}$ is a diagonal regularization matrix. \end{definition} The structure of $\mathbf H$ allows matrix-vector products without constructing the approximated Hessian \cite[Theorem 3.5.10]{diniz2019}, as exploited by CG and related CPD solvers \cite{phan2013}. We focus here on the gradient, the memory required by the iterations and the choice of iteration parameters. \subsection{Computing the gradient with two partial contractions}\label{sec:gradient} \begin{definition}\label{def:hadamard} The \emph{Hadamard product} $\mathbf{A}\ast\mathbf{B}$ of two matrices of the same shape is the matrix with entries $(\mathbf{A}\ast\mathbf{B})_{ij}=a_{ij}b_{ij}$. \end{definition} In the gradient computation described in the thesis \cite[Section 4.1.3.3]{diniz2019}, the contraction with the objective tensor is performed separately for each mode. We now reuse two partial contractions to compute these quantities for all modes. As before, define $$ \Pi^{(\ell)}=\pi^{(1)}\ast\cdots\ast\pi^{(\ell-1)}\ast\pi^{(\ell+1)}\ast\cdots\ast\pi^{(L)}, $$ where $\pi^{(\ell)}=(\W^{(\ell)})^T\W^{(\ell)}$. Consider also the Khatri-Rao product $\odot$ introduced in Section~\ref{sec:split}. We have the following result. We recall the theorem for the objective tensor $\T$ and then apply it to $\Stilde$. \begin{theorem}[{T. G. Kolda, E. Acar, D. M. Dunlavy, 2011, \cite{acar2011}, \cite[Theorem 4.1.2]{diniz2019}}]\label{thm:gradient} The partial derivatives of $F$ with respect to $\W^{(\ell)}$ are given by $$ \frac{\partial F}{\partial\W^{(\ell)}}(\w)=\W^{(\ell)}\Pi^{(\ell)}-\T_{(\ell)}\left(\W^{(L)}\odot\cdots\odot\W^{(\ell+1)}\odot\W^{(\ell-1)}\odot\cdots\odot\W^{(1)}\right). $$ \end{theorem} Applying the theorem with $\Stilde$ in place of $\T$, the blocks of the gradient are obtained by vectorizing these matrices. The first term is inexpensive to compute once the Gram products are available. Let $$ \mathbf{N}^{(\ell)}=\Stilde_{(\ell)}\left(\W^{(L)}\odot\cdots\odot\W^{(\ell+1)}\odot\W^{(\ell-1)}\odot\cdots\odot\W^{(1)}\right), $$ whose entries are given by $$ n^{(\ell)}_{i_\ell r}=\sum_{i_j,\ j\ne\ell}\tilde{s}_{i_1\ldots i_L}\prod_{j\ne\ell}w^{(j)}_{i_jr}. $$ Independent computation of the $\mathbf N^{(\ell)}$ requires $L$ contractions with all entries of $\Stilde$. We replace these with two large contractions and smaller ones. Use the partition $A,B$ and the matrices $\mathbf{K}_A,\mathbf{K}_B$ of Section~\ref{sec:split}. Let $\tilde{\mathbf{S}}$ be the matrix obtained by grouping the indexes of $\Stilde$ into $p$ rows and $q$ columns, corresponding to these groups of modes. Define $$ \begin{aligned} \mathbf{C}_A&=\tilde{\mathbf{S}}\mathbf{K}_B\in\R^{p\times R},\\ \mathbf{C}_B&=\tilde{\mathbf{S}}^T\mathbf{K}_A\in\R^{q\times R}. \end{aligned} $$ For instance, the entry of $\mathbf{C}_A$ corresponding to the indexes $i_A,r$ is $$ (\mathbf{C}_A)_{i_A r}=\sum_{i_B}\tilde{s}_{i_A i_B}\prod_{j\in B}w^{(j)}_{i_jr}. $$ Therefore, for every $\ell\in A$, we obtain $$ \begin{aligned} n^{(\ell)}_{i_\ell r}&=\sum_{i_{A\setminus\{\ell\}}} (\mathbf{C}_A)_{i_A r}\prod_{j\in A\setminus\{\ell\}}w^{(j)}_{i_jr}. \end{aligned} $$ The analogous formula using $\mathbf C_B$ holds for $\ell\in B$. Substituting the definition of $\mathbf C_A$ gives exactly the sum defining $n^{(\ell)}_{i_\ell r}$. Algorithm~\ref{alg:gradient} uses matrix multiplication for the two large products and Einstein summation for the remaining contractions. \begin{algorithm}[htbp] \small \caption{Gradient using two partial contractions}\label{alg:gradient} \begin{algorithmic}[1] \Require $\Stilde$, factors $\W^{(\ell)}$, Gram products $\Pi^{(\ell)}$ \Ensure Gradient blocks $\partial F/\partial\W^{(\ell)}$ for $\ell=1,\ldots,L$ \State Form the two partial Khatri-Rao products $\mathbf K_A,\mathbf K_B$ \State View $\Stilde$ as $\tilde{\mathbf S}\in\R^{p\times q}$ \State $\mathbf C_A\gets\tilde{\mathbf S}\mathbf K_B$, $\mathbf C_B\gets\tilde{\mathbf S}^T\mathbf K_A$ \Comment{Two large matrix products} \For{$D\in\{A,B\}$} \For{$\ell\in D$} \State $n^{(\ell)}_{i_\ell r}\gets\displaystyle\sum_{i_{D\setminus\{\ell\}}}(\mathbf C_D)_{i_D r}\prod_{j\in D\setminus\{\ell\}} w^{(j)}_{i_jr}$ \State $\partial F/\partial\W^{(\ell)}\gets\W^{(\ell)}\Pi^{(\ell)}-\mathbf N^{(\ell)}$ \EndFor \EndFor \State \Return all gradient blocks \end{algorithmic} \end{algorithm} As an example, consider a fourth order tensor. We have $$ \begin{aligned} c^{(A)}_{i_1i_2r}&=\sum_{i_3,i_4}\tilde{s}_{i_1i_2i_3i_4}w^{(3)}_{i_3r}w^{(4)}_{i_4r},\\ n^{(1)}_{i_1r}&=\sum_{i_2}c^{(A)}_{i_1i_2r}w^{(2)}_{i_2r},\qquad n^{(2)}_{i_2r}=\sum_{i_1}c^{(A)}_{i_1i_2r}w^{(1)}_{i_1r}. \end{aligned} $$ Modes 3 and 4 are treated in the same manner with the other partial contraction. Figure~\ref{fig:gradient} illustrates the general procedure. \begin{figure}[htbp] \centering \includegraphics[width=0.93\textwidth]{figures/gradient_contractions.png} \caption{Computation of all mode contractions of a fourth order tensor. Two matrix products with the compressed tensor are followed by contractions of the two smaller matrices. The Gram-dependent part of the gradient is not shown.} \label{fig:gradient} \end{figure} The independent matrix products require approximately $2LRpq$ flops, whereas the two large products require $4Rpq$. Constructing the Khatri-Rao products and contracting within the groups adds $\mathcal O(R(|A|^2p+|B|^2q))$ work, bounded by $\mathcal O(LR(|A|p+|B|q))$. For fixed $L$ and equal dimensions $n$, the latter contractions cost $\mathcal O(Rn^{\lceil L/2\rceil})$. The Gram-dependent term costs $\mathcal O(R^2\sum_\ell\tilde R_\ell)$ given the Gram products. The two partial products and contractions occupy $\mathcal O(R(p+q))$ entries, whereas an independent mode contraction may require a Khatri-Rao product with $R\max_\ell\prod_{j\ne\ell}\tilde R_j$ entries. The factor $L/2$ compares only the arithmetic work of the large matrix products, not execution times. The contraction $\mathbf N^{(\ell)}$ is also known as the matricized tensor times Khatri-Rao product (MTTKRP). Kosmacher, Phipps and Rajamanickam \cite{kosmacher2025} propose a matrix-free blocked MTTKRP in GenTen, with memory cost $\mathcal O(R\sum_{\ell=1}^L I_\ell)$ apart from the input tensor. Our approach retains two partial Khatri-Rao products and exploits their reuse across all $L$ modes at a dGN iteration. Moreover, compression replaces $I_\ell$ by $\tilde R_\ell$. The authors observe that matrix multiplication can be faster when its intermediates fit in memory. Our approach therefore offers a different compromise between storage and arithmetic efficiency. \subsection{Avoiding the computation of all unfoldings} The independent mode contractions of \cite[Section 4.1.3.3]{diniz2019} use all $L$ unfoldings of the objective tensor. The previous implementation constructs them before the dGN iterations. Storing all these matrices requires $L\prod_\ell \tilde{R}_\ell$ entries. The two-group gradient computation does not require them. We only need a matrix representation of the compressed tensor and the representation used in the computation of the residual. For residual evaluation a column-major tensor admits a view of its first unfolding, and a row-major tensor admits the corresponding view after reversing the modes and factors. In the gradient computation the grouping instead follows the row-major ordering, reversing the modes and factors for a column-major tensor. Reversing the modes leaves the relative error unchanged and permutes the gradient blocks accordingly. Noncontiguous tensors may require copies. Combining the two-group gradient computation with blockwise residual evaluation reduces the memory requirements of dGN when the normal equations are solved by CG or a direct linear solver: neither all the unfoldings nor the complete tensor approximation need be stored at each nonlinear iteration. \subsection{Small nonlinear problems and stopping conditions} Let $d=R\sum_{\ell=1}^L\tilde R_\ell$ be the number of parameters. We extend the special stopping criteria for small problems from $d\leq100$ to $d\leq1000$, allowing more iterations when they are inexpensive. For these problems we use a relative error tolerance of $10^{-16}$, disable the step, improvement, gradient and CG tolerances, and do not cap the number of CG iterations by $d$. The maximum number of nonlinear iterations still applies. \section{Initialization} The initialization of the factors is of particular interest in the CPD problem because different starting points may lead to different stationary points. As in \cite[Section 4.1.2]{diniz2019}, we generate the factors $\W^{(\ell)}\in\R^{\tilde{R}_\ell\times R}$ for the truncated core $\Stilde\in\R^{\tilde{R}_1\times\cdots\times\tilde{R}_L}$. The tensor given by a candidate set of factors is denoted by $\Shat$. \subsection{Scaling of the random initialization} The standard Gaussian initialization of \cite[Section 4.1.2]{diniz2019} does not account for the scale of $\Stilde$. We now choose its variance according to the norm, dimensions and prescribed rank. Let all entries of the factor matrices be independent centered Gaussian random variables with variance $\alpha^2$. For each entry of the tensor $\Shat$ given by these factors we have $$ \mathbb{E}[\hat{s}_{i_1\ldots i_L}]=0. $$ The expected value of its square is $$ \begin{aligned} \mathbb{E}[\hat{s}_{i_1\ldots i_L}^2] &=\sum_{r,s=1}^R\mathbb{E}\left[\prod_{\ell=1}^L w^{(\ell)}_{i_\ell r}w^{(\ell)}_{i_\ell s}\right]\\ &=\sum_{r=1}^R\prod_{\ell=1}^L\mathbb{E}\left[(w^{(\ell)}_{i_\ell r})^2\right] =R\alpha^{2L}. \end{aligned} $$ The mixed terms vanish by independence and the zero means of the random variables. Summing over all coordinates, it follows that $$ \mathbb{E}[\|\Shat\|^2]=R\alpha^{2L}\prod_{\ell=1}^L \tilde{R}_\ell. $$ We would like this expected value to be equal to $\|\Stilde\|^2$. Therefore we choose $$ \alpha=\left(\frac{\|\Stilde\|^2}{R\prod_{\ell=1}^L \tilde{R}_\ell}\right)^{1/(2L)}. $$ Multiplying standard Gaussian entries by $\alpha$ gives the desired expected squared norm. Norm balancing preserves the tensor, while subsequent perturbations or prescribed symmetry and norms need not. The expectation does not guarantee a better individual starting point. \subsection{Evaluating candidates in the smart random initialization} The initialization proposed in \cite[Section 4.1.2]{diniz2019} constructs approximations from entries of the compressed tensor near its first entry. In the smart random initialization, the indexes are sampled from a distribution favoring small indexes. We retain this sampling strategy but replace the reconstruction of each candidate with a score computed directly from its sampled coordinates. The sampling distribution is motivated by Theorems~\ref{thm:mlsvd} and~\ref{thm:mlsvd-hyperslices}, which describe the energy distribution of the hyperslices of the core tensor of a full MLSVD. Although this property concerns entire hyperslices rather than individual entries, it motivates the choice of sampling distribution. Each candidate generated in this way is a sum of $R$ terms of the form $$ \tilde{s}_{i_1\ldots i_L}\mathbf{e}_{i_1}\otimes\cdots\otimes\mathbf{e}_{i_L}. $$ where $\mathbf{e}_{i_\ell}$ is a canonical basis vector of $\R^{\tilde{R}_\ell}$. The same tuple can be sampled more than once. Let $m_i$ be the number of times that the tuple $i=(i_1,\ldots,i_L)$ is selected in a given candidate. Then the tensor associated with this candidate, $\Shat$, has entry $\hat{s}_i=m_i \tilde{s}_i$ at index $i$. In particular, if $m_i=0$, its corresponding entry is zero. The squared approximation error given by this procedure is $$ \|\Stilde-\Shat\|^2 = \sum_i(1-m_i)^2\tilde{s}_i^2 = \|\Stilde\|^2-\sum_{i:m_i>0}m_i(2-m_i)\tilde{s}_i^2. $$ Thus, to find the candidate with the smallest error, it suffices to find the candidate for which the quantity $$ \sum_{i:m_i>0}m_i(2-m_i)\tilde{s}_i^2 $$ is largest. Notice that selecting an entry once reduces the squared error by $\tilde{s}_i^2$. Selecting it twice gives the same squared error as not selecting it at all, while selecting it three or more times increases the squared error. Hence we must take the multiplicity of each entry into account. Algorithm~\ref{alg:smart-random} compares candidates by counting the multiplicities instead of reconstructing the approximations. The expected cost of evaluating each candidate is $\mathcal{O}(LR)$ using a hash table, excluding the generation of the factors and the sampled indexes. In contrast, constructing the tensor approximation and computing its error takes $\mathcal{O}(R\prod_\ell \tilde{R}_\ell)$ arithmetic operations. The number of candidates used by Tensor Fox is $$ 1+\left\lfloor\sqrt{\prod_{\ell=1}^L \tilde{R}_\ell}\right\rfloor. $$ In exact arithmetic, maximizing the score is equivalent to minimizing the residual norm over the candidates, before the small perturbations subsequently applied to the chosen factors. The sampling distribution is unchanged. \begin{algorithm}[htbp] \small \caption{Selection of a smart random initialization}\label{alg:smart-random} \begin{algorithmic}[1] \Require $\Stilde$, CPD rank $R$ \Ensure Factors of the best sampled candidate before perturbation \State $N_s\gets1+\lfloor\sqrt{\prod_{\ell=1}^L\tilde R_\ell}\rfloor$, $v_*\gets-\infty$ \For{$j=1,\ldots,N_s$} \State Sample $R$ multi-indexes and form their rank one terms \State Count each distinct sampled multi-index $i$ with multiplicity $m_i$ \State $v\gets\sum_{i:m_i>0}m_i(2-m_i)\tilde s_i^2$ \Comment{No tensor reconstruction} \If{$v>v_*$} \State $v_*\gets v$, retain the current candidate factors \EndIf \EndFor \State \Return the retained factors \end{algorithmic} \end{algorithm} \subsection{Sorting the entries in the smart initialization} We retain the procedure for assembling the initial rank one terms but replace the repeated searches for the largest remaining entry with a single sort. The smart initialization first considers the $R$ entries of $\Stilde$ with largest absolute values, provided $R$ does not exceed the number of entries. The selection is deterministic. If some of these entries have all but one index in common, it may be possible to include them in the same rank one term. This allows us to include more entries of the tensor using the same number of rank one terms. For instance, two entries differing only in the first index give the tensor $$ (\tilde{s}_{i_1i_2\ldots i_L}\mathbf{e}_{i_1}+\tilde{s}_{j_1i_2\ldots i_L}\mathbf{e}_{j_1}) \otimes\mathbf{e}_{i_2}\otimes\cdots\otimes\mathbf{e}_{i_L}, $$ which has rank at most one. In the thesis approach, the ordering repeatedly searches for the largest remaining absolute value, requiring $\mathcal O((\prod_\ell\tilde R_\ell)^2)$ comparisons. We instead sort linear indexes by decreasing absolute value, breaking ties by the original index. For large tensors, we use a radix sort based on the magnitudes of the entries, partitioning the indexes into buckets that can be sorted in parallel. This avoids allocating a copy of the tensor or a full array of sorting keys. For small tensors we use a conventional stable indirect sort. With fixed-width keys, radix sorting requires work and storage of order $\mathcal O(\prod_\ell\tilde R_\ell)$. Only the $t$ selected indexes are converted into multi-indexes, adding $\mathcal O(Lt)$ operations and entries. These bounds concern the ordering, not the subsequent construction of the rank one terms. \section{The tensor train approach} We retain the approach for computing a CPD through a tensor train decomposition described in \cite[Section 4.6]{diniz2019} and modify the computation of the first core, the number of singular components in subsequent cores and the propagation of the right singular vectors. In the compressed CPD problem we apply the tensor train decomposition (TTD) to $\Stilde$. For simplicity, suppose that none of its dimensions needs to be enlarged to accommodate the prescribed rank $R$. \begin{definition}\label{def:ttd} A \emph{tensor train decomposition} (TTD) represents a tensor approximation $\Shat$ by tensors $\mathcal{G}^{(\ell)}\in\R^{r_{\ell-1}\times \tilde{R}_\ell\times r_\ell}$, $\ell=1,\ldots,L$, called the \emph{cores} of the TTD, where $r_0=r_L=1$. For each $i_\ell$, denote the lateral slice $\mathcal{G}^{(\ell)}_{:i_\ell:}$ by $\mathbf{G}^{(\ell)}(i_\ell)$. The coordinates of $\Shat$ are given by $$ \hat{s}_{i_1\ldots i_L}=\mathbf{G}^{(1)}(i_1)\mathbf{G}^{(2)}(i_2)\cdots\mathbf{G}^{(L)}(i_L). $$ The right hand side is a product of matrices, the first having only one row and the last only one column, so the result is a scalar. The numbers $r_\ell$ are called the \emph{TT-ranks}. \end{definition} The last index of each core is shared with the first index of the next core, as illustrated in Figure~\ref{fig:tt-network} (see \cite[Section 4.6.1]{diniz2019}). \begin{figure}[htbp] \centering \includegraphics[width=0.95\textwidth]{figures/tt_network.png} \caption{Tensor train network. The circles represent the shared indexes $j_1,\ldots,j_{L-1}$, and $i_1,\ldots,i_L$ are the indexes of the tensor (from \cite[Figure 4.22]{diniz2019}).} \label{fig:tt-network} \end{figure} \subsection{Computing the cores using truncated SVDs} Recall that computing the cores of a tensor train decomposition by successive singular value decompositions is known as \emph{TT-SVD} \cite{oseledets2011}. At each step of the TT-SVD, a partially decomposed tensor is reshaped into a matrix $\mathbf{A}$ with $r_{\ell-1}\tilde{R}_\ell$ rows and $\prod_{j=\ell+1}^L \tilde{R}_j$ columns, where $r_0=1$ and $r_{\ell-1}$ is the preceding tensor train rank. Let $m,n$ be the dimensions of $\mathbf{A}$ and $d_\ell=\min\{m,n\}$. Algorithm 4.6.1 of \cite{diniz2019} computes an SVD at each step. In the previous implementation, we request all $d_\ell$ singular components from a randomized SVD with no power iterations and then retain the first $r_\ell$. For the first core ($\ell=1$), the current implementation forms $\mathbf{A}\mathbf{A}^T$ and computes its eigendecomposition. It retains the $r_1$ eigenvectors associated with the largest eigenvalues, forming a matrix $\U_{r_1}$, and propagates $\U_{r_1}^T\mathbf{A}$. This costs $\mathcal{O}(m^2n+m^3)$ arithmetic operations and $\mathcal{O}(m^2+r_1n)$ additional storage. The use of squared singular values may reduce the relative accuracy of small singular components. The Gram approach is suited to a wide matrix, but need not be cheaper for a tall matrix. For the subsequent cores, the number $h$ of requested singular components is determined by the same heuristic used in the MLSVD, given by \eqref{eq:svd-components}, with $I_\ell$ and $R$ replaced by $d_\ell$ and $r_\ell$, respectively. The randomized SVD is performed without power iterations. The cost of the randomized SVD with sampling width $b$, including oversampling vectors, is $\mathcal{O}(mnb+(m+n)b^2)$. Therefore, when $r_\ell\ll d_\ell$, this approach can substantially reduce the computational cost without changing the prescribed tensor train ranks. When the compressed tensor is enlarged before the TT-SVD, its effective mode dimensions are $\max\{R,\tilde{R}_\ell\}$ instead of $\tilde{R}_\ell$. For a TT-SVD in the original space without enlargement we use $I_\ell$. In the CPD tensor train approach the internal TT-ranks are prescribed as $r_\ell=R$, for $1\leq\ell{\raggedright\arraybackslash}p{0.29\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}@{}} \toprule \textbf{Operation} & \textbf{Thesis approach} & \textbf{Tensor Fox 2.0.0} \\ \midrule \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Tensor compression}} \\ \addlinespace[1pt] Dense randomized SVD (one matrix) & $\mathcal O((m+n)b_o+b_o^2)$ & $\mathcal O((m+n)b_n+b_n^2)$ \\ Separate triangular QR output ($h=\min\{m,b\}$) & $\mathcal O(hb)$ & $\mathcal O(1)$ \\ Foldback (one tensor) & $\mathcal O(J)$ & $\mathcal O(L)$ \\ Explicit Gram construction & $\mathcal O(N+I_\ell+g_\ell)$ & $\mathcal O(N+I_\ell+g_\ell)$ \\ Randomized SVD of explicit Gram & $\mathcal O(I_\ell b_o+b_o^2)$ & $\mathcal O(I_\ell b_e+b_e^2)$ \\ Sparse multilinear multiplication & $\mathcal O(C+N(D+L))$ & $\mathcal O\left(\begin{gathered}C+LN+M_g\\+\tilde R_1 n_1\end{gathered}\right)$ \\ \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Optimization and initialization}} \\ \addlinespace[1pt] Dense CPD reconstruction (first unfolding) & $\mathcal O(C+RK)$ & $\mathcal O(C+R(p+q))$ \\ Dense CPD residual & $\mathcal O(C+RK)$ & $\mathcal O(R(p+q)+b_rq)$ \\ Unfoldings before dGN (CG) & $\mathcal O(LC)$ & $\mathcal O(L)$ \\ Dense gradient (given Gram products) & $\mathcal O(R(H+D))$ & $\mathcal O(R(p+q+D))$ \\ Random initialization & $\mathcal O(RD)$ & $\mathcal O(RD)$ \\ Smart random scoring (one candidate) & $\mathcal O(C+RK)$ & $\mathcal O(LR)$ \\ Smart entry ordering & $\mathcal O(LC)$ & $\mathcal O(C+Lt)$ \\ \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Tensor train}} \\ \addlinespace[1pt] TT-SVD (first core, with propagation) & $\mathcal O((m+n)d)$ & $\mathcal O(m^2+r_1n)$ \\ TT-SVD (later cores, SVD only) & $\mathcal O((m+n)d)$ & $\mathcal O((m+n)b_n+b_n^2)$ \\ TT-SVD propagation (later cores) & $\mathcal O(d^2+dn)$ & $\mathcal O(r_\ell n)$ \\ \bottomrule \end{tabular} \caption{Asymptotic memory costs of the in-memory operations, in array entries. The disk-backed and implicit Gram paths are detailed in Table~\ref{tab:sparse-compression-costs}.} \label{tab:memory-costs} \end{table} \begin{table}[p] \centering \small \setlength{\tabcolsep}{4pt} \renewcommand{\arraystretch}{1.12} \begin{tabular}{@{}>{\raggedright\arraybackslash}p{0.29\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}@{}} \toprule \textbf{Operation} & \textbf{Thesis approach} & \textbf{Tensor Fox 2.0.0} \\ \midrule \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Tensor compression}} \\ \addlinespace[1pt] Dense randomized SVD (one matrix) & $\mathcal O\left(\begin{gathered}mnb_o\\+(m+n)b_o^2\end{gathered}\right)$ & $\mathcal O\left(\begin{gathered}mnb_n\\+(m+n)b_n^2\end{gathered}\right)$ \\ Economic QR (basis construction) & $\mathcal O(mbh+mh^2)$ & $\mathcal O(mbh+mh^2)$ \\ Foldback (one tensor) & $\mathcal O(J)$ & $\mathcal O(L)$ \\ Explicit Gram construction & $\mathcal O\left(\begin{gathered}N\log(N+1)\\+I_\ell+f_\ell\end{gathered}\right)$ & $\mathcal O\left(\begin{gathered}N\log(N+1)\\+I_\ell+f_\ell\end{gathered}\right)$ \\ Randomized SVD of explicit Gram & $\mathcal O\left(\begin{gathered}(g_\ell+I_\ell)b_o\\+I_\ell b_o^2+b_o^3\end{gathered}\right)$ & $\mathcal O\left(\begin{gathered}(g_\ell+I_\ell)b_e\\+I_\ell b_e^2+b_e^3\end{gathered}\right)$ \\ Sparse multilinear multiplication & $\mathcal O(A_o)$ & $\mathcal O(A_g+Q_g)$ \\ \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Optimization and initialization}} \\ \addlinespace[1pt] Dense CPD reconstruction (first unfolding) & $\mathcal O(RC)$ & $\mathcal O(RC)$ \\ Dense CPD residual & $\mathcal O(RC)$ & $\mathcal O(RC)$ \\ Unfoldings before dGN (CG) & $\mathcal O(LC)$ & $\mathcal O(L)$ \\ Dense gradient (given Gram products) & $\mathcal O(LRC+R^2D)$ & $\mathcal O\left(\begin{gathered}RC+R^2D\\+LR(|A|p+|B|q)\end{gathered}\right)$ \\ Random initialization & $\mathcal O(RD)$ & $\mathcal O(C+RD)$ \\ Smart random scoring (one candidate) & $\mathcal O(RC)$ & $\mathcal O(LR)$ \\ Smart entry ordering (radix path) & $\mathcal O(C^2+LC)$ & $\mathcal O(C+Lt)$ \\ \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Tensor train}} \\ \addlinespace[1pt] TT-SVD (first core, with propagation) & $\mathcal O(mnd)$ & $\mathcal O(m^2n+m^3)$ \\ TT-SVD (later cores, SVD only) & $\mathcal O(mnd)$ & $\mathcal O\left(\begin{gathered}mnb_n\\+(m+n)b_n^2\end{gathered}\right)$ \\ TT-SVD propagation (later cores) & $\mathcal O(d^2n)$ & $\mathcal O(r_\ell n)$ \\ \bottomrule \end{tabular} \caption{Asymptotic computational costs of the in-memory operations. Sparse preparation and grouping include comparison-sort bounds.} \label{tab:costs} \end{table} The grouped contraction bound $M_g$ describes the unblocked algorithm. When blocking starts at mode $m$, the current partial contraction is retained and subsequent dense intermediates require \[ \mathcal O\left(n_m h+b_*\max_{1\leq\ell\leq m}(d_{\ell+1}+d_\ell)\right) \] entries, with $h$, $d_\ell$ and the largest block width $b_*$ as in Section~\ref{sec:group-blocked}. Core storage and the selected submatrix of the first compression matrix are additional. The grouping maps and coordinates require at most $\mathcal O(LN+\sum_{\ell=2}^m\ell n_\ell)$ entries. Blocking reduces dense temporary storage, not the arithmetic bound, and does not guarantee that every allocation fits in RAM. Foldback and unfolding bounds assume that views can be used. A copy instead requires $\mathcal O(J)$ or $\mathcal O(C)$ entries and operations. Smart random scoring refers to one candidate and excludes its generation. For smart entry ordering, $t\leq C$ denotes the number of selected indexes converted into multi-indexes. The cost of joining rank one terms is not included. Random initialization includes the norm computation. The TT-SVD bounds distinguish the first Gram-based step, subsequent randomized SVDs and propagation. If a reshape requires copying its input matrix, its $mn$ entries must be counted separately. The spectral rows in Tables~\ref{tab:memory-costs} and~\ref{tab:costs} refer to the SVD itself, before energy-based truncation. Suppose all compressed dimensions are equal to $n\geq2$. Independent mode contractions require $\mathcal O(LRn^L)$ work in the gradient, while the two-group approach uses two large products followed by smaller contractions. Auxiliary residual arrays decrease from $\mathcal O(n^L+Rn^{L-1})$ to $\mathcal O(Rn^{\lceil L/2\rceil}+b_r q)$, without changing the arithmetic order of residual evaluation. The Gram products and CG arrays require $\mathcal O(L^2R^2+RD)$ entries in both approaches, in addition to the objective tensor and the arrays listed in the tables. The objective tensor itself still has $n^L$ entries. The bound for the preparation of unfoldings assumes an inner method that does not use ALS. \subsection{In-memory versus out-of-core sparse compression} Table~\ref{tab:sparse-compression-costs} compares the usual explicit in-memory path with the disk-backed implicit approach. The explicit path uses $LN$ temporary entries to linearize coordinates, and retains the first unfolding during compression of the other modes. In the implicit approach, only the current sparse window is accessed. Let $B$ denote its number of entries. The explicit randomized SVD of the Gram matrix uses six sparse matrix products, including the projection. The implicit eigendecomposition uses four Gram products, each with two passes through the fibers. Let $N_*$ and $c_*$ be the maximum numbers of entries and fibers per Gram block and $s$ the number of blocks. The auxiliary sparse workspace per product is $$ \mathcal O(LB+N_*+a_\ell+c_*b_i), $$ and the direct construction of the traversal indexes costs $\mathcal O(LN+sa_\ell)$ operations. The table includes this work. Long fibers are processed in bounded windows. Energy-based truncation may require $\mathcal O(k_\ell^2)$ operations from repeated tail sums. A mode represented by a dense identity requires no sorting or spectral computation. For external sorting, $s_0=\lceil N/B_0\rceil$ is the number of initial runs. With $w$ workers the auxiliary storage is $\mathcal O(wLB_0+s_0)$ entries. A merge of $q_m$ runs requires $\mathcal O(L(q_m+1)h_m+s_0)$ entries. For a fixed fan-in $q_m\geq2$, the number of passes is at most $u=\lceil\log_{q_m}s_0\rceil$. Each tournament selection costs $\mathcal O(L\log q_m)$ operations. Including the buffer refills, a conservative bound for external sorting with the bounded fan-in used here is $\mathcal O(LN\log(N+1))$. Temporary disk storage is $\mathcal O(LN)$ entries and record traffic per compressed mode is $\mathcal O(LN(1+u))$ entries, including the Gram scans. The fan-in can vary between passes. For order three let $d_*$ be the largest number of active rows in a subdivision and $a,b,c$ the mode permutation of Section~\ref{sec:outcore}. Let $t_a,t_b$ be the maximum widths of the retained-column blocks and $v_*$ the largest number of active columns gathered from $\Utilde^{(b)}$ when column blocking is necessary. Up to bounded small broadcast temporaries, the dense workspace of a contraction is of order $$ W_3=d_*(t_a+t_b)+v_*t_b+t_at_b. $$ For a slice subdivision with $m_{p,i_c}$ nonzeros and $d_{p,i_c}$ active rows, the arithmetic cost of the two contractions and accumulation is bounded by $$ A_3=\sum_{p,i_c}\left(m_{p,i_c}\tilde R_b+d_{p,i_c}\tilde R_a\tilde R_b+C\right). $$ The sum includes each active-row subdivision separately. When the free mode uses the identity, $C$ in this sum can be replaced by $\tilde R_a\tilde R_b$. Blocking the compressed dimensions does not change these arithmetic counts, apart from preparation and repeated gathers. For higher orders the direct accumulation costs $\mathcal O(A_o)$ arithmetic operations. Its per-thread workspace is of order $D+L$, in addition to the shared bases, core and current window. We assume a fixed number of threads in the table. The main distinction is that the input and sorted records need not be resident in RAM. The principal sparse arithmetic of the implicit approach is linear in $N$ at fixed sampling width, whereas explicit Gram formation also depends on the fiber occupancies. The dense factors and the core remain necessary in both paths. External storage removes the memory barrier associated with the sparse representation and the Gram matrix, but not the one associated with the dense compressed problem. \begin{table}[p] \centering \footnotesize \setlength{\tabcolsep}{4pt} \renewcommand{\arraystretch}{1.16} \begin{tabular}{@{}>{\raggedright\arraybackslash}p{0.29\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}@{}} \toprule \textbf{Stage} & \textbf{In-memory (explicit Gram)} & \textbf{Out-of-core (implicit Gram)} \\ \midrule \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Memory (array entries)}} \\ \addlinespace[1pt] Original sparse coordinates and values & $\mathcal O((L+1)N)$ & On disk, $\mathcal O((L+1)B)$ block access \\ Unfolding / fiber preparation & $\mathcal O(LN+I_\ell)$ & $\mathcal O\left(\begin{gathered}LwB_0+L(q_m+1)h_m\\+s_0+I_\ell\end{gathered}\right)$ \\ Explicit Gram matrix & $\mathcal O(g_\ell+I_\ell)$ & Not formed \\ Spectral step (one mode) & $\mathcal O(I_\ell b_e+b_e^2)$ & $\mathcal O\left(\begin{gathered}a_\ell b_i+b_i^2\\+I_\ell\tilde R_\ell+I_\ell\end{gathered}\right)$ \\ Retained basis allocations & $\mathcal O(F_e)$ & $\mathcal O(F_U)$ \\ Gram block workspace & --- & $\mathcal O(LB+N_*+a_\ell+c_*b_i)$ \\ Dense core & $\mathcal O(C)$ & $\mathcal O(C)$ \\ Core contraction workspace ($L=3$) & $\mathcal O\left(\begin{gathered}LN+M_g\\+\tilde R_1n_1\end{gathered}\right)$ & $\mathcal O(B_3+W_3)$ \\ Higher orders ($L>3$) & Same grouped bound & $\mathcal O(LB_c+D+L)$ \\ \addlinespace[2pt] \multicolumn{3}{@{}l}{\textit{Computational work}} \\ \addlinespace[1pt] Unfolding / fiber preparation & $\mathcal O\left(\begin{gathered}LN+L^2+I_\ell\\+N\log(N+1)\end{gathered}\right)$ & $\mathcal O(LN\log(N+1)+I_\ell)$ \\ Explicit Gram construction & $\mathcal O\left(\begin{gathered}N\log(N+1)\\+I_\ell+f_\ell\end{gathered}\right)$ & Not formed \\ One Gram product & $\mathcal O((g_\ell+I_\ell)b_e)$ & $\mathcal O((L+b_i)N+a_\ell(b_i+s))$ \\ Full spectral step (one mode) & $\mathcal O\left(\begin{gathered}(g_\ell+I_\ell)b_e\\+I_\ell b_e^2+b_e^3+k_\ell^2\end{gathered}\right)$ & $\mathcal O\left(\begin{gathered}(L+b_i)N+a_\ell(b_i^2+s)\\+b_i^3+I_\ell\tilde R_\ell+I_\ell+k_\ell^2\end{gathered}\right)$ \\ Core construction ($L=3$) & $\mathcal O(A_g+Q_g)$ & $\mathcal O\left(\begin{gathered}A_3+N\log(B_3+1)+C\end{gathered}\right)$ \\ Higher orders ($L>3$) & Same grouped bound & $\mathcal O(A_o+C)$ \\ \bottomrule \end{tabular} \caption{Sparse MLSVD in Tensor Fox 2.0.0. Mode-dependent costs are evaluated separately for each compressed mode. The core is constructed once. See the text for block workspace and disk traffic.} \label{tab:sparse-compression-costs} \end{table} \subsection{Examples of computational and memory costs} The following examples substitute concrete dimensions and sparse patterns into the preceding bounds. They give storage and arithmetic counts, not measured peak memory or execution times. A multiply-add is counted as one contribution (two floating-point operations). In the examples, a MiB is $2^{20}$ bytes and a GiB is $2^{30}$ bytes. \subsubsection{Effect of the distribution of nonzeros} Consider one unfolding with $I_\ell=a_\ell=10^4$, $N=10^6$ and sampling width $b_e=b_i=110$. Partition its rows into disjoint groups of $d$ rows. Each nonempty fiber has nonzeros on exactly one group, and each group occurs in $100$ different fibers. With positive entries the Gram matrix has $g_\ell=I_\ell d$ nonzeros and its construction involves $f_\ell=Nd$ products. For the storage estimates we assign eight bytes to each value and four to each CSR index, giving $12g_\ell+4(I_\ell+1)$ bytes. Table~\ref{tab:example-gram} compares that storage and the arithmetic work of one Gram product. The sampled matrix and output, which both approaches require, are not included in the Gram storage column. \begin{table}[htbp] \centering \small \setlength{\tabcolsep}{4pt} \begin{tabular}{rrrrr} \toprule \shortstack{Entries per\\fiber $d$} & \shortstack{Gram\\storage (MiB)} & \shortstack{Gram\\construction} & \shortstack{Explicit\\product} & \shortstack{Implicit\\product} \\ \midrule $1$ & 0.15 & $10^{6}$ & $1.1\cdot10^{6}$ & $2.2\cdot10^{8}$ \\ $100$ & 11.48 & $10^{8}$ & $1.1\cdot10^{8}$ & $2.2\cdot10^{8}$ \\ $10\,000$ & 1,144.45 & $10^{10}$ & $1.1\cdot10^{10}$ & $2.2\cdot10^{8}$ \\ \bottomrule \end{tabular} \caption{One mode with fixed $N$ and different fiber occupancies. The last three columns count arithmetic contributions, not elapsed time. The implicit approach does not allocate the Gram matrix.} \label{tab:example-gram} \end{table} In all three cases the original tensor has the same number of nonzeros and the sampled matrix has $10^4\cdot110$ entries, or approximately $8.39$ MiB. A stored Gram matrix is very efficient for the first pattern. For the third pattern its construction and storage become substantially more expensive, while the implicit products retain the same arithmetic count. This illustrates why $N$ alone does not predict the cost of explicit sparse compression. \subsubsection{The dense allocations in out-of-core compression} Let $L=3$ and $(I_1,I_2,I_3)=(50,250000,250000)$. Assume all rows are active, the first mode is represented by the identity, and the other two modes retain all $k$ computed components, so that $(\tilde R_1,\tilde R_2,\tilde R_3)=(50,k,k)$. We assume that the input occupies $32N$ bytes and that the dense matrices use double precision. Table~\ref{tab:example-bases} varies $N$ at fixed $k$ and then varies $k$ at fixed $N$. It gives the memory of the retained bases, one sampled matrix and the core, along with the arithmetic contributions of the eight implicit Gram products (four for each of the two compressed modes). The number of contributions is $16N(k+10)$. This count excludes factorizations, sorting, core construction and other overhead. \begin{table}[htbp] \centering \small \setlength{\tabcolsep}{4pt} \begin{tabular}{rrrrrrr} \toprule $N$ & $k$ & \shortstack{Input\\(GiB)} & \shortstack{Bases\\(GiB)} & \shortstack{Sample\\(GiB)} & \shortstack{Core\\(MiB)} & \shortstack{Gram product\\contributions} \\ \midrule $10^{6}$ & $100$ & 0.030 & 0.373 & 0.205 & 3.81 & $1.76\cdot10^{9}$ \\ $10^{7}$ & $100$ & 0.298 & 0.373 & 0.205 & 3.81 & $1.76\cdot10^{10}$ \\ $10^{8}$ & $100$ & 2.980 & 0.373 & 0.205 & 3.81 & $1.76\cdot10^{11}$ \\ $10^{8}$ & $500$ & 2.980 & 1.863 & 0.950 & 95.37 & $8.16\cdot10^{11}$ \\ $10^{8}$ & $1000$ & 2.980 & 3.725 & 1.881 & 381.47 & $1.616\cdot10^{12}$ \\ \bottomrule \end{tabular} \caption{Dense RAM allocations and Gram arithmetic for the implicit compression of a third-order sparse tensor. The input is stored on disk. The sizes are for individual arrays or sets of bases, not simultaneous peak RAM.} \label{tab:example-bases} \end{table} Increasing $N$ from $10^6$ to $10^8$ at fixed $k$ multiplies the sparse input size and the Gram arithmetic by $100$, but does not change the dense allocation sizes in the table. Increasing $k$ from $100$ to $1000$ instead increases the basis memory by approximately one order of magnitude and the core memory by two orders of magnitude. The actual spectral step requires several sampled matrices and smaller dense factors. Thus the memory of a single sample is not a bound on that step's peak storage. \subsubsection{Effect of grouping on the sparse core} Consider a fourth-order tensor with $I_\ell=1000$, $\tilde R_\ell=16$ and $N=10^6$. Suppose that each prefix has ten extensions in the next mode, starting with $n_1=1000$ distinct first coordinates. Then $n_1=10^3$, $n_2=10^4$, $n_3=10^5$ and $n_4=10^6$. The dense core has $16^4=65536$ entries, or $0.5$ MiB. The grouped algorithm requires $A_g=148\,096\,000$ multiply-add contributions. The direct accumulation updates the entire core for every nonzero, requiring $NC=65\,536\,000\,000$ multiply-add contributions in the last mode alone, in addition to the earlier partial products. \begin{table}[htbp] \centering \small \setlength{\tabcolsep}{4pt} \begin{tabular}{@{}lrrr@{}} \toprule \shortstack[l]{Core construction\\stage} & \shortstack{Input partial\\array (MiB)} & \shortstack{Output partial\\array (MiB)} & \shortstack{Multiply-add\\contributions} \\ \midrule Grouped, mode $4$ & 7.63 & 12.21 & $1.6\cdot10^{7}$ \\ Grouped, mode $3$ & 12.21 & 19.53 & $2.56\cdot10^{7}$ \\ Grouped, mode $2$ & 19.53 & 31.25 & $4.1\cdot10^{7}$ \\ Grouped, mode $1$ & 31.25 & 0.50 & $6.55\cdot10^{7}$ \\ \midrule Grouped, total & \multicolumn{2}{c}{50.78 MiB (largest pair)} & $1.48096\cdot10^8$ \\ Direct, final updates & \multicolumn{2}{c}{0.50 MiB (core)} & $6.5536\cdot10^{10}$ \\ \bottomrule \end{tabular} \caption{Core construction for the specified fourth-order sparse pattern. Grouped array sizes assume no blocking. The direct row is the higher-order disk-backed accumulation, excluding its small coefficient workspace, compression matrices and input block. Coordinate grouping and earlier direct partial products are excluded from the arithmetic counts.} \label{tab:example-grouped} \end{table} In this example, grouping reduces the number of multiply-add contributions by more than two orders of magnitude, but requires larger dense intermediates. Blocked grouping reduces the memory of subsequent contractions while preserving the arithmetic advantage. If the coordinate prefixes were mostly distinct, the arithmetic gain would be smaller. \subsubsection{Dense operations in the compressed space} Let $L=6$, $\tilde R_\ell=16$ and $R=20$. Then $C=16^6$, $K=16^5$ and $p=q=16^3$. The objective tensor occupies $128$ MiB. We use $512$ rows per block, giving a buffer of $16$ MiB in double precision. Table~\ref{tab:example-dense} compares the main auxiliary arrays and the multiply-add contributions of the large matrix products in residual and gradient evaluation. These counts exclude the construction of Khatri-Rao products, the smaller contractions of the two-group gradient and the Gram-dependent gradient terms. \begin{table}[htbp] \centering \small \setlength{\tabcolsep}{4pt} \begin{tabular}{@{}lrrrr@{}} \toprule & \multicolumn{2}{c}{Auxiliary arrays (MiB)} & \multicolumn{2}{c}{Large-product contributions} \\ \cmidrule(lr){2-3}\cmidrule(lr){4-5} Operation & Thesis & Current & Thesis & Current \\ \midrule Residual & 288 & 17.25 & $3.3554\cdot10^8$ & $3.3554\cdot10^8$ \\ Gradient & 160 & 2.50 & $2.0133\cdot10^9$ & $6.7109\cdot10^8$ \\ Unfolding copies & 768 & 0 & --- & --- \\ \bottomrule \end{tabular} \caption{Principal auxiliary arrays and large matrix products in a dense compressed problem. The gradient memory counts one full Khatri-Rao product in the thesis approach and the two partial Khatri-Rao products plus two contractions in the current approach. The unfolding row assumes that only views are needed in the current approach.} \label{tab:example-dense} \end{table} The two-group gradient reduces the arithmetic cost of the large products by a factor of three for this tensor. Residual evaluation saves memory without changing the number of multiply-add contributions. Neither change eliminates the memory required by the objective tensor itself. Finally, the cost of uncompression remains $\mathcal O(R\sum_\ell I_\ell\tilde R_\ell)$ operations and $\mathcal O(R\sum_\ell I_\ell)$ output entries \cite[Section 4.1.4]{diniz2019}. The execution time also depends on compression dimensions, data movement, and the numbers of dGN and CG iterations. \clearpage \section{Computational experiments}\label{sec:experiments} We revisit the benchmark families of \cite[Sections 4.3, 4.5 and 4.6.2]{diniz2019} to compare Tensor Fox 2.0.0 with the implementation associated with the thesis (denoted by Tensor Fox 1.0.0 in the figures). The experiments were performed on an Intel Core i7-9700 processor with 8 cores and 64 GB of RAM. We compute 30 rank-$R$ CPDs per configuration and implementation with the default parameters, except for the choice of method. % Experimental software environment: Python 3.14.4, NumPy 2.4.6, SciPy 1.17.1. \subsection{Third order benchmark tensors} We use the swimmer ($32\times32\times256$, $R=50$), handwritten digits ($20\times20\times5000$, $R=150$) and matrix multiplication tensors ($49\times49\times49$, $R=236$). For the last tensor we follow the construction in \cite[Section 4.3.4]{diniz2019}, with $N=7$ instead of $5$. We also use tensors with collinear factors (swamps) and double bottlenecks with shape $300\times300\times300$ and $R=15$. The parameter $c=0.1,0.5,0.9$ controls the collinearity, with smaller values giving more nearly collinear factors. In the double bottleneck construction only the first two columns of each factor matrix are modified. As in \cite[Sections 4.3.5-4.3.6]{diniz2019}, the noisy input is $\widehat{\T}=\T+0.01 \cdot \mathcal N$, with standard normal entries in $\mathcal N$. We also include the clean cases, that is, noiseless (suffix \texttt{x} in the figures). The error in both cases is evaluated by reconstruction against the noiseless tensor, $$e=\frac{\|\T-\Ttilde\|}{\|\T\|}.$$ The border rank example of the thesis is not included. Figure~\ref{fig:bench-third} shows the distributions of relative errors and execution times over the 30 trials for each tensor, while Table~\ref{tab:bench-third} presents the corresponding medians. The median time decreases in all 15 tensors, with speed-ups between $1.16$ and $2.82$ (median $2.10$ across cases). The swimmer tensor illustrates simultaneous improvements in error and time. The median error decreases in 12 cases, notably in the noiseless swamp with $c=0.9$, from $4.02\times10^{-7}$ to $4.56\times10^{-15}$. For the noisy swamps with $c=0.1$ and $0.5$ the errors increase, while for the double bottleneck with $c=0.1$ the differences are negligible. The remaining double bottlenecks still have substantial errors even without noise. \begin{table}[htbp] \centering\small \setlength{\tabcolsep}{3pt} \begin{tabular}{lr rr rr} \toprule & & \multicolumn{2}{c}{Relative error} & \multicolumn{2}{c}{Time (s)}\\ \cmidrule(lr){3-4}\cmidrule(lr){5-6} Tensor & $R$ & \scriptsize{Tensor Fox 1} & \scriptsize{Tensor Fox 2} & \scriptsize{Tensor Fox 1} & \scriptsize{Tensor Fox 2}\\ \midrule Swimmer & 50 & $9.35\times10^{-4}$ & $5.52\times10^{-4}$ & 1.748 & 0.621 \\ Handwritten digits & 150 & 0.08297 & 0.08224 & 6.189 & 3.292 \\ Matrix multiplication & 236 & 0.4051 & 0.3929 & 13.943 & 11.974 \\ Swamp $c=0.1$ & 15 & 0.04767 & 0.05568 & 1.631 & 0.848 \\ Swamp $c=0.5$ & 15 & 0.1052 & 0.1083 & 1.599 & 0.840 \\ Swamp $c=0.9$ & 15 & 0.2421 & 0.2278 & 1.692 & 0.748 \\ Double bottleneck $c=0.1$ & 15 & 0.2379 & 0.2379 & 1.430 & 0.572 \\ Double bottleneck $c=0.5$ & 15 & 0.2883 & 0.2201 & 1.381 & 0.559 \\ Double bottleneck $c=0.9$ & 15 & 0.2381 & 0.2142 & 1.365 & 0.590 \\ Swamp $c=0.1$ (clean) & 15 & 0.003136 & 0.001683 & 1.523 & 1.175 \\ Swamp $c=0.5$ (clean) & 15 & 0.008876 & 0.004396 & 1.757 & 1.055 \\ Swamp $c=0.9$ (clean) & 15 & $4.02\times10^{-7}$ & $4.56\times10^{-15}$ & 1.562 & 0.951 \\ Double bottleneck $c=0.1$ (clean) & 15 & 0.2365 & 0.2365 & 1.381 & 0.658 \\ Double bottleneck $c=0.5$ (clean) & 15 & 0.2189 & 0.2104 & 1.385 & 0.636 \\ Double bottleneck $c=0.9$ (clean) & 15 & 0.2501 & 0.2135 & 1.373 & 0.638 \\ \bottomrule \end{tabular} \caption{Median relative errors and running times over 30 computations of each third order CPD. The clean cases use the tensors without additive noise.} \label{tab:bench-third} \end{table} \subsection{Higher order tensors} Following \cite[Figures 4.23 and 4.24]{diniz2019}, we generate rank-$5$ tensors with standard normal factor entries. First we fix $L=4$ and vary $I_1=\cdots=I_4=n$ for $n=10,20,\ldots,80$. Then we fix $I_1=\cdots=I_L=10$ and vary $L=3,4,\ldots,8$. Each family contains its own fourth order tensor of dimension 10, giving 14 cases in total. Each tensor is reused for 30 computations per method (dGN and TT-CPD). Table~\ref{tab:bench-high} presents the median errors and times for both methods. Figures~\ref{fig:bench-dgn-dim} and \ref{fig:bench-dgn-order} show the results for dGN without the tensor train. For variable $n$, the median errors are of order $10^{-15}$ for both implementations, with speed-ups between $1.91$ and $10.65$. For $L=5,6,7$, the median errors decrease from order $10^{-7}$ to order $10^{-15}$. For $L=8$ the error decreases from $4.53\times10^{-2}$ to $1.34\times10^{-14}$, at comparable times ($16.75$ and $17.15$ seconds). Thus the median time decreases in 13 of the 14 cases. For TT-CPD (Figures~\ref{fig:bench-tt-dim} and \ref{fig:bench-tt-order}), the speed-ups range from $1.28$ to $17.75$. The median error increases in 13 cases, but remains below $10^{-6}$ in all cases. At $n=80$ the median time decreases from $3.25$ to $1.23$ seconds. The times are not comparisons at equal achieved accuracy. Table~\ref{tab:bench-tail} complements the medians with counts of larger errors. Despite the increase in median error, none of the 420 new TT-CPD computations has a reported error above $10^{-5}$, compared with nine for the associated implementation. For dGN the corresponding count decreases from 39 to 11. \begin{table}[p] \centering\small \setlength{\tabcolsep}{4pt} \begin{tabular}{ll rr rr} \toprule & & \multicolumn{2}{c}{Relative error} & \multicolumn{2}{c}{Time (s)}\\ \cmidrule(lr){3-4}\cmidrule(lr){5-6} Family & Parameter & \scriptsize{Tensor Fox 1} & \scriptsize{Tensor Fox 2} & \scriptsize{Tensor Fox 1} & \scriptsize{Tensor Fox 2}\\ \midrule \multicolumn{6}{l}{\textit{dGN}} \\ Variable dimension & $n=10$ & $3.36\times10^{-15}$ & $1.72\times10^{-15}$ & 1.856 & 0.174 \\ & $n=20$ & $2.16\times10^{-15}$ & $1.94\times10^{-15}$ & 1.745 & 0.269 \\ & $n=30$ & $1.98\times10^{-15}$ & $1.91\times10^{-15}$ & 1.790 & 0.228 \\ & $n=40$ & $2.27\times10^{-15}$ & $2.25\times10^{-15}$ & 1.783 & 0.284 \\ & $n=50$ & $1.92\times10^{-15}$ & $1.92\times10^{-15}$ & 1.915 & 0.409 \\ & $n=60$ & $2.08\times10^{-15}$ & $2.17\times10^{-15}$ & 2.081 & 0.579 \\ & $n=70$ & $2.21\times10^{-15}$ & $2.52\times10^{-15}$ & 2.456 & 0.909 \\ & $n=80$ & $2.40\times10^{-15}$ & $2.26\times10^{-15}$ & 2.734 & 1.428 \\ \addlinespace Variable order & $L=3$ & $2.43\times10^{-15}$ & $1.43\times10^{-15}$ & 1.092 & 0.120 \\ & $L=4$ & $8.22\times10^{-15}$ & $1.67\times10^{-15}$ & 1.895 & 0.155 \\ & $L=5$ & $5.08\times10^{-7}$ & $2.39\times10^{-15}$ & 1.082 & 0.248 \\ & $L=6$ & $4.78\times10^{-7}$ & $3.76\times10^{-15}$ & 1.200 & 0.513 \\ & $L=7$ & $9.84\times10^{-7}$ & $4.95\times10^{-15}$ & 2.684 & 2.090 \\ & $L=8$ & $4.53\times10^{-2}$ & $1.34\times10^{-14}$ & 16.748 & 17.152 \\ \midrule \multicolumn{6}{l}{\textit{TT-CPD}} \\ Variable dimension & $n=10$ & $1.38\times10^{-8}$ & $2.10\times10^{-7}$ & 2.141 & 0.163 \\ & $n=20$ & $5.11\times10^{-9}$ & $2.22\times10^{-8}$ & 2.145 & 0.121 \\ & $n=30$ & $1.17\times10^{-9}$ & $2.77\times10^{-8}$ & 2.156 & 0.154 \\ & $n=40$ & $7.52\times10^{-10}$ & $2.04\times10^{-7}$ & 2.238 & 0.204 \\ & $n=50$ & $1.67\times10^{-10}$ & $9.03\times10^{-8}$ & 2.276 & 0.305 \\ & $n=60$ & $2.75\times10^{-10}$ & $5.69\times10^{-10}$ & 2.414 & 0.498 \\ & $n=70$ & $7.37\times10^{-12}$ & $3.19\times10^{-7}$ & 2.816 & 0.765 \\ & $n=80$ & $8.46\times10^{-12}$ & $1.79\times10^{-8}$ & 3.251 & 1.234 \\ \addlinespace Variable order & $L=3$ & $4.55\times10^{-15}$ & $2.11\times10^{-15}$ & 1.126 & 0.130 \\ & $L=4$ & $2.46\times10^{-9}$ & $6.75\times10^{-7}$ & 2.146 & 0.231 \\ & $L=5$ & $8.15\times10^{-10}$ & $2.00\times10^{-7}$ & 3.182 & 0.179 \\ & $L=6$ & $7.95\times10^{-9}$ & $3.51\times10^{-7}$ & 4.256 & 0.294 \\ & $L=7$ & $2.31\times10^{-8}$ & $4.21\times10^{-7}$ & 6.499 & 1.653 \\ & $L=8$ & $1.35\times10^{-8}$ & $6.63\times10^{-7}$ & 21.735 & 16.995 \\ \bottomrule \end{tabular} \caption{Median relative errors and running times over 30 rank-5 CPDs per method and tensor. The variable-dimension family has $L=4$, while the variable-order family has all dimensions equal to 10. The two cases with $L=4$ and $n=10$ are distinct tensors.} \label{tab:bench-high} \end{table} \begin{table}[htbp] \centering \begin{tabular}{l r rr rr} \toprule & & \multicolumn{2}{c}{$e>10^{-5}$} & \multicolumn{2}{c}{$e>10^{-3}$}\\ \cmidrule(lr){3-4}\cmidrule(lr){5-6} Method & Trials per version & \scriptsize{Tensor Fox 1} & \scriptsize{Tensor Fox 2} & \scriptsize{Tensor Fox 1} & \scriptsize{Tensor Fox 2} \\ \midrule dGN & 420 & 39 & 11 & 36 & 10\\ TT-CPD & 420 & 9 & 0 & 2 & 0\\ \bottomrule \end{tabular} \caption{Number of higher order benchmark trials exceeding two relative error thresholds. All 14 cases, including the two distinct order-four tensors of dimension 10, are counted once per method.} \label{tab:bench-tail} \end{table} \subsection{Accuracy and time} Figure~\ref{fig:bench-attainment} shows the fraction of trials satisfying an error tolerance within a given time. Trials that do not satisfy the tolerance are included in the denominator. For the third order benchmarks the errors attainable vary considerably between tensors. We therefore use the median error of Tensor Fox 1 for each tensor as its tolerance and divide the times by its median time. For the higher order benchmarks we use the common tolerances $10^{-5}$ and $10^{-12}$ and retain the times in seconds. For the third order benchmarks, $59.6\%$ of the new trials attain the reference error within the reference time, compared with $20.0\%$ of the old trials. At the less restrictive tolerance for higher orders, both methods benefit from the computational improvements. At $10^{-12}$, however, the difference between the direct dGN and tensor train approaches becomes evident. The fraction of dGN trials satisfying this tolerance increases from $70.7\%$ to $97.4\%$, whereas for TT-CPD it remains close to $7\%$ in both implementations. Thus the gain in speed of the tensor train approach does not imply a gain in accuracy. \clearpage \begin{figure}[H] \centering \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_rel_error.png}\par \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_time.png}\par \caption{Box plots for the third order benchmarks, with 30 trials per box. The labels \texttt{hw}, \texttt{Mntensor(7)}, \texttt{Coll} and \texttt{Bn} denote handwritten digits, matrix multiplication, swamps and double bottlenecks, respectively. The suffix \texttt{x} denotes the clean cases. Outlier markers are omitted from the plots, not from the statistics.} \label{fig:bench-third} \end{figure} \begin{figure}[H] \centering \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_dGN_rel_error_Dimension.png}\par \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_dGN_time_Dimension.png}\par \caption{Rank-5 CPDs of fourth order tensors with shape $n\times n\times n\times n$, for $n=10,20,\ldots,80$, computed with the direct dGN approach. The curves represent the medians of the relative errors and times.} \label{fig:bench-dgn-dim} \end{figure} \begin{figure}[H] \centering \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_dGN_rel_error_Order.png}\par \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_dGN_time_Order.png}\par \caption{Rank-5 CPDs of tensors of shape $10\times\cdots\times10$, for $L=3,4,\ldots,8$, computed with the direct dGN approach. The curves represent the medians of the relative errors and times.} \label{fig:bench-dgn-order} \end{figure} \begin{figure}[H] \centering \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_ttcpd_rel_error_Dimension.png}\par \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_ttcpd_time_Dimension.png}\par \caption{Rank-5 CPDs of fourth order tensors with shape $n\times n\times n\times n$, for $n=10,20,\ldots,80$, computed with the tensor train approach. The curves represent the medians of the relative errors and times.} \label{fig:bench-tt-dim} \end{figure} \begin{figure}[H] \centering \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_ttcpd_rel_error_Order.png}\par \includegraphics[width=.94\textwidth,height=.275\textheight,keepaspectratio]{figures/compare_high_order_ttcpd_time_Order.png}\par \caption{Rank-5 CPDs of tensors of shape $10\times\cdots\times10$, for $L=3,4,\ldots,8$, computed with the tensor train approach. The curves represent the medians of the relative errors and times.} \label{fig:bench-tt-order} \end{figure} \clearpage \begin{figure}[H] \centering \includegraphics[width=.94\textwidth,height=.78\textheight,keepaspectratio]{figures/benchmark_accuracy_time.png} \caption{Percentage of trials satisfying the indicated error tolerance within a given time. The denominators are 450 trials per version in (a) and 420 per method and version in (b) and (c). Each tensor contributes 30 trials. In (a), both the tolerance and the time scale are given by the corresponding medians of Tensor Fox 1.} \label{fig:bench-attainment} \end{figure} \section{Conclusions} Tensor Fox 2.0.0 extends the compression-based CPD algorithm to sparse tensors for which explicit unfoldings or Gram matrices are impractical. For tensors on disk, parallel external sorting, compact records and bounded access to the sparse data reserve RAM for the dense computations. Grouping can reduce the arithmetic cost of core construction, and blockwise multiplication limits its workspace. The two partial contractions reduce the work of computing the gradient, and blockwise residual evaluation avoids storing the complete approximation at each dGN iteration. Initialization and TT-SVD benefit from avoiding unnecessary reconstructions and singular components. The dense experiments show a reduction in median time in 42 of the 43 configurations. The tensor train approach reduces times but usually gives larger median errors than its counterpart in the implementation associated with the thesis. The dense core and compression matrices remain in RAM, and their dimensions influence both cost and accuracy. \bibliographystyle{plain} {\small\bibliography{references}} \end{document}