|
| 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 | + |
| 20 | +/*! |
| 21 | + * \file tvm/relay/attrs/on_device.h |
| 22 | + * \brief Attribute for the on device annotation. |
| 23 | + */ |
| 24 | +#ifndef TVM_RELAY_ATTRS_ON_DEVICE_H_ |
| 25 | +#define TVM_RELAY_ATTRS_ON_DEVICE_H_ |
| 26 | + |
| 27 | +#include <tvm/ir/attrs.h> |
| 28 | +#include <tvm/target/se_scope.h> |
| 29 | + |
| 30 | +#include <string> |
| 31 | + |
| 32 | +namespace tvm { |
| 33 | +namespace relay { |
| 34 | + |
| 35 | +/*! |
| 36 | + * \brief Attributes for the "on_device" special operator. |
| 37 | + * |
| 38 | + * The Relay call (aka 'annotation'): |
| 39 | + * \code |
| 40 | + * on_device(sub_expr, se_scope=S) |
| 41 | + * \endcode |
| 42 | + * constrains \p sub_expr to execute and store its result on the \p SEScope \p S. |
| 43 | + * However the annotation itself may appear in an expression to be executed and stored on a |
| 44 | + * different \p SEScope. If so the compiler will automatically insert a "device_copy" call to |
| 45 | + * mediate the transition between \p SEScopes. |
| 46 | + * |
| 47 | + * E.g.: Assuming %x and %y reside on the GPU and %z on the CPU then: |
| 48 | + * \code |
| 49 | + * multiply(on_device(add(%x, %y), se_scope=GPU), %z) |
| 50 | + * \endcode |
| 51 | + * indicates the \p add should execute on the GPU but the \p multiply should execute on the CPU. |
| 52 | + * The compiler will rewrite this to: |
| 53 | + * \code |
| 54 | + * multiply(device_copy(add(%x, %y), src_se_scope=GPU, dst_se_scope=CPU), %z) |
| 55 | + * \endcode |
| 56 | + * |
| 57 | + * The Relay call |
| 58 | + * \code |
| 59 | + * on_device(sub_expr, se_scope=S, is_fixed=True) |
| 60 | + * \endcode |
| 61 | + * is similar to the above, however the annotation itself must appear in an expression on the |
| 62 | + * same \p SEScope \p S. The compiler will check the \p SEScopes are consistent, and will not |
| 63 | + * insert any "device_copy" call. This form of annotation shouldn't be necessary in user programs. |
| 64 | + * However it is needed by the \p PlanDevices pass to fully specify the results of device planning |
| 65 | + * so that the pass is idempotent. |
| 66 | + * |
| 67 | + * E.g.: The following program is equivalent to the above: |
| 68 | + * \code |
| 69 | + * let %a = on_device(add(%x, %y), se_scope=GPU, is_fixed=True) |
| 70 | + * multiply(device_copy(%a, src_se_scope=GPU, dst_se_scope=CPU), %z) |
| 71 | + * \endcode |
| 72 | + * The "on_device" annotation with \p is_fixed=True indicates unambiguously that \p %a is stored |
| 73 | + * on the GPU. |
| 74 | + */ |
| 75 | +struct OnDeviceAttrs : public tvm::AttrsNode<OnDeviceAttrs> { |
| 76 | + /*! |
| 77 | + * \brief (Virtual) \p SEScope on which the result of the argument expression should be stored. |
| 78 | + */ |
| 79 | + SEScope se_scope = SEScope::FullyUnconstrained(); |
| 80 | + /*! |
| 81 | + * \brief If true, the result \p SEScope must also be \p se_scope, and device planning should |
| 82 | + * not insert any "device_copy" calls to respect this annotation. |
| 83 | + * |
| 84 | + * This is used by the device planning pass itself when annotating the planned program. |
| 85 | + */ |
| 86 | + bool is_fixed = false; |
| 87 | + |
| 88 | + TVM_DECLARE_ATTRS(OnDeviceAttrs, "relay.attrs.OnDeviceAttrs") { |
| 89 | + TVM_ATTR_FIELD(se_scope) |
| 90 | + .describe("The (virtual) device and scope holding the expression result.") |
| 91 | + .set_default(SEScope::FullyUnconstrained()); |
| 92 | + TVM_ATTR_FIELD(is_fixed) |
| 93 | + .describe("If true, do not insert a \"device_copy\" call to respect this annotation.") |
| 94 | + .set_default(false); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +} // namespace relay |
| 99 | +} // namespace tvm |
| 100 | + |
| 101 | +#endif // TVM_RELAY_ATTRS_ON_DEVICE_H_ |
0 commit comments