Skip to content

Commit

Permalink
Gradient update to give more control to cortenn (#24)
Browse files Browse the repository at this point in the history
* clean up docs

* add fwd information when deriving

* add option to separate shaper and coordinate mapper

* avoid bwd mapper in place of a fwd flag

* fix fwd assignment

* fix grader and update test

* cleanup and fix grader

* ensure ade integrates properly with llo in cortenn

* plug leaks

* plug leak again

* revert removed sum bwd in grader

* fix bwd for mappedtensors dis-similar shaper and coorder

* avoid optimizing during gradient

* maximize coverage and move docs/html to docs

* move over bwd rehaul from cortenn

* add doc for mapped tensor functions
  • Loading branch information
raggledodo committed Jan 4, 2019
1 parent 3733217 commit 959d51e
Show file tree
Hide file tree
Showing 952 changed files with 3,082 additions and 19,790 deletions.
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ GENERATE_HTML = YES
# The default directory is: html.
# This tag requires that the tag GENERATE_HTML is set to YES.

HTML_OUTPUT = html
HTML_OUTPUT = .

# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each
# generated HTML page (for example: .htm, .php, .asp).
Expand Down
45 changes: 15 additions & 30 deletions ade/coord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ struct iCoordMap
{
virtual ~iCoordMap (void) = default;

/// Forward transform coordinates
virtual void forward (CoordT::iterator out,
CoordT::const_iterator in) const = 0;

/// Return matmul(this, rhs)
virtual iCoordMap* forward (const iCoordMap& rhs) const = 0;
virtual iCoordMap* connect (const iCoordMap& rhs) const = 0;

/// Reverse transform coordinates
virtual void backward (CoordT::iterator out,
/// Forward transform coordinates
virtual void forward (CoordT::iterator out,
CoordT::const_iterator in) const = 0;

/// Return coordinate transformation with its forward and backward
Expand All @@ -53,15 +49,10 @@ struct CoordMap final : public iCoordMap
std::memset(fwd_, 0, mat_size);
fwd_[rank_cap][rank_cap] = 1;
init(fwd_);
inverse(bwd_, fwd_);
}

/// Implementation of iCoordMap
void forward (CoordT::iterator out,
CoordT::const_iterator in) const override;

/// Implementation of iCoordMap
iCoordMap* forward (const iCoordMap& rhs) const override
iCoordMap* connect (const iCoordMap& rhs) const override
{
return new CoordMap([&](MatrixT out)
{
Expand All @@ -73,13 +64,16 @@ struct CoordMap final : public iCoordMap
}

/// Implementation of iCoordMap
void backward (CoordT::iterator out,
void forward (CoordT::iterator out,
CoordT::const_iterator in) const override;

/// Implementation of iCoordMap
iCoordMap* reverse (void) const override
{
return new CoordMap(bwd_, fwd_);
return new CoordMap([this](MatrixT m)
{
inverse(m, this->fwd_);
});
}

/// Implementation of iCoordMap
Expand All @@ -95,36 +89,27 @@ struct CoordMap final : public iCoordMap
}

private:
CoordMap (const MatrixT fwd, const MatrixT bwd)
{
std::memcpy(fwd_, fwd, mat_size);
std::memcpy(bwd_, bwd, mat_size);
}

/// Forward transformation matrix
MatrixT fwd_;

/// Inverse of the forward transformation matrix
MatrixT bwd_;
};

/// Type of iCoordMap smartpointer
using CoordPtrT = std::shared_ptr<iCoordMap>;
using CoordptrT = std::shared_ptr<iCoordMap>;

/// Identity matrix instance
extern CoordPtrT identity;
extern CoordptrT identity;

/// Return coordinate mapper dividing dimensions after rank
/// by values in red vector
/// For example, given coordinate [2, 2, 6, 6], rank=2, and red=[3, 3],
/// mapper forward transforms to coordinate [2, 2, 2, 2]
CoordPtrT reduce (uint8_t rank, std::vector<DimT> red);
CoordptrT reduce (uint8_t rank, std::vector<DimT> red);

/// Return coordinate mapper multiplying dimensions after rank
/// by values in ext vector
/// For example, given coordinate [6, 6, 2, 2], rank=2, and ext=[3, 3],
/// mapper forward transforms to coordinate [6, 6, 6, 6]
CoordPtrT extend (uint8_t rank, std::vector<DimT> ext);
CoordptrT extend (uint8_t rank, std::vector<DimT> ext);

/// Return coordinate mapper permuting coordinate according to input order
/// Order is a vector of indices of the dimensions to appear in order
Expand All @@ -134,12 +119,12 @@ CoordPtrT extend (uint8_t rank, std::vector<DimT> ext);
/// mapper forward transforms to coordinate [2, 4, 1, 3]
/// Returned coordinate mapper will be a CoordMap instance, so inversibility
/// requires order indices be unique, otherwise throw fatal error
CoordPtrT permute (std::vector<uint8_t> order);
CoordptrT permute (std::vector<uint8_t> order);

/// Return coordinate mapper flipping coordinate value at specified dimension
/// Flipped dimension with original value x is represented as -x-1
/// (see CoordT definition)
CoordPtrT flip (uint8_t dim);
CoordptrT flip (uint8_t dim);

}

Expand Down
2 changes: 1 addition & 1 deletion ade/ifunctor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// Define functor nodes of an equation graph
///

#include "ade/itensor.hpp"
#include "ade/mtens.hpp"

#ifndef ADE_IFUNCTOR_HPP
#define ADE_IFUNCTOR_HPP
Expand Down
42 changes: 1 addition & 41 deletions ade/itensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// Define interfaces and building blocks for an equation graph
///

#include "ade/coord.hpp"
#include "ade/shape.hpp"

#ifndef ADE_INTERFACE_HPP
#define ADE_INTERFACE_HPP
Expand Down Expand Up @@ -49,46 +49,6 @@ using TensptrT = std::shared_ptr<iTensor>;

using TensrefT = std::weak_ptr<iTensor>;

/// Coordinate mapper and tensor pair
struct MappedTensor final
{
MappedTensor (CoordPtrT mapper, TensptrT tensor) :
mapper_(mapper), tensor_(tensor)
{
if (tensor_ == nullptr)
{
logs::fatal("cannot map a null tensor");
}
}

/// Return shape of tensor filtered through coordinate mapper
Shape shape (void) const
{
const Shape& shape = tensor_->shape();
CoordT out;
CoordT in;
std::copy(shape.begin(), shape.end(), in.begin());
mapper_->forward(out.begin(), in.begin());
std::vector<DimT> slist(rank_cap);
std::transform(out.begin(), out.end(), slist.begin(),
[](CDimT cd) -> DimT
{
if (cd < 0)
{
cd = -cd - 1;
}
return std::round(cd);
});
return Shape(slist);
}

/// Coordinate mapper
CoordPtrT mapper_;

/// Tensor reference
TensptrT tensor_;
};

}

#endif // ADE_INTERFACE_HPP
126 changes: 126 additions & 0 deletions ade/mtens.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include "ade/itensor.hpp"
#include "ade/coord.hpp"

#ifndef ADE_MTENS_HPP
#define ADE_MTENS_HPP

namespace ade
{

/// Coordinate mapper and tensor pair
struct MappedTensor final
{
/// Construct MappedTensor auto deducing coorder_ and map_io_ flag
MappedTensor (TensptrT tensor, CoordptrT shaper) :
tensor_(tensor), shaper_(shaper)
{
if (tensor_ == nullptr)
{
logs::fatal("cannot map a null tensor");
}
map_io_ = tensor_->shape().n_elems() > shape().n_elems();
if (shaper == identity || map_io_)
{
coorder_ = shaper;
}
else
{
coorder_ = CoordptrT(shaper->reverse());
}
}

/// Construct MappedTensor with specific coorder_ and map_io_ flag
MappedTensor (TensptrT tensor, CoordptrT shaper,
bool map_io, CoordptrT coorder) :
tensor_(tensor), shaper_(shaper),
map_io_(map_io), coorder_(coorder)
{
if (tensor_ == nullptr)
{
logs::fatal("cannot map a null tensor");
}
}

/// Return shape of tensor filtered through coordinate mapper
Shape shape (void) const
{
ade::Shape shape = tensor_->shape();
CoordT out;
CoordT in;
std::copy(shape.begin(), shape.end(), in.begin());
shaper_->forward(out.begin(), in.begin());
std::vector<DimT> slist(rank_cap);
std::transform(out.begin(), out.end(), slist.begin(),
[](CDimT cd) -> DimT
{
if (cd < 0)
{
cd = -cd - 1;
}
return std::round(cd);
});
return Shape(slist);
}

/// Return tensor being mapped
TensptrT get_tensor (void) const
{
return tensor_;
}

/// Return shaper coord map
CoordptrT get_shaper (void) const
{
return shaper_;
}

/// Return map_io_ flag, True if coorder accepts input coord
/// and generated output, False otherwise
bool map_io (void) const
{
return map_io_;
}

/// Return coord map for coordinates
CoordptrT get_coorder (void) const
{
return coorder_;
}

private:
/// Tensor reference
TensptrT tensor_;

/// Shape mapper
CoordptrT shaper_;

/// True if map input coordinate to output, False otherwise
/// (if n_elems of inputshape > n_elems of outputshape)
bool map_io_;

/// Coordinate mapper
CoordptrT coorder_;
};

/// Return MappedTensor that identity maps input tensor
MappedTensor identity_map (TensptrT tensor);

/// Return MappedTensor that reduces input tensor according to
/// rank and reduction vector
MappedTensor reduce_map (TensptrT tensor,
uint8_t rank, std::vector<DimT> red);

/// Return MappedTensor that extends input tensor by
/// rank and extension vector
MappedTensor extend_map (TensptrT tensor,
uint8_t rank, std::vector<DimT> ext);

/// Return MappedTensor that permutes input tensor by order
MappedTensor permute_map (TensptrT tensor, std::vector<uint8_t> order);

/// Return MappedTensor that flips input tensor along dimension
MappedTensor flip_map (TensptrT tensor, uint8_t dim);

}

#endif // ADE_MTENS_HPP
Loading

0 comments on commit 959d51e

Please sign in to comment.