-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb6a74d
commit f67c7d4
Showing
4 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Initial Benchmarking | ||
This is a crude starting point to comparing runtimes to pyttb. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pyttb as ttb | ||
from tensor_ext import Dense, Kruskal, cp_als | ||
from typing import Tuple | ||
import time | ||
import numpy as np | ||
|
||
def run_pyttb(source: ttb.tensor, init: ttb.ktensor, rank: int) -> ttb.ktensor: | ||
M, _, _ = ttb.cp_als(source, rank, init=init, printitn=-1) | ||
return M | ||
|
||
def run_rusty(source:Dense, init: Kruskal, rank:int) -> Kruskal: | ||
M = cp_als(source, init, rank) | ||
return M | ||
|
||
def benchmark(shape: Tuple[int,...], num_iters: int): | ||
pyttb_time = 0.0 | ||
rusty_time = 0.0 | ||
for _ in range(num_iters): | ||
data = np.random.random(shape) | ||
rank = 2 | ||
weights = np.ones((rank,)) | ||
factors = tuple(np.random.random((first, rank)) for first in shape) | ||
py_tensor = ttb.tensor(data) | ||
py_ktensor = ttb.ktensor(list(factors), weights) | ||
rust_dense = Dense(data) | ||
rust_kruskal = Kruskal(weights, factors) | ||
|
||
# TODO should provide init here for fair comparison | ||
start = time.time() | ||
pyttb_result = run_pyttb(py_tensor, py_ktensor, rank) | ||
pyttb_time += time.time() - start | ||
|
||
start = time.time() | ||
rusty_result = run_rusty(rust_dense, rust_kruskal, rank) | ||
rusty_time += time.time() - start | ||
|
||
np.testing.assert_allclose(pyttb_result.full().data, rusty_result.full().data) | ||
print( | ||
f"Pyttb time: {pyttb_time}\n" | ||
f"Rust time: {rusty_time}\n" | ||
f"Shape: {shape}" | ||
) | ||
|
||
if __name__=="__main__": | ||
benchmark((100, 100, 100), 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters