-
Notifications
You must be signed in to change notification settings - Fork 9
Add tests #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add tests #179
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace ams | ||
| { | ||
|
|
||
| /// Field-to-column mapping for layout transformations. | ||
| struct IndexMap { | ||
| struct FieldInfo { | ||
| std::string Name; | ||
|
|
||
| enum class Kind { Input, InOut, Output }; | ||
| Kind EKind; | ||
|
|
||
| int64_t Offset; ///< Starting column in the concatenated tensor | ||
| int64_t Cols; ///< Number of columns this field covers | ||
| }; | ||
|
|
||
| std::vector<FieldInfo> Fields; | ||
| }; | ||
|
|
||
| } // namespace ams | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| #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"); | ||
|
|
||
| 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 {}; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace ams |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.