diff --git a/mindspore/ccsrc/backend/kernel_compiler/gpu/debug/print_gpu_kernel.cc b/mindspore/ccsrc/backend/kernel_compiler/gpu/debug/print_gpu_kernel.cc new file mode 100644 index 0000000000..62bb23bd44 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/gpu/debug/print_gpu_kernel.cc @@ -0,0 +1,55 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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 "backend/kernel_compiler/gpu/debug/print_gpu_kernel.h" + +namespace mindspore { +namespace kernel { +MS_REG_GPU_KERNEL_ONE(Print, + KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeInt8).AddOutputAttr(kNumberTypeInt8), + PrintGpuKernel, int8_t) +MS_REG_GPU_KERNEL_ONE(Print, + KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeInt16).AddOutputAttr(kNumberTypeInt16), + PrintGpuKernel, int16_t) +MS_REG_GPU_KERNEL_ONE(Print, + KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeInt32).AddOutputAttr(kNumberTypeInt32), + PrintGpuKernel, int) +MS_REG_GPU_KERNEL_ONE(Print, + KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeInt64).AddOutputAttr(kNumberTypeInt64), + PrintGpuKernel, int64_t) +MS_REG_GPU_KERNEL_ONE(Print, + KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeUInt8).AddOutputAttr(kNumberTypeUInt8), + PrintGpuKernel, uint8_t) +MS_REG_GPU_KERNEL_ONE(Print, + KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeBool).AddOutputAttr(kNumberTypeBool), + PrintGpuKernel, bool) +MS_REG_GPU_KERNEL_ONE( + Print, KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeUInt16).AddOutputAttr(kNumberTypeUInt16), + PrintGpuKernel, uint16_t) +MS_REG_GPU_KERNEL_ONE( + Print, KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeUInt32).AddOutputAttr(kNumberTypeUInt32), + PrintGpuKernel, uint32_t) +MS_REG_GPU_KERNEL_ONE( + Print, KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeUInt64).AddOutputAttr(kNumberTypeUInt64), + PrintGpuKernel, uint64_t) +MS_REG_GPU_KERNEL_ONE( + Print, KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeFloat16).AddOutputAttr(kNumberTypeFloat16), + PrintGpuKernel, half) +MS_REG_GPU_KERNEL_ONE( + Print, KernelAttr().AddAllSameAttr(true).AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32), + PrintGpuKernel, float) +} // namespace kernel +} // namespace mindspore diff --git a/mindspore/ccsrc/backend/kernel_compiler/gpu/debug/print_gpu_kernel.h b/mindspore/ccsrc/backend/kernel_compiler/gpu/debug/print_gpu_kernel.h new file mode 100644 index 0000000000..8aa9fda1e1 --- /dev/null +++ b/mindspore/ccsrc/backend/kernel_compiler/gpu/debug/print_gpu_kernel.h @@ -0,0 +1,98 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + * + * 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. + */ + +#ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_DEBUG_PRINT_GPU_KERNEL_H_ +#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_DEBUG_PRINT_GPU_KERNEL_H_ + +#include +#include +#include "backend/kernel_compiler/gpu/gpu_kernel.h" +#include "backend/kernel_compiler/gpu/gpu_kernel_factory.h" + +namespace mindspore { +namespace kernel { +template +class PrintGpuKernel : public GpuKernel { + public: + PrintGpuKernel() { ResetResource(); } + ~PrintGpuKernel() override = default; + + const std::vector &GetInputSizeList() const override { return input_size_list_; } + const std::vector &GetOutputSizeList() const override { return output_size_list_; } + const std::vector &GetWorkspaceSizeList() const override { return workspace_size_list_; } + + bool Launch(const std::vector &inputs, const std::vector &workspace, + const std::vector &outputs, void *stream_ptr) override { + VARIABLE_NOT_USED(workspace); + VARIABLE_NOT_USED(outputs); + for (size_t i = 0; i < inputs.size(); i++) { + input_device_data_[i] = GetDeviceAddress(inputs, i); + } + CHECK_CUDA_RET_WITH_EXCEPT( + kernel_node_, + cudaMemcpy(&input_host_data_[0], &input_device_data_[0], input_size_ * sizeof(T), cudaMemcpyDeviceToHost), + "cudaMemcpy output failed"); + for (size_t i = 0; i < input_num_.size(); i++) { + for (size_t j = 0; j < input_num_[i]; j++) { + std::cout << input_host_data_[i][j]; + } + } + return true; + } + + bool Init(const CNodePtr &kernel_node) override { + kernel_node_ = kernel_node; + size_t input_tensor_num = AnfAlgo::GetInputTensorNum(kernel_node); + input_device_data_ = std::make_unique(input_tensor_num); + input_host_data_ = std::make_unique(input_tensor_num); + for (size_t i = 0; i < input_tensor_num; i++) { + size_t counter = 0; + auto input_shape = AnfAlgo::GetInputDeviceShape(kernel_node, i); + for (size_t j = 0; j < input_shape.size(); j++) { + input_size_ *= input_shape[j]; + counter++; + } + input_num_.push_back(counter); + } + InitSizeLists(); + return true; + } + + void ResetResource() noexcept override { + input_size_ = 1; + input_device_data_ = nullptr; + input_host_data_ = nullptr; + input_num_.clear(); + input_size_list_.clear(); + output_size_list_.clear(); + workspace_size_list_.clear(); + } + + protected: + void InitSizeLists() override { input_size_list_.push_back(input_size_ * sizeof(T)); } + + private: + size_t input_size_; + std::unique_ptr input_device_data_; + std::unique_ptr input_host_data_; + std::vector input_num_; + std::vector input_size_list_; + std::vector output_size_list_; + std::vector workspace_size_list_; +}; // namespace kernel +} // namespace kernel +} // namespace mindspore +#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GPU_DEBUG_PRINT_GPU_KERNEL_H_ diff --git a/mindspore/ops/operations/debug_ops.py b/mindspore/ops/operations/debug_ops.py index 0db42b2c8b..77d55d9e97 100644 --- a/mindspore/ops/operations/debug_ops.py +++ b/mindspore/ops/operations/debug_ops.py @@ -337,7 +337,7 @@ class Print(PrimitiveWithInfer): Supports multiple inputs which are separated by ','. Supported Platforms: - ``Ascend`` + ``Ascend`` ``GPU`` Examples: >>> class PrintDemo(nn.Cell):