packed_func.h 40.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

20
/*!
tqchen committed
21
 * \file tvm/runtime/packed_func.h
22
 * \brief Type-erased function used across TVM API.
23
 */
24 25
#ifndef TVM_RUNTIME_PACKED_FUNC_H_
#define TVM_RUNTIME_PACKED_FUNC_H_
26

27 28 29
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
#include <sstream>
#endif
30
#include <dmlc/logging.h>
31 32
#include <functional>
#include <tuple>
33 34
#include <vector>
#include <string>
35 36
#include <limits>
#include <memory>
37
#include <utility>
38
#include <type_traits>
39 40 41
#include "c_runtime_api.h"
#include "module.h"
#include "ndarray.h"
42
#include "object.h"
43

44 45 46 47 48
// Whether use TVM runtime in header only mode.
#ifndef TVM_RUNTIME_HEADER_ONLY
#define TVM_RUNTIME_HEADER_ONLY 0
#endif

49
namespace tvm {
50 51
// forward declarations
class Integer;
52 53
class DataType;
class Expr;
54

55
namespace runtime {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

/*!
 * \brief Runtime utility for getting custom type name from code
 * \param type_code Custom type code
 * \return Custom type name
 */
TVM_DLL std::string GetCustomTypeName(uint8_t type_code);

/*!
 * \brief Runtime utility for checking whether custom type is registered
 * \param type_code Custom type code
 * \return Bool representing whether type is registered
 */
TVM_DLL bool GetCustomTypeRegistered(uint8_t type_code);

/*!
 * \brief Runtime utility for parsing string of the form "custom[<typename>]"
 * \param s String to parse
 * \param scan pointer to parsing pointer, which is scanning across s
 * \return type code of custom type parsed
 */
TVM_DLL uint8_t ParseCustomDatatype(const std::string& s, const char** scan);

79 80 81 82 83
// forward declarations
class TVMArgs;
class TVMArgValue;
class TVMRetValue;
class TVMArgsSetter;
84 85

/*!
86 87
 * \brief Packed function is a type-erased function.
 *  The arguments are passed by packed format.
88
 *
89 90 91
 *  This is an useful unified interface to call generated functions,
 *  It is the unified function function type of TVM.
 *  It corresponds to TVMFunctionHandle in C runtime API.
92 93 94
 */
class PackedFunc {
 public:
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  /*!
   * \brief The internal std::function
   * \param args The arguments to the function.
   * \param rv The return value.
   *
   * \code
   *   // Example code on how to implemented FType
   *   void MyPackedFunc(TVMArgs args, TVMRetValue* rv) {
   *     // automatically convert arguments to desired type.
   *     int a0 = args[0];
   *     float a1 = args[1];
   *     ...
   *     // automatically assign values to rv
   *     std::string my_return_value = "x";
   *     *rv = my_return_value;
   *   }
   * \endcode
   */
  using FType = std::function<void (TVMArgs args, TVMRetValue* rv)>;
114
  /*! \brief default constructor */
115
  PackedFunc() {}
116 117
  /*! \brief constructor from null */
  PackedFunc(std::nullptr_t null) {}  // NOLINT(*)
118 119 120 121
  /*!
   * \brief constructing a packed function from a std::function.
   * \param body the internal container of packed function.
   */
122 123
  explicit PackedFunc(FType body) : body_(body) {}
  /*!
124
   * \brief Call packed function by directly passing in unpacked format.
125 126
   * \param args Arguments to be passed.
   * \tparam Args arguments to be passed.
127 128 129 130 131 132 133 134 135
   *
   * \code
   *   // Example code on how to call packed function
   *   void CallPacked(PackedFunc f) {
   *     // call like normal functions by pass in arguments
   *     // return value is automatically converted back
   *     int rvalue = f(1, 2.0);
   *   }
   * \endcode
136 137
   */
  template<typename... Args>
138
  inline TVMRetValue operator()(Args&& ...args) const;
139 140 141
  /*!
   * \brief Call the function in packed format.
   * \param args The arguments
142
   * \param rv The return value.
143
   */
144
  inline void CallPacked(TVMArgs args, TVMRetValue* rv) const;
145
  /*! \return the internal body function */
146
  inline FType body() const;
147 148 149 150 151 152 153 154
  /*! \return Whether the packed function is nullptr */
  bool operator==(std::nullptr_t null) const {
    return body_ == nullptr;
  }
  /*! \return Whether the packed function is not nullptr */
  bool operator!=(std::nullptr_t null) const {
    return body_ != nullptr;
  }
155 156 157 158 159 160

 private:
  /*! \brief internal container of packed function */
  FType body_;
};

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
/*!
 * \brief Please refer to \ref TypedPackedFuncAnchor "TypedPackedFunc<R(Args..)>"
 */
template<typename FType>
class TypedPackedFunc;

/*!
 * \anchor TypedPackedFuncAnchor
 * \brief A PackedFunc wrapper to provide typed function signature.
 * It is backed by a PackedFunc internally.
 *
 * TypedPackedFunc enables compile time type checking.
 * TypedPackedFunc works with the runtime system:
 * - It can be passed as an argument of PackedFunc.
 * - It can be assigned to TVMRetValue.
 * - It can be directly converted to a type-erased PackedFunc.
 *
 * Developers should prefer TypedPackedFunc over PackedFunc in C++ code
 * as it enables compile time checking.
 * We can construct a TypedPackedFunc from a lambda function
 * with the same signature.
 *
 * \code
 *  // user defined lambda function.
 *  auto addone = [](int x)->int {
 *    return x + 1;
 *  };
 *  // We can directly convert
 *  // lambda function to TypedPackedFunc
 *  TypedPackedFunc<int(int)> ftyped(addone);
 *  // invoke the function.
 *  int y = ftyped(1);
 *  // Can be directly converted to PackedFunc
 *  PackedFunc packed = ftype;
 * \endcode
 * \tparam R The return value of the function.
 * \tparam Args The argument signature of the function.
 */
template<typename R, typename ...Args>
class TypedPackedFunc<R(Args...)> {
 public:
  /*! \brief short hand for this function type */
  using TSelf = TypedPackedFunc<R(Args...)>;
  /*! \brief default constructor */
  TypedPackedFunc() {}
206 207
  /*! \brief constructor from null */
  TypedPackedFunc(std::nullptr_t null) {}  // NOLINT(*)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
  /*!
   * \brief construct by wrap a PackedFunc
   *
   * Example usage:
   * \code
   * PackedFunc packed([](TVMArgs args, TVMRetValue *rv) {
   *   int x = args[0];
   *   *rv = x + 1;
   *  });
   * // construct from packed function
   * TypedPackedFunc<int(int)> ftyped(packed);
   * // call the typed version.
   * CHECK_EQ(ftyped(1), 2);
   * \endcode
   *
   * \param packed The packed function
   */
225 226 227 228 229 230 231 232 233 234 235
  inline TypedPackedFunc(PackedFunc packed);  // NOLINT(*)
  /*!
   * \brief constructor from TVMRetValue
   * \param value The TVMRetValue
   */
  inline TypedPackedFunc(const TVMRetValue& value);  // NOLINT(*)
  /*!
   * \brief constructor from TVMArgValue
   * \param value The TVMArgValue
   */
  inline TypedPackedFunc(const TVMArgValue& value);  // NOLINT(*)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  /*!
   * \brief construct from a lambda function with the same signature.
   *
   * Example usage:
   * \code
   * auto typed_lambda = [](int x)->int { return x + 1; }
   * // construct from packed function
   * TypedPackedFunc<int(int)> ftyped(typed_lambda);
   * // call the typed version.
   * CHECK_EQ(ftyped(1), 2);
   * \endcode
   *
   * \param typed_lambda typed lambda function.
   * \tparam FLambda the type of the lambda function.
   */
  template<typename FLambda,
           typename = typename std::enable_if<
             std::is_convertible<FLambda,
                                 std::function<R(Args...)>
                                 >::value>::type>
256
  TypedPackedFunc(const FLambda& typed_lambda) {  // NOLINT(*)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    this->AssignTypedLambda(typed_lambda);
  }
  /*!
   * \brief copy assignment operator from typed lambda
   *
   * Example usage:
   * \code
   * // construct from packed function
   * TypedPackedFunc<int(int)> ftyped;
   * ftyped = [](int x) { return x + 1; }
   * // call the typed version.
   * CHECK_EQ(ftyped(1), 2);
   * \endcode
   *
   * \param typed_lambda typed lambda function.
   * \tparam FLambda the type of the lambda function.
   * \returns reference to self.
   */
  template<typename FLambda,
           typename = typename std::enable_if<
             std::is_convertible<FLambda,
                                 std::function<R(Args...)>
                                 >::value>::type>
  TSelf& operator=(FLambda typed_lambda) {  // NOLINT(*)
    this->AssignTypedLambda(typed_lambda);
    return *this;
  }
  /*!
   * \brief copy assignment operator from PackedFunc.
   * \param packed The packed function.
   * \returns reference to self.
   */
  TSelf& operator=(PackedFunc packed) {
    packed_ = packed;
    return *this;
  }
  /*!
   * \brief Invoke the operator.
   * \param args The arguments
   * \returns The return value.
   */
  inline R operator()(Args ...args) const;
  /*!
   * \brief convert to PackedFunc
   * \return the internal PackedFunc
   */
  operator PackedFunc() const {
    return packed();
  }
  /*!
   * \return reference the internal PackedFunc
   */
  const PackedFunc& packed() const {
    return packed_;
  }
312 313 314 315 316 317 318 319
  /*! \return Whether the packed function is nullptr */
  bool operator==(std::nullptr_t null) const {
    return packed_ == nullptr;
  }
  /*! \return Whether the packed function is not nullptr */
  bool operator!=(std::nullptr_t null) const {
    return packed_ != nullptr;
  }
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

 private:
  friend class TVMRetValue;
  /*! \brief The internal packed function */
  PackedFunc packed_;
  /*!
   * \brief Assign the packed field using a typed lambda function.
   *
   * \param flambda The lambda function.
   * \tparam FLambda The lambda function type.
   * \note We capture the lambda when possible for maximum efficiency.
   */
  template<typename FLambda>
  inline void AssignTypedLambda(FLambda flambda);
};

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/*! \brief Arguments into TVM functions. */
class TVMArgs {
 public:
  const TVMValue* values;
  const int* type_codes;
  int num_args;
  /*!
   * \brief constructor
   * \param values The argument values
   * \param type_codes The argument type codes
   * \param num_args number of arguments.
   */
  TVMArgs(const TVMValue* values,
          const int* type_codes,
          int num_args)
      : values(values),
        type_codes(type_codes),
        num_args(num_args) { }
  /*! \return size of the arguments */
  inline int size() const;
  /*!
   * \brief Get i-th argument
   * \param i the index.
   * \return the ith argument.
   */
  inline TVMArgValue operator[](int i) const;
};

/*!
 * \brief Convert type code to its name
 * \param type_code The type code .
 * \return The name of type code.
 */
inline const char* TypeCode2Str(int type_code);

/*!
 * \brief convert a string to TVM type.
 * \param s The string to be converted.
 * \return The corresponding tvm type.
 */
inline TVMType String2TVMType(std::string s);

378 379 380 381 382 383 384
/*!
 * \brief convert a TVM type to string.
 * \param t The type to be converted.
 * \return The corresponding tvm type in string.
 */
inline std::string TVMType2String(TVMType t);

385 386 387 388 389 390
// macro to check type code.
#define TVM_CHECK_TYPE_CODE(CODE, T)                           \
  CHECK_EQ(CODE, T) << " expected "                            \
  << TypeCode2Str(T) << " but get " << TypeCode2Str(CODE)      \

/*!
391 392 393 394 395 396 397 398 399 400 401
 * \brief Type traits to mark if a class is tvm extension type.
 *
 * To enable extension type in C++ must be register () ed via marco.
 * TVM_REGISTER_EXT_TYPE(TypeName) after defining this with this traits.
 *
 * Extension class can be passed and returned via PackedFunc in all tvm runtime.
 * Internally extension class is stored as T*.
 *
 * \tparam T the typename
 */
template<typename T>
402
struct extension_type_info {
403 404 405 406
  static const int code = 0;
};

/*!
407
 * \brief Runtime function table about extension type.
408
 */
409 410
class ExtTypeVTable {
 public:
411 412 413 414
  /*! \brief function to be called to delete a handle */
  void (*destroy)(void* handle);
  /*! \brief function to be called when clone a handle */
  void* (*clone)(void* handle);
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  /*!
   * \brief Register type
   * \tparam T The type to be register.
   * \return The registered vtable.
   */
  template <typename T>
  static inline ExtTypeVTable* Register_();
  /*!
   * \brief Get a vtable based on type code.
   * \param type_code The type code
   * \return The registered vtable.
   */
  TVM_DLL static ExtTypeVTable* Get(int type_code);

 private:
  // Internal registration function.
  TVM_DLL static ExtTypeVTable* RegisterInternal(int type_code, const ExtTypeVTable& vt);
432 433 434
};

/*!
435 436 437 438 439 440
 * \brief Internal base class to
 *  handle conversion to POD values.
 */
class TVMPODValue_ {
 public:
  operator double() const {
441 442 443 444 445 446
    // Allow automatic conversion from int to float
    // This avoids errors when user pass in int from
    // the frontend while the API expects a float.
    if (type_code_ == kDLInt) {
      return static_cast<double>(value_.v_int64);
    }
447
    TVM_CHECK_TYPE_CODE(type_code_, kDLFloat);
448 449 450
    return value_.v_float64;
  }
  operator int64_t() const {
451
    TVM_CHECK_TYPE_CODE(type_code_, kDLInt);
452 453 454
    return value_.v_int64;
  }
  operator uint64_t() const {
455
    TVM_CHECK_TYPE_CODE(type_code_, kDLInt);
456 457 458
    return value_.v_int64;
  }
  operator int() const {
459
    TVM_CHECK_TYPE_CODE(type_code_, kDLInt);
460 461 462 463 464
    CHECK_LE(value_.v_int64,
             std::numeric_limits<int>::max());
    return static_cast<int>(value_.v_int64);
  }
  operator bool() const {
465
    TVM_CHECK_TYPE_CODE(type_code_, kDLInt);
466 467 468 469 470 471 472 473
    return value_.v_int64 != 0;
  }
  operator void*() const {
    if (type_code_ == kNull) return nullptr;
    if (type_code_ == kArrayHandle) return value_.v_handle;
    TVM_CHECK_TYPE_CODE(type_code_, kHandle);
    return value_.v_handle;
  }
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
  operator DLTensor*() const {
    if (type_code_ == kArrayHandle ||
        type_code_ == kNDArrayContainer) {
      return static_cast<DLTensor*>(value_.v_handle);
    } else {
      if (type_code_ == kNull) return nullptr;
      LOG(FATAL) << "Expected "
                 << "DLTensor* or NDArray but get "
                 << TypeCode2Str(type_code_);
      return nullptr;
    }
  }
  operator NDArray() const {
    if (type_code_ == kNull) return NDArray();
    TVM_CHECK_TYPE_CODE(type_code_, kNDArrayContainer);
    return NDArray(static_cast<NDArray::Container*>(value_.v_handle));
490
  }
491
  operator ObjectRef() const {
492 493 494
    if (type_code_ == kNull) {
      return ObjectRef(ObjectPtr<Object>(nullptr));
    }
495
    TVM_CHECK_TYPE_CODE(type_code_, kObjectHandle);
496 497
    return ObjectRef(
        ObjectPtr<Object>(static_cast<Object*>(value_.v_handle)));
498
  }
499 500 501 502
  operator TVMContext() const {
    TVM_CHECK_TYPE_CODE(type_code_, kTVMContext);
    return value_.v_ctx;
  }
503 504 505 506 507 508 509 510 511
  template<typename TNDArray,
           typename = typename std::enable_if<
           std::is_base_of<NDArray, TNDArray>::value>::type>
  TNDArray AsNDArray() const {
    if (type_code_ == kNull) return TNDArray(nullptr);
    auto *container = static_cast<NDArray::Container*>(value_.v_handle);
    CHECK_EQ(container->array_type_code_, array_type_info<TNDArray>::code);
    return TNDArray(container);
  }
512 513
  template<typename TExtension>
  const TExtension& AsExtension() const {
514 515
    CHECK_LT(type_code_, kExtEnd);
    return static_cast<TExtension*>(value_.v_handle)[0];
516
  }
517
  template<typename TObjectRef,
518
           typename = typename std::enable_if<
519
             std::is_class<TObjectRef>::value>::type>
520
  inline bool IsObjectRef() const;
521 522 523
  int type_code() const {
    return type_code_;
  }
524

525 526 527 528 529 530 531 532 533
  /*!
   * \brief return handle as specific pointer type.
   * \tparam T the data type.
   * \return The pointer type.
   */
  template<typename T>
  T* ptr() const {
    return static_cast<T*>(value_.v_handle);
  }
534 535 536 537 538 539 540 541

 protected:
  friend class TVMArgsSetter;
  friend class TVMRetValue;
  TVMPODValue_() : type_code_(kNull) {}
  TVMPODValue_(TVMValue value, int type_code)
      : value_(value), type_code_(type_code) {}

542 543 544 545 546 547 548 549 550 551 552 553 554 555
  /*! \brief The value */
  TVMValue value_;
  /*! \brief the type code */
  int type_code_;
};

/*!
 * \brief A single argument value to PackedFunc.
 *  Containing both type_code and TVMValue
 *
 *  Provides utilities to do type cast into other types.
 */
class TVMArgValue : public TVMPODValue_ {
 public:
556 557
  /*! \brief default constructor */
  TVMArgValue() {}
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
  /*!
   * \brief constructor
   * \param value of the function
   * \param type_code The type code.
   */
  TVMArgValue(TVMValue value, int type_code)
      : TVMPODValue_(value, type_code) {
  }
  // reuse converter from parent
  using TVMPODValue_::operator double;
  using TVMPODValue_::operator int64_t;
  using TVMPODValue_::operator uint64_t;
  using TVMPODValue_::operator int;
  using TVMPODValue_::operator bool;
  using TVMPODValue_::operator void*;
573 574
  using TVMPODValue_::operator DLTensor*;
  using TVMPODValue_::operator NDArray;
575
  using TVMPODValue_::operator TVMContext;
576
  using TVMPODValue_::operator ObjectRef;
577
  using TVMPODValue_::IsObjectRef;
578

579 580
  // conversion operator.
  operator std::string() const {
581 582
    if (type_code_ == kTVMType) {
      return TVMType2String(operator TVMType());
583 584 585 586 587 588
    } else if (type_code_ == kBytes) {
      TVMByteArray* arr = static_cast<TVMByteArray*>(value_.v_handle);
      return std::string(arr->data, arr->size);
    } else {
      TVM_CHECK_TYPE_CODE(type_code_, kStr);
      return std::string(value_.v_str);
589
    }
590 591 592 593 594
  }
  operator TVMType() const {
    if (type_code_ == kStr) {
      return String2TVMType(operator std::string());
    }
595 596 597 598 599 600
    // None type
    if (type_code_ == kNull) {
      TVMType t;
      t.code = kHandle; t.bits = 0; t.lanes = 0;
      return t;
    }
601 602 603 604
    TVM_CHECK_TYPE_CODE(type_code_, kTVMType);
    return value_.v_type;
  }
  operator PackedFunc() const {
605
    if (type_code_ == kNull) return PackedFunc();
606 607 608
    TVM_CHECK_TYPE_CODE(type_code_, kFuncHandle);
    return *ptr<PackedFunc>();
  }
609 610 611 612
  template<typename FType>
  operator TypedPackedFunc<FType>() const {
    return TypedPackedFunc<FType>(operator PackedFunc());
  }
613 614 615 616
  operator Module() const {
    TVM_CHECK_TYPE_CODE(type_code_, kModuleHandle);
    return *ptr<Module>();
  }
617 618 619
  const TVMValue& value() const {
    return value_;
  }
620
  // Deferred extension handler.
621 622
  template<typename TObjectRef>
  inline TObjectRef AsObjectRef() const;
623
  template<typename T,
624
           typename = typename std::enable_if<
625
           std::is_class<T>::value>::type>
626
  inline operator T() const;
627 628
  inline operator tvm::DataType() const;
  inline operator tvm::Expr() const;
629
  inline operator tvm::Integer() const;
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
};

/*!
 * \brief Return Value container,
 *  Unlike TVMArgValue, which only holds reference and do not delete
 *  the underlying container during destruction.
 *
 *  TVMRetValue holds value and will manage the underlying containers
 *  when it stores a complicated data type.
 */
class TVMRetValue : public TVMPODValue_ {
 public:
  /*! \brief default constructor */
  TVMRetValue() {}
  /*!
   * \brief move constructor from anoter return value.
   * \param other The other return value.
   */
  TVMRetValue(TVMRetValue&& other)
      : TVMPODValue_(other.value_, other.type_code_) {
650 651
    other.value_.v_handle = nullptr;
    other.type_code_ = kNull;
652 653 654 655 656 657 658 659 660 661 662 663
  }
  /*! \brief destructor */
  ~TVMRetValue() {
    this->Clear();
  }
  // reuse converter from parent
  using TVMPODValue_::operator double;
  using TVMPODValue_::operator int64_t;
  using TVMPODValue_::operator uint64_t;
  using TVMPODValue_::operator int;
  using TVMPODValue_::operator bool;
  using TVMPODValue_::operator void*;
664
  using TVMPODValue_::operator DLTensor*;
665
  using TVMPODValue_::operator TVMContext;
666
  using TVMPODValue_::operator NDArray;
667
  using TVMPODValue_::operator ObjectRef;
668 669
  using TVMPODValue_::IsObjectRef;

670
  TVMRetValue(const TVMRetValue& other) : TVMPODValue_() {
671 672 673 674
    this->Assign(other);
  }
  // conversion operators
  operator std::string() const {
675 676
    if (type_code_ == kTVMType) {
      return TVMType2String(operator TVMType());
677 678
    } else if (type_code_ == kBytes) {
      return *ptr<std::string>();
679
    }
680 681 682 683 684 685 686 687 688 689 690
    TVM_CHECK_TYPE_CODE(type_code_, kStr);
    return *ptr<std::string>();
  }
  operator TVMType() const {
    if (type_code_ == kStr) {
      return String2TVMType(operator std::string());
    }
    TVM_CHECK_TYPE_CODE(type_code_, kTVMType);
    return value_.v_type;
  }
  operator PackedFunc() const {
691
    if (type_code_ == kNull) return PackedFunc();
692 693 694
    TVM_CHECK_TYPE_CODE(type_code_, kFuncHandle);
    return *ptr<PackedFunc>();
  }
695 696 697 698
  template<typename FType>
  operator TypedPackedFunc<FType>() const {
    return TypedPackedFunc<FType>(operator PackedFunc());
  }
699 700 701 702
  operator Module() const {
    TVM_CHECK_TYPE_CODE(type_code_, kModuleHandle);
    return *ptr<Module>();
  }
703 704 705 706 707 708 709 710 711
  // Assign operators
  TVMRetValue& operator=(TVMRetValue&& other) {
    this->Clear();
    value_ = other.value_;
    type_code_ = other.type_code_;
    other.type_code_ = kNull;
    return *this;
  }
  TVMRetValue& operator=(double value) {
712
    this->SwitchToPOD(kDLFloat);
713 714 715 716 717 718 719 720 721 722 723 724 725 726
    value_.v_float64 = value;
    return *this;
  }
  TVMRetValue& operator=(std::nullptr_t value) {
    this->SwitchToPOD(kNull);
    value_.v_handle = value;
    return *this;
  }
  TVMRetValue& operator=(void* value) {
    this->SwitchToPOD(kHandle);
    value_.v_handle = value;
    return *this;
  }
  TVMRetValue& operator=(int64_t value) {
727
    this->SwitchToPOD(kDLInt);
728 729 730 731
    value_.v_int64 = value;
    return *this;
  }
  TVMRetValue& operator=(int value) {
732
    this->SwitchToPOD(kDLInt);
733 734 735
    value_.v_int64 = value;
    return *this;
  }
736 737 738 739 740
  TVMRetValue& operator=(TVMContext value) {
    this->SwitchToPOD(kTVMContext);
    value_.v_ctx = value;
    return *this;
  }
741 742 743 744 745 746
  TVMRetValue& operator=(TVMType t) {
    this->SwitchToPOD(kTVMType);
    value_.v_type = t;
    return *this;
  }
  TVMRetValue& operator=(bool value) {
747
    this->SwitchToPOD(kDLInt);
748 749 750 751 752 753 754
    value_.v_int64 = value;
    return *this;
  }
  TVMRetValue& operator=(std::string value) {
    this->SwitchToClass(kStr, value);
    return *this;
  }
755 756 757 758
  TVMRetValue& operator=(TVMByteArray value) {
    this->SwitchToClass(kBytes, std::string(value.data, value.size));
    return *this;
  }
759 760 761 762 763 764 765
  TVMRetValue& operator=(NDArray other) {
    this->Clear();
    type_code_ = kNDArrayContainer;
    value_.v_handle = other.data_;
    other.data_ = nullptr;
    return *this;
  }
766
  TVMRetValue& operator=(ObjectRef other) {
767 768 769 770 771 772 773 774 775 776 777 778 779
    return operator=(std::move(other.data_));
  }
  template<typename T>
  TVMRetValue& operator=(ObjectPtr<T> other) {
    if (other.data_ != nullptr) {
      this->Clear();
      type_code_ = kObjectHandle;
      // move the handle out
      value_.v_handle = other.data_;
      other.data_ = nullptr;
    } else {
      SwitchToPOD(kNull);
    }
780 781
    return *this;
  }
782 783 784 785
  TVMRetValue& operator=(PackedFunc f) {
    this->SwitchToClass(kFuncHandle, f);
    return *this;
  }
786 787 788 789
  template<typename FType>
  TVMRetValue& operator=(const TypedPackedFunc<FType>& f) {
    return operator=(f.packed());
  }
790 791 792 793
  TVMRetValue& operator=(Module m) {
    this->SwitchToClass(kModuleHandle, m);
    return *this;
  }
794 795 796 797
  TVMRetValue& operator=(const TVMRetValue& other) {  // NOLINT(*0
    this->Assign(other);
    return *this;
  }
798
  TVMRetValue& operator=(const TVMArgValue& other) {
799 800 801
    this->Assign(other);
    return *this;
  }
802 803
  template<typename T,
           typename = typename std::enable_if<
804
             extension_type_info<T>::code != 0>::type>
805 806
  TVMRetValue& operator=(const T& other) {
    this->SwitchToClass<T>(
807
        extension_type_info<T>::code, other);
808 809
    return *this;
  }
810 811 812 813 814 815 816 817 818 819 820 821
  /*!
   * \brief Move the value back to front-end via C API.
   *  This marks the current container as null.
   *  The managed resources is moved to front-end and
   *  the front end should take charge in managing them.
   *
   * \param ret_value The return value.
   * \param ret_type_code The return type code.
   */
  void MoveToCHost(TVMValue* ret_value,
                   int* ret_type_code) {
    // cannot move str; need specially handle.
822
    CHECK(type_code_ != kStr && type_code_ != kBytes);
823 824 825 826
    *ret_value = value_;
    *ret_type_code = type_code_;
    type_code_ = kNull;
  }
827 828
  /*! \return The value field, if the data is POD */
  const TVMValue& value() const {
829
    CHECK(type_code_ != kObjectHandle &&
830
          type_code_ != kFuncHandle &&
831
          type_code_ != kModuleHandle &&
832 833 834
          type_code_ != kStr) << "TVMRetValue.value can only be used for POD data";
    return value_;
  }
835
  // ObjectRef related extenstions: in tvm/packed_func_ext.h
836 837 838 839
  template<typename T,
           typename = typename std::enable_if<
             std::is_class<T>::value>::type>
  inline operator T() const;
840 841
  template<typename TObjectRef>
  inline TObjectRef AsObjectRef() const;
842
  // type related
843 844
  inline operator tvm::DataType() const;
  inline TVMRetValue& operator=(const tvm::DataType& other);
845 846 847 848 849

 private:
  template<typename T>
  void Assign(const T& other) {
    switch (other.type_code()) {
850
      case kStr: {
851 852 853
        SwitchToClass<std::string>(kStr, other);
        break;
      }
854 855 856 857
      case kBytes: {
        SwitchToClass<std::string>(kBytes, other);
        break;
      }
858 859 860 861
      case kFuncHandle: {
        SwitchToClass<PackedFunc>(kFuncHandle, other);
        break;
      }
862
      case kModuleHandle: {
863
        SwitchToClass<Module>(kModuleHandle, other);
864 865
        break;
      }
866 867 868 869
      case kNDArrayContainer: {
        *this = other.operator NDArray();
        break;
      }
870
      case kObjectHandle: {
871
        *this = other.operator ObjectRef();
872 873
        break;
      }
874
      default: {
875 876 877 878
        if (other.type_code() < kExtBegin) {
          SwitchToPOD(other.type_code());
          value_ = other.value_;
        } else {
879 880 881
#if TVM_RUNTIME_HEADER_ONLY
          LOG(FATAL) << "Header only mode do not support ext type";
#else
882 883 884 885 886
          this->Clear();
          type_code_ = other.type_code();
          value_.v_handle =
              (*(ExtTypeVTable::Get(other.type_code())->clone))(
                  other.value().v_handle);
887
#endif
888
        }
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
        break;
      }
    }
  }
  // get the internal container.
  void SwitchToPOD(int type_code) {
    if (type_code_ != type_code) {
      this->Clear();
      type_code_ = type_code;
    }
  }
  template<typename T>
  void SwitchToClass(int type_code, T v) {
    if (type_code_ != type_code) {
      this->Clear();
      type_code_ = type_code;
      value_.v_handle = new T(v);
    } else {
      *static_cast<T*>(value_.v_handle) = v;
    }
  }
  void Clear() {
    if (type_code_ == kNull) return;
    switch (type_code_) {
      case kStr: delete ptr<std::string>(); break;
      case kFuncHandle: delete ptr<PackedFunc>(); break;
915
      case kModuleHandle: delete ptr<Module>(); break;
916 917 918 919
      case kNDArrayContainer: {
        static_cast<NDArray::Container*>(value_.v_handle)->DecRef();
        break;
      }
920
      case kObjectHandle: {
921
        static_cast<Object*>(value_.v_handle)->DecRef();
922 923
        break;
      }
924
    }
925
    if (type_code_ > kExtBegin) {
926 927 928
#if TVM_RUNTIME_HEADER_ONLY
          LOG(FATAL) << "Header only mode do not support ext type";
#else
929
      (*(ExtTypeVTable::Get(type_code_)->destroy))(value_.v_handle);
930
#endif
931
    }
932 933 934 935 936 937 938
    type_code_ = kNull;
  }
};

// implementation details
inline const char* TypeCode2Str(int type_code) {
  switch (type_code) {
939 940 941
    case kDLInt: return "int";
    case kDLUInt: return "uint";
    case kDLFloat: return "float";
942
    case kStr: return "str";
943
    case kBytes: return "bytes";
944
    case kHandle: return "handle";
945 946 947
    case kNull: return "NULL";
    case kArrayHandle: return "ArrayHandle";
    case kTVMType: return "TVMType";
948
    case kTVMContext: return "TVMContext";
949
    case kFuncHandle: return "FunctionHandle";
950
    case kModuleHandle: return "ModuleHandle";
951
    case kNDArrayContainer: return "NDArrayContainer";
952
    case kObjectHandle: return "ObjectCell";
953 954 955 956 957
    default: LOG(FATAL) << "unknown type_code="
                        << static_cast<int>(type_code); return "";
  }
}

nhynes committed
958
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
959
inline std::ostream& operator<<(std::ostream& os, TVMType t) {  // NOLINT(*)
960 961 962
  if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) {
    os << "bool"; return os;
  }
963
  if (t.code < kCustomBegin) {
964
    os << TypeCode2Str(t.code);
965 966
  } else {
    os << "custom[" << GetCustomTypeName(t.code) << "]";
967
  }
968 969
  if (t.code == kHandle) return os;
  os << static_cast<int>(t.bits);
970 971 972 973 974
  if (t.lanes != 1) {
    os << 'x' << static_cast<int>(t.lanes);
  }
  return os;
}
975

nhynes committed
976
#endif
977 978

inline std::string TVMType2String(TVMType t) {
979
  if (t.bits == 0) return "";
nhynes committed
980
#ifndef _LIBCPP_SGX_NO_IOSTREAMS
981 982 983
  std::ostringstream os;
  os << t;
  return os.str();
nhynes committed
984
#else
985 986 987
  if (t.bits == 1 && t.lanes == 1 && t.code == kDLUInt) {
    return "bool";
  }
988
  if (t.code < kCustomBegin) {
989
    repr += TypeCode2Str(t.code);
990 991
  } else {
    repr += "custom[" + GetCustomTypeName(t.code) + "]";
992
  }
nhynes committed
993 994 995 996 997 998 999
  if (t.code == kHandle) return repr;
  repr += std::to_string(static_cast<int>(t.bits));
  if (t.lanes != 1) {
    repr += "x" + std::to_string(static_cast<int>(t.lanes));
  }
  return repr;
#endif
1000 1001
}

1002 1003
inline TVMType String2TVMType(std::string s) {
  TVMType t;
1004 1005 1006 1007 1008
  // handle None type
  if (s.length() == 0) {
    t.bits = 0; t.lanes = 0; t.code = kHandle;
    return t;
  }
1009 1010 1011
  t.bits = 32; t.lanes = 1;
  const char* scan;
  if (s.substr(0, 3) == "int") {
1012
    t.code = kDLInt;  scan = s.c_str() + 3;
1013
  } else if (s.substr(0, 4) == "uint") {
1014
    t.code = kDLUInt; scan = s.c_str() + 4;
1015
  } else if (s.substr(0, 5) == "float") {
1016
    t.code = kDLFloat; scan = s.c_str() + 5;
1017
  } else if (s.substr(0, 6) == "handle") {
1018 1019 1020
    t.code = kHandle;
    t.bits = 64;  // handle uses 64 bit by default.
    scan = s.c_str() + 6;
1021 1022 1023 1024 1025
  } else if (s == "bool") {
    t.code = kDLUInt;
    t.bits = 1;
    t.lanes = 1;
    return t;
1026 1027
  } else if (s.substr(0, 6) == "custom") {
    t.code = ParseCustomDatatype(s, &scan);
1028 1029 1030 1031
  } else {
    scan = s.c_str();
    LOG(FATAL) << "unknown type " << s;
  }
nhynes committed
1032
  char* xdelim;  // emulate sscanf("%ux%u", bits, lanes)
1033 1034
  uint8_t bits = static_cast<uint8_t>(strtoul(scan, &xdelim, 10));
  if (bits != 0) t.bits = bits;
1035
  char* endpt = xdelim;
nhynes committed
1036
  if (*xdelim == 'x') {
1037
    t.lanes = static_cast<uint16_t>(strtoul(xdelim + 1, &endpt, 10));
nhynes committed
1038
  }
1039
  CHECK(endpt == s.c_str() + s.length()) << "unknown type " << s;
1040 1041 1042 1043 1044 1045 1046
  return t;
}

inline TVMArgValue TVMArgs::operator[](int i) const {
  CHECK_LT(i, num_args)
      << "not enough argument passed, "
      << num_args << " passed"
1047
      << " but request arg[" << i << "].";
1048 1049 1050 1051 1052 1053 1054 1055 1056
  return TVMArgValue(values[i], type_codes[i]);
}

inline int TVMArgs::size() const {
  return num_args;
}

inline void PackedFunc::CallPacked(TVMArgs args, TVMRetValue* rv) const {
  body_(args, rv);
1057 1058
}

1059 1060 1061 1062
inline PackedFunc::FType PackedFunc::body() const {
  return body_;
}

1063 1064
// internal namespace
namespace detail {
1065 1066

template<bool stop, std::size_t I, typename F>
1067
struct for_each_dispatcher {
1068 1069 1070 1071 1072
  template<typename T, typename ...Args>
  static void run(const F& f, T&& value, Args&&... args) {  // NOLINT(*)
    f(I, std::forward<T>(value));
    for_each_dispatcher<sizeof...(Args) == 0, (I+1), F>
        ::run(f, std::forward<Args>(args)...);
1073 1074 1075
  }
};

1076 1077 1078
template<std::size_t I, typename F>
struct for_each_dispatcher<true, I, F>  {
  static void run(const F& f) {}  // NOLINT(*)
1079 1080 1081
};

template<typename F, typename ...Args>
1082 1083 1084
inline void for_each(const F& f, Args&&... args) {  // NOLINT(*)
  for_each_dispatcher<sizeof...(Args) == 0, 0, F>
      ::run(f, std::forward<Args>(args)...);
1085
}
1086
}  // namespace detail
1087

1088 1089 1090
/* \brief argument settter to PackedFunc */
class TVMArgsSetter {
 public:
1091 1092
  TVMArgsSetter(TVMValue* values, int* type_codes)
      : values_(values), type_codes_(type_codes) {}
1093 1094
  // setters for POD types
  template<typename T,
1095 1096
           typename = typename std::enable_if<
             std::is_integral<T>::value>::type>
1097 1098
  void operator()(size_t i, T value) const {
    values_[i].v_int64 = static_cast<int64_t>(value);
1099
    type_codes_[i] = kDLInt;
1100 1101 1102 1103 1104
  }
  void operator()(size_t i, uint64_t value) const {
    values_[i].v_int64 = static_cast<int64_t>(value);
    CHECK_LE(value,
             static_cast<uint64_t>(std::numeric_limits<int64_t>::max()));
1105
    type_codes_[i] = kDLInt;
1106 1107 1108
  }
  void operator()(size_t i, double value) const {
    values_[i].v_float64 = value;
1109
    type_codes_[i] = kDLFloat;
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
  }
  void operator()(size_t i, std::nullptr_t value) const {
    values_[i].v_handle = value;
    type_codes_[i] = kNull;
  }
  void operator()(size_t i, const TVMArgValue& value) const {
    values_[i] = value.value_;
    type_codes_[i] = value.type_code_;
  }
  void operator()(size_t i, void* value) const {
    values_[i].v_handle = value;
    type_codes_[i] = kHandle;
  }
1123
  void operator()(size_t i, DLTensor* value) const {
1124 1125 1126
    values_[i].v_handle = value;
    type_codes_[i] = kArrayHandle;
  }
1127 1128 1129 1130
  void operator()(size_t i, TVMContext value) const {
    values_[i].v_ctx = value;
    type_codes_[i] = kTVMContext;
  }
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  void operator()(size_t i, TVMType value) const {
    values_[i].v_type = value;
    type_codes_[i] = kTVMType;
  }
  void operator()(size_t i, const char* value) const {
    values_[i].v_str = value;
    type_codes_[i] = kStr;
  }
  // setters for container type
  // They must be reference(instead of const ref)
  // to make sure they are alive in the tuple(instead of getting converted)
1142
  void operator()(size_t i, const std::string& value) const {  // NOLINT(*)
1143 1144 1145
    values_[i].v_str = value.c_str();
    type_codes_[i] = kStr;
  }
1146 1147
  void operator()(size_t i, const TVMByteArray& value) const {  // NOLINT(*)
    values_[i].v_handle = const_cast<TVMByteArray*>(&value);
1148 1149
    type_codes_[i] = kBytes;
  }
1150 1151
  void operator()(size_t i, const PackedFunc& value) const {  // NOLINT(*)
    values_[i].v_handle = const_cast<PackedFunc*>(&value);
1152 1153
    type_codes_[i] = kFuncHandle;
  }
1154 1155 1156 1157
  template<typename FType>
  void operator()(size_t i, const TypedPackedFunc<FType>& value) const {  // NOLINT(*)
    operator()(i, value.packed());
  }
1158 1159
  void operator()(size_t i, const Module& value) const {  // NOLINT(*)
    values_[i].v_handle = const_cast<Module*>(&value);
1160 1161
    type_codes_[i] = kModuleHandle;
  }
1162 1163 1164 1165
  void operator()(size_t i, const NDArray& value) const {  // NOLINT(*)
    values_[i].v_handle = value.data_;
    type_codes_[i] = kNDArrayContainer;
  }
1166
  void operator()(size_t i, const ObjectRef& value) const {  // NOLINT(*)
1167 1168 1169 1170 1171 1172
    if (value.defined()) {
      values_[i].v_handle = value.data_.data_;
      type_codes_[i] = kObjectHandle;
    } else {
      type_codes_[i] = kNull;
    }
1173
  }
1174
  void operator()(size_t i, const TVMRetValue& value) const {  // NOLINT(*)
1175 1176 1177 1178
    if (value.type_code() == kStr) {
      values_[i].v_str = value.ptr<std::string>()->c_str();
      type_codes_[i] = kStr;
    } else {
1179
      CHECK_NE(value.type_code(), kBytes) << "not handled.";
1180 1181 1182 1183
      values_[i] = value.value_;
      type_codes_[i] = value.type_code();
    }
  }
1184 1185 1186
  // extension
  template<typename T,
           typename = typename std::enable_if<
1187
             extension_type_info<T>::code != 0>::type>
1188
  inline void operator()(size_t i, const T& value) const;
1189
  inline void operator()(size_t i, const tvm::DataType& t) const;
1190 1191 1192 1193 1194 1195 1196 1197

 private:
  /*! \brief The values fields */
  TVMValue* values_;
  /*! \brief The type code fields */
  int* type_codes_;
};

1198
template<typename... Args>
1199
inline TVMRetValue PackedFunc::operator()(Args&& ...args) const {
1200
  const int kNumArgs = sizeof...(Args);
1201 1202 1203
  const int kArraySize = kNumArgs > 0 ? kNumArgs : 1;
  TVMValue values[kArraySize];
  int type_codes[kArraySize];
1204
  detail::for_each(TVMArgsSetter(values, type_codes),
1205
                   std::forward<Args>(args)...);
1206 1207 1208
  TVMRetValue rv;
  body_(TVMArgs(values, type_codes, kNumArgs), &rv);
  return rv;
1209
}
1210

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
namespace detail {
template<typename R, int nleft, int index, typename F>
struct unpack_call_dispatcher {
  template<typename ...Args>
  static void run(const F& f,
                  const TVMArgs& args_pack,
                  TVMRetValue* rv,
                  Args&&... unpacked_args) {
    unpack_call_dispatcher<R, nleft - 1, index + 1, F>
        ::run(f, args_pack, rv,
              std::forward<Args>(unpacked_args)...,
              args_pack[index]);
  }
};

template<typename R, int index, typename F>
struct unpack_call_dispatcher<R, 0, index, F> {
  template<typename ...Args>
  static void run(const F& f,
                  const TVMArgs& args_pack,
                  TVMRetValue* rv,
                  Args&&... unpacked_args) {
    *rv = R(f(std::forward<Args>(unpacked_args)...));
  }
};

template<int index, typename F>
struct unpack_call_dispatcher<void, 0, index, F> {
  template<typename ...Args>
  static void run(const F& f,
                  const TVMArgs& args_pack,
                  TVMRetValue* rv,
                  Args&&... unpacked_args) {
    f(std::forward<Args>(unpacked_args)...);
  }
};

template<typename R, int nargs, typename F>
inline void unpack_call(const F& f, const TVMArgs& args, TVMRetValue* rv) {
  unpack_call_dispatcher<R, nargs, 0, F>::run(f, args, rv);
}

template<typename R, typename ...Args>
inline R call_packed(const PackedFunc& pf, Args&& ...args) {
  return R(pf(std::forward<Args>(args)...));
}

template<typename R>
struct typed_packed_call_dispatcher {
  template<typename ...Args>
  static inline R run(const PackedFunc& pf, Args&& ...args) {
    return pf(std::forward<Args>(args)...);
  }
};

template<>
struct typed_packed_call_dispatcher<void> {
  template<typename ...Args>
  static inline void run(const PackedFunc& pf, Args&& ...args) {
    pf(std::forward<Args>(args)...);
  }
};
}  // namespace detail

template<typename R, typename ...Args>
1276 1277 1278 1279
TypedPackedFunc<R(Args...)>::TypedPackedFunc(PackedFunc packed)
  : packed_(packed) {}

template<typename R, typename ...Args>
1280 1281 1282 1283 1284 1285 1286 1287
TypedPackedFunc<R(Args...)>::TypedPackedFunc(const TVMRetValue& value)
    : packed_(value.operator PackedFunc()) {}

template<typename R, typename ...Args>
TypedPackedFunc<R(Args...)>::TypedPackedFunc(const TVMArgValue& value)
    : packed_(value.operator PackedFunc()) {}

template<typename R, typename ...Args>
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
template<typename FType>
inline void TypedPackedFunc<R(Args...)>::AssignTypedLambda(FType flambda) {
  packed_ = PackedFunc([flambda](const TVMArgs& args, TVMRetValue* rv) {
      detail::unpack_call<R, sizeof...(Args)>(flambda, args, rv);
    });
}

template<typename R, typename ...Args>
inline R TypedPackedFunc<R(Args...)>::operator()(Args... args) const {
  return detail::typed_packed_call_dispatcher<R>
      ::run(packed_, std::forward<Args>(args)...);
}

1301 1302
// extension and node type handling
namespace detail {
1303
template<typename T, typename TSrc, bool is_ext, bool is_nd>
1304 1305
struct TVMValueCast {
  static T Apply(const TSrc* self) {
1306
    static_assert(!is_ext && !is_nd, "The default case accepts only non-extensions");
1307
    return self->template AsObjectRef<T>();
1308 1309 1310 1311
  }
};

template<typename T, typename TSrc>
1312
struct TVMValueCast<T, TSrc, true, false> {
1313 1314 1315 1316
  static T Apply(const TSrc* self) {
    return self->template AsExtension<T>();
  }
};
1317 1318 1319 1320 1321 1322 1323 1324

template<typename T, typename TSrc>
struct TVMValueCast<T, TSrc, false, true> {
  static T Apply(const TSrc* self) {
    return self->template AsNDArray<T>();
  }
};

1325 1326 1327 1328 1329
}  // namespace detail

template<typename T, typename>
inline TVMArgValue::operator T() const {
  return detail::
1330 1331 1332
      TVMValueCast<T, TVMArgValue,
                   (extension_type_info<T>::code != 0),
                   (array_type_info<T>::code > 0)>
1333 1334 1335 1336 1337 1338
      ::Apply(this);
}

template<typename T, typename>
inline TVMRetValue::operator T() const {
  return detail::
1339 1340 1341
      TVMValueCast<T, TVMRetValue,
                   (extension_type_info<T>::code != 0),
                   (array_type_info<T>::code > 0)>
1342 1343 1344
      ::Apply(this);
}

1345 1346
template<typename T, typename>
inline void TVMArgsSetter::operator()(size_t i, const T& value) const {
1347
  static_assert(extension_type_info<T>::code != 0,
1348
                "Need to have extesion code");
1349
  type_codes_[i] = extension_type_info<T>::code;
1350 1351 1352
  values_[i].v_handle = const_cast<T*>(&value);
}

1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
// extension type handling
template<typename T>
struct ExtTypeInfo {
  static void destroy(void* handle) {
    delete static_cast<T*>(handle);
  }
  static void* clone(void* handle) {
    return new T(*static_cast<T*>(handle));
  }
};

1364 1365
template<typename T>
inline ExtTypeVTable* ExtTypeVTable::Register_() {
1366
  const int code = extension_type_info<T>::code;
1367
  static_assert(code != 0,
1368
                "require extension_type_info traits to be declared with non-zero code");
1369 1370 1371 1372
  ExtTypeVTable vt;
  vt.clone = ExtTypeInfo<T>::clone;
  vt.destroy = ExtTypeInfo<T>::destroy;
  return ExtTypeVTable::RegisterInternal(code, vt);
1373
}
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387

// Implement Module::GetFunction
// Put implementation in this file so we have seen the PackedFunc
inline PackedFunc Module::GetFunction(const std::string& name, bool query_imports) {
  PackedFunc pf = node_->GetFunction(name, node_);
  if (pf != nullptr) return pf;
  if (query_imports) {
    for (const Module& m : node_->imports_) {
      pf = m.node_->GetFunction(name, m.node_);
      if (pf != nullptr) return pf;
    }
  }
  return pf;
}
1388 1389
}  // namespace runtime
}  // namespace tvm
1390
#endif  // TVM_RUNTIME_PACKED_FUNC_H_