stdlib: Add SparseHist to PyStats

This is inclusive of tests to ensure they have implemented correctly.

Change-Id: I5c84d5ffdb7b914936cfd86ca012a7b141eeaf42
This commit is contained in:
Bobby R. Bruce
2024-03-27 10:06:16 -07:00
parent b5e8804cd4
commit 178679cbfd
9 changed files with 251 additions and 0 deletions

View File

@@ -253,3 +253,31 @@ class Distribution(Vector):
# These check some basic conditions of a distribution.
assert self.bin_size >= 0
assert self.num_bins >= 1
class SparseHist(Vector):
"""A Sparse Histogram of values. A sparse histogram simply counts the "
frequency of each value in a sample. Ergo, it is, ineffect an disctionary
of values mapped to their count"""
def __init__(
self,
value: Dict[float, Scalar],
description: Optional[str] = None,
):
super().__init__(
value=value,
type="SparseHist",
description=description,
)
def size(self) -> int:
"""The number of unique sampled values."""
return len(self.value)
def count(self) -> int:
"""
Returns the total number of samples.
"""
assert self.value != None
return sum(self.value.values())

View File

@@ -140,6 +140,8 @@ def __get_statistic(statistic: _m5.stats.Info) -> Optional[Statistic]:
return __get_vector(statistic)
elif isinstance(statistic, _m5.stats.Vector2dInfo):
return __get_vector2d(statistic)
elif isinstance(statistic, _m5.stats.SparseHistInfo):
return __get_sparse_hist(statistic)
return None
@@ -268,6 +270,24 @@ def __get_vector2d(statistic: _m5.stats.Vector2dInfo) -> Vector2d:
return Vector2d(value=vector_rep, type="Vector2d", description=description)
def __get_sparse_hist(statistic: _m5.stats.SparseHistInfo) -> SparseHist:
description = statistic.desc
value = statistic.values
parsed_values = {}
for val in value:
parsed_values[val] = Scalar(
value=value[val],
unit=statistic.unit,
datatype=StorageType["f64"],
)
return SparseHist(
value=parsed_values,
description=description,
)
def _prepare_stats(group: _m5.stats.Group):
"""
Prepares the statistics for dumping.

View File

@@ -78,6 +78,7 @@ cast_stat_info(const statistics::Info *info)
TRY_CAST(statistics::VectorInfo);
TRY_CAST(statistics::Vector2dInfo);
TRY_CAST(statistics::DistInfo);
TRY_CAST(statistics::SparseHistInfo);
return py::cast(info);
@@ -195,6 +196,15 @@ pybind_init_stats(py::module_ &m_native)
.def_readonly("value", &statistics::Vector2dInfo::cvec)
;
py::class_<statistics::SparseHistInfo, statistics::Info,
std::unique_ptr<statistics::SparseHistInfo, py::nodelete>>(
m, "SparseHistInfo")
.def_property_readonly("values", //A Dict[float, int] of sample & count
[](const statistics::SparseHistInfo &info) {
return info.data.cmap;
})
;
py::class_<statistics::FormulaInfo, statistics::VectorInfo,
std::unique_ptr<statistics::FormulaInfo, py::nodelete>>(
m, "FormulaInfo")

View File

@@ -32,5 +32,6 @@ if env['CONF']['USE_TEST_OBJECTS']:
'ScalarStatTester',
'VectorStatTester',
'Vector2dStatTester',
'SparseHistStatTester',
])
Source('stat_tester.cc')

View File

@@ -87,3 +87,14 @@ class Vector2dStatTester(StatTester):
[],
"The vector stat's y subdescriptions. If empty, the subdescriptions ",
)
class SparseHistStatTester(StatTester):
type = "SparseHistStatTester"
cxx_header = "test_objects/stat_tester.hh"
cxx_class = "gem5::SparseHistStatTester"
samples = VectorParam.Float(
"The sparse histogram's sampled values, to be inserted into the "
"histogram."
)

View File

@@ -28,6 +28,8 @@
#include "test_objects/stat_tester.hh"
#include <set>
#include "base/stats/group.hh"
namespace gem5
@@ -133,4 +135,28 @@ Vector2dStatTester::Vector2dStatTesterStats::Vector2dStatTesterStats(
}
void
SparseHistStatTester::setStats()
{
for (auto sample : params.samples) {
stats.sparse_histogram.sample(sample);
}
}
SparseHistStatTester::SparseHistStatTesterStats::SparseHistStatTesterStats(
statistics::Group *parent,
const SparseHistStatTesterParams &params
) : statistics::Group(parent),
sparse_histogram(
this,
params.name.c_str(),
statistics::units::Count::get(),
params.description.c_str()
)
{
sparse_histogram.init(
(std::set(params.samples.begin(), params.samples.end())).size()
);
}
} // namespace gem5

View File

@@ -31,6 +31,7 @@
#include "base/statistics.hh"
#include "params/ScalarStatTester.hh"
#include "params/SparseHistStatTester.hh"
#include "params/StatTester.hh"
#include "params/Vector2dStatTester.hh"
#include "params/VectorStatTester.hh"
@@ -158,6 +159,27 @@ class Vector2dStatTester : public StatTester
} stats;
};
class SparseHistStatTester : public StatTester
{
private:
SparseHistStatTesterParams params;
public:
SparseHistStatTester(const SparseHistStatTesterParams &p) :
StatTester(p), params(p), stats(this, p) {}
protected:
void setStats() override;
struct SparseHistStatTesterStats : public statistics::Group
{
SparseHistStatTesterStats(
statistics::Group *parent,
const SparseHistStatTesterParams &params
);
statistics::SparseHistogram sparse_histogram;
} stats;
};
} // namespace gem5