/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 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. ==============================================================================*/ #include #include #include #include "tensorflow/compiler/xla/literal_util.h" #include "tensorflow/compiler/xla/service/generic_transfer_manager.h" #include "tensorflow/compiler/xla/shape_util.h" #include "tensorflow/compiler/xla/tests/literal_test_util.h" #include "tensorflow/compiler/xla/xla_data.pb.h" #include "tensorflow/core/platform/logging.h" #include "tensorflow/core/platform/stream_executor_no_cuda.h" #include "tensorflow/compiler/xla/statusor.h" #include "tensorflow/compiler/xla/types.h" #include "tensorflow/core/platform/types.h" namespace se = ::perftools::gputools; namespace xla { namespace { class CpuTransferManagerTest : public ::testing::Test { protected: CpuTransferManagerTest() : transfer_manager_(se::host::kHostPlatformId) { se::Platform* platform = se::MultiPlatformManager::PlatformWithId(se::host::kHostPlatformId) .ValueOrDie(); stream_exec_ = platform->GetExecutor(se::StreamExecutorConfig(/*ordinal=*/0)) .ValueOrDie(); } ~CpuTransferManagerTest() override {} se::StreamExecutor* stream_exec_; GenericTransferManager transfer_manager_; }; TEST_F(CpuTransferManagerTest, TransferR0U32ToDevice) { std::vector storage(sizeof(uint32), '\x00'); se::DeviceMemoryBase memptr(storage.data(), storage.size()); std::unique_ptr literal = Literal::CreateR0(42); TF_CHECK_OK(transfer_manager_.TransferLiteralToDevice(stream_exec_, *literal, &memptr)); CHECK_EQ(42, *reinterpret_cast(&storage[0])); } TEST_F(CpuTransferManagerTest, TransferR1F32ToDevice) { std::vector storage(4 * sizeof(float), '\x00'); se::DeviceMemoryBase memptr(storage.data(), storage.size()); std::unique_ptr literal = Literal::CreateR1({1.25f, 2.5f, -17.0f, -20.125f}); TF_CHECK_OK(transfer_manager_.TransferLiteralToDevice(stream_exec_, *literal, &memptr)); CHECK_EQ(1.25f, *reinterpret_cast(&storage[0])); CHECK_EQ(2.5f, *reinterpret_cast(&storage[sizeof(float)])); CHECK_EQ(-17.0f, *reinterpret_cast(&storage[2 * sizeof(float)])); CHECK_EQ(-20.125f, *reinterpret_cast(&storage[3 * sizeof(float)])); } TEST_F(CpuTransferManagerTest, TransferR1U8ToDevice) { std::vector storage(16, '\x00'); se::DeviceMemoryBase memptr(storage.data(), storage.size()); const char* str = "0123456789abcdef"; std::unique_ptr literal = Literal::CreateR1U8(str); TF_CHECK_OK(transfer_manager_.TransferLiteralToDevice(stream_exec_, *literal, &memptr)); CHECK_EQ('0', storage[0]); CHECK_EQ('8', storage[8]); CHECK_EQ('f', storage[15]); } TEST_F(CpuTransferManagerTest, TransferR0U32FromDevice) { std::vector storage(1, 42); se::DeviceMemoryBase memptr(storage.data(), storage.size() * sizeof(storage[0])); Literal literal; const Shape shape = ShapeUtil::MakeShape(U32, {}); TF_CHECK_OK(transfer_manager_.TransferLiteralFromDevice( stream_exec_, memptr, shape, shape, &literal)); LiteralTestUtil::ExpectR0Equal(42, literal); } TEST_F(CpuTransferManagerTest, TransferR1F32FromDevice) { std::vector storage{1.25f, 2.5f, -17.0f, -20.125f}; se::DeviceMemoryBase memptr(storage.data(), storage.size() * sizeof(storage[0])); Literal literal; const Shape shape = ShapeUtil::MakeShape(F32, {4}); TF_CHECK_OK(transfer_manager_.TransferLiteralFromDevice( stream_exec_, memptr, shape, shape, &literal)); LiteralTestUtil::ExpectR1Equal({1.25, 2.5, -17.0, -20.125}, literal); } TEST_F(CpuTransferManagerTest, TransferR1U8FromDevice) { std::vector storage{'k', 'l', 'm', 'n'}; se::DeviceMemoryBase memptr(storage.data(), storage.size() * sizeof(storage[0])); Literal literal; const Shape shape = ShapeUtil::MakeShape(U8, {4}); TF_CHECK_OK(transfer_manager_.TransferLiteralFromDevice( stream_exec_, memptr, shape, shape, &literal)); CHECK_EQ("klmn", literal.u8s_string()); } TEST_F(CpuTransferManagerTest, TransferBufferFromDevice) { std::vector storage{1, 5, 42}; int64 size = storage.size() * sizeof(storage[0]); se::DeviceMemoryBase memptr(storage.data(), size); std::vector dest(3, 0); TF_CHECK_OK(transfer_manager_.TransferBufferFromDevice(stream_exec_, memptr, size, dest.data())); ASSERT_EQ(1, dest[0]); ASSERT_EQ(5, dest[1]); ASSERT_EQ(42, dest[2]); } TEST_F(CpuTransferManagerTest, TransferBufferToDevice) { int64 size = 3 * sizeof(uint64); std::vector storage(size, 0); se::DeviceMemoryBase memptr(storage.data(), size); std::vector dest{1, 5, 42}; TF_CHECK_OK(transfer_manager_.TransferBufferToDevice(stream_exec_, size, dest.data(), &memptr)); std::vector* storage64 = reinterpret_cast*>(&storage); ASSERT_EQ(1, (*storage64)[0]); ASSERT_EQ(5, (*storage64)[1]); ASSERT_EQ(42, (*storage64)[2]); } // TODO(b/24679870): add similar tests for GPUs } // namespace } // namespace xla