Skip to content

Commit

Permalink
add FSetInputVariableAttrs (apache#92)
Browse files Browse the repository at this point in the history
* add FSetInputVariableAttrs

* rename
  • Loading branch information
piiswrong committed Jan 7, 2017
1 parent d6a6e0e commit 7b0ac8f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/nnvm/op_attr_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ using FGradient = std::function<std::vector<NodeEntry>(
const NodePtr& nodeptr,
const std::vector<NodeEntry>& out_grads)>;

/*!
* \brief Set the attributes of input variable.
* Usually used for setting initialization or weight decay.
* \param attrs The attributes of this node.
* \param var the input variable
* \param index index of var in all inputs
*/
using FSetInputVarAttrOnCompose = std::function<void(
const NodeAttrs& attrs,
NodePtr var,
const int index)>;

} // namespace nnvm

#endif // NNVM_OP_ATTR_TYPES_H_
10 changes: 10 additions & 0 deletions src/core/symbolic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ void Symbol::Compose(const array_view<const Symbol*>& args,
const std::unordered_map<std::string, const Symbol*>& kwargs,
const std::string& name) {
static auto& flist_inputs = Op::GetAttr<FListInputNames>("FListInputNames");
static auto& fset_attrs = Op::GetAttr<FSetInputVarAttrOnCompose>("FSetInputVarAttrOnCompose");

CHECK(!outputs[0].node->is_variable()) << "Variable cannot be composed";
// parameter check.
Expand Down Expand Up @@ -323,6 +324,15 @@ void Symbol::Compose(const array_view<const Symbol*>& args,
}
}
UpdateNodeVersion(n);

FSetInputVarAttrOnCompose fn = fset_attrs.get(n->op(), nullptr);
if (fn != nullptr) {
for (size_t i = 0; i < n->inputs.size(); ++i) {
if (n->inputs[i].node->is_variable()) {
fn(n->attrs, n->inputs[i].node, i);
}
}
}
} else {
// general composition
CHECK_EQ(args.size(), 0U)
Expand Down

0 comments on commit 7b0ac8f

Please sign in to comment.