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
infinilm
Commits
c50a5d09
Commit
c50a5d09
authored
May 15, 2025
by
PanZezhong
Browse files
fix naming
parent
81fe2ba3
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
72 additions
and
72 deletions
+72
-72
src/models/jiuge/jiuge.cpp
src/models/jiuge/jiuge.cpp
+14
-14
src/models/jiuge/jiuge_kv_cache.cpp
src/models/jiuge/jiuge_kv_cache.cpp
+4
-4
src/models/jiuge/jiuge_weight.hpp
src/models/jiuge/jiuge_weight.hpp
+14
-14
src/tensor.hpp
src/tensor.hpp
+11
-11
src/tensor/tensor.cpp
src/tensor/tensor.cpp
+18
-18
src/tensor/transform.cpp
src/tensor/transform.cpp
+7
-7
src/utils.hpp
src/utils.hpp
+4
-4
No files found.
src/models/jiuge/jiuge.cpp
View file @
c50a5d09
...
@@ -24,32 +24,32 @@ void createDeviceResource(DeviceResource *rsrc, const JiugeMeta *meta,
...
@@ -24,32 +24,32 @@ void createDeviceResource(DeviceResource *rsrc, const JiugeMeta *meta,
w_ffn_norm
,
w_ffn_gate_up
,
w_ffn_down
;
w_ffn_norm
,
w_ffn_gate_up
,
w_ffn_down
;
for
(
size_t
layer
=
0
;
layer
<
meta
->
nlayer
;
layer
++
)
{
for
(
size_t
layer
=
0
;
layer
<
meta
->
nlayer
;
layer
++
)
{
w_attn_norm
.
push_back
(
w_attn_norm
.
push_back
(
get
_a
ttn
_n
orm
(
meta
,
weights
,
layer
));
get
A
ttn
N
orm
(
meta
,
weights
,
layer
));
w_attn_qkv
.
push_back
(
w_attn_qkv
.
push_back
(
get
_a
ttn
_qkv
(
meta
,
weights
,
layer
,
idev
,
ndev
));
get
A
ttn
QKV
(
meta
,
weights
,
layer
,
idev
,
ndev
));
if
(
weights
->
attn_qkv_b
!=
nullptr
)
{
if
(
weights
->
attn_qkv_b
!=
nullptr
)
{
b_attn_qkv
.
push_back
(
b_attn_qkv
.
push_back
(
get
_a
ttn
_qkv_b
ias
(
meta
,
weights
,
layer
,
idev
,
ndev
));
get
A
ttn
QKVB
ias
(
meta
,
weights
,
layer
,
idev
,
ndev
));
}
}
w_attn_out
.
push_back
(
w_attn_out
.
push_back
(
get
_a
ttn
_o
(
meta
,
weights
,
layer
,
idev
,
ndev
));
get
A
ttn
O
(
meta
,
weights
,
layer
,
idev
,
ndev
));
w_ffn_norm
.
push_back
(
w_ffn_norm
.
push_back
(
get
_ffn_n
orm
(
meta
,
weights
,
layer
));
get
FFNN
orm
(
meta
,
weights
,
layer
));
w_ffn_gate_up
.
push_back
(
w_ffn_gate_up
.
push_back
(
get
_ffn_g
ate
_u
p
(
meta
,
weights
,
layer
,
idev
,
ndev
));
get
FFNG
ate
U
p
(
meta
,
weights
,
layer
,
idev
,
ndev
));
w_ffn_down
.
push_back
(
w_ffn_down
.
push_back
(
get
_ffn_d
own
(
meta
,
weights
,
layer
,
idev
,
ndev
));
get
FFND
own
(
meta
,
weights
,
layer
,
idev
,
ndev
));
}
}
*
rsrc
=
DeviceResource
{
device
,
*
rsrc
=
DeviceResource
{
device
,
dev_id
,
dev_id
,
handle
,
handle
,
get
_in_e
mbd
(
meta
,
weights
),
get
InE
mbd
(
meta
,
weights
),
get
_out_n
orm
(
meta
,
weights
),
get
OutN
orm
(
meta
,
weights
),
get
_out_e
mbd
(
meta
,
weights
),
get
OutE
mbd
(
meta
,
weights
),
get
_sin_t
able
(
meta
),
get
SinT
able
(
meta
),
get
_cos_t
able
(
meta
),
get
CosT
able
(
meta
),
w_attn_norm
,
w_attn_norm
,
w_attn_qkv
,
w_attn_qkv
,
b_attn_qkv
,
b_attn_qkv
,
...
@@ -136,7 +136,7 @@ void inferDeviceBatch(const JiugeMeta &meta, const DeviceResource &rsrc,
...
@@ -136,7 +136,7 @@ void inferDeviceBatch(const JiugeMeta &meta, const DeviceResource &rsrc,
RUN_INFINI
(
infiniopGetGemmWorkspaceSize
(
desc_attn_o
,
&
temp_size
));
RUN_INFINI
(
infiniopGetGemmWorkspaceSize
(
desc_attn_o
,
&
temp_size
));
workspace_size
=
std
::
max
(
workspace_size
,
temp_size
);
workspace_size
=
std
::
max
(
workspace_size
,
temp_size
);
infiniopRoPEDescriptor_t
desc_rope_q
,
desc_rope_k
;
infiniopRoPEDescriptor_t
desc_rope_q
,
desc_rope_k
;
qkv_buf
->
dim
_s
plit
(
1
,
{
nh
+
nkvh
*
2
,
dh
});
// (ntok, nh + 2 * nkvh, dh)
qkv_buf
->
dim
S
plit
(
1
,
{
nh
+
nkvh
*
2
,
dh
});
// (ntok, nh + 2 * nkvh, dh)
auto
qkv_buf_q
=
qkv_buf
->
slice
(
1
,
0
,
nh
);
auto
qkv_buf_q
=
qkv_buf
->
slice
(
1
,
0
,
nh
);
auto
qkv_buf_k
=
qkv_buf
->
slice
(
1
,
nh
,
nkvh
);
auto
qkv_buf_k
=
qkv_buf
->
slice
(
1
,
nh
,
nkvh
);
RUN_INFINI
(
infiniopCreateRoPEDescriptor
(
RUN_INFINI
(
infiniopCreateRoPEDescriptor
(
...
@@ -154,7 +154,7 @@ void inferDeviceBatch(const JiugeMeta &meta, const DeviceResource &rsrc,
...
@@ -154,7 +154,7 @@ void inferDeviceBatch(const JiugeMeta &meta, const DeviceResource &rsrc,
// attention inner
// attention inner
auto
desc_attns
=
std
::
vector
<
infiniopAttentionDescriptor_t
>
(
nreq
);
auto
desc_attns
=
std
::
vector
<
infiniopAttentionDescriptor_t
>
(
nreq
);
size_t
token_offset
=
0
;
size_t
token_offset
=
0
;
o_buf
->
dim
_s
plit
(
1
,
{
nh
,
dh
});
o_buf
->
dim
S
plit
(
1
,
{
nh
,
dh
});
for
(
uint32_t
req
=
0
;
req
<
nreq
;
req
++
)
{
for
(
uint32_t
req
=
0
;
req
<
nreq
;
req
++
)
{
auto
past_len
=
req_pos
[
req
];
auto
past_len
=
req_pos
[
req
];
auto
seq_len
=
req_lens
[
req
];
auto
seq_len
=
req_lens
[
req
];
...
...
src/models/jiuge/jiuge_kv_cache.cpp
View file @
c50a5d09
...
@@ -32,13 +32,13 @@ __C struct KVCache *duplicateKVCache(const JiugeModel *model,
...
@@ -32,13 +32,13 @@ __C struct KVCache *duplicateKVCache(const JiugeModel *model,
for
(
unsigned
int
layer
=
0
;
layer
<
model
->
meta
.
nlayer
;
layer
++
)
{
for
(
unsigned
int
layer
=
0
;
layer
<
model
->
meta
.
nlayer
;
layer
++
)
{
new_kv_cache
->
k
[
idev
][
layer
]
new_kv_cache
->
k
[
idev
][
layer
]
->
slice
(
1
,
0
,
seq_len
)
->
slice
(
1
,
0
,
seq_len
)
->
copy
_f
rom
(
kv_cache
->
k
[
idev
][
layer
]
->
slice
(
1
,
0
,
seq_len
),
->
copy
F
rom
(
kv_cache
->
k
[
idev
][
layer
]
->
slice
(
1
,
0
,
seq_len
),
model
->
dev_resources
[
idev
].
handle
);
model
->
dev_resources
[
idev
].
handle
);
new_kv_cache
->
v
[
idev
][
layer
]
new_kv_cache
->
v
[
idev
][
layer
]
->
slice
(
1
,
0
,
seq_len
)
->
slice
(
1
,
0
,
seq_len
)
->
copy
_f
rom
(
kv_cache
->
v
[
idev
][
layer
]
->
slice
(
1
,
0
,
seq_len
),
->
copy
F
rom
(
kv_cache
->
v
[
idev
][
layer
]
->
slice
(
1
,
0
,
seq_len
),
model
->
dev_resources
[
idev
].
handle
);
model
->
dev_resources
[
idev
].
handle
);
}
}
}
}
return
new_kv_cache
;
return
new_kv_cache
;
...
...
src/models/jiuge/jiuge_weight.hpp
View file @
c50a5d09
...
@@ -4,21 +4,21 @@
...
@@ -4,21 +4,21 @@
#include "jiuge_impl.hpp"
#include "jiuge_impl.hpp"
#include <cmath>
#include <cmath>
inline
std
::
shared_ptr
<
Tensor
>
get
_in_e
mbd
(
inline
std
::
shared_ptr
<
Tensor
>
get
InE
mbd
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
)
{
JiugeWeights
const
*
w
)
{
auto
shape
=
std
::
vector
<
size_t
>
({
meta
->
dvoc
,
meta
->
d
});
auto
shape
=
std
::
vector
<
size_t
>
({
meta
->
dvoc
,
meta
->
d
});
return
Tensor
::
weight
((
char
*
)
w
->
input_embd
,
meta
->
dt_logits
,
shape
);
return
Tensor
::
weight
((
char
*
)
w
->
input_embd
,
meta
->
dt_logits
,
shape
);
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_out_n
orm
(
inline
std
::
shared_ptr
<
Tensor
>
get
OutN
orm
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
)
{
JiugeWeights
const
*
w
)
{
auto
shape
=
std
::
vector
<
size_t
>
({
meta
->
d
});
auto
shape
=
std
::
vector
<
size_t
>
({
meta
->
d
});
return
Tensor
::
weight
((
char
*
)
w
->
output_norm
,
meta
->
dt_norm
,
shape
);
return
Tensor
::
weight
((
char
*
)
w
->
output_norm
,
meta
->
dt_norm
,
shape
);
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_out_e
mbd
(
inline
std
::
shared_ptr
<
Tensor
>
get
OutE
mbd
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
)
{
JiugeWeights
const
*
w
)
{
auto
shape
=
std
::
vector
<
size_t
>
({
meta
->
dvoc
,
meta
->
d
});
auto
shape
=
std
::
vector
<
size_t
>
({
meta
->
dvoc
,
meta
->
d
});
...
@@ -26,7 +26,7 @@ inline std::shared_ptr<Tensor> get_out_embd(
...
@@ -26,7 +26,7 @@ inline std::shared_ptr<Tensor> get_out_embd(
->
permute
({
1
,
0
});
->
permute
({
1
,
0
});
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_a
ttn
_n
orm
(
inline
std
::
shared_ptr
<
Tensor
>
get
A
ttn
N
orm
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
JiugeWeights
const
*
w
,
size_t
layer
)
{
size_t
layer
)
{
...
@@ -34,7 +34,7 @@ inline std::shared_ptr<Tensor> get_attn_norm(
...
@@ -34,7 +34,7 @@ inline std::shared_ptr<Tensor> get_attn_norm(
return
Tensor
::
weight
((
char
*
)(
w
->
attn_norm
[
layer
]),
meta
->
dt_norm
,
shape
);
return
Tensor
::
weight
((
char
*
)(
w
->
attn_norm
[
layer
]),
meta
->
dt_norm
,
shape
);
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_a
ttn
_qkv
(
inline
std
::
shared_ptr
<
Tensor
>
get
A
ttn
QKV
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
JiugeWeights
const
*
w
,
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
...
@@ -48,7 +48,7 @@ inline std::shared_ptr<Tensor> get_attn_qkv(
...
@@ -48,7 +48,7 @@ inline std::shared_ptr<Tensor> get_attn_qkv(
->
permute
({
1
,
0
});
->
permute
({
1
,
0
});
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_a
ttn
_qkv_b
ias
(
inline
std
::
shared_ptr
<
Tensor
>
get
A
ttn
QKVB
ias
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
JiugeWeights
const
*
w
,
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
...
@@ -60,9 +60,9 @@ inline std::shared_ptr<Tensor> get_attn_qkv_bias(
...
@@ -60,9 +60,9 @@ inline std::shared_ptr<Tensor> get_attn_qkv_bias(
return
Tensor
::
weight
((
char
*
)(
w
->
attn_qkv_b
[
layer
])
+
offset
,
meta
->
dt_mat
,
shape
);
return
Tensor
::
weight
((
char
*
)(
w
->
attn_qkv_b
[
layer
])
+
offset
,
meta
->
dt_mat
,
shape
);
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_a
ttn
_o
(
JiugeMeta
const
*
meta
,
inline
std
::
shared_ptr
<
Tensor
>
get
A
ttn
O
(
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
size_t
layer
,
JiugeWeights
const
*
w
,
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
size_t
idev
,
size_t
ndev
)
{
auto
nh
=
meta
->
nh
;
auto
nh
=
meta
->
nh
;
auto
dh
=
meta
->
dh
;
auto
dh
=
meta
->
dh
;
auto
d
=
meta
->
d
;
auto
d
=
meta
->
d
;
...
@@ -72,7 +72,7 @@ inline std::shared_ptr<Tensor> get_attn_o(JiugeMeta const *meta,
...
@@ -72,7 +72,7 @@ inline std::shared_ptr<Tensor> get_attn_o(JiugeMeta const *meta,
->
permute
({
1
,
0
});
->
permute
({
1
,
0
});
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_ffn_n
orm
(
inline
std
::
shared_ptr
<
Tensor
>
get
FFNN
orm
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
JiugeWeights
const
*
w
,
size_t
layer
)
{
size_t
layer
)
{
...
@@ -80,7 +80,7 @@ inline std::shared_ptr<Tensor> get_ffn_norm(
...
@@ -80,7 +80,7 @@ inline std::shared_ptr<Tensor> get_ffn_norm(
return
Tensor
::
weight
((
char
*
)(
w
->
ffn_norm
[
layer
]),
meta
->
dt_norm
,
shape
);
return
Tensor
::
weight
((
char
*
)(
w
->
ffn_norm
[
layer
]),
meta
->
dt_norm
,
shape
);
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_ffn_g
ate
_u
p
(
inline
std
::
shared_ptr
<
Tensor
>
get
FFNG
ate
U
p
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
JiugeWeights
const
*
w
,
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
...
@@ -93,7 +93,7 @@ inline std::shared_ptr<Tensor> get_ffn_gate_up(
...
@@ -93,7 +93,7 @@ inline std::shared_ptr<Tensor> get_ffn_gate_up(
->
permute
({
1
,
0
});
->
permute
({
1
,
0
});
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_ffn_d
own
(
inline
std
::
shared_ptr
<
Tensor
>
get
FFND
own
(
JiugeMeta
const
*
meta
,
JiugeMeta
const
*
meta
,
JiugeWeights
const
*
w
,
JiugeWeights
const
*
w
,
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
size_t
layer
,
size_t
idev
,
size_t
ndev
)
{
...
@@ -105,7 +105,7 @@ inline std::shared_ptr<Tensor> get_ffn_down(
...
@@ -105,7 +105,7 @@ inline std::shared_ptr<Tensor> get_ffn_down(
->
permute
({
1
,
0
});
->
permute
({
1
,
0
});
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_sin_t
able
(
JiugeMeta
const
*
meta
)
{
inline
std
::
shared_ptr
<
Tensor
>
get
SinT
able
(
JiugeMeta
const
*
meta
)
{
float
*
table
=
(
float
*
)
std
::
malloc
(
meta
->
dctx
*
meta
->
dh
*
sizeof
(
float
));
float
*
table
=
(
float
*
)
std
::
malloc
(
meta
->
dctx
*
meta
->
dh
*
sizeof
(
float
));
auto
half_dh
=
meta
->
dh
/
2
;
auto
half_dh
=
meta
->
dh
/
2
;
for
(
size_t
i
=
0
;
i
<
meta
->
dctx
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
meta
->
dctx
;
i
++
)
{
...
@@ -122,7 +122,7 @@ inline std::shared_ptr<Tensor> get_sin_table(JiugeMeta const *meta) {
...
@@ -122,7 +122,7 @@ inline std::shared_ptr<Tensor> get_sin_table(JiugeMeta const *meta) {
return
tensor
;
return
tensor
;
}
}
inline
std
::
shared_ptr
<
Tensor
>
get
_cos_t
able
(
JiugeMeta
const
*
meta
)
{
inline
std
::
shared_ptr
<
Tensor
>
get
CosT
able
(
JiugeMeta
const
*
meta
)
{
float
*
table
=
(
float
*
)
std
::
malloc
(
meta
->
dctx
*
meta
->
dh
*
sizeof
(
float
));
float
*
table
=
(
float
*
)
std
::
malloc
(
meta
->
dctx
*
meta
->
dh
*
sizeof
(
float
));
auto
half_dh
=
meta
->
dh
/
2
;
auto
half_dh
=
meta
->
dh
/
2
;
for
(
size_t
i
=
0
;
i
<
meta
->
dctx
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
meta
->
dctx
;
i
++
)
{
...
...
src/tensor.hpp
View file @
c50a5d09
...
@@ -48,9 +48,9 @@ private:
...
@@ -48,9 +48,9 @@ private:
std
::
shared_ptr
<
Storage
>
storage
;
std
::
shared_ptr
<
Storage
>
storage
;
infiniopTensorDescriptor_t
_desc
;
infiniopTensorDescriptor_t
_desc
;
void
*
data
_i
mpl
(
ptrdiff_t
offset
)
const
;
void
*
data
I
mpl
(
ptrdiff_t
offset
)
const
;
std
::
shared_ptr
<
Tensor
>
std
::
shared_ptr
<
Tensor
>
slice
_i
mpl
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
;
slice
I
mpl
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
;
public:
public:
static
std
::
shared_ptr
<
Tensor
>
buffer
(
infiniDtype_t
dtype
,
static
std
::
shared_ptr
<
Tensor
>
buffer
(
infiniDtype_t
dtype
,
...
@@ -65,23 +65,23 @@ public:
...
@@ -65,23 +65,23 @@ public:
std
::
shared_ptr
<
Tensor
>
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
);
std
::
shared_ptr
<
Tensor
>
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
);
std
::
shared_ptr
<
Tensor
const
>
std
::
shared_ptr
<
Tensor
const
>
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
;
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
;
std
::
shared_ptr
<
Tensor
>
dim
_m
erge
(
size_t
dim_start
,
size_t
dim_end
);
std
::
shared_ptr
<
Tensor
>
dim
M
erge
(
size_t
dim_start
,
size_t
dim_end
);
std
::
shared_ptr
<
Tensor
>
dim
_s
plit
(
size_t
dim
,
std
::
shared_ptr
<
Tensor
>
dim
S
plit
(
size_t
dim
,
const
std
::
vector
<
size_t
>
&
dims
);
const
std
::
vector
<
size_t
>
&
dims
);
std
::
shared_ptr
<
Tensor
>
permute
(
const
std
::
vector
<
size_t
>
&
order
);
std
::
shared_ptr
<
Tensor
>
permute
(
const
std
::
vector
<
size_t
>
&
order
);
void
*
data
(
ptrdiff_t
offset
=
0
);
void
*
data
(
ptrdiff_t
offset
=
0
);
void
const
*
data
(
ptrdiff_t
offset
=
0
)
const
;
void
const
*
data
(
ptrdiff_t
offset
=
0
)
const
;
void
copy
_f
rom
(
std
::
shared_ptr
<
Tensor
const
>
src
,
infiniopHandle_t
handle
,
void
copy
F
rom
(
std
::
shared_ptr
<
Tensor
const
>
src
,
infiniopHandle_t
handle
,
infinirtStream_t
stream
=
nullptr
);
infinirtStream_t
stream
=
nullptr
);
const
std
::
vector
<
size_t
>
&
shape
()
const
;
const
std
::
vector
<
size_t
>
&
shape
()
const
;
const
std
::
vector
<
ptrdiff_t
>
&
strides
()
const
;
const
std
::
vector
<
ptrdiff_t
>
&
strides
()
const
;
size_t
ndim
()
const
;
size_t
ndim
()
const
;
infiniDtype_t
dtype
()
const
;
infiniDtype_t
dtype
()
const
;
std
::
shared_ptr
<
TensorDesc
>
desc
()
const
;
std
::
shared_ptr
<
TensorDesc
>
desc
()
const
;
size_t
byte
_s
ize
()
const
;
size_t
byte
S
ize
()
const
;
ptrdiff_t
data
_o
ffset
()
const
;
ptrdiff_t
data
O
ffset
()
const
;
infiniDevice_t
device
_t
ype
()
const
;
infiniDevice_t
device
T
ype
()
const
;
int
device
_i
d
()
const
;
int
device
I
d
()
const
;
bool
is_contigous
()
const
;
bool
is_contigous
()
const
;
void
debug
(
const
std
::
string
&
filename
)
const
;
void
debug
(
const
std
::
string
&
filename
)
const
;
...
...
src/tensor/tensor.cpp
View file @
c50a5d09
...
@@ -21,12 +21,12 @@ const std::vector<size_t> &Tensor::shape() const { return this->_shape; }
...
@@ -21,12 +21,12 @@ const std::vector<size_t> &Tensor::shape() const { return this->_shape; }
const
std
::
vector
<
ptrdiff_t
>
&
Tensor
::
strides
()
const
{
return
this
->
_strides
;
}
const
std
::
vector
<
ptrdiff_t
>
&
Tensor
::
strides
()
const
{
return
this
->
_strides
;
}
size_t
Tensor
::
ndim
()
const
{
return
this
->
_shape
.
size
();
}
size_t
Tensor
::
ndim
()
const
{
return
this
->
_shape
.
size
();
}
infiniDtype_t
Tensor
::
dtype
()
const
{
return
this
->
_dtype
;
}
infiniDtype_t
Tensor
::
dtype
()
const
{
return
this
->
_dtype
;
}
size_t
Tensor
::
byte
_s
ize
()
const
{
return
this
->
_size
;
}
size_t
Tensor
::
byte
S
ize
()
const
{
return
this
->
_size
;
}
infiniDevice_t
Tensor
::
device
_t
ype
()
const
{
return
this
->
storage
->
device_type
;
}
infiniDevice_t
Tensor
::
device
T
ype
()
const
{
return
this
->
storage
->
device_type
;
}
int
Tensor
::
device
_i
d
()
const
{
return
this
->
storage
->
device_id
;
}
int
Tensor
::
device
I
d
()
const
{
return
this
->
storage
->
device_id
;
}
Tensor
::~
Tensor
()
{}
Tensor
::~
Tensor
()
{}
ptrdiff_t
Tensor
::
data
_o
ffset
()
const
{
ptrdiff_t
Tensor
::
data
O
ffset
()
const
{
return
(
char
*
)(
this
->
_data
)
-
(
char
*
)(
this
->
storage
->
memory
);
return
(
char
*
)(
this
->
_data
)
-
(
char
*
)(
this
->
storage
->
memory
);
}
}
...
@@ -90,22 +90,22 @@ std::shared_ptr<Tensor> Tensor::weight(void *data, infiniDtype_t dtype,
...
@@ -90,22 +90,22 @@ std::shared_ptr<Tensor> Tensor::weight(void *data, infiniDtype_t dtype,
return
tensor
;
return
tensor
;
}
}
void
*
Tensor
::
data
_i
mpl
(
ptrdiff_t
offset
)
const
{
void
*
Tensor
::
data
I
mpl
(
ptrdiff_t
offset
)
const
{
ASSERT
(
offset
*
dsize
(
this
->
dtype
())
<
this
->
_size
);
ASSERT
(
offset
*
dsize
(
this
->
dtype
())
<
this
->
_size
);
return
(
char
*
)(
this
->
_data
)
+
offset
*
dsize
(
this
->
dtype
());
return
(
char
*
)(
this
->
_data
)
+
offset
*
dsize
(
this
->
dtype
());
}
}
void
*
Tensor
::
data
(
ptrdiff_t
offset
)
{
void
*
Tensor
::
data
(
ptrdiff_t
offset
)
{
return
this
->
data
_i
mpl
(
offset
);
return
this
->
data
I
mpl
(
offset
);
}
}
const
void
*
Tensor
::
data
(
ptrdiff_t
offset
)
const
{
const
void
*
Tensor
::
data
(
ptrdiff_t
offset
)
const
{
return
this
->
data
_i
mpl
(
offset
);
return
this
->
data
I
mpl
(
offset
);
}
}
void
Tensor
::
copy
_f
rom
(
std
::
shared_ptr
<
Tensor
const
>
src
,
void
Tensor
::
copy
F
rom
(
std
::
shared_ptr
<
Tensor
const
>
src
,
infiniopHandle_t
handle
,
infinirtStream_t
stream
)
{
infiniopHandle_t
handle
,
infinirtStream_t
stream
)
{
ASSERT_EQ
(
this
->
shape
(),
src
->
shape
());
ASSERT_EQ
(
this
->
shape
(),
src
->
shape
());
ASSERT_EQ
(
this
->
dtype
(),
src
->
dtype
());
ASSERT_EQ
(
this
->
dtype
(),
src
->
dtype
());
infiniopRearrangeDescriptor_t
desc
;
infiniopRearrangeDescriptor_t
desc
;
...
@@ -172,11 +172,11 @@ void Tensor::debug(const std::string &filename) const {
...
@@ -172,11 +172,11 @@ void Tensor::debug(const std::string &filename) const {
std
::
cout
<<
s
<<
" "
;
std
::
cout
<<
s
<<
" "
;
}
}
std
::
cout
<<
"] dtype="
<<
this
->
dtype
()
std
::
cout
<<
"] dtype="
<<
this
->
dtype
()
<<
" device="
<<
this
->
device
_t
ype
()
<<
" device="
<<
this
->
device
T
ype
()
<<
" device_id="
<<
this
->
device
_i
d
()
<<
std
::
endl
;
<<
" device_id="
<<
this
->
device
I
d
()
<<
std
::
endl
;
auto
dtype
=
this
->
dtype
();
auto
dtype
=
this
->
dtype
();
void
const
*
cpu_data
;
void
const
*
cpu_data
;
if
(
this
->
device
_t
ype
()
!=
INFINI_DEVICE_CPU
)
{
if
(
this
->
device
T
ype
()
!=
INFINI_DEVICE_CPU
)
{
void
*
cpu_memory
=
std
::
malloc
(
this
->
storage
->
size
);
void
*
cpu_memory
=
std
::
malloc
(
this
->
storage
->
size
);
RUN_INFINI
(
infinirtMemcpy
(
cpu_memory
,
this
->
storage
->
memory
,
RUN_INFINI
(
infinirtMemcpy
(
cpu_memory
,
this
->
storage
->
memory
,
this
->
storage
->
size
,
INFINIRT_MEMCPY_D2H
));
this
->
storage
->
size
,
INFINIRT_MEMCPY_D2H
));
...
@@ -199,27 +199,27 @@ void Tensor::debug(const std::string &filename) const {
...
@@ -199,27 +199,27 @@ void Tensor::debug(const std::string &filename) const {
switch
(
dtype
)
{
switch
(
dtype
)
{
case
INFINI_DTYPE_F16
:
case
INFINI_DTYPE_F16
:
print_data
((
uint16_t
const
*
)((
char
const
*
)
cpu_data
+
data
_o
ffset
()),
print_data
((
uint16_t
const
*
)((
char
const
*
)
cpu_data
+
data
O
ffset
()),
this
->
shape
(),
this
->
strides
(),
0
);
this
->
shape
(),
this
->
strides
(),
0
);
break
;
break
;
case
INFINI_DTYPE_F32
:
case
INFINI_DTYPE_F32
:
print_data
((
float
const
*
)((
char
const
*
)
cpu_data
+
data
_o
ffset
()),
print_data
((
float
const
*
)((
char
const
*
)
cpu_data
+
data
O
ffset
()),
this
->
shape
(),
this
->
strides
(),
0
);
this
->
shape
(),
this
->
strides
(),
0
);
break
;
break
;
case
INFINI_DTYPE_U64
:
case
INFINI_DTYPE_U64
:
print_data
((
uint64_t
const
*
)((
char
const
*
)
cpu_data
+
data
_o
ffset
()),
print_data
((
uint64_t
const
*
)((
char
const
*
)
cpu_data
+
data
O
ffset
()),
this
->
shape
(),
this
->
strides
(),
0
);
this
->
shape
(),
this
->
strides
(),
0
);
break
;
break
;
case
INFINI_DTYPE_I64
:
case
INFINI_DTYPE_I64
:
print_data
((
int64_t
const
*
)((
char
const
*
)
cpu_data
+
data
_o
ffset
()),
print_data
((
int64_t
const
*
)((
char
const
*
)
cpu_data
+
data
O
ffset
()),
this
->
shape
(),
this
->
strides
(),
0
);
this
->
shape
(),
this
->
strides
(),
0
);
break
;
break
;
case
INFINI_DTYPE_U32
:
case
INFINI_DTYPE_U32
:
print_data
((
uint32_t
const
*
)((
char
const
*
)
cpu_data
+
data
_o
ffset
()),
print_data
((
uint32_t
const
*
)((
char
const
*
)
cpu_data
+
data
O
ffset
()),
this
->
shape
(),
this
->
strides
(),
0
);
this
->
shape
(),
this
->
strides
(),
0
);
break
;
break
;
case
INFINI_DTYPE_I32
:
case
INFINI_DTYPE_I32
:
print_data
((
int32_t
const
*
)((
char
const
*
)
cpu_data
+
data
_o
ffset
()),
print_data
((
int32_t
const
*
)((
char
const
*
)
cpu_data
+
data
O
ffset
()),
this
->
shape
(),
this
->
strides
(),
0
);
this
->
shape
(),
this
->
strides
(),
0
);
break
;
break
;
default:
default:
...
...
src/tensor/transform.cpp
View file @
c50a5d09
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
#include <numeric>
#include <numeric>
#include <vector>
#include <vector>
std
::
shared_ptr
<
Tensor
>
Tensor
::
slice
_i
mpl
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
slice
I
mpl
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
{
std
::
shared_ptr
<
Tensor
>
tensor
=
std
::
make_shared
<
Tensor
>
();
std
::
shared_ptr
<
Tensor
>
tensor
=
std
::
make_shared
<
Tensor
>
();
auto
new_shape
=
std
::
vector
<
size_t
>
(
this
->
_shape
);
auto
new_shape
=
std
::
vector
<
size_t
>
(
this
->
_shape
);
...
@@ -32,22 +32,22 @@ std::shared_ptr<Tensor> Tensor::slice_impl(const std::vector<SliceParams> &slice
...
@@ -32,22 +32,22 @@ std::shared_ptr<Tensor> Tensor::slice_impl(const std::vector<SliceParams> &slice
}
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
slice
(
size_t
dim
,
size_t
start
,
size_t
len
)
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
slice
(
size_t
dim
,
size_t
start
,
size_t
len
)
{
return
this
->
slice
_i
mpl
({{
dim
,
start
,
len
}});
return
this
->
slice
I
mpl
({{
dim
,
start
,
len
}});
}
}
std
::
shared_ptr
<
Tensor
const
>
Tensor
::
slice
(
size_t
dim
,
size_t
start
,
size_t
len
)
const
{
std
::
shared_ptr
<
Tensor
const
>
Tensor
::
slice
(
size_t
dim
,
size_t
start
,
size_t
len
)
const
{
return
this
->
slice
_i
mpl
({{
dim
,
start
,
len
}});
return
this
->
slice
I
mpl
({{
dim
,
start
,
len
}});
}
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
)
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
)
{
return
this
->
slice
_i
mpl
(
slices
);
return
this
->
slice
I
mpl
(
slices
);
}
}
std
::
shared_ptr
<
Tensor
const
>
Tensor
::
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
{
std
::
shared_ptr
<
Tensor
const
>
Tensor
::
slice
(
const
std
::
vector
<
SliceParams
>
&
slices
)
const
{
return
this
->
slice
_i
mpl
(
slices
);
return
this
->
slice
I
mpl
(
slices
);
}
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
dim
_m
erge
(
size_t
dim_start
,
size_t
dim_end
)
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
dim
M
erge
(
size_t
dim_start
,
size_t
dim_end
)
{
ASSERT
(
dim_start
<=
dim_end
&&
dim_end
<
this
->
_shape
.
size
());
ASSERT
(
dim_start
<=
dim_end
&&
dim_end
<
this
->
_shape
.
size
());
if
(
dim_start
==
dim_end
)
{
if
(
dim_start
==
dim_end
)
{
return
shared_from_this
();
return
shared_from_this
();
...
@@ -77,7 +77,7 @@ std::shared_ptr<Tensor> Tensor::dim_merge(size_t dim_start, size_t dim_end) {
...
@@ -77,7 +77,7 @@ std::shared_ptr<Tensor> Tensor::dim_merge(size_t dim_start, size_t dim_end) {
return
shared_from_this
();
return
shared_from_this
();
}
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
dim
_s
plit
(
size_t
dim
,
const
std
::
vector
<
size_t
>
&
dims
)
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
dim
S
plit
(
size_t
dim
,
const
std
::
vector
<
size_t
>
&
dims
)
{
ASSERT_EQ
(
this
->
_shape
[
dim
],
std
::
accumulate
(
dims
.
begin
(),
dims
.
end
(),
size_t
(
1
),
std
::
multiplies
<
size_t
>
()));
ASSERT_EQ
(
this
->
_shape
[
dim
],
std
::
accumulate
(
dims
.
begin
(),
dims
.
end
(),
size_t
(
1
),
std
::
multiplies
<
size_t
>
()));
auto
new_shape
=
std
::
vector
<
size_t
>
();
auto
new_shape
=
std
::
vector
<
size_t
>
();
auto
new_strides
=
std
::
vector
<
ptrdiff_t
>
();
auto
new_strides
=
std
::
vector
<
ptrdiff_t
>
();
...
...
src/utils.hpp
View file @
c50a5d09
...
@@ -6,16 +6,16 @@
...
@@ -6,16 +6,16 @@
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
inline
void
assert
_t
rue
(
int
expr
,
const
char
*
msg
,
const
char
*
file
,
int
line
)
{
inline
void
assert
T
rue
(
int
expr
,
const
char
*
msg
,
const
char
*
file
,
int
line
)
{
if
(
!
expr
)
{
if
(
!
expr
)
{
fprintf
(
stderr
,
"
\033
[31mAssertion failed:
\033
[0m %s at file %s, line %d
\n
"
,
msg
,
file
,
line
);
fprintf
(
stderr
,
"
\033
[31mAssertion failed:
\033
[0m %s at file %s, line %d
\n
"
,
msg
,
file
,
line
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
}
}
}
}
#define ASSERT(expr) assert
_t
rue((expr), #expr " is false", __FILE__, __LINE__)
#define ASSERT(expr) assert
T
rue((expr), #expr " is false", __FILE__, __LINE__)
#define ASSERT_EQ(a, b) assert
_t
rue((a) == (b), #a " != " #b, __FILE__, __LINE__)
#define ASSERT_EQ(a, b) assert
T
rue((a) == (b), #a " != " #b, __FILE__, __LINE__)
#define ASSERT_VALID_PTR(a) assert
_t
rue((a) != nullptr, #a " is nullptr", __FILE__, __LINE__)
#define ASSERT_VALID_PTR(a) assert
T
rue((a) != nullptr, #a " is nullptr", __FILE__, __LINE__)
#define PANIC(EXPR) \
#define PANIC(EXPR) \
printf("Error at %s:%d - %s\n", __FILE__, __LINE__, #EXPR); \
printf("Error at %s:%d - %s\n", __FILE__, __LINE__, #EXPR); \
...
...
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