-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathpointwise_layout_transform.hpp
More file actions
169 lines (146 loc) · 5.22 KB
/
pointwise_layout_transform.hpp
File metadata and controls
169 lines (146 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#pragma once
#include <ATen/ATen.h>
#include <torch/script.h>
#include <cstdint>
#include <optional>
#include <vector>
#include "wf/index_map.hpp"
#include "wf/layout_transform.hpp"
#include "wf/tensor_bundle.hpp"
namespace ams
{
/// PointwiseConcatTransform:
///
/// Converts Inputs + InOuts into a single matrix [N, SUM(K_i)] where:
/// - N = batch size (outer dim)
/// - K_i = flattened size of each tensor field except the batch dimension
///
/// Supports:
/// ✔ Scalar fields (shape [N])
/// ✔ Multi-channel fields (shape [N, K])
/// ✔ Arbitrary shapes [N, ...] → flattened to [N, M]
/// ✔ Prediction-only models
/// ✔ Uncertainty-aware models returning (pred, uncertainty)
///
/// Produces IndexMap for both pack() and unpack().
class PointwiseConcatTransform : public LayoutTransform
{
public:
const char* name() const noexcept override
{
return "PointwiseConcatTransform";
}
// ------------------------------------------------------------------
// PACK
// ------------------------------------------------------------------
AMSExpected<IndexMap> pack(const TensorBundle& Inputs,
const TensorBundle& InOuts,
at::Tensor& ModelInput) override
{
IndexMap map;
std::vector<at::Tensor> cols;
int total_cols{0};
if (auto st = process(
Inputs, IndexMap::FieldInfo::Kind::Input, map, cols, total_cols);
!st)
return tl::unexpected(st.error());
if (auto st = process(
InOuts, IndexMap::FieldInfo::Kind::InOut, map, cols, total_cols);
!st)
return tl::unexpected(st.error());
if (total_cols <= 0) {
return AMS_MAKE_ERROR(AMSErrorType::InvalidShapes,
fmt::format("PointwiseConcatTransform expected at "
"least a single dimension in pack"));
}
// Concatenate horizontally
ModelInput = at::cat(cols, /*dim=*/1);
return map;
}
// ------------------------------------------------------------------
// UNPACK
// ------------------------------------------------------------------
AMSStatus unpack(const torch::jit::IValue& ModelOutput,
TensorBundle& Outs,
TensorBundle& InOuts,
std::optional<at::Tensor>& Uncertainties) override
{
at::Tensor ModelOut;
at::Tensor Uncertainty;
bool has_uncertainty = false;
// --------------------------------------------
// Case 1: Single tensor prediction
// --------------------------------------------
if (ModelOutput.isTensor()) {
ModelOut = ModelOutput.toTensor();
}
// --------------------------------------------
// Case 2: Tuple(pred, uncertainty)
// --------------------------------------------
else if (ModelOutput.isTuple()) {
auto tup = ModelOutput.toTuple();
if (tup->elements().size() != 2)
return AMS_MAKE_ERROR(AMSErrorType::InvalidShapes,
"PointwiseConcatTransform: expected "
"tuple(pred,uncertainty).");
ModelOut = tup->elements()[0].toTensor();
Uncertainty = tup->elements()[1].toTensor();
has_uncertainty = true;
} else {
return AMS_MAKE_ERROR(AMSErrorType::InvalidShapes,
"PointwiseConcatTransform: ModelOutput must be "
"tensor or "
"tuple.");
}
// Uncertainties
if (has_uncertainty) {
Uncertainties = Uncertainty;
} else {
Uncertainties.reset();
}
if (ModelOut.size(1) != Outs.size() + InOuts.size())
return AMS_MAKE_ERROR(AMSErrorType::InvalidShapes,
"Expected the output size to match the Application "
"output dimensions");
std::vector<at::Tensor> Slices{static_cast<size_t>(ModelOut.size(1))};
int k = 0;
for (; k < Outs.size(); ++k) {
Outs[k].tensor =
ModelOut.narrow(/*dim=*/1, /*start=*/k, /*length=*/1).squeeze();
}
for (int i = 0; i < InOuts.size(); ++k, ++i) {
InOuts[i].tensor =
ModelOut.narrow(/*dim=*/1, /*start=*/k, /*length=*/1).squeeze();
}
return {};
}
private:
AMSStatus process(const TensorBundle& tb,
IndexMap::FieldInfo::Kind kind,
IndexMap& map,
std::vector<at::Tensor>& cols,
int& total_cols)
{
for (size_t i = 0; i < tb.size(); i++) {
const auto& item = tb.items[i];
at::Tensor t = item.tensor;
if (t.dim() < 1)
return AMS_MAKE_ERROR(AMSErrorType::InvalidShapes,
fmt::format("PointwiseConcatTransform for "
"field {} must have at least 1 "
"dimension",
item.name));
int64_t N = t.size(0);
// Flatten everything except outer dimension.
at::Tensor flat = t.reshape({N, -1});
int64_t M = flat.size(1);
int64_t offset = total_cols;
total_cols += M;
map.Fields.push_back({item.name, kind, offset, M});
cols.push_back(flat);
}
return {};
}
IndexMap last_pack_map_;
};
} // namespace ams