{
virtual ~PlainWeightContext() = default;
+ /**
+ * @brief Allocate a weight space for a given blob
+ *
+ * @require the following code SHOULD work for any bag "b":
+ * PlainWeightContext<T> ctx;
+ *
+ * ctx.allocate(b);
+ * auto span = ctx.weight(b);
+ * assert(span.data() != nullptr);
+ * assert(span.size() == bag->size());
+ */
+ virtual void allocate(const Bag *) = 0;
+ // WARN Depercated
virtual void allocate(const KernelObject *) = 0;
/**
PlainWeightContextImpl(PlainWeightContextImpl &&) = delete;
public:
+ void allocate(const coco::Bag *bag) override
+ {
+ assert(bag != nullptr);
+ _blob->allocate(bag, sizeof(T));
+ }
+
void allocate(const coco::KernelObject *obj)
{
assert(obj != nullptr);
ASSERT_EQ(mutable_ptr->f32(), immutable_ptr->f32());
}
+TEST(IR_DATA, allocate_and_link_bag)
+{
+ auto m = coco::Module::create();
+ auto d = coco::Data::create();
+
+ // Create a bag
+ auto bag = m->entity()->bag()->create(9);
+
+ // weight(...) SHOULD return a null-span for an invalid bag
+ {
+ auto span = d->f32()->weight(bag);
+
+ ASSERT_EQ(span.data(), nullptr);
+ ASSERT_EQ(span.size(), 0);
+ }
+
+ // Allocate a weight space
+ d->f32()->allocate(bag);
+
+ // weight(...) SHOULD return a valid for a valid bag
+ {
+ auto span = d->f32()->weight(bag);
+
+ ASSERT_NE(span.data(), nullptr);
+ ASSERT_EQ(span.size(), bag->size());
+ }
+}
+
TEST(IR_DATA, allocate_and_link_kernel)
{
using nncc::core::ADT::kernel::num_elements;