Skip to content

Commit 68978c3

Browse files
committed
format code style
1 parent 3a55565 commit 68978c3

10 files changed

Lines changed: 201 additions & 176 deletions

File tree

src/nnfusion/core/kernels/cuda_gpu/cuda_cudnn.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,12 @@ LanguageUnit_p cuda::get_cudnn_convolution_descriptor(const Shape& padding,
201201
<< "window_dilation_strides_int, CUDNN_CROSS_CORRELATION, " << data_type << "));\n";
202202
}
203203

204-
if(type == nnfusion::element::f16){
204+
if (type == nnfusion::element::f16)
205+
{
205206
// half precision, use tensor core
206207
lu << "CUDNN_SAFE_CALL(cudnnSetConvolutionMathType(" << desc << ", "
207-
<< "CUDNN_TENSOR_OP_MATH"
208-
<< "));\n";
208+
<< "CUDNN_TENSOR_OP_MATH"
209+
<< "));\n";
209210
}
210211

211212
return _lu;

src/nnfusion/core/kernels/cuda_gpu/kernels/batch_matmul.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ namespace nnfusion
103103
@hCublas@, @transA@, @transB@, @m@, @n@, @k@,
104104
&alpha, input1, @lda@, @stride_a@, input0, @ldb@, @stride_b@,
105105
&beta, output0, @ldc@, @stride_c@, @batch@));
106-
)" :
107-
R"(
106+
)"
107+
:
108+
R"(
108109
static const float alpha = @alpha@F, beta = @beta@F;
109110
// if (!@hCublas@)
110111
// CUBLAS_SAFE_CALL(@api_create@(&@hCublas@));
@@ -116,7 +117,9 @@ namespace nnfusion
116117
{
117118
{"hCublas", "cublas_handle"},
118119
{"api_create", "cublasCreate"},
119-
{"api_exec", dtype == nnfusion::element::f16 ? "cublasHgemmStridedBatched" : "cublasSgemmStridedBatched"},
120+
{"api_exec",
121+
dtype == nnfusion::element::f16 ? "cublasHgemmStridedBatched"
122+
: "cublasSgemmStridedBatched"},
120123
{"transA", transB ? "CUBLAS_OP_T" : "CUBLAS_OP_N"},
121124
{"transB", transA ? "CUBLAS_OP_T" : "CUBLAS_OP_N"},
122125
{"alpha", alpha},

src/nnfusion/core/kernels/cuda_gpu/kernels/batch_norm.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ LanguageUnit_p cuda::BatchNormNCHW::emit_function_body()
171171
/*
172172
* todo: may have better solution, details in https://github.com/microsoft/nnfusion/issues/434
173173
* */
174-
if(dtype == nnfusion::element::f16){
175-
lu << "output0[st + i] = __hadd(input1[c_id] , __hdiv(__hmul(input0[c_id], __hsub(input2[st + i], input3[c_id])), sqrtf(__hadd(__float2half("
174+
if (dtype == nnfusion::element::f16)
175+
{
176+
lu << "output0[st + i] = __hadd(input1[c_id] , __hdiv(__hmul(input0[c_id], "
177+
"__hsub(input2[st + i], input3[c_id])), sqrtf(__hadd(__float2half("
176178
<< epsilon << "), input4[c_id]))));\n";
177-
}else{
179+
}
180+
else
181+
{
178182
lu << "(input1[c_id] + (input0[c_id] * "
179183
"(input2[st + i] - input3[c_id]) / sqrtf("
180184
<< epsilon << " + input4[c_id])));\n";

src/nnfusion/core/kernels/cuda_gpu/kernels/dot.cpp

Lines changed: 142 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -207,161 +207,165 @@ LanguageUnit_p cuda::Dot::emit_function_body()
207207
else if (dtype == element::f16)
208208
{
209209
// case 1: Scalar * Tensor
210-
if (arg0_shape.empty() || arg1_shape.empty())
211-
{
212-
auto& second = (arg0_shape.empty() ? arg1_shape : arg0_shape);
213-
size_t count = nnfusion::shape_size(second);
210+
if (arg0_shape.empty() || arg1_shape.empty())
211+
{
212+
auto& second = (arg0_shape.empty() ? arg1_shape : arg0_shape);
213+
size_t count = nnfusion::shape_size(second);
214214

215-
string firstarg = (arg0_shape.empty() ? "input1" : "input0");
216-
string secondarg = (arg0_shape.empty() ? "input0" : "input1");
215+
string firstarg = (arg0_shape.empty() ? "input1" : "input0");
216+
string secondarg = (arg0_shape.empty() ? "input0" : "input1");
217217

218-
lu << "cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n";
218+
lu << "cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n";
219219

220-
lu << "CUDA_SAFE_CALL(cudaMemcpy(outupt0, " << firstarg << ", " << count << ", cudaMemcpyDeviceToDevice));\n"; // copy `firstarg` to `output0`
221-
lu << "CUBLAS_SAFE_CALL(nnfusionHalfScale(" << secondarg << ", output0, " << count << "));\n";
222-
}
220+
lu << "CUDA_SAFE_CALL(cudaMemcpy(outupt0, " << firstarg << ", " << count
221+
<< ", cudaMemcpyDeviceToDevice));\n"; // copy `firstarg` to `output0`
222+
lu << "CUBLAS_SAFE_CALL(nnfusionHalfScale(" << secondarg << ", output0, " << count
223+
<< "));\n";
224+
}
223225
// // case 2: 1d Dot
224-
else if ((arg0_shape.size() == arg1_shape.size()) && (arg0_shape.size() == reduction_axes))
225-
{
226-
for (int i = 0; i < arg0_shape.size(); i++)
227-
{
228-
if (arg0_shape[i] != arg1_shape[i])
229-
{
230-
std::vector<std::string> arg_vec{"arg0", "arg1"};
231-
std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
232-
233-
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
234-
<< nnfusion::join(shape_vec) << " respectively, at Node "
235-
<< m_context->gnode->get_name()
236-
<< ", do not match for dot op";
237-
}
238-
}
239-
240-
size_t count = nnfusion::shape_size(arg0_shape);
241-
lu << "cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n";
242-
243-
lu << "CUBLAS_SAFE_CALL(cublasSdot(cublas_handle, " << count
244-
<< ", static_cast<const float*>(input0), 1, static_cast<const float*>(input1), 1, "
245-
"static_cast<float*>(output0)));\n";
246-
}
247-
// // matrix * vector
248-
else if ((arg0_shape.size() == 2) && (arg1_shape.size() == 1) && (reduction_axes == 1))
249-
{
250-
lu << "const float alpha = 1.0;\n const float beta = 0;\n";
251-
lu << "CUBLAS_SAFE_CALL(cublasSgemv(cublas_handle, ";
252-
if (trans_A)
253-
lu << "CUBLAS_OP_N, " << arg0_shape[0] << ", " << arg0_shape[1] << ", ";
254-
else
255-
lu << "CUBLAS_OP_T, " << arg0_shape[1] << ", " << arg0_shape[0] << ", ";
256-
lu << " &alpha,"
257-
<< " static_cast<const float*>(input0)," << arg0_shape[1] << ", "
258-
<< " static_cast<const float*>(input1),"
259-
<< " 1,"
260-
<< " &beta,"
261-
<< " static_cast<float*>(output0),"
262-
<< " 1));\n";
263-
}
264-
else if ((arg0_shape.size() == 2) && (arg1_shape.size() == 2) && (reduction_axes == 1) &&
265-
(trans_A || trans_B))
266-
{
267-
int m = trans_B ? arg1_shape[0] : arg1_shape[1];
268-
int n = trans_A ? arg0_shape[1] : arg0_shape[0];
269-
int k = trans_A ? arg0_shape[0] : arg0_shape[1];
270-
271-
lu << "const half alpha = 1.0;\nconst half beta = 0;\n";
272-
273-
lu << "CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
274-
<< (trans_B ? " CUBLAS_OP_T," : " CUBLAS_OP_N,")
275-
<< (trans_A ? " CUBLAS_OP_T," : " CUBLAS_OP_N,") << " " << m << ","
276-
<< " " << n << ","
277-
<< " " << k << ","
278-
<< " &alpha,"
279-
<< " static_cast<const half*>(input1),"
280-
<< " " << arg1_shape[1] << ","
281-
<< " static_cast<const half*>(input0),"
282-
<< " " << arg0_shape[1] << ","
283-
<< " &beta,"
284-
<< " static_cast<half*>(output0),"
285-
<< " " << m << "));\n";
286-
} else {
287-
size_t axes_for_m_count = arg0_shape.size() - reduction_axes;
288-
size_t axes_for_n_count = arg1_shape.size() - reduction_axes;
289-
size_t axes_for_k_count = reduction_axes;
290-
size_t m = 1;
291-
size_t n = 1;
292-
size_t k = 1;
293-
294-
// check if input and output size correct
295-
// check and calculate k for arg0 and arg1
296-
size_t arg0_k_idx = axes_for_m_count; // first axe in arg0 for k
297-
size_t arg1_k_idx = 0; // first axe in arg1 for k
298-
299-
for (size_t i = 0; i < axes_for_k_count; i++)
226+
else if ((arg0_shape.size() == arg1_shape.size()) && (arg0_shape.size() == reduction_axes))
300227
{
301-
k *= arg0_shape[arg0_k_idx];
302-
if (arg0_shape[arg0_k_idx++] != arg1_shape[arg1_k_idx++])
228+
for (int i = 0; i < arg0_shape.size(); i++)
303229
{
304-
std::vector<std::string> arg_vec{"arg0", "arg1"};
305-
std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
230+
if (arg0_shape[i] != arg1_shape[i])
231+
{
232+
std::vector<std::string> arg_vec{"arg0", "arg1"};
233+
std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
306234

307-
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
308-
<< nnfusion::join(shape_vec) << " respectively, at Node "
309-
<< m_context->gnode->get_name()
310-
<< ", do not match for dot op";
235+
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
236+
<< nnfusion::join(shape_vec) << " respectively, at Node "
237+
<< m_context->gnode->get_name()
238+
<< ", do not match for dot op";
239+
}
311240
}
241+
242+
size_t count = nnfusion::shape_size(arg0_shape);
243+
lu << "cublasSetPointerMode(cublas_handle, CUBLAS_POINTER_MODE_DEVICE);\n";
244+
245+
lu << "CUBLAS_SAFE_CALL(cublasSdot(cublas_handle, " << count
246+
<< ", static_cast<const float*>(input0), 1, static_cast<const float*>(input1), 1, "
247+
"static_cast<float*>(output0)));\n";
312248
}
313-
// check and calculate m for arg0 and out
314-
size_t arg0_m_idx = 0; // first axe in arg0 for m
315-
size_t out_m_idx = 0; // first axe in out for m
316-
for (size_t i = 0; i < axes_for_m_count; i++)
249+
// // matrix * vector
250+
else if ((arg0_shape.size() == 2) && (arg1_shape.size() == 1) && (reduction_axes == 1))
317251
{
318-
m *= arg0_shape[arg0_m_idx];
319-
if (arg0_shape[arg0_m_idx++] != out_shape[out_m_idx++])
320-
{
321-
std::vector<std::string> arg_vec{"arg0", "output"};
322-
std::vector<nnfusion::Shape> shape_vec{arg0_shape, out_shape};
252+
lu << "const float alpha = 1.0;\n const float beta = 0;\n";
253+
lu << "CUBLAS_SAFE_CALL(cublasSgemv(cublas_handle, ";
254+
if (trans_A)
255+
lu << "CUBLAS_OP_N, " << arg0_shape[0] << ", " << arg0_shape[1] << ", ";
256+
else
257+
lu << "CUBLAS_OP_T, " << arg0_shape[1] << ", " << arg0_shape[0] << ", ";
258+
lu << " &alpha,"
259+
<< " static_cast<const float*>(input0)," << arg0_shape[1] << ", "
260+
<< " static_cast<const float*>(input1),"
261+
<< " 1,"
262+
<< " &beta,"
263+
<< " static_cast<float*>(output0),"
264+
<< " 1));\n";
265+
}
266+
else if ((arg0_shape.size() == 2) && (arg1_shape.size() == 2) && (reduction_axes == 1) &&
267+
(trans_A || trans_B))
268+
{
269+
int m = trans_B ? arg1_shape[0] : arg1_shape[1];
270+
int n = trans_A ? arg0_shape[1] : arg0_shape[0];
271+
int k = trans_A ? arg0_shape[0] : arg0_shape[1];
323272

324-
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
325-
<< nnfusion::join(shape_vec) << " respectively, at Node "
326-
<< m_context->gnode->get_name()
327-
<< ", do not match for dot op";
328-
}
273+
lu << "const half alpha = 1.0;\nconst half beta = 0;\n";
274+
275+
lu << "CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
276+
<< (trans_B ? " CUBLAS_OP_T," : " CUBLAS_OP_N,")
277+
<< (trans_A ? " CUBLAS_OP_T," : " CUBLAS_OP_N,") << " " << m << ","
278+
<< " " << n << ","
279+
<< " " << k << ","
280+
<< " &alpha,"
281+
<< " static_cast<const half*>(input1),"
282+
<< " " << arg1_shape[1] << ","
283+
<< " static_cast<const half*>(input0),"
284+
<< " " << arg0_shape[1] << ","
285+
<< " &beta,"
286+
<< " static_cast<half*>(output0),"
287+
<< " " << m << "));\n";
329288
}
330-
// check and calculate n for arg1 and out
331-
size_t arg1_n_idx = axes_for_k_count; // first axe in arg1 for n
332-
size_t out_n_idx = axes_for_m_count; // first axe in arg1 for n
333-
for (size_t i = 0; i < axes_for_n_count; i++)
289+
else
334290
{
335-
n *= arg1_shape[arg1_n_idx];
336-
if (arg1_shape[arg1_n_idx++] != out_shape[out_n_idx++])
291+
size_t axes_for_m_count = arg0_shape.size() - reduction_axes;
292+
size_t axes_for_n_count = arg1_shape.size() - reduction_axes;
293+
size_t axes_for_k_count = reduction_axes;
294+
size_t m = 1;
295+
size_t n = 1;
296+
size_t k = 1;
297+
298+
// check if input and output size correct
299+
// check and calculate k for arg0 and arg1
300+
size_t arg0_k_idx = axes_for_m_count; // first axe in arg0 for k
301+
size_t arg1_k_idx = 0; // first axe in arg1 for k
302+
303+
for (size_t i = 0; i < axes_for_k_count; i++)
337304
{
338-
std::vector<std::string> arg_vec{"arg1", "output"};
339-
std::vector<nnfusion::Shape> shape_vec{arg1_shape, out_shape};
305+
k *= arg0_shape[arg0_k_idx];
306+
if (arg0_shape[arg0_k_idx++] != arg1_shape[arg1_k_idx++])
307+
{
308+
std::vector<std::string> arg_vec{"arg0", "arg1"};
309+
std::vector<nnfusion::Shape> shape_vec{arg0_shape, arg1_shape};
340310

341-
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
342-
<< nnfusion::join(shape_vec) << " respectively, at Node "
343-
<< m_context->gnode->get_name()
344-
<< ", do not match for dot op";
311+
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
312+
<< nnfusion::join(shape_vec) << " respectively, at Node "
313+
<< m_context->gnode->get_name()
314+
<< ", do not match for dot op";
315+
}
345316
}
346-
}
317+
// check and calculate m for arg0 and out
318+
size_t arg0_m_idx = 0; // first axe in arg0 for m
319+
size_t out_m_idx = 0; // first axe in out for m
320+
for (size_t i = 0; i < axes_for_m_count; i++)
321+
{
322+
m *= arg0_shape[arg0_m_idx];
323+
if (arg0_shape[arg0_m_idx++] != out_shape[out_m_idx++])
324+
{
325+
std::vector<std::string> arg_vec{"arg0", "output"};
326+
std::vector<nnfusion::Shape> shape_vec{arg0_shape, out_shape};
327+
328+
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
329+
<< nnfusion::join(shape_vec) << " respectively, at Node "
330+
<< m_context->gnode->get_name()
331+
<< ", do not match for dot op";
332+
}
333+
}
334+
// check and calculate n for arg1 and out
335+
size_t arg1_n_idx = axes_for_k_count; // first axe in arg1 for n
336+
size_t out_n_idx = axes_for_m_count; // first axe in arg1 for n
337+
for (size_t i = 0; i < axes_for_n_count; i++)
338+
{
339+
n *= arg1_shape[arg1_n_idx];
340+
if (arg1_shape[arg1_n_idx++] != out_shape[out_n_idx++])
341+
{
342+
std::vector<std::string> arg_vec{"arg1", "output"};
343+
std::vector<nnfusion::Shape> shape_vec{arg1_shape, out_shape};
347344

348-
lu << "const half alpha = 1.0f;\nconst half beta = 0.f;\n";
349-
350-
lu << "CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
351-
<< " CUBLAS_OP_N,"
352-
<< " CUBLAS_OP_N,"
353-
<< " " << n << ","
354-
<< " " << m << ","
355-
<< " " << k << ","
356-
<< " &alpha,"
357-
<< " static_cast<const half*>(input1),"
358-
<< " " << n << ","
359-
<< " static_cast<const half*>(input0),"
360-
<< " " << k << ","
361-
<< " &beta,"
362-
<< " static_cast<half*>(output0),"
363-
<< " " << n << "));\n";
364-
}
345+
NNFUSION_CHECK_FAIL() << nnfusion::join(arg_vec) << " with "
346+
<< nnfusion::join(shape_vec) << " respectively, at Node "
347+
<< m_context->gnode->get_name()
348+
<< ", do not match for dot op";
349+
}
350+
}
351+
352+
lu << "const half alpha = 1.0f;\nconst half beta = 0.f;\n";
353+
354+
lu << "CUBLAS_SAFE_CALL(cublasHgemm(cublas_handle,"
355+
<< " CUBLAS_OP_N,"
356+
<< " CUBLAS_OP_N,"
357+
<< " " << n << ","
358+
<< " " << m << ","
359+
<< " " << k << ","
360+
<< " &alpha,"
361+
<< " static_cast<const half*>(input1),"
362+
<< " " << n << ","
363+
<< " static_cast<const half*>(input0),"
364+
<< " " << k << ","
365+
<< " &beta,"
366+
<< " static_cast<half*>(output0),"
367+
<< " " << n << "));\n";
368+
}
365369
}
366370
else
367371
{

src/nnfusion/core/kernels/cuda_gpu/kernels/reduce.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@ for (int tidx = thread_idx; tidx < width; tidx += block_size) {
230230
val = reduceSum(val, thread_idx, block_size, shm);
231231
if (thread_idx == 0) output0[block_idx] = val;
232232
)",
233-
{{"width", width}, {"block_size", expected_block_size}, {"warp_size", 32},{"dataType", dtype==nnfusion::element::f16? "half" : "float"}});
233+
{{"width", width},
234+
{"block_size", expected_block_size},
235+
{"warp_size", 32},
236+
{"dataType", dtype == nnfusion::element::f16 ? "half" : "float"}});
234237

235238
lu << code << "\n";
236239
return _lu;
@@ -582,7 +585,6 @@ if (thread_idx == 0) output0[block_idx] = val;
582585
m_gridDim = dim3(1, 1, 1);
583586
m_blockDim = dim3(block_size_x, 1, 1);
584587
}
585-
586588
}
587589
}
588590

src/nnfusion/core/operators/generic_op/generic_op_define/Slice.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ REGISTER_OP(Slice)
3636
auto step = steps[d];
3737
auto start = starts[d];
3838
auto end = ends[d];
39-
auto range = (u_int64_t)ceil((double)(end-start)/(double)step);
40-
input_layout.push_back((step == 1? output_layout[d] : output_layout[d] + " * " + to_string(step)) + " + " + to_string(start));
41-
slice_dims += (slice_dims.empty() ? "" : " , ") + output_layout[d] +
42-
" in " + to_string(range);
39+
auto range = (u_int64_t)ceil((double)(end - start) / (double)step);
40+
input_layout.push_back(
41+
(step == 1 ? output_layout[d] : output_layout[d] + " * " + to_string(step)) +
42+
" + " + to_string(start));
43+
slice_dims +=
44+
(slice_dims.empty() ? "" : " , ") + output_layout[d] + " in " + to_string(range);
4345
}
4446

4547
auto expression_code = op::create_code_from_template(

0 commit comments

Comments
 (0)