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

Exclude integer expressions earlier from autograd #599

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions include/autograd/propagate_defs_need_grad.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PropagateRequires : public SymbolTable<Visitor> {

protected:
using BaseClass::visit;
void visitExpr(const Expr &e) override;
void visit(const Load &op) override;
void visit(const Store &op) override;
void visit(const ReduceTo &op) override;
Expand Down Expand Up @@ -71,6 +72,7 @@ class PropagateProvides : public SymbolTable<Visitor> {

protected:
using BaseClass::visit;
void visitExpr(const Expr &e) override;
void visit(const Load &op) override;
void visit(const Store &op) override;
void visit(const ReduceTo &op) override;
Expand Down
3 changes: 3 additions & 0 deletions src/autograd/derivative.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ void Derivative::setPartial(const Expr &expr, const Expr &partial) {
}

void Derivative::visitExpr(const Expr &expr) {
if (!isFloat(expr->dtype())) {
return;
}
if (!rootExpr_.isValid()) {
rootExpr_ = StmtOrExprID{expr, expr->parentStmt()};
setPartial(expr, makeIntConst(1));
Expand Down
17 changes: 14 additions & 3 deletions src/autograd/propagate_defs_need_grad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

namespace freetensor {

void PropagateRequires::visitExpr(const Expr &e) {
if (isFloat(e->dtype())) {
BaseClass::visitExpr(e);
}
}

void PropagateRequires::visit(const Load &op) {
if (isFloat(op->dtype()) && curTarget_.isValid() &&
affectedDefs_.count(def(op->var_)->id())) {
if (curTarget_.isValid() && affectedDefs_.count(def(op->var_)->id())) {
affectedDefs_.insert(curTarget_);
// No need to recurse deeper
}
Expand Down Expand Up @@ -71,8 +76,14 @@ std::unordered_set<ID> PropagateRequires::propagateUntilConverge(
return propagator.affectedDefs();
}

void PropagateProvides::visitExpr(const Expr &e) {
if (isFloat(e->dtype())) {
BaseClass::visitExpr(e);
}
}

void PropagateProvides::visit(const Load &op) {
if (isFloat(op->dtype()) && curTarget_.isValid() &&
if (curTarget_.isValid() &&
buffer(op->var_)->atype() == AccessType::Cache) {
affectedDefs_.insert(def(op->var_)->id());
// No need to recurse deeper
Expand Down
18 changes: 18 additions & 0 deletions test/21.autograd/test_grad.py
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,24 @@ def f(a, b):
return ft.libop.matmul(a, b)


def test_no_grad_integer():
# Should not report error on intrinsic because it is integer
with ft.VarDef([("x", (), "float32", "input", "cpu"),
("y", (), "float32", "output", "cpu")]) as (x, y):
y[...] = ft.intrinsic("(%)", ft.cast(x[...], "int32"), ret_type="int32")
ast = ft.pop_ast(verbose=True)
_, ast, _, _, _ = ft.grad_body(ast, ["x"], ["y"],
set(),
reset_provided_grad=False)
print(ast)

with ft.VarDef("d_x", (), "float32", "output", "cpu") as d_x:
d_x[...] = 0
std = ft.pop_ast()

assert std.match(ast)


def test_error_input_not_found():

@ft.transform
Expand Down
Loading