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
b3275d7c
You need to sign in or sign up before continuing.
Commit
b3275d7c
authored
Aug 08, 2025
by
wooway777
Browse files
issue/21 - Slightly improved key creation process
parent
e641693d
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
33 additions
and
31 deletions
+33
-31
src/models/cache_manager.hpp
src/models/cache_manager.hpp
+1
-25
src/tensor.hpp
src/tensor.hpp
+6
-2
src/tensor/tensor.cpp
src/tensor/tensor.cpp
+18
-4
src/tensor/transform.cpp
src/tensor/transform.cpp
+3
-0
src/utils.hpp
src/utils.hpp
+5
-0
No files found.
src/models/cache_manager.hpp
View file @
b3275d7c
...
...
@@ -10,30 +10,6 @@
#include "../utils.hpp"
#include "infinicore_infer.h"
// Hash combine utility (similar to boost::hash_combine)
inline
void
hash_combine
(
size_t
&
seed
,
size_t
value
)
{
seed
^=
value
+
0x9e3779b9
+
(
seed
<<
6
)
+
(
seed
>>
2
);
}
// Specialization for enum types
template
<
typename
T
>
inline
void
hash_combine
(
size_t
&
seed
,
T
value
,
typename
std
::
enable_if
<
std
::
is_enum
<
T
>::
value
>::
type
*
=
0
)
{
hash_combine
(
seed
,
static_cast
<
size_t
>
(
value
));
}
// Helper function to compute hash for tensor descriptors
inline
size_t
computeTensorDescHash
(
std
::
shared_ptr
<
Tensor
>
&
tensor
)
{
size_t
seed
=
0
;
hash_combine
(
seed
,
tensor
->
dtype
());
for
(
auto
dim
:
tensor
->
shape
())
{
hash_combine
(
seed
,
dim
);
}
for
(
auto
stride
:
tensor
->
strides
())
{
hash_combine
(
seed
,
static_cast
<
size_t
>
(
stride
));
}
return
seed
;
}
class
IDescriptorDestroyer
{
public:
virtual
~
IDescriptorDestroyer
()
=
default
;
...
...
@@ -260,7 +236,7 @@ public:
template
<
typename
...
Tensors
>
static
size_t
createDescriptorKey
(
Tensors
...
tensors
)
{
size_t
seed
=
0
;
(...,
(
tensors
?
hash_combine
(
seed
,
computeTensorDescHash
(
tensors
))
:
(
void
)
0
));
(...,
(
tensors
?
hash_combine
(
seed
,
tensors
->
seed
(
))
:
(
void
)
0
));
return
seed
;
}
};
...
...
src/tensor.hpp
View file @
b3275d7c
...
...
@@ -51,10 +51,12 @@ private:
std
::
vector
<
size_t
>
_shape
;
std
::
vector
<
ptrdiff_t
>
_strides
;
infiniopTensorDescriptor_t
_desc
;
size_t
_seed
;
TensorDesc
(
infiniDtype_t
dtype
,
const
std
::
vector
<
size_t
>
&
shape
,
const
std
::
vector
<
ptrdiff_t
>
&
strides
)
:
_dtype
(
dtype
),
_shape
(
shape
),
_strides
(
strides
),
_desc
(
nullptr
)
{}
const
std
::
vector
<
ptrdiff_t
>
&
strides
)
:
_dtype
(
dtype
),
_shape
(
shape
),
_strides
(
strides
),
_desc
(
nullptr
)
{
computeTensorDesHash
();
}
void
resetDesc
();
void
computeTensorDesHash
();
public:
~
TensorDesc
();
...
...
@@ -74,6 +76,7 @@ public:
infiniopTensorDescriptor_t
desc
()
const
;
bool
isContigous
()
const
;
std
::
string
info
()
const
;
size_t
seed
()
const
{
return
_seed
;
}
void
dimMerge
(
size_t
dim_start
,
size_t
dim_end
);
void
dimSplit
(
size_t
dim
,
const
std
::
vector
<
size_t
>
&
dims
);
...
...
@@ -127,10 +130,11 @@ public:
void
debug
(
const
std
::
string
&
filename
)
const
;
void
debug
()
const
;
std
::
string
info
()
const
;
size_t
seed
()
const
;
std
::
shared_ptr
<
Tensor
>
view
(
const
std
::
vector
<
size_t
>
&
new_shape
)
const
;
std
::
shared_ptr
<
Tensor
>
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
,
const
std
::
vector
<
ptrdiff_t
>
&
new_strides
)
const
;
std
::
shared_ptr
<
Tensor
>
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
)
const
;
std
::
shared_ptr
<
Tensor
>
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
,
const
std
::
vector
<
ptrdiff_t
>
&
new_strides
)
const
;
~
Tensor
();
};
...
...
src/tensor/tensor.cpp
View file @
b3275d7c
...
...
@@ -62,6 +62,16 @@ void TensorDesc::resetDesc() {
}
}
void
TensorDesc
::
computeTensorDesHash
()
{
_seed
=
0
;
for
(
auto
dim
:
this
->
shape
())
{
hash_combine
(
_seed
,
dim
);
}
for
(
auto
stride
:
this
->
strides
())
{
hash_combine
(
_seed
,
static_cast
<
size_t
>
(
stride
));
}
}
bool
TensorDesc
::
isContigous
()
const
{
auto
ndim
=
this
->
ndim
();
auto
shape
=
this
->
shape
();
...
...
@@ -258,6 +268,10 @@ std::string Tensor::info() const {
return
this
->
_desc
->
info
();
}
size_t
Tensor
::
seed
()
const
{
return
this
->
_desc
->
seed
();
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
view
(
const
std
::
vector
<
size_t
>
&
new_shape
)
const
{
// Calculate total number of elements
size_t
numel
=
1
;
...
...
@@ -383,18 +397,18 @@ std::shared_ptr<Tensor> Tensor::view(const std::vector<size_t> &new_shape) const
return
this
->
view_as
(
inferred_shape
,
new_strides
);
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
,
const
std
::
vector
<
ptrdiff_t
>
&
new_strides
)
const
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
)
const
{
std
::
shared_ptr
<
Tensor
>
tensor
=
std
::
make_shared
<
Tensor
>
();
tensor
->
_storage
=
this
->
_storage
;
tensor
->
_desc
=
TensorDesc
::
create
(
this
->
dtype
(),
new_shape
,
new_strides
);
tensor
->
_desc
=
TensorDesc
::
create
(
this
->
dtype
(),
new_shape
);
tensor
->
_offset
=
this
->
_offset
;
return
tensor
;
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
)
const
{
std
::
shared_ptr
<
Tensor
>
Tensor
::
view_as
(
const
std
::
vector
<
size_t
>
&
new_shape
,
const
std
::
vector
<
ptrdiff_t
>
&
new_strides
)
const
{
std
::
shared_ptr
<
Tensor
>
tensor
=
std
::
make_shared
<
Tensor
>
();
tensor
->
_storage
=
this
->
_storage
;
tensor
->
_desc
=
TensorDesc
::
create
(
this
->
dtype
(),
new_shape
);
tensor
->
_desc
=
TensorDesc
::
create
(
this
->
dtype
(),
new_shape
,
new_strides
);
tensor
->
_offset
=
this
->
_offset
;
return
tensor
;
}
...
...
src/tensor/transform.cpp
View file @
b3275d7c
...
...
@@ -63,6 +63,7 @@ void TensorDesc::dimMerge(size_t dim_start, size_t dim_end) {
this
->
_shape
=
new_shape
;
this
->
_strides
=
new_strides
;
this
->
resetDesc
();
this
->
computeTensorDesHash
();
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
dimMerge
(
size_t
dim_start
,
size_t
dim_end
)
{
...
...
@@ -89,6 +90,7 @@ void TensorDesc::dimSplit(size_t dim, const std::vector<size_t> &dims) {
this
->
_shape
=
new_shape
;
this
->
_strides
=
new_strides
;
this
->
resetDesc
();
this
->
computeTensorDesHash
();
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
dimSplit
(
size_t
dim
,
const
std
::
vector
<
size_t
>
&
dims
)
{
...
...
@@ -108,6 +110,7 @@ void TensorDesc::permute(const std::vector<size_t> &order) {
this
->
_shape
=
new_shape
;
this
->
_strides
=
new_strides
;
this
->
resetDesc
();
this
->
computeTensorDesHash
();
}
std
::
shared_ptr
<
Tensor
>
Tensor
::
permute
(
const
std
::
vector
<
size_t
>
&
order
)
{
...
...
src/utils.hpp
View file @
b3275d7c
...
...
@@ -119,4 +119,9 @@ inline uint16_t f32_to_bf16(float val) {
return
bf16_bits
;
}
// Hash combine utility (similar to boost::hash_combine)
inline
void
hash_combine
(
size_t
&
seed
,
size_t
value
)
{
seed
^=
value
+
0x9e3779b9
+
(
seed
<<
6
)
+
(
seed
>>
2
);
}
#endif
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