|
| 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 | +#include <gtest/gtest.h> |
| 21 | +#include <tvm/runtime/device_api.h> |
| 22 | +#include <tvm/runtime/logging.h> |
| 23 | +#include <tvm/runtime/tensor.h> |
| 24 | + |
| 25 | +#include <memory> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +#include "../../../src/runtime/rpc/rpc_session.h" |
| 29 | + |
| 30 | +namespace tvm { |
| 31 | +namespace runtime { |
| 32 | + |
| 33 | +Tensor TensorFromRemoteOpaqueHandle(std::shared_ptr<RPCSession> sess, void* handle, |
| 34 | + DLTensor* template_tensor, Device dev, |
| 35 | + void* remote_tensor_handle); |
| 36 | + |
| 37 | +namespace { |
| 38 | + |
| 39 | +class RecordingRPCSession final : public RPCSession { |
| 40 | + public: |
| 41 | + PackedFuncHandle GetFunction(const std::string& name) final { return nullptr; } |
| 42 | + |
| 43 | + void CallFunc(PackedFuncHandle func, ffi::PackedArgs args, |
| 44 | + const FEncodeReturn& fencode_return) final {} |
| 45 | + |
| 46 | + void CopyToRemote(void* local_from_bytes, DLTensor* remote_to, uint64_t nbytes) final {} |
| 47 | + |
| 48 | + void CopyFromRemote(DLTensor* remote_from, void* local_to_bytes, uint64_t nbytes) final {} |
| 49 | + |
| 50 | + void FreeHandle(void* handle) final { |
| 51 | + ++free_handle_calls; |
| 52 | + last_freed_handle = handle; |
| 53 | + if (throw_on_free) { |
| 54 | + TVM_FFI_THROW(InternalError) << "simulated remote close"; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + DeviceAPI* GetDeviceAPI(Device dev, bool allow_missing = false) final { return nullptr; } |
| 59 | + |
| 60 | + bool IsLocalSession() const final { return false; } |
| 61 | + |
| 62 | + int free_handle_calls{0}; |
| 63 | + void* last_freed_handle{nullptr}; |
| 64 | + bool throw_on_free{false}; |
| 65 | +}; |
| 66 | + |
| 67 | +DLTensor MakeTemplateTensor() { |
| 68 | + static int64_t shape[1] = {4}; |
| 69 | + DLTensor tensor{}; |
| 70 | + tensor.data = nullptr; |
| 71 | + tensor.device = Device{kDLCPU, 0}; |
| 72 | + tensor.ndim = 1; |
| 73 | + tensor.dtype = DataType::Float(32); |
| 74 | + tensor.shape = shape; |
| 75 | + tensor.strides = nullptr; |
| 76 | + tensor.byte_offset = 0; |
| 77 | + return tensor; |
| 78 | +} |
| 79 | + |
| 80 | +Device MakeRemoteDevice(const std::shared_ptr<RPCSession>& sess) { |
| 81 | + return AddRPCSessionMask(Device{kDLCPU, 0}, sess->table_index()); |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace |
| 85 | + |
| 86 | +TEST(RPCTensorTest, ReturnedTensorFreesRemoteTensorHandle) { |
| 87 | + auto sess = std::make_shared<RecordingRPCSession>(); |
| 88 | + DLTensor template_tensor = MakeTemplateTensor(); |
| 89 | + void* data_handle = reinterpret_cast<void*>(0x1234); |
| 90 | + void* tensor_handle = reinterpret_cast<void*>(0x5678); |
| 91 | + |
| 92 | + { |
| 93 | + auto tensor = TensorFromRemoteOpaqueHandle(sess, data_handle, &template_tensor, |
| 94 | + MakeRemoteDevice(sess), tensor_handle); |
| 95 | + EXPECT_NE(tensor.defined(), false); |
| 96 | + } |
| 97 | + |
| 98 | + EXPECT_EQ(sess->free_handle_calls, 1); |
| 99 | + EXPECT_EQ(sess->last_freed_handle, tensor_handle); |
| 100 | + EXPECT_NE(sess->last_freed_handle, data_handle); |
| 101 | +} |
| 102 | + |
| 103 | +TEST(RPCTensorTest, ReturnedTensorDestructorIgnoresFreeHandleErrors) { |
| 104 | + auto sess = std::make_shared<RecordingRPCSession>(); |
| 105 | + sess->throw_on_free = true; |
| 106 | + DLTensor template_tensor = MakeTemplateTensor(); |
| 107 | + void* data_handle = reinterpret_cast<void*>(0x1234); |
| 108 | + void* tensor_handle = reinterpret_cast<void*>(0x5678); |
| 109 | + |
| 110 | + EXPECT_NO_THROW({ |
| 111 | + auto tensor = TensorFromRemoteOpaqueHandle(sess, data_handle, &template_tensor, |
| 112 | + MakeRemoteDevice(sess), tensor_handle); |
| 113 | + }); |
| 114 | + EXPECT_EQ(sess->free_handle_calls, 1); |
| 115 | + EXPECT_EQ(sess->last_freed_handle, tensor_handle); |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace runtime |
| 119 | +} // namespace tvm |
0 commit comments