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, int64_t stop, int64_t step,
63  ObjectPtr<SliceObj>&& data = make_object<SliceObj>()) {
64  data->start = start;
65  data->stop = stop;
66  data->step = step;
67  data_ = std::move(data);
68  }
69 
70  explicit inline Slice(int64_t stop)
71  : Slice(kNoneValue, stop, kNoneValue) {
72  }
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,
95  ObjectPtr<IntegerObj>&& data = make_object<IntegerObj>()) {
96  data->value = value;
97  data_ = std::move(data);
98  }
100 };
101 
102 // Helper functions for fast FFI implementations
106 class ADTBuilder {
107  public:
109  ADTBuilder() = default;
110 
111  explicit inline ADTBuilder(uint32_t tag, uint32_t size)
112  : data_(make_inplace_array_object<ADTObj, ObjectRef>(size)) {
113  data_->size = size;
114  }
115 
116  template <typename... Args>
117  void inline EmplaceInit(size_t idx, Args&&... args) {
118  data_->EmplaceInit(idx, std::forward<Args>(args)...);
119  }
120 
121  ADT inline Get() {
122  return ADT(std::move(data_));
123  }
124 
125  private:
126  friend class ADT;
127  ObjectPtr<ADTObj> data_;
128 };
129 } // namespace runtime
130 } // namespace mxnet
131 #endif // MXNET_RUNTIME_FFI_HELPER_H_
int64_t start
Definition: ffi_helper.h:51
static constexpr const uint32_t _type_index
Definition: ffi_helper.h:39
#define MXNET_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:686
void EmplaceInit(size_t idx, Args &&...args)
Definition: ffi_helper.h:117
Slice.
Definition: ffi_helper.h:49
Runtime memory management.
namespace of mxnet
Definition: api_registry.h:33
static constexpr int64_t kNoneValue
Definition: ffi_helper.h:75
A custom smart pointer for Object.
Definition: object.h:345
Slice(int64_t start, int64_t stop, int64_t step, ObjectPtr< SliceObj > &&data=make_object< SliceObj >())
Definition: ffi_helper.h:62
Definition: ffi_helper.h:84
An object representing a structure or enumeration.
Definition: container.h:166
Base class of all object reference.
Definition: object.h:499
MXNET_DECLARE_FINAL_OBJECT_INFO(EllipsisObj, Object)
ADTBuilder(uint32_t tag, uint32_t size)
Definition: ffi_helper.h:111
int64_t stop
Definition: ffi_helper.h:52
Definition: object.h:58
Definition: object.h:59
base class of all object containers.
Definition: object.h:149
Definition: object.h:60
int64_t step
Definition: ffi_helper.h:53
Slice(int64_t stop)
Definition: ffi_helper.h:70
Ellipsis.
Definition: ffi_helper.h:37
A managed object in MXNet runtime.
static constexpr const char * _type_key
Definition: ffi_helper.h:40
ADT Get()
Definition: ffi_helper.h:121
A builder class that helps to incrementally build ADT.
Definition: ffi_helper.h:106
Integer(int64_t value, ObjectPtr< IntegerObj > &&data=make_object< IntegerObj >())
Definition: ffi_helper.h:94
Definition: ffi_helper.h:92
int64_t value
Definition: ffi_helper.h:86
ObjectPtr< ArrayType > make_inplace_array_object(size_t num_elems, Args &&...args)
Definition: memory.h:208
reference to algebraic data type objects.
Definition: container.h:208
ObjectRef CreateEllipsis()
Definition: ffi_helper.h:44
int64_t SliceNoneValue()
Definition: ffi_helper.h:80
Definition: ffi_helper.h:60