Skip to content

Commit e192071

Browse files
committed
[Relay] Odd's 'n ends changes to help Collage.
- Complete the implementation of WithFields. (Unfortunately they appear to be without unit tests and I continue this tradition...) - InferTypeExpr for InferTypeLocal but return the expression rather than the type. - Remove python binding of InlineComposites since C++ impl was removed some time ago. - Make IndexedGraph<Expr/DFPattern> more robust as stand-alone datastructure, and avoid unnecessary copies. This will become a fundamental datastructure in Collage rather than just a helper for DFPatternMatcher. - Extend IndexedGraph with a notion of 'basic block' on every dataflow node. Needed by Collage to avoid impossible partitions.
1 parent 4f5ab57 commit e192071

File tree

22 files changed

+2036
-1423
lines changed

22 files changed

+2036
-1423
lines changed

include/tvm/ir/expr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ class GlobalVarNode : public RelayExprNode {
260260
*/
261261
class GlobalVar : public RelayExpr {
262262
public:
263-
TVM_DLL explicit GlobalVar(String name_hint, Type type = {});
263+
TVM_DLL explicit GlobalVar(String name_hint, Type type = {}, Span span = {});
264264

265265
TVM_DEFINE_OBJECT_REF_METHODS(GlobalVar, RelayExpr, GlobalVarNode);
266+
TVM_DEFINE_OBJECT_REF_COW_METHOD(GlobalVarNode);
266267
};
267268

268269
// PrimExprs that are useful as runtime containers.

include/tvm/relay/adt.h

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,9 @@ class Clause : public ObjectRef {
264264
};
265265

266266
/*!
267-
* \brief Returns the clause with given properties. A null property denotes 'no change'.
268-
* Returns clause if all properties are unchanged. Otherwise, returns a copy with the new fields.
269-
* \param clause The clause to copy.
270-
* \param opt_lhs The (optional) lhs for the copied clause. If none, ret_clause->lhs = clause->lhs.
271-
* \param opt_rhs The (optional) rhs for the copied clause. If none,
272-
* ret_clause->rhs = clause->rhs.
273-
* \return If all
274-
* properties are null or the same as the property in the input clause (i.e., opt_lhs is null or
275-
* opt_lhs.value() == clause->lhs, etc.), then we return clause. Otherwise, we return a copy of
276-
* clause with the different fields overwritten. (i.e., if opt_lhs.value() != clause->lhs, then
277-
* ret_clause->lhs = opt_lhs.value()).
267+
* \brief Returns \p clause with the given properties. A null property denotes 'no change'.
268+
* Returns \p clause if all properties are unchanged. Otherwise, returns a copy with the new
269+
* fields.
278270
*/
279271
Clause WithFields(Clause clause, Optional<Pattern> opt_lhs = Optional<Pattern>(),
280272
Optional<Expr> opt_rhs = Optional<Expr>());
@@ -337,20 +329,9 @@ class Match : public Expr {
337329
};
338330

339331
/*!
340-
* \brief Returns the match with given properties. A null property denotes 'no change'.
341-
* Returns match if all properties are unchanged. Otherwise, returns a copy with the new fields.
342-
* \param match The match to copy.
343-
* \param opt_data The (optional) data for the copied match. If none, ret_match->data = match->data.
344-
* \param opt_clauses The (optional) clauses for the copied match. If none, ret_match->clauses =
345-
* match->clauses.
346-
* \param opt_complete The (optional) complete for the copied match. If none, ret_match->complete =
347-
* match->complete.
348-
* \param opt_span The (optional) span for the copied match. If none, ret_match->span = match->span.
349-
* \return If all properties are null or the same as the
350-
* property in the input match (i.e., opt_clauses is null or opt_clauses.value() == match->clauses,
351-
* etc.), then we return match. Otherwise, we return a copy of match with the different fields
352-
* overwritten. (i.e., if opt_clauses.value() != match->clauses, then ret_match->clauses =
353-
* opt_clauses.value()).
332+
* \brief Returns \p match with the given properties. A null property denotes 'no change'.
333+
* Returns \p match if all properties are unchanged. Otherwise, returns a copy with the new
334+
* fields.
354335
*/
355336
Match WithFields(Match match, Optional<Expr> opt_data = Optional<Expr>(),
356337
Optional<Array<Clause>> opt_clauses = Optional<Array<Clause>>(),

include/tvm/relay/expr.h

Lines changed: 45 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
#include "./type.h"
4040

4141
namespace tvm {
42+
43+
/*!
44+
* \brief Returns \p global_var with the given properties. A null property denotes 'no change'.
45+
* Returns \p global_var if all properties are unchanged. Otherwise, returns a copy with the new
46+
* fields.
47+
*/
48+
GlobalVar WithFields(GlobalVar global_var, Optional<String> opt_name_hint = {},
49+
Optional<Type> opt_type = {}, Optional<VirtualDevice> opt_virtual_device = {},
50+
Optional<Span> opt_span = {});
51+
4252
namespace relay {
4353

4454
using Expr = tvm::RelayExpr;
@@ -97,8 +107,17 @@ class Constant : public Expr {
97107
TVM_DLL explicit Constant(runtime::NDArray data, Span span = Span());
98108

99109
TVM_DEFINE_OBJECT_REF_METHODS(Constant, RelayExpr, ConstantNode);
110+
TVM_DEFINE_OBJECT_REF_COW_METHOD(ConstantNode);
100111
};
101112

113+
/*!
114+
* \brief Returns \p constant with the given properties. A null property denotes 'no change'.
115+
* Returns \p constant if all properties are unchanged. Otherwise, returns a copy with the new
116+
* fields.
117+
*/
118+
Constant WithFields(Constant constant, Optional<runtime::NDArray> opt_data = {},
119+
Optional<VirtualDevice> opt_virtual_device = {}, Optional<Span> opt_span = {});
120+
102121
/*! \brief Tuple of multiple Exprs */
103122
class Tuple;
104123
/*! \brief Tuple container */
@@ -149,15 +168,9 @@ class Tuple : public Expr {
149168
};
150169

151170
/*!
152-
* \brief Returns the tuple with given properties. A null property denotes 'no change'.
153-
* Returns this if all properties are unchanged. Otherwise, returns a copy with the new fields.
154-
* \param tuple The tuple to copy
155-
* \param opt_fields The (optional) fields for the copied tuple. If none, ret_tuple->fields =
156-
* tuple->fields.
157-
* \param opt_virtual_device The (optional) virtual_device for the copied tuple. If none,
158-
* ret_tuple->virtual_device = tuple->virtual_device.
159-
* \param opt_span The (optional) span for the copied tuple. If none,
160-
* ret_tuple->span = tuple->span.
171+
* \brief Returns \p tuple with the given properties. A null property denotes 'no change'.
172+
* Returns \p tuple if all properties are unchanged. Otherwise, returns a copy with the new
173+
* fields.
161174
*/
162175
Tuple WithFields(Tuple tuple, Optional<Array<Expr>> opt_fields = Optional<Array<Expr>>(),
163176
Optional<VirtualDevice> opt_virtual_device = Optional<VirtualDevice>(),
@@ -251,19 +264,9 @@ class Var : public Expr {
251264
};
252265

253266
/*!
254-
* \brief Returns the var with given properties. A null property denotes 'no change'.
255-
* Returns var if all properties are unchanged. Otherwise, returns a copy with the new fields.
256-
* \param var The var to copy.
257-
* \param opt_vid The (optional) vid for the copied var. If none, ret_var->vid = var->vid.
258-
* \param opt_type_annotation The (optional) type_annotation for the copied var. If none,
259-
* ret_var->type_annotation = var->type_annotation.
260-
* \param opt_virtual_device The (optional) virtual_device for the copied tuple. If none,
261-
* ret_tuple->virtual_device = tuple->virtual_device.
262-
* \param opt_span The (optional) span for the copied var. If none, ret_var->span = var->span.
263-
* \return If all properties are null or the same as the property in the input var (i.e., opt_vid is
264-
* null or opt_vid.value() == var->vid, etc.), then we return var. Otherwise, we return a copy of
265-
* call with the different fields overwritten. (i.e., if opt_vid.value() != var->vid, then
266-
* ret_var->vid = opt_.value()).
267+
* \brief Returns \p vor with the given properties. A null property denotes 'no change'.
268+
* Returns \p var if all properties are unchanged. Otherwise, returns a copy with the new
269+
* fields.
267270
*/
268271
Var WithFields(Var var, Optional<Id> opt_vid = Optional<Id>(),
269272
Optional<Type> opt_type_annotation = Optional<Type>(),
@@ -374,22 +377,9 @@ class Call : public Expr {
374377
};
375378

376379
/*!
377-
* \brief Returns the call with given properties. A null property denotes 'no change'.
378-
* Returns call if all properties are unchanged. Otherwise, returns a copy with the new fields.
379-
* \param call The call to copy.
380-
* \param opt_op The (optional) op for the copied call. If none, ret_call->op = call->op.
381-
* \param opt_args The (optional) args for the copied call. If none, ret_call->args = call->args.
382-
* \param opt_attrs The (optional) attrs for the copied call. If none, ret_call->attrs =
383-
* call->attrs.
384-
* \param opt_type_args The (optional) type args for the copied call. If none,
385-
* ret_call->type_args = call->type_args.
386-
* \param opt_virtual_device The (optional) virtual_device for the copied call. If none,
387-
* ret_call->virtual_device = call->virtual_device.
388-
* \param opt_span The (optional) span for the copied call. If none, ret_call->span = call->span.
389-
* \return If all properties are null or the same as the property in the input call (i.e., opt_op is
390-
* null or opt_op.value() == call->op, etc.), then we return call. Otherwise, we return a copy of
391-
* call with the different fields overwritten. (i.e., if opt_op.value() != call->op, then
392-
* ret_call->op = opt_op.value()).
380+
* \brief Returns \p call with the given properties. A null property denotes 'no change'.
381+
* Returns \p call if all properties are unchanged. Otherwise, returns a copy with the new
382+
* fields.
393383
*/
394384
Call WithFields(Call call, Optional<Expr> opt_op = Optional<Expr>(),
395385
Optional<Array<Expr>> opt_args = Optional<Array<Expr>>(),
@@ -475,19 +465,9 @@ class Let : public Expr {
475465
};
476466

477467
/*!
478-
* \brief Returns the let with given properties. A null property denotes 'no change'.
479-
* Returns let if all properties are unchanged. Otherwise, returns a copy with the new fields.
480-
* \param let The let to copy.
481-
* \param opt_var The (optional) var for the copied let. If none, ret_let->op = let->op.
482-
* \param opt_value The (optional) value for the copied let. If none, ret_let->args = let->args.
483-
* \param opt_body The (optional) body for the copied let. If none, ret_let->attrs = let->attrs.
484-
* \param opt_virtual_device The (optional) virtual_device for the copied let. If none,
485-
* ret_let->virtual_device = let->virtual_device.
486-
* \param opt_span The (optional) span for the copied let. If none, ret_let->span = let->span.
487-
* \return If all properties are null or the same as the property in the input let (i.e., opt_var is
488-
* null or opt_var.value() == let->var, etc.), then we return let. Otherwise, we return a copy of
489-
* let with the different fields overwritten. (i.e., if opt_var.value() != let->var, then
490-
* ret_let->var = opt_var.value()).
468+
* \brief Returns \p let with the given properties. A null property denotes 'no change'.
469+
* Returns \p let if all properties are unchanged. Otherwise, returns a copy with the new
470+
* fields.
491471
*/
492472
Let WithFields(Let let, Optional<Var> opt_var = Optional<Var>(),
493473
Optional<Expr> opt_value = Optional<Expr>(),
@@ -559,23 +539,9 @@ class If : public Expr {
559539
};
560540

561541
/*!
562-
* \brief Returns the if_expr with given properties. A null property denotes 'no change'.
563-
* Returns if_expr if all properties are unchanged. Otherwise, returns a copy with the new fields.
564-
* \param if_expr The if expression to copy.
565-
* \param opt_cond The (optional) cond for the copied if_expr. If none, ret_if->cond =
566-
* if_expr->cond.
567-
* \param opt_true_branch The (optional) true_branch for the copied if_expr. If none,
568-
* ret_if->true_branch = ret_if->false_branch.
569-
* \param opt_false_branch The (optional) false_branch
570-
* for the copied if_expr. If none, ret_if->false_branch = if_expr->false_branch.
571-
* \param opt_virtual_device The (optional) virtual_device for the copied if_expr. If none,
572-
* ret_if->virtual_device = if_expr->virtual_device.
573-
* \param opt_span The (optional) span for the copied if_expr. If none,
574-
* ret_if->span = if_expr->span.
575-
* \return If all properties are null or the same as the property in
576-
* the input if_expr (i.e., opt_cond is null or opt_cond.value() == if_expr->cond, etc.), then we
577-
* return if_expr. Otherwise, we return a copy of if_expr with the different fields overwritten.
578-
* (i.e., if opt_cond.value() != if_expr->cond, then ret_if->cond = opt_cond.value()).
542+
* \brief Returns \p if_expr with the given properties. A null property denotes 'no change'.
543+
* Returns \p if_expr if all properties are unchanged. Otherwise, returns a copy with the new
544+
* fields.
579545
*/
580546
If WithFields(If if_expr, Optional<Expr> opt_cond = Optional<Expr>(),
581547
Optional<Expr> opt_true_branch = Optional<Expr>(),
@@ -628,22 +594,9 @@ class TupleGetItem : public Expr {
628594
};
629595

630596
/*!
631-
* \brief Returns the tuple_get_item with given properties. A null property denotes 'no change'.
632-
* Returns if_expr if all properties are unchanged. Otherwise, returns a copy with the new fields.
633-
* \param tuple_get_item The tuple_get_item to copy.
634-
* \param opt_tuple The (optional) tuple for the copied tuple_get_item. If none,
635-
* ret_tuple_get_item->tuple = tuple_get_item->tuple.
636-
* \param opt_index The (optional) index for the copied tuple_get_item. If none,
637-
* ret_tuple_get_item->index = tuple_get_item->index.
638-
* \param opt_virtual_device The (optional) virtual_device for the copied tuple_get_item.
639-
* If none, ret_tuple_get_item->virtual_device = tuple_get_item->virtual_device.
640-
* \param opt_span The (optional) span for the copied tuple_get_item. If none,
641-
* ret_tuple_get_item->span = tuple_get_item->span.
642-
* \return If all properties are null or the same as the property in the input tuple_get_item
643-
* (i.e., opt_tuple is null or opt_tuple.value() == tuple_get_item->tuple, etc.), then we return
644-
* tuple_get_item. Otherwise, we return a copy of tuple_get_item with the different fields
645-
* overwritten. (i.e., if opt_tuple.value() != tuple_get_item->tuple, then
646-
* ret_tuple_get_item->tuple = opt_tuple.value()).
597+
* \brief Returns \p tuple_get_item with the given properties. A null property denotes 'no change'.
598+
* Returns \p tuple_get_item if all properties are unchanged. Otherwise, returns a copy with the new
599+
* fields.
647600
*/
648601
TupleGetItem WithFields(TupleGetItem tuple_get_item, Optional<Expr> opt_tuple = Optional<Expr>(),
649602
Optional<Integer> opt_index = Optional<Integer>(),
@@ -692,21 +645,9 @@ class RefCreate : public Expr {
692645
};
693646

694647
/*!
695-
* \brief Returns the ref create with given properties. A null property denotes 'no change'.
696-
* Returns ref_create if all properties are unchanged. Otherwise, returns a copy with the new
648+
* \brief Returns \p ref_create with the given properties. A null property denotes 'no change'.
649+
* Returns \p ref_crete if all properties are unchanged. Otherwise, returns a copy with the new
697650
* fields.
698-
* \param ref_create The ref_create to copy.
699-
* \param opt_value The (optional) value for the copied ref_create. If none,
700-
* ret_ref_create->value = ref_create->value.
701-
* \param opt_virtual_device The (optional) virtual_device for the copied ref_create. If none,
702-
* ret_ref_create->virtual_device = ref_create->virtual_device.
703-
* \param opt_span The (optional) span for the copied ref_create. If none,
704-
* ret_ref_create->span = ref_create->span.
705-
* \return If all properties are null or the same as the property in the input ref_create
706-
* (i.e., opt_value is null or opt_value.value() == ref_create->value, etc.), then we return
707-
* ref_create. Otherwise, we return a copy of ref_create with the different fields overwritten.
708-
* (i.e., if opt_value.value() != ref_create->value, then
709-
* ret_ref_create->value = opt_value.value()).
710651
*/
711652
RefCreate WithFields(RefCreate ref_create, Optional<Expr> opt_value = Optional<Expr>(),
712653
Optional<VirtualDevice> opt_virtual_device = Optional<VirtualDevice>(),
@@ -754,20 +695,9 @@ class RefRead : public Expr {
754695
};
755696

756697
/*!
757-
* \brief Returns the ref read with given properties. A null property denotes 'no change'.
758-
* Returns ref_read if all properties are unchanged. Otherwise, returns a copy with the new fields.
759-
* \param ref_read The ref_read to copy.
760-
* \param opt_ref The (optional) ref for the copied ref_read. If none, ret_ref_read->ref =
761-
* ref_read->ref.
762-
* \param opt_virtual_device
763-
* The (optional) virtual_device for the copied ref_read. If none, ret_ref_read->virtual_device =
764-
* ref_read->virtual_device.
765-
* \param opt_span The (optional) span for the copied ref_read. If none, ret_ref_read->span =
766-
* ref_read->span.
767-
* \return If all properties are null or the same as the property in the input
768-
* ref_read (i.e., opt_ref is null or opt_ref.value() == ref_read->ref, etc.), then we return
769-
* ref_read. Otherwise, we return a copy of ref_read with the different fields overwritten. (i.e.,
770-
* if opt_ref.value() != ref_read->ref, then ret_ref_read->ref = opt_ref.value()).
698+
* \brief Returns \p ref_read with the given properties. A null property denotes 'no change'.
699+
* Returns \p ref_read if all properties are unchanged. Otherwise, returns a copy with the new
700+
* fields.
771701
*/
772702
RefRead WithFields(RefRead ref_read, Optional<Expr> opt_ref = Optional<Expr>(),
773703
Optional<VirtualDevice> opt_virtual_device = Optional<VirtualDevice>(),
@@ -820,22 +750,9 @@ class RefWrite : public Expr {
820750
};
821751

822752
/*!
823-
* \brief Returns the ref write with given properties. A null property denotes 'no change'.
824-
* Returns ref_write if all properties are unchanged. Otherwise, returns a copy with the new fields.
825-
* \param ref_write The ref_write to copy.
826-
* \param opt_ref The (optional) ref for the copied ref_write. If none,
827-
* ret_ref_write->ref = ref_write->ref.
828-
* \param opt_value The (optional) value for the copied ref_write. If none,
829-
* ret_ref_write->value = ref_write->value.
830-
* \param opt_virtual_device
831-
* The (optional) virtual_device for the copied ref_write. If none, ret_ref_write->virtual_device =
832-
* ref_write->virtual_device.
833-
* \param opt_span The (optional) span for the copied ref_write. If none, ret_ref_write->span =
834-
* ref_write->span.
835-
* \return If all properties are null or the same as the property in the input ref_write (i.e.,
836-
* opt_ref is null or opt_ref.value() == ref_write->ref, etc.), then we return ref_write. Otherwise,
837-
* we return a copy of ref_write with the different fields overwritten. (i.e., if ref_write.value()
838-
* != ref_write->ref, then ret_ref_write->ref = opt_ref.value()).
753+
* \brief Returns \p ref_write with the given properties. A null property denotes 'no change'.
754+
* Returns \p ref_write if all properties are unchanged. Otherwise, returns a copy with the new
755+
* fields.
839756
*/
840757
RefWrite WithFields(RefWrite ref_write, Optional<Expr> opt_ref = Optional<Expr>(),
841758
Optional<Expr> opt_value = Optional<Expr>(),

include/tvm/relay/expr_functor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ class MixedModeVisitor : public ::tvm::relay::ExprVisitor {
240240
*/
241241
explicit MixedModeVisitor(int visit_limit = 1);
242242

243+
using ExprVisitor::VisitExpr_;
244+
243245
/*!
244246
* \brief VisitExpr is finalized to preserve call expansion of dataflow regions
245247
*/

include/tvm/relay/function.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,9 @@ class Function : public BaseFunc {
121121
};
122122

123123
/*!
124-
* \brief Returns the function with given properties. A null property denotes 'no change'.
125-
* Returns function if all properties are unchanged. Otherwise, returns a copy with the new fields.
126-
* \param function The function to copy.
127-
* \param opt_params The (optional) params for the copied function. If none,
128-
* ret_function->params = function->params.
129-
* \param opt_body The (optional) body for the copied function. If none,
130-
* ret_function->body = function->body.
131-
* \param opt_ret_type The (optional) return type for the copied function. If none,
132-
* ret_function->ret_type = function->ret_type.
133-
* \param opt_ty_params The (optional) type params for the copied function. If none,
134-
* ret_function->type_params = function->type_params.
135-
* \param opt_attrs
136-
* The (optional) attributes for the copied function. If none,
137-
* ret_function->attrs = function->attrs.
138-
* \param opt_virtual_device The (optional) virtual_device for the copied function. If none,
139-
* ret_function->virtual_device = function->virtual_device.
140-
* \param opt_span The (optional) span for the copied function. If none,
141-
* ret_function->span = function->span.
142-
* \return If all properties are null or the same as the property in the input function
143-
* (i.e., opt_params is null or opt_params.value() == function->params, etc.), then we return
144-
* function. Otherwise, we return a copy of function with the different fields overwritten. (i.e.,
145-
* if opt_params.value() != function->params, then ret_function->params = opt_params.value()).
124+
* \brief Returns \p function with the given properties. A null property denotes 'no change'.
125+
* Returns \p function if all properties are unchanged. Otherwise, returns a copy with the new
126+
* fields.
146127
*/
147128
Function WithFields(Function function, Optional<Array<Var>> opt_params = Optional<Array<Var>>(),
148129
Optional<Expr> opt_body = Optional<Expr>(),

include/tvm/relay/transform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ TVM_DLL Pass InferType();
281281
*/
282282
TVM_DLL Type InferTypeLocal(const Expr& expr);
283283

284+
/*!
285+
* \brief Infer the types of all sub-expression of expr.
286+
*/
287+
TVM_DLL Expr InferTypeExpr(const Expr& expr);
288+
284289
/*!
285290
* \brief Search and eliminate common subexpression. For example, if there are
286291
* two expressions evaluated to an identical value, a single variable is created

0 commit comments

Comments
 (0)