Skip to content

[Placeholder] Remove the ability to differentiate variables. #1735

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

Merged
merged 1 commit into from
Sep 28, 2018
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
3 changes: 2 additions & 1 deletion include/glow/Graph/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ class Function final : public Named {

struct TrainingConfig;

using VariableGradientsList = std::list<std::pair<Storage *, Storage *>>;
using VariableGradientsList =
std::list<std::pair<Placeholder *, Placeholder *>>;

/// Create a new Function that 'trains' the input Function. We differentiate the
/// nodes and insert code to update the weights based on the \p config
Expand Down
24 changes: 12 additions & 12 deletions lib/Graph/Grad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,36 +228,36 @@ Function *glow::differentiate(Function *F, const TrainingConfig &conf,
} // End of the for-each instr loop.

for (auto N : nodes) {
// Iterate only through Variables/Placeholders used by the Function.
// These are inserted during the post-order walk.
Storage *V = llvm::dyn_cast<Storage>(N);
if (!V)
// Iterate only through Placeholders used by the Function. These are
// inserted during the post-order walk.
Placeholder *PH = llvm::dyn_cast<Placeholder>(N);
if (!PH)
continue;

// In this special differentiation mode we record the last gradient value
// without performing the SGD update. This mode is used by the unit tests.
if (varGrads) {
if (map.hasGradient(V)) {
std::string nodeName = "_grad_" + V->getName().str();
if (map.hasGradient(PH)) {
std::string nodeName = "_grad_" + PH->getName().str();
// Save the gradient and return the destination variable.
auto *saveNode = G->createSavePH(nodeName, map.getGradient(V));
auto *GradV = llvm::dyn_cast<Storage>(saveNode->getPlaceholder());
varGrads->push_back({V, GradV});
auto *saveNode = G->createSavePH(nodeName, map.getGradient(PH));
Placeholder *GradV = saveNode->getPlaceholder();
varGrads->push_back({PH, GradV});
}
continue;
}

// Don't update nodes that are not marked as trainable.
if (!V->isTraining()) {
if (!PH->isTraining()) {
continue;
}

auto X = new SGDNode(V->getName(), map.getGradient(V), V, conf.L1Decay,
auto X = new SGDNode(PH->getName(), map.getGradient(PH), PH, conf.L1Decay,
conf.L2Decay, conf.learningRate, conf.momentum,
conf.batchSize);
toAppend.push_back(X);
// Now update the weight with the value computed by SGD.
auto *save = new SaveNode(V->getName().str() + ".saveGrad", {X, 0}, V);
auto *save = new SaveNode(PH->getName().str() + ".saveGrad", {X, 0}, PH);
toAppend.push_back(save);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/unittests/gradCheckTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ float gradDiff(float G1, float G2) {
Placeholder *getGrad(const VariableGradientsList &grads, Placeholder *V) {
for (auto &p : grads) {
if (p.first == V) {
return cast<Placeholder>(p.second);
return p.second;
}
}
return nullptr;
}

void allocateGrads(Context &ctx, const VariableGradientsList &grads) {
for (auto &p : grads) {
auto grad = cast<Placeholder>(p.second);
auto grad = p.second;
ctx.allocate(grad);
}
}
Expand Down