1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "runtime/device/launch_mul.h"
18 #include "abstract/utils.h"
19 #include "backend/session/single_kernel_graph.h"
20 #include "frontend/parallel/context.h"
21
22 namespace mindspore::device {
ObtainMulKernelGraph()23 std::shared_ptr<session::KernelGraph> LaunchMul::ObtainMulKernelGraph() {
24 std::vector<TypeId> input_dtypes = {dtype_, dtype_};
25 std::vector<TypeId> output_dtypes = {dtype_};
26 // obtain input & output shapes
27 size_t dtype_size = abstract::TypeIdSize(dtype_);
28 if (dtype_size == 0) {
29 MS_LOG(EXCEPTION) << "Divide by zero.";
30 }
31 int64_t shape = SizeToLong(total_size_ / dtype_size);
32 std::vector<std::vector<int64_t>> input_shapes = {{shape}, {1}};
33 std::vector<std::vector<size_t>> output_shapes = {{static_cast<size_t>(shape)}};
34 auto mul_graph = session::SingleKernelGraph::ConstructKernelGraphBasedOnSingleOp(
35 kMulOpName, input_dtypes, input_shapes, output_dtypes, output_shapes);
36 MS_EXCEPTION_IF_NULL(mul_graph);
37 return mul_graph;
38 }
39
ObtainLaunchMulKernelMod()40 kernel::KernelMod *LaunchMul::ObtainLaunchMulKernelMod() {
41 if (mul_graph_ == nullptr) {
42 // construct mul kernel graph
43 mul_graph_ = ObtainMulKernelGraph();
44 MS_EXCEPTION_IF_NULL(mul_graph_);
45 // kernel select
46 KernelSelect(mul_graph_);
47 // kernel build
48 KernelBuild(mul_graph_);
49 }
50 // obtain kernel_mod
51 if (mul_graph_->execution_order().size() != 1) {
52 MS_LOG(ERROR) << "the execution order of the mul graph should have only one node, however, it has "
53 << mul_graph_->execution_order().size() << " nodes.";
54 }
55 return AnfAlgo::GetKernelMod(mul_graph_->execution_order()[0]);
56 }
57
ObtainMulInputsAddr()58 void LaunchMul::ObtainMulInputsAddr() {
59 inputs_addr_.push_back(input1_addr_);
60
61 auto parallel_context = parallel::ParallelContext::GetInstance();
62 MS_EXCEPTION_IF_NULL(parallel_context);
63 auto device_num = parallel_context->device_num();
64 if (device_num == 0) {
65 MS_LOG(ERROR) << "device num can't be zero";
66 }
67 input2_value_ = 1.0f / device_num;
68 auto size = abstract::TypeIdSize(dtype_);
69 auto input_size = AlignSizeForLaunchKernel(size * 1);
70 // alloc memory
71 input2_addr_ = AllocDeviceMem(input_size);
72 CopyHostMemToDevice(size, input_size);
73 inputs_addr_.push_back(input2_addr_);
74 }
75
FreeInputDeviceMemory()76 void LaunchMul::FreeInputDeviceMemory() {
77 input1_addr_ = nullptr;
78 if (input2_addr_ != nullptr) {
79 FreeDeviceMem(input2_addr_);
80 input2_addr_ = nullptr;
81 }
82 inputs_addr_.clear();
83 }
84 } // namespace mindspore::device
85