pulsatrix 1.1.0
Loading...
Searching...
No Matches
conv2d_module.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstddef>
8#include <optional>
9#include <initializer_list>
10#include <vector>
11
12#include "pulsatrix/module.hpp"
13
14namespace pulsatrix {
15
31class Conv2DModule : public Module {
32public:
46 Conv2DModule(int64_t in_channels, int64_t out_channels, int64_t kernel_h, int64_t kernel_w,
47 DeviceBackend* backend, int64_t stride = 1, int64_t padding = 0);
48
50 [[nodiscard]] int64_t stride() const { return stride_; }
52 [[nodiscard]] int64_t padding() const { return padding_; }
53
64 [[nodiscard]] Tensor backward(const Tensor& grad_output) override;
65
67 [[nodiscard]] OpType op_type() const override { return OpType::Conv; }
68
70 void set_kernel(std::initializer_list<float> values);
71
73 void set_bias(std::initializer_list<float> values);
74
76 void set_kernel(const std::vector<float>& values);
77
79 void set_bias(const std::vector<float>& values);
80
81 [[nodiscard]] const Tensor& kernel() const { return kernel_; }
82 [[nodiscard]] const Tensor& bias() const { return bias_; }
83 [[nodiscard]] const Tensor& kernel_grad() const { return kernel_grad_; }
84 [[nodiscard]] const Tensor& bias_grad() const { return bias_grad_; }
85
110 [[nodiscard]] Tensor propagate_relevance(const Tensor& relevance_out, const LRPRuleConfig& config) override;
111
113 [[nodiscard]] bool supports_lrp_rule(LRPRule) const override { return true; }
114
115 [[nodiscard]] std::vector<NamedParamRef> named_parameters() override {
116 return {{"weight", {&kernel_, &kernel_grad_}}, {"bias", {&bias_, &bias_grad_}}};
117 }
118
119
121 [[nodiscard]] std::optional<DeviceType> compute_device() const override { return backend_->device(); }
122
131 void set_max_workspace_bytes(size_t bytes) { max_workspace_bytes_ = bytes; }
132 [[nodiscard]] size_t max_workspace_bytes() const { return max_workspace_bytes_; }
139 static constexpr size_t kDefaultMaxWorkspaceBytes = size_t{16} << 20;
140
141protected:
150 [[nodiscard]] Tensor forward_impl(const Tensor& input) override;
151
152private:
153 int64_t in_channels_;
154 int64_t out_channels_;
155 int64_t kernel_h_;
156 int64_t kernel_w_;
157 int64_t stride_;
158 int64_t padding_;
159 DeviceBackend* backend_;
160 Tensor kernel_; // shape (out_channels, in_channels, kernel_h, kernel_w)
161 Tensor bias_; // shape (out_channels,)
162 Tensor kernel_grad_;
163 Tensor bias_grad_;
164 Tensor last_input_; // (N, in_channels, H, W)
165 Tensor last_im2col_; // (N, patch_size, out_h*out_w) when cached_cols_, else empty
166 bool cached_cols_ = false; // the whole batch's patches fit the workspace and were kept
167 size_t max_workspace_bytes_ = kDefaultMaxWorkspaceBytes;
168 Tensor last_pre_bias_output_; // (N, out_channels, out_h, out_w), cached for LRP
169 int64_t last_out_h_ = 0;
170 int64_t last_out_w_ = 0;
171 bool has_forwarded_ = false;
172
174 [[nodiscard]] int64_t chunk_examples(int64_t n, int64_t p, int64_t q) const;
176 [[nodiscard]] const float* patches(int64_t e0, int64_t count, Tensor& buffer) const;
177
178 [[nodiscard]] ConvGeometry geometry() const {
179 const auto s = static_cast<size_t>(stride_), p = static_cast<size_t>(padding_);
180 return {static_cast<size_t>(kernel_h_), static_cast<size_t>(kernel_w_), s, s, p, p};
181 }
182};
183
184} // namespace pulsatrix
2D convolution, batched (input/output are rank-4: N x channels x H x W) – migrated from the original ...
Definition conv2d_module.hpp:31
std::optional< DeviceType > compute_device() const override
Where this layer computes, so forward() rejects an input on another device (FND-8).
Definition conv2d_module.hpp:121
Tensor backward(const Tensor &grad_output) override
Computes gradients w.r.t. input, kernel, and bias.
std::vector< NamedParamRef > named_parameters() override
This module's trainable parameters, each with its hierarchical name – the one place a module declares...
Definition conv2d_module.hpp:115
OpType op_type() const override
Conv per charter's closed OpType set.
Definition conv2d_module.hpp:67
const Tensor & kernel() const
Definition conv2d_module.hpp:81
const Tensor & kernel_grad() const
Definition conv2d_module.hpp:83
Tensor propagate_relevance(const Tensor &relevance_out, const LRPRuleConfig &config) override
Epsilon-rule LRP relevance propagation.
void set_kernel(const std::vector< float > &values)
Vector overload for runtime-sized sources – see Tensor's own vector ctor.
const Tensor & bias_grad() const
Definition conv2d_module.hpp:84
bool supports_lrp_rule(LRPRule) const override
Implements every LRPRule.
Definition conv2d_module.hpp:113
int64_t stride() const
Step between windows along both axes.
Definition conv2d_module.hpp:50
Conv2DModule(int64_t in_channels, int64_t out_channels, int64_t kernel_h, int64_t kernel_w, DeviceBackend *backend, int64_t stride=1, int64_t padding=0)
Constructs a conv layer with zero-initialized kernel/bias.
Tensor forward_impl(const Tensor &input) override
The actual forward computation (im2col + gemm + per-channel bias add).
int64_t padding() const
Zero padding on every side, along both axes.
Definition conv2d_module.hpp:52
void set_bias(const std::vector< float > &values)
Vector overload for runtime-sized sources – see Tensor's own vector ctor.
size_t max_workspace_bytes() const
Definition conv2d_module.hpp:132
void set_bias(std::initializer_list< float > values)
Overwrites the bias buffer – test/initialization use only.
static constexpr size_t kDefaultMaxWorkspaceBytes
16 MiB. Small layers (MNIST-sized) keep a whole batch's patches; large ones work in chunks....
Definition conv2d_module.hpp:139
void set_kernel(std::initializer_list< float > values)
Overwrites the kernel buffer – test/initialization use only.
const Tensor & bias() const
Definition conv2d_module.hpp:82
void set_max_workspace_bytes(size_t bytes)
The most memory the unfolded input patches (im2col, N x C*kh*kw x out_h*out_w floats) may take (HIP-7...
Definition conv2d_module.hpp:131
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
virtual DeviceType device() const noexcept=0
Which device this backend's buffers reside on.
Base class for every layer type (LinearModule, Conv2DModule, activations, ...).
Definition module.hpp:78
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Abstract base every layer subclasses – NVI forward(), pure-virtual LRP contract.
Definition acquisition_functions.hpp:16
OpType
The op-type tag a Node carries. Charter Part 2 §3: nodes are tagged by a small closed set of op types...
Definition op_type.hpp:19
LRPRule
The LRP rule family a module applies. Semantics follow Zennit 1.0.0 exactly (Anders et al....
Definition lrp_rule_config.hpp:21
Window geometry for DeviceBackend::im2col / col2im_add: kernel size, stride and zero padding per axis...
Definition device_backend.hpp:173
Configuration for LRP relevance propagation: which rule a module applies and its hyperparameters....
Definition lrp_rule_config.hpp:72