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 relay/op/memory/device_copy.h
22 * \brief Helpers for working with "device_copy" attributes.
23 */
24
25#ifndef TVM_RELAY_OP_MEMORY_DEVICE_COPY_H_
26#define TVM_RELAY_OP_MEMORY_DEVICE_COPY_H_
27
28#include <tvm/relay/attrs/device_copy.h>
29#include <tvm/relay/expr.h>
30
31#include <utility>
32
33#include "../call/call.h"
34
35namespace tvm {
36namespace relay {
37
38/*! \brief Returns the "device_copy" operator. */
39const Op& DeviceCopyOp();
40
41/*!
42 * \brief Wraps \p expr in a "device_copy" CallNode indicating it should be evaluated and
43 * stored at \p src_virtual_device but then copied to \p dst_virtual_device.
44 */
45Expr DeviceCopy(Expr expr, VirtualDevice src_virtual_device, VirtualDevice dst_virtual_device);
46
47/*!
48 * \brief Wraps \p expr in a "device_copy" CallNode indicating it should be evaluated and
49 * stored at \p src_virtual_device but then copied to \p dst_virtual_device.However, return \p expr
50 * directly if \p src_virtual_device and \p dst_virtual_device are (structurally) the same.
51 */
52Expr MaybeDeviceCopy(Expr expr, VirtualDevice src_virtual_device, VirtualDevice dst_virtual_device);
53
54/*! \brief Result of \p GetDeviceCopyProps. */
55struct DeviceCopyProps {
56 Expr body; // = null
57 VirtualDevice src_virtual_device = VirtualDevice::FullyUnconstrained();
58 VirtualDevice dst_virtual_device = VirtualDevice::FullyUnconstrained();
59
60 DeviceCopyProps() = default;
61
62 DeviceCopyProps(Expr body, VirtualDevice src_virtual_device, VirtualDevice dst_virtual_device)
63 : body(std::move(body)),
64 src_virtual_device(std::move(src_virtual_device)),
65 dst_virtual_device(std::move(dst_virtual_device)) {}
66};
67
68/*!
69 * \brief Returns the body expression, source, and destination \p VirtualDevices for \p call_node
70 * if it is a "device_copy" CallNode. Otherwise returns the null expression and unconstrained
71 * virtual device.
72 */
73DeviceCopyProps GetDeviceCopyProps(const CallNode* call_node);
74
75/*!
76 * \brief Returns the body expression, source, and destination \p VirtualDevices for \p expr if it
77 * is a "device_copy" Call. Otherwise returns the null expression and unconstrained virtual device.
78 */
79DeviceCopyProps GetDeviceCopyProps(const Expr& expr);
80
81} // namespace relay
82} // namespace tvm
83
84#endif // TVM_RELAY_OP_MEMORY_DEVICE_COPY_H_
85