mxnet
ffi_helper.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
24 // Acknowledgement: This file originates from incubator-tvm
25 #ifndef MXNET_RUNTIME_FFI_HELPER_H_
26 #define MXNET_RUNTIME_FFI_HELPER_H_
27 
28 #include <mxnet/runtime/object.h>
30 #include <mxnet/runtime/memory.h>
31 #include <limits>
32 
33 namespace mxnet {
34 namespace runtime {
35 
37 class EllipsisObj : public Object {
38  public:
39  static constexpr const uint32_t _type_index = TypeIndex::kEllipsis;
40  static constexpr const char* _type_key = "MXNet.Ellipsis";
42 };
43 
45  return ObjectRef(make_object<EllipsisObj>());
46 }
47 
49 class SliceObj : public Object {
50  public:
51  int64_t start;
52  int64_t stop;
53  int64_t step;
54 
55  static constexpr const uint32_t _type_index = TypeIndex::kSlice;
56  static constexpr const char* _type_key = "MXNet.Slice";
58 };
59 
60 class Slice : public ObjectRef {
61  public:
62  explicit inline Slice(int64_t start,
63  int64_t stop,
64  int64_t step,
65  ObjectPtr<SliceObj>&& data = make_object<SliceObj>()) {
66  data->start = start;
67  data->stop = stop;
68  data->step = step;
69  data_ = std::move(data);
70  }
71 
72  explicit inline Slice(int64_t stop) : Slice(kNoneValue, stop, kNoneValue) {}
73 
74  // constant to represent None.
75  static constexpr int64_t kNoneValue = std::numeric_limits<int64_t>::min();
76 
78 };
79 
80 int64_t inline SliceNoneValue() {
81  return Slice::kNoneValue;
82 }
83 
84 class IntegerObj : public Object {
85  public:
86  int64_t value;
87  static constexpr const uint32_t _type_index = TypeIndex::kInteger;
88  static constexpr const char* _type_key = "MXNet.Integer";
90 };
91 
92 class Integer : public ObjectRef {
93  public:
94  explicit Integer(int64_t value, ObjectPtr<IntegerObj>&& data = make_object<IntegerObj>()) {
95  data->value = value;
96  data_ = std::move(data);
97  }
99 };
100 
101 class FloatObj : public Object {
102  public:
103  double value;
104  static constexpr const uint32_t _type_index = TypeIndex::kFloat;
105  static constexpr const char* _type_key = "MXNet.Float";
107 };
108 
109 class Float : public ObjectRef {
110  public:
111  explicit Float(double value, ObjectPtr<FloatObj>&& data = make_object<FloatObj>()) {
112  data->value = value;
113  data_ = std::move(data);
114  }
116 };
117 
118 // Helper functions for fast FFI implementations
122 class ADTBuilder {
123  public:
125  ADTBuilder() = default;
126 
127  explicit inline ADTBuilder(uint32_t tag, uint32_t size)
128  : data_(make_inplace_array_object<ADTObj, ObjectRef>(size)) {
129  data_->size = size;
130  }
131 
132  template <typename... Args>
133  void inline EmplaceInit(size_t idx, Args&&... args) {
134  data_->EmplaceInit(idx, std::forward<Args>(args)...);
135  }
136 
137  ADT inline Get() {
138  return ADT(std::move(data_));
139  }
140 
141  private:
142  friend class ADT;
143  ObjectPtr<ADTObj> data_;
144 };
145 } // namespace runtime
146 } // namespace mxnet
147 #endif // MXNET_RUNTIME_FFI_HELPER_H_
mxnet
namespace of mxnet
Definition: api_registry.h:33
mxnet::runtime::SliceObj::stop
int64_t stop
Definition: ffi_helper.h:52
mxnet::runtime::EllipsisObj::_type_index
static constexpr const uint32_t _type_index
Definition: ffi_helper.h:39
mxnet::runtime::Object
base class of all object containers.
Definition: object.h:151
container.h
Common POD(plain old data) container types.
mxnet::runtime::Float
Definition: ffi_helper.h:109
mxnet::runtime::SliceObj::_type_index
static constexpr const uint32_t _type_index
Definition: ffi_helper.h:55
mxnet::runtime::SliceObj
Slice.
Definition: ffi_helper.h:49
mxnet::runtime::EllipsisObj
Ellipsis.
Definition: ffi_helper.h:37
mxnet::runtime::Integer
Definition: ffi_helper.h:92
mxnet::runtime::ObjectPtr
A custom smart pointer for Object.
Definition: object.h:346
mxnet::runtime::ADTBuilder::EmplaceInit
void EmplaceInit(size_t idx, Args &&... args)
Definition: ffi_helper.h:133
mxnet::runtime::IntegerObj
Definition: ffi_helper.h:84
mxnet::runtime::Slice::Slice
Slice(int64_t start, int64_t stop, int64_t step, ObjectPtr< SliceObj > &&data=make_object< SliceObj >())
Definition: ffi_helper.h:62
mxnet::runtime::IntegerObj::_type_key
static constexpr const char * _type_key
Definition: ffi_helper.h:88
MXNET_DECLARE_FINAL_OBJECT_INFO
#define MXNET_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:668
mxnet::runtime::CreateEllipsis
ObjectRef CreateEllipsis()
Definition: ffi_helper.h:44
mxnet::runtime::SliceNoneValue
int64_t SliceNoneValue()
Definition: ffi_helper.h:80
mxnet::runtime::Integer::Integer
Integer(int64_t value, ObjectPtr< IntegerObj > &&data=make_object< IntegerObj >())
Definition: ffi_helper.h:94
mxnet::runtime::ADT
reference to algebraic data type objects.
Definition: container.h:209
mxnet::runtime::IntegerObj::_type_index
static constexpr const uint32_t _type_index
Definition: ffi_helper.h:87
mxnet::runtime::SliceObj::_type_key
static constexpr const char * _type_key
Definition: ffi_helper.h:56
mxnet::runtime::Float::Float
Float(double value, ObjectPtr< FloatObj > &&data=make_object< FloatObj >())
Definition: ffi_helper.h:111
mxnet::runtime::kSlice
@ kSlice
Definition: object.h:60
mxnet::runtime::kEllipsis
@ kEllipsis
Definition: object.h:59
mxnet::runtime::ADTBuilder::ADTBuilder
ADTBuilder(uint32_t tag, uint32_t size)
Definition: ffi_helper.h:127
mxnet::runtime::EllipsisObj::_type_key
static constexpr const char * _type_key
Definition: ffi_helper.h:40
mxnet::runtime::kFloat
@ kFloat
Definition: object.h:62
mxnet::runtime::kInteger
@ kInteger
Definition: object.h:61
mxnet::runtime::Slice
Definition: ffi_helper.h:60
mxnet::runtime::ADTBuilder::ADT
friend class ADT
Definition: ffi_helper.h:142
mxnet::runtime::ADTBuilder::ADTBuilder
ADTBuilder()=default
default constructor
mxnet::runtime::SliceObj::start
int64_t start
Definition: ffi_helper.h:51
memory.h
Runtime memory management.
mxnet::runtime::ObjectRef::data_
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:575
mxnet::runtime::FloatObj::value
double value
Definition: ffi_helper.h:103
mxnet::runtime::ADTObj
An object representing a structure or enumeration.
Definition: container.h:165
mxnet::runtime::Slice::Slice
Slice(int64_t stop)
Definition: ffi_helper.h:72
mxnet::runtime::IntegerObj::value
int64_t value
Definition: ffi_helper.h:86
mxnet::runtime::ObjectRef
Base class of all object reference.
Definition: object.h:500
mxnet::runtime::SliceObj::step
int64_t step
Definition: ffi_helper.h:53
mxnet::runtime::FloatObj::_type_index
static constexpr const uint32_t _type_index
Definition: ffi_helper.h:104
mxnet::runtime::ADTBuilder::Get
ADT Get()
Definition: ffi_helper.h:137
mxnet::runtime::Slice::kNoneValue
static constexpr int64_t kNoneValue
Definition: ffi_helper.h:75
MXNET_DEFINE_OBJECT_REF_METHODS
#define MXNET_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:689
mxnet::runtime::FloatObj
Definition: ffi_helper.h:101
mxnet::runtime::make_inplace_array_object
ObjectPtr< ArrayType > make_inplace_array_object(size_t num_elems, Args &&... args)
Definition: memory.h:204
mxnet::runtime::ADTBuilder
A builder class that helps to incrementally build ADT.
Definition: ffi_helper.h:122
mxnet::runtime::FloatObj::_type_key
static constexpr const char * _type_key
Definition: ffi_helper.h:105
object.h
A managed object in MXNet runtime.