! * \brief Node for a variable. * * It must be the output of exactly one OperatorNode and may be input to other * OperatorNode. * * Each variable has an owner, the operator that generates this variable as one * of the output. * * VarNode class exposes most commonly used memory management interface */
| 235 | * VarNode class exposes most commonly used memory management interface |
| 236 | */ |
| 237 | class VarNode final : public GraphNodeBase { |
| 238 | public: |
| 239 | /*! |
| 240 | * \brief this constructor should only be called by |
| 241 | * OperatorNodeBase::add_output |
| 242 | * |
| 243 | * implemented in core/impl/graph/operator_node.cpp |
| 244 | */ |
| 245 | inline VarNode(Maybe<std::string> name, OperatorNodeBase* owner); |
| 246 | |
| 247 | /* ===================== memory optimization ===================== */ |
| 248 | |
| 249 | using LayoutConstraintCallback = thin_function<bool(const TensorLayout&)>; |
| 250 | |
| 251 | /*! |
| 252 | * \brief add a callback function to check the validity of a particular |
| 253 | * tensor layout |
| 254 | * |
| 255 | * If callback returns true, it means that this VarNode's dev_tensor |
| 256 | * with given layout may be forwarded to opr directly, otherwise it |
| 257 | * will be implicitly rearranged to a contiguous one. |
| 258 | */ |
| 259 | MGE_WIN_DECLSPEC_FUC VarNode& add_layout_constraint( |
| 260 | LayoutConstraintCallback callback); |
| 261 | |
| 262 | /*! |
| 263 | * \brief requires the layout to be contiguous |
| 264 | * |
| 265 | * Note: since many oprs require inputs to be contiguous, this is |
| 266 | * implemented by marking a flag on the var rather than adding a |
| 267 | * LayoutConstraintCallback to check whether it is contiguous. All the |
| 268 | * existing callbacks would be cleared and new callbacks would be |
| 269 | * ignored after add_layout_constraint_contiguous() is invoked. |
| 270 | */ |
| 271 | MGE_WIN_DECLSPEC_FUC VarNode& add_layout_constraint_contiguous(); |
| 272 | |
| 273 | /*! |
| 274 | * \brief requires the layout to be monotone while allowing broadcast |
| 275 | * |
| 276 | * Note: similar to add_layout_constraint_contiguous() this is |
| 277 | * implemented by marking a flag; however user-defined callbacks are |
| 278 | * still invoked since they might impose stronger constraints. |
| 279 | */ |
| 280 | MGE_WIN_DECLSPEC_FUC VarNode& add_layout_constraint_monotone(); |
| 281 | |
| 282 | /*! |
| 283 | * \brief request that memory should be readonly forwarded from other |
| 284 | * var |
| 285 | * |
| 286 | * Note that this function must be called from |
| 287 | * OperatorNodeBase::mem_plan_fwd_in2out_readonly. |
| 288 | * |
| 289 | * \return whether this request could be satisfied |
| 290 | */ |
| 291 | MGB_WARN_UNUSED_RESULT MGE_WIN_DECLSPEC_FUC bool set_fwd_in2out_readonly( |
| 292 | VarNode* input, const SubTensorSpec& sub); |
| 293 | |
| 294 | /*! |