add mode switch in rtMemcpyAsync for graph mode
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
# Copyright (c) 2022, Huawei Technologies.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Licensed under the BSD 3-Clause License (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://opensource.org/licenses/BSD-3-Clause
|
||||
#
|
||||
# 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.
|
||||
|
||||
import torch
|
||||
import numpy as np
|
||||
from torch_npu.testing.testcase import TestCase, run_tests
|
||||
from torch_npu.testing.decorator import graph_mode
|
||||
from torch_npu.testing.common_utils import create_common_tensor
|
||||
import torch.nn as nn
|
||||
|
||||
class TestMemcpy(TestCase):
|
||||
def test_copy_memory_(self):
|
||||
def cpu_op_exec(input1, input2):
|
||||
out_mul = torch.mul(input1, input2)
|
||||
out_add = torch.add(input1, input2)
|
||||
out_mul.copy_(out_add)
|
||||
out = torch.sub(out_mul, input2)
|
||||
return out.numpy()
|
||||
def npu_op_exec(input1, input2):
|
||||
torch.npu.enable_graph_mode()
|
||||
out_mul = torch.mul(input1, input2)
|
||||
out_add = torch.add(input1, input2)
|
||||
out_mul.copy_memory_(out_add)
|
||||
out = torch.sub(out_mul, input2)
|
||||
torch.npu.launch_graph()
|
||||
return out.to("cpu").numpy()
|
||||
|
||||
dtype_list = [np.int32, np.float32]
|
||||
format_list = [0, 3, 29]
|
||||
shape_list = [
|
||||
[9, 13],
|
||||
[3, 16, 5, 5],
|
||||
]
|
||||
dtype_shape_format = [
|
||||
[i, j, k] for i in dtype_list for j in format_list for k in shape_list
|
||||
]
|
||||
|
||||
for item in dtype_shape_format:
|
||||
cpu_input, npu_input = create_common_tensor(item, -100, 100)
|
||||
cpu_output = cpu_op_exec(cpu_input, 2)
|
||||
npu_output = npu_op_exec(npu_input, 2)
|
||||
self.assertRtolEqual(cpu_output, npu_output)
|
||||
|
||||
|
||||
@graph_mode
|
||||
def test_h2d_inplace(self):
|
||||
def cpu_copy_op_exec(input1_host, input2_host):
|
||||
input1_host.add_(input1_host)
|
||||
input1_host.copy_(input2_host)
|
||||
return input1_host.numpy()
|
||||
|
||||
def npu_copy_op_exec(input1_device, input2_host):
|
||||
input1_device.add_(input1_device)
|
||||
input1_device.copy_(input2_host)
|
||||
return input1_device.to("cpu").numpy()
|
||||
|
||||
cpu_input1, npu_input1 = create_common_tensor([np.float32, -1, (5, 5)], 1, 100)
|
||||
cpu_input2 = cpu_input1 + 1
|
||||
cpu_output = cpu_copy_op_exec(cpu_input1, cpu_input2)
|
||||
npu_output = npu_copy_op_exec(npu_input1, cpu_input2)
|
||||
self.assertRtolEqual(cpu_output, npu_output)
|
||||
|
||||
|
||||
@graph_mode
|
||||
def test_d2h_inplace(self):
|
||||
def cpu_copy_op_exec(input1_host, input2_host):
|
||||
input1_host.add_(input1_host)
|
||||
input2_host.copy_(input1_host)
|
||||
return input2_host.numpy()
|
||||
|
||||
def npu_copy_op_exec(input1_device, input2_host):
|
||||
input1_device.add_(input1_device)
|
||||
input2_host.copy_(input1_device)
|
||||
return input2_host.numpy()
|
||||
|
||||
cpu_input1, npu_input1 = create_common_tensor([np.float32, -1, (5, 5)], 1, 100)
|
||||
cpu_out1 = cpu_input1 + 1
|
||||
cpu_out2 = cpu_input1 + 1
|
||||
cpu_output = cpu_copy_op_exec(cpu_input1, cpu_out1)
|
||||
npu_output = npu_copy_op_exec(npu_input1, cpu_out2)
|
||||
self.assertRtolEqual(cpu_output, npu_output)
|
||||
|
||||
|
||||
@graph_mode
|
||||
def test_item(self):
|
||||
def cpu_copy_op_exec(input1, input2):
|
||||
input1.add_(input2)
|
||||
out = input1 * input2
|
||||
out = out.item()
|
||||
return out
|
||||
|
||||
def npu_copy_op_exec(input1, input2):
|
||||
input1.add_(input2)
|
||||
out = input1 * input2
|
||||
out = out.item()
|
||||
return out
|
||||
|
||||
dtype_list = [np.int32, np.float32]
|
||||
format_list = [-1]
|
||||
shape_list = [
|
||||
[1],
|
||||
[1, 1, 1,],
|
||||
]
|
||||
dtype_shape_format = [
|
||||
[i, j, k] for i in dtype_list for j in format_list for k in shape_list
|
||||
]
|
||||
|
||||
for item in dtype_shape_format:
|
||||
cpu_input, npu_input = create_common_tensor(item, -100, 100)
|
||||
cpu_output = cpu_copy_op_exec(cpu_input, 2) + cpu_copy_op_exec(cpu_input, 3)
|
||||
npu_output = npu_copy_op_exec(npu_input, 2) + npu_copy_op_exec(npu_input, 3)
|
||||
self.assertEqual(cpu_output, npu_output)
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_tests()
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <c10/util/Exception.h>
|
||||
|
||||
#include "torch_npu/csrc/core/npu/NPUGuard.h"
|
||||
#include "torch_npu/csrc/framework/utils/CalcuOpUtil.h"
|
||||
#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
|
||||
#include "torch_npu/csrc/framework/contiguous/ContiguousOpt.h"
|
||||
#include "torch_npu/csrc/framework/FormatHelper.h"
|
||||
@@ -107,15 +108,20 @@ void copy_between_host_and_device(
|
||||
const at::Tensor& src,
|
||||
aclrtMemcpyKind kind,
|
||||
bool non_blocking) {
|
||||
void* dst_ptr = dst.data_ptr();
|
||||
void* src_ptr = src.data_ptr();
|
||||
int64_t nbytes = dst.numel() * dst.element_size();
|
||||
c10_npu::NPUStream stream = c10_npu::getCurrentNPUStream();
|
||||
C10_NPU_CHECK(aclrtMemcpyAsync(dst_ptr, nbytes, src_ptr, nbytes, kind, stream));
|
||||
auto ret = CalcuOpUtil::AclrtMemcpyAsyncWithModeSwitch(
|
||||
std::make_pair(dst.storage().unsafeGetStorageImpl(), dst.storage_offset() * dst.itemsize()),
|
||||
nbytes,
|
||||
std::make_pair(src.storage().unsafeGetStorageImpl(), src.storage_offset() * src.itemsize()),
|
||||
nbytes,
|
||||
kind,
|
||||
stream);
|
||||
C10_NPU_CHECK(ret);
|
||||
|
||||
if (non_blocking) {
|
||||
NPU_LOGD("non_blocking copy without StreamSynchronize.");
|
||||
void* ptr = at_npu::key::isDeviceTensor(dst) ? src_ptr : dst_ptr;
|
||||
void* ptr = at_npu::key::isDeviceTensor(dst) ? src.data_ptr() : dst.data_ptr();
|
||||
C10_NPU_CHECK(THNPUCachingHostAllocator_recordEvent(ptr, stream));
|
||||
} else {
|
||||
aclError error = aclrtSynchronizeStream(stream);
|
||||
|
||||
@@ -114,6 +114,7 @@ void copy_d2d_by_memcpy(at::Tensor& dst, const at::Tensor& src, int64_t exceptSi
|
||||
return;
|
||||
}
|
||||
|
||||
// The current logic is only used in single op mode.
|
||||
aclError error = c10_npu::queue::LaunchAsyncCopyTask(
|
||||
dst.data_ptr(),
|
||||
size * dst.element_size(),
|
||||
|
||||
@@ -64,12 +64,14 @@ at::Tensor& NPUNativeFunctions::copy_memory_(at::Tensor& self, const at::Tensor&
|
||||
|
||||
// Designed for the gather of tensors, ignoring npu_format_ and
|
||||
// copying continuous memory between npu tensors.
|
||||
C10_NPU_CHECK(c10_npu::queue::LaunchAsyncCopyTask(
|
||||
self.data_ptr(),
|
||||
auto ret = CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
|
||||
self,
|
||||
dst_size * self.itemsize(),
|
||||
src.data_ptr(),
|
||||
src,
|
||||
dst_size * self.itemsize(),
|
||||
ACL_MEMCPY_DEVICE_TO_DEVICE));
|
||||
ACL_MEMCPY_DEVICE_TO_DEVICE);
|
||||
C10_NPU_CHECK(ret);
|
||||
|
||||
if (!non_blocking) {
|
||||
c10_npu::NPUStream stream = c10_npu::getCurrentNPUStream();
|
||||
C10_NPU_CHECK(aclrtSynchronizeStream(stream));
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <ATen/NativeFunctions.h>
|
||||
#include "torch_npu/csrc/core/npu/NPUStream.h"
|
||||
#include "torch_npu/csrc/framework/graph/util/GraphModeGuard.h"
|
||||
#include "torch_npu/csrc/framework/utils/CalcuOpUtil.h"
|
||||
#include "third_party/acl/inc/acl/acl_base.h"
|
||||
#include "third_party/acl/inc/acl/acl_rt.h"
|
||||
#include "torch_npu/csrc/aten/NPUNativeFunctions.h"
|
||||
@@ -33,13 +34,13 @@ c10::Scalar NPUNativeFunctions::_local_scalar_dense(const at::Tensor& self) {
|
||||
self.scalar_type(),
|
||||
"_local_scalar_dense_npu",
|
||||
[&] {
|
||||
GraphModeGuard mode_guard(c10_npu::ModeKind::SINGLE_OP_MODE);
|
||||
scalar_t value = 0;
|
||||
c10_npu::NPUStream copy_stream = c10_npu::getCurrentNPUStream();
|
||||
aclError error = aclrtMemcpyAsync(
|
||||
aclError error = CalcuOpUtil::AclrtMemcpyAsyncWithModeSwitch(
|
||||
&value,
|
||||
sizeof(scalar_t),
|
||||
self.data_ptr<scalar_t>(),
|
||||
std::make_pair(
|
||||
self.storage().unsafeGetStorageImpl(), self.storage_offset() * self.itemsize()),
|
||||
sizeof(scalar_t),
|
||||
ACL_MEMCPY_DEVICE_TO_HOST,
|
||||
copy_stream);
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "torch_npu/csrc/core/NPUStorageImpl.h"
|
||||
#include "torch_npu/csrc/core/npu/NPURunMode.h"
|
||||
#include "torch_npu/csrc/framework/graph/util/GraphUtils.h"
|
||||
#include "torch_npu/csrc/framework/utils/CalcuOpUtil.h"
|
||||
|
||||
namespace at_npu {
|
||||
namespace native {
|
||||
@@ -40,6 +41,8 @@ static void storage_resize_npu(
|
||||
}
|
||||
|
||||
at::DataPtr new_data;
|
||||
auto storage_desc = storage.npu_desc_;
|
||||
size_t itemsize = storage_desc.data_type_.itemsize();
|
||||
if (size != 0) {
|
||||
new_data = storage.allocator()->allocate(size);
|
||||
}
|
||||
@@ -56,11 +59,11 @@ static void storage_resize_npu(
|
||||
copy_size = storage.nbytes();
|
||||
}
|
||||
if (copy_size > 0) {
|
||||
aclError error = c10_npu::queue::LaunchAsyncCopyTask(
|
||||
storage.data(),
|
||||
copy_size,
|
||||
aclError error = CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
|
||||
storage,
|
||||
itemsize * copy_size,
|
||||
old_data.get(),
|
||||
copy_size,
|
||||
itemsize * copy_size,
|
||||
ACL_MEMCPY_DEVICE_TO_DEVICE);
|
||||
if (error != ACL_ERROR_NONE) {
|
||||
AT_ERROR("ACL_Memcpy device to device error.");
|
||||
|
||||
@@ -108,6 +108,7 @@ private:
|
||||
|
||||
if (temp_src.is_contiguous()) {
|
||||
auto temp_dst = NPUNativeFunctions::npu_broadcast(temp_src, self.sizes());
|
||||
// The current logic is only used in single op mode.
|
||||
c10_npu::queue::LaunchAsyncCopyTask(self.data_ptr(), self.nbytes(), temp_dst.data_ptr(),
|
||||
self.nbytes(), ACL_MEMCPY_DEVICE_TO_DEVICE);
|
||||
return true;
|
||||
|
||||
@@ -43,6 +43,9 @@ void ScalarMemContext::ExecuteH2D(c10_npu::NPUStream stream) {
|
||||
{host_mem_valid_len_},
|
||||
at::TensorOptions().device(at_npu::key::NativeDeviceType, deviceIndex).dtype(at::kByte));
|
||||
|
||||
// This aclrtMemcpyAsync is only used for graph mode, and the target device
|
||||
// memory is always available. Executing run graph here will result in a
|
||||
// circular call to the run graph interface.
|
||||
C10_NPU_CHECK(
|
||||
aclrtMemcpyAsync(
|
||||
npu_tensor_.data_ptr(),
|
||||
|
||||
@@ -30,10 +30,15 @@
|
||||
#include "torch_npu/csrc/framework/utils/NpuFuzzyBlacklist.h"
|
||||
#include "torch_npu/csrc/framework/interface/EnvVariables.h"
|
||||
#include "third_party/acl/inc/acl/acl_base.h"
|
||||
#include "third_party/acl/inc/acl/acl_rt.h"
|
||||
#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
|
||||
#include "torch_npu/csrc/core/npu/interface/AclInterface.h"
|
||||
#include "torch_npu/csrc/framework/contiguous/ReshapeOpt.h"
|
||||
#include "torch_npu/csrc/core/NPUBridge.h"
|
||||
|
||||
|
||||
#include"torch_npu/csrc/core/NPUStorageImpl.h"
|
||||
|
||||
namespace at_npu
|
||||
{
|
||||
namespace native
|
||||
@@ -85,6 +90,29 @@ namespace at_npu
|
||||
|
||||
return iter->second;
|
||||
}
|
||||
|
||||
aclError AclrtMemcpyAsyncParamCheck(
|
||||
void *dst,
|
||||
size_t destMax,
|
||||
const void *src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream)
|
||||
{
|
||||
if (c10_npu::NpuRunMode::IsGraphMode())
|
||||
{
|
||||
if (dst == nullptr || src == nullptr)
|
||||
{
|
||||
AT_ERROR(
|
||||
"Dst ptr or Src ptr of aclrtMemcpyAsync is nullptr!",
|
||||
"Current run mode is graph mode, "
|
||||
"try to use torch.npu.disable_graph_mode() to fix this error.");
|
||||
}
|
||||
}
|
||||
|
||||
auto ret = aclrtMemcpyAsync(dst, destMax, src, count, kind, stream);
|
||||
return ret;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
aclDataType CalcuOpUtil::convert_to_acl_data_type(const at::ScalarType data_type)
|
||||
@@ -200,6 +228,89 @@ namespace at_npu
|
||||
return "SUCCESS";
|
||||
}
|
||||
|
||||
aclError CalcuOpUtil::AclrtMemcpyAsyncWithModeSwitch(
|
||||
const StorageAndOffsetMemSizePair& dst,
|
||||
size_t dstMax,
|
||||
const StorageAndOffsetMemSizePair& src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream) {
|
||||
if (c10_npu::NpuRunMode::IsGraphMode()) {
|
||||
GraphExecutor::GetInstance().ConstructAndExecuteGraph();
|
||||
}
|
||||
|
||||
void* dst_ptr = static_cast<void*>(
|
||||
static_cast<uint8_t*>(dst.first->data()) + dst.second);
|
||||
void* src_ptr = static_cast<void*>(
|
||||
static_cast<uint8_t*>(src.first->data()) + src.second);
|
||||
return AclrtMemcpyAsyncParamCheck(
|
||||
dst_ptr, dstMax, const_cast<void*>(src_ptr), count, kind, stream);
|
||||
}
|
||||
|
||||
aclError CalcuOpUtil::AclrtMemcpyAsyncWithModeSwitch(
|
||||
const StorageAndOffsetMemSizePair& dst,
|
||||
size_t dstMax,
|
||||
const void* src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream) {
|
||||
if (c10_npu::NpuRunMode::IsGraphMode()) {
|
||||
GraphExecutor::GetInstance().ConstructAndExecuteGraph();
|
||||
}
|
||||
|
||||
void* dst_ptr = static_cast<void*>(
|
||||
static_cast<uint8_t*>(dst.first->data()) + dst.second);
|
||||
return AclrtMemcpyAsyncParamCheck(
|
||||
dst_ptr, dstMax, src, count, kind, stream);
|
||||
}
|
||||
|
||||
aclError CalcuOpUtil::AclrtMemcpyAsyncWithModeSwitch(
|
||||
void* dst,
|
||||
size_t dstMax,
|
||||
const StorageAndOffsetMemSizePair& src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream) {
|
||||
if (c10_npu::NpuRunMode::IsGraphMode()) {
|
||||
GraphExecutor::GetInstance().ConstructAndExecuteGraph();
|
||||
}
|
||||
|
||||
void* src_ptr = static_cast<void*>(
|
||||
static_cast<uint8_t*>(src.first->data()) + src.second);
|
||||
return AclrtMemcpyAsyncParamCheck(
|
||||
dst, dstMax, const_cast<void*>(src_ptr), count, kind, stream);
|
||||
}
|
||||
|
||||
aclError CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
|
||||
const at::Tensor& dst,
|
||||
size_t dstMax,
|
||||
const at::Tensor& src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind) {
|
||||
if (c10_npu::NpuRunMode::IsGraphMode()) {
|
||||
GraphExecutor::GetInstance().ConstructAndExecuteGraph();
|
||||
}
|
||||
|
||||
aclError ret = c10_npu::queue::LaunchAsyncCopyTask(
|
||||
dst.data_ptr(), dstMax, src.data_ptr(), count, kind);
|
||||
return ret;
|
||||
}
|
||||
|
||||
aclError CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
|
||||
const c10::StorageImpl& dst,
|
||||
size_t dstMax,
|
||||
void* src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind) {
|
||||
if (c10_npu::NpuRunMode::IsGraphMode()) {
|
||||
GraphExecutor::GetInstance().ConstructAndExecuteGraph();
|
||||
}
|
||||
|
||||
aclError ret = c10_npu::queue::LaunchAsyncCopyTask(
|
||||
dst.data(), dstMax, src, count, kind);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool check_npu_format_unchanged_in_format_contiguous(
|
||||
const at::Tensor &tensor,
|
||||
const torch_npu::NPUStorageDesc &tensor_desc) {
|
||||
|
||||
@@ -70,6 +70,8 @@ using std::vector;
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
using StorageAndOffsetMemSizePair = std::pair<const c10::StorageImpl*, int64_t>;
|
||||
|
||||
namespace at_npu
|
||||
{
|
||||
namespace native
|
||||
@@ -226,6 +228,43 @@ namespace at_npu
|
||||
const std::pair<at::Tensor, int64_t>& src,
|
||||
size_t src_size,
|
||||
aclrtMemcpyKind kind);
|
||||
|
||||
// Add some public interfaces for aclrtmemcpy process,
|
||||
// to launch graph in graph mode automatically.
|
||||
static aclError AclrtMemcpyAsyncWithModeSwitch(
|
||||
const StorageAndOffsetMemSizePair& dst,
|
||||
size_t dstMax,
|
||||
const StorageAndOffsetMemSizePair& src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream);
|
||||
static aclError AclrtMemcpyAsyncWithModeSwitch(
|
||||
const StorageAndOffsetMemSizePair& dst,
|
||||
size_t dstMax,
|
||||
const void* src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream);
|
||||
static aclError AclrtMemcpyAsyncWithModeSwitch(
|
||||
void* dst,
|
||||
size_t dstMax,
|
||||
const StorageAndOffsetMemSizePair& src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind,
|
||||
aclrtStream stream);
|
||||
static aclError LaunchAsyncCopyTaskWithModeSwitch(
|
||||
const at::Tensor& dst,
|
||||
size_t dstMax,
|
||||
const at::Tensor& src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind);
|
||||
static aclError LaunchAsyncCopyTaskWithModeSwitch(
|
||||
const c10::StorageImpl& dst,
|
||||
size_t dstMax,
|
||||
void* src,
|
||||
size_t count,
|
||||
aclrtMemcpyKind kind);
|
||||
|
||||
static void check_memory_over_laps(
|
||||
c10::SmallVector<at::Tensor, N> &inputs,
|
||||
c10::SmallVector<at::Tensor, N> &outputs);
|
||||
|
||||
Reference in New Issue
Block a user