## SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.# SPDX-License-Identifier: Apache-2.0## Licensed 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.#fromdataclassesimportdataclassimporttripy.frontend.trace.ops.utilsasop_utilsfromtripyimportconstraints,exportfromtripy.common.deviceimportdevicefromtripy.frontend.trace.ops.baseimportBaseTraceOp@dataclass(repr=False)classCopy(BaseTraceOp):target:deviceinfer_rank=op_utils.InferRankPolicies.same_as_input()definfer_devices(self):self.outputs[0].device=self.targetdefto_flat_ir(self,inputs,outputs):fromtripy.flat_ir.opsimportCopyOpCopyOp.build(inputs,outputs,target=self.target)
[docs]@export.public_api(document_under="operations/functions")@constraints.dtypes(constraints={"input":"T1",constraints.RETURN_VALUE:"T1"},variables={"T1":["float32","float16","bfloat16","float8","int4","int8","int32","int64","bool"],},)defcopy(input:"tripy.Tensor",device:"tripy.device")->"tripy.Tensor":r""" Returns a copy of the input tensor on the target device. Args: input: Tensor that will be copied device: The target device. Returns: A copy of input tensor on target device. .. code-block:: python :linenos: :caption: Example input = tp.Tensor([1, 2], device=tp.device("gpu")) output = tp.copy(input, tp.device("cpu")) assert np.array_equal(np.from_dlpack(output), np.array([1, 2], dtype=np.float32)) assert output.trace_tensor.producer.device.kind == "cpu" """returnCopy.build([input],device)