Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jerrrrry
infinicore
Commits
c9dbc0ff
Commit
c9dbc0ff
authored
Feb 26, 2025
by
zhangyue
Browse files
issue/25: restruct matmul
parent
8583cae7
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
153 additions
and
176 deletions
+153
-176
src/infiniop/devices/handle.cc
src/infiniop/devices/handle.cc
+1
-1
src/infiniop/devices/kunlun/common_kunlun.h
src/infiniop/devices/kunlun/common_kunlun.h
+7
-9
src/infiniop/devices/kunlun/kunlun_handle.cc
src/infiniop/devices/kunlun/kunlun_handle.cc
+8
-8
src/infiniop/devices/kunlun/kunlun_handle.h
src/infiniop/devices/kunlun/kunlun_handle.h
+3
-3
src/infiniop/ops/matmul/kunlun/matmul_kunlun.cc
src/infiniop/ops/matmul/kunlun/matmul_kunlun.cc
+113
-0
src/infiniop/ops/matmul/kunlun/matmul_kunlun.h
src/infiniop/ops/matmul/kunlun/matmul_kunlun.h
+8
-0
src/infiniop/ops/matmul/kunlun/matmul_xdnn.cc
src/infiniop/ops/matmul/kunlun/matmul_xdnn.cc
+0
-106
src/infiniop/ops/matmul/kunlun/matmul_xdnn.h
src/infiniop/ops/matmul/kunlun/matmul_xdnn.h
+0
-17
src/infiniop/ops/matmul/kunlun/matmul_xdnn_api.h
src/infiniop/ops/matmul/kunlun/matmul_xdnn_api.h
+0
-31
src/infiniop/ops/matmul/operator.cc
src/infiniop/ops/matmul/operator.cc
+13
-1
No files found.
src/infiniop/devices/handle.cc
View file @
c9dbc0ff
...
...
@@ -12,7 +12,7 @@
#include "ascend/ascend_handle.h"
#endif
#ifdef ENABLE_KUNLUN_API
#include "
./
kunlun/kunlun_handle.h"
#include "kunlun/kunlun_handle.h"
#endif
__C
infiniStatus_t
infiniopCreateHandle
(
infiniopHandle_t
*
handle_ptr
,
...
...
src/infiniop/devices/kunlun/common_kunlun.h
View file @
c9dbc0ff
...
...
@@ -11,6 +11,7 @@
namespace
xdnn
=
baidu
::
xpu
::
api
;
typedef
xdnn
::
Context
*
xdnnHandle_t
;
typedef
XPUStream
KunlunStream_t
;
#define CHECK_KUNLUN(call) \
{ \
...
...
@@ -18,7 +19,7 @@ typedef xdnn::Context *xdnnHandle_t;
if (XPU_SUCCESS != err) { \
fprintf(stderr, "KUNLUN error in %s:%i : %s.\n", __FILE__, \
__LINE__, xpu_strerror(err)); \
return INFINI
OP
_STATUS_INTERNAL_ERROR; \
return INFINI_STATUS_INTERNAL_ERROR;
\
} \
}
...
...
@@ -29,17 +30,14 @@ struct InfiniopKunlunHandle {
};
template
<
typename
T
>
infiniopStatus_t
use_xdnn
(
std
::
shared_ptr
<
Pool
<
xdnnHandle_t
>>
xdnn_handle_pool
,
XPUStream
stream
,
T
const
&
f
)
{
auto
handle
=
xdnn_handle_pool
->
pop
();
void
use_xdnn
(
std
::
shared_ptr
<
Pool
<
xdnnHandle_t
>>
&
pool
,
KunlunStream_t
stream
,
T
const
&
f
)
{
auto
handle
=
pool
->
pop
();
if
(
!
handle
)
{
*
handle
=
xdnn
::
create_context
();
}
(
*
handle
)
->
set_stream
(
stream
);
auto
ret
=
f
(
*
handle
);
xdnn_handle_pool
->
push
(
std
::
move
(
*
handle
));
return
ret
;
f
(
*
handle
);
pool
->
push
(
std
::
move
(
*
handle
));
}
#endif
#endif
//__INFINIOP_COMMON_KUNLUN_H__
src/infiniop/devices/kunlun/kunlun_handle.cc
View file @
c9dbc0ff
#include "common_kunlun.h"
infini
op
Status_t
createKunlunHandle
(
infiniopKunlunHandle_t
*
handle_ptr
)
{
infiniStatus_t
createKunlunHandle
(
infiniopKunlunHandle_t
*
handle_ptr
)
{
int
device_id
;
CHECK_KUNLUN
(
xpu_current_device
(
&
device_id
))
CHECK_KUNLUN
(
xpu_current_device
(
&
device_id
));
auto
pool
=
std
::
make_shared
<
Pool
<
xdnnHandle_t
>>
();
xdnnHandle_t
handle
=
xdnn
::
create_context
();
pool
->
push
(
std
::
move
(
handle
));
...
...
@@ -14,11 +13,12 @@ infiniopStatus_t createKunlunHandle(infiniopKunlunHandle_t *handle_ptr) {
std
::
move
(
pool
),
};
return
INFINI
OP
_STATUS_SUCCESS
;
return
INFINI_STATUS_SUCCESS
;
}
infiniopStatus_t
destroyKunlunHandle
(
infiniopKunlunHandle_t
handle
)
{
handle
->
xdnn_handle_pool
=
nullptr
;
delete
handle
;
return
INFINIOP_STATUS_SUCCESS
;
infiniStatus_t
destroyKunlunHandle
(
infiniopKunlunHandle_t
handle_ptr
)
{
handle_ptr
->
xdnn_handle_pool
=
nullptr
;
delete
handle_ptr
;
return
INFINI_STATUS_SUCCESS
;
}
src/infiniop/devices/kunlun/kunlun_handle.h
View file @
c9dbc0ff
...
...
@@ -7,7 +7,7 @@ struct InfiniopKunlunHandle;
typedef
struct
InfiniopKunlunHandle
*
infiniopKunlunHandle_t
;
infini
op
Status_t
createKunlunHandle
(
infiniopKunlunHandle_t
*
handle_ptr
);
infini
op
Status_t
destroyKunlunHandle
(
infiniopKunlunHandle_t
handle
);
infiniStatus_t
createKunlunHandle
(
infiniopKunlunHandle_t
*
handle_ptr
);
infiniStatus_t
destroyKunlunHandle
(
infiniopKunlunHandle_t
handle
);
#endif
#endif
// __INFINIOP_KUNLUN_HANDLE_H__
src/infiniop/ops/matmul/kunlun/matmul_kunlun.cc
0 → 100644
View file @
c9dbc0ff
#include "matmul_kunlun.h"
#include "../../../devices/kunlun/common_kunlun.h"
#include "../../utils.h"
namespace
matmul
::
kunlun
{
struct
Descriptor
::
Opaque
{
std
::
shared_ptr
<
Pool
<
xdnnHandle_t
>>
xdnn_handle_pool
;
};
Descriptor
::~
Descriptor
()
{
delete
_opaque
;
}
infiniStatus_t
Descriptor
::
create
(
infiniopHandle_t
handle_
,
Descriptor
**
desc_ptr
,
infiniopTensorDescriptor_t
c_desc
,
infiniopTensorDescriptor_t
a_desc
,
infiniopTensorDescriptor_t
b_desc
)
{
auto
handle
=
reinterpret_cast
<
infiniopKunlunHandle_t
>
(
handle_
);
auto
dtype
=
c_desc
->
dtype
;
if
(
dtype
!=
INFINI_DTYPE_F16
&&
dtype
!=
INFINI_DTYPE_F32
)
{
return
INFINI_STATUS_BAD_TENSOR_DTYPE
;
}
infiniStatus_t
status
;
auto
info
=
MatmulInfo
(
c_desc
,
a_desc
,
b_desc
,
&
status
,
MatrixLayout
::
ROW_MAJOR
);
if
(
status
!=
INFINI_STATUS_SUCCESS
)
{
return
status
;
}
*
desc_ptr
=
new
Descriptor
(
dtype
,
info
,
0
,
new
Opaque
{
handle
->
xdnn_handle_pool
},
handle
->
device
,
handle
->
device_id
);
return
INFINI_STATUS_SUCCESS
;
}
template
<
class
Tdata
>
void
calculate
(
const
MatmulInfo
&
info
,
std
::
shared_ptr
<
Pool
<
xdnnHandle_t
>>
&
xdnn_handle_pool
,
infiniDtype_t
dtype
,
void
*
c
,
float
beta
,
void
const
*
a
,
void
const
*
b
,
float
alpha
,
KunlunStream_t
stream
)
{
if
(
info
.
is_transed
)
{
std
::
swap
(
a
,
b
);
}
auto
transA
=
info
.
a_matrix
.
col_stride
==
1
?
false
:
true
;
auto
transB
=
info
.
b_matrix
.
col_stride
==
1
?
false
:
true
;
auto
unit
=
infiniSizeof
(
dtype
);
use_xdnn
(
xdnn_handle_pool
,
(
KunlunStream_t
)
stream
,
[
&
](
xdnnHandle_t
handle
)
{
for
(
size_t
i
=
0
;
i
<
info
.
batch
;
i
++
)
{
xdnn
::
fc_fusion
<
Tdata
,
Tdata
,
Tdata
,
int16_t
>
(
handle
,
(
Tdata
*
)((
char
*
)
a
+
i
*
info
.
a_matrix
.
stride
*
unit
),
(
Tdata
*
)((
char
*
)
b
+
i
*
info
.
b_matrix
.
stride
*
unit
),
(
Tdata
*
)((
char
*
)
c
+
i
*
info
.
c_matrix
.
stride
*
unit
),
info
.
m
,
info
.
n
,
info
.
k
,
transA
,
transB
,
nullptr
,
nullptr
,
nullptr
,
info
.
a_matrix
.
ld
(),
info
.
b_matrix
.
ld
(),
info
.
c_matrix
.
ld
(),
alpha
,
beta
,
nullptr
,
xdnn
::
Activation_t
::
LINEAR
,
nullptr
);
}
});
}
infiniStatus_t
Descriptor
::
calculate
(
void
*
workspace
,
size_t
worksapce_size
,
void
*
c
,
float
beta
,
const
void
*
a
,
const
void
*
b
,
float
alpha
,
void
*
stream
)
const
{
switch
(
_dtype
)
{
case
INFINI_DTYPE_F16
:
kunlun
::
calculate
<
float16
>
(
_info
,
_opaque
->
xdnn_handle_pool
,
_dtype
,
c
,
beta
,
a
,
b
,
alpha
,
(
KunlunStream_t
)
stream
);
return
INFINI_STATUS_SUCCESS
;
case
INFINI_DTYPE_F32
:
kunlun
::
calculate
<
float
>
(
_info
,
_opaque
->
xdnn_handle_pool
,
_dtype
,
c
,
beta
,
a
,
b
,
alpha
,
(
KunlunStream_t
)
stream
);
return
INFINI_STATUS_SUCCESS
;
default:
return
INFINI_STATUS_BAD_TENSOR_DTYPE
;
}
}
}
// namespace matmul::kunlun
src/infiniop/ops/matmul/kunlun/matmul_kunlun.h
0 → 100644
View file @
c9dbc0ff
#ifndef __MATMUL_KUNLUN_H__
#define __MATMUL_KUNLUN_H__
#include "../matmul.h"
DESCRIPTOR
(
kunlun
)
#endif // __MATMUL_KUNLUN_H__
src/infiniop/ops/matmul/kunlun/matmul_xdnn.cc
deleted
100644 → 0
View file @
8583cae7
#include "matmul_xdnn.h"
template
<
typename
T
>
infiniopStatus_t
matmulKunlunCommon
(
infiniopMatmulKunlunDescriptor_t
desc
,
void
*
c
,
float
beta
,
void
const
*
a
,
void
const
*
b
,
float
alpha
,
void
*
stream
)
{
auto
info
=
desc
->
info
;
if
(
info
.
is_transed
)
{
std
::
swap
(
a
,
b
);
}
auto
transA
=
info
.
a_matrix
.
col_stride
==
1
?
false
:
true
;
auto
transB
=
info
.
b_matrix
.
col_stride
==
1
?
false
:
true
;
auto
ret
=
use_xdnn
(
desc
->
xdnn_handle_pool
,
(
XPUStream
)
stream
,
[
&
](
xdnnHandle_t
handle
)
{
for
(
size_t
i
=
0
;
i
<
info
.
batch
;
i
++
)
{
CHECK_KUNLUN
((
xdnn
::
fc_fusion
<
T
,
T
,
T
,
int16_t
>
(
handle
,
(
T
*
)((
char
*
)
a
+
i
*
info
.
a_matrix
.
stride
*
infiniSizeof
(
desc
->
dtype
)),
(
T
*
)((
char
*
)
b
+
i
*
info
.
b_matrix
.
stride
*
infiniSizeof
(
desc
->
dtype
)),
(
T
*
)((
char
*
)
c
+
i
*
info
.
c_matrix
.
stride
*
infiniSizeof
(
desc
->
dtype
)),
info
.
m
,
info
.
n
,
info
.
k
,
transA
,
transB
,
nullptr
,
nullptr
,
nullptr
,
info
.
a_matrix
.
ld
(),
info
.
b_matrix
.
ld
(),
info
.
c_matrix
.
ld
(),
alpha
,
beta
,
nullptr
,
xdnn
::
Activation_t
::
LINEAR
,
nullptr
)));
}
return
INFINIOP_STATUS_SUCCESS
;
});
return
ret
;
}
infiniopStatus_t
kunlunCreateMatmulDescriptor
(
infiniopKunlunHandle_t
handle
,
infiniopMatmulKunlunDescriptor_t
*
desc_ptr
,
infiniopTensorDescriptor_t
c_desc
,
infiniopTensorDescriptor_t
a_desc
,
infiniopTensorDescriptor_t
b_desc
)
{
infiniDtype_t
dtype
=
c_desc
->
dtype
;
if
(
dtype
!=
INFINI_DTYPE_F16
&&
dtype
!=
INFINI_DTYPE_F32
)
{
return
INFINIOP_STATUS_BAD_TENSOR_DTYPE
;
}
infiniopStatus_t
status
;
auto
info
=
MatmulInfo
(
c_desc
,
a_desc
,
b_desc
,
&
status
,
false
);
if
(
status
!=
INFINIOP_STATUS_SUCCESS
)
{
return
status
;
}
*
desc_ptr
=
new
InfiniopMatmulKunlunDescriptor
{
INFINI_DEVICE_KUNLUN
,
dtype
,
handle
->
device_id
,
info
,
handle
->
xdnn_handle_pool
};
return
INFINIOP_STATUS_SUCCESS
;
}
infiniopStatus_t
kunlunGetMatmulWorkspaceSize
(
infiniopMatmulKunlunDescriptor_t
desc
,
size_t
*
size
)
{
*
size
=
0
;
return
INFINIOP_STATUS_SUCCESS
;
}
infiniopStatus_t
kunlunMatmul
(
infiniopMatmulKunlunDescriptor_t
desc
,
void
*
workspace
,
size_t
workspace_size
,
void
*
c
,
void
const
*
a
,
void
const
*
b
,
float
alpha
,
float
beta
,
void
*
stream
)
{
if
(
desc
->
dtype
==
INFINI_DTYPE_F16
)
{
return
matmulKunlunCommon
<
float16
>
(
desc
,
c
,
beta
,
a
,
b
,
alpha
,
stream
);
}
if
(
desc
->
dtype
==
INFINI_DTYPE_F32
)
{
return
matmulKunlunCommon
<
float
>
(
desc
,
c
,
beta
,
a
,
b
,
alpha
,
stream
);
}
return
INFINIOP_STATUS_BAD_TENSOR_DTYPE
;
}
infiniopStatus_t
kunlunDestroyMatmulDescriptor
(
infiniopMatmulKunlunDescriptor_t
desc
)
{
desc
->
xdnn_handle_pool
=
nullptr
;
delete
desc
;
return
INFINIOP_STATUS_SUCCESS
;
}
src/infiniop/ops/matmul/kunlun/matmul_xdnn.h
deleted
100644 → 0
View file @
8583cae7
#ifndef __MATMUL_XDNN_H__
#define __MATMUL_XDNN_H__
#include "../../../devices/kunlun/common_kunlun.h"
#include "../../utils.h"
#include "../blas.h"
#include "matmul_xdnn_api.h"
struct
InfiniopMatmulKunlunDescriptor
{
infiniDevice_t
device
;
infiniDtype_t
dtype
;
int
device_id
;
MatmulInfo
info
;
std
::
shared_ptr
<
Pool
<
xdnnHandle_t
>>
xdnn_handle_pool
;
};
#endif
src/infiniop/ops/matmul/kunlun/matmul_xdnn_api.h
deleted
100644 → 0
View file @
8583cae7
#ifndef __MATMUL_XDNN_API_H__
#define __MATMUL_XDNN_API_H__
#include "../../../devices/kunlun/kunlun_handle.h"
#include "infiniop/operator.h"
struct
InfiniopMatmulKunlunDescriptor
;
typedef
struct
InfiniopMatmulKunlunDescriptor
*
infiniopMatmulKunlunDescriptor_t
;
infiniopStatus_t
kunlunCreateMatmulDescriptor
(
infiniopKunlunHandle_t
handle
,
infiniopMatmulKunlunDescriptor_t
*
desc_ptr
,
infiniopTensorDescriptor_t
c_desc
,
infiniopTensorDescriptor_t
a_desc
,
infiniopTensorDescriptor_t
b_desc
);
infiniopStatus_t
kunlunGetMatmulWorkspaceSize
(
infiniopMatmulKunlunDescriptor_t
desc
,
size_t
*
size
);
infiniopStatus_t
kunlunMatmul
(
infiniopMatmulKunlunDescriptor_t
desc
,
void
*
workspace
,
size_t
workspace_size
,
void
*
c
,
void
const
*
a
,
void
const
*
b
,
float
alpha
,
float
beta
,
void
*
stream
);
infiniopStatus_t
kunlunDestroyMatmulDescriptor
(
infiniopMatmulKunlunDescriptor_t
desc
);
#endif
src/infiniop/ops/matmul/operator.cc
View file @
c9dbc0ff
...
...
@@ -13,7 +13,7 @@
#include "ascend/matmul_ascend.h"
#endif
#ifdef ENABLE_KUNLUN_API
#include "kunlun/matmul_
xdnn_api
.h"
#include "kunlun/matmul_
kunlun
.h"
#endif
__C
infiniStatus_t
infiniopCreateMatmulDescriptor
(
...
...
@@ -46,6 +46,9 @@ __C infiniStatus_t infiniopCreateMatmulDescriptor(
#ifdef ENABLE_ASCEND_API
CREATE
(
INFINI_DEVICE_ASCEND
,
ascend
);
#endif
#ifdef ENABLE_KUNLUN_API
CREATE
(
INFINI_DEVICE_KUNLUN
,
kunlun
);
#endif
default:
return
INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED
;
...
...
@@ -78,6 +81,9 @@ infiniopGetMatmulWorkspaceSize(
#ifdef ENABLE_ASCEND_API
GET
(
INFINI_DEVICE_ASCEND
,
ascend
);
#endif
#ifdef ENABLE_KUNLUN_API
GET
(
INFINI_DEVICE_KUNLUN
,
kunlun
);
#endif
default:
return
INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED
;
...
...
@@ -118,6 +124,9 @@ __C infiniStatus_t infiniopMatmul(
#ifdef ENABLE_ASCEND_API
CALCULATE
(
INFINI_DEVICE_ASCEND
,
ascend
);
#endif
#ifdef ENABLE_KUNLUN_API
CALCULATE
(
INFINI_DEVICE_KUNLUN
,
kunlun
);
#endif
default:
return
INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED
;
...
...
@@ -148,6 +157,9 @@ infiniopDestroyMatmulDescriptor(infiniopMatmulDescriptor_t desc) {
#ifdef ENABLE_ASCEND_API
DELETE
(
INFINI_DEVICE_ASCEND
,
ascend
);
#endif
#ifdef ENABLE_KUNLUN_API
DELETE
(
INFINI_DEVICE_KUNLUN
,
kunlun
);
#endif
default:
return
INFINI_STATUS_DEVICE_TYPE_NOT_SUPPORTED
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment