Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linear Algebra API & Parser #364

Open
wants to merge 70 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
83ad3c0
Add in linalg parser that still uses IndexExpr notation
weiya711 Oct 30, 2020
4630aed
Add in linalg_notation files for future use
weiya711 Nov 3, 2020
8811eda
some debugging couts
mattlkf Nov 3, 2020
0d7a174
Merge branch 'master' into me-playing-with-her-parser
mattlkf Nov 3, 2020
561aa51
pair programming - LinalgBase, Matrix, Vector dummies
mattlkf Nov 3, 2020
00ed53d
Add in shapes and format for matrix and vector API and also assignmen…
weiya711 Nov 5, 2020
0a5a547
Add in code to start rewriting from linalg notation to index notation
weiya711 Nov 6, 2020
d3a6d83
squash init order warning, add dummy at method for Matrix, use size_t…
mattlkf Nov 7, 2020
da5dbc2
init-ed a tensorbase inside the linalgbase
mattlkf Nov 8, 2020
ef59d4b
poking around at the Tensor API
mattlkf Nov 9, 2020
20f1450
resolve merge
mattlkf Nov 9, 2020
e039952
declarations for passing in a TensorBase ptr to the LinalgExpr
mattlkf Nov 9, 2020
3e1ed50
hopeful signs - getting tensorBase
mattlkf Nov 10, 2020
d5336fc
can reference the TensorBase inside the rewrite
mattlkf Nov 10, 2020
8d512fe
experiment with doing the assignment after the rewrite of the RHS
mattlkf Nov 10, 2020
4576e3c
set idxcount(0) and print tensorBase in <<
mattlkf Nov 10, 2020
703e62e
Add in transpose() and elemMul() for linalg expr and also add more no…
weiya711 Nov 11, 2020
86e2da1
Add in rewrite for LinalgLiteralNodes
weiya711 Nov 11, 2020
9ebc5ba
Add in matrix multiplication and shape checks and vectors and scalars
weiya711 Nov 11, 2020
8ce89c6
demonstrate that insertion works, compute on actual data works too
mattlkf Nov 12, 2020
b98e3f5
rename my simplest test to tensorbase
mattlkf Nov 12, 2020
6fd2d0c
Add in transpose for vectors
weiya711 Nov 12, 2020
fb63f98
resolve merge
mattlkf Nov 12, 2020
8b539a1
resolve merge
mattlkf Nov 12, 2020
8cb0c14
Add in implicit rewrite after assignment
weiya711 Nov 12, 2020
1a8d7f0
Add in fixes after TensorBase merge and partial linalg_parser that us…
weiya711 Nov 12, 2020
0e06201
remove the more egregious spewing of couts
mattlkf Nov 12, 2020
d6f25c1
add data to testcases involving vectors + add a write method for vector
mattlkf Nov 12, 2020
98aedd5
use ASSERT_EQ in some tests
mattlkf Nov 13, 2020
c7b8c4a
Add in changes to fix linalg_parser
weiya711 Nov 16, 2020
8bbb892
Add in scalar support for linalg_parser
weiya711 Nov 16, 2020
14ce8e9
WIP blocking
weiya711 Nov 19, 2020
34732e9
Add in blocking code...still may be a little bit buggy. Has a lot of …
weiya711 Nov 19, 2020
7ee2315
enable A(1,2) = 3 writes, and define custom cast operator for Scalars
mattlkf Dec 8, 2020
b7ba7fa
Access Matrix/Vector classes with IndexVars
mattlkf Dec 9, 2020
ce9a605
assign IndexExpr to Scalar properly
mattlkf Dec 9, 2020
3e8f4a6
quick test that printing and reading the ScalarAccess returned by A(1…
mattlkf Dec 10, 2020
6ad76a7
Merge branch 'master' into insert-better
mattlkf Dec 15, 2020
79c61a5
change indexexpr test to show mixed use of tensor and matrix in linal…
mattlkf Dec 15, 2020
711d7c8
fix warning about initialization out of order
mattlkf Dec 15, 2020
7bd3e89
fixed another Matrix constructor
mattlkf Dec 15, 2020
b9ac34f
fixed another Matrix constructor
mattlkf Dec 15, 2020
15a41d5
fixed two more Matrix constructors
mattlkf Dec 15, 2020
1f4f574
fixed simplest Matrix constructor and commented out two Matrix constr…
mattlkf Dec 15, 2020
f6fe343
Fix minor mistake in test
weiya711 Dec 15, 2020
0feaa60
Rename indexVars for linalg compiler and remove blocking tests
weiya711 Dec 15, 2020
1ed47a4
remove unwanted constructors + remove optional colVec arg from simple…
mattlkf Dec 16, 2020
238cbbf
rename tensorbase test
mattlkf Dec 16, 2020
e90083a
remove tensorapi test
mattlkf Dec 16, 2020
f070811
removing some cruft
mattlkf Dec 16, 2020
e9b2cb5
Change rewrite to use LinalgNotationRewriter
weiya711 Dec 16, 2020
40101c8
get ASSERT_TENSOR_EQ working, update tests to use new ScalarAccess me…
mattlkf Dec 16, 2020
02188df
Fixes to rewrite using LinalgNotationRewriter
weiya711 Dec 16, 2020
f311d36
more tests cleanup
mattlkf Dec 17, 2020
e77c9a2
merge in master
mattlkf Dec 17, 2020
9efe965
remove unnecessary couts and large commented-out section for rewrite
mattlkf Dec 17, 2020
ee2c596
merge upstream taco
mattlkf Dec 17, 2020
db3b5c0
Fix some things with rewriter and Scalar LinalgBase constructor and a…
weiya711 Dec 17, 2020
470a1c5
Remove bool useTensorBase from Scalar constructor
weiya711 Dec 18, 2020
2ebb31f
allow for vector ASSERT_TENSOR_EQ too
mattlkf Dec 18, 2020
0a39d92
Merge branch 'master' of github.com:mattlkf/taco
mattlkf Dec 18, 2020
e8399dd
add some Scalar tests (and allow for direct assignment of value to Sc…
mattlkf Dec 18, 2020
542566a
minor cleanup
mattlkf Dec 18, 2020
ee18452
Add in test that sometimes fails
weiya711 Dec 22, 2020
0e39c8a
WIP debugging linalg and tensor api
weiya711 Jan 5, 2021
da4c9ef
Make LinalgTensorBaseNode subclass of LinalgVarNode
mattlkf Jan 14, 2021
1726246
test printing explicitly, fix extra newline when printing Linalg
mattlkf Jan 14, 2021
951e840
merge upstream master
mattlkf Jan 14, 2021
e0fe427
Fix command line flags for linalg to be correct
weiya711 Jan 14, 2021
d5fcc3d
Remove print statements in linalg parser
weiya711 Jan 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ CMakeCache.txt
doc

apps/tensor_times_vector/tensor_times_vector
tags
5 changes: 5 additions & 0 deletions include/taco/index_notation/index_notation.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "taco/lower/iterator.h"
#include "taco/index_notation/provenance_graph.h"

#include "taco/linalg_notation/linalg_notation_nodes_abstract.h"

namespace taco {

class Type;
Expand Down Expand Up @@ -59,6 +61,9 @@ struct SuchThatNode;
class IndexExprVisitorStrict;
class IndexStmtVisitorStrict;

struct VarNode;
class LinalgAssignment;

/// A tensor index expression describes a tensor computation as a scalar
/// expression where tensors are indexed by index variables (`IndexVar`). The
/// index variables range over the tensor dimensions they index, and the scalar
Expand Down
7 changes: 7 additions & 0 deletions include/taco/index_notation/index_notation_nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ struct NegNode : public UnaryExprNode {
}
};

struct TransposeNode : public UnaryExprNode {
TransposeNode(IndexExpr operand) : UnaryExprNode(operand) {}

void accept (IndexExprVisitorStrict* v) const {
v->visit(this);
}
};

struct BinaryExprNode : public IndexExprNode {
virtual std::string getOperatorString() const = 0;
Expand Down
271 changes: 271 additions & 0 deletions include/taco/linalg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
#ifndef TACO_LINALG_H
#define TACO_LINALG_H

#include "taco/type.h"
#include "taco/tensor.h"
#include "taco/format.h"

#include "taco/linalg_notation/linalg_notation.h"
#include "taco/linalg_notation/linalg_notation_nodes.h"
#include "taco/linalg_notation/linalg_notation_printer.h"


namespace taco {

class LinalgBase : public LinalgExpr {
protected:
std::string name;
Type tensorType;

LinalgAssignment assignment;
IndexStmt indexAssignment;

int idxcount = 0;
std::vector<std::string> indexVarNameList = {"i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line seems a bit long. Whenever possible, lines should be kept to 80 characters max (here and in other parts of code base).


IndexExpr rewrite(LinalgExpr linalg, std::vector<IndexVar> indices);

IndexVar getUniqueIndex();

std::vector<IndexVar> getUniqueIndices(size_t order);

public:
LinalgBase(std::string name, Type tensorType, Datatype dtype, std::vector<int> dims, Format format, bool isColVec = false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the constructor need to take both tensorType and dims as arguments? Doesn't tensorType have a shape member that should always be the same as dims?


/// [LINALG NOTATION]
LinalgAssignment operator=(const LinalgExpr &expr);

const LinalgAssignment getAssignment() const;

const IndexStmt getIndexAssignment() const;

IndexStmt rewrite();

typedef LinalgVarNode Node;
};

std::ostream &operator<<(std::ostream &os, const LinalgBase &linalg);

IndexExpr rewrite(LinalgExpr linalg, std::vector<IndexVar>);

IndexStmt rewrite(LinalgStmt linalg);

// ------------------------------------------------------------
// Matrix class
// ------------------------------------------------------------

template<typename CType>
class Matrix : public LinalgBase {
public:
explicit Matrix(std::string name);

Matrix(std::string name, size_t dim1, size_t dim2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dim1 and dim2 should probably just be named rows and cols?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change this


Matrix(std::string name, std::vector<size_t> dimensions);

Matrix(std::string name, size_t dim1, size_t dim2, Format format);

Matrix(std::string name, std::vector<size_t> dimensions, Format format);

Matrix(std::string name, size_t dim1, size_t dim2, ModeFormat format1, ModeFormat format2);

LinalgAssignment operator=(const LinalgExpr &expr) {
return LinalgBase::operator=(expr);
}

// Read method
CType at(int coord_x, int coord_y);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why coord_x and coord_y are used here when i and j are used for operator(). I think it'd make more sense to use one or the other consistently.


// Write method
void insert(int coord_x, int coord_y, CType value);

// ScalarAccess supports reading/assigning to single element
ScalarAccess<CType> operator()(int i, int j);

// Access methods
const Access operator()(const IndexVar i, const IndexVar j) const;
Access operator()(const IndexVar i, const IndexVar j);

// Allow to be cast to a TensorBase for the sake of ASSERT_TENSOR_EQ
operator TensorBase() const { return *tensorBase; }
};

// ------------------------------------------------------------
// Matrix template method implementations
// ------------------------------------------------------------

template<typename CType>
Matrix<CType>::Matrix(std::string name) :
LinalgBase(name, Type(type<CType>(), {42, 42}), type<CType>(), {42, 42}, Format({dense, dense})) {}

template<typename CType>
Matrix<CType>::Matrix(std::string name, std::vector<size_t> dimensions) :
LinalgBase(name, Type(type<CType>(), Shape(std::vector<Dimension>(dimensions.begin(), dimensions.end()))), type<CType>(), std::vector<int>(dimensions.begin(), dimensions.end()), Format({dense,dense})) {}

template<typename CType>
Matrix<CType>::Matrix(std::string name, size_t dim1, size_t dim2) :
LinalgBase(name, Type(type<CType>(), {dim1, dim2}), type<CType>(), {(int) dim1, (int) dim2}, Format({dense,dense})) {}

template<typename CType>
Matrix<CType>::Matrix(std::string name, size_t dim1, size_t dim2, Format format) :
LinalgBase(name, Type(type<CType>(), {dim1, dim2}), type<CType>(), {(int) dim1, (int) dim2}, format) {}

template<typename CType>
Matrix<CType>::Matrix(std::string name, std::vector<size_t> dimensions, Format format) :
LinalgBase(name, Type(type<CType>(), Shape(std::vector<Dimension>(dimensions.begin(), dimensions.end()))), type<CType>(), std::vector<int>(dimensions.begin(), dimensions.end()), format) {}

template<typename CType>
Matrix<CType>::Matrix(std::string name, size_t dim1, size_t dim2, ModeFormat format1, ModeFormat format2) :
LinalgBase(name, Type(type<CType>(), {dim1, dim2}), type<CType>(), {(int)dim1, (int)dim2}, Format({format1, format2}), false) {}

// Definition of Read methods
template <typename CType>
CType Matrix<CType>::at(int coord_x, int coord_y) {
return tensorBase->at<CType>({coord_x, coord_y});
}

// Definition of Write methods
template <typename CType>
void Matrix<CType>::insert(int coord_x, int coord_y, CType value) {
tensorBase->insert({coord_x, coord_y}, value);
}

template <typename CType>
ScalarAccess<CType> Matrix<CType>::operator()(int i, int j) {
return ScalarAccess<CType>(tensorBase, {i, j});
}

// Definition of Access methods
template <typename CType>
const Access Matrix<CType>::operator()(const IndexVar i, const IndexVar j) const {
return (*tensorBase)({i,j});
}

template <typename CType>
Access Matrix<CType>::operator()(const IndexVar i, const IndexVar j) {
return (*tensorBase)({i,j});
}


// ------------------------------------------------------------
// Vector class
// ------------------------------------------------------------

template<typename CType>
class Vector : public LinalgBase {
std::string name;
Datatype ctype;
public:
explicit Vector(std::string name);

Vector(std::string name, int dim, bool isColVec = true);

Vector(std::string name, size_t dim, Format format, bool isColVec = true);

Vector(std::string name, size_t dim, ModeFormat format, bool isColVec = true);

LinalgAssignment operator=(const LinalgExpr &expr) {
return LinalgBase::operator=(expr);
}

// Support some Write methods
void insert(int coord, CType value);

ScalarAccess<CType> operator()(int i);

// Support some Read methods too
CType at(int coord);

// Access methods for use in IndexExprs
const Access operator()(const IndexVar i) const;
Access operator()(const IndexVar i);

// Allow to be cast to a TensorBase for the sake of ASSERT_TENSOR_EQ
operator TensorBase() const { return *tensorBase; }
};

// ------------------------------------------------------------
// Vector template method implementations
// ------------------------------------------------------------

template<typename CType>
Vector<CType>::Vector(std::string name) :
LinalgBase(name, Type(type<CType>(), {42}), type<CType>(), {42}, Format({dense}), true) {}

template<typename CType>
Vector<CType>::Vector(std::string name, int dim, bool isColVec) :
LinalgBase(name, Type(type<CType>(), {(size_t)dim}), type<CType>(), {(int)dim}, Format({dense}), isColVec) {}

template<typename CType>
Vector<CType>::Vector(std::string name, size_t dim, Format format, bool isColVec) : LinalgBase(name,
Type(type<CType>(), {dim}),
type<CType>(),
{(int) dim},
format, isColVec) {}

template<typename CType>
Vector<CType>::Vector(std::string name, size_t dim, ModeFormat format, bool isColVec) :
LinalgBase(name, Type(type<CType>(), {dim}), type<CType>(), {(int)dim}, Format(format), isColVec) {}

// Vector write methods
template<typename CType>
void Vector<CType>::insert(int coord, CType value) {
tensorBase->insert({coord}, value);
}

template <typename CType>
ScalarAccess<CType> Vector<CType>::operator()(int i) {
return ScalarAccess<CType>(tensorBase, {i});
}

template <typename CType>
CType Vector<CType>::at(int coord) {
return tensorBase->at<CType>({coord});
}

// Definition of Access methods
template <typename CType>
const Access Vector<CType>::operator()(const IndexVar i) const {
return (*tensorBase)({i});
}

template <typename CType>
Access Vector<CType>::operator()(const IndexVar i) {
return (*tensorBase)({i});
}

// ------------------------------------------------------------
// Scalar class
// ------------------------------------------------------------

template<typename CType>
class Scalar : public LinalgBase {
std::string name;
Datatype ctype;
public:
explicit Scalar(std::string name);

LinalgAssignment operator=(const LinalgExpr &expr) {
return LinalgBase::operator=(expr);
}

void operator=(const IndexExpr& expr) {
(*tensorBase) = expr;
}

CType operator=(CType x) {
tensorBase->insert({}, x);
return x;
}

operator CType() const { return tensorBase->at<CType>({}); }

operator TensorBase() const { return *tensorBase; }
};

template<typename CType>
Scalar<CType>::Scalar(std::string name) :
LinalgBase(name, Type(type<CType>(), {}) , type<CType>(), {}, Format(), false) {}

} // namespace taco
#endif
Loading