pulsatrix 1.1.0
Loading...
Searching...
No Matches
saliency.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <stdexcept>
9#include <string>
10#include <vector>
11
12#include "pulsatrix/assert.hpp"
17#include "pulsatrix/tensor.hpp"
18
19namespace pulsatrix {
20
30class Saliency {
31public:
33 [[nodiscard]] static const ExplainerCapabilities& capabilities() { return ExplainerCapabilitiesOf("Saliency"); }
34
53 [[nodiscard]] Attribution explain(ExplainerContext& ctx, const Tensor& input, int64_t target_index,
54 DeviceBackend* backend) const {
55 Tensor output = ctx.forward_pass(input);
56 // External boundary (Mission 2, finding 15 systemic sweep; rank check added by
57 // campaign_exai_dl_library_batch_dimension_support) -- escalated from
58 // PULSATRIX_ASSERT-only.
59 if (output.rank() != 2) {
60 throw std::invalid_argument("Saliency::explain: network output must be rank-2 (N, num_classes)");
61 }
62 if (target_index < 0 || target_index >= output.shape().dim(1)) {
63 throw std::invalid_argument("Saliency::explain: target_index out of range");
64 }
65 int64_t N = output.shape().dim(0);
66 int64_t C = output.shape().dim(1);
67
68 std::vector<float> seed_values(static_cast<size_t>(output.numel()), 0.0f);
69 for (int64_t n = 0; n < N; ++n) {
70 seed_values[static_cast<size_t>(n * C + target_index)] = 1.0f;
71 }
72 Tensor seed(output.shape(), explainer_detail::backend_beside(output, backend), seed_values,
73 output.device());
74
75 Tensor grad = ctx.backward_pass(seed);
76
77 // Attribution has no default constructor -- Tensor (its values member) doesn't
78 // either, by design (Phase 0). Aggregate-initialize directly instead.
79 return Attribution{"saliency", std::move(grad), {{"target_index", std::to_string(target_index)}}};
80 }
81};
82
83} // namespace pulsatrix
PULSATRIX_ASSERT – debug-only invariant check for programmer errors, distinct from throw (used for ca...
First-class explanation result type – values, method, and metadata together.
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
Wraps an ordered chain of Modules, running them via Module::forward_traced to build a real Computatio...
Definition explainer_context.hpp:67
Tensor backward_pass(const Tensor &output_grad)
Runs Autograd::backward from the most recent forward_pass()'s output node.
Definition explainer_context.hpp:207
Tensor forward_pass(const Tensor &input)
Runs the full module chain forward, building a fresh graph and caching every node's activation value ...
Definition explainer_context.hpp:95
Raw-gradient saliency: d(output[target_index])/d(input), computed by seeding ExplainerContext::backwa...
Definition saliency.hpp:30
Attribution explain(ExplainerContext &ctx, const Tensor &input, int64_t target_index, DeviceBackend *backend) const
Computes the saliency map for one output index.
Definition saliency.hpp:53
static const ExplainerCapabilities & capabilities()
What it answers and needs: its ExplainerCatalog() entry.
Definition saliency.hpp:33
int64_t dim(size_t index) const
Size of a single dimension.
Definition shape.hpp:93
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
DeviceType device() const
Which device this tensor's buffer conceptually resides on.
Definition tensor.hpp:122
int64_t rank() const
Number of dimensions – shape().rank().
Definition tensor.hpp:119
int64_t numel() const
Total element count – shape().numel().
Definition tensor.hpp:116
const Shape & shape() const
This tensor's shape.
Definition tensor.hpp:113
Abstract interface isolating vendor-specific memory/compute operations from Tensor/ComputationGraph.
What each explainer answers: the nine question categories (Liao, Gruen and Miller,...
Stable interface every explainer gets, regardless of type (charter Part 2 SS2).
DeviceBackend * backend_beside(const Tensor &like, DeviceBackend *backend)
The backend to allocate a tensor through that must live beside like.
Definition attribution.hpp:49
Definition acquisition_functions.hpp:16
const ExplainerCapabilities & ExplainerCapabilitiesOf(std::string_view name)
The catalog entry named name.
An explanation result: the raw attribution values, the method that produced them, and any relevant me...
Definition attribution.hpp:23
One explainer's declaration: what it answers and what it needs.
Definition explainer_capabilities.hpp:99
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).